From bd4341f10b3d578fa90169f24e61d8db933d680d Mon Sep 17 00:00:00 2001 From: fvogel Date: Fri, 3 May 2019 20:48:18 +0000 Subject: Fix [2858503fff]: 'end' index for ttk::combobox current --- doc/ttk_combobox.n | 4 +++- generic/ttk/ttkEntry.c | 55 +++++++++++++++++++++++++++++++++++++++++-------- tests/ttk/combobox.test | 11 ++++++++++ 3 files changed, 60 insertions(+), 10 deletions(-) diff --git a/doc/ttk_combobox.n b/doc/ttk_combobox.n index 9c01409..e287815 100644 --- a/doc/ttk_combobox.n +++ b/doc/ttk_combobox.n @@ -67,7 +67,9 @@ The following subcommands are possible for combobox widgets: .TP \fIpathName \fBcurrent\fR ?\fInewIndex\fR? If \fInewIndex\fR is supplied, sets the combobox value -to the element at position \fInewIndex\fR in the list of \fB\-values\fR. +to the element at position \fInewIndex\fR in the list of \fB\-values\fR +(in addition to integers, the \fBend\fR index is supported and indicates +the last element of the list). Otherwise, returns the index of the current value in the list of \fB\-values\fR or \fB\-1\fR if the current value does not appear in the list. .TP diff --git a/generic/ttk/ttkEntry.c b/generic/ttk/ttkEntry.c index ebc485e..a8d4f15 100644 --- a/generic/ttk/ttkEntry.c +++ b/generic/ttk/ttkEntry.c @@ -1700,6 +1700,16 @@ static WidgetSpec EntryWidgetSpec = { }; /*------------------------------------------------------------------------ + * Named indices for the combobox "current" command + */ +static const char *const comboboxCurrentIndexNames[] = { + "end", NULL +}; +enum comboboxCurrentIndices { + INDEX_END +}; + +/*------------------------------------------------------------------------ * +++ Combobox widget record. */ @@ -1800,15 +1810,42 @@ static int ComboboxCurrentCommand( Tcl_SetObjResult(interp, Tcl_NewIntObj(currentIndex)); return TCL_OK; } else if (objc == 3) { - if (Tcl_GetIntFromObj(interp, objv[2], ¤tIndex) != TCL_OK) { - return TCL_ERROR; - } - if (currentIndex < 0 || currentIndex >= nValues) { - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "Index %s out of range", Tcl_GetString(objv[2]))); - Tcl_SetErrorCode(interp, "TTK", "COMBOBOX", "IDX_RANGE", NULL); - return TCL_ERROR; - } + 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 */ + currentIndex = nValues - 1; + break; + } + } else { + + /* + * 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) { + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "Index %s out of range", Tcl_GetString(objv[2]))); + Tcl_SetErrorCode(interp, "TTK", "COMBOBOX", "IDX_RANGE", NULL); + return TCL_ERROR; + } + } cbPtr->combobox.currentIndex = currentIndex; diff --git a/tests/ttk/combobox.test b/tests/ttk/combobox.test index 7ea0c5c..45fe0fc 100644 --- a/tests/ttk/combobox.test +++ b/tests/ttk/combobox.test @@ -43,6 +43,17 @@ test combobox-2.4 "current -- value not in list" -body { .cb current } -result -1 +test combobox-2.5 "current -- set to end index" -body { + .cb configure -values [list a b c d e thelastone] + .cb current end + .cb get +} -result thelastone + +test combobox-2.6 "current -- set to unknown index" -body { + .cb configure -values [list a b c d e] + .cb current notanindex +} -returnCodes error -result {Incorrect index notanindex} + test combobox-2.end "Cleanup" -body { destroy .cb } test combobox-3 "Read postoffset value dynamically from current style" -body { -- cgit v0.12