📄 tkwindialog.c
字号:
* to the arguments. * *---------------------------------------------------------------------- */static int ParseFileDlgArgs(interp, ofnPtr, argc, argv, isOpen) Tcl_Interp * interp; /* Current interpreter. */ OPENFILENAME *ofnPtr; /* Info about the file dialog */ int argc; /* Number of arguments. */ char **argv; /* Argument strings. */ int isOpen; /* true if we should call GetOpenFileName(), * false if we should call GetSaveFileName() */{ OpenFileData * custData; int i; Tk_Window parent = Tk_MainWindow(interp); int doneFilter = 0; int windowsMajorVersion; Tcl_DString buffer; custData = (OpenFileData*)ckalloc(sizeof(OpenFileData)); custData->interp = interp; strcpy(custData->szFile, ""); /* Fill in the OPENFILENAME structure to */ ofnPtr->lStructSize = sizeof(OPENFILENAME); ofnPtr->hwndOwner = 0; /* filled in below */ ofnPtr->lpstrFilter = NULL; ofnPtr->lpstrCustomFilter = NULL; ofnPtr->nMaxCustFilter = 0; ofnPtr->nFilterIndex = 0; ofnPtr->lpstrFile = custData->szFile; ofnPtr->nMaxFile = sizeof(custData->szFile); ofnPtr->lpstrFileTitle = NULL; ofnPtr->nMaxFileTitle = 0; ofnPtr->lpstrInitialDir = NULL; ofnPtr->lpstrTitle = NULL; ofnPtr->nFileOffset = 0; ofnPtr->nFileExtension = 0; ofnPtr->lpstrDefExt = NULL; ofnPtr->lpfnHook = NULL; ofnPtr->lCustData = (DWORD)custData; ofnPtr->lpTemplateName = NULL; ofnPtr->Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST; windowsMajorVersion = LOBYTE(LOWORD(GetVersion())); if (windowsMajorVersion >= 4) { /* * Use the "explorer" style file selection box on platforms that * support it (Win95 and NT4.0, both have a major version number * of 4) */ ofnPtr->Flags |= OFN_EXPLORER; } if (isOpen) { ofnPtr->Flags |= OFN_FILEMUSTEXIST; } else { ofnPtr->Flags |= OFN_OVERWRITEPROMPT; } for (i=1; i<argc; i+=2) { int v = i+1; int len = strlen(argv[i]); if (strncmp(argv[i], "-defaultextension", len)==0) { if (v==argc) {goto arg_missing;} ofnPtr->lpstrDefExt = argv[v]; if (ofnPtr->lpstrDefExt[0] == '.') { /* Windows will insert the dot for us */ ofnPtr->lpstrDefExt ++; } } else if (strncmp(argv[i], "-filetypes", len)==0) { if (v==argc) {goto arg_missing;} if (MakeFilter(interp, ofnPtr, argv[v]) != TCL_OK) { return TCL_ERROR; } doneFilter = 1; } else if (strncmp(argv[i], "-initialdir", len)==0) { if (v==argc) {goto arg_missing;} if (Tcl_TranslateFileName(interp, argv[v], &buffer) == NULL) { return TCL_ERROR; } ofnPtr->lpstrInitialDir = ckalloc(Tcl_DStringLength(&buffer)+1); strcpy((char*)ofnPtr->lpstrInitialDir, Tcl_DStringValue(&buffer)); Tcl_DStringFree(&buffer); } else if (strncmp(argv[i], "-initialfile", len)==0) { if (v==argc) {goto arg_missing;} if (Tcl_TranslateFileName(interp, argv[v], &buffer) == NULL) { return TCL_ERROR; } strcpy(ofnPtr->lpstrFile, Tcl_DStringValue(&buffer)); Tcl_DStringFree(&buffer); } else if (strncmp(argv[i], "-parent", len)==0) { if (v==argc) {goto arg_missing;} parent=Tk_NameToWindow(interp, argv[v], Tk_MainWindow(interp)); if (parent == NULL) { return TCL_ERROR; } } else if (strncmp(argv[i], "-title", len)==0) { if (v==argc) {goto arg_missing;} ofnPtr->lpstrTitle = argv[v]; } else { Tcl_AppendResult(interp, "unknown option \"", argv[i], "\", must be -defaultextension, ", "-filetypes, -initialdir, -initialfile, -parent or -title", NULL); return TCL_ERROR; } } if (!doneFilter) { if (MakeFilter(interp, ofnPtr, "") != TCL_OK) { return TCL_ERROR; } } if (Tk_WindowId(parent) == None) { Tk_MakeWindowExist(parent); } ofnPtr->hwndOwner = Tk_GetHWND(Tk_WindowId(parent)); return TCL_OK; arg_missing: Tcl_AppendResult(interp, "value for \"", argv[argc-1], "\" missing", NULL); return TCL_ERROR;}/* *---------------------------------------------------------------------- * * MakeFilter -- * * Allocate a buffer to store the filters in a format understood by * Windows * * Results: * A standard TCL return value. * * Side effects: * ofnPtr->lpstrFilter is modified. * *---------------------------------------------------------------------- */static int MakeFilter(interp, ofnPtr, string) Tcl_Interp *interp; /* Current interpreter. */ OPENFILENAME *ofnPtr; /* Info about the file dialog */ char *string; /* String value of the -filetypes option */{ char *filterStr; char *p; int pass; FileFilterList flist; FileFilter *filterPtr; TkInitFileFilters(&flist); if (TkGetFileFilters(interp, &flist, string, 1) != TCL_OK) { return TCL_ERROR; } if (flist.filters == NULL) { /* * Use "All Files (*.*) as the default filter is none is specified */ char *defaultFilter = "All Files (*.*)"; p = filterStr = (char*)ckalloc(30 * sizeof(char)); strcpy(p, defaultFilter); p+= strlen(defaultFilter); *p++ = '\0'; *p++ = '*'; *p++ = '.'; *p++ = '*'; *p++ = '\0'; *p++ = '\0'; *p = '\0'; } else { /* We format the filetype into a string understood by Windows: * {"Text Documents" {.doc .txt} {TEXT}} becomes * "Text Documents (*.doc,*.txt)\0*.doc;*.txt\0" * * See the Windows OPENFILENAME manual page for details on the filter * string format. */ /* * Since we may only add asterisks (*) to the filter, we need at most * twice the size of the string to format the filter */ filterStr = ckalloc(strlen(string) * 3); for (filterPtr = flist.filters, p = filterStr; filterPtr; filterPtr = filterPtr->next) { char *sep; FileFilterClause *clausePtr; /* * First, put in the name of the file type */ strcpy(p, filterPtr->name); p+= strlen(filterPtr->name); *p++ = ' '; *p++ = '('; for (pass = 1; pass <= 2; pass++) { /* * In the first pass, we format the extensions in the * name field. In the second pass, we format the extensions in * the filter pattern field */ sep = ""; for (clausePtr=filterPtr->clauses;clausePtr; clausePtr=clausePtr->next) { GlobPattern *globPtr; for (globPtr=clausePtr->patterns; globPtr; globPtr=globPtr->next) { strcpy(p, sep); p+= strlen(sep); strcpy(p, globPtr->pattern); p+= strlen(globPtr->pattern); if (pass==1) { sep = ","; } else { sep = ";"; } } } if (pass == 1) { if (pass == 1) { *p ++ = ')'; } } *p ++ = '\0'; } } /* * Windows requires the filter string to be ended by two NULL * characters. */ *p++ = '\0'; *p = '\0'; } if (ofnPtr->lpstrFilter != NULL) { ckfree((char*)ofnPtr->lpstrFilter); } ofnPtr->lpstrFilter = filterStr; TkFreeFileFilters(&flist); return TCL_OK;}/* *---------------------------------------------------------------------- * * Tk_MessageBoxCmd -- * * This procedure implements the MessageBox window for the * Windows platform. See the user documentation for details on what * it does. * * Results: * See user documentation. * * Side effects: * None. The MessageBox window will be destroy before this procedure * returns. * *---------------------------------------------------------------------- */intTk_MessageBoxCmd(clientData, interp, argc, argv) ClientData clientData; /* Main window associated with interpreter. */ Tcl_Interp *interp; /* Current interpreter. */ int argc; /* Number of arguments. */ char **argv; /* Argument strings. */{ int flags; Tk_Window parent = Tk_MainWindow(interp); HWND hWnd; char *message = ""; char *title = ""; int icon = MB_ICONINFORMATION; int type = MB_OK; int i, j; char *result; int code, oldMode; char *defaultBtn = NULL; int defaultBtnIdx = -1; for (i=1; i<argc; i+=2) { int v = i+1; int len = strlen(argv[i]); if (strncmp(argv[i], "-default", len)==0) { if (v==argc) {goto arg_missing;} defaultBtn = argv[v]; } else if (strncmp(argv[i], "-icon", len)==0) { if (v==argc) {goto arg_missing;} if (strcmp(argv[v], "error") == 0) { icon = MB_ICONERROR; } else if (strcmp(argv[v], "info") == 0) { icon = MB_ICONINFORMATION; } else if (strcmp(argv[v], "question") == 0) { icon = MB_ICONQUESTION; } else if (strcmp(argv[v], "warning") == 0) { icon = MB_ICONWARNING; } else { Tcl_AppendResult(interp, "invalid icon \"", argv[v], "\", must be error, info, question or warning", NULL); return TCL_ERROR; } } else if (strncmp(argv[i], "-message", len)==0) { if (v==argc) {goto arg_missing;} message = argv[v]; } else if (strncmp(argv[i], "-parent", len)==0) { if (v==argc) {goto arg_missing;} parent=Tk_NameToWindow(interp, argv[v], Tk_MainWindow(interp)); if (parent == NULL) { return TCL_ERROR; } } else if (strncmp(argv[i], "-title", len)==0) { if (v==argc) {goto arg_missing;} title = argv[v]; } else if (strncmp(argv[i], "-type", len)==0) { int found = 0; if (v==argc) {goto arg_missing;} for (j=0; j<NUM_TYPES; j++) { if (strcmp(argv[v], msgTypeInfo[j].name) == 0) { type = msgTypeInfo[j].type; found = 1; break; } } if (!found) { Tcl_AppendResult(interp, "invalid message box type \"", argv[v], "\", must be abortretryignore, ok, ", "okcancel, retrycancel, yesno or yesnocancel", NULL); return TCL_ERROR; } } else { Tcl_AppendResult(interp, "unknown option \"", argv[i], "\", must be -default, -icon, ", "-message, -parent, -title or -type", NULL); return TCL_ERROR; } } /* Make sure we have a valid hWnd to act as the parent of this message box */ if (Tk_WindowId(parent) == None) { Tk_MakeWindowExist(parent); } hWnd = Tk_GetHWND(Tk_WindowId(parent)); if (defaultBtn != NULL) { for (i=0; i<NUM_TYPES; i++) { if (type == msgTypeInfo[i].type) { for (j=0; j<msgTypeInfo[i].numButtons; j++) { if (strcmp(defaultBtn, msgTypeInfo[i].btnNames[j])==0) { defaultBtnIdx = j; break; } } if (defaultBtnIdx < 0) { Tcl_AppendResult(interp, "invalid default button \"", defaultBtn, "\"", NULL); return TCL_ERROR; } break; } } switch (defaultBtnIdx) { case 0: flags = MB_DEFBUTTON1; break; case 1: flags = MB_DEFBUTTON2; break; case 2: flags = MB_DEFBUTTON3; break; case 3: flags = MB_DEFBUTTON4; break; } } else { flags = 0; } flags |= icon | type; oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL); code = MessageBox(hWnd, message, title, flags|MB_SYSTEMMODAL); (void) Tcl_SetServiceMode(oldMode); switch (code) { case IDABORT: result = "abort"; break; case IDCANCEL: result = "cancel"; break; case IDIGNORE: result = "ignore"; break; case IDNO: result = "no"; break; case IDOK: result = "ok"; break; case IDRETRY: result = "retry"; break; case IDYES: result = "yes"; break; default: result = ""; } /* * When we come to here interp->result may have been changed by some * background scripts. Call Tcl_SetResult() to make sure that any stuff * lingering in interp->result will not appear in the result of * this command. */ Tcl_SetResult(interp, result, TCL_STATIC); return TCL_OK; arg_missing: Tcl_AppendResult(interp, "value for \"", argv[argc-1], "\" missing", NULL); return TCL_ERROR;}/* *---------------------------------------------------------------------- * * ProcessCDError -- * * This procedure gets called if a Windows-specific error message * has occurred during the execution of a common dialog or the * user has pressed the CANCEL button. * * Results: * If an error has indeed happened, returns a standard TCL result * that reports the error code in string format. If the user has * pressed the CANCEL button (dwErrorCode == 0), resets * interp->result to the empty string. * * Side effects: * interp->result is changed. * *---------------------------------------------------------------------- */static int ProcessCDError(interp, dwErrorCode, hWnd) Tcl_Interp * interp; /* Current interpreter. */ DWORD dwErrorCode; /* The Windows-specific error code */ HWND hWnd; /* window in which the error happened*/{ char *string; Tcl_ResetResult(interp); switch(dwErrorCode) { case 0: /* User has hit CANCEL */ return TCL_OK; case CDERR_DIALOGFAILURE: string="CDERR_DIALOGFAILURE"; break; case CDERR_STRUCTSIZE: string="CDERR_STRUCTSIZE"; break; case CDERR_INITIALIZATION: string="CDERR_INITIALIZATION"; break; case CDERR_NOTEMPLATE: string="CDERR_NOTEMPLATE"; break; case CDERR_NOHINSTANCE: string="CDERR_NOHINSTANCE"; break; case CDERR_LOADSTRFAILURE: string="CDERR_LOADSTRFAILURE"; break; case CDERR_FINDRESFAILURE: string="CDERR_FINDRESFAILURE"; break; case CDERR_LOADRESFAILURE: string="CDERR_LOADRESFAILURE"; break; case CDERR_LOCKRESFAILURE: string="CDERR_LOCKRESFAILURE"; break; case CDERR_MEMALLOCFAILURE: string="CDERR_MEMALLOCFAILURE"; break; case CDERR_MEMLOCKFAILURE: string="CDERR_MEMLOCKFAILURE"; break; case CDERR_NOHOOK: string="CDERR_NOHOOK"; break; case PDERR_SETUPFAILURE: string="PDERR_SETUPFAILURE"; break; case PDERR_PARSEFAILURE: string="PDERR_PARSEFAILURE"; break; case PDERR_RETDEFFAILURE: string="PDERR_RETDEFFAILURE"; break; case PDERR_LOADDRVFAILURE: string="PDERR_LOADDRVFAILURE"; break; case PDERR_GETDEVMODEFAIL: string="PDERR_GETDEVMODEFAIL"; break; case PDERR_INITFAILURE: string="PDERR_INITFAILURE"; break; case PDERR_NODEVICES: string="PDERR_NODEVICES"; break; case PDERR_NODEFAULTPRN: string="PDERR_NODEFAULTPRN"; break; case PDERR_DNDMMISMATCH: string="PDERR_DNDMMISMATCH"; break; case PDERR_CREATEICFAILURE: string="PDERR_CREATEICFAILURE"; break; case PDERR_PRINTERNOTFOUND: string="PDERR_PRINTERNOTFOUND"; break; case CFERR_NOFONTS: string="CFERR_NOFONTS"; break; case FNERR_SUBCLASSFAILURE: string="FNERR_SUBCLASSFAILURE"; break; case FNERR_INVALIDFILENAME: string="FNERR_INVALIDFILENAME"; break; case FNERR_BUFFERTOOSMALL: string="FNERR_BUFFERTOOSMALL"; break; default: string="unknown error"; } Tcl_AppendResult(interp, "Win32 internal error: ", string, NULL); return TCL_ERROR;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -