📄 tkentry.c
字号:
* If we're displaying a special character instead of the value of * the entry, recompute the displayString. */ if (entryPtr->displayString != NULL) { ckfree(entryPtr->displayString); entryPtr->displayString = NULL; } if (entryPtr->showChar != NULL) { entryPtr->displayString = (char *) ckalloc((unsigned) (entryPtr->numChars + 1)); for (p = entryPtr->displayString, i = entryPtr->numChars; i > 0; i--, p++) { *p = entryPtr->showChar[0]; } *p = 0; displayString = entryPtr->displayString; } else { displayString = entryPtr->string; } Tk_FreeTextLayout(entryPtr->textLayout); entryPtr->textLayout = Tk_ComputeTextLayout(entryPtr->tkfont, displayString, entryPtr->numChars, 0, entryPtr->justify, TK_IGNORE_NEWLINES, &totalLength, &height); entryPtr->layoutY = (Tk_Height(entryPtr->tkwin) - height) / 2; /* * Recompute where the leftmost character on the display will * be drawn (entryPtr->leftX) and adjust leftIndex if necessary * so that we don't let characters hang off the edge of the * window unless the entire window is full. */ overflow = totalLength - (Tk_Width(entryPtr->tkwin) - 2*entryPtr->inset); if (overflow <= 0) { entryPtr->leftIndex = 0; if (entryPtr->justify == TK_JUSTIFY_LEFT) { entryPtr->leftX = entryPtr->inset; } else if (entryPtr->justify == TK_JUSTIFY_RIGHT) { entryPtr->leftX = Tk_Width(entryPtr->tkwin) - entryPtr->inset - totalLength; } else { entryPtr->leftX = (Tk_Width(entryPtr->tkwin) - totalLength)/2; } entryPtr->layoutX = entryPtr->leftX; } else { /* * The whole string can't fit in the window. Compute the * maximum number of characters that may be off-screen to * the left without leaving empty space on the right of the * window, then don't let leftIndex be any greater than that. */ maxOffScreen = Tk_PointToChar(entryPtr->textLayout, overflow, 0); Tk_CharBbox(entryPtr->textLayout, maxOffScreen, &rightX, NULL, NULL, NULL); if (rightX < overflow) { maxOffScreen += 1; } if (entryPtr->leftIndex > maxOffScreen) { entryPtr->leftIndex = maxOffScreen; } Tk_CharBbox(entryPtr->textLayout, entryPtr->leftIndex, &rightX, NULL, NULL, NULL); entryPtr->leftX = entryPtr->inset; entryPtr->layoutX = entryPtr->leftX - rightX; } Tk_GetFontMetrics(entryPtr->tkfont, &fm); height = fm.linespace + 2*entryPtr->inset + 2*(YPAD-XPAD); if (entryPtr->prefWidth > 0) { width = entryPtr->prefWidth*entryPtr->avgWidth + 2*entryPtr->inset; } else { if (totalLength == 0) { width = entryPtr->avgWidth + 2*entryPtr->inset; } else { width = totalLength + 2*entryPtr->inset; } } Tk_GeometryRequest(entryPtr->tkwin, width, height);}/* *---------------------------------------------------------------------- * * InsertChars -- * * Add new characters to an entry widget. * * Results: * None. * * Side effects: * New information gets added to entryPtr; it will be redisplayed * soon, but not necessarily immediately. * *---------------------------------------------------------------------- */static voidInsertChars(entryPtr, index, string) register Entry *entryPtr; /* Entry that is to get the new * elements. */ int index; /* Add the new elements before this * element. */ char *string; /* New characters to add (NULL-terminated * string). */{ int length; char *new; length = strlen(string); if (length == 0) { return; } new = (char *) ckalloc((unsigned) (entryPtr->numChars + length + 1)); strncpy(new, entryPtr->string, (size_t) index); strcpy(new+index, string); strcpy(new+index+length, entryPtr->string+index); ckfree(entryPtr->string); entryPtr->string = new; entryPtr->numChars += length; /* * Inserting characters invalidates all indexes into the string. * Touch up the indexes so that they still refer to the same * characters (at new positions). When updating the selection * end-points, don't include the new text in the selection unless * it was completely surrounded by the selection. */ if (entryPtr->selectFirst >= index) { entryPtr->selectFirst += length; } if (entryPtr->selectLast > index) { entryPtr->selectLast += length; } if ((entryPtr->selectAnchor > index) || (entryPtr->selectFirst >= index)) { entryPtr->selectAnchor += length; } if (entryPtr->leftIndex > index) { entryPtr->leftIndex += length; } if (entryPtr->insertPos >= index) { entryPtr->insertPos += length; } EntryValueChanged(entryPtr);}/* *---------------------------------------------------------------------- * * DeleteChars -- * * Remove one or more characters from an entry widget. * * Results: * None. * * Side effects: * Memory gets freed, the entry gets modified and (eventually) * redisplayed. * *---------------------------------------------------------------------- */static voidDeleteChars(entryPtr, index, count) register Entry *entryPtr; /* Entry widget to modify. */ int index; /* Index of first character to delete. */ int count; /* How many characters to delete. */{ char *new; if ((index + count) > entryPtr->numChars) { count = entryPtr->numChars - index; } if (count <= 0) { return; } new = (char *) ckalloc((unsigned) (entryPtr->numChars + 1 - count)); strncpy(new, entryPtr->string, (size_t) index); strcpy(new+index, entryPtr->string+index+count); ckfree(entryPtr->string); entryPtr->string = new; entryPtr->numChars -= count; /* * Deleting characters results in the remaining characters being * renumbered. Update the various indexes into the string to reflect * this change. */ if (entryPtr->selectFirst >= index) { if (entryPtr->selectFirst >= (index+count)) { entryPtr->selectFirst -= count; } else { entryPtr->selectFirst = index; } } if (entryPtr->selectLast >= index) { if (entryPtr->selectLast >= (index+count)) { entryPtr->selectLast -= count; } else { entryPtr->selectLast = index; } } if (entryPtr->selectLast <= entryPtr->selectFirst) { entryPtr->selectFirst = entryPtr->selectLast = -1; } if (entryPtr->selectAnchor >= index) { if (entryPtr->selectAnchor >= (index+count)) { entryPtr->selectAnchor -= count; } else { entryPtr->selectAnchor = index; } } if (entryPtr->leftIndex > index) { if (entryPtr->leftIndex >= (index+count)) { entryPtr->leftIndex -= count; } else { entryPtr->leftIndex = index; } } if (entryPtr->insertPos >= index) { if (entryPtr->insertPos >= (index+count)) { entryPtr->insertPos -= count; } else { entryPtr->insertPos = index; } } EntryValueChanged(entryPtr);}/* *---------------------------------------------------------------------- * * EntryValueChanged -- * * This procedure is invoked when characters are inserted into * an entry or deleted from it. It updates the entry's associated * variable, if there is one, and does other bookkeeping such * as arranging for redisplay. * * Results: * None. * * Side effects: * None. * *---------------------------------------------------------------------- */static voidEntryValueChanged(entryPtr) Entry *entryPtr; /* Entry whose value just changed. */{ char *newValue; if (entryPtr->textVarName == NULL) { newValue = NULL; } else { newValue = Tcl_SetVar(entryPtr->interp, entryPtr->textVarName, entryPtr->string, TCL_GLOBAL_ONLY); } if ((newValue != NULL) && (strcmp(newValue, entryPtr->string) != 0)) { /* * The value of the variable is different than what we asked for. * This means that a trace on the variable modified it. In this * case our trace procedure wasn't invoked since the modification * came while a trace was already active on the variable. So, * update our value to reflect the variable's latest value. */ EntrySetValue(entryPtr, newValue); } else { /* * Arrange for redisplay. */ entryPtr->flags |= UPDATE_SCROLLBAR; EntryComputeGeometry(entryPtr); EventuallyRedraw(entryPtr); }}/* *---------------------------------------------------------------------- * * EntrySetValue -- * * Replace the contents of a text entry with a given value. This * procedure is invoked when updating the entry from the entry's * associated variable. * * Results: * None. * * Side effects: * The string displayed in the entry will change. The selection, * insertion point, and view may have to be adjusted to keep them * within the bounds of the new string. Note: this procedure does * *not* update the entry's associated variable, since that could * result in an infinite loop. * *---------------------------------------------------------------------- */static voidEntrySetValue(entryPtr, value) register Entry *entryPtr; /* Entry whose value is to be * changed. */ char *value; /* New text to display in entry. */{ ckfree(entryPtr->string); entryPtr->numChars = strlen(value); entryPtr->string = (char *) ckalloc((unsigned) (entryPtr->numChars + 1)); strcpy(entryPtr->string, value); if (entryPtr->selectFirst != -1) { if (entryPtr->selectFirst >= entryPtr->numChars) { entryPtr->selectFirst = entryPtr->selectLast = -1; } else if (entryPtr->selectLast > entryPtr->numChars) { entryPtr->selectLast = entryPtr->numChars; } } if (entryPtr->leftIndex >= entryPtr->numChars) { entryPtr->leftIndex = entryPtr->numChars-1; if (entryPtr->leftIndex < 0) { entryPtr->leftIndex = 0; } } if (entryPtr->insertPos > entryPtr->numChars) { entryPtr->insertPos = entryPtr->numChars; } entryPtr->flags |= UPDATE_SCROLLBAR; EntryComputeGeometry(entryPtr); EventuallyRedraw(entryPtr);}/* *-------------------------------------------------------------- * * EntryEventProc -- * * This procedure is invoked by the Tk dispatcher for various * events on entryes. * * Results: * None. * * Side effects: * When the window gets deleted, internal structures get * cleaned up. When it gets exposed, it is redisplayed. * *-------------------------------------------------------------- */static voidEntryEventProc(clientData, eventPtr) ClientData clientData; /* Information about window. */ XEvent *eventPtr; /* Information about event. */{ Entry *entryPtr = (Entry *) clientData; if (eventPtr->type == Expose) { EventuallyRedraw(entryPtr); entryPtr->flags |= BORDER_NEEDED; } else if (eventPtr->type == DestroyNotify) { if (entryPtr->tkwin != NULL) { entryPtr->tkwin = NULL; Tcl_DeleteCommandFromToken(entryPtr->interp, entryPtr->widgetCmd); } if (entryPtr->flags & REDRAW_PENDING) { Tcl_CancelIdleCall(DisplayEntry, (ClientData) entryPtr); } Tcl_EventuallyFree((ClientData) entryPtr, DestroyEntry); } else if (eventPtr->type == ConfigureNotify) { Tcl_Preserve((ClientData) entryPtr); entryPtr->flags |= UPDATE_SCROLLBAR; EntryComputeGeometry(entryPtr); EventuallyRedraw(entryPtr); Tcl_Release((ClientData) entryPtr); } else if (eventPtr->type == FocusIn) { if (eventPtr->xfocus.detail != NotifyInferior) { EntryFocusProc(entryPtr, 1); } } else if (eventPtr->type == FocusOut) { if (eventPtr->xfocus.detail != NotifyInferior) { EntryFocusProc(entryPtr, 0); } }}/* *---------------------------------------------------------------------- * * EntryCmdDeletedProc -- * * This procedure is invoked when a widget command is deleted. If * the widget isn't already in the process of being destroyed, * this command destroys it. * * Results: * None. * * Side effects: * The widget is destroyed. * *---------------------------------------------------------------------- */static voidEntryCmdDeletedProc(clientData) ClientData clientData; /* Pointer to widget record for widget. */{ Entry *entryPtr = (Entry *) clientData; Tk_Window tkwin = entryPtr->tkwin; /* * This procedure could be invoked either because the window was
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -