📄 fselect.c
字号:
} /* cb_button() *//*--------------------------------------------------------------------*/static void cb_destroy (Widget w, XtPointer client, XtPointer call){ /* --- dialog box destroyed callback */ w_shell = NULL; /* clear shell variable */} /* cb_destroy() *//*---------------------------------------------------------------------- Action Functions----------------------------------------------------------------------*/static void fs_dclick (Widget w, XEvent *e, String *s, Cardinal *c){ /* --- double click on list entry */ if (dclick) /* process as if 'ok' button pressed */ cb_button(w, (void*)1, NULL);} /* fs_dclick() *//*--------------------------------------------------------------------*/static void fs_focus (Widget w, XEvent *e, String *s, Cardinal *c){ /* --- set focus on input field */ XtSetKeyboardFocus(w_shell, w);} /* fs_focus() *//*--------------------------------------------------------------------*/static void fs_next (Widget w, XEvent *e, String *s, Cardinal *c){ /* --- switch to next input field */ if (w == w_path) w = w_file; else if (w == w_file) w = w_path; else return; /* switch to next input field */ XtSetKeyboardFocus(w_shell, w); /* and set keyboard focus */} /* fs_next() *//*--------------------------------------------------------------------*/static void fs_close (Widget w, XEvent *e, String *s, Cardinal *c){ /* --- close file select dialog box */ if ((*c == 0) || (strcmp(*s, "cancel") == 0)) cb_button(XtNameToWidget(w_shell, "*cancel"), (void*)0, NULL); else cb_button(XtNameToWidget(w_shell, "*ok"), (void*)1, NULL);} /* fs_close() *//*---------------------------------------------------------------------- Functions----------------------------------------------------------------------*/int fselect (Widget parent, const char *title, const char *path, const char *fname, XtCallbackProc callback){ /* --- open file select dialog box */ XtTranslations tl; /* translations for text widgets */ Atom wm_delwin; /* delete window atom */ Widget shell = NULL, form; /* widgets for dialog shell and form */ Widget view; /* widget for file list viewport */ Widget label; /* buffer for label widgets */ Widget bupdate; /* widget for update filter button */ Widget ok, cancel; /* widgets for ok and cancel buttons */ Arg args[12]; /* widget argument list */ int n; /* widget argument counter */ Position x, y; /* position of created dialog */ Dimension wd, ht; /* width and height of created dialog */ const char *t; /* temporary buffer */ if (!w_shell) { /* if dialog box is not created */ shell = XtCreatePopupShell("fileSelect", transientShellWidgetClass, parent, NULL, 0); /* create a dialog box shell */ if (!shell) return -1; /* and a form for the box layout */ XtAddCallback(shell, XtNdestroyCallback, cb_destroy, NULL); XtSetArg(args[0], XtNborderWidth, 0); form = XtCreateManagedWidget("form", formWidgetClass, shell,args,1); if (!form) { XtDestroyWidget(shell); return -1; } /* --- create path/filter input field --- */ n = 0; /* build argument list */ XtSetArg(args[n], XtNborderWidth, 0); n++; XtSetArg(args[n], XtNleft, XtChainLeft); n++; XtSetArg(args[n], XtNtop, XtChainTop); n++; XtSetArg(args[n], XtNbottom, XtChainTop); n++; XtSetArg(args[n], XtNjustify, XtJustifyLeft); n++; label = XtCreateManagedWidget("pathLabel", labelWidgetClass, form, args, n); /* create path label widget */ if (!label) { XtDestroyWidget(shell); return -1; } n = 0; /* build argument list */ XtSetArg(args[n], XtNborderWidth, 1); n++; XtSetArg(args[n], XtNeditType, XawtextEdit); n++; XtSetArg(args[n], XtNfromVert, label); n++; XtSetArg(args[n], XtNleft, XtChainLeft); n++; XtSetArg(args[n], XtNtop, XtChainTop); n++; XtSetArg(args[n], XtNright, XtChainRight); n++; XtSetArg(args[n], XtNbottom, XtChainTop); n++; XtSetArg(args[n], XtNwidth, LISTWIDTH); n++; w_path = XtCreateManagedWidget("path", asciiTextWidgetClass, form, args, n); /* create path widget */ if (!w_path) { XtDestroyWidget(shell); return -1; } /* --- create file list field --- */ n = 0; /* build argument list */ XtSetArg(args[n], XtNborderWidth, 0); n++; XtSetArg(args[n], XtNfromVert, w_path); n++; XtSetArg(args[n], XtNleft, XtChainLeft); n++; XtSetArg(args[n], XtNtop, XtChainTop); n++; XtSetArg(args[n], XtNbottom, XtChainTop); n++; XtSetArg(args[n], XtNjustify, XtJustifyLeft); n++; XtSetArg(args[n], XtNwidth, LISTWIDTH/2 -1); n++; label = XtCreateManagedWidget("listLabel", labelWidgetClass, form, args, n); /* create file list label widget */ if (!label) { XtDestroyWidget(shell); return -1; } n = 0; /* build argument list */ XtSetArg(args[n], XtNfromHoriz, label); n++; XtSetArg(args[n], XtNfromVert, w_path); n++; XtSetArg(args[n], XtNtop, XtChainTop); n++; XtSetArg(args[n], XtNright, XtChainRight); n++; XtSetArg(args[n], XtNbottom, XtChainTop); n++; XtSetArg(args[n], XtNwidth, LISTWIDTH/2 -3); n++; bupdate = XtCreateManagedWidget("update", commandWidgetClass, form, args, n); /* create 'update' button */ if (!bupdate) { XtDestroyWidget(shell); return -1; } XtAddCallback(bupdate, XtNcallback, cb_update, (void*)0); n = 0; /* build argument list */ XtSetArg(args[n], XtNborderWidth, 1); n++; XtSetArg(args[n], XtNfromVert, bupdate); n++; XtSetArg(args[n], XtNallowHoriz, True); n++; XtSetArg(args[n], XtNallowVert, True); n++; XtSetArg(args[n], XtNuseRight, True); n++; XtSetArg(args[n], XtNuseBottom, True); n++; XtSetArg(args[n], XtNforceBars, True); n++; XtSetArg(args[n], XtNleft, XtChainLeft); n++; XtSetArg(args[n], XtNtop, XtChainTop); n++; XtSetArg(args[n], XtNright, XtChainRight); n++; XtSetArg(args[n], XtNbottom, XtChainBottom); n++; view = XtCreateManagedWidget("listView", viewportWidgetClass, form, args, n); /* create file list viewport */ if (!view) { XtDestroyWidget(shell); return -1; } XtVaGetValues(w_path, XtNheight, &ht, NULL); n = 0; /* build argument list */ XtSetArg(args[n], XtNstring, ""); n++; XtSetArg(args[n], XtNresizable, True); n++; XtSetArg(args[n], XtNborderWidth, 0); n++; XtSetArg(args[n], XtNcolumnSpacing, ht); n++; XtSetArg(args[n], XtNverticalList, True); n++; XtSetArg(args[n], XtNwidth, LISTWIDTH); n++; XtSetArg(args[n], XtNheight, ht *LISTHEIGHT +13); n++; w_list = XtCreateManagedWidget("list", listWidgetClass, view, args, n); /* create file list widget */ if (!w_list) { XtDestroyWidget(shell); return -1; } XtAddCallback(w_list, XtNcallback, cb_list, NULL); /* --- create file selection input field --- */ n = 0; /* build argument list */ XtSetArg(args[n], XtNborderWidth, 0); n++; XtSetArg(args[n], XtNfromVert, view); n++; XtSetArg(args[n], XtNleft, XtChainLeft); n++; XtSetArg(args[n], XtNtop, XtChainBottom); n++; XtSetArg(args[n], XtNbottom, XtChainBottom); n++; XtSetArg(args[n], XtNjustify, XtJustifyLeft); n++; label = XtCreateManagedWidget("fileLabel", labelWidgetClass, form, args, n); /* create selection label widget */ if (!label) { XtDestroyWidget(shell); return -1; } n = 0; /* build argument list */ XtSetArg(args[n], XtNborderWidth, 1); n++; XtSetArg(args[n], XtNeditType, XawtextEdit); n++; XtSetArg(args[n], XtNfromVert, label); n++; XtSetArg(args[n], XtNleft, XtChainLeft); n++; XtSetArg(args[n], XtNtop, XtChainBottom); n++; XtSetArg(args[n], XtNright, XtChainRight); n++; XtSetArg(args[n], XtNbottom, XtChainBottom); n++; XtSetArg(args[n], XtNwidth, LISTWIDTH); n++; w_file = XtCreateManagedWidget("file", asciiTextWidgetClass, form, args, n); /* create path label widget */ if (!w_file) { XtDestroyWidget(shell); return -1; } /* --- create ok button --- */ n = 0; /* build argument list */ XtSetArg(args[n], XtNfromVert, w_file); n++; XtSetArg(args[n], XtNleft, XtChainLeft); n++; XtSetArg(args[n], XtNtop, XtChainBottom); n++; XtSetArg(args[n], XtNbottom, XtChainBottom); n++; XtSetArg(args[n], XtNwidth, LISTWIDTH/2 -3); n++; ok = XtCreateManagedWidget("ok", commandWidgetClass, form, args, n); /* create 'ok' button */ if (!ok) { XtDestroyWidget(shell); return -1; } XtAddCallback(ok, XtNcallback, cb_button, (void*)1); /* --- create cancel button --- */ n = 0; /* build argument list */ XtSetArg(args[n], XtNfromHoriz, ok); n++; XtSetArg(args[n], XtNfromVert, w_file); n++; XtSetArg(args[n], XtNtop, XtChainBottom); n++; XtSetArg(args[n], XtNright, XtChainRight); n++; XtSetArg(args[n], XtNbottom, XtChainBottom); n++; XtSetArg(args[n], XtNwidth, LISTWIDTH/2 -3); n++; cancel = XtCreateManagedWidget("cancel", commandWidgetClass, form, args, n); /* create 'cancel' button */ if (!cancel) { XtDestroyWidget(shell); return -1; } XtAddCallback(cancel, XtNcallback, cb_button, (void*)0); w_shell = shell; /* install created dialog box */ XtAppAddActions(XtWidgetToApplicationContext(parent), actions, XtNumber(actions)); XtAugmentTranslations(w_list, XtParseTranslationTable( "<Btn1Down>,<Btn1Up>: Set()Notify()\n" "<Btn1Down>(2+): fs_dclick()")); tl = XtParseTranslationTable( "<Key>Home : beginning-of-line()\n" "<Key>End : end-of-line()\n"); XtAugmentTranslations(w_path, tl); XtAugmentTranslations(w_file, tl); tl = XtParseTranslationTable( "<Key>Tab : fs_next()\n" "<Key>Return: fs_close(ok)\n" "!Alt<Key>o : fs_close(ok)\n" "!Alt<Key>c : fs_close(cancel)\n" "<Btn1Down> : fs_focus()\n"); XtOverrideTranslations(w_path, tl); XtOverrideTranslations(w_file, tl); XtInstallAllAccelerators(form, form); } /* install actions and accelerators */ /* --- show dialog box --- */ XtVaGetValues(parent, XtNx, &x, XtNy, &y, NULL); XtVaSetValues(w_shell, XtNx, x, XtNy, y, NULL); XtPopup(w_shell, XtGrabExclusive); /* place and show dialog box */ XtSetKeyboardFocus(w_shell, w_file); /* and set keyboard focus */ /* --- get path and file name --- */ if (!path || !*path) { /* if no path name is given */ if (path /* if the path */ || (cpend == cpath)) { /* or the buffer is empty */ if (getcwd(cpath, PATH_MAX) == NULL) cpath[0] = '\0'; /* get current working directory */ cpend = cpath +strlen(cpath); if ((cpend <= cpath) || (*(cpend-1) != '/')) { *cpend++ = '/'; *cpend = '\0'; } } /* make sure that there is a '/' */ path = cpath; /* at the end of the path name */ strcpy(cpend, cfilt); /* use path name and */ } /* filter from buffer */ if (!fname) fname = ""; /* if no file name given, */ else { /* set empty name, otherwise */ t = strrchr(fname, '/'); /* search for last '/' and let */ if (t) fname = t+1; /* the file name start behind it */ } /* (remove preceding path) */ /* --- set title and input fields --- */ XtVaSetValues(w_shell, XtNtitle, title, NULL); update(path, 1); /* update filter and list of files */ XtVaSetValues(w_file, XtNstring, fname ? fname : "", NULL); cb_user = callback; /* note user callback function */ if (shell) { /* if dialog box has been created */ XtVaGetValues(shell, XtNwidth, &wd, XtNheight, &ht, NULL); XtVaSetValues(shell, XtNminWidth, wd, XtNminHeight, ht, NULL); XtOverrideTranslations(shell, XtParseTranslationTable("<Message>WM_PROTOCOLS: fs_close()")); wm_delwin = XInternAtom(XtDisplay(shell), "WM_DELETE_WINDOW", False); XSetWMProtocols(XtDisplay(shell), XtWindow(shell), &wm_delwin, 1); /* override window manager delete */ } return 0; /* return 'ok' */} /* fselect() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -