VDialog

Class to build a modeless dialog.

Synopsis

 * Header:
 * 


 * Class name:
 * vDialog


 * Hierarchy:
 * (vBaseWindow,vCmdParent) ->vDialog


 * Contains:
 * [CommandObject]

Description
The vDialog class is used to build modeless dialogs. Since most dialogs will require a response to the commands they define, you will almost always derive your own subclass based on vDialog, and override the DialogCommand method to handle those commands. Note that vDialog is multiply derived from the vBaseWindow and the vCmdParent classes.

Constructor
vDialog(vBaseWindow* parent) vDialog(vApp* parent) vDialog(vBaseWindow* parent, int isModal = 0, char* title = "") vDialog(vApp* parent, int isModal = 0, char* title = "")

A dialog is constructed by calling it with a pointer to a vBaseWindow or vApp, which is usually the 'this' of the object that creates the vDialog</tt>. The isModal</tt> parameter indicates if the dialog should be modal or modeless. You would usually use the default of 0. The modal flag is used by the derived vModalDialog</tt> class. The title</tt> parameter can be used to set a title for your dialog (see SetDialogTitle</tt> for information on titles). If you create a derived dialog class, you might provide a parent</tt> and a title</tt> in your constructor, and provide the 0 for the isModal</tt> flag in the call to the vDialog</tt> constructor.

The constructor builds an empty dialog. The AddDialogCmds</tt> method must be called in order to build a useful dialog, which you would usually do from within the constructor of your derived dialog class.

IMPORTANT! When you derive your own vDialog</tt> objects, you should write constructors for both the vBaseWindow*</tt> and vApp*</tt> versions. These two different constructors allow dialogs to be used both from windows directly, and from the vApp</tt> code as well. Normally, you would construct a dialog from a window. Occasionally, it will be useful to build a dialog from the vApp that applies to all windows, and not just the window that constructed it.

void vDialog::AddDialogCmds(CommandObject* cList)

This method is used to add a list of commands to a dialog. It is called after the dialog object has been created. You can usually do this in the constructor for your derived Dialog class. This method is passed an array of CommandObject</tt> structures.

void vDialog::SetDialogTitle(char* title)

This can be used to dynamically change the title of any object derived from a vDialog</tt> object. Note that the title will not always be displayed. This depends on the host system. For example, the user can set up their X window manager to not show decorations on transient windows, which is how dialogs are implemented on X. You should write your applications to provide a meaningful title as they are often helpful when displayed.

Example
This example shows the steps required to use a dialog object. Note that the example uses the vDialog</tt> class directly, and thus only uses the default behavior of responding to the <tt>OK</tt> button.



CommandObject cmdList[] =          // list of the commands {        {C_Label, lbl1, 0, "Label",NoList,CA_MainMsg,isSens,0,0}, {C_Button, M_OK, M_OK, " OK ", NoList, CA_DefaultButton, isSens,lbl1,0}, {C_EndOfList,0,0,0,0,CA_None,0,0} // This ends list };    ...     vDialog curDialog(this,0,"Sample Dialog"); // create dialog instance curDialog.AddDialogCmds(cmdList);         // add the commands curDialog.ShowDialog("Sample modeless dialog."); // invoke ...
 * 1) include <v/vdialog.h>

This example creates a simple modeless dialog with a label and an OK button placed below the label (see the description of layout control below). <tt>ShowDialog</tt> displays the dialog, and the <tt>vDialog::DialogCommand</tt> method will be invoked with the id (2) and value (<tt>M_OK</tt>) of the OK button when it is pressed.

Use the <tt>vModalDialog</tt> class to define modal dialogs.

The <tt>CommandObject</tt> structure includes the following: typedef struct CommandObject {        CmdType cmdType;    // what kind of item is this ItemVal cmdId;     // unique id for the item ItemVal retVal;    // initial value // depends on type of command char* title;       // string for label or title void* itemList;    // a list of stuff to use for the cmd // depends on type of command CmdAttribute attrs; // list of attributes of command unsigned Sensitive:1;  // if item is sensitive or not ItemVal cFrame;    // if item part of a frame ItemVal cRightOf;  // Item placed left of this id         ItemVal cBelow;     // Item placed below this one int size;          // Used for size information char* tip;         // tool tip string } CommandObject;

Placements of command objects within the dialog box are controlled by the <tt>cRightOf</tt> and <tt>cBelow</tt> fields. By specifying where an object goes in relation to other command objects in the dialog (using their <tt>cmdId</tt> value), it is simple to get a very pleasing layout of the dialog. The exact spacing of command objects is controlled by the <tt>vDialog</tt> class, but the application can used <tt>C_Blank</tt> command objects to help control spacing. See [cmdobj.htm#CommandObject CommandObject] for more details.

The various types of command objects that can be added include (with suggested id prefix in parens): C_EndOfList:  Used to denote end of command list C_Blank:      filler to help RightOfs, Belows work (blk) C_BoxedLabel: a label with a box (bxl) C_Button:     Button (btn) C_CheckBox:   Checked Item (chk) C_ColorButton: Colored button (cbt) C_ColorLabel: Colored label (clb) C_ComboBox:   Popup combo list (cbx) C_Frame:      General purpose frame (frm) C_Icon:       a display only Icon (ico) C_IconButton: a command button Icon (icb) C_Label:      Regular text label (lbl) C_List:       List of items (lst) C_ProgressBar: Bar to show progress (pbr) C_RadioButton: Radio button (rdb) C_Slider:     Slider to enter value (sld) C_Spinner:    Spinner value entry (spn) C_TextIn:     Text input field (txi) C_Text:       wrapping text out (txt) C_ToggleButton: a toggle button (tbt) C_ToggleFrame: a toggle frame (tfr) C_ToggleIconButton: a toggle Icon button (tib)

These command values are passed to the <tt>vDialog::DialogCommand</tt> function, which you override to interpret commands. See [cmdobj.htm#Commands Command Objects] for more details about the commands. virtual void CancelDialog This method is used to cancel any action that took place in the dialog. The values of any items in the dialog are reset to their original values, and the This method is automatically invoked when the user selects a button with the value <tt>M_Cancel</tt> and the <tt>DialogCommand</tt> method invoked as appropriate to reset values of check boxes and so on. <tt>CancelDialog</tt> can also be invoked by the application code.

virtual void CloseDialog The <tt>CloseDialog</tt> is used to close the dialog. It can be called by user code, and is automatically invoked if the user selects the <tt>M_Done</tt> or <tt>M_OK</tt> buttons and the user either doesn't override the <tt>DialogCommand</tt> or calls the default <tt>DialogCommand</tt> from any derived <tt>DialogCommand</tt> methods. virtual void DialogCommand(ItemVal Id, ItemVal Val, CmdType Type) This method is invoked when a user selects some command item of the dialog. The default <tt>DialogCommand</tt> method will normally be overridden by a user derived class. It is useful to call the default <tt>DialogCommand</tt> from the derived method for default handling of the <tt>M_Cancel</tt> and <tt>M_OK</tt> buttons.

The <tt>Id</tt> parameter is the value of the <tt>cmdId</tt> field of the <tt>CommandObject</tt> structure. The <tt>Val</tt> parameter is the <tt>retVal</tt> value, and the <tt>Type</tt> is the <tt>cmdType</tt>.

The user defined <tt>DialogCommand</tt> is where most of the work defined by the dialog is done. Typically the derived <tt>DialogCommand</tt> will have a <tt>switch</tt> statement with a <tt>case</tt> for each of the command <tt>cmdId</tt> values defined for items in the dialog.

void DialogDisplayed

This method is called by the V runtime system after a dialog has actually been displayed on the screen. This method is especially useful to override to set values of dialog controls with <tt>SetValue</tt> and <tt>SetString</tt>.

It is important to understand that the dialog does not get displayed until <tt>ShowDialog</tt> or <tt>ShowModalDialog</tt> has been called. There is a very important practical limitation implied by this, especially for modal dialogs. The values of controls cannot be changed until the dialog has been displayed, even though the <tt>vDialog</tt> object may exist. Thus, you can't call <tt>SetValue</tt> or <tt>SetString</tt> until after you call <tt>ShowDialog</tt> for modeless dialogs, or <tt>ShowModalDialog</tt> for modal dialogs. Since <tt>ShowModalDialog</tt> does not return until the user has closed the dialog, you must override <tt>DialogDisplayed</tt> if you want to change the values of controls in a modal dialog dynamically.

For most applications, this is not a problem because the static definitions of controls in the <tt>CommandObject</tt> definition will be usually be what is needed. However, if you need to create a dialog that has those values changed at runtime, then the easiest way is to include the required <tt>SetValue</tt> and <tt>SetString</tt> calls inside the overridden <tt>DialogDisplayed</tt>.

void GetDialogPosition(int& left, int& top, int& width, int& height)

Returns the position and size of <tt>this</tt> dialog. These values reflect the actual position and size on the screen of the dialog. The intent of this method is to allow you to find out where a dialog is so position it so that it doesn't cover a window. virtual int GetTextIn(ItemVal Id, char* str, int maxlen) This method is called by the application to retrieve any text entered into any <tt>C_TextIn</tt> items included in the dialog box. It will usually be called after the dialog is closed. You call <tt>GetTextIn</tt> with the <tt>Id</tt> of the TextIn command, the address of a buffer (<tt>str</tt>), and the size of <tt>str</tt> in <tt>maxlen</tt>. virtual int GetValue(ItemVal Id) This method is called by the user code to retrieve values of command items, usually after the dialog is closed. The most typical use is to get the index of any item selected by the user in a <tt>C_List</tt> or <tt>C_ComboBox</tt>. int IsDisplayed This returns true if the dialog object is currently displayed, and false if it isn't. Typically, it will make sense only to have a single displayed instance of any dialog, and your code will want to create only one instance of any dialog. Since modal dialogs allow the user to continue to interact with the parent window, you must prevent multiple calls to <tt>ShowDialog</tt>. One way would be to make the command that displays the dialog to be insensitive. <tt>IsDisplayed</tt> is provided as an alternative method. You can check the <tt>IsDisplayed</tt> status before calling <tt>ShowDialog</tt>. virtual void SetDialogPosition(int left, int top) Moves <tt>this</tt> dialog to the location <tt>left</tt> and <tt>top</tt>. This function can be used to move dialogs so they don't cover other windows.

virtual void SetValue(ItemVal Id, ItemVal val, ItemSetType type)

This method is used to change the state of dialog command items. The <tt>ItemSetType</tt> parameter is used to control what is set. Not all dialog command items can use all types of settings. The possibilities include:

Checked The <tt>Checked</tt> type is used to change the checked status of check boxes. V will normally handle checkboxes, but if you implement a command such as Check All, you can use <tt>SetValue</tt> to change the check state according to <tt>ItemVal val</tt>.

Sensitive The <tt>Sensitive</tt> type is used to change the sensitivity of a dialog command.

Value The <tt>Value</tt> type is used primarily to preselect the item specified by <tt>ItemVal val</tt> in a list or combo box list.

ChangeList, ChangeListPtr Lists, Combo Boxes, and Spinners use the <tt>itemList</tt> field of the defining <tt>CommandObject</tt> to specify an appropriate list. <tt>SetValue</tt> provides two ways to change the list values associated with these controls.

The key to using <tt>ChangeListPtr</tt> and <tt>ChangeList</tt> is an understanding of just how the controls use the list. When a list type control is instantiated, it keeps a private copy of the pointer to the original list as specified in the <tt>itemList</tt> field of the defining <tt>CommandObject</tt>.

So if you want to change the original list, then <tt>ChangeList</tt> is used. The original list may be longer or shorter, but it must be in the same place. Remember that a NULL entry marks the end of the list. So you could allocate a 100 item array, for example, and then reuse it to hold 0 to 100 items.

Call <tt>SetValue</tt> with <tt>type</tt> set to <tt>ChangeList</tt>. This will cause the list to be updated. Note that you must not change the <tt>itemList</tt> pointer used when you defined the list or combo box. The contents of the list can change, but the pointer must be the same. The <tt>val</tt> parameter is not used for <tt>ChangeList</tt>.

Sometimes, especially for regular list controls, a statically sized list just won't work. Using <tt>ChangeListPtr</tt> allows you to use dynamically created list, but with a small coding penalty. To use <tt>ChangeListPtr</tt>, you must first modify the contents of the <tt>itemList</tt> field of the original <tt>CommandObject</tt> definition to point to the new list. Then call <tt>SetValue</tt> with <tt>ChangeListPtr</tt>. Note that this will both update the pointer, and update the contents of the list. You don't need to call again with <tt>ChangeList</tt>.

The following illustrates using both types of list change: char* comboList[] = { "Bruce", "Katrina", "Risa", "Van", 0 }; char* list1[] = {"1", "2", "3", 0}; char* list2[] = {"A", "B", "C", "D", 0}; // The definition of the dialog CommandObject ListExample[] = { {C_ComboBox,100,0,"",(void*)comboList,CA_None,isSens,0,0,0}, {C_List,200,0,"",(void*)list1,CA_None,isSens,0,0,0}, ...    };    ...     // Change the contents of the combo list comboList[0] = "Wampler"; // Change Bruce to Wampler SetValue(200,0,ChangeList); ...    // Change to a new list entirely for list // Note that we have to change ListExample[1], the // original definition of the list control. ListExample[1].itemList = (void*)list2; // change to list2 SetValue(100,0,ChangeListPtr); ...

Note that this example uses static definitions of lists. It is perfectly fine to use completely dynamic lists: you just have to dynamically fill in the appropriate <tt>itemList</tt> field of the defining <tt>CommandObject</tt>.

Please see the description of <tt>DialogDisplayed</tt> for an important discussion of setting dialog control values. virtual void SetString(ItemVal Id, char* str) This method is called to set the string values of dialog items. This can include the labels on check boxes and radio buttons and labels, as well as the text value of a Text item.

Please see the description of <tt>DialogDisplayed</tt> for an important discussion of setting dialog control values. virtual void ShowDialog(char* message) After the dialog has been defined, it must then be displayed by calling the <tt>ShowDialog</tt> method. If a <tt>C_Label</tt> was defined with a <tt>CA_MainMsg</tt> attribute, then the message provided to <tt>ShowDialog</tt> will be used for that label.

<tt>ShowDialog</tt> returns to the calling code as soon as the dialog is displayed. It is up to the <tt>DialogCommand</tt> method to then handle command input to the dialog, and to close the dialog when done.

Please see the description of <tt>DialogDisplayed</tt> for an important discussion of setting dialog control values.

Derived Methods
None.

Inherited Methods
None.