diff options
Diffstat (limited to 'generic/ttk/ttkEntry.c')
-rw-r--r-- | generic/ttk/ttkEntry.c | 348 |
1 files changed, 178 insertions, 170 deletions
diff --git a/generic/ttk/ttkEntry.c b/generic/ttk/ttkEntry.c index 08fd085..3620a9a 100644 --- a/generic/ttk/ttkEntry.c +++ b/generic/ttk/ttkEntry.c @@ -1,11 +1,11 @@ /* * DERIVED FROM: tk/generic/tkEntry.c r1.35. * - * Copyright (c) 1990-1994 The Regents of the University of California. - * Copyright (c) 1994-1997 Sun Microsystems, Inc. - * Copyright (c) 2000 Ajuba Solutions. - * Copyright (c) 2002 ActiveState Corporation. - * Copyright (c) 2004 Joe English + * Copyright © 1990-1994 The Regents of the University of California. + * Copyright © 1994-1997 Sun Microsystems, Inc. + * Copyright © 2000 Ajuba Solutions. + * Copyright © 2002 ActiveState Corporation. + * Copyright © 2004 Joe English */ #include "tkInt.h" @@ -73,6 +73,7 @@ static const char *const validateReasonStrings[] = { /* Style parameters: */ typedef struct { + Tcl_Obj *placeholderForegroundObj;/* Foreground color for placeholder text */ Tcl_Obj *foregroundObj; /* Foreground color for normal text */ Tcl_Obj *backgroundObj; /* Entry widget background color */ Tcl_Obj *selBorderObj; /* Border and background for selection */ @@ -87,12 +88,12 @@ typedef struct { * Internal state: */ char *string; /* Storage for string (malloced) */ - int numBytes; /* Length of string in bytes. */ - int numChars; /* Length of string in characters. */ + Tcl_Size numBytes; /* Length of string in bytes. */ + Tcl_Size numChars; /* Length of string in characters. */ - int insertPos; /* Insert index */ - int selectFirst; /* Index of start of selection, or -1 */ - int selectLast; /* Index of end of selection, or -1 */ + Tcl_Size insertPos; /* Insert index */ + Tcl_Size selectFirst; /* Index of start of selection, or TCL_INDEX_NONE */ + Tcl_Size selectLast; /* Index of end of selection, or TCL_INDEX_NONE */ Scrollable xscroll; /* Current scroll position */ ScrollHandle xscrollHandle; @@ -118,6 +119,8 @@ typedef struct { Tcl_Obj *stateObj; /* Compatibility option -- see CheckStateObj */ + Tcl_Obj *placeholderObj; /* Text to display for placeholder text */ + /* * Derived resources: */ @@ -148,54 +151,62 @@ typedef struct { * Default option values: */ #define DEF_SELECT_BG "#000000" -#define DEF_SELECT_FG "#FFFFFF" +#define DEF_SELECT_FG "#ffffff" +#define DEF_PLACEHOLDER_FG "#b3b3b3" #define DEF_INSERT_BG "black" #define DEF_ENTRY_WIDTH "20" #define DEF_ENTRY_FONT "TkTextFont" #define DEF_LIST_HEIGHT "10" -static Tk_OptionSpec EntryOptionSpecs[] = { +static const Tk_OptionSpec EntryOptionSpecs[] = { {TK_OPTION_BOOLEAN, "-exportselection", "exportSelection", - "ExportSelection", "1", -1, Tk_Offset(Entry, entry.exportSelection), + "ExportSelection", "1", TCL_INDEX_NONE, offsetof(Entry, entry.exportSelection), 0,0,0 }, {TK_OPTION_FONT, "-font", "font", "Font", - DEF_ENTRY_FONT, Tk_Offset(Entry, entry.fontObj),-1, + DEF_ENTRY_FONT, offsetof(Entry, entry.fontObj),TCL_INDEX_NONE, 0,0,GEOMETRY_CHANGED}, {TK_OPTION_STRING, "-invalidcommand", "invalidCommand", "InvalidCommand", - NULL, -1, Tk_Offset(Entry, entry.invalidCmd), + NULL, TCL_INDEX_NONE, offsetof(Entry, entry.invalidCmd), TK_OPTION_NULL_OK, 0, 0}, {TK_OPTION_JUSTIFY, "-justify", "justify", "Justify", - "left", -1, Tk_Offset(Entry, entry.justify), - 0, 0, GEOMETRY_CHANGED}, + "left", TCL_INDEX_NONE, offsetof(Entry, entry.justify), + TK_OPTION_ENUM_VAR, 0, GEOMETRY_CHANGED}, + {TK_OPTION_STRING, "-placeholder", "placeHolder", "PlaceHolder", + NULL, offsetof(Entry, entry.placeholderObj), TCL_INDEX_NONE, + TK_OPTION_NULL_OK, 0, 0}, {TK_OPTION_STRING, "-show", "show", "Show", - NULL, -1, Tk_Offset(Entry, entry.showChar), + NULL, TCL_INDEX_NONE, offsetof(Entry, entry.showChar), TK_OPTION_NULL_OK, 0, 0}, {TK_OPTION_STRING, "-state", "state", "State", - "normal", Tk_Offset(Entry, entry.stateObj), -1, + "normal", offsetof(Entry, entry.stateObj), TCL_INDEX_NONE, 0,0,STATE_CHANGED}, {TK_OPTION_STRING, "-textvariable", "textVariable", "Variable", - NULL, Tk_Offset(Entry, entry.textVariableObj), -1, + NULL, offsetof(Entry, entry.textVariableObj), TCL_INDEX_NONE, TK_OPTION_NULL_OK,0,TEXTVAR_CHANGED}, {TK_OPTION_STRING_TABLE, "-validate", "validate", "Validate", - "none", -1, Tk_Offset(Entry, entry.validate), + "none", TCL_INDEX_NONE, offsetof(Entry, entry.validate), TK_OPTION_ENUM_VAR, validateStrings, 0}, {TK_OPTION_STRING, "-validatecommand", "validateCommand", "ValidateCommand", - NULL, -1, Tk_Offset(Entry, entry.validateCmd), + NULL, TCL_INDEX_NONE, offsetof(Entry, entry.validateCmd), TK_OPTION_NULL_OK, 0, 0}, {TK_OPTION_INT, "-width", "width", "Width", - DEF_ENTRY_WIDTH, Tk_Offset(Entry, entry.widthObj), -1, + DEF_ENTRY_WIDTH, offsetof(Entry, entry.widthObj), TCL_INDEX_NONE, 0,0,GEOMETRY_CHANGED}, {TK_OPTION_STRING, "-xscrollcommand", "xScrollCommand", "ScrollCommand", - NULL, -1, Tk_Offset(Entry, entry.xscroll.scrollCmd), + NULL, TCL_INDEX_NONE, offsetof(Entry, entry.xscroll.scrollCmd), TK_OPTION_NULL_OK, 0, SCROLLCMD_CHANGED}, /* EntryStyleData options: */ + {TK_OPTION_COLOR, "-background", "windowColor", "WindowColor", + NULL, offsetof(Entry, entry.styleData.backgroundObj), TCL_INDEX_NONE, + TK_OPTION_NULL_OK,0,0}, {TK_OPTION_COLOR, "-foreground", "textColor", "TextColor", - NULL, Tk_Offset(Entry, entry.styleData.foregroundObj), -1, + NULL, offsetof(Entry, entry.styleData.foregroundObj), TCL_INDEX_NONE, TK_OPTION_NULL_OK,0,0}, - {TK_OPTION_COLOR, "-background", "windowColor", "WindowColor", - NULL, Tk_Offset(Entry, entry.styleData.backgroundObj), -1, + {TK_OPTION_COLOR, "-placeholderforeground", "placeholderForeground", + "PlaceholderForeground", NULL, + offsetof(Entry, entry.styleData.placeholderForegroundObj), TCL_INDEX_NONE, TK_OPTION_NULL_OK,0,0}, WIDGET_TAKEFOCUS_TRUE, @@ -216,6 +227,7 @@ static void EntryInitStyleDefaults(EntryStyleData *es) #define INIT(member, value) \ es->member = Tcl_NewStringObj(value, -1); \ Tcl_IncrRefCount(es->member); + INIT(placeholderForegroundObj, DEF_PLACEHOLDER_FG) INIT(foregroundObj, DEFAULT_FOREGROUND) INIT(selBorderObj, DEF_SELECT_BG) INIT(selForegroundObj, DEF_SELECT_FG) @@ -227,6 +239,7 @@ static void EntryInitStyleDefaults(EntryStyleData *es) static void EntryFreeStyleDefaults(EntryStyleData *es) { + Tcl_DecrRefCount(es->placeholderForegroundObj); Tcl_DecrRefCount(es->foregroundObj); Tcl_DecrRefCount(es->selBorderObj); Tcl_DecrRefCount(es->selForegroundObj); @@ -253,6 +266,7 @@ static void EntryInitStyleData(Entry *entryPtr, EntryStyleData *es) # define INIT(member, name) \ if ((tmp=Ttk_QueryOption(entryPtr->core.layout,name,state))) \ es->member=tmp; + INIT(placeholderForegroundObj, "-placeholderforeground"); INIT(foregroundObj, "-foreground"); INIT(selBorderObj, "-selectbackground") INIT(selBorderWidthObj, "-selectborderwidth") @@ -263,6 +277,7 @@ static void EntryInitStyleData(Entry *entryPtr, EntryStyleData *es) /* Reacquire color & border resources from resource cache. */ + es->placeholderForegroundObj = Ttk_UseColor(cache, tkwin, es->placeholderForegroundObj); es->foregroundObj = Ttk_UseColor(cache, tkwin, es->foregroundObj); es->selForegroundObj = Ttk_UseColor(cache, tkwin, es->selForegroundObj); es->insertColorObj = Ttk_UseColor(cache, tkwin, es->insertColorObj); @@ -304,12 +319,23 @@ static char *EntryDisplayString(const char *showChar, int numChars) */ static void EntryUpdateTextLayout(Entry *entryPtr) { + Tcl_Size length; + char *text; Tk_FreeTextLayout(entryPtr->entry.textLayout); - entryPtr->entry.textLayout = Tk_ComputeTextLayout( + if ((entryPtr->entry.numChars != 0) || (entryPtr->entry.placeholderObj == NULL)) { + entryPtr->entry.textLayout = Tk_ComputeTextLayout( Tk_GetFontFromObj(entryPtr->core.tkwin, entryPtr->entry.fontObj), entryPtr->entry.displayString, entryPtr->entry.numChars, 0/*wraplength*/, entryPtr->entry.justify, TK_IGNORE_NEWLINES, &entryPtr->entry.layoutWidth, &entryPtr->entry.layoutHeight); + } else { + text = Tcl_GetStringFromObj(entryPtr->entry.placeholderObj, &length); + entryPtr->entry.textLayout = Tk_ComputeTextLayout( + Tk_GetFontFromObj(entryPtr->core.tkwin, entryPtr->entry.fontObj), + text, length, + 0/*wraplength*/, entryPtr->entry.justify, TK_IGNORE_NEWLINES, + &entryPtr->entry.layoutWidth, &entryPtr->entry.layoutHeight); + } } /* EntryEditable -- @@ -328,32 +354,32 @@ EntryEditable(Entry *entryPtr) /* EntryFetchSelection -- * Selection handler for entry widgets. */ -static int +static Tcl_Size EntryFetchSelection( - void *clientData, int offset, char *buffer, int maxBytes) + void *clientData, Tcl_Size offset, char *buffer, Tcl_Size maxBytes) { Entry *entryPtr = (Entry *)clientData; - int byteCount; + Tcl_Size byteCount; const char *string; const char *selStart, *selEnd; if (entryPtr->entry.selectFirst < 0 || (!entryPtr->entry.exportSelection) || Tcl_IsSafe(entryPtr->core.interp)) { - return -1; + return TCL_INDEX_NONE; } string = entryPtr->entry.displayString; selStart = TkUtfAtIndex(string, entryPtr->entry.selectFirst); selEnd = TkUtfAtIndex(selStart, entryPtr->entry.selectLast - entryPtr->entry.selectFirst); + if (selEnd <= selStart + offset) { + return 0; + } byteCount = selEnd - selStart - offset; if (byteCount > maxBytes) { /* @@@POSSIBLE BUG: Can transfer partial UTF-8 sequences. Is this OK? */ byteCount = maxBytes; } - if (byteCount <= 0) { - return 0; - } memcpy(buffer, selStart + offset, byteCount); buffer[byteCount] = '\0'; return byteCount; @@ -367,7 +393,7 @@ static void EntryLostSelection(void *clientData) { Entry *entryPtr = (Entry *)clientData; entryPtr->core.flags &= ~GOT_SELECTION; - entryPtr->entry.selectFirst = entryPtr->entry.selectLast = -1; + entryPtr->entry.selectFirst = entryPtr->entry.selectLast = TCL_INDEX_NONE; TtkRedisplayWidget(&entryPtr->core); } @@ -399,7 +425,7 @@ ExpandPercents( Entry *entryPtr, /* Entry that needs validation. */ const char *templ, /* Script template */ const char *newValue, /* Potential new value of entry string */ - int index, /* index of insert/delete */ + Tcl_Size index, /* index of insert/delete */ int count, /* #changed characters */ VREASON reason, /* Reason for change */ Tcl_DString *dsPtr) /* Result of %-substitutions */ @@ -420,7 +446,7 @@ ExpandPercents( /* No more %-sequences to expand. * Copy the rest of the template. */ - Tcl_DStringAppend(dsPtr, templ, -1); + Tcl_DStringAppend(dsPtr, templ, TCL_INDEX_NONE); return; } if (string != templ) { @@ -451,7 +477,7 @@ ExpandPercents( string = numStorage; break; case 'i': /* index of insert/delete */ - snprintf(numStorage, sizeof(numStorage), "%d", index); + snprintf(numStorage, sizeof(numStorage), "%" TCL_SIZE_MODIFIER "d", index); string = numStorage; break; case 'P': /* 'Peeked' new value of the string */ @@ -509,8 +535,8 @@ static int RunValidationScript( const char *templ, /* Script template */ const char *optionName, /* "-validatecommand", "-invalidcommand" */ const char *newValue, /* Potential new value of entry string */ - int index, /* index of insert/delete */ - int count, /* #changed characters */ + Tcl_Size index, /* index of insert/delete */ + Tcl_Size count, /* #changed characters */ VREASON reason) /* Reason for change */ { Tcl_DString script; @@ -570,8 +596,8 @@ static int EntryValidateChange( Entry *entryPtr, /* Entry that needs validation. */ const char *newValue, /* Potential new value of entry string */ - int index, /* index of insert/delete, -1 otherwise */ - int count, /* #changed characters */ + Tcl_Size index, /* index of insert/delete, TCL_INDEX_NONE otherwise */ + Tcl_Size count, /* #changed characters */ VREASON reason) /* Reason for change */ { Tcl_Interp *interp = entryPtr->core.interp; @@ -701,7 +727,7 @@ static void AdjustIndices(Entry *entryPtr, int index, int nChars) e->xscroll.first= AdjustIndex(e->xscroll.first, index+g, nChars); if (e->selectLast <= e->selectFirst) - e->selectFirst = e->selectLast = -1; + e->selectFirst = e->selectLast = TCL_INDEX_NONE; } /* EntryStoreValue -- @@ -714,7 +740,7 @@ static void EntryStoreValue(Entry *entryPtr, const char *value) { size_t numBytes = strlen(value); - int numChars = Tcl_NumUtfChars(value, numBytes); + Tcl_Size numChars = TkNumUtfChars(value, numBytes); if (entryPtr->core.flags & VALIDATING) entryPtr->core.flags |= VALIDATION_SET_VALUE; @@ -815,13 +841,13 @@ static void EntryTextVariableTrace(void *recordPtr, const char *value) static int InsertChars( Entry *entryPtr, /* Entry that is to get the new elements. */ - int index, /* Insert before this index */ + Tcl_Size index, /* Insert before this index */ const char *value) /* New characters to add */ { char *string = entryPtr->entry.string; size_t byteIndex = TkUtfAtIndex(string, index) - string; size_t byteCount = strlen(value); - int charsAdded = Tcl_NumUtfChars(value, byteCount); + int charsAdded = TkNumUtfChars(value, byteCount); size_t newByteCount = entryPtr->entry.numBytes + byteCount + 1; char *newBytes; int code; @@ -855,8 +881,8 @@ InsertChars( static int DeleteChars( Entry *entryPtr, /* Entry widget to modify. */ - int index, /* Index of first character to delete. */ - int count) /* How many characters to delete. */ + Tcl_Size index, /* Index of first character to delete. */ + Tcl_Size count) /* How many characters to delete. */ { char *string = entryPtr->entry.string; size_t byteIndex, byteCount, newByteCount; @@ -866,7 +892,7 @@ DeleteChars( if (index < 0) { index = 0; } - if (count + index > entryPtr->entry.numChars) { + if (count + index > entryPtr->entry.numChars) { count = entryPtr->entry.numChars - index; } if (count <= 0) { @@ -954,8 +980,8 @@ EntryInitialize( TtkCreateScrollHandle(&entryPtr->core, &entryPtr->entry.xscroll); entryPtr->entry.insertPos = 0; - entryPtr->entry.selectFirst = -1; - entryPtr->entry.selectLast = -1; + entryPtr->entry.selectFirst = TCL_INDEX_NONE; + entryPtr->entry.selectLast = TCL_INDEX_NONE; } static void @@ -1010,7 +1036,7 @@ static int EntryConfigure(Tcl_Interp *interp, void *recordPtr, int mask) /* Claim the selection, in case we've suddenly started exporting it. */ - if (entryPtr->entry.exportSelection && (entryPtr->entry.selectFirst != -1) + if (entryPtr->entry.exportSelection && (entryPtr->entry.selectFirst >= 0) && (!Tcl_IsSafe(entryPtr->core.interp))) { EntryOwnSelection(entryPtr); } @@ -1071,7 +1097,7 @@ static int EntryPostConfigure( * Precondition: textLayout and layoutX up-to-date. */ static int -EntryCharPosition(Entry *entryPtr, int index) +EntryCharPosition(Entry *entryPtr, Tcl_Size index) { int xPos; Tk_CharBbox(entryPtr->entry.textLayout, index, &xPos, NULL, NULL, NULL); @@ -1180,7 +1206,7 @@ static void EntryDisplay(void *clientData, Drawable d) { Entry *entryPtr = (Entry *)clientData; Tk_Window tkwin = entryPtr->core.tkwin; - int leftIndex = entryPtr->entry.xscroll.first, + Tcl_Size leftIndex = entryPtr->entry.xscroll.first, rightIndex = entryPtr->entry.xscroll.last + 1, selFirst = entryPtr->entry.selectFirst, selLast = entryPtr->entry.selectLast; @@ -1190,6 +1216,7 @@ static void EntryDisplay(void *clientData, Drawable d) Ttk_Box textarea; TkRegion clipRegion; XRectangle rect; + Tcl_Obj *foregroundObj; EntryInitStyleData(entryPtr, &es); @@ -1289,7 +1316,21 @@ static void EntryDisplay(void *clientData, Drawable d) /* Draw the text: */ - gc = EntryGetGC(entryPtr, es.foregroundObj, clipRegion); + if ((*(entryPtr->entry.displayString) == '\0') + && (entryPtr->entry.placeholderObj != NULL)) { + /* No text displayed, but -placeholder is given */ + if (TkGetCharLength(es.placeholderForegroundObj) > 0) { + foregroundObj = es.placeholderForegroundObj; + } else { + foregroundObj = es.foregroundObj; + } + /* Use placeholder text width */ + leftIndex = 0; + (void)Tcl_GetStringFromObj(entryPtr->entry.placeholderObj, &rightIndex); + } else { + foregroundObj = es.foregroundObj; + } + gc = EntryGetGC(entryPtr, foregroundObj, clipRegion); if (showSelection) { /* Draw the selected and unselected portions separately. @@ -1359,15 +1400,25 @@ EntryIndex( Tcl_Interp *interp, /* For error messages. */ Entry *entryPtr, /* Entry widget to query */ Tcl_Obj *indexObj, /* Symbolic index name */ - int *indexPtr) /* Return value */ + Tcl_Size *indexPtr) /* Return value */ { # define EntryWidth(e) (Tk_Width(entryPtr->core.tkwin)) /* Not Right */ - const char *string = Tcl_GetString(indexObj); - size_t length = indexObj->length; + Tcl_Size length, idx; + const char *string; + + if (TCL_OK == TkGetIntForIndex(indexObj, entryPtr->entry.numChars - 1, 1, &idx)) { + if (idx < 0) { + idx = 0; + } else if (idx > entryPtr->entry.numChars) { + idx = entryPtr->entry.numChars; + } + *indexPtr = idx; + return TCL_OK; + } - if (strncmp(string, "end", length) == 0) { - *indexPtr = entryPtr->entry.numChars; - } else if (strncmp(string, "insert", length) == 0) { + string = Tcl_GetStringFromObj(indexObj, &length); + + if (strncmp(string, "insert", length) == 0) { *indexPtr = entryPtr->entry.insertPos; } else if (strncmp(string, "left", length) == 0) { /* for debugging */ *indexPtr = entryPtr->entry.xscroll.first; @@ -1419,14 +1470,7 @@ EntryIndex( *indexPtr += 1; } } else { - if (Tcl_GetIntFromObj(interp, indexObj, indexPtr) != TCL_OK) { - goto badIndex; - } - if (*indexPtr < 0) { - *indexPtr = 0; - } else if (*indexPtr > entryPtr->entry.numChars) { - *indexPtr = entryPtr->entry.numChars; - } + goto badIndex; } return TCL_OK; @@ -1442,11 +1486,11 @@ badIndex: */ static int EntryBBoxCommand( - void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) + void *recordPtr, Tcl_Interp *interp, Tcl_Size objc, Tcl_Obj *const objv[]) { Entry *entryPtr = (Entry *)recordPtr; Ttk_Box b; - int index; + Tcl_Size index; if (objc != 3) { Tcl_WrongNumArgs(interp, 2, objv, "index"); @@ -1472,10 +1516,10 @@ EntryBBoxCommand( */ static int EntryDeleteCommand( - void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) + void *recordPtr, Tcl_Interp *interp, Tcl_Size objc, Tcl_Obj *const objv[]) { Entry *entryPtr = (Entry *)recordPtr; - int first, last; + Tcl_Size first, last; if ((objc < 3) || (objc > 4)) { Tcl_WrongNumArgs(interp, 2, objv, "firstIndex ?lastIndex?"); @@ -1501,7 +1545,7 @@ EntryDeleteCommand( */ static int EntryGetCommand( - void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) + void *recordPtr, Tcl_Interp *interp, Tcl_Size objc, Tcl_Obj *const objv[]) { Entry *entryPtr = (Entry *)recordPtr; if (objc != 2) { @@ -1517,7 +1561,7 @@ EntryGetCommand( */ static int EntryICursorCommand( - void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) + void *recordPtr, Tcl_Interp *interp, Tcl_Size objc, Tcl_Obj *const objv[]) { Entry *entryPtr = (Entry *)recordPtr; if (objc != 3) { @@ -1537,10 +1581,10 @@ EntryICursorCommand( */ static int EntryIndexCommand( - void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) + void *recordPtr, Tcl_Interp *interp, Tcl_Size objc, Tcl_Obj *const objv[]) { Entry *entryPtr = (Entry *)recordPtr; - int index; + Tcl_Size index; if (objc != 3) { Tcl_WrongNumArgs(interp, 2, objv, "string"); @@ -1549,7 +1593,7 @@ EntryIndexCommand( if (EntryIndex(interp, entryPtr, objv[2], &index) != TCL_OK) { return TCL_ERROR; } - Tcl_SetObjResult(interp, Tcl_NewIntObj(index)); + Tcl_SetObjResult(interp, TkNewIndexObj(index)); return TCL_OK; } @@ -1559,10 +1603,10 @@ EntryIndexCommand( */ static int EntryInsertCommand( - void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) + void *recordPtr, Tcl_Interp *interp, Tcl_Size objc, Tcl_Obj *const objv[]) { Entry *entryPtr = (Entry *)recordPtr; - int index; + Tcl_Size index; if (objc != 4) { Tcl_WrongNumArgs(interp, 2, objv, "index text"); @@ -1581,7 +1625,7 @@ EntryInsertCommand( * Clear selection. */ static int EntrySelectionClearCommand( - void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) + void *recordPtr, Tcl_Interp *interp, Tcl_Size objc, Tcl_Obj *const objv[]) { Entry *entryPtr = (Entry *)recordPtr; @@ -1589,7 +1633,7 @@ static int EntrySelectionClearCommand( Tcl_WrongNumArgs(interp, 3, objv, NULL); return TCL_ERROR; } - entryPtr->entry.selectFirst = entryPtr->entry.selectLast = -1; + entryPtr->entry.selectFirst = entryPtr->entry.selectLast = TCL_INDEX_NONE; TtkRedisplayWidget(&entryPtr->core); return TCL_OK; } @@ -1598,7 +1642,7 @@ static int EntrySelectionClearCommand( * Returns 1 if any characters are selected, 0 otherwise. */ static int EntrySelectionPresentCommand( - void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) + void *recordPtr, Tcl_Interp *interp, Tcl_Size objc, Tcl_Obj *const objv[]) { Entry *entryPtr = (Entry *)recordPtr; if (objc != 3) { @@ -1614,10 +1658,10 @@ static int EntrySelectionPresentCommand( * Explicitly set the selection range. */ static int EntrySelectionRangeCommand( - void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) + void *recordPtr, Tcl_Interp *interp, Tcl_Size objc, Tcl_Obj *const objv[]) { Entry *entryPtr = (Entry *)recordPtr; - int start, end; + Tcl_Size start, end; if (objc != 5) { Tcl_WrongNumArgs(interp, 3, objv, "start end"); return TCL_ERROR; @@ -1631,7 +1675,7 @@ static int EntrySelectionRangeCommand( } if (start >= end) { - entryPtr->entry.selectFirst = entryPtr->entry.selectLast = -1; + entryPtr->entry.selectFirst = entryPtr->entry.selectLast = TCL_INDEX_NONE; } else { entryPtr->entry.selectFirst = start; entryPtr->entry.selectLast = end; @@ -1652,7 +1696,7 @@ static const Ttk_Ensemble EntrySelectionCommands[] = { * Sets the value of an entry widget. */ static int EntrySetCommand( - void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) + void *recordPtr, Tcl_Interp *interp, Tcl_Size objc, Tcl_Obj *const objv[]) { Entry *entryPtr = (Entry *)recordPtr; if (objc != 3) { @@ -1668,7 +1712,7 @@ static int EntrySetCommand( * or error status from -validatecommand / -invalidcommand. */ static int EntryValidateCommand( - void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) + void *recordPtr, Tcl_Interp *interp, Tcl_Size objc, Tcl_Obj *const objv[]) { Entry *entryPtr = (Entry *)recordPtr; int code; @@ -1690,11 +1734,11 @@ static int EntryValidateCommand( /* $entry xview -- horizontal scrolling interface */ static int EntryXViewCommand( - void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) + void *recordPtr, Tcl_Interp *interp, Tcl_Size objc, Tcl_Obj *const objv[]) { Entry *entryPtr = (Entry *)recordPtr; if (objc == 3) { - int newFirst; + Tcl_Size newFirst; if (EntryIndex(interp, entryPtr, objv[2], &newFirst) != TCL_OK) { return TCL_ERROR; } @@ -1717,6 +1761,7 @@ static const Ttk_Ensemble EntryCommands[] = { { "instate", TtkWidgetInstateCommand,0 }, { "selection", 0,EntrySelectionCommands }, { "state", TtkWidgetStateCommand,0 }, + { "style", TtkWidgetStyleCommand,0 }, { "validate", EntryValidateCommand,0 }, { "xview", EntryXViewCommand,0 }, { 0,0,0 } @@ -1726,7 +1771,7 @@ static const Ttk_Ensemble EntryCommands[] = { * +++ Entry widget definition. */ -static WidgetSpec EntryWidgetSpec = { +static const WidgetSpec EntryWidgetSpec = { "TEntry", /* className */ sizeof(Entry), /* recordSize */ EntryOptionSpecs, /* optionSpecs */ @@ -1742,16 +1787,6 @@ static WidgetSpec EntryWidgetSpec = { }; /*------------------------------------------------------------------------ - * Named indices for the combobox "current" command - */ -static const char *const comboboxCurrentIndexNames[] = { - "end", NULL -}; -enum comboboxCurrentIndices { - INDEX_END -}; - -/*------------------------------------------------------------------------ * +++ Combobox widget record. */ @@ -1759,7 +1794,7 @@ typedef struct { Tcl_Obj *postCommandObj; Tcl_Obj *valuesObj; Tcl_Obj *heightObj; - int currentIndex; + Tcl_Size currentIndex; } ComboboxPart; typedef struct { @@ -1768,15 +1803,15 @@ typedef struct { ComboboxPart combobox; } Combobox; -static Tk_OptionSpec ComboboxOptionSpecs[] = { +static const Tk_OptionSpec ComboboxOptionSpecs[] = { {TK_OPTION_STRING, "-height", "height", "Height", - DEF_LIST_HEIGHT, Tk_Offset(Combobox, combobox.heightObj), -1, + DEF_LIST_HEIGHT, offsetof(Combobox, combobox.heightObj), TCL_INDEX_NONE, 0,0,0 }, {TK_OPTION_STRING, "-postcommand", "postCommand", "PostCommand", - "", Tk_Offset(Combobox, combobox.postCommandObj), -1, + "", offsetof(Combobox, combobox.postCommandObj), TCL_INDEX_NONE, 0,0,0 }, {TK_OPTION_STRING, "-values", "values", "Values", - "", Tk_Offset(Combobox, combobox.valuesObj), -1, + "", offsetof(Combobox, combobox.valuesObj), TCL_INDEX_NONE, 0,0,0 }, WIDGET_INHERIT_OPTIONS(EntryOptionSpecs) }; @@ -1789,7 +1824,7 @@ ComboboxInitialize(Tcl_Interp *interp, void *recordPtr) { Combobox *cb = (Combobox *)recordPtr; - cb->combobox.currentIndex = -1; + cb->combobox.currentIndex = TCL_INDEX_NONE; TtkTrackElementState(&cb->core); EntryInitialize(interp, recordPtr); } @@ -1801,7 +1836,7 @@ static int ComboboxConfigure(Tcl_Interp *interp, void *recordPtr, int mask) { Combobox *cbPtr = (Combobox *)recordPtr; - int unused; + Tcl_Size unused; /* Make sure -values is a valid list: */ @@ -1818,12 +1853,12 @@ ComboboxConfigure(Tcl_Interp *interp, void *recordPtr, int mask) * in sync at all times, [$cb current] double-checks */ static int ComboboxCurrentCommand( - void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) + void *recordPtr, Tcl_Interp *interp, Tcl_Size objc, Tcl_Obj *const objv[]) { Combobox *cbPtr = (Combobox *)recordPtr; - int currentIndex = cbPtr->combobox.currentIndex; + Tcl_Size currentIndex = cbPtr->combobox.currentIndex; const char *currentValue = cbPtr->entry.string; - int nValues; + Tcl_Size nValues; Tcl_Obj **values; Tcl_ListObjGetElements(interp, cbPtr->combobox.valuesObj, &nValues, &values); @@ -1845,57 +1880,28 @@ static int ComboboxCurrentCommand( } if (currentIndex >= nValues) { /* Not found */ - currentIndex = -1; + currentIndex = TCL_INDEX_NONE; } } cbPtr->combobox.currentIndex = currentIndex; - Tcl_SetObjResult(interp, Tcl_NewIntObj(currentIndex)); + Tcl_SetObjResult(interp, TkNewIndexObj(currentIndex)); return TCL_OK; } else if (objc == 3) { - int result, index; - - result = Tcl_GetIndexFromObj(NULL, objv[2], comboboxCurrentIndexNames, - "", 0, &index); - if (result == TCL_OK) { - - /* - * The index is one of the named indices. - */ - - switch (index) { - case INDEX_END: - /* "end" index */ - if (nValues <= 0) { - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "index \"end\" out of range")); - Tcl_SetErrorCode(interp, "TTK", "COMBOBOX", "IDX_RANGE", NULL); - return TCL_ERROR; - } - currentIndex = nValues - 1; - break; - default: - Tcl_Panic("Unknown named index"); - return TCL_ERROR; - } - } else { + Tcl_Size idx; - /* - * The index should be just an integer. - */ - - if (Tcl_GetIntFromObj(NULL, objv[2], ¤tIndex) != TCL_OK) { - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "Incorrect index %s", Tcl_GetString(objv[2]))); - Tcl_SetErrorCode(interp, "TTK", "COMBOBOX", "IDX_VALUE", NULL); - return TCL_ERROR; - } - - if (currentIndex < 0 || currentIndex >= nValues) { + if (TCL_OK == TkGetIntForIndex(objv[2], nValues - 1, 0, &idx)) { + if (idx < 0 || idx >= nValues) { Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "Index %s out of range", Tcl_GetString(objv[2]))); + "index \"%s\" out of range", Tcl_GetString(objv[2]))); Tcl_SetErrorCode(interp, "TTK", "COMBOBOX", "IDX_RANGE", NULL); return TCL_ERROR; } + currentIndex = idx; + } else { + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "bad index \"%s\"", Tcl_GetString(objv[2]))); + Tcl_SetErrorCode(interp, "TTK", "COMBOBOX", "IDX_VALUE", NULL); + return TCL_ERROR; } cbPtr->combobox.currentIndex = currentIndex; @@ -1924,14 +1930,15 @@ static const Ttk_Ensemble ComboboxCommands[] = { { "insert", EntryInsertCommand,0 }, { "instate", TtkWidgetInstateCommand,0 }, { "selection", 0,EntrySelectionCommands }, - { "state", TtkWidgetStateCommand,0 }, { "set", EntrySetCommand,0 }, + { "state", TtkWidgetStateCommand,0 }, + { "style", TtkWidgetStyleCommand,0 }, { "validate", EntryValidateCommand,0 }, { "xview", EntryXViewCommand,0 }, { 0,0,0 } }; -static WidgetSpec ComboboxWidgetSpec = { +static const WidgetSpec ComboboxWidgetSpec = { "TCombobox", /* className */ sizeof(Combobox), /* recordSize */ ComboboxOptionSpecs, /* optionSpecs */ @@ -1968,29 +1975,29 @@ typedef struct { SpinboxPart spinbox; } Spinbox; -static Tk_OptionSpec SpinboxOptionSpecs[] = { +static const Tk_OptionSpec SpinboxOptionSpecs[] = { {TK_OPTION_STRING, "-values", "values", "Values", - "", Tk_Offset(Spinbox, spinbox.valuesObj), -1, + "", offsetof(Spinbox, spinbox.valuesObj), TCL_INDEX_NONE, 0,0,0 }, {TK_OPTION_DOUBLE, "-from", "from", "From", - "0", Tk_Offset(Spinbox,spinbox.fromObj), -1, + "0.0", offsetof(Spinbox,spinbox.fromObj), TCL_INDEX_NONE, 0,0,0 }, {TK_OPTION_DOUBLE, "-to", "to", "To", - "0", Tk_Offset(Spinbox,spinbox.toObj), -1, + "0.0", offsetof(Spinbox,spinbox.toObj), TCL_INDEX_NONE, 0,0,0 }, {TK_OPTION_DOUBLE, "-increment", "increment", "Increment", - "1", Tk_Offset(Spinbox,spinbox.incrementObj), -1, + "1.0", offsetof(Spinbox,spinbox.incrementObj), TCL_INDEX_NONE, 0,0,0 }, {TK_OPTION_STRING, "-format", "format", "Format", - "", Tk_Offset(Spinbox, spinbox.formatObj), -1, + "", offsetof(Spinbox, spinbox.formatObj), TCL_INDEX_NONE, 0,0,0 }, {TK_OPTION_STRING, "-command", "command", "Command", - "", Tk_Offset(Spinbox, spinbox.commandObj), -1, + "", offsetof(Spinbox, spinbox.commandObj), TCL_INDEX_NONE, 0,0,0 }, {TK_OPTION_BOOLEAN, "-wrap", "wrap", "Wrap", - "0", Tk_Offset(Spinbox,spinbox.wrapObj), -1, + "0", offsetof(Spinbox,spinbox.wrapObj), TCL_INDEX_NONE, 0,0,0 }, WIDGET_INHERIT_OPTIONS(EntryOptionSpecs) @@ -2014,7 +2021,7 @@ static int SpinboxConfigure(Tcl_Interp *interp, void *recordPtr, int mask) { Spinbox *sb = (Spinbox *)recordPtr; - int unused; + Tcl_Size unused; /* Make sure -values is a valid list: */ @@ -2036,14 +2043,15 @@ static const Ttk_Ensemble SpinboxCommands[] = { { "insert", EntryInsertCommand,0 }, { "instate", TtkWidgetInstateCommand,0 }, { "selection", 0,EntrySelectionCommands }, - { "state", TtkWidgetStateCommand,0 }, { "set", EntrySetCommand,0 }, + { "state", TtkWidgetStateCommand,0 }, + { "style", TtkWidgetStyleCommand,0 }, { "validate", EntryValidateCommand,0 }, { "xview", EntryXViewCommand,0 }, { 0,0,0 } }; -static WidgetSpec SpinboxWidgetSpec = { +static const WidgetSpec SpinboxWidgetSpec = { "TSpinbox", /* className */ sizeof(Spinbox), /* recordSize */ SpinboxOptionSpecs, /* optionSpecs */ @@ -2070,11 +2078,11 @@ typedef struct { Tcl_Obj *widthObj; } TextareaElement; -static Ttk_ElementOptionSpec TextareaElementOptions[] = { +static const Ttk_ElementOptionSpec TextareaElementOptions[] = { { "-font", TK_OPTION_FONT, - Tk_Offset(TextareaElement,fontObj), DEF_ENTRY_FONT }, + offsetof(TextareaElement,fontObj), DEF_ENTRY_FONT }, { "-width", TK_OPTION_INT, - Tk_Offset(TextareaElement,widthObj), "20" }, + offsetof(TextareaElement,widthObj), "20" }, { NULL, TK_OPTION_BOOLEAN, 0, NULL } }; @@ -2101,7 +2109,7 @@ static void TextareaElementSize( *widthPtr = prefWidth * avgWidth; } -static Ttk_ElementSpec TextareaElementSpec = { +static const Ttk_ElementSpec TextareaElementSpec = { TK_STYLE_VERSION_2, sizeof(TextareaElement), TextareaElementOptions, |