Index: doc/listbox.n ================================================================== --- doc/listbox.n +++ doc/listbox.n @@ -15,13 +15,14 @@ \fBlistbox\fR \fIpathName \fR?\fIoptions\fR? .SO \-background \-borderwidth \-cursor \-disabledforeground \-exportselection \-font \-foreground \-highlightbackground \-highlightcolor -\-highlightthickness \-relief \-selectbackground -\-selectborderwidth \-selectforeground \-setgrid -\-takefocus \-xscrollcommand \-yscrollcommand +\-highlightthickness \-justify \-relief +\-selectbackground \-selectborderwidth \-selectforeground +\-setgrid \-takefocus \-xscrollcommand +\-yscrollcommand .SE .SH "WIDGET-SPECIFIC OPTIONS" .OP \-activestyle activeStyle ActiveStyle Specifies the style in which to draw the active element. This must be one of \fBdotbox\fR (show a focus ring around the active element), Index: generic/tkListbox.c ================================================================== --- generic/tkListbox.c +++ generic/tkListbox.c @@ -88,18 +88,18 @@ int height; /* Desired height of window, in lines. */ int lineHeight; /* Number of pixels allocated for each line in * display. */ int topIndex; /* Index of top-most element visible in * window. */ - int fullLines; /* Number of lines that fit are completely + int fullLines; /* Number of lines that are completely * visible in window. There may be one * additional line at the bottom that is * partially visible. */ int partialLine; /* 0 means that the window holds exactly * fullLines lines. 1 means that there is one * additional line that is partially - * visble. */ + * visible. */ int setGrid; /* Non-zero means pass gridding information to * window manager. */ /* * Information to support horizontal scrolling: @@ -112,11 +112,12 @@ * horizontally in increments of this size). * This is an average character size. */ int xOffset; /* The left edge of each string in the listbox * is offset to the left by this many pixels * (0 means no offset, positive means there is - * an offset). */ + * an offset). This is x scrolling information + * is not linked to justification. */ /* * Information about what's selected or active, if any. */ @@ -129,11 +130,11 @@ int exportSelection; /* Non-zero means tie internal listbox to X * selection. */ int active; /* Index of "active" element (the one that has * been selected by keyboard traversal). -1 * means none. */ - int activeStyle; /* style in which to draw the active element. + int activeStyle; /* Style in which to draw the active element. * One of: underline, none, dotbox */ /* * Information for scanning: */ @@ -163,10 +164,11 @@ * to issue. Malloc'ed. */ int state; /* Listbox state. */ Pixmap gray; /* Pixmap for displaying disabled text. */ int flags; /* Various flag bits: see below for * definitions. */ + Tk_Justify justify; /* Justification. */ } Listbox; /* * How to encode the keys for the hash tables used to store what items are * selected and what the attributes are. @@ -195,11 +197,11 @@ * updated. * UPDATE_H_SCROLLBAR: Non-zero means horizontal scrollbar needs to * be updated. * GOT_FOCUS: Non-zero means this widget currently has the * input focus. - * MAXWIDTH_IS_STALE: Stored maxWidth may be out-of-date + * MAXWIDTH_IS_STALE: Stored maxWidth may be out-of-date. * LISTBOX_DELETED: This listbox has been effectively destroyed. */ #define REDRAW_PENDING 1 #define UPDATE_V_SCROLLBAR 2 @@ -273,10 +275,12 @@ DEF_LISTBOX_HIGHLIGHT, -1, Tk_Offset(Listbox, highlightColorPtr), 0, 0, 0}, {TK_OPTION_PIXELS, "-highlightthickness", "highlightThickness", "HighlightThickness", DEF_LISTBOX_HIGHLIGHT_WIDTH, -1, Tk_Offset(Listbox, highlightWidth), 0, 0, 0}, + {TK_OPTION_JUSTIFY, "-justify", "justify", "Justify", + DEF_LISTBOX_JUSTIFY, -1, Tk_Offset(Listbox, justify), 0, 0, 0}, {TK_OPTION_RELIEF, "-relief", "relief", "Relief", DEF_LISTBOX_RELIEF, -1, Tk_Offset(Listbox, relief), 0, 0, 0}, {TK_OPTION_BORDER, "-selectbackground", "selectBackground", "Foreground", DEF_LISTBOX_SELECT_COLOR, -1, Tk_Offset(Listbox, selBorder), 0, DEF_LISTBOX_SELECT_MONO, 0}, @@ -311,11 +315,11 @@ {TK_OPTION_END, NULL, NULL, NULL, NULL, 0, -1, 0, 0, 0} }; /* * The itemAttrOptionSpecs table defines the valid configuration options for - * listbox items + * listbox items. */ static const Tk_OptionSpec itemAttrOptionSpecs[] = { {TK_OPTION_BORDER, "-background", "background", "Background", NULL, -1, Tk_Offset(ItemAttr, border), @@ -338,11 +342,11 @@ DEF_LISTBOX_SELECT_FG_MONO, 0}, {TK_OPTION_END, NULL, NULL, NULL, NULL, 0, -1, 0, 0, 0} }; /* - * The following tables define the listbox widget commands (and sub- commands) + * The following tables define the listbox widget commands (and sub-commands) * and map the indexes into the string tables into enumerated types used to * dispatch the listbox widget command. */ static const char *const commandNames[] = { @@ -434,10 +438,11 @@ static char * ListboxListVarProc(ClientData clientData, Tcl_Interp *interp, const char *name1, const char *name2, int flags); static void MigrateHashEntries(Tcl_HashTable *table, int first, int last, int offset); +static int GetMaxOffset(Listbox *listPtr); /* * The structure below defines button class behavior by means of procedures * that can be invoked from generic window code. */ @@ -544,10 +549,11 @@ listPtr->xScrollUnit = 1; listPtr->exportSelection = 1; listPtr->cursor = None; listPtr->state = STATE_NORMAL; listPtr->gray = None; + listPtr->justify = TK_JUSTIFY_LEFT; /* * Keep a hold of the associated tkwin until we destroy the listbox, * otherwise Tk might free it while we still need it. */ @@ -611,11 +617,11 @@ return TCL_ERROR; } /* * Parse the command by looking up the second argument in the list of - * valid subcommand names + * valid subcommand names. */ result = Tcl_GetIndexFromObj(interp, objv[1], commandNames, "option", 0, &cmdIndex); if (result != TCL_OK) { @@ -1074,10 +1080,11 @@ ListboxBboxSubCmd( Tcl_Interp *interp, /* Pointer to the calling Tcl interpreter */ Listbox *listPtr, /* Information about the listbox */ int index) /* Index of the element to get bbox info on */ { + register Tk_Window tkwin = listPtr->tkwin; int lastVisibleIndex; /* * Determine the index of the last visible item in the listbox. */ @@ -1109,11 +1116,19 @@ stringRep = Tcl_GetStringFromObj(el, &stringLen); Tk_GetFontMetrics(listPtr->tkfont, &fm); pixelWidth = Tk_TextWidth(listPtr->tkfont, stringRep, stringLen); - x = listPtr->inset + listPtr->selBorderWidth - listPtr->xOffset; + if (listPtr->justify == TK_JUSTIFY_LEFT) { + x = (listPtr->inset + listPtr->selBorderWidth) - listPtr->xOffset; + } else if (listPtr->justify == TK_JUSTIFY_RIGHT) { + x = Tk_Width(tkwin) - (listPtr->inset + listPtr->selBorderWidth) + - pixelWidth - listPtr->xOffset + GetMaxOffset(listPtr); + } else { + x = (Tk_Width(tkwin) - pixelWidth)/2 + - listPtr->xOffset + GetMaxOffset(listPtr)/2; + } y = ((index - listPtr->topIndex)*listPtr->lineHeight) + listPtr->inset + listPtr->selBorderWidth; results[0] = Tcl_NewIntObj(x); results[1] = Tcl_NewIntObj(y); results[2] = Tcl_NewIntObj(pixelWidth); @@ -1835,10 +1850,11 @@ unsigned long mask; int left, right; /* Non-zero values here indicate that the left * or right edge of the listbox is * off-screen. */ Pixmap pixmap; + int textWidth; listPtr->flags &= ~REDRAW_PENDING; if (listPtr->flags & LISTBOX_DELETED) { return; } @@ -2014,11 +2030,11 @@ } prevSelected = 1; } else { /* * If there is an item attributes record for this item, draw - * the background box and set the foreground color accordingly + * the background box and set the foreground color accordingly. */ if (entry != NULL) { attrs = Tcl_GetHashValue(entry); gcValues.foreground = listPtr->fgColorPtr->pixel; @@ -2054,16 +2070,28 @@ /* * Draw the actual text of this item. */ + Tcl_ListObjIndex(listPtr->interp, listPtr->listObj, i, &curElement); + stringRep = Tcl_GetStringFromObj(curElement, &stringLen); + textWidth = Tk_TextWidth(listPtr->tkfont, stringRep, stringLen); + Tk_GetFontMetrics(listPtr->tkfont, &fm); y += fm.ascent + listPtr->selBorderWidth; - x = listPtr->inset + listPtr->selBorderWidth - listPtr->xOffset; - Tcl_ListObjIndex(listPtr->interp, listPtr->listObj, i, &curElement); - stringRep = Tcl_GetStringFromObj(curElement, &stringLen); - Tk_DrawChars(listPtr->display, pixmap, gc, listPtr->tkfont, + + if (listPtr->justify == TK_JUSTIFY_LEFT) { + x = (listPtr->inset + listPtr->selBorderWidth) - listPtr->xOffset; + } else if (listPtr->justify == TK_JUSTIFY_RIGHT) { + x = Tk_Width(tkwin) - (listPtr->inset + listPtr->selBorderWidth) + - textWidth - listPtr->xOffset + GetMaxOffset(listPtr); + } else { + x = (Tk_Width(tkwin) - textWidth)/2 + - listPtr->xOffset + GetMaxOffset(listPtr)/2; + } + + Tk_DrawChars(listPtr->display, pixmap, gc, listPtr->tkfont, stringRep, stringLen, x, y); /* * If this is the active element, apply the activestyle to it. */ @@ -2456,11 +2484,11 @@ } /* * Check width of the element. We only have to check if widthChanged * has not already been set to 1, because we only need one maxWidth - * element to disappear for us to have to recompute the width + * element to disappear for us to have to recompute the width. */ if (widthChanged == 0) { Tcl_ListObjIndex(listPtr->interp, listPtr->listObj, i, &element); stringRep = Tcl_GetStringFromObj(element, &length); @@ -2733,11 +2761,15 @@ * The index didn't match any of the named indices; maybe it's an @x,y */ stringRep = Tcl_GetString(indexObj); if (stringRep[0] == '@') { - /* @x,y index */ + + /* + * @x,y index + */ + int y; const char *start; char *end; start = stringRep + 1; @@ -2842,13 +2874,11 @@ * Add half a scroll unit to do entry/text-like synchronization. [Bug * #225025] */ offset += listPtr->xScrollUnit / 2; - maxOffset = listPtr->maxWidth - (Tk_Width(listPtr->tkwin) - - 2*listPtr->inset - 2*listPtr->selBorderWidth) - + listPtr->xScrollUnit - 1; + maxOffset = GetMaxOffset(listPtr); if (offset > maxOffset) { offset = maxOffset; } if (offset < 0) { offset = 0; @@ -2885,13 +2915,11 @@ int y) /* Y-coordinate to use for scan operation. */ { int newTopIndex, newOffset, maxIndex, maxOffset; maxIndex = listPtr->nElements - listPtr->fullLines; - maxOffset = listPtr->maxWidth + (listPtr->xScrollUnit - 1) - - (Tk_Width(listPtr->tkwin) - 2*listPtr->inset - - 2*listPtr->selBorderWidth - listPtr->xScrollUnit); + maxOffset = GetMaxOffset(listPtr); /* * Compute new top line for screen by amplifying the difference between * the current position and the place where the scan started (the "mark" * position). If we run off the top or bottom of the list, then reset the @@ -3463,11 +3491,11 @@ Tcl_DecrRefCount(oldListObj); } /* * If the list length has decreased, then we should clean up selection and - * attributes information for elements past the end of the new list + * attributes information for elements past the end of the new list. */ oldLength = listPtr->nElements; Tcl_ListObjLength(listPtr->interp, listPtr->listObj, &listPtr->nElements); if (listPtr->nElements < oldLength) { @@ -3580,12 +3608,48 @@ } } return; } +/* + *---------------------------------------------------------------------- + * + * GetMaxOffset -- + * + * Passing in a listbox pointer, returns the maximum offset for the box, + * i.e. the maximum possible horizontal scrolling value (in pixels). + * + * Results: + * Listbox's maxOffset. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------- +*/ +static int GetMaxOffset( + register Listbox *listPtr) +{ + int maxOffset; + + maxOffset = listPtr->maxWidth - + (Tk_Width(listPtr->tkwin) - 2*listPtr->inset - + 2*listPtr->selBorderWidth) + listPtr->xScrollUnit - 1; + if (maxOffset < 0) { + + /* + * Listbox is larger in width than its largest width item. + */ + + maxOffset = 0; + } + maxOffset -= maxOffset % listPtr->xScrollUnit; + + return maxOffset; +} /* * Local Variables: * mode: c * c-basic-offset: 4 * fill-column: 78 * End: */ Index: library/demos/states.tcl ================================================================== --- library/demos/states.tcl +++ library/demos/states.tcl @@ -16,10 +16,21 @@ wm iconname $w "states" positionWindow $w label $w.msg -font $font -wraplength 4i -justify left -text "A listbox containing the 50 states is displayed below, along with a scrollbar. You can scan the list either using the scrollbar or by scanning. To scan, press button 2 in the widget and drag up or down." pack $w.msg -side top + +labelframe $w.justif -text Justification +foreach c {Left Center Right} { + set lower [string tolower $c] + radiobutton $w.justif.$lower -text $c -variable just \ + -relief flat -value $lower -anchor w \ + -command "$w.frame.list configure -justify \$just" \ + -tristatevalue "multi" + pack $w.justif.$lower -side left -pady 2 -fill x +} +pack $w.justif ## See Code / Dismiss buttons set btns [addSeeDismiss $w.buttons $w] pack $btns -side bottom -fill x Index: macosx/tkMacOSXDefault.h ================================================================== --- macosx/tkMacOSXDefault.h +++ macosx/tkMacOSXDefault.h @@ -257,10 +257,11 @@ #define DEF_LISTBOX_FG BLACK #define DEF_LISTBOX_HEIGHT "10" #define DEF_LISTBOX_HIGHLIGHT_BG NORMAL_BG #define DEF_LISTBOX_HIGHLIGHT BLACK #define DEF_LISTBOX_HIGHLIGHT_WIDTH "0" +#define DEF_LISTBOX_JUSTIFY "left" #define DEF_LISTBOX_RELIEF "solid" #define DEF_LISTBOX_SCROLL_COMMAND "" #define DEF_LISTBOX_LIST_VARIABLE "" #define DEF_LISTBOX_SELECT_COLOR SELECT_BG #define DEF_LISTBOX_SELECT_MONO BLACK Index: tests/listbox.test ================================================================== --- tests/listbox.test +++ tests/listbox.test @@ -202,10 +202,25 @@ .l configure -highlightthickness -2 list [lindex [.l configure -highlightthickness] 4] [.l cget -highlightthickness] } -cleanup { .l configure -highlightthickness [lindex [.l configure -highlightthickness] 3] } -result {0 0} +test listbox-1.32.1 {configuration options} -setup { + set res {} +} -body { + .l configure -justify left + set res [list [lindex [.l configure -justify] 4] [.l cget -justify]] + .l configure -justify center + lappend res [lindex [.l configure -justify] 4] [.l cget -justify] + .l configure -justify right + lappend res [lindex [.l configure -justify] 4] [.l cget -justify] +} -cleanup { + .l configure -justify [lindex [.l configure -justify] 3] +} -result {left left center center right right} +test listbox-1.32.2 {configuration options} -body { + .l configure -justify bogus +} -returnCodes error -result {bad justification "bogus": must be left, right, or center} test listbox-1.33 {configuration options} -body { .l configure -relief groove list [lindex [.l configure -relief] 4] [.l cget -relief] } -cleanup { .l configure -relief [lindex [.l configure -relief] 3] @@ -440,10 +455,62 @@ fonts } -body { mkPartial list [.partial.l bbox 3] [.partial.l bbox 4] } -result {{5 56 24 14} {5 73 23 14}} +test listbox-3.18a {ListboxWidgetCmd procedure, "bbox" option, justified} -constraints { + fonts +} -setup { + destroy .top.l .top + unset -nocomplain res +} -body { + toplevel .top + listbox .top.l -justify left + .top.l insert end Item1 LongerItem2 MuchLongerItem3 + pack .top.l + update + lappend res [.top.l bbox 0] [.top.l bbox 1] [.top.l bbox 2] + .top.l configure -justify center + lappend res [.top.l bbox 0] [.top.l bbox 1] [.top.l bbox 2] + .top.l configure -justify right + lappend res [.top.l bbox 0] [.top.l bbox 1] [.top.l bbox 2] +} -cleanup { + destroy .top.l .top + unset -nocomplain res +} -result [list \ + {5 5 34 14} {5 22 74 14} {5 39 106 14} \ + {58 5 34 14} {38 22 74 14} {22 39 106 14} \ + {111 5 34 14} {71 22 74 14} {39 39 106 14} \ +] +test listbox-3.18b {ListboxWidgetCmd procedure, "bbox" option, justified, non-default borderwidth} -setup { + destroy .top.l .top + unset -nocomplain lres res +} -body { + # This test checks whether all "x" values from bbox for different size + # items with different justification settings are all positive or zero + # This checks a bit the calculation of this x value with non-default + # borders widths of the listbox + toplevel .top + listbox .top.l -justify left -borderwidth 17 -highlightthickness 19 -selectborderwidth 22 + .top.l insert end Item1 LongerItem2 MuchLongerItem3 + .top.l selection set 1 + pack .top.l + update + lappend lres [.top.l bbox 0] [.top.l bbox 1] [.top.l bbox 2] + .top.l configure -justify center + lappend lres [.top.l bbox 0] [.top.l bbox 1] [.top.l bbox 2] + .top.l configure -justify right + lappend lres [.top.l bbox 0] [.top.l bbox 1] [.top.l bbox 2] + set res 1 + for {set i 0} {$i < [llength $lres]} {incr i 4} { + set res [expr {$res * [expr {[lindex $lres $i] >= 0}] }] + } + set res +} -cleanup { + destroy .top.l .top + unset -nocomplain lres res +} -result {1} test listbox-3.19 {ListboxWidgetCmd procedure, "cget" option} -body { .l cget } -returnCodes error -result {wrong # args: should be ".l cget option"} test listbox-3.20 {ListboxWidgetCmd procedure, "cget" option} -body { .l cget a b @@ -454,11 +521,11 @@ test listbox-3.22 {ListboxWidgetCmd procedure, "cget" option} -body { .l cget -setgrid } -result {0} test listbox-3.23 {ListboxWidgetCmd procedure, "configure" option} -body { llength [.l configure] -} -result {27} +} -result {28} test listbox-3.24 {ListboxWidgetCmd procedure, "configure" option} -body { .l configure -gorp } -returnCodes error -result {unknown option "-gorp"} test listbox-3.25 {ListboxWidgetCmd procedure, "configure" option} -body { .l configure -setgrid Index: unix/tkUnixDefault.h ================================================================== --- unix/tkUnixDefault.h +++ unix/tkUnixDefault.h @@ -219,10 +219,11 @@ #define DEF_LISTBOX_FG BLACK #define DEF_LISTBOX_HEIGHT "10" #define DEF_LISTBOX_HIGHLIGHT_BG NORMAL_BG #define DEF_LISTBOX_HIGHLIGHT BLACK #define DEF_LISTBOX_HIGHLIGHT_WIDTH "1" +#define DEF_LISTBOX_JUSTIFY "left" #define DEF_LISTBOX_RELIEF "sunken" #define DEF_LISTBOX_SCROLL_COMMAND "" #define DEF_LISTBOX_LIST_VARIABLE "" #define DEF_LISTBOX_SELECT_COLOR SELECT_BG #define DEF_LISTBOX_SELECT_MONO BLACK Index: win/tkWinDefault.h ================================================================== --- win/tkWinDefault.h +++ win/tkWinDefault.h @@ -222,10 +222,11 @@ #define DEF_LISTBOX_FG NORMAL_FG #define DEF_LISTBOX_HEIGHT "10" #define DEF_LISTBOX_HIGHLIGHT_BG NORMAL_BG #define DEF_LISTBOX_HIGHLIGHT HIGHLIGHT #define DEF_LISTBOX_HIGHLIGHT_WIDTH "1" +#define DEF_LISTBOX_JUSTIFY "left" #define DEF_LISTBOX_RELIEF "sunken" #define DEF_LISTBOX_SCROLL_COMMAND "" #define DEF_LISTBOX_LIST_VARIABLE "" #define DEF_LISTBOX_SELECT_COLOR SELECT_BG #define DEF_LISTBOX_SELECT_MONO BLACK