ppt

Dialog Widgets
Dialog widgets present information and collect
input from the user:
Dialogs are windows that are often displayed for
a relatively short amount of time.
The user is expected to dismiss them when they
are no longer needed.
Dialog Classes
The basis of all dialogs is the TransientShell
widget class provided by Xt
Motif provides an XmDialogShell widget class
which is a subclass of TransientShell
Motif also provides an XmMessageBox class
upon which several pre-defined dialog types are
based
All dialogs have a modality, which controls
screen input while they are displayed
Pre-Defined Motif Dialogs
The following kinds of dialogs are pre-defined by
the XmMessageBox class:
Error
Warning
Information
Working
Question
They differ primarily in the type of icon presented
Example
The following code creates a top-level shell to act
as a parent for some example dialogs:
Widget shell,
errord, infod, warnd, workd, questiond;
XtAppContext app;
XmString msg;
ArgList args = new Arg[10];
Integer n = 0;
XtSetArg(args[n], XmNdefaultPosition, FALSE); n++;
XtSetArg(args[n], XmNheight, 100); n++;
XtSetArg(args[n], XmNwidth, 100); n++;
XtSetArg(args[n], XmNx, 400); n++;
XtSetArg(args[n], XmNy, 400); n++;
shell = XtAppInitialize ( &app, "Test", NULL, 0,
&argc, argv, NULL, args, n );
XtRealizeWidget ( shell );
XtAppMainLoop ( app );
Shell Widget Display
Height and width are 100 pixels.
Upper left corner is at (400,400) in root window.
Does not have any children yet.
Example Error Dialog
errord = XmCreateErrorDialog(shell, "error", NULL, 0);
msg = XmStringCreateLocalized("Bad input. Try again.");
XtVaSetValues(errord,
XmNmessageString, msg,
NULL);
XtManageChild(errord);
XmCreateErrorDialog is a convenience function.
errord is made a child of shell.
Note use of variable arguments instead of an ArgList.
XtManageChild will allow the dialog to appear when
shell is realized.
Error Dialog Display
About the Error Dialog
Note the message icon
Note the default placement of the dialog at (0,0)
Note the default set of buttons
Example Warning Dialog
warnd = XmCreateWarningDialog(shell, "warning", NULL, 0);
msg = XmStringCreateLocalized(
"Bad input. Using default value.");
XtVaSetValues(warnd,
XmNmessageString, msg,
XmNdefaultPosition, FALSE,
XmNx, 300,
XmNy, 0,
NULL);
XtManageChild(warnd);
The convenience function takes the same arguments.
Note that the placement (300,0) is explicitly given.
Still must set XmNdefaultPosition to FALSE.
Warning Dialog Display
About the Warning Dialog
The only difference is the icon
Both dialogs are children of the shell
If the shell is quit all windows disappear
Example Information Dialog
infod = XmCreateInformationDialog(shell, "information",
NULL, 0);
msg = XmStringCreateLocalized(
"Next we will get personal data.");
XtVaSetValues(infod,
XmNmessageString, msg,
XmNdefaultPosition, FALSE,
XmNx, 0,
XmNy, 200,
XmNokLabelString,XmStringCreateLocalized(
"CONTINUE"),
NULL);
XtManageChild(infod);
Note that the default label on the OK button is changed.
Information Dialog Display
Example Working Dialog
workd = XmCreateWorkingDialog(shell, "working", NULL, 0);
msg = XmStringCreateLocalized(
"Processing... click Cancel to quit.");
XtVaSetValues(workd,
XmNmessageString, msg,
XmNdefaultPosition, FALSE,
XmNx, 350,
XmNy, 200,
XmNdefaultButtonType, XmDIALOG_CANCEL_BUTTON,
NULL);
XtManageChild(workd);
Note that the Cancel button has been made the
default button type.
Working Dialog Display
Example Question Dialog
questiond = XmCreateQuestionDialog(shell, "question", NULL, 0);
msg = XmStringCreateLocalized("Shall we proceed?");
XtVaSetValues(questiond,
XmNmessageString, msg,
XmNdefaultPosition, FALSE,
XmNx, 0,
XmNy, 400,
XmNokLabelString, XmStringCreateLocalized("Yes"),
XmNcancelLabelString, XmStringCreateLocalized("No"),
NULL);
XtManageChild(questiond);
Note that both the Ok and Cancel button labels
have been changed.
Question Dialog Display
Dialog Modality
The previous examples use nonmodal dialogs.
Even though the dialog box is visible, the user is not
prevented from interacting with other windows on
the desktop.
There are 4 levels of modality:
Nonmodal
Primary application modal: input to the window that
launched the dialog is locked out but not others
Full application modal: input to all windows in the
application is locked out
Full system modal: input to all windows in all
applications in the system is locked out
Setting Dialog Modality
Resource: XmNdialogStyle
Values:
XmDIALOG_MODELESS
XmDIALOG_PRIMARY_APPLICATION_MODAL
XmDIALOG_FULL_APPLICATION_MODAL
XmDIALOG_SYSTEM_MODAL
Modal Dialog Example
questiond = XmCreateQuestionDialog(shell, "question", NULL, 0);
msg = XmStringCreateLocalized("Shall we proceed?");
XtVaSetValues(questiond,
XmNmessageString, msg,
XmNdefaultPosition, FALSE,
XmNx, 0,
XmNy, 400,
XmNokLabelString, XmStringCreateLocalized("Yes"),
XmNcancelLabelString, XmStringCreateLocalized("No"),
XmNdialogStyle, XmDIALOG_SYSTEM_MODAL,
NULL);
XtManageChild(questiond);
The question dialog will now insist that it be answered
before input to any other application is accepted.
Dialogs and Callbacks
Recall the opening dialog for the Tic-Tac-Toe game:
The program must deal (at least) with clicks on
the OK and Cancel buttons.
Recall the Class Diagram
Board
canvas: Widget board
display: Display
gc: GC
Game
game state: CharArray
count: Integer
game playerX: Player
game
board
Main
shell: Widget
app: XtAppContext
startd
endd
summaryd
Dialog
dialog: Widget
*
The start dialog startd is owned by the main program
object.
Tic-Tac-Toe Main Program
shell = XtAppInitialize ( &app, "TicTacToe", NULL, 0,
&argc, argv, NULL, NULL, 0 );
XtVaSetValues(shell, XmNdefaultPosition, FALSE,
XmNheight, 300, XmNwidth, 300,
XmNx, 400, XmNy, 400, NULL);
game = new GameInfo();
// Create a new game
board = new BoardInfo(shell, game);
// Create a board drawing area
game->setBoard(board);
// Tell game about board
startd = new DialogInfo("start", shell, game); // Create dialog
startd->manage("Computer plays first? (Cancel for NO)");
// expose dialog
XtRealizeWidget ( shell );
XtAppMainLoop ( app );
Tic-Tac-Toe Main Program (cont'd)
startd is not a Motif object but a C++
DialogInfo object
The Motif start dialog is an attribute of the
DialogInfo object
Multiple instances of DialogInfo will
ultimately be made for the program, including:
game ending dialog endd
game summary dialog summaryd
The message to be carried by the dialog is
given as a parameter to the manage method
Dialog Class
Recall that a Dialog object has a Game attribute
by association.
DialogInfo Class Definition
class DialogInfo {
private:
Widget dialog;
Game game;
public:
DialogInfo(Text name, Widget
void manage(Text msg);
private:
DECL_CALLBACK(startOk);
DECL_CALLBACK(startCancel);
};
// the dialog widget
// the game state
parent, Game g);
// expose dialog with msg
// Computer plays first
// User plays first
DialogInfo Class Constructor
DialogInfo::DialogInfo(Text name, Widget parent, Game g)
{
game = g;
dialog = XmCreateQuestionDialog (parent, name, NULL, 0);
XtVaSetValues (dialog,
XmNdefaultPosition, FALSE,
XmNx, 400,
XmNy, 200,
XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL,
NULL);
if (strcmp(name,"start")==0) {
// creating the start dialog
XtAddCallback (dialog, XmNokCallback,
&DialogInfo::startOkCallback, (XtPointer) this);
XtAddCallback (dialog, XmNcancelCallback,
&DialogInfo::startCancelCallback, (XtPointer) this);
}
}
DialogInfo Class Constructor
(cont'd)
Callbacks are registered depending on the type of
the dialog
Currently there is just one type (start)
The if statement could be extended to register
other callbacks for other dialog types (end and
summary)
Start Dialog Callbacks
IMPL_CALLBACK(DialogInfo, startOk)
{
// Computer plays first
game->init(COMPUTER);
}
IMPL_CALLBACK(DialogInfo, startCancel)
{
// User plays first
game->init(USER);
}
DialogInfo::manage Method
Recall how the main program gets the start dialog
to display:
startd->manage("Computer plays first? (Cancel for NO)");
void DialogInfo::manage(Text msg)
{
// expose dialog with
message
XmString xmstr = XmStringCreateLocalized(msg);
XtVaSetValues (dialog, XmNmessageString, xmstr, NULL);
XtManageChild(dialog);
}