home *** CD-ROM | disk | FTP | other *** search
- <html>
- <title>Derived Windows</title>
- <h1>Derived Windows</h1>
-
- The Window class provides facilities for the manipulation of basic windows. In
- order to provide more functional windows, it is necessary to define new
- classes based on the Window class. The Window class has a set of virtual
- member functions which may be replaced by a derived class in order to
- enhance the functionality.
- <p>
- Vista includes a set of useful functional windows which may be used
- by the program. Again, if the functionality of the windows defined i
- not sufficient, it is a simple matter of deriving a new class from
- them.
- <p>
- The derived windows supplied with Vista are:
- <p>
- <ul>
- <li><a href="file:wins#dbox">DialogueBox:</a> - An input/output device
- <li><a href="file:wins#grid">IconGrid:</a> - A window like a Filer
- <li><a href="file:wins#save">SaveBox:</a> - The standard Save box
- <li><a href="file:wins#prog">ProgramInfo:</a> - The standard Program Info box
- </ul>
-
- <p>
- <hr>
- <p>
- <a name="dbox"></a>
- <h2>DialogueBox</h2>
-
- The DialogueBox class implements a generic data input/output mechanism.
- It consists of a window which contains a set of <a href="file:wins#attr">attributes.</a>
- An attribute
- is a piece of data that may be modified by the user. It appears as
- an icon in the window.
- <p>
- The Dialogue Box class is defined as:
- <hr>
- <pre>
-
- class DialogueBox : public Window, public Thread
- {
- public:
- DialogueBox (Task *t, char *tname, int cancel = -1, int ok = -1, char *menu = 0) ;
- virtual ~DialogueBox() ;
- void click (int mx, int my, int buttons, int icon) ;
- void key(int icon, int x, int y, int height, int index, int code) ;
- virtual void show() ;
- virtual void hide() ;
- void run() ;
- void close() ;
- Icon *find_icon (int icon) { return NULL ; }
- virtual void cancel(int button) ;
- virtual void ok(int button) ;
- void add_attribute (Attribute *attr) ;
- void create_attribute (int iconnum, char *str) ;
- void create_attribute (int iconnum, int &num) ;
- void create_attribute (int iconnum, bool &value) ;
- void create_attribute (int num_values, bool *values, ...) ;
- void create_attribute (Icon *icon, char *str) ;
- void create_attribute (Icon *icon, int &num) ;
- void create_attribute (Icon *icon, bool &num) ;
- void next_attribute (int icon) ;
- void prev_attribute (int icon) ;
- protected:
- Attribute *attributes ;
- Attribute *last_attribute ;
- Icon *cancel_icon ;
- Icon *ok_icon ;
- ThreadSemaphore *waiting ;
- int from_menu ;
- public:
- int cancelled ;
- } ;
-
- </pre>
- <hr>
- <p>
- It is derived from both the Window and Thread classes. This means that it
- behaves like both classes. It appears as a window on the screen, but also
- runs as a thread.
- <p>
- The normal operation for a dialogue box is for the user to display it on the
- screen, fill in some of the fields and click on either the OK or Cancel button.
- If the OK button is clicked, the attributes are committed, otherwise the values
- entered by the user are discarded. It is normally necessary for the program
- to wait for the user to enter the data before it can continue.
- <p>
- The DialogueBox is created by use of the constructor. This is supplied with the
- following parameters:
- <p>
- <ol>
- <li>The task
- <li>The name of the window template
- <li>The icon number for the 'Cancel' icon (-1 = none)
- <li>The icon number for the 'OK' icon (-1 = none)
- <li>The name of a menu (0 = none)
- </ol>
- <p>
- The dialogue box does not appear on the screen. To do this, the 'show()'
- function must be called. This causes the window to appear on the screen
- and the thread started. The thread terminates when the user has closed the
- dialogue box by clicking on OK or Cancel (or by otherwise causing the
- window to close). Usually the function which opened the dialogue box will
- be waiting the the user to fill it in.
-
- <p>
- <a name="attr"></a>
- <h3>Attributes</h3>
-
- When defining a dialogue box, a new class should be derived from the
- DialogueBox base class. This new class should contain all the attributes
- to be filled in by the user. An attribute consists of 2 parts:
- <p>
- <ol>
- <li>A piece of data which will contain the value of the attribute
- <li>A user interface allowing the user to modify the value.
- </ol>
- <p>
- In order to tie these 2 parts together, you need to tell the DialogueBox
- class what they are. The functions 'create_attribute' allow the dialogue
- box to be told of all the attributes it contains.
- <p>
- For example, consider the following dialogue box:
- <pre>
-
- class PersonDetails : public DialogueBox
- {
- public:
- PersonDetails(Task *t) : DialogueBox (t, "details", 1, 2)
- {
- strcpy (name, "") ;
- age = 21 ;
- create_attribute (3, name) ;
- create_attribute (4, age) ;
- }
- ~PersonDetails() {}
-
- // attributes
-
- char name[32] ;
- int age ;
- } ;
-
- </pre>
- This dialogue box is created from a template called "details" and
- has an OK and a Cancel button. The OK button has icon number 2 and the
- Cancel button is icon 1.
- <p>
- There are 2 attributes:
- <p>
- <ol>
- <li><b>name</b> - a string (icon number 3)
- <li><b>age</b> - an integral number (icon number 4)
- </ol>
- <p>
- The constructor initialises the values for the attributes and tells the
- DialogueBox class the icon number and data address of each. The icons
- representing the attributes should be writeable icons so that the user
- may fill them in.
- <p>
- When the user clicks on the OK button, the values he has types in to the
- icons are copied into the attributes value. If he clicks on Cancel, the
- values types in are ignored. Also, clicking on Cancel sets a variable
- called 'cancelled' inside the DialogueBox and this can be interrogated
- by the caller.
- <p>
- Attributes may also be created manually by creating an object of a type
- derived from the type 'Attribute'. This is an abstract class which has
- a number of specific attribute types derived from it. The attribute
- types are:
- <p>
- <ol>
- <li><b>StringAttribute</b> - the value is a string
- <li><b>BoolAttribute</b> - the value is a boolean
- <li><b>IntegerAttribute</b> - the value is an integer
- <li><b>SetAttribute</b> - the value is a set of boolean values (array of words)
- </ol>
- <p>
- Once the attribute has been created it is automatically added to the dialogue box
- and deleted when the dialogue box is deleted.
- <p>
- <h3>Displaying a Dialogue Box</h3>
-
- In order to cause a dialogue box to appear on the screen, the function
- 'show()' is called. This opens the window and starts the thread running.
- The function that called 'show()' may then wait for the user to fill in
- and close the dialogue box by use of the 'sleep()' function.
- <p>
-
- For example, if we wanted to open the PersonDetails dialogue box and
- wait for the user to fill in the name and age, we would write the following
- code:
- <pre>
-
- void fill_in_details(Task *task)
- {
- PersonDetails details (task) ; // create the DialogueBox
- details.show() ; // show it on the screen
- task->sleep (&details) ; // sleep until it is closed
- if (!details.cancelled) // check if input was cancelled
- {
- // use the attribute values from details.
- }
- }
-
- </pre>
- The destructor for the PersonDetails dialogue box will be called
- at the end of the function when the variable 'details' goes out
- of scope.
- <p>
- If the dialogue box is attached to a menu selection it will behave
- differently. When Vista informs the task or window that the
- appropriate menu selection has been made, the menu handler function
- simply opens the DialogueBox. If the user then moves the mouse
- back over the menu, or clicks outside the dialogue box, it will
- be closed.
- <p>
- The code for opening a dialogue box off a menu selection would be:
- <pre>
-
- void MyWin::menu (MenuItem items[])
- {
- switch (items[0])
- {
- case DETAILS:
- {
- PersonDetails details (task) ;
- details.show() ;
- task->sleep (&details) ;
- if (!details.cancelled)
- {
- }
- break ;
- }
- case OTHERSELECTION:
- ...
- }
- }
-
- </pre>
-
- The task will awaken when the dialogue box thread has terminated. This
- will occur when the box closes for any reason.
- <p>
- <ul>
- <li><em>NOTE: the braces round the case limb for DETAILS are necessary. This is
- because there is a variable created ('details') if the appropriate
- menu selection is made, but the destructor for this variable will
- be called when the variable goes out of scope. This is fine if the
- menu selection was correct, but if another menu selection is made, the
- destructor will be called for a variable that hasn't been constructed.
- You could always get round this by using the 'new' and 'delete' operators
- to create the dialogue box, but this will have a performance hit as
- it will allocate the memory from the heap.</em>
- </ul>
-
-
-
- <p>
- <hr>
- <p>
- <a name="grid"></a>
- <h2>IconGrid</h2>
-
- This class behaves like a Filer window. It holds and displays a set of
- icons in a grid formation. It is derived directly from the <a href="file:window">Window</a>
- class and is defined as:
- <pre>
-
- class IconGrid : public Window
- {
- public:
- enum flags
- {
- NONE,
- NOSELECT = 0x01, // click doesn't select icons
- NODESELECT = 0x02, // double-click doesn't deselect
- SORTED = 0x04, // keep icons sorted
- } ;
- IconGrid (Task *t, char *tname, int ticon, char *menu = 0) ;
- IconGrid (Window *w, char *tname, int ticon, char *menu = 0) ;
- virtual ~IconGrid() ;
- Icon *insert_icon(char *text, void *ref = 0) ;
- Icon *insert_icon(char *text, char *sprite, void *ref = 0) ;
- void delete_icon (Icon *) ;
- void delete_icon (void *ref) ; // delete icon with ref
- void open (int x0, int y0, int x1, int y1, int scx, int scy, int behind) ;
- void click (int mx, int my, int buttons, int icon) ;
- virtual void double_click (int mx, int my, int buttons, Icon *icon) ;
- virtual void drag_icon (int mx, int my, int buttons, Icon *icon) ;
- virtual void drag_selection (int mx, int my, int buttons, int x0, int y0, int x1, int y1) ;
- Icon *find_icon (int icon) ;
- virtual void select_all () ; // select all icons
- virtual void clear_selection() ; // clear selection
- void drag (int x0, int y0, int x1, int y1, int id) ;
- void set_flag (flags flag) ;
- void clear_flag (flags flag) ;
- virtual void sort() ; // sort the icons
-
- protected:
- Icon *template_icon ; // icon to copy
- int num_icons ; // number of icons in the window
- int num_columns ; // number of columns
- int num_rows ; // number of rows
- int icon_width ; // width of icons
- int icon_height ; // height of icons
- int current_column ; // current column number
- int current_row ; // current row number
- Icon::buttontype template_button_type;
- flags flag_set ;
- int min_height ; // minimum height of window in OS units
-
- void rearrange() ; // rearrange the icons in the window
- void init (int ticon) ; // initialise
- } ;
-
- </pre>
- The IconGrid handles the rearrangement of the icons during resizing operations.
- It will keep the icons in a grid which is wide enough to fit in the
- window. It can keep the icons sorted or unsorted depending on the
- value of a flag.
- <p>
- The window works by having a 'template icon' available at all times. Every
- time a new icon is <a href="file:wins#insert">inserted,</a> the template icon is copied.
- Usually the
- template icon will be of type 'Text and Sprite'. The template
- icon is never visible.
- <p>
- When creating the IconGrid object, the template icon's number is
- supplied to the constructor. This can be positioned anywhere in
- the window as it will be moved out of the way at startup.
- <p>
- If the icon grid is not resizeable and consists of one column, then
- it behaves like a 'Scrolling List'.
- <p>
- The user may select icons in the grid in exactly the same way
- as a in filer windows. The icons can also be dragged out
- of the window.
- <p>
- Because the IconGrid is derived from the Windoe class, it inherits
- all the member functions and facilities provided by the Window
- class.
-
- <p>
- <a name="insert"></a>
- <h3>Inserting and deleting icons</h3>
-
- In order to insert a new icon into an IconGrid, the function 'insert_icon'
- is called. This function has 2 versions available depending on whether the
- icon is to have the same sprite as the template icon or not. A new icon
- is created by taking a copy of the template icon and moving the new
- icon to the appropriate position. The icon created is returned to the
- caller, so the program may write to the icon text (using the Icon::print()
- function) or change the sprite (using the Icon::change_sprite() function)
- <p>
- An icon is deleted by using the 'delete_icon' function. You can either
- delete an icon given a pointer to it (returned from 'insert_icon')
- or you may delete it using its user reference. If it is deleted
- using the user reference, the first icon found whose user reference
- matches that passed will be deleted.
-
- <p>
- <hr>
- <p>
- <a name="save"></a>
- <h2>SaveBox</h2>
-
- A SaveBox class is a DialogueBox that allows the user to save
- something by dragging an icon to a filer window or another application.
- It displays a window containing a sprite that may be dragged and
- a writeable icon.
- <p>
- The SaveBox is defined as:
- <pre>
-
- class SaveBox : public DialogueBox
- {
- public:
- SaveBox (Task *t, char *tname, char *path, char *leafname, DataSave *saver) ;
- ~SaveBox() ;
- void drag (int x0, int y0, int x1, int y1, int id) ; // end of drag operation
- void ok(int button) ;
- protected:
- DataSave *saver ;
- char path[256] ;
- Icon *file_icon ;
- Attribute *name ;
- } ;
-
- </pre>
- As can be seen, it is derived from the DialogueBox class and therefore
- inherits all the facilities of it.
- <p>
- The SaveBox is created using the constructor. This takes the following
- parameters:
- <p>
- <ol>
- <li>The <em>task</em>
- <li>The name of the <em>template</em> to use
- <li>The <em>full path</em> to save to if it exists. If there is no path, this should
- be an empty string (not a NULL pointer, but "")
- <li>The name of the <em>leaf</em> to use if there is no path supplied.
- <li>A pointer to an <em>object</em> to actually do the saving.
- </ol>
- <p>
- The SaveBox will invoke functions in the object called the 'saver' and
- passed at the last parameter to the constructor. This object is
- specific to the data being saved and is derived from the DataSave
- class. The user can invoke a save operation in 2 ways:
- <p>
- <ol>
- <li>He can drag the sprite to a filer window
- <li>He can click on the 'Save' button in the SaveBox.
- </ol>
- <p>
- Both these operations result in a call to the saver. The DataSave class is defined:
- <pre>
-
- class DataSave : public Channel
- {
- public:
- struct saveackdata
- {
- int window ;
- int icon ;
- int x ;
- int y ;
- int est_size ;
- int filetype ;
- char pathname[1] ;
- } ;
- DataSave(Task *task) ;
- virtual ~DataSave() ;
- virtual void receive (int action, int task, int my_ref,
- int your_ref, int data_length, void *data) = 0 ;
- int accept (int your_ref) ; // accept a message?
- virtual void save (int window, int icon, int x, int y, char *leaf) ;
- virtual void save (char *path) ;
- void datasave (int window, int icon, int x, int y, int est_size, int filetype, char *leaf) ;
- void datasaveack (int my_ref, saveackdata *data, char *path) ;
- void dataload (int task, int my_ref, int your_ref, int data_length, void *data) ;
- void dataloadack (int task, int my_ref, int your_ref, int data_length, void *data) ;
- protected:
- int my_ref ;
- } ;
-
- </pre>
- The important functions are the 2 'save' functions. The first save function
- is called when the user drags the icon to a filer window (or another
- application). The second function is called when the user has clicked on
- the 'Save' button and a full path is available.
- <p>
- For further information see the section on <a href="file:channel">Communications</a>.
-
- <p>
- <hr>
- <p>
- <a name="prog"></a>
- <h2>ProgramInfo</h2>
-
- This is a simple dialogue box which displays information about the program being
- run. When the dialogue box is created, you pass a version string to it and
- tell it what the icon number for the version icon is. The template for the
- window should have the other information already contained in it.
- <p>
- The class is simply defined as:
- <pre>
-
- class ProgramInfo : public DialogueBox
- {
- public:
- ProgramInfo (Task *t, int version_icon, char *version) ;
- ~ProgramInfo() ;
- private:
- Icon *version ;
- } ;
-
- </pre>
- If other fields are required in the dialogue box, other classes may be derived
- from this one.
-
-