Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: Advanced Color Imaging on the Mac OS /
Chapter 2 - Color Picker Manager / Writing Your Own Color Pickers


Handling Events for Your Color Picker

The Color Picker Manager sends the kDrawPicker request in response to an update event. Your color picker responds to this code by redrawing your color picker, as illustrated in Listing 2-20.

Listing 2-20 Redrawing a color picker

pascal ComponentResult MyDrawPicker(PickerStorageHndl storage)
{
   if((*storage)->visible)  
   {
      MyDrawColorList(storage);
      MyDrawColorEditor(storage,true);
   }
   return noErr;
}
The Color Picker Manager calls the Event Manager function BeginUpdate before sending the kDrawPicker request code and the Event Manager function EndUpdate after sending the kDrawPicker request code.

The Color Picker Manager sends the kEvent request code so that your color picker can handle events that the Dialog Manager does not handle. A color picker responds to the kEvent request code by performing any event processing in addition to or instead of that normally performed by the Dialog Manager. Listing 2-21 illustrates how a color picker can perform such event processing.

Listing 2-21 Responding to events before handing them to the Dialog Manager

pascal ComponentResult MyDoEvent(PickerStorageHndl storage, 
                         EventData *data)
{
   OSErr err = noErr;
   /* initialize so events are not filtered */
   data->handled = false;
   data->action = kDidNothing;
   if(data->event)  
   {
      switch(data->event->what)  
      {
         case nullEvent:
            DoIdle(storage,data);
            break;
         case mouseDown:
            err = DoMouseDown(storage,data);
            break;
         case keyDown:
         case autoKey:
            err = DoKeyDown(storage,data);
            break;
         case keyUp:
            err = DoKeyUp(storage,data);
            break;
         case activateEvt:
            if(data->event->modifiers & activeFlag)
               ActivatePicker(storage);
            else
               DeactivatePicker(storage);
            break;
      }  
   }
   return err;
}
The Color Picker Manager sends the kItemHit request code to inform your color picker of an event in one of its items. In turn, your color picker responds to the event for the item reported in the itemHit field of an ItemHitData record, which is described in "Color Picker Manager Reference" (page 2-5) in Advanced Color Imaging Reference. Listing 2-22 illustrates how a color picker can perform such event processing.

Listing 2-22 Responding to events in color picker items

pascal ComponentResult MyItemHit(PickerStorageHndl storage, 
                         ItemHitData *data)
{
   #pragma unused(iMod)
   Handle theItem;
   short iType;
   Rect iBox;

   OSErr err = noErr;
   data->action = kDidNothing;
   GetDialogItem((*storage)->port, 
                  (*storage)->baseItem + data->itemHit, &iType, 
                  &theItem, &iBox);
   switch(data->itemHit)  
   {
      case iRedText:
      case iGreenText:
      case iBlueText:
         /* don't udpate everything as the user types each key; 
            update only after the user leaves an edit field */
         if(data->iMod != kKeyDown && data->itemHit != kMouseDown)
            CheckCurrentWorld(storage,data->itemHit);
         break;
      case iOrigColor:
         err = DoListClick(storage,data);
         break;
      case iNewColor:
         break;
   }  
   return err;
}
The Color Picker Manager sends the kEdit request code to inform your color picker that the user has chosen one of the edit commands from the Edit menu (or typed its keyboard equivalent). If your color picker, rather than the Dialog Manager, needs to perform the editing command, your color picker should do so in response to this request code. For example, because the Dialog Manager does not perform the Undo command, your MyDoEdit function can instead. The editing command is passed to your function in the field theEdit of the EditData record pointed to in the data parameter. Listing 2-23 illustrates how a color picker can respond to this result code.

Listing 2-23 Handling events in the color picker's Edit menu

pascal ComponentResult MyDoEdit(PickerStorageHndl storage, 
                        EditData *data)
{
   RGBColor rgb;

   switch(data->theEdit)  
   {
      default:
         /* default behavior is appropriate */
         data->action = kDidNothing;
         data->handled = false;
         break;
      case kUndo:
         rgb = (*storage)->lastRGB;
         (*storage)->lastRGB = (*storage)->color.color.rgb;
         (*storage)->color.color.rgb = rgb;
         /* update the other internal data and then redraw */
         MySetSelectionColor(storage);
         MyUpdateColorText(storage);
         MyDrawColorRects(storage, false);
         data->action = kColorChanged;
         data->handled = true;
         break;
   }
   return noErr;
}

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 NOV 1996