Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dialog Widgets Dialog widgets present information and collect input from the user: Dialogs are windows that are often displayed for a relatively short.

Similar presentations


Presentation on theme: "Dialog Widgets Dialog widgets present information and collect input from the user: Dialogs are windows that are often displayed for a relatively short."— Presentation transcript:

1 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.

2 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

3 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

4 Example 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 ); The following code creates a top-level shell to act as a parent for some example dialogs:

5 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.

6 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.

7 Error Dialog Display

8 About the Error Dialog  Note the message icon  Note the default placement of the dialog at (0,0)  Note the default set of buttons

9 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.

10 Warning Dialog Display

11 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

12 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.

13 Information Dialog Display

14 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.

15 Working Dialog Display

16 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.

17 Question Dialog Display

18 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

19 Setting Dialog Modality  Resource: XmNdialogStyle  Values:  XmDIALOG_MODELESS  XmDIALOG_PRIMARY_APPLICATION_MODAL  XmDIALOG_FULL_APPLICATION_MODAL  XmDIALOG_SYSTEM_MODAL

20 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.

21 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.

22 Recall the Class Diagram Board canvas: Widget display: Display gc: GC Game state: CharArray count: Integer playerX: Player Dialog dialog: Widget Main shell: Widget app: XtAppContext board game board game startd endd summaryd * The start dialog startd is owned by the main program object.

23 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 );

24 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

25 Dialog Class Recall that a Dialog object has a Game attribute by association.

26 DialogInfo Class Definition class DialogInfo { private: Widget dialog; // the dialog widget Game game; // the game state public: DialogInfo(Text name, Widget parent, Game g); void manage(Text msg); // expose dialog with msg private: DECL_CALLBACK(startOk); // Computer plays first DECL_CALLBACK(startCancel); // User plays first };

27 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); }

28 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)

29 Start Dialog Callbacks IMPL_CALLBACK(DialogInfo, startOk) { // Computer plays first game->init(COMPUTER); } IMPL_CALLBACK(DialogInfo, startCancel) { // User plays first game->init(USER); }

30 DialogInfo::manage Method void DialogInfo::manage(Text msg) { // expose dialog with message XmString xmstr = XmStringCreateLocalized(msg); XtVaSetValues (dialog, XmNmessageString, xmstr, NULL); XtManageChild(dialog); } startd->manage("Computer plays first? (Cancel for NO)"); Recall how the main program gets the start dialog to display:


Download ppt "Dialog Widgets Dialog widgets present information and collect input from the user: Dialogs are windows that are often displayed for a relatively short."

Similar presentations


Ads by Google