📄 test11.c
字号:
{ Boolean done_with_dialog=False; /* ok to destroy dialog flag */ char *fileString; /* C string for file selected */ char *dirString; /* C string for dir of file selected */ XmString cFileString; /* compound string for file selected */ XmString cDir; /* compound directory selected */ XmString cPattern; /* compound filter pattern */ Widget help; /* help window form dialog */#if XmVersion < 1002 int i;#endif XtAddCallback(existFileSB, XmNokCallback, (XtCallbackProc)existOkCB, &done_with_dialog); XtAddCallback(existFileSB, XmNcancelCallback, (XtCallbackProc)existCancelCB, &done_with_dialog); AddMotifCloseCallback(XtParent(existFileSB), (XtCallbackProc)existCancelCB, &done_with_dialog);// help = createPanelHelp(existFileSB, HelpExist, "Selecting Files to Open");// createErrorDialog(existFileSB);// XtAddCallback(existFileSB, XmNhelpCallback, (XtCallbackProc)existHelpCB,// (char *)help);// if (DefaultDirectory != NULL || DefaultPattern != NULL)// XtVaSetValues(existFileSB, XmNdirectory, DefaultDirectory,// XmNpattern, DefaultPattern, NULL);#ifndef SGI_CUSTOM makeListTypeable(XmFileSelectionBoxGetChild(existFileSB,XmDIALOG_LIST)); makeListTypeable(XmFileSelectionBoxGetChild(existFileSB,XmDIALOG_DIR_LIST));#if XmVersion >= 1002 XtVaSetValues(existFileSB, XmNinitialFocus, XtParent( XmFileSelectionBoxGetChild(existFileSB, XmDIALOG_LIST)), NULL);#endif#endif ManageDialogCenteredOnPointer(existFileSB);#if 0 /* Typing in the directory list is dependent on the list being in the same form of alphabetical order expected by the character processing routines. As of about 1.2.3, some Motif libraries seem to have a different idea of ordering than is usual for Unix directories. To sort them properly, we have to patch the directory and file searching routines to re-sort the lists when they change */ XtVaGetValues(existFileSB, XmNdirSearchProc, &OrigDirSearchProc, XmNfileSearchProc, &OrigFileSearchProc, NULL); XtVaSetValues(existFileSB, XmNdirSearchProc, replacementDirSearchProc, XmNfileSearchProc, replacementFileSearchProc, NULL); sortWidgetList(XmFileSelectionBoxGetChild(existFileSB, XmDIALOG_DIR_LIST)); sortWidgetList(XmFileSelectionBoxGetChild(existFileSB, XmDIALOG_LIST));#endif /* SGI_CUSTOM */ while (!done_with_dialog) XtAppProcessEvent(XtWidgetToApplicationContext(existFileSB), XtIMAll); if (SelectResult == GFN_OK) { XtVaGetValues(existFileSB, XmNdirSpec, &cFileString, XmNdirectory, &cDir, XmNpattern, &cPattern, NULL); /* Undocumented: file selection box widget allocates copies of these strings on getValues calls. I have risked freeing them to avoid memory leaks, since I assume other developers have made this same realization, therefore OSF can't easily go back and change it */// if (DefaultDirectory != NULL) XmStringFree(DefaultDirectory);// if (DefaultPattern != NULL) XmStringFree(DefaultPattern);// DefaultDirectory = cDir;// DefaultPattern = cPattern; XmStringGetLtoR(cFileString, XmSTRING_DEFAULT_CHARSET, &fileString); /* Motif 2.x seem to contain a bug that causes it to return only the relative name of the file in XmNdirSpec when XmNpathMode is set to XmPATH_MODE_RELATIVE (through X resources), although the man page states that it always returns the full path name. We can easily work around this by checking that the first character of the file name is a `/'. */ if (fileString[0] == '/') { /* The directory name is already present in the file name or the user entered a full path name. */ strcpy(filename, fileString); } else { /* Concatenate the directory name and the file name */ XmStringGetLtoR(cDir, XmSTRING_DEFAULT_CHARSET, &dirString); strcpy(filename, dirString); strcat(filename, fileString); XtFree(dirString); } XmStringFree(cFileString); XtFree(fileString); } XtDestroyWidget(existFileSB); return SelectResult;}static int nKeystrokes = 0; /* Global key stroke history counter */static void listCharEH(Widget w, XtPointer callData, XEvent *event, Boolean *continueDispatch){ char charString[5], c, *itemString; int nChars, nItems, i, cmp, selectPos, topPos, nVisible; XmString *items; KeySym kSym; char name[MAXPATHLEN], path[MAXPATHLEN]; static char keystrokes[MAX_LIST_KEYSTROKES]; static Time lastKeyTime = 0; /* Get the ascii character code represented by the event */ nChars = XLookupString((XKeyEvent *)event, charString, sizeof(charString), &kSym, NULL); c = charString[0]; /* Process selected control keys, but otherwise ignore the keystroke if it isn't a single printable ascii character */ *continueDispatch = False; if (kSym==XK_BackSpace || kSym==XK_Delete) { nKeystrokes = nKeystrokes > 0 ? nKeystrokes-1 : 0; return; } else if (kSym==XK_Clear || kSym==XK_Cancel || kSym==XK_Break) { nKeystrokes = 0; return; } else if (nChars!=1 || c<0x021 || c>0x07e) { *continueDispatch = True; return; } /* Throw out keystrokes and start keystroke accumulation over from scratch if user waits more than MAX_LIST_KESTROKE_WAIT milliseconds */ if (((XKeyEvent *)event)->time - lastKeyTime > MAX_LIST_KESTROKE_WAIT) nKeystrokes = 0; lastKeyTime = ((XKeyEvent *)event)->time; /* Accumulate the current keystroke, just beep if there are too many */ if (nKeystrokes >= MAX_LIST_KEYSTROKES) XBell(XtDisplay(w), 0); else#ifdef VMS keystrokes[nKeystrokes++] = toupper(c);#else keystrokes[nKeystrokes++] = c;#endif /* Get the items (filenames) in the list widget */ XtVaGetValues(w, XmNitems, &items, XmNitemCount, &nItems, NULL); /* compare them with the accumulated user keystrokes & decide the appropriate line in the list widget to select */ selectPos = 0; for (i=0; i<nItems; i++) { XmStringGetLtoR(items[i], XmSTRING_DEFAULT_CHARSET, &itemString); if (ParseFilename(itemString, name, path) != 0) { XtFree(itemString); return; } XtFree(itemString); cmp = strncmp(name, keystrokes, nKeystrokes); if (cmp == 0) { selectPos = i+1; break; } else if (cmp > 0) { selectPos = i; break; } } /* Make the selection, and make sure it will be visible */ XmListSelectPos(w, selectPos, True); if (selectPos == 0) /* XmListSelectPos curiously returns 0 for last item */ selectPos = nItems + 1; XtVaGetValues(w, XmNtopItemPosition, &topPos, XmNvisibleItemCount, &nVisible, NULL); if (selectPos < topPos) XmListSetPos(w, selectPos-2 > 1 ? selectPos-2 : 1); else if (selectPos > topPos+nVisible-1) XmListSetBottomPos(w, selectPos+2 <= nItems ? selectPos+2 : 0); /* For LessTif 0.89.9. Obsolete now? */ XmListSelectPos(w, selectPos, True);}static Widget CreateMenu(Widget parent, char *name, char *label, char mnemonic, Widget *cascadeBtn, int mode){ Widget menu, cascade; XmString st1; menu = XmCreatePulldownMenu(parent, name, NULL, 0); cascade = XtVaCreateWidget(name, xmCascadeButtonWidgetClass, parent, XmNlabelString, st1=XmStringCreateSimple(label), XmNsubMenuId, menu, NULL); XmStringFree(st1); if (mnemonic != 0) XtVaSetValues(cascade, XmNmnemonic, mnemonic, NULL);#ifdef SGI_CUSTOM if (mode == SHORT || !GetPrefShortMenus()) XtManageChild(cascade); if (mode == FULL) addToToggleShortList(cascade);#else XtManageChild(cascade);#endif if (cascadeBtn != NULL) *cascadeBtn = cascade; return menu;} static voidmakeListTypeable(Widget listW){ XtAddEventHandler(listW, KeyPressMask, False, listCharEH, NULL);} static Widget createMenuItem(Widget parent, char *name, char *label, char mnemonic, menuCallbackProc callback, void *cbArg, int mode){ Widget button; XmString st1; button = XtVaCreateWidget(name, xmPushButtonWidgetClass, parent, XmNlabelString, st1=XmStringCreateSimple(label), XmNmnemonic, mnemonic, NULL); XtAddCallback(button, XmNactivateCallback, (XtCallbackProc)callback, cbArg); XmStringFree(st1); XtManageChild(button); return button;}int GetExistingFilename (Widget parent, char *promptString, char *filename){ int n; /* number of arguments */ Arg args[MAX_ARGS]; /* arg list */ Widget existFileSB; /* widget file select box */ XmString titleString; /* compound string for dialog title */ n = 0; titleString = XmStringCreateSimple(promptString); XtSetArg(args[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++; XtSetArg(args[n], XmNdialogTitle, titleString); n++; XtSetArg(args[n], XmNresizePolicy, XmRESIZE_GROW); n++; existFileSB = XmCreateFileSelectionDialog(parent,"FileSelect",args,n); XmStringFree(titleString);// if (RemoveRedundantTextField)// XtUnmanageChild(XmFileSelectionBoxGetChild(existFileSB, XmDIALOG_TEXT)); XtUnmanageChild(XmFileSelectionBoxGetChild(existFileSB, XmDIALOG_SELECTION_LABEL)); XtVaSetValues(XmFileSelectionBoxGetChild(existFileSB, XmDIALOG_FILTER_LABEL), XmNmnemonic, 'l', XmNuserData, XmFileSelectionBoxGetChild(existFileSB, XmDIALOG_FILTER_TEXT), NULL); XtVaSetValues(XmFileSelectionBoxGetChild(existFileSB, XmDIALOG_DIR_LIST_LABEL), XmNmnemonic, 'D', XmNuserData, XmFileSelectionBoxGetChild(existFileSB, XmDIALOG_DIR_LIST), NULL); XtVaSetValues(XmFileSelectionBoxGetChild(existFileSB, XmDIALOG_LIST_LABEL), XmNmnemonic, promptString[strspn(promptString, "lD")], XmNuserData, XmFileSelectionBoxGetChild(existFileSB, XmDIALOG_LIST), NULL);// AddDialogMnemonicHandler(existFileSB, FALSE);// RemapDeleteKey(XmFileSelectionBoxGetChild(existFileSB, XmDIALOG_FILTER_TEXT));// RemapDeleteKey(XmFileSelectionBoxGetChild(existFileSB, XmDIALOG_TEXT)); return HandleCustomExistFileSB(existFileSB, filename);}int PromptForExistingFile(Widget parent, char *prompt, char *fullname){ char *savedDefaultDir; int retVal; /* Temporarily set default directory to window->path, prompt for file, then, if the call was unsuccessful, restore the original default directory */ // savedDefaultDir = GetFileDialogDefaultDirectory();// if (*window->path != '\0')// SetFileDialogDefaultDirectory(window->path); retVal = GetExistingFilename(toplevel, prompt, fullname);// if (retVal != GFN_OK)// SetFileDialogDefaultDirectory(savedDefaultDir);// if (savedDefaultDir != NULL)// XtFree(savedDefaultDir); return retVal;} static void exitAP(Widget w, XEvent *event, String *args, Cardinal *nArgs){ exit(0);}static void openAP(Widget w, XEvent *event, String *args, Cardinal *nArgs){// WindowInfo *window = WidgetToWindow(w); char filename[MAXPATHLEN], pathname[MAXPATHLEN];fprintf(stderr, "openAP()\n"); if (*nArgs == 0) { fprintf(stderr, "NEdit: open action requires file argument\n"); return; } if (ParseFilename(args[0], filename, pathname) != 0) { fprintf(stderr, "NEdit: invalid file name for open action: %s\n", args[0]); return; } EditExistingFile(NULL, filename, pathname, 0, NULL, False, NULL);// CheckCloseDim();}static void openDialogAP(Widget w, XEvent *event, String *args, Cardinal *nArgs){// WindowInfo *window = WidgetToWindow(w); char fullname[MAXPATHLEN], *params[1]; int response; response = PromptForExistingFile(w, "File to Edit", fullname); if (response != GFN_OK) return; params[0] = fullname;fprintf(stderr, "openDialogAP() : call 'open' action proc\n"); XtCallActionProc(w, "open", event, params, 1);// CheckCloseDim();}static XtActionsRec Actions[] = { {"open", openAP}, {"open_dialog", openDialogAP}, {"exit", exitAP},};static void doActionCB(Widget w, XtPointer clientData, XtPointer callData){ Widget menu = MENU_WIDGET(w);// HidePointerOnKeyedEvent(WidgetToWindow(menu)->lastFocus,// ((XmAnyCallbackStruct *)callData)->event); XtCallActionProc(w, (char *)clientData, ((XmAnyCallbackStruct *)callData)->event, NULL, 0);}Widget CreateWindow(char *name){ Widget shell, mw, stats, mb, pane; Arg al[32]; int ac = 0; XtSetArg(al[ac], XmNtitle, name); ac++; XtSetArg(al[ac], XmNdeleteResponse, XmDO_NOTHING); ac++; XtSetArg(al[ac], XmNiconName, name); ac++; XtSetArg(al[ac], XmNinitialState, NormalState); ac++; shell = XmCreateDialogShell(toplevel, name, al, ac); AddMotifCloseCallback(shell, (XtCallbackProc)closeCB, NULL); ac = 0; XtSetArg(al[ac], XmNwidth, 100); ac++; XtSetArg(al[ac], XmNheight, 60); ac++; mw = XmCreateMainWindow(shell, ComposeName(name, "main"), al, ac); XtManageChild(mw); mb = XmCreateMenuBar(mw, ComposeName(name, "menubar"), NULL, 0); XtManageChild(mb); pane = CreateMenu(mb, ComposeName(name, "fileMenu"), ComposeName(name, "File"), 0, NULL, SHORT); createMenuItem(pane, ComposeName(name, "open"), ComposeName(name, "Open..."), 'O', doActionCB, "open_dialog", SHORT); createMenuItem(pane, ComposeName(name, "exit"), ComposeName(name, "Exit"), 'x', doActionCB, "exit", SHORT); stats = XtVaCreateWidget(ComposeName(name, "statsAreaForm"), xmFormWidgetClass, mw, NULL); XtManageChild(stats); return shell;}static voidEditExistingFile(void *inWindow, const char *name, const char *path, int flags, char *geometry, int iconic, const char *languageMode){ fprintf(stderr, "EditExistingFile()\n");// CreateWindow("editexisting"); XtRealizeWidget(CreateWindow("b"));}intmain(int argc, char **argv){ toplevel = XtAppInitialize(&TheAppContext, "menushell1", NULL, 0, &argc, argv, fallback, NULL, 0); TheDisplay = XtDisplay(toplevel); XtAppAddActions(TheAppContext, Actions, XtNumber(Actions)); XtRealizeWidget(CreateWindow("a")); XtRealizeWidget(CreateWindow("x")); XtAppMainLoop(TheAppContext);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -