📄 dialog.c
字号:
/**** dialog.c ****//*********************************************************************** Copyright (c) 1991, 1992 Iris Computing Laboratories.** This software is provided for demonstration purposes only. As* freely-distributed, modifiable source code, this software carries* absolutely no warranty. Iris Computing Laboratories disclaims* all warranties for this software, including any implied warranties* of merchantability and fitness, and shall not be liable for* damages of any type resulting from its use.* Permission to use, copy, modify, and distribute this source code* for any purpose and without fee is hereby granted, provided that* the above copyright and this permission notice appear in all copies* and supporting documentation, and provided that Iris Computing* Laboratories not be named in advertising or publicity pertaining* to the redistribution of this software without specific, written* prior permission.**********************************************************************//**************************************************************************A `Dialog' object is a Motif dialog box in a shell that supportsobject-level configuration for modal or nonmodal behavior, numberof buttons, and optional string input.A `Dialog' object maintains its state between invocations of thedialog box, making it convenient for the application to retrievedata stored with the dialog, such as a user's string response (inthe case of a dialog box configured for string input) and/or thedata associated with the most recently selected button.Sample usage:#include "dialog.h"......void main(argc, argv)int argc;char *argv[];{ void DialogSelection1(), ...; Widget topLevel, ...; Dialog dialog1, dialog2; Arg args[10]; int i; static int one = 1, two = 2, three = 3; static DialogButtonItem items1[] = { {"Button 1", DialogSelection1, (XtPointer) &one}, {"Button 2", DialogSelection1, (XtPointer) &two}, {"Button 3", DialogSelection1, (XtPointer) &three}, {NULL, NULL, NULL}, }; static DialogButtonItem items2[] = { {"Dismiss", Dismiss, (XtPointer) "one"}, {"Button 2", DialogSelection2, (XtPointer) "two"}, {"Button 3", DialogSelection2, (XtPointer) "three"}, {"Button 4", DialogSelection2, (XtPointer) "four"}, {NULL, NULL, NULL}, }; XtAppContext app; topLevel = XtAppInitialize(&app, "TestDialog", (XrmOptionDescList) NULL, 0, &argc, argv, (String *) NULL, (ArgList) NULL, 0);......\*********************************************************************** dialog_create(app, parent, instance_name, items, num_columns, title, prompt, char_set, dialog_position, modal_dialog, max_win_mgr, string_input, auto_popdown, default_button);***********************************************************************\ dialog1 = dialog_create(app, topLevel, "dialog1", items1, 30, "Dialog Box #1", "This is only a test...", XmSTRING_DEFAULT_CHARSET, dialog_CENTER, TRUE, FALSE, FALSE, TRUE, 0); dialog2 = dialog_create(app, topLevel, "dialog2", items2, 30, "Dialog Box #2", "This is only a test...", XmSTRING_DEFAULT_CHARSET, dialog_DEFAULT, FALSE, TRUE, TRUE, FALSE, 0);...... XtRealizeWidget(topLevel); dialog_realize(dialog1); dialog_realize(dialog2);......NOTE: A `Dialog' stores pointers (XtPointer) to client data for theconvenience of the application programmer. In particular, a `Dialog'objects stores the client data associated with the most recentlyselected button, and provides an access function that retrieves thisvalue, dialog_get_most_recent_button_response(). Thus, theapplication should always pass addresses as client data. Becauseclient data can be of any type, there is no reason that the `Dialog'object should interpret the client data's type, or store it locally.In particular, if the application wants to associate integer data witheach button, it should define storage for the data in the application(within the scope of its anticipated retrieval of the data withdialog_get_most_recent_button_response()), pass the address of thisdata in the array of button data, and then interpret (dereference)the address that's returned. E.g., in the first example below, it'sincorrect to pass the *value* of the integer, because Xt will storethat value internally and it may then be out of scope in a futurereference. Examples:main(...)...{ static DialogButtonItem items1[] = { {"Button 1", DialogSelection1, (XtPointer) 1}, // wrong // {"Button 2", DialogSelection1, (XtPointer) 2}, // wrong // {"Button 3", DialogSelection1, (XtPointer) 3}, // wrong // {NULL, NULL, NULL}, }; ... ... { int v = (int) dialog_get_most_recent_button_response(dialog); // wrong // printf("Most recently selected button's value: %d\n", v); // wrong // } ...}main(...)...{ static int one = 1, two = 2, three = 3; static DialogButtonItem items1[] = { {"Button 1", DialogSelection1, (XtPointer) &one}, // right // {"Button 2", DialogSelection1, (XtPointer) &two}, // right // {"Button 3", DialogSelection1, (XtPointer) &three}, // right // {NULL, NULL, NULL}, }; ... ... { int *v = (int *) dialog_get_most_recent_button_response(dialog); // right // if (v) printf("Most recently selected button's value: %d\n", *v); // right // } ...}**************************************************************************/#include "dialog.h"/*Private callback functions:*/static void DialogDismiss();static void UpdateButtonResponse();/*Private support functions:*/static void popup_and_wait_for_button_response();static void position_dialog();/*Public functions:*//*dialog_create() creates an dialog box object, optionally witha single-line edit window for the user's string response.*/Dialog dialog_create(app, parent, instance_name, items, num_columns, title, prompt, char_set, dialog_position, modal_dialog, max_win_mgr, string_input, auto_popdown, default_button)XtAppContext app;Widget parent;char *instance_name;DialogButtonItem *items;int num_columns;char *title, *prompt;XmStringCharSet char_set;int dialog_position;Boolean modal_dialog, max_win_mgr, string_input, auto_popdown;int default_button;{ Arg args[15]; int fraction_base, i, left_pos, n, num_btns, space_incr; Dimension height, margin_height; XmString string; Dialog dObject; for (num_btns = 0; items[num_btns].label; num_btns++) /* count the items */; if (num_btns < 1) return 0; if (!(dObject = (Dialog) XtMalloc((Cardinal) sizeof(_Dialog)))) return 0; dObject->self = dObject; dObject->num_buttons = num_btns; dObject->app = app; /* used with modal dialog boxes */ dObject->char_set = char_set; dObject->dialog_position = dialog_position; dObject->modal_dialog = modal_dialog; dObject->max_win_mgr = max_win_mgr; dObject->dialogText = NULL; /* instantiation is optional */ dObject->most_recent_button_data = NULL; dObject->button_pressed = dialog_BTN_NULL; /* not currently used */ dObject->remember_string_response = True; i = 0; if (title && *title) { XtSetArg(args[i], XmNtitle, (XtArgVal) title); i++; } XtSetArg(args[i], XmNallowShellResize, (XtArgVal) True); i++; XtSetArg(args[i], XmNmappedWhenManaged, (XtArgVal) False); i++; dObject->dialogShell = XtCreatePopupShell("dialogShell", max_win_mgr ? topLevelShellWidgetClass : transientShellWidgetClass, parent, args, i); /* instances have precedence over classes: */ dObject->instance = XtCreateManagedWidget(instance_name, xmFrameWidgetClass, dObject->dialogShell, NULL, 0); dObject->class = XtCreateManagedWidget("Dialog", xmFrameWidgetClass, dObject->instance, NULL, 0); i = 0; XtSetArg(args[i], XmNsashWidth, (XtArgVal) 1); i++; XtSetArg(args[i], XmNsashHeight, (XtArgVal) 1); i++; dObject->dialogPane = XtCreateManagedWidget("dialogPane", xmPanedWindowWidgetClass, dObject->class, args, i); i = 0;/* XtSetArg(args[i], XmNhorizontalSpacing, (XtArgVal) 5); i++; XtSetArg(args[i], XmNverticalSpacing, (XtArgVal) 5); i++;*/ XtSetArg(args[i], XmNallowResize, (XtArgVal) True); i++; dObject->dialogControl = XtCreateManagedWidget("dialogControl", xmFormWidgetClass, dObject->dialogPane, args, i); i = 0; XtSetArg(args[i], XmNleftAttachment, (XtArgVal) XmATTACH_FORM); i++; XtSetArg(args[i], XmNrightAttachment, (XtArgVal) XmATTACH_FORM); i++; XtSetArg(args[i], XmNtopAttachment, (XtArgVal) XmATTACH_FORM); i++; if (!string_input) { XtSetArg(args[i], XmNbottomAttachment, (XtArgVal) XmATTACH_FORM); i++; } if (!prompt) prompt = ""; string = XmStringCreateLtoR(prompt, char_set); XtSetArg(args[i], XmNlabelString, string); i++; XtSetArg(args[i], XmNresizable, (XtArgVal) True); i++; dObject->dialogPrompt = XtCreateManagedWidget("dialogPrompt", xmLabelWidgetClass, dObject->dialogControl, args, i); XmStringFree(string); if (string_input) { i = 0; XtSetArg(args[i], XmNleftAttachment, (XtArgVal) XmATTACH_FORM); i++; XtSetArg(args[i], XmNrightAttachment, (XtArgVal) XmATTACH_FORM); i++; XtSetArg(args[i], XmNtopAttachment, (XtArgVal) XmATTACH_WIDGET); i++; XtSetArg(args[i], XmNtopWidget, (XtArgVal) dObject->dialogPrompt); i++; XtSetArg(args[i], XmNborderWidth, (XtArgVal) 0); i++; XtSetArg(args[i], XmNvalue, (XtArgVal) ""); i++; if (num_columns > 0) { XtSetArg(args[i], XmNcolumns, (XtArgVal) num_columns); i++; } dObject->dialogText = XtCreateManagedWidget("dialogText", xmTextWidgetClass, dObject->dialogControl, args, i); } if (default_button != dialog_NO_DEFAULT_BTN) if (default_button < 0 || default_button >= num_btns) default_button = 0; space_incr = 1; if (num_btns == 1) fraction_base = 5; else if (num_btns == 2) fraction_base = (num_btns * 2) + 1; else { space_incr = dialog_BTN_WIDTH_FACTOR; fraction_base = (dialog_BTN_WIDTH_FACTOR * num_btns) + ((dialog_BTN_WIDTH_FACTOR / dialog_BTN_SPACE_FACTOR) * (num_btns * 2)); } i = 0;/* XtSetArg(args[i], XmNhorizontalSpacing, (XtArgVal) 5); i++; XtSetArg(args[i], XmNverticalSpacing, (XtArgVal) 5); i++;*/ XtSetArg(args[i], XmNfractionBase, (XtArgVal) fraction_base); i++; dObject->dialogAction = XtCreateManagedWidget("dialogAction", xmFormWidgetClass, dObject->dialogPane, args, i); dObject->buttons = (Widget *) XtMalloc((Cardinal) (num_btns * sizeof(Widget))); for (left_pos = n = 0; n < num_btns; n++) { i = 0; XtSetArg(args[i], XmNuserData, (XtArgVal) items[n].client_data); i++; XtSetArg(args[i], XmNtopAttachment, (XtArgVal) XmATTACH_FORM); i++; XtSetArg(args[i], XmNbottomAttachment, (XtArgVal) XmATTACH_FORM); i++; XtSetArg(args[i], XmNleftAttachment, (XtArgVal) XmATTACH_POSITION); i++; left_pos += space_incr / dialog_BTN_SPACE_FACTOR; XtSetArg(args[i], XmNleftPosition, (XtArgVal) (space_incr == 1) ? ((num_btns == 1) ? 2 : ((2 * n) + 1)) : left_pos); i++; XtSetArg(args[i], XmNrightAttachment, (XtArgVal) XmATTACH_POSITION); i++; XtSetArg(args[i], XmNrightPosition, (XtArgVal) (space_incr == 1) ? ((num_btns == 1) ? 3 : ((n + 1) * 2)) : (left_pos + space_incr)); i++; left_pos += space_incr + (space_incr / dialog_BTN_SPACE_FACTOR); if (default_button != dialog_NO_DEFAULT_BTN) { XtSetArg(args[i], XmNshowAsDefault, (XtArgVal) (n == default_button)); i++; XtSetArg(args[i], XmNdefaultButtonShadowThickness, (XtArgVal) 1); i++; } dObject->buttons[n] = XtCreateManagedWidget(items[n].label, xmPushButtonWidgetClass, dObject->dialogAction, args, i); XtAddCallback(dObject->buttons[n], XmNactivateCallback, (XtCallbackProc) UpdateButtonResponse, (XtPointer) dObject); if (items[n].callback) XtAddCallback(dObject->buttons[n], XmNactivateCallback, (XtCallbackProc) items[n].callback, (XtPointer) items[n].client_data); if (auto_popdown) XtAddCallback(dObject->buttons[n], XmNactivateCallback, (XtCallbackProc) DialogDismiss, (XtPointer) dObject); } i = 0; XtSetArg(args[i], XmNmarginHeight, &margin_height); i++; XtGetValues(dObject->dialogAction, args, i); i = 0; XtSetArg(args[i], XmNheight, &height); i++; XtGetValues(dObject->buttons[0], args, i); i = 0; XtSetArg(args[i], XmNpaneMinimum, (XtArgVal) (height + (margin_height * 2))); i++; XtSetArg(args[i], XmNpaneMaximum, (XtArgVal) (height + (margin_height * 2))); i++; if (default_button != dialog_NO_DEFAULT_BTN) { XtSetArg(args[i], XmNdefaultButton, (XtArgVal) dObject->buttons[default_button]); i++; } XtSetValues(dObject->dialogAction, args, i); remove_sash_traversal(dObject->dialogPane); return dObject;} /* dialog_create *//*dialog_destroy() frees the storage for a `Dialog' object.*/void dialog_destroy(dObject)Dialog dObject;{ XtDestroyWidget(dObject->dialogShell); XtFree(dObject->buttons); /* free the array for the */ /* XmPushButton widget IDs */ XtFree(dObject->self);} /* dialog_destroy *//*dialog_realize() realizes the editor object, specifically, thetop-level shell window used by the search-and-replace window.*/void dialog_realize(dObject)Dialog dObject;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -