TweetFollow Us on Twitter

Color Selector
Volume Number:9
Issue Number:2
Column Tag:TCL Workshop

Related Info: Palette Manager Window Manager Color Manager
Color Quickdraw

A Floating Color Selection
Window for TCL

Creating a floating pallette in TCL without using the Palette Manager

By Paul M. Hyman, Hughes Aircraft Co., Fullerton, CA

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

About the author

Paul Hyman is a Systems Engineer with Hughes Aircraft Company. He has developed software on a variety of different systems including Macintosh, Unix/X Windows, and several Hughes-built computers no one has ever heard of.

The Think Class Library provides a useful object oriented application framework for users of Think C. It provides much of the standard Macintosh application functionality, including scrolling windows, menus, and standard AppleEvent handling if you’re using System 7, as well as some not-so-standard functionality such as tear-off menus and floating windows.

This article describes some new classes which implement a floating color selection window, which seems to be the way most color drawing and painting programs allow users to select colors these days. This window might sometimes be referred to as a color palette, but I will call it a color selection window to avoid confusion with the palette manager. These classes were developed using Think C 5.0.2 with Think Class Library version 1.1.2.

The color selection window does the following. It shows the available colors in the palette assigned to the active document (up to 256), and highlights the current selection. The size of 256 was chosen since it is appropriate for 8 bit color displays and is used in many programs. You could modify it to be some other size if you want. The window allows the user to select a new color by clicking on it and allows the application to change the currently selected color by sending it a message. It can also inform the application of the current color in response to a message and will notify the current document when the user selects a new color. The color selector is designed to be used as a global object; there is only one and it is always present, even though its window may be hidden if the user clicks in its close box.

The sample program which is shown illustrates how to use this color selection window. It also shows how to maintain a list of objects which the user has created. Basically, it allows the user to create colored boxes (rectangles) in a window and recognizes when one of the boxes has been selected by being clicked on. Boxes are created with the currently selected color. When an existing box is clicked on, its color is selected in the floating window.

As you will unoubtedly notice, I didn’t follow the TCL convention of putting C in front of all class names. This distinguishes the new classes from the classes provided with the TCL.

How to Use the Color Selector

If you’re starting a new program, you might want to use the color selector sample program as a starting point. Copy the source files from the Starter application (which comes with Think C) into your project folder. These are needed since they serve as superclasses for some of the sample application’s classes. If you have the MacTutor disk, you will have the project and resource files and will be ready to go. If not, copy the project and resource files from the Starter application and add in the classes from the color selector sample application.

In order to make use of the color selector window in an existing program, do the following:

1. Put the colordirector, colorselector, and selectorwindow classes into your project.

2. In your document class’ DoCommand method, add a case for cmdColorSelect, and #include colorselector.h (this is where cmdColorSelect is defined). This will enable your document to receive a notice when the user selects a new color in the color selection window. See the code in the mydoc class for how to handle this message.

3. Copy the code from the Activate method of docwindow into your window class’ code. This Activate method sends a notification to the color selector window informing it that a new window has been activated and that its palette should be displayed.

4. Override CApplication’s MakeDesktop method in your application class to create a floating window desktop. See the MakeDesktop method in the myapp class to see how to do this.

5. Copy the code from myapp's Imyapp method into your applications initialization method. This code creates the color selector objects.

You will need to add a few resources to your resource file. You will need to add the windoid WDEF which is supplied with the TCL. You will also need a pltt resource with the same ID as your document window (500 in the case of the sample program described below) unless you build your palette in the code. (The use of palettes is explained more in the next section.) In addition, the sample program has a menu item, “Show Color Selector #1025” (the 1025 is the TCL command number), and ALRT 130 for the About box.

The sample program.

The sample program presented with this article allows the user to select a color from the selection window and then create a box of that color in the document window by clicking in some empty spot and dragging down and to the right. Clicking on an existing box causes its color to be selected in the selection window. The program maintains a list of boxes so that they can be drawn when the window is scrolled and so that it can detect when a mouse click occurred in an existing box.

To use the color selection window, each document window must have a color palette associated with it. The easiest way to create a palette is to use Resedit and create a pltt resource with the same id as the id of the WIND resource for the window. Since this program uses a WIND resource of ID 500 to build its document windows, putting in a pltt resource of ID 500 automatically associates the palette with the window. Just for illustrative purposes, the program shows how to create a palette in the code. The first document window (the one that appears when the program starts up) uses the palette from the pltt resource. The second window, which is created when New is selected from the File menu, has a palette of shades of red, while the third window, which is created when New is selected the second time, has a palette of shades of green.

When using the TCL, every program must have an application class. For this demo program, I created a class called myapp, which is a subclass of CStarterApp, the application class that comes with the Starter demo program. If the application has documents, it must also have a document class, as well as a pane class in which to do its drawing. Just as with the application class, I created a document class, mydoc, which is a subclass of CStarterDoc, and a pane class, mypane, which is a subclass of CStarterPane. I could have just modified the classes in the Starter demo rather than creating subclasses, but this would have made it difficult to explain the code which is necessary to use the color selector. By creating subclasses, the new code is isolated in the new classes. I also created a new subclass of CWindow, which I called docwindow, since I needed to modify the window behavior slightly.

In addition to the application, document, and pane classes, there is a class called genericbox, which manages a box on the screen, and boxlist, which keeps a list of all the boxes belonging to a document. The class genericbox is so named because if this program were to be expanded into a drawing program, genericbox would probably be used as an abstract class to give general behavior to boxes on the screen and would be overridden to create specific shapes.

In the following paragraphs, the methods in these new classes and what they do to support the color selection window are briefly described. For more details, refer to the comments in the code.

The application class, myapp:

Imyapp - This is the initialization method. It creates the colordirector object and sends it an initialize message.

DoCommand - This method processes menu selections. In the case of the “Show Color Selector” menu item, this method sends a Show message to the color selector’s window. (The window will be hidden if the user clicked in its close box and this is how it gets made visible again.)

UpdateMenus - This method is responsible for enabling appropriate menu items before a menu is displayed. It simply enables the “Show Color Selector” menu item. It could be made smarter and only enable it if the window is hidden.

CreateDocument - This is basically the same as the CreateDocument method in the CStarterApp class. The only difference is that it creates a mydoc document instead of a CStarterDoc document.

MakeDesktop - This method creates a floating window desktop instead of the normal kind.

The document class, mydoc:

Imydoc - This is the initialization method. It creates a boxlist object for this document.

BuildWindow - This code is mostly copied from the CStarterDoc code. The main differences are that it creates a docwindow instead of a CWindow and puts a mypane pane in it, and it creates individual color palettes for the second and third document windows.

selectabox - Determines whether the user clicked in an existing box by calling the boxlist object’s hitbox method with the mouse location. If the user did click in a box, the box is sent a doselectstuff message.

DoNewBox - Creates a new box, using the currently selected color. This is called after the user has clicked and dragged the mouse to create a box.

DoCommand - Processes the command which is sent when the user changes the selected color in the color selection window. The sample program doesn’t do anything here, but it would be the place to implement changing the color of selected boxes.

The pane class, mypane:

Imypane - This is the initialization method. It merely calls the CStarterPane initialization method.

Perform_draw - This method is passed a genericbox object as a parameter and calls its draw method. It is used in conjunction with the DoForEach method of the CList object to call each box to draw itself.

Draw - This method is called when all or part of the pane is to be drawn. A rectangle is passed as an argument which defines the area to be redrawn. This method ignores the rectangle and redraws everything. It makes use of the DoForEach function provided by the TCL’s CList class, of which boxlist is a subclass. It calls the DoForEach method of the boxlist belonging to the current document and passes it the Perform_draw function as the function to be executed for each box.

DoClick - This method checks whether the user clicked in an existing box. If not, it tracks the mouse as the user drags it and then calls the document’s DoNewBox method to create a new box.

ChangeSize - I copied some code from the ArtClass demo application in here to make the pane scroll correctly. The Starter application does not scroll properly.

The genericbox class, which manages a box on the screen:

Igenericbox - The initialization method. Saves the box’s bounding rectangle and its color.

draw - Draws the box by calling PaintRect.

testhit - Determines whether a point (presumably the mouse location) is inside the box.

doselectstuff - Handles the actions to be taken when the user selects (clicks on) the box. It erases and redraws the box to make it flash, and sends a changecolor message to the color selector so that the box’s color will be selected in the color selector window.

The boxlist class, which manages a list of boxes:

hitbox - This is the only method. Boxlist is a subclass of CList, which is used for managing an ordered list of objects. The additional functionality provided by boxlist is in this method, which calls each object’s testhit method to see whether or not the mouse coordinates are inside its box. If one of the boxes returns true, that object is passed back as the function result.

How the Color Selector Works

The Think Class Library provides a class called CGridSelector which allows for the selection of an item in a grid of items. This class serves as the basis for the colorselector class which displays the selection of colors in a 16x16 grid.

There are actually three classes necessary to implement the color selection window: colorselector, selectorwindow, and colordirector. The colorselector class is a subclass of CPane, since CGridSelector (its parent class) is a subclass of CPane. In TCL all drawing is done within Panes. The pane must be placed into a window, and in this case a new class, selectorwindow, which is a subclass of CWindow, is used to hold the colorselector pane. The only difference between selectorwindow and CWindow is that when the close box is clicked in a normal (CWindow) window, the window is closed and the object is deleted. I wanted the object to stay around, so selectorwindow overrides the Close method and simply hides the window. The other class, colordirector, is a subclass of CDirector. In TCL, each window must have a director associated with it. The most common kind of director is CDocument, which manages a normal document window. In the case of the color selector window, a special director, colordirector, is required. The colordirector class has an initialization method which places the window on the screen having the greatest pixel depth, and creates a colorselector Pane to put in the window. It also has a DoCommand method which processes color change commands which the color selector sends.

The following paragraphs describe the methods in these classes. For more details, refer to the comments in the code.

colorselector:

DrawItem - This method draws a single color rectangle in the color selection window. It uses the item number as an index and calls the palette manager to get the appropriate entry color.

HiliteItem - Highlights the item so the user can see which color is selected. The default hiliting method used by GridSelector is to invert the item. This doesn’t look good with colors, so this method (which overrides HiliteItem in CGridSelector) draws a rectangle around the item.

currentcolor - Returns the currently selected color and its palette entry number.

windowactivated - This method is called by mydoc’s Activate method whenever a document window is activated. It copies the document window’s palette and associates it with the color selector window. The palette must be associated with the color selector window because as far as the palette manager is concerned, the color selector window is the top window and its palette takes precedence over all other windows.

changecolor - Informs the color selector that the selected color should be changed. This method should be called when the program wishes to change the selected color, such as when the user has selected something and its color should be shown as selected.

colordirector:

Icolordirector - This is the initialization method. It creates the color selector window and puts it on the screen with the greatest pixel depth.

DoCommand - This method forwards the command (which will be a notification of a color selection change) on to the top document. (The colorselector class, by virtue of its inheritance of CGridSelector’s behavior, sends a command to its supervisor (the colordirector) whenever the user makes a selection.)

Suspend - Hides the floating window when the application is suspended.

Resume - Shows the floating window when the application is resumed.

Conclusion

By using a class library such as TCL, programmers can take advantage of a framework which provides not only the standard functionality found in most Macintosh applications, but additional functionality such as floating windows. The classes described here extend the TCL’s functionality in the form of a floating color selection window, a user interface feature found in many current applications.

Listing: boxlist.h
#pragma once
#include <CList.h>
#include “genericbox.h”

class boxlist : public CList
{
public:
 virtual genericbox *hitbox(Point mouseloc);
};
Listing: boxlist.c
#include “boxlist.h”

/*********************************************************
 boxlist is a subclass of CList.  It is used for 
 keeping a list of boxes which the user has drawn.  
 It provides one additional method not found in CList,
 which is a method to test each box in the list to see 
 if a mouse click ocurred in it.
 
**********************************************************/

/*********************************************************
 hitbox
 
 Test each box in the list by calling the box’s testhit 
 method.  When a box’s testhit method returns a value of 
 true, returns this box as the function result.

*********************************************************/
genericbox *boxlist::hitbox(Point mouseloc)
{
 short i;
 
 for (i = 0; i < numItems; i++) 
 {
 if (((genericbox *) (*items)[i])->testhit(mouseloc))
 {
 // Mouseloc is in this box
 return (genericbox *)((*items)[i]);
 }
 }
 return 0L;
}
Listing: colordirector.h

#pragma once
#include <CDirector.h>
#include <CApplication.h>
/* Class Declaration */
class colordirector : public CDirector
{
 public:
 /** Instance Variables **/

 /** Instance Methods **/
 void Icolordirector(CApplication *aSupervisor);
 void DoCommand(long theCommand);
 void Suspend(void);
 void Resume(void);
};
Listing: colordirector.c

#include “colordirector.h”
#include “colorselector.h”
#include <CDesktop.h>
#include “selectorwindow.h”
#include “Global.h”
#include <Palettes.h>
#include <Picker.h>
#include <CFWDesktop.h>

extern CDesktop  *gDesktop; /* The visible Desktop */
colorselector    *gcolorselector;
/* First in line to get commands */
extern CBureaucrat *gGopher;

/********************************************************
 Icolordirector 
 Initializes the colordirector object.
 ********************************************************/
void colordirector::Icolordirector(CApplication 
 *aSupervisor)
{
 Rect bounds, deeprect;
 GDHandle thedevice;
 
 CDirector::IDirector(aSupervisor);
 // Find the screen with the most colors (the deepest device)
 // And put the window on it
 SetRect ( &bounds,  -32767, -32767, 32767, 32767);
 thedevice = GetMaxDevice(&bounds);
 deeprect = (**thedevice).gdRect;
 bounds.left = deeprect.left + 40;
 bounds.top = deeprect.top + 40;
 bounds.bottom = 16*BOXSIZE + bounds.top;
 bounds.right = 16*BOXSIZE + bounds.left;
 
 // Create the selector window
 itsWindow = (CWindow *)new(selectorwindow);
 itsWindow->INewWindow(&bounds, TRUE, 3200, TRUE, TRUE,  
 gDesktop, this);
 // Process the click that activates it
 itsWindow->SetActClick(TRUE);
 
 // Create the selector pane
 gcolorselector = new colorselector;
 gcolorselector->Icolorselector(itsWindow, this, 1, 
 cmdColorSelect, thedevice);
}

/********************************************************
 DoCommand {OVERRIDE}

 Execute a command
 ********************************************************/
void  colordirector::DoCommand(long theCommand)
{
 CBureaucrat *thesupervisor;
 
 if (theCommand < 0) 
 {
 if (HiShort(-theCommand) == cmdColorSelect) 
 {
 // The color selector palette sent us a color change.  
 // We want to send it to the current document for 
 // processing.  We do this by sending the command to 
 // the gopher which will send it up to the
 // current document.
 
 gGopher->DoCommand(theCommand); 
 }
 }
 else
 { 
 switch (theCommand) 
 { 
 default:
 inherited::DoCommand(theCommand);
 break;
 }
 }
}

/********************************************************
 Suspend {OVERRIDE}

 Following the Human Interface Guidelines, tear-off menus 
 are hidden when the application is suspended.  (This is 
 copied from CTearOffMenu.)
 ********************************************************/
void  colordirector::Suspend()
{
    if (active) { 
            itsWindow->HideSuspend();
        ((CFWDesktop *) gDesktop)->CalcTopFloat();
        active = TRUE;
    }
}

/********************************************************
 Resume {OVERRIDE}

 Resume by showing the tear-off window if it was visible 
 before we were suspended.  (This is copied from 
 CTearOffMenu.)
 ********************************************************/
void  colordirector::Resume()
{
    if (active) {
    
        itsWindow->ShowResume();
        ((CFWDesktop *) gDesktop)->CalcTopFloat();
    }
}
Listing: colorselector.h

#pragma once
#include <CGridSelector.h>
#include <Palette.h>

#define BOXSIZE 10
#define cmdColorSelect 25000 

class colorselector : public CGridSelector
{
public:
 void   Icolorselector(CView *anEnclosure, 
 CBureaucrat *aSupervisor,
 short aSelection, short aCommandBase, 
 GDHandle thedevice);
 virtual void    DrawItem(short theItem, Rect *theBox);
 virtual void    HiliteItem(short theItem, 
 HiliteState state);
 void   currentcolor(RGBColor *thecolor, 
 short *theentry);
 void   windowactivated(WindowPtr thewindow);
 void   changecolor(short thecolor);
 
 short  prevhiliteitem;
 PaletteHandle   ph;
};
Listing: colorselector.c

#include “colorselector.h”

void colorselector::Icolorselector(CView *anEnclosure, 
 CBureaucrat *aSupervisor, short aSelection, 
 short aCommandBase, GDHandle thedevice)
{
 CGridSelector::IGridSelector(anEnclosure, aSupervisor, 0, 0, 
 sizFIXEDLEFT, sizFIXEDTOP,
 aSelection, aCommandBase,
 16,16, BOXSIZE, BOXSIZE);
 prevhiliteitem = 0; // No previous item hilighted
 ph = 0L; // No palette exists
}
/**********************************************************
 DrawItem {OVERRIDE}

 Draws the appropriate color in the rectangle.  Finds the 
 color from the palette.
 *********************************************************/
void colorselector::DrawItem(short theItem, Rect *theBox)
{
 RGBColor oldcolor;
 GDHandle curdevice;
 RGBColor curcolor;
 short    pixdepth;
 
 Lock(TRUE);
 if (ph)
 {
 // If theItem is withing the number of entries in the 
 // window’s palette, draw the rectangle in the 
 // appropriate color.  Otherwise, draw it as black.
 if ((theItem-1) < (**ph).pmEntries)
 {
 GetEntryColor(ph, theItem-1, &curcolor);
 }
 else
 {
 curcolor.red = curcolor.green = curcolor.blue = 0;
 }
 GetForeColor ( &oldcolor );
 RGBForeColor( &curcolor );
 PaintRect(theBox);
 RGBForeColor( &oldcolor );
 }
 else
 {
 // No palette associated with the window.  Draw the first 
 // entry as white and the others as black.
 if (theItem == 1)
 curcolor.red = curcolor.green = curcolor.blue = 0xFFFF;       
 // white
 else
 curcolor.red = curcolor.green = curcolor.blue = 0;            
 // black
 GetForeColor ( &oldcolor );
 RGBForeColor( &curcolor );
 PaintRect(theBox);
 RGBForeColor( &oldcolor );
 }
 Lock(FALSE);
}

/**********************************************************
 HiliteItem {OVERRIDE}

 The default hiliting method used by GridSelector is to 
 invert the item.  This doesn’t look good with colors, so 
 we draw a rectangle around the item.
 **********************************************************/
void  colorselector::HiliteItem(
 short theItem,  /* Item number to hilite    */
 HiliteState state)/* State of hiliting desired */
{
 Rect   theRect;
 register Rect *theBox;
 long   ticks;
 
 if (theItem == 0)
 return;
 if (prevhiliteitem != 0)
 {
 // Unhilite the previous item
 FindItemBox(prevhiliteitem, &theRect);
 DrawItem(prevhiliteitem, &theRect);
 InsetRect(&theRect, -1, -1);
 FrameRect(&theRect);
 }
 // Get the item’s rectangle
 FindItemBox(theItem, &theRect);
 theBox = &theRect;
 
 // Draw a white rectangle inside of a black rectangle 
 // around the color
 InsetRect(theBox, -1, -1);
 ForeColor(whiteColor);
 FrameRect(theBox);
 ForeColor(blackColor);
 InsetRect(theBox, 1, 1);
 FrameRect(theBox);
 
 prevhiliteitem = theItem;
}

/*********************************************************
 currentcolor 
 
 Returns currently selected color.

**********************************************************/

void colorselector::currentcolor(RGBColor *thecolor, 
 short *theentry)
{
 GDHandle curdevice;
 
 Lock(TRUE);
 *theentry = selection;
 if (ph)
 {
 if ((selection-1) < (**ph).pmEntries)
 {
 GetEntryColor(ph, (selection-1), thecolor);
 }
 else
 {
 thecolor->red = thecolor->green = thecolor->blue = 0;
 }
 }
 else
 {
 if (selection == 1)
 thecolor->red = thecolor->green = 
 thecolor->blue = 0xFFFF;
 else
 thecolor->red = thecolor->green = thecolor->blue = 0;
 }
 Lock(FALSE);
}

/********************************************************
 windowactivated 
 
 This function is called by a document window when the 
 window is activated.  It copies the palette associated 
 with the window and associates it with the floating color 
 selection window so that the correct colors are 
 displayed.
*********************************************************/
void colorselector::windowactivated(WindowPtr thewindow)
{
 PaletteHandle newph;
 Rect   tmprect;
 GrafPtrsavePort;/* The current port */
 GetPort(&savePort); /* Save the original port */
 SetPort(macPort);
 Lock(TRUE);
 // Get palette associated with the activated document window
 ph = GetPalette(macPort);
 if (ph)
 {
 DisposePalette(ph); // Get rid of it
 ph = 0L;
 }
 newph = GetPalette(thewindow);
 if (newph)
 {
 ph = newph;
 HandToHand ( &ph ); // Make a copy of the palette
 // associate the palette with our selection window
 SetPalette(macPort, ph, TRUE);
 ActivatePalette(macPort);
 }
 Lock(FALSE);
 // Make sure the window gets redrawn
 InvalRect(&(macPort->portRect));
 SetPort(savePort);/* Restore the original port */
}

void colorselector::changecolor(short thecolor)
{
 GrafPtrsavePort;/* The current port */
 
 GetPort(&savePort); /* Save the original port */
 Prepare(); /* Set up Window’s port */
 ChangeSelection(thecolor);
 SetPort(savePort);/* Restore the original port */
 ForceNextPrepare();
}
Listing: docwindow.h

#pragma once

#include <CWindow.h>

class docwindow : public CWindow 
{
public:
 
 virtual void    Activate(void);
};
Listing: docwindow.c

#include “docwindow.h”
#include “colorselector.h”

extern colorselector *gcolorselector;// The color selector palette (pane)
/*********************************************************
 Activate

 Notify the color selector object that this document 
 window has become active.
 *********************************************************/
void  docwindow::Activate(void)
{
 Rect tmprect;
 
 inherited::Activate();
 
 gcolorselector->windowactivated(macPort);
}
Listing: genericbox.h

/********************************************************
 genericbox.h

 Interface for the genericbox Class
 
********************************************************/
#pragma once

#include <CObject.h> /* Interface for its superclass */

class genericbox : public CObject 
{/* Class Declaration */
public:
 /** Instance Variables **/
 Rect   boxrect; // The box’s rectangle
 RGBColor boxcolor;// The box’s color
 short  colorentry;// The palette entry for
 // this box’s color
 
 /** Instance Methods **/
 void   Igenericbox(Rect *therect, RGBColor *thecolor,  
 short colorentry);
 virtual void    draw(void);
 Boolean  testhit(Point mouseloc);
 void   doselectstuff(void);
};
Listing: genericbox.c

#include <CObject.h>
#include “genericbox.h”
#include “colorselector.h”

// The color selector pane
extern colorselector *gcolorselector;

/********************************************************
 Igenericbox
 
 Initialize a generic box.  The box’s rectangle, color, 
 and its color index in the palette are saved.  We keep 
 the palette index as well as the RGB color itself because 
 when we want to tell the color selection window to 
 display the color of this box there is no convenient way 
 to go from an RGB color to a palette index.
 ********************************************************/
void genericbox::Igenericbox(Rect *therect, 
 RGBColor *thecolor, short colorentry)
{
 boxrect = *therect;
 boxcolor = *thecolor;
 this->colorentry = colorentry;
}

/********************************************************
 draw
 
 Draws a generic box by painting a rectangle in the 
 box’s color.
 ********************************************************/
void genericbox::draw(void)
{
 Lock(TRUE);
 RGBForeColor(&boxcolor);
 PaintRect(&boxrect);
 Lock(FALSE);
}

/********************************************************
 testhit
 
 Returns true if the point is inside the box.
 ********************************************************/
Boolean genericbox::testhit(Point mouseloc)
{
 Boolean result;
 
 Lock(TRUE);
 result = PtInRect ( mouseloc, &boxrect );
 Lock(FALSE);
 return result;
}

/*********************************************************
 doselectstuff
 
 This method is invoked when the user has selected this 
 box by clicking on it.  It sends a message to the color 
 selection window to change the currently selected color 
 to the color of this box.
 ********************************************************/
void genericbox::doselectstuff(void)
{
 Lock(TRUE);
 EraseRect(&boxrect);// Erase the box (& redraw it 
 // later to make it flash)
 // Change selected color to this box’s
 gcolorselector->changecolor(colorentry);
 RGBForeColor(&boxcolor);
 PaintRect(&boxrect);
 Lock(FALSE);
}
Listing: selectorwindow.h

#pragma once
#include <CWindow.h>

/* Class Declaration */
class selectorwindow : public CWindow {
public:
 virtual void    Close(void);
};
Listing: selectorwindow.c

#include “selectorwindow.h”

/********************************************************
 Close

 Respond to a click in the close box.  We just hide the 
 window instead of deleting it.
 ********************************************************/
void  selectorwindow::Close()
{
 Hide();
}
Listing: mypane.c

#pragma once
/****
 * mypane.h
 *
 *
 ****/

#include “CStarterPane.h”

struct mypane : CStarterPane 
{
 void   Imypane(CView *anEnclosure, CBureaucrat *aSupervisor,
 short aWidth, short aHeight,
 short aHEncl, short aVEncl,
 SizingOption aHSizing, SizingOption aVSizing);

 void   Draw(Rect *area);

 void   DoClick(Point hitPt, short modifierKeys, long when);
 void   ChangeSize(register Rect *delta, Boolean redraw);
};
Listing: mypane.c

/****
 * mypane.c
 *
 *
 ****/
/**
 *
 * Initialization method.  Simply calls the superclass’s 
 * initialization method.
 *
 **/

#include “mypane.h”
#include “mydoc.h”
#include “genericbox.h”

void Perform_draw(genericbox *thebox);

void mypane::Imypane(CView *anEnclosure, 
 CBureaucrat *aSupervisor,
 short aWidth, short aHeight,
 short aHEncl, short aVEncl,
 SizingOption aHSizing, SizingOption aVSizing)
{
 CStarterPane::IStarterPane(anEnclosure, aSupervisor, 
 aWidth, aHeight, aHEncl, aVEncl, aHSizing, aVSizing);
}

/***
Perform_draw
Iteration procedure for drawing each box in the box list.  It is called 
by the DoForEach method of the boxlist object.  See the Draw function 
here for more information.
***/
static void Perform_draw(genericbox *thebox)
{
 thebox->draw();
}

/***
 * Draw 
 *
 * Draw the contents of the pane.
 ***/

void mypane::Draw(Rect *area)

{
 // Sends a DoForEach message to the boxlist object belonging 
 // to the pane’s supervisor (the document).  Object oriented 
 // purists will undoubtedly gag at referencing an instance 
 // variable belonging to another class.  The “pure” way to 
 // do this would be to send a message to the document 
 // requesting that it call the DoForEach method of its 
 // boxlist, but that would require another method in
 // the document class.
 ((mydoc *)itsSupervisor)->boxlist->DoForEach(Perform_draw);
}

/***
 * DoClick
 *
 * The mouse went down in the pane.
 * If the click was in an existing box, select the box.
 * Otherwise, draw a new one.
 *
 ***/ 

void mypane::DoClick(Point hitPt, short modifierKeys, 
 long when)
{
 /* what happens when the mouse goes down */
 Rect tmprect;
 Point dragpt,prevdragpt;
 Boolean first;
 CursHandle crosscursor;
 
 if (!(((mydoc *)itsSupervisor)->selectabox(hitPt)))
 {
 // Didn’t select one.
 // Track the mouse, drawing a “rubber band” rectangle.
 first = TRUE;
 prevdragpt = hitPt;
 PenMode ( patXor );
 SetRect ( &tmprect, hitPt.h, hitPt.v, hitPt.h+100, 
 hitPt.v+100 );
 crosscursor = GetCursor ( crossCursor );
 SetCursor ( *crosscursor );
 while (StillDown())
 {
 GetMouse(&dragpt);
 if (!EqualPt(dragpt,prevdragpt))
 {
 if (!first)
 {
 // Erase the previous rectangle
 SetRect ( &tmprect, hitPt.h, hitPt.v, 
 prevdragpt.h, prevdragpt.v );
 FrameRect(&tmprect);
 }
 first = FALSE;
 // Draw the new rectangle
 SetRect ( &tmprect, hitPt.h, hitPt.v, 
 dragpt.h, dragpt.v );
 FrameRect(&tmprect);
 prevdragpt = dragpt;
 }
 }
 if (!first) FrameRect(&tmprect);  // Erase the last one
 InitCursor();
 PenMode(patCopy);
 Lock(TRUE);
 ((mydoc *)itsSupervisor)->DoNewBox(&tmprect);
 Lock(FALSE);
 }
}

/********************************************************
 ChangeSize

 Change the size. If necessary, shift drawing so
 that it always completely covers the frame.  (This code 
 is copied from the Art Class sample application.  It 
 makes scrolling work correctly.)
 ********************************************************/
void  mypane::ChangeSize(
 register Rect   *delta,  /* Movement for each side */
 Booleanredraw)  /* Redraw Pane or not? */
{
 short  hShift;
 short  vShift;
 
 /* If new size would cause the */
 /*   frame to extend beyond the */
 /*   bounds of the diagram, we */
 /*   have to shift the diagram. */
 hShift = Max(frame.right + delta->right - bounds.right, 0);
 vShift = Max(frame.bottom + delta->bottom - 
 bounds.bottom, 0);

 inherited::ChangeSize(delta, redraw);
 
 if ( (hShift > 0)  ||  (vShift > 0) ) {
 Scroll(-hShift, -vShift, false);
 if (redraw) {
 Refresh();
 }
 }
}
Listing: mydoc.h

#pragma once
/****
 * mydoc.h
 *
 *
 ****/

#include “CStarterDoc.h”
#include <CApplication.h> 
#include <CList.h>
#include “boxlist.h”

struct mydoc : CStarterDoc {

 Rect   docsizerect;
 short  rightbound;
 short  bottombound;
 boxlist*boxlist;

 void   Imydoc(CApplication *aSupervisor, 
 Boolean printable);

 void   BuildWindow(Handle theData);
 
 void   DoNewBox(Rect *boxrect);
 Booleanselectabox(Point testpoint);
 void   DoCommand(long theCommand);
}; 
Listing: mydoc.c

/****
 * mydoc.c
 *
 * Document methods for a typical application.
 *
 *  Copyright © 1990 Symantec Corporation.  
 *  All rights reserved.
 *
 ****/

#include “Global.h”
#include “Commands.h”
#include “CApplication.h”
#include “CBartender.h”
#include “CDataFile.h”
#include “CDecorator.h”
#include “CDesktop.h”
#include “CError.h”
#include “CPanorama.h”
#include “CScrollPane.h”
#include “mydoc.h”
#include “mypane.h”
#include “TBUtilities.h”
#include <Packages.h>
#include <CPaneBorder.h>
#include “genericbox.h”
#include “colorselector.h”
#include “docwindow.h”
#include <Palettes.h>
#include <Picker.h>

#define WINDStarter500    /* Res ID for WIND template */

extern  CApplication *gApplication;/* The application */
extern  CDecorator *gDecorator;  /* Window dressing object */
/* The enclosure for all windows */
extern  CDesktop *gDesktop;
/* The current boss in the chain of command */
extern  CBureaucrat*gGopher;
extern  OSType   gSignature;/* The application’s signature */
extern  CError   *gError; /* The global error handler */
// The color selector palette (pane)
extern  colorselector *gcolorselector;
/* The menu handling object */
extern  CBartender *gBartender;

/***
 * Imydoc
 *
 * Initialize the document by setting its window size and 
 *   creating a boxlist for it.
 ***/
void mydoc::Imydoc(CApplication *aSupervisor, 
 Boolean printable)

{
 CStarterDoc::IDocument(aSupervisor, printable);
 
 // Set the window size to be the printed page size
 rightbound = pageWidth;
 bottombound = pageHeight;
 SetRect ( &docsizerect, 100, 100, rightbound+15, 
 bottombound+15 );
 
 // Create the list to hold all the boxes.
 boxlist = new boxlist;
 boxlist->IList();
}

/***
 * BuildWindow
 * Most of this is lifted from the CStarterDoc code, except 
 * for the Pane type and the code to create the palettes.
 *
 ***/

void mydoc::BuildWindow (Handle theData)

{
 CScrollPane*theScrollPane;
 mypane *theMainPane;
 GrafPtr theport;
 PaletteHandle bp;
 RGBColor rgbc;
 long i;
 short wcount;

 itsWindow = new docwindow;
 itsWindow->IWindow(WINDStarter, FALSE, gDesktop, this);

 wcount = gDecorator->GetWCount();
 if (wcount == 1)
 {
 // Give the second window a palette of red colors
 theport = itsWindow->GetMacPort();
 bp = NewPalette(250, 0L, pmTolerant, 0);
 rgbc.blue = 0;
 rgbc.green = 0;
 for (i=0; i<250; i++)
 {
 rgbc.red = (i*65535)/250;
 SetEntryColor(bp, i, &rgbc);
 }
 SetPalette(theport, bp, TRUE);
 }
 else if (wcount == 2)
 {
 // Give the third window a palette of green colors
 theport = itsWindow->GetMacPort();
 bp = NewPalette(200, 0L, pmTolerant, 0);
 rgbc.blue = 0;
 rgbc.red = 0;
 for (i=0; i<200; i++)
 {
 rgbc.green = (i*65535)/200;
 SetEntryColor(bp, i, &rgbc);
 }
 SetPalette(theport, bp, TRUE);
 }
 itsWindow->SetSizeRect(&docsizerect);
 
    theScrollPane = new(CScrollPane);
    
 theScrollPane->IScrollPane(itsWindow, this, 10, 10, 0, 0,
 sizELASTIC, sizELASTIC,
 TRUE, TRUE, TRUE);

 theScrollPane->FitToEnclFrame(TRUE, TRUE);

 theMainPane = new(mypane);
 theMainPane->Imypane(theScrollPane, this, rightbound, 
 bottombound, 0, 0, sizELASTIC, sizELASTIC);
 itsMainPane = theMainPane;
 itsGopher = theMainPane;
 
 theMainPane->FitToEnclosure(TRUE, TRUE);
 theScrollPane->InstallPanorama(theMainPane);
 
 gDecorator->PlaceNewWindow(itsWindow);
 theMainPane->SetWantsClicks (true);
}

/***
 * selectabox
 *
 * Returns true if a box was selected (clicked in) and 
 *   false otherwise.  If a box was selected, its 
 *   doselectstuff method is called.
 ***/
Boolean mydoc::selectabox(Point testpoint)
{
 genericbox *gbox;
 
 gbox = 0;
 Lock(TRUE);
 
 // Ask the boxlist to check each box. It will return the box
 // object which was selected or null if none was selected.
 if ((gbox = boxlist->hitbox(testpoint)) != 0L)          
 {
 gbox->doselectstuff();
 }
 Lock(FALSE);
 return (gbox != 0);
}

/***
 * DoNewBox
 *
 * Create a new box.
 ***/
void mydoc::DoNewBox(Rect *boxrect)
{
 genericbox *gbox;
 RGBColor thecolor;
 short colorentry;
 
 Lock(TRUE);
 
 // Get the currently selected color from 
 // the color selection palette
 gcolorselector->currentcolor(&thecolor, &colorentry);
 
 gbox = new genericbox;
 gbox->Igenericbox(boxrect, &thecolor, colorentry);
 gbox->draw();
 boxlist->Append((CObject *)gbox);
 Lock(FALSE);
}

void mydoc::DoCommand(long theCommand) 
 
{ 
 if (HiShort(-theCommand) == cmdColorSelect) 
 { 
 // Color selection change.  The low word has 
 // the current color number.  The currentcolor method of 
 // gColorselector could also be called here 
 // to get the current entry number and RGB value.  
 // This would be the place where we might check for 
 // selected boxes and change their color.  Right now, 
 // we don’t do anything.

 FlashMenuBar(0);      // Just to see something happen 
 FlashMenuBar(0); 
 } 
 else 
 { 
 inherited::DoCommand(theCommand); 
 } 
} 
Listing: myapp.h

#pragma once
/*****
 * myapp.h
 *
 *****/

#include “CStarterApp.h”
#include <CWindow.h>
#include <CDirector.h>
#include “colordirector.h”

class myapp : public CStarterApp 
{
public:
 
 void   Imyapp(void);
 void   UpdateMenus(void); 
 void   DoCommand(long theCommand);
 void   CreateDocument(void);
 void   MakeDesktop(void);
 
protected:

 colordirector *thecolordirector;
};
Listing: myapp.c

/*****
 * myapp.c
 *
 * Application class for the sample program to illustrate 
 * use of the color selector window.
 * Note:  This application does not check for the existence 
 * of color quickdraw.  Obviously, you should do this in a 
 * real program before calling color routines, but I didn’t 
 * want to add a lot of extra code.
 *****/

#include “myapp.h”
#include “mydoc.h”
#include <CFWDesktop.h>
#include <CBartender.h>
#include <Commands.h>
#include “colordirector.h”
#include “colorselector.h”

extern  OSType   gSignature;
extern CDesktop  *gDesktop; /* The visible Desktop */
extern CBartender*gBartender; /* Manages all menus */

// The color selector palette (pane)
extern colorselector *gcolorselector;

/***
 * Imyapp
 *
 * Initialize the application.  Creates the 
 *   colordirector object.
 ***/

void myapp::Imyapp(void)

{
 CStarterApp::IStarterApp(); // Call inherited initialization
 
 thecolordirector = new colordirector;
 thecolordirector->Icolordirector(this);
}

/***
 * DoCommand
 *
 * Processes a menu selection.
 ***/
void myapp::DoCommand(long theCommand)

{
 switch (theCommand) 
 {
 case cmdAbout:
 Alert(130, 0L); // Show the about box
 break;
 case 1025:
 // 1025 is the command number assigned
 // to the “Show Color Selector ” menu item

 // Tell the window to show itself
 gcolorselector->itsEnclosure->Show();
 break;
 default: 
 inherited::DoCommand(theCommand);
 break;
 }
}

/***
 *
 * UpdateMenus 
 *
 *   Enable/Disable appropriate menu items.
 *
***/

 void myapp::UpdateMenus()
 {
 /* Enable standard commands */
   inherited::UpdateMenus();
 /* Enable “Show Color Selector ” */
   gBartender->EnableCmd(1025);
 }

/***
 * CreateDocument
 *
 * The user chose New from the File menu, so create 
 * a new document.
 ***/

void myapp::CreateDocument()

{
 mydoc  *theDocument = NULL;
 
 TRY
 {
 theDocument = new(mydoc);
 theDocument->Imydoc(this, TRUE);
 theDocument->NewFile();
 }
 CATCH
 {
  if (theDocument) theDocument->Dispose();
 }
 ENDTRY;
}

/********************************************************
 MakeDesktop
 
 Create the global Desktop object, which is the top level 
 of the visual hierarchy. Overrrides the standard method 
 to use a desktop which supports floating windows.
 *********************************************************/
void  myapp::MakeDesktop()
{
 /* Use a floating window Desktop */
 gDesktop = new(CFWDesktop);
 ((CFWDesktop *)gDesktop)->IFWDesktop(this);
}
Listing: main.c

/********************************************************
  main.c
 ********************************************************/
#include “myapp.h”

void main()
{
 myapp  *StarterApp; 

 StarterApp = new myapp;
 
 StarterApp->Imyapp();
 StarterApp->Run();
 StarterApp->Exit();
}

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Final Cut Pro 10.6.4 - Professional vide...
Redesigned from the ground up, Final Cut Pro combines revolutionary video editing with a powerful media organization and incredible performance to let you create at the speed of thought.... Read more
iMovie 10.3.4 - Edit personal videos and...
With a streamlined design and intuitive editing features, iMovie lets you create Hollywood-style trailers and beautiful movies like never before. Browse your video library, share favorite moments,... Read more
Motion 5.6.2 - Create and customize Fina...
Motion is designed for video editors, Motion 5 lets you customize Final Cut Pro titles, transitions, and effects. Or create your own dazzling animations in 2D or 3D space, with real-time feedback as... Read more
iMazing 2.15.8 - Complete iOS device man...
iMazing is the world’s favourite iOS device manager for Mac and PC. Millions of users every year leverage its powerful capabilities to make the most of their personal or business iPhone and iPad.... Read more
VueScan 9.7.90 - Scanner software with a...
VueScan is a scanning program that works with most high-quality flatbed and film scanners to produce scans that have excellent color fidelity and color balance. VueScan is easy to use, and has... Read more
Compressor 4.6.2 - Adds power and flexib...
Compressor adds power and flexibility to Final Cut Pro X export. Customize output settings, work faster with distributed encoding, and tap into a comprehensive set of delivery features. Features:... Read more
Capture One 15.3.2.11 - RAW workflow sof...
Capture One is a professional RAW converter offering you ultimate image quality with accurate colors and incredible detail from more than 400 high-end cameras - straight out of the box. It offers... Read more
Vivaldi 5.4.2753.28 - An advanced browse...
Vivaldi is a browser for our friends. We live in our browsers. Choose one that has the features you need, a style that fits and values you can stand by. From the look and feel, to how you interact... Read more
Parallels Desktop 18.0.0 - Run Windows a...
Parallels allows you to run Windows and Mac applications side by side. Choose your view to make Windows invisible while still using its applications, or keep the familiar Windows background and... Read more
TechTool Pro 16.0.1 - Hard drive and sys...
TechTool Pro has long been one of the foremost utilities for keeping your Mac running smoothly and efficiently. With the release of this version, it has become more proficient than ever. Main... Read more

Latest Forum Discussions

See All

Turn-Based RPG ‘Avatar: Generations’ Sof...
Square Enix London Mobile, Navigator Games, and Paramount Consumer Products just announced that the turn-based RPG Avatar: Generations based on Nickelodeon’s Avatar: The Last Airbender is soft launching this month for mobile. Avatar: Generations is... | Read more »
Tower of Fantasy launches today and brin...
Level Infinite and Hotta Studio have announced the release of their very ambitious looking shared open world MMORPG Tower of Fantasy. With its cross-platform functionality between PC and mobile, it looks to be one to roll the dice on and enjoy at... | Read more »
‘Genshin Impact’ Version 3.0 Gets a New...
After HoYoverse released Genshin Impact (Free) version 2.8 on all platforms, the company has slowly been teasing the major upcoming 3.0 update. This update features the Sumeru region with many characters. While details on the update including a... | Read more »
Out Now: ‘Tower of Fantasy’, ‘Tightrope...
Each and every day new mobile games are hitting the App Store, and so each week we put together a big old list of all the best new releases of the past seven days. Back in the day the App Store would showcase the same games for a week, and then... | Read more »
SwitchArcade Round-Up: ‘Book Quest’, ‘Cl...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for August 10th, 2022. In today’s article, we’ve got a little news about an update to a game I really like, a few new releases to summarize, and some sales to look at. A bit of a quiet... | Read more »
‘Pine Tar Poker’ is an Otherworldly Poke...
Developer BJ Malicoat, who put out the well-received and former Apple Game of the Day pick Downwordly in June of last year, is back working on another mobile game project called Pine Tar Poker, and it has caught my attention. Why? Because it’s a... | Read more »
Darkness Rises celebrates four years of...
Four years of uptime for a mobile game is akin to eternity, and this is exactly the milestone that Darkness Rises has reached. It is important for developers to keep updating to keep the game fresh, and NEXON has announced a massive anniversary... | Read more »
Keep Your Smatphone’s Case On When Using...
The original Gamevice was born as a sort of offshoot of the weird Wikipad gaming tablet/controller/hybrid thing way back in 2014. Interestingly, the first Gamevice controller for iOS only supported the iPad mini and launched in 2015, with versions... | Read more »
SwitchArcade Round-Up: A ‘Splatoon 3’ Ni...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for August 9th, 2022. In today’s article, we’ve got some news about a Splatoon 3 Nintendo Direct, a review of QUByte’s Thunderbolt Collection, a single new release summary, and the usual... | Read more »
Orangepixel’s Pacifist Survival Game ‘Re...
Back in June we learned that long-time mobile developer Orangepixel, who also makes games for PC and consoles (including the Atari VCS!), would be bringing the unique survival game Residual to mobile devices sometime this year. Originally launched... | Read more »

Price Scanner via MacPrices.net

Apple has 24-inch M1 iMacs available starting...
Apple has 24-inch M1 iMacs with M1 CPUs (8-core CPU/7-core GPU) available today in their Certified Refurbished store for $1099 shipped. Their price is $200 off standard MSRP. Each iMac is in like-new... Read more
13″ M1 MacBook Airs in stock today for $799,...
QuickShip Electronics has open-box return 13″ M1 MacBook Airs in stock and on sale for $200 off MSRP on their eBay store right now, each with free express delivery. According to QuickShip, “The item... Read more
In stock today: Mac Studio models for up to $...
Apple retailer Expercom has Mac Studio models in stock today and on sale for up to $400 off Apple’s MSRP, depending on configuration. Their prices are the lowest price available for a Mac Studio from... Read more
Mac mini with M1 CPU and 512GB of storage on...
Amazon has the M1 Mac mini with a 512GB SSD in stock today on sale for $749.99 including free shipping. Their price is $150 off Apple’s MSRP, and it’s the lowest price available for this... Read more
Need a Mac or iPad for school? Get a free App...
Apple’s Back to School promotion for 2022 continues to run through September 26, 2022. As part of this promotion, Apple will include a free $150 Apple Gift Card with the purchase of any MacBook Air,... Read more
Apple Watch SE on sale for $50 off MSRP
Amazon has Apple Watch SE GPS models on sale for $50 off MSRP for a limited time, each including free shipping. Their prices are the lowest currently available for SE Watches: – 40mm Apple Watch SE... Read more
Save $310 on a 14″ 24-core GPU M1 Max MacBook...
Save $310 on 14″ MacBook Pros with 24-core M1 Max processors at Apple (32GB RAM/1TB SSD) with these Certified Refurbished models in stock today for $2789 in Space Gray or Silver colors. Regular price... Read more
14″ M1 Pro MacBook Pros available today at Ap...
Apple has Certified Refurbished standard-configuration 14″ MacBook Pros with M1 Pro CPUs available today for up to $250 off original MSRP, starting at $1799. Each model features a new outer case,... Read more
13″ MacBook Air with M2 CPU, in Starlight, on...
Apple retailer Expercom has the new Starlight 13″ MacBook Air with an M2 CPU (8GB RAM/256GB SSD) on sale for $1135.05, shipped, through August 12, 2022. Their price is $64 off Apple’s MSRP, and it’s... Read more
14″ M1 Pro MacBook Pro with 1TB SSD on sale f...
Expercom is offering a $200 instant discount on the 14″ M1 Pro MacBook Pro with a 1TB SSD through August 12, 2022. Their discount reduces the price of this configuration to $1999 shipped — the lowest... Read more

Jobs Board

Solutions Engineering Manager - *Apple* - S...
…in our Hardware and Advanced Solutions group leading and developing our Apple technical practice to increase revenue and profitability. The ideal candidate would Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Sephora Beauty Advisor - *Apple* Blossom Ma...
Sephora Beauty Advisor - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.