//====================================================================== // File: laf.h // Author: originally written by Timothy P. Justice // modifications by Thomas G. Dietterich and Timothy A. Budd // Created: March 22, 1994 // Last modified: Feb 7, 1995 // Description: This file contains the interface of the // classes for the Little Application Framework. // has been tested with symantic 6 and 7 // and metrowerks codewarrior 6 //---------------------------------------------------------------------- // Copyright (c) 1992 by Timothy P. Justice. // Copyright (c) 1994 by Thomas G. Dietterich // Copyright (c) 1995 by Timothy A. Budd // // This file is part of LAF, the Little Application Framework. // // Permission is hereby granted to copy and distribute // without fee verbatim copies of this file. This copyright notice // must be retained on all copies. No warranty expressed or implied // is made concerning this software. // Commercial use of the LAF, while not prohibited, is ill-advised. // //====================================================================== #ifndef LAF_H #define LAF_H // // Note: your project must contain the following: // In Segment 2 (with your program): // MacLaf // MacTraps // In Segment 3 // ANSI++ // Class Hierarchy: Little Application Framework class window; class application; class button; //class tbutton; class TEditCommon; class staticText; class editableText; class menu; class menuItem; //class tmenuItem; // Other Types enum justification { rightText = teJustRight, leftText = teJustLeft, centerText = teJustCenter}; // Colors // The following named colors are always available: // blackColor, whiteColor, redColor, greenColor, blueColor, cyanColor // magentaColor, yellowColor // Pen Patterns // The following named patterns are always available: // white, black, gray, ltGray, dkGray // --- Window Class ---------------------------------------------------- // // Window is an abstract class that defines the behavior common // to all windows. //---------------------------------------------------------------------- class window { public: // constructor window(char *, int, int); // size information functions int bottom(); // screen coordinate of window bottom int height(); // window height in pixels int left(); // screen coordinate of window left int right(); // screen coordinate of window right int top(); // screen coordinate of window top int width(); // window width in pixels // refresh operations on the window void clearAndUpdate(); void update(); // graphics operations void circle(const int x, const int y, const int radius); void line(const int x, const int y, const int newx, const int newy); void point(const int x, const int y); void rectangle(const int x, const int y, const int width, const int height); void print(int x, int y, const char * text); void setPen(long int color, Pattern , int); void setPenColor(long int color); void setPenSize(int); // grid controls. When the grid is on, a single-pixel dot is displayed at each grid intersection, // and mouse events are snapped to the nearest grid point. void gridOn(int color, int size); void gridOff(); protected: friend class TEditCommon; friend class editableText; friend class menu; friend class button; friend class staticText; Rect boundRect; // bounding rectangle for the root window Rect dragRect; // the WindowPtr is a pointer to the QuickDraw graphics port of this window. // that record stores the pen location, size, color, // as well as font, face, etc. // See Inside Mac I-203 WindowPtr macWindow; // the WindowRecord is a record containing information about the window. // See Inside Mac I-304 WindowRecord macWindowRecord; // search through buttons, find the one matching the ControlHandle, and "press" it. void findAndHitButton(ControlHandle); // linked lists of objects in this window. button * buttons; menu * menus; editableText * editableTexts; staticText * staticTexts; // editableText currently in focus editableText * focussedEditText; // editableText (if any) below Point editableText * editableTextUnderPoint(Point &); // editableText (if any) that owns this control editableText * findeditableTextFromScrollControl(ControlHandle); int gridSize; // zero => no grid int gridColor; void paintGrid(); void snapCoord(int &); }; //---menus and menu items ------------------------------------- // // user creates subclasses of menu items, inserting them into menu // when selected, method ``selected'' is invoked -- usually subclassed by user // class menu { public: /* constructor -- needs a window and a name */ menu (window *, char *); protected: void addMenuItem(menuItem *, char *); virtual void select (window *, int); friend class window; friend class application; friend class menuItem; MenuHandle menuPtr; int idNumber; menu * nextMenu; menuItem * firstItem; }; class menuItem { public: /* constructor */ menuItem (menu &, char *); protected: /* this method is invoked when item is selected */ virtual void selected(window *); friend class menu; int positionNumber; // menu position number menuItem * nextItem; }; #if __MWERKS__ class quitItemClass : public menuItem { public: quitItemClass(menu & m) : menuItem(m, "quit/Q") { } protected: virtual void selected(window * w) { ExitToShell(); } }; # else // if the menu item just executes a member function, // then the following declaration can be used template class tmenuItem : public menuItem { public: tmenuItem (menu & m, char * t, void (T::* f)() ) : menuItem(m, t), fun(f) { } protected: void (T::* fun)(); virtual void selected (window * win) { (((T *) win)->*fun)(); } }; # endif // --- Application Class ----------------------------------------------- // // Application is the basic framework class. Every program should specialize this // class, create exactly one instance of it, and invoke its run() method. // The run() method contains the main event loop for handling Macintosh events. // // The run() method creates a single top-level window for the application. // All other objects created for the application (e.g., buttons, textEdit, // staticText regions) are just regions within this top-level window. // // If the user wishes to draw lines and write text at arbitrary locations in the // application window, the user will need to specialize the paint() method to redraw // those items when the window becomes visible; otherwise they will disappear // when the window is hidden and then re-exposed. // The application() permits moving and quitting the application. // The application can be quit either by selecting the GoAway box in the upper left corner // of the window or by selecting Quit from the File menu //---------------------------------------------------------------------- class application : public window { // the apple menu is handled as a special case class appleMenuClass : public menu { public: appleMenuClass(window * w); virtual void select(window *, int); }; public: // constructor - argument is window title application(char *, int width = 400, int height = 300); // public event handling methods void run(); void quit(); protected: // event handling methods virtual void mouseButtonDown(int, int); virtual void keyPressed(char); virtual void paint(); // the following is executed once every event loop // can be used for animation virtual void eventTick(); private: // the standard menu items appleMenuClass appleMenu; menu fileMenu; #if __MWERKS__ quitItemClass quitItem; # else tmenuItem quitItem; # endif // event handling methods int MainEvent(); void MaintainCursor(); void Update(); void DoMouseDown(int, WindowPtr, EventRecord *); void DoContent(EventRecord *); int DoCommand(long int); void DoGrow(WindowPtr, EventRecord *); void findAndScrolleditableText(int cntlCode, ControlHandle theControl, Point where); }; // --- button ---------------------------------------------------------- // buttons subclass the pressed function, which is invoked when pressed. // buttons are normally declared along with the constructor for a window class button { public: button (window *, char *, int, int, int, int); protected: virtual void pressed(window *) = 0; private: int idNumber; // internal identification number ControlHandle control; // Mac control object handle button * nextButton; // link to the next button friend class window; }; // if all your button invokes is a member function, then // using tbutton avoids the need to define a new class template class tbutton : public button { public: tbutton (window * win, char * t, int x, int y, int h, int w, void (T::* f)() ) : button(win, t, x, y, h, w), fun(f) { } protected: void (T::* fun)(); virtual void pressed (window * win) { (((T *) win)->*fun)(); } }; // --- TEditCommon ----------------------------------------------------- // // This is a virtual class that gathers together those aspects of // TEdit's and staticText's that are identical (primarily their // input and output operations. class TEditCommon { friend class window; friend class application; public: // constructors TEditCommon(window *, int, int, int, int, justification, int, char * text); // get the number of characters in the text edit buffer int size(); // copy text edit buffer to a string char * text(); // set the text of the TEdit void setText(char []); // repaint the display void update(); int pointInTEdit(Point &); virtual void showSelect(); // text output TEditCommon & operator << (char *); TEditCommon & operator << (char); TEditCommon & operator << (int); //TEditCommon & operator << (TEditCommon & (*)(TEditCommon &)); protected: window * win; TEHandle handle; // Macintosh handle to TextEdit int topValue; int leftValue; int heightValue; int widthValue; int drawWithBorder; // true if we should draw border. }; //--- editableText --------------------------------------------------------- class editableText: public TEditCommon { public: // constructor editableText(window *, int, int, int, int, char * = 0, int border = 1, justification = leftText); void setFocus(); void unSetFocus(); // adjust the displayed text and scroll bar indicator void adjustText(); // update scroll bar, scroll to follow user typing virtual void showSelect(); // set the vScroll thumb position void setVScroll(); private: friend class window; friend class application; friend pascal void ScrollProc(ControlHandle, short); ControlHandle vScroll; // vertical scroll bar int linesInFolder; // number of visible lines on screen editableText * nexteditableText; // pointer to next edit text. }; // --- Static Text Class ----------------------------------------------- // // The staticText class implements fields of fixed text. // They are automatically repainted by application. // class staticText : public TEditCommon { public: // constructor staticText(window *, int, int, int, int, char * = 0, int border = 0, justification = leftText); private: friend class window; friend class application; staticText * nextStaticText; // pointer to next staticText }; #endif