📄 tkentry.c
字号:
entryPtr->inset = XPAD; entryPtr->textLayout = NULL; entryPtr->layoutX = 0; entryPtr->layoutY = 0; entryPtr->leftIndex = 0; entryPtr->leftX = 0; entryPtr->insertBlinkHandler = (Tcl_TimerToken) NULL; entryPtr->textGC = None; entryPtr->selTextGC = None; entryPtr->highlightGC = None; entryPtr->avgWidth = 1; entryPtr->flags = 0; Tk_SetClass(entryPtr->tkwin, "Entry"); TkSetClassProcs(entryPtr->tkwin, &entryClass, (ClientData) entryPtr); Tk_CreateEventHandler(entryPtr->tkwin, ExposureMask|StructureNotifyMask|FocusChangeMask, EntryEventProc, (ClientData) entryPtr); Tk_CreateSelHandler(entryPtr->tkwin, XA_PRIMARY, XA_STRING, EntryFetchSelection, (ClientData) entryPtr, XA_STRING); if (ConfigureEntry(interp, entryPtr, argc-2, argv+2, 0) != TCL_OK) { goto error; } interp->result = Tk_PathName(entryPtr->tkwin); return TCL_OK; error: Tk_DestroyWindow(entryPtr->tkwin); return TCL_ERROR;}/* *-------------------------------------------------------------- * * EntryWidgetCmd -- * * This procedure is invoked to process the Tcl command * that corresponds to a widget managed by this module. * See the user documentation for details on what it does. * * Results: * A standard Tcl result. * * Side effects: * See the user documentation. * *-------------------------------------------------------------- */static intEntryWidgetCmd(clientData, interp, argc, argv) ClientData clientData; /* Information about entry widget. */ Tcl_Interp *interp; /* Current interpreter. */ int argc; /* Number of arguments. */ char **argv; /* Argument strings. */{ register Entry *entryPtr = (Entry *) clientData; int result = TCL_OK; size_t length; int c; if (argc < 2) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " option ?arg arg ...?\"", (char *) NULL); return TCL_ERROR; } Tcl_Preserve((ClientData) entryPtr); c = argv[1][0]; length = strlen(argv[1]); if ((c == 'b') && (strncmp(argv[1], "bbox", length) == 0)) { int index; int x, y, width, height; if (argc != 3) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " bbox index\"", (char *) NULL); goto error; } if (GetEntryIndex(interp, entryPtr, argv[2], &index) != TCL_OK) { goto error; } if ((index == entryPtr->numChars) && (index > 0)) { index--; } Tk_CharBbox(entryPtr->textLayout, index, &x, &y, &width, &height); sprintf(interp->result, "%d %d %d %d", x + entryPtr->layoutX, y + entryPtr->layoutY, width, height); } else if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0) && (length >= 2)) { if (argc != 3) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " cget option\"", (char *) NULL); goto error; } result = Tk_ConfigureValue(interp, entryPtr->tkwin, configSpecs, (char *) entryPtr, argv[2], 0); } else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0) && (length >= 2)) { if (argc == 2) { result = Tk_ConfigureInfo(interp, entryPtr->tkwin, configSpecs, (char *) entryPtr, (char *) NULL, 0); } else if (argc == 3) { result = Tk_ConfigureInfo(interp, entryPtr->tkwin, configSpecs, (char *) entryPtr, argv[2], 0); } else { result = ConfigureEntry(interp, entryPtr, argc-2, argv+2, TK_CONFIG_ARGV_ONLY); } } else if ((c == 'd') && (strncmp(argv[1], "delete", length) == 0)) { int first, last; if ((argc < 3) || (argc > 4)) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " delete firstIndex ?lastIndex?\"", (char *) NULL); goto error; } if (GetEntryIndex(interp, entryPtr, argv[2], &first) != TCL_OK) { goto error; } if (argc == 3) { last = first+1; } else { if (GetEntryIndex(interp, entryPtr, argv[3], &last) != TCL_OK) { goto error; } } if ((last >= first) && (entryPtr->state == tkNormalUid)) { DeleteChars(entryPtr, first, last-first); } } else if ((c == 'g') && (strncmp(argv[1], "get", length) == 0)) { if (argc != 2) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " get\"", (char *) NULL); goto error; } interp->result = entryPtr->string; } else if ((c == 'i') && (strncmp(argv[1], "icursor", length) == 0) && (length >= 2)) { if (argc != 3) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " icursor pos\"", (char *) NULL); goto error; } if (GetEntryIndex(interp, entryPtr, argv[2], &entryPtr->insertPos) != TCL_OK) { goto error; } EventuallyRedraw(entryPtr); } else if ((c == 'i') && (strncmp(argv[1], "index", length) == 0) && (length >= 3)) { int index; if (argc != 3) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " index string\"", (char *) NULL); goto error; } if (GetEntryIndex(interp, entryPtr, argv[2], &index) != TCL_OK) { goto error; } sprintf(interp->result, "%d", index); } else if ((c == 'i') && (strncmp(argv[1], "insert", length) == 0) && (length >= 3)) { int index; if (argc != 4) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " insert index text\"", (char *) NULL); goto error; } if (GetEntryIndex(interp, entryPtr, argv[2], &index) != TCL_OK) { goto error; } if (entryPtr->state == tkNormalUid) { InsertChars(entryPtr, index, argv[3]); } } else if ((c == 's') && (length >= 2) && (strncmp(argv[1], "scan", length) == 0)) { int x; if (argc != 4) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " scan mark|dragto x\"", (char *) NULL); goto error; } if (Tcl_GetInt(interp, argv[3], &x) != TCL_OK) { goto error; } if ((argv[2][0] == 'm') && (strncmp(argv[2], "mark", strlen(argv[2])) == 0)) { entryPtr->scanMarkX = x; entryPtr->scanMarkIndex = entryPtr->leftIndex; } else if ((argv[2][0] == 'd') && (strncmp(argv[2], "dragto", strlen(argv[2])) == 0)) { EntryScanTo(entryPtr, x); } else { Tcl_AppendResult(interp, "bad scan option \"", argv[2], "\": must be mark or dragto", (char *) NULL); goto error; } } else if ((c == 's') && (length >= 2) && (strncmp(argv[1], "selection", length) == 0)) { int index, index2; if (argc < 3) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " select option ?index?\"", (char *) NULL); goto error; } length = strlen(argv[2]); c = argv[2][0]; if ((c == 'c') && (strncmp(argv[2], "clear", length) == 0)) { if (argc != 3) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " selection clear\"", (char *) NULL); goto error; } if (entryPtr->selectFirst != -1) { entryPtr->selectFirst = entryPtr->selectLast = -1; EventuallyRedraw(entryPtr); } goto done; } else if ((c == 'p') && (strncmp(argv[2], "present", length) == 0)) { if (argc != 3) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " selection present\"", (char *) NULL); goto error; } if (entryPtr->selectFirst == -1) { interp->result = "0"; } else { interp->result = "1"; } goto done; } if (argc >= 4) { if (GetEntryIndex(interp, entryPtr, argv[3], &index) != TCL_OK) { goto error; } } if ((c == 'a') && (strncmp(argv[2], "adjust", length) == 0)) { if (argc != 4) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " selection adjust index\"", (char *) NULL); goto error; } if (entryPtr->selectFirst >= 0) { int half1, half2; half1 = (entryPtr->selectFirst + entryPtr->selectLast)/2; half2 = (entryPtr->selectFirst + entryPtr->selectLast + 1)/2; if (index < half1) { entryPtr->selectAnchor = entryPtr->selectLast; } else if (index > half2) { entryPtr->selectAnchor = entryPtr->selectFirst; } else { /* * We're at about the halfway point in the selection; * just keep the existing anchor. */ } } EntrySelectTo(entryPtr, index); } else if ((c == 'f') && (strncmp(argv[2], "from", length) == 0)) { if (argc != 4) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " selection from index\"", (char *) NULL); goto error; } entryPtr->selectAnchor = index; } else if ((c == 'r') && (strncmp(argv[2], "range", length) == 0)) { if (argc != 5) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " selection range start end\"", (char *) NULL); goto error; } if (GetEntryIndex(interp, entryPtr, argv[4], &index2) != TCL_OK) { goto error; } if (index >= index2) { entryPtr->selectFirst = entryPtr->selectLast = -1; } else { entryPtr->selectFirst = index; entryPtr->selectLast = index2; } if (!(entryPtr->flags & GOT_SELECTION) && (entryPtr->exportSelection)) { Tk_OwnSelection(entryPtr->tkwin, XA_PRIMARY, EntryLostSelection, (ClientData) entryPtr); entryPtr->flags |= GOT_SELECTION; } EventuallyRedraw(entryPtr); } else if ((c == 't') && (strncmp(argv[2], "to", length) == 0)) { if (argc != 4) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " selection to index\"", (char *) NULL); goto error; } EntrySelectTo(entryPtr, index); } else { Tcl_AppendResult(interp, "bad selection option \"", argv[2], "\": must be adjust, clear, from, present, range, or to", (char *) NULL); goto error; } } else if ((c == 'x') && (strncmp(argv[1], "xview", length) == 0)) { int index, type, count, charsPerPage; double fraction, first, last; if (argc == 2) { EntryVisibleRange(entryPtr, &first, &last); sprintf(interp->result, "%g %g", first, last); goto done; } else if (argc == 3) { if (GetEntryIndex(interp, entryPtr, argv[2], &index) != TCL_OK) { goto error; } } else { type = Tk_GetScrollInfo(interp, argc, argv, &fraction, &count); index = entryPtr->leftIndex; switch (type) { case TK_SCROLL_ERROR: goto error; case TK_SCROLL_MOVETO: index = (int) ((fraction * entryPtr->numChars) + 0.5); break; case TK_SCROLL_PAGES: charsPerPage = ((Tk_Width(entryPtr->tkwin) - 2*entryPtr->inset) / entryPtr->avgWidth) - 2; if (charsPerPage < 1) { charsPerPage = 1; } index += charsPerPage*count; break; case TK_SCROLL_UNITS: index += count; break; } } if (index >= entryPtr->numChars) { index = entryPtr->numChars-1; } if (index < 0) { index = 0; } entryPtr->leftIndex = index; entryPtr->flags |= UPDATE_SCROLLBAR; EntryComputeGeometry(entryPtr); EventuallyRedraw(entryPtr); } else { Tcl_AppendResult(interp, "bad option \"", argv[1], "\": must be bbox, cget, configure, delete, get, ", "icursor, index, insert, scan, selection, or xview", (char *) NULL); goto error; } done: Tcl_Release((ClientData) entryPtr); return result; error: Tcl_Release((ClientData) entryPtr); return TCL_ERROR;}/* *---------------------------------------------------------------------- * * DestroyEntry -- * * This procedure is invoked by Tcl_EventuallyFree or Tcl_Release * to clean up the internal structure of an entry at a safe time * (when no-one is using it anymore). * * Results: * None. * * Side effects: * Everything associated with the entry is freed up. * *---------------------------------------------------------------------- */static voidDestroyEntry(memPtr) char *memPtr; /* Info about entry widget. */{ register Entry *entryPtr = (Entry *) memPtr; /* * Free up all the stuff that requires special handling, then * let Tk_FreeOptions handle all the standard option-related * stuff. */ ckfree(entryPtr->string); if (entryPtr->textVarName != NULL) { Tcl_UntraceVar(entryPtr->interp, entryPtr->textVarName, TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS, EntryTextVarProc, (ClientData) entryPtr); } if (entryPtr->textGC != None) { Tk_FreeGC(entryPtr->display, entryPtr->textGC); } if (entryPtr->selTextGC != None) { Tk_FreeGC(entryPtr->display, entryPtr->selTextGC); } Tcl_DeleteTimerHandler(entryPtr->insertBlinkHandler); if (entryPtr->displayString != NULL) { ckfree(entryPtr->displayString); } Tk_FreeTextLayout(entryPtr->textLayout); Tk_FreeOptions(configSpecs, (char *) entryPtr, entryPtr->display, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -