📄 tkentry.c
字号:
/* * tkEntry.c -- * * This module implements entry widgets for the Tk * toolkit. An entry displays a string and allows * the string to be edited. * * Copyright (c) 1990-1994 The Regents of the University of California. * Copyright (c) 1994-1996 Sun Microsystems, Inc. * * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * * SCCS: @(#) tkEntry.c 1.112 97/11/06 16:56:16 */#include "tkInt.h"#include "default.h"/* * A data structure of the following type is kept for each entry * widget managed by this file: */typedef struct { Tk_Window tkwin; /* Window that embodies the entry. NULL * means that the window has been destroyed * but the data structures haven't yet been * cleaned up.*/ Display *display; /* Display containing widget. Used, among * other things, so that resources can be * freed even after tkwin has gone away. */ Tcl_Interp *interp; /* Interpreter associated with entry. */ Tcl_Command widgetCmd; /* Token for entry's widget command. */ /* * Fields that are set by widget commands other than "configure". */ char *string; /* Pointer to storage for string; * NULL-terminated; malloc-ed. */ int insertPos; /* Index of character before which next * typed character will be inserted. */ /* * Information about what's selected, if any. */ int selectFirst; /* Index of first selected character (-1 means * nothing selected. */ int selectLast; /* Index of last selected character (-1 means * nothing selected. */ int selectAnchor; /* Fixed end of selection (i.e. "select to" * operation will use this as one end of the * selection). */ /* * Information for scanning: */ int scanMarkX; /* X-position at which scan started (e.g. * button was pressed here). */ int scanMarkIndex; /* Index of character that was at left of * window when scan started. */ /* * Configuration settings that are updated by Tk_ConfigureWidget. */ Tk_3DBorder normalBorder; /* Used for drawing border around whole * window, plus used for background. */ int borderWidth; /* Width of 3-D border around window. */ Tk_Cursor cursor; /* Current cursor for window, or None. */ int exportSelection; /* Non-zero means tie internal entry selection * to X selection. */ Tk_Font tkfont; /* Information about text font, or NULL. */ XColor *fgColorPtr; /* Text color in normal mode. */ XColor *highlightBgColorPtr;/* Color for drawing traversal highlight * area when highlight is off. */ XColor *highlightColorPtr; /* Color for drawing traversal highlight. */ int highlightWidth; /* Width in pixels of highlight to draw * around widget when it has the focus. * <= 0 means don't draw a highlight. */ Tk_3DBorder insertBorder; /* Used to draw vertical bar for insertion * cursor. */ int insertBorderWidth; /* Width of 3-D border around insert cursor. */ int insertOffTime; /* Number of milliseconds cursor should spend * in "off" state for each blink. */ int insertOnTime; /* Number of milliseconds cursor should spend * in "on" state for each blink. */ int insertWidth; /* Total width of insert cursor. */ Tk_Justify justify; /* Justification to use for text within * window. */ int relief; /* 3-D effect: TK_RELIEF_RAISED, etc. */ Tk_3DBorder selBorder; /* Border and background for selected * characters. */ int selBorderWidth; /* Width of border around selection. */ XColor *selFgColorPtr; /* Foreground color for selected text. */ char *showChar; /* Value of -show option. If non-NULL, first * character is used for displaying all * characters in entry. Malloc'ed. */ Tk_Uid state; /* Normal or disabled. Entry is read-only * when disabled. */ char *textVarName; /* Name of variable (malloc'ed) or NULL. * If non-NULL, entry's string tracks the * contents of this variable and vice versa. */ char *takeFocus; /* Value of -takefocus option; not used in * the C code, but used by keyboard traversal * scripts. Malloc'ed, but may be NULL. */ int prefWidth; /* Desired width of window, measured in * average characters. */ char *scrollCmd; /* Command prefix for communicating with * scrollbar(s). Malloc'ed. NULL means * no command to issue. */ /* * Fields whose values are derived from the current values of the * configuration settings above. */ int numChars; /* Number of non-NULL characters in * string (may be 0). */ char *displayString; /* If non-NULL, points to string with same * length as string but whose characters * are all equal to showChar. Malloc'ed. */ int inset; /* Number of pixels on the left and right * sides that are taken up by XPAD, borderWidth * (if any), and highlightWidth (if any). */ Tk_TextLayout textLayout; /* Cached text layout information. */ int layoutX, layoutY; /* Origin for layout. */ int leftIndex; /* Index of left-most character visible in * window. */ int leftX; /* X position at which character at leftIndex * is drawn (varies depending on justify). */ Tcl_TimerToken insertBlinkHandler; /* Timer handler used to blink cursor on and * off. */ GC textGC; /* For drawing normal text. */ GC selTextGC; /* For drawing selected text. */ GC highlightGC; /* For drawing traversal highlight. */ int avgWidth; /* Width of average character. */ int flags; /* Miscellaneous flags; see below for * definitions. */} Entry;/* * Assigned bits of "flags" fields of Entry structures, and what those * bits mean: * * REDRAW_PENDING: Non-zero means a DoWhenIdle handler has * already been queued to redisplay the entry. * BORDER_NEEDED: Non-zero means 3-D border must be redrawn * around window during redisplay. Normally * only text portion needs to be redrawn. * CURSOR_ON: Non-zero means insert cursor is displayed at * present. 0 means it isn't displayed. * GOT_FOCUS: Non-zero means this window has the input * focus. * UPDATE_SCROLLBAR: Non-zero means scrollbar should be updated * during next redisplay operation. * GOT_SELECTION: Non-zero means we've claimed the selection. */#define REDRAW_PENDING 1#define BORDER_NEEDED 2#define CURSOR_ON 4#define GOT_FOCUS 8#define UPDATE_SCROLLBAR 0x10#define GOT_SELECTION 0x20/* * The following macro defines how many extra pixels to leave on each * side of the text in the entry. */#define XPAD 1#define YPAD 1/* * Information used for argv parsing. */static Tk_ConfigSpec configSpecs[] = { {TK_CONFIG_BORDER, "-background", "background", "Background", DEF_ENTRY_BG_COLOR, Tk_Offset(Entry, normalBorder), TK_CONFIG_COLOR_ONLY}, {TK_CONFIG_BORDER, "-background", "background", "Background", DEF_ENTRY_BG_MONO, Tk_Offset(Entry, normalBorder), TK_CONFIG_MONO_ONLY}, {TK_CONFIG_SYNONYM, "-bd", "borderWidth", (char *) NULL, (char *) NULL, 0, 0}, {TK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL, (char *) NULL, 0, 0}, {TK_CONFIG_PIXELS, "-borderwidth", "borderWidth", "BorderWidth", DEF_ENTRY_BORDER_WIDTH, Tk_Offset(Entry, borderWidth), 0}, {TK_CONFIG_ACTIVE_CURSOR, "-cursor", "cursor", "Cursor", DEF_ENTRY_CURSOR, Tk_Offset(Entry, cursor), TK_CONFIG_NULL_OK}, {TK_CONFIG_BOOLEAN, "-exportselection", "exportSelection", "ExportSelection", DEF_ENTRY_EXPORT_SELECTION, Tk_Offset(Entry, exportSelection), 0}, {TK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL, (char *) NULL, 0, 0}, {TK_CONFIG_FONT, "-font", "font", "Font", DEF_ENTRY_FONT, Tk_Offset(Entry, tkfont), 0}, {TK_CONFIG_COLOR, "-foreground", "foreground", "Foreground", DEF_ENTRY_FG, Tk_Offset(Entry, fgColorPtr), 0}, {TK_CONFIG_COLOR, "-highlightbackground", "highlightBackground", "HighlightBackground", DEF_ENTRY_HIGHLIGHT_BG, Tk_Offset(Entry, highlightBgColorPtr), 0}, {TK_CONFIG_COLOR, "-highlightcolor", "highlightColor", "HighlightColor", DEF_ENTRY_HIGHLIGHT, Tk_Offset(Entry, highlightColorPtr), 0}, {TK_CONFIG_PIXELS, "-highlightthickness", "highlightThickness", "HighlightThickness", DEF_ENTRY_HIGHLIGHT_WIDTH, Tk_Offset(Entry, highlightWidth), 0}, {TK_CONFIG_BORDER, "-insertbackground", "insertBackground", "Foreground", DEF_ENTRY_INSERT_BG, Tk_Offset(Entry, insertBorder), 0}, {TK_CONFIG_PIXELS, "-insertborderwidth", "insertBorderWidth", "BorderWidth", DEF_ENTRY_INSERT_BD_COLOR, Tk_Offset(Entry, insertBorderWidth), TK_CONFIG_COLOR_ONLY}, {TK_CONFIG_PIXELS, "-insertborderwidth", "insertBorderWidth", "BorderWidth", DEF_ENTRY_INSERT_BD_MONO, Tk_Offset(Entry, insertBorderWidth), TK_CONFIG_MONO_ONLY}, {TK_CONFIG_INT, "-insertofftime", "insertOffTime", "OffTime", DEF_ENTRY_INSERT_OFF_TIME, Tk_Offset(Entry, insertOffTime), 0}, {TK_CONFIG_INT, "-insertontime", "insertOnTime", "OnTime", DEF_ENTRY_INSERT_ON_TIME, Tk_Offset(Entry, insertOnTime), 0}, {TK_CONFIG_PIXELS, "-insertwidth", "insertWidth", "InsertWidth", DEF_ENTRY_INSERT_WIDTH, Tk_Offset(Entry, insertWidth), 0}, {TK_CONFIG_JUSTIFY, "-justify", "justify", "Justify", DEF_ENTRY_JUSTIFY, Tk_Offset(Entry, justify), 0}, {TK_CONFIG_RELIEF, "-relief", "relief", "Relief", DEF_ENTRY_RELIEF, Tk_Offset(Entry, relief), 0}, {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground", DEF_ENTRY_SELECT_COLOR, Tk_Offset(Entry, selBorder), TK_CONFIG_COLOR_ONLY}, {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground", DEF_ENTRY_SELECT_MONO, Tk_Offset(Entry, selBorder), TK_CONFIG_MONO_ONLY}, {TK_CONFIG_PIXELS, "-selectborderwidth", "selectBorderWidth", "BorderWidth", DEF_ENTRY_SELECT_BD_COLOR, Tk_Offset(Entry, selBorderWidth), TK_CONFIG_COLOR_ONLY}, {TK_CONFIG_PIXELS, "-selectborderwidth", "selectBorderWidth", "BorderWidth", DEF_ENTRY_SELECT_BD_MONO, Tk_Offset(Entry, selBorderWidth), TK_CONFIG_MONO_ONLY}, {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background", DEF_ENTRY_SELECT_FG_COLOR, Tk_Offset(Entry, selFgColorPtr), TK_CONFIG_COLOR_ONLY}, {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background", DEF_ENTRY_SELECT_FG_MONO, Tk_Offset(Entry, selFgColorPtr), TK_CONFIG_MONO_ONLY}, {TK_CONFIG_STRING, "-show", "show", "Show", DEF_ENTRY_SHOW, Tk_Offset(Entry, showChar), TK_CONFIG_NULL_OK}, {TK_CONFIG_UID, "-state", "state", "State", DEF_ENTRY_STATE, Tk_Offset(Entry, state), 0}, {TK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus", DEF_ENTRY_TAKE_FOCUS, Tk_Offset(Entry, takeFocus), TK_CONFIG_NULL_OK}, {TK_CONFIG_STRING, "-textvariable", "textVariable", "Variable", DEF_ENTRY_TEXT_VARIABLE, Tk_Offset(Entry, textVarName), TK_CONFIG_NULL_OK}, {TK_CONFIG_INT, "-width", "width", "Width", DEF_ENTRY_WIDTH, Tk_Offset(Entry, prefWidth), 0}, {TK_CONFIG_STRING, "-xscrollcommand", "xScrollCommand", "ScrollCommand", DEF_ENTRY_SCROLL_COMMAND, Tk_Offset(Entry, scrollCmd), TK_CONFIG_NULL_OK}, {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL, (char *) NULL, 0, 0}};/* * Flags for GetEntryIndex procedure: */#define ZERO_OK 1#define LAST_PLUS_ONE_OK 2/* * Forward declarations for procedures defined later in this file: */static int ConfigureEntry _ANSI_ARGS_((Tcl_Interp *interp, Entry *entryPtr, int argc, char **argv, int flags));static void DeleteChars _ANSI_ARGS_((Entry *entryPtr, int index, int count));static void DestroyEntry _ANSI_ARGS_((char *memPtr));static void DisplayEntry _ANSI_ARGS_((ClientData clientData));static void EntryBlinkProc _ANSI_ARGS_((ClientData clientData));static void EntryCmdDeletedProc _ANSI_ARGS_(( ClientData clientData));static void EntryComputeGeometry _ANSI_ARGS_((Entry *entryPtr));static void EntryEventProc _ANSI_ARGS_((ClientData clientData, XEvent *eventPtr));static void EntryFocusProc _ANSI_ARGS_ ((Entry *entryPtr, int gotFocus));static int EntryFetchSelection _ANSI_ARGS_((ClientData clientData, int offset, char *buffer, int maxBytes));static void EntryLostSelection _ANSI_ARGS_(( ClientData clientData));static void EventuallyRedraw _ANSI_ARGS_((Entry *entryPtr));static void EntryScanTo _ANSI_ARGS_((Entry *entryPtr, int y));static void EntrySetValue _ANSI_ARGS_((Entry *entryPtr, char *value));static void EntrySelectTo _ANSI_ARGS_(( Entry *entryPtr, int index));static char * EntryTextVarProc _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, char *name1, char *name2, int flags));static void EntryUpdateScrollbar _ANSI_ARGS_((Entry *entryPtr));static void EntryValueChanged _ANSI_ARGS_((Entry *entryPtr));static void EntryVisibleRange _ANSI_ARGS_((Entry *entryPtr, double *firstPtr, double *lastPtr));static int EntryWidgetCmd _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc, char **argv));static void EntryWorldChanged _ANSI_ARGS_(( ClientData instanceData));static int GetEntryIndex _ANSI_ARGS_((Tcl_Interp *interp, Entry *entryPtr, char *string, int *indexPtr));static void InsertChars _ANSI_ARGS_((Entry *entryPtr, int index, char *string));/* * The structure below defines entry class behavior by means of procedures * that can be invoked from generic window code. */static TkClassProcs entryClass = { NULL, /* createProc. */ EntryWorldChanged, /* geometryProc. */ NULL /* modalProc. */};/* *-------------------------------------------------------------- * * Tk_EntryCmd -- * * This procedure is invoked to process the "entry" Tcl * command. See the user documentation for details on what * it does. * * Results: * A standard Tcl result. * * Side effects: * See the user documentation. * *-------------------------------------------------------------- */intTk_EntryCmd(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. */{ Tk_Window tkwin = (Tk_Window) clientData; register Entry *entryPtr; Tk_Window new; if (argc < 2) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " pathName ?options?\"", (char *) NULL); return TCL_ERROR; } new = Tk_CreateWindowFromPath(interp, tkwin, argv[1], (char *) NULL); if (new == NULL) { return TCL_ERROR; } /* * Initialize the fields of the structure that won't be initialized * by ConfigureEntry, or that ConfigureEntry requires to be * initialized already (e.g. resource pointers). */ entryPtr = (Entry *) ckalloc(sizeof(Entry)); entryPtr->tkwin = new; entryPtr->display = Tk_Display(new); entryPtr->interp = interp; entryPtr->widgetCmd = Tcl_CreateCommand(interp, Tk_PathName(entryPtr->tkwin), EntryWidgetCmd, (ClientData) entryPtr, EntryCmdDeletedProc); entryPtr->string = (char *) ckalloc(1); entryPtr->string[0] = '\0'; entryPtr->insertPos = 0; entryPtr->selectFirst = -1; entryPtr->selectLast = -1; entryPtr->selectAnchor = 0; entryPtr->scanMarkX = 0; entryPtr->scanMarkIndex = 0; entryPtr->normalBorder = NULL; entryPtr->borderWidth = 0; entryPtr->cursor = None; entryPtr->exportSelection = 1; entryPtr->tkfont = NULL; entryPtr->fgColorPtr = NULL; entryPtr->highlightBgColorPtr = NULL; entryPtr->highlightColorPtr = NULL; entryPtr->highlightWidth = 0; entryPtr->insertBorder = NULL; entryPtr->insertBorderWidth = 0; entryPtr->insertOffTime = 0; entryPtr->insertOnTime = 0; entryPtr->insertWidth = 0; entryPtr->justify = TK_JUSTIFY_LEFT; entryPtr->relief = TK_RELIEF_FLAT; entryPtr->selBorder = NULL; entryPtr->selBorderWidth = 0; entryPtr->selFgColorPtr = NULL; entryPtr->showChar = NULL; entryPtr->state = tkNormalUid; entryPtr->textVarName = NULL; entryPtr->takeFocus = NULL; entryPtr->prefWidth = 0; entryPtr->scrollCmd = NULL; entryPtr->numChars = 0; entryPtr->displayString = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -