TweetFollow Us on Twitter

MACINTOSH C
MACINTOSH C: A Hobbyist's Guide To Programming the Mac OS in C
Version 2.3

© 2000 K. J. Bricknell

Go to Contents

(Chapter 18)

SCRAP

A link to the associated demonstration program listing is at the bottom of this page




The Scrap Manager and the Desk Scrap

Introduction

For each open application, the Scrap Manager maintains a storage area to hold the last data cut or copied by the user. This area is called the scrap or, sometimes, the desk scrap. The desk scrap can reside in memory or on disk. All applications which support cut, copy, and paste operations write data to, and read data from, the desk scrap. Typically, that data relates to text, graphics, sounds, or movies.

Your application specifies the format, or formats, in which data is written to, and read from, the desk scrap. Your application should write that data using the so-called standard formats (in addition to any other format it might specify), since this ensures that a user can copy and paste data between documents created by your application and other applications as well as within and between documents created by your application. The ultimate aim is to allow the user to:

  • Copy and paste data within a document created by your application.

  • Copy and paste data between different documents created by your application.

  • Copy and paste data between documents created by your application and documents created by other applications.

Scrap Data Formats

Standard Formats

Your application must be capable of writing at least one of the following standard formats to the scrap and should be capable of reading both:

  • 'TEXT', that is, a series of ASCII characters.

  • 'PICT', that is, a QuickDraw picture.

Optional Formats

Your application may also choose to support the following optional scrap format types:

  • 'snd ', that is, a series of bytes which define a sound, and which have the same format as a 'snd ' resource.

  • 'movv', that is, a series of bytes which define a movie, and which have the same format as a 'movv' resource.

  • 'styl', that is, a series of bytes which have the same format as a TextEdit 'styl' resource, and which describe styled text data.

Private Formats

It is also possible for your application to use its own private format, or formats, but this should be in addition to one of the standard formats.

Location of the Desk Scrap and Getting Information About the Scrap

Location of the Desk Scrap

System software allocates space in each application's heap for the desk scrap and allocates a handle to reference the scrap. The system global variable Scraphandle contains a handle to the desk scrap of the current process.

When system software launches an application, it copies the data from the scrap of the previously active application into the application heap of the newly active application. If the scrap is too large to fit in the application's application heap, system software copies the scrap to disk and sets the value of the handle to the scrap in the application's heap to NULL to indicate that the scrap is on disk.

Getting Information About the Desk Scrap

To get information about the scrap, you can use InfoScrap, which returns a pointer to a scrap information structure, which is defined by the data type ScrapStuff. The information in the scrap information structure includes:

  • The size, in bytes, of the scrap.

  • A handle to the scrap (if it is in memory).

  • The location of the scrap (memory or disk).

  • The filename of the scrap when it is on disk.

Using the Desk Scrap - Implementing Edit Menu Commands

You use the Edit menu Cut, Copy, and Paste commands to implement cutting, copying, and pasting of data within or between documents. The following are the actions your application should perform to support these three commands:

Edit Command Actions Performed by Your Application
Cut If there is a current selection range, copy the data in the selection range to the desk scrap and remove the data from the document.
Copy If there is a current selection range, copy the data in the selection range to the desk scrap.
Paste Read the desk scrap and insert the data (if any) at the insertion point, replacing any current selection.

Note:The insertion point in a text document is represented by the blinking vertical bar known as the caret. There is a close relationship between the selection range and the insertion point in that the insertion point is, in effect, an empty selection range.

Note that, if your application implements a Clear command, it should remove the data in the current selection range but should not save the data to the desk scrap.

Cut and Copy - Putting Data in the Scrap

A typical approach to implementing the Cut and Copy commands is as follows:

  • Determine whether the frontmost window is a document window or a dialog box.

  • If the frontmost window is a document window:

    • Call an application-defined function which determines whether the current selection contains text or whether it contains graphics.

    • Get a pointer to the selection range data and get the selection length.

    • Call ZeroScrap to purge the current contents of the desk scrap.

    • Call PutScrap to write the data to the scrap, specifying 'TEXT' or 'PICT', as appropriate, as the format type.

    • If the command was the Cut command, delete the selection from the current document.

  • If the frontmost window is a dialog box, use the Dialog Manager functions DialogCut or DialogCopy, as appropriate, to write the selected data to the scrap.

Paste - Getting Data From the Scrap

When the user chooses the Paste command, your application should paste the data last cut or copied by the user. Your application gets the data to paste by reading the data from the desk scrap.

When you read the data from the scrap, your application should request the data in the application's preferred format type. If your application determines that that format does not exist in the scrap, it should then request the data in another format. If your application does not have a preferred format type, it should read each format type that your application supports.

If you request a scrap format that is not in the scrap, the Scrap Manager uses the Translation Manager to convert any one of the scrap format types currently in the scrap into the scrap format requested by your application. The Translation Manager looks in the Extensions folder for a translator that can perform one of these translations. If such a translator is available, the Translation Manager uses the translator to translate the data in the scrap into the requested format type.

A typical approach, for an application that prefers a data format other than 'TEXT' or 'PICT' as its first preference, is as follows:

  • Determine whether the frontmost window is a document window or a dialog box.

  • If the frontmost window is a document window:

    • Call GetScrap to search the scrap for the preferred format type. (If you specify a NULL handle as the location to which to return the data, GetScrap does not return the data but does return as its function result the number of bytes (if any) of data in the specified format that exists in the scrap. Thus, if GetScrap returns a non-positive value, data of that format type does not exist.)

    • If data of the specified format does exist, allocate a handle to hold the data from the scrap and call GetScrap again to read in the data in that format. (GetScrap automatically resizes the handle passed to it to the required size.)

    • If the scrap does not contain data of the preferred format type, repeat the above process specifying 'TEXT' as the format type in the calls to GetScrap. If this is not successful, repeat the process again specifying 'PICT' as the format type.

    • Paste the data to the current document.

  • If the frontmost window is a dialog box, use the Dialog Manager function DialogPaste to paste the text from the scrap in the dialog.

Example

Fig 1 illustrates two cases, both of which deal with a user copying a picture consisting of text from a source document created by one application to a destination document created by another application.

In the first case, the source application has chosen to write only the 'PICT' format to the desk scrap, and the destination application has pasted the data to its document in that format.

In the second case, the source application has chosen to write both the 'PICT' and the 'TEXT' formats to the desk scrap, and the destination application has chosen the 'TEXT' format as the preferred format for the paste. The data is thus inserted into the document as editable text.

(Specifying formats)

The Clipboard

The Clipboard refers to what the user views as residing in the scrap. Your application can provide a Show Clipboard command which, when chosen, should show a window which displays the current contents of the desk scrap. Such a window is known as a Clipboard window. The Show Clipboard command should be toggled with a Hide Clipboard command to allow the user to hide the Clipboard window when required.

Although multiple scrap format types can reside in the desk scrap, applications which support a Clipboard window typically display the data in one format only.

Transferring the Desk Scrap to Disk

Although the scrap is usually located in memory, your application can write the contents of the scrap in memory to a scrap file using UnloadScrap. You should do this only if memory is not large enough to hold the data you need to write to the scrap. After writing the contents of the scrap to disk, UnloadScrap releases the memory previously occupied by the scrap. Thereafter, any operations your application performs on data in the scrap affect the scrap as stored in the scrap file on disk. You can use LoadScrap to read the contents of the scrap file back into memory.

Private Scrap

As an alternative to writing to and reading from the desk scrap whenever the user cuts, copies and pastes data, your application can choose to use its own private scrap. An application which uses a private scrap copies data to its private scrap when the user chooses the Cut or Copy command and pastes data from the private scrap when the user chooses the Paste command.

In addition, an application which uses a private scrap must take the following actions on receipt of suspend and resume events:

  • Suspend Event. On receipt of a suspend event, the data from the private scrap must be copied to the desk scrap. If your application supports the Show Clipboard command, the Clipboard window must be hidden if it is currently showing (because the contents of the scrap may change while the application yields time to another application).

  • Resume Event. On receipt of a resume event, your application must determine if the data in the desk scrap has changed since the previous suspend event and, if so, copy the data from the desk scrap to its private scrap either immediately or when the user next chooses the Paste command. In addition, if your application supports the Show Clipboard command, and if the data in the desk scrap has changed, your application must update the contents of the Clipboard window.
Note that, when the contents of the desk scrap have changed since the last suspend event, system software sets the convertClipboardFlag bit in the message field of the resume event structure.

The process of copying data between an application's document, an application's private scrap, and the desk scrap in response to suspend and resume events is shown diagrammatically at Fig 2.

(Private scrap)

Copying Data Between Private Scrap and the Desk Scrap

A typical approach to copying data between the private scrap and the desk scrap is as follows:

  • Resume Event. When a resume event is received, and a check indicates that the contents of the desk scrap have changed since the last suspend event:

    • Call GetScrap, with NULL passed as the destHandle parameter, to determine if the scrap contains data in the 'PICT' format type. If data of that format type exists:

      • Allocate a handle to hold the data from the scrap and call GetScrap again to read in the data.

      • Call an application-defined function to copy the data to the private scrap.

      • Dispose of the handle.

    • If data of the 'PICT' format type does not exist in the scrap, repeat this process specifying 'TEXT' as the data format type.

  • Suspend Event. When a suspend event is received:

    • Call an application-defined function which determines if there is any data in the private scrap. If there is data in the private scrap, call ZeroScrap to empty the desk scrap.

    • Create a non-relocatable block to receive the private scrap data.

    • For each appropriate data format type:

      • Determine if data in that format exists in the private scrap.

      • If data in that format type exists in the private scrap, call an application-defined function which gets the data from the private scrap into the nonrelocatable block. Then call PutScrap to copy the data from the nonrelocatable block to the scrap.

    • Dispose of the nonrelocatable block.

TextEdit, Dialog Boxes, and Scrap

TextEdit and Scrap

TextEdit is a collection of functions and data structures which you can use to provide your application with basic text editing capabilities.

If your application uses TextEdit in its windows, be aware that TextEdit maintains its own private scrap. Accordingly:

  • PutScrap is not used and the special TextEdit functions TECut, TECopy, and TEToScrap are used in the processes of cutting text from the document and copying text to the TextEdit private scrap and to the desk scrap.

  • GetScrap is not used and the special TextEdit functions TEPaste, TEStylePaste, and TEFromScrap are used in the processes of pasting text from the TextEdit private scrap and copying text from the desk scrap to the TextEdit private scrap.
Chapter 19 - Text and TextEdit describes TextEdit, including the TextEdit private scrap and the TextEdit scrap-related functions.

Dialog Boxes and Scrap

Dialog boxes may contain editable text items, and the Dialog Manager uses TextEdit to perform the editing operations within those editable text items.

You can use the Dialog Manager to handle most editing operations within dialog boxes. The Dialog Manager functions DialogCut, DialogCopy, and DialogPaste may be used to implement Cut, Copy and Paste commands within editable text items in dialog boxes. (See the demonstration program at Chapter 8 - Dialogs and Alerts.)

TextEdit's private scrap facilitates the copying and pasting of data between dialog boxes. However, your application must ensure that the user can copy and paste data between your application's dialog boxes and its document windows. If your application uses TextEdit for all editing operations within its document windows, this is easily achieved because TextEdit's TECut, TECopy, TEPaste, and TEStylePaste functions and the Dialog Manager's DialogCut, DialogCopy, and DialogPaste functions all use TextEdit's private scrap.

If your application does not use TextEdit for text handling within its document windows, and if it uses a private scrap, then, when the user activates a dialog box, you should copy any data in your private scrap to TextEdit's private scrap. Also, when a document window becomes active, and there is data in TextEdit's private scrap, that data should be copied to your application's private scrap (or to the desk scrap if your application does not use a private scrap).

Similarly, before displaying the Standard File Package's save dialog box, your application should copy any text data in its private scrap to the desk scrap. The Standard File Package reads the data from the desk scrap whenever the user chooses an editing operation and a standard file dialog box is active. Accordingly, your application needs to put the text data (if any) from the last cut or copy in the desk scrap before calling StandardPutFile.



Main Scrap Manager Data Types and Functions

Data Types

Scrap Information Structure

struct ScrapStuff
{
  SInt32    scrapSize;    // Size of scrap in bytes.
  Handle    scrapHandle;  // Handle to scrap.
  SInt16    scrapCount;   // Indicates whether contents of scrap have changed.
  SInt16    scrapState;   // Indicates state and location of scrap
  StringPtr scrapName;    // Filename of scrap.
};
typedef struct ScrapStuff ScrapStuff;
typedef ScrapStuff *PScrapStuff;
typedef ScrapStuff *ScrapStuffPtr;

Functions

Getting Information About the Scrap

ScrapStuffPtr  InfoScrap(void);

Writing Information to the Scrap

SInt32  ZeroScrap(void);
SInt32  PutScrap(long length,ResType theType,void *source);

Reading Information From the Scrap

SInt32  GetScrap(Handle hDest,ResType theType,long *offset);

Transferring the Scrap Between Memory and Disk

SInt32  UnloadScrap(void);
SInt32  LoadScrap(void);

Go to Demo

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Dispatch is bringing its superhero shena...
If you were anywhere online last year, then you would have no doubt heard of Dispatch, and been flooded with videos of it. With its star-studded cast and unrelenting humour, it fast became a hit, racking up over four million downloads. And it is a... | Read more »
5 reasons to sink your teeth into VAMPIR...
On October 28, Netmarble will globally launch VAMPIR, its latest and most macabre MMORPG, plunging players into a Gothic world of vampires, sorcerers and cursed creatures of the night. Built around blood, fear and large scale battles, the MMORPG... | Read more »
Gundam Blitz Order opens registration fo...
It would appear that video game developers are delving into my childhood and making idle games from my favourite TV shows. First, Bandai completely whiffed with Digimon Up, perhaps the single biggest miss of a beloved franchise in recent memory. Now... | Read more »
The Traitors Interactive Game lets you l...
With the second season of The Celebrity Traitors in full swing, the world is once more enthralled by just how good a liar our heroes are. I don’t watch it, but I hope James Blunt wins. The premise does intrigue me, however, and thanks to Fusebox... | Read more »
Dissidia Duellum Final Fantasy brings in...
For all his successful manipulations and successes through Final Fantasy 9, I never really liked Kuja. And it's not like I dislike him; more I nothing him. But that's just me; the vast majority of the fandom adore him, and they are in luck, for Kuja... | Read more »
Combo Quest (Games)
Combo Quest 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: Combo Quest is an epic, time tap role-playing adventure. In this unique masterpiece, you are a knight on a heroic quest to retrieve... | Read more »
Hero Emblems (Games)
Hero Emblems 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: ** 25% OFF for a limited time to celebrate the release ** ** Note for iPhone 6 user: If it doesn't run fullscreen on your device... | Read more »
Puzzle Blitz (Games)
Puzzle Blitz 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Puzzle Blitz is a frantic puzzle solving race against the clock! Solve as many puzzles as you can, before time runs out! You have... | Read more »
Sky Patrol (Games)
Sky Patrol 1.0.1 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0.1 (iTunes) Description: 'Strategic Twist On The Classic Shooter Genre' - Indie Game Mag... | Read more »
The Princess Bride - The Official Game...
The Princess Bride - The Official Game 1.1 Device: iOS Universal Category: Games Price: $3.99, Version: 1.1 (iTunes) Description: An epic game based on the beloved classic movie? Inconceivable! Play the world of The Princess Bride... | Read more »

Price Scanner via MacPrices.net

Amazon Offers 14″ M5 Pro MacBook Pro for $206...
Amazon is offering another opportunity to save on Apple’s 14-inch M5 Pro MacBook Pro, with new offers on the Space Black configuration with 24GB RAM and a 1TB SSD starting at just $2069.10. Apple... Read more
Amazon Takes $900 Off 16″ M5 Max MacBook Pro,...
Amazon has expanded its latest sale on Apple’s 16-inch M5 Max MacBook Pro, dropping the Space Black configuration with 36GB RAM and a 2TB SSD to just $3499. Apple sells this configuration for $4399,... Read more
B&H Slashes Up to $1400 Off M4 Pro Mac mi...
B&H is offering new clearance discounts on select previous-generation Apple Mac minis, with savings of up to $1400 on high-end M4 Pro configurations. The sale includes the standard M4 Mac mini... Read more
B&H Slashes $400 Off 14″ M5 Pro MacBook P...
B&H has launched a substantial new discount on Apple’s 14-inch M5 Pro MacBook Pro, taking $400 off the Space Black configuration with 24GB RAM and a 1TB SSD. Regularly priced at $2499, the 14-... Read more
Take $50 off iPad at Amazon, prices start at...
Amazon again has 11th-generation A16 WiFi iPads in stock on sale for $50 off MSRP, starting at $399. Shipping is free: – 11″ 11th-generation 128GB WiFi iPads: $399 $50 off MSRP – 11″ 11th-generation... Read more
Amazon Lowers 13″ M5 MacBook Air to $1149, No...
Amazon has increased its discounts on Apple’s 13-inch M5 MacBook Air, dropping the standard 16GB/512GB configuration to just $1149. Apple sells this configuration for $1299, so Amazon’s latest price... Read more
Amazon and B&H Slash Mac mini Prices, M6...
Amazon and B&H are offering new discounts on Apple’s M6 and M5 Pro Mac minis, with savings reaching $149.01 and prices starting at just $779. Configuration Apple MSRP Retailer Sale Price Savings... Read more
Amazon Takes Up to $150 Off 15″ M5 MacBook Ai...
Amazon has launched new discounts on Apple’s 15-inch M5 MacBook Airs, with savings of up to $150 and prices starting at just $1399. Configuration Apple MSRP Amazon Price Savings 15″ M5 16GB/512GB $... Read more
Amazon Takes Up to $150 Off 13″ M5 MacBook Ai...
Amazon has launched a new round of discounts on Apple’s 13-inch M5 MacBook Airs, with savings of up to $150 and prices starting at just $1199. Configuration Apple MSRP Amazon Price Savings 13″ M5... Read more
Verizon Launches iPhone Duo Pre-Orders With N...
Verizon has opened pre-orders for Apple’s new iPhone Duo, the company’s first foldable iPhone, with prices starting at $1999.99 and a new no-trade-in promotion available through its Simplicity Plan.... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.