diff options
author | fvogel <fvogelnew1@free.fr> | 2019-05-26 19:11:02 (GMT) |
---|---|---|
committer | fvogel <fvogelnew1@free.fr> | 2019-05-26 19:11:02 (GMT) |
commit | b450dcba5a91d6d3f4514306af14a2df22505ee7 (patch) | |
tree | 1ebfe9657383599acd58f593ea33450eeb569226 | |
parent | 075c8c77b5d81bbd425891f8b107b647a6f85254 (diff) | |
parent | 9e395306396549a5ef50de28a5bafad4ee00faed (diff) | |
download | tk-b450dcba5a91d6d3f4514306af14a2df22505ee7.zip tk-b450dcba5a91d6d3f4514306af14a2df22505ee7.tar.gz tk-b450dcba5a91d6d3f4514306af14a2df22505ee7.tar.bz2 |
Fix [ce470f20fd]: ttk::treeview has column dragging issues
-rw-r--r-- | doc/ttk_treeview.n | 26 | ||||
-rw-r--r-- | generic/ttk/ttkTreeview.c | 48 | ||||
-rw-r--r-- | library/ttk/treeview.tcl | 2 | ||||
-rw-r--r-- | tests/ttk/treeview.test | 88 |
4 files changed, 143 insertions, 21 deletions
diff --git a/doc/ttk_treeview.n b/doc/ttk_treeview.n index 5fd5e6d..125cc78 100644 --- a/doc/ttk_treeview.n +++ b/doc/ttk_treeview.n @@ -133,25 +133,29 @@ The column name. This is a read-only option. For example, [\fI$pathname \fBcolumn #\fIn \fB\-id\fR] returns the data column associated with display column #\fIn\fR. .TP -\fB\-anchor\fR +\fB\-anchor \fIanchor\fR Specifies how the text in this column should be aligned -with respect to the cell. One of +with respect to the cell. \fIAnchor\fR is one of \fBn\fR, \fBne\fR, \fBe\fR, \fBse\fR, \fBs\fR, \fBsw\fR, \fBw\fR, \fBnw\fR, or \fBcenter\fR. .TP -\fB\-minwidth\fR +\fB\-minwidth \fIminwidth\fR The minimum width of the column in pixels. The treeview widget will not make the column any smaller than \fB\-minwidth\fR when the widget is resized or the user drags a +column separator. Default is 20 pixels. +.TP +\fB\-stretch \fIboolean\fR +Specifies whether or not the column width should be adjusted +when the widget is resized or the user drags a column separator. +\fIBoolean\fR may have any of the forms accepted by \fBTcl_GetBoolean\fR. +By default columns are stretchable. +.TP +\fB\-width \fIwidth\fR +The width of the column in pixels. Default is 200 pixels. The specified +column width may be changed by Tk in order to honor \fB\-stretch\fR +and/or \fB\-minwidth\fR, or when the widget is resized or the user drags a column separator. -.TP -\fB\-stretch\fR -Specifies whether or not the column's width should be adjusted -when the widget is resized. -.TP -\fB\-width \fIw\fR -The width of the column in pixels. Default is something reasonable, -probably 200 or so. .PP Use \fIpathname column #0\fR to configure the tree column. .RE diff --git a/generic/ttk/ttkTreeview.c b/generic/ttk/ttkTreeview.c index daee5db..7ca7f0c 100644 --- a/generic/ttk/ttkTreeview.c +++ b/generic/ttk/ttkTreeview.c @@ -280,7 +280,7 @@ static Tk_OptionSpec ColumnOptionSpecs[] = { 0,0,0 }, {TK_OPTION_BOOLEAN, "-stretch", "stretch", "Stretch", "1", -1, Tk_Offset(TreeColumn,stretch), - 0,0,0 }, + 0,0,GEOMETRY_CHANGED }, {TK_OPTION_ANCHOR, "-anchor", "anchor", "Anchor", "w", Tk_Offset(TreeColumn,anchorObj), -1, /* <<NOTE-ANCHOR>> */ 0,0,0 }, @@ -1232,11 +1232,10 @@ static int ConfigureColumn( TtkResizeWidget(&tv->core); } RecomputeSlack(tv); + ResizeColumns(tv, TreeWidth(tv)); } TtkRedisplayWidget(&tv->core); - /* ASSERT: SLACKINVARIANT */ - Tk_FreeSavedOptions(&savedOptions); return TCL_OK; @@ -1613,13 +1612,10 @@ static void TreeviewDoLayout(void *clientData) Treeview *tv = clientData; int visibleRows; - /* ASSERT: SLACKINVARIANT */ - Ttk_PlaceLayout(tv->core.layout,tv->core.state,Ttk_WinBox(tv->core.tkwin)); tv->tree.treeArea = Ttk_ClientRegion(tv->core.layout, "treearea"); ResizeColumns(tv, tv->tree.treeArea.width); - /* ASSERT: SLACKINVARIANT */ TtkScrolled(tv->tree.xscrollHandle, tv->tree.xscroll.first, @@ -2889,9 +2885,28 @@ static int TreeviewDragCommand( TreeColumn *c = tv->tree.displayColumns[i]; int right = left + c->width; if (c == column) { - DragColumn(tv, i, newx - right); - /* ASSERT: SLACKINVARIANT */ - TtkRedisplayWidget(&tv->core); + /* The limit not to exceed at the right is given by the tree width + minus the sum of the min widths of the columns at the right of + the one being resized (and don't forget possible x scrolling!). + For stretchable columns, this min width really is the minWidth, + for non-stretchable columns, this is the column width. + */ + int newxRightLimit = tv->tree.treeArea.x - tv->tree.xscroll.first + + tv->tree.treeArea.width; + int j = i + 1; + while (j < tv->tree.nDisplayColumns) { + TreeColumn *cr = tv->tree.displayColumns[j]; + if (cr->stretch) { + newxRightLimit -= cr->minWidth; + } else { + newxRightLimit -= cr->width; + } + ++j; + } + if (newx <= newxRightLimit) { + DragColumn(tv, i, newx - right); + TtkRedisplayWidget(&tv->core); + } return TCL_OK; } left = right; @@ -2903,6 +2918,20 @@ static int TreeviewDragCommand( return TCL_ERROR; } +static int TreeviewDropCommand( + void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) +{ + Treeview *tv = recordPtr; + + if (objc != 2) { + Tcl_WrongNumArgs(interp, 1, objv, "drop"); + return TCL_ERROR; + } + ResizeColumns(tv, TreeWidth(tv)); + TtkRedisplayWidget(&tv->core); + return TCL_OK; +} + /*------------------------------------------------------------------------ * +++ Widget commands -- focus and selection */ @@ -3252,6 +3281,7 @@ static const Ttk_Ensemble TreeviewCommands[] = { { "delete", TreeviewDeleteCommand,0 }, { "detach", TreeviewDetachCommand,0 }, { "drag", TreeviewDragCommand,0 }, + { "drop", TreeviewDropCommand,0 }, { "exists", TreeviewExistsCommand,0 }, { "focus", TreeviewFocusCommand,0 }, { "heading", TreeviewHeadingCommand,0 }, diff --git a/library/ttk/treeview.tcl b/library/ttk/treeview.tcl index 1e8b055..39273ed 100644 --- a/library/ttk/treeview.tcl +++ b/library/ttk/treeview.tcl @@ -205,7 +205,7 @@ proc ttk::treeview::resize.drag {w x} { } proc ttk::treeview::resize.release {w x} { - # no-op + $w drop } ### Heading activation. diff --git a/tests/ttk/treeview.test b/tests/ttk/treeview.test index 7131032..1c5c5fb 100644 --- a/tests/ttk/treeview.test +++ b/tests/ttk/treeview.test @@ -690,4 +690,92 @@ test treeview-368fa4561e "indicators cannot be clicked on leafs" -setup { lappend res [.tv item foo -open] } -result {0 0 0} +test treeview-ce470f20fd-1 "dragging further than the right edge of the treeview is forbidden" -setup { + pack [ttk::treeview .tv] + .tv heading #0 -text "Drag my right edge -->" + update +} -body { + set res [.tv column #0 -width] + .tv drag #0 400 + lappend res [expr {[.tv column #0 -width] > $res}] +} -cleanup { + destroy .tv +} -result {200 0} + +proc nostretch {tv} { + foreach col [$tv cget -columns] { + $tv column $col -stretch 0 + } + $tv column #0 -stretch 0 + update idletasks ; # redisplay $tv +} + +test treeview-ce470f20fd-2 "changing -stretch resizes columns" -setup { + pack [ttk::treeview .tv -columns {bar colA colB colC foo}] + foreach col [.tv cget -columns] { + .tv heading $col -text $col + } + nostretch .tv + .tv column colA -width 50 ; .tv column colB -width 50 ; # slack created + update idletasks ; # redisplay treeview +} -body { + # when no column is stretchable and one of them becomes stretchable + # the stretchable column takes the slack and the widget is redisplayed + # automatically at idle time + set res [.tv column colA -width] + .tv column colA -stretch 1 + update idletasks ; # no slack anymore, widget redisplayed + lappend res [expr {[.tv column colA -width] > $res}] +} -cleanup { + destroy .tv +} -result {50 1} + +test treeview-ce470f20fd-3 "changing -stretch resizes columns" -setup { + pack [ttk::treeview .tv -columns {bar colA colB colC foo}] + foreach col [.tv cget -columns] { + .tv heading $col -text $col + } + .tv configure -displaycolumns {colB colA colC} + nostretch .tv + .tv column colA -width 50 ; .tv column colB -width 50 ; # slack created + update idletasks ; # redisplay treeview +} -body { + # only some columns are displayed (and in a different order than declared + # in -columns), a displayed column becomes stretchable --> the stretchable + # column expands + set res [.tv column colA -width] + .tv column colA -stretch 1 + update idletasks ; # no slack anymore, widget redisplayed + lappend res [expr {[.tv column colA -width] > $res}] +} -cleanup { + destroy .tv +} -result {50 1} + +test treeview-ce470f20fd-4 "changing -stretch resizes columns" -setup { + pack [ttk::treeview .tv -columns {bar colA colB colC foo}] + foreach col [.tv cget -columns] { + .tv heading $col -text $col + } + .tv configure -displaycolumns {colB colA colC} + nostretch .tv + .tv column colA -width 50 ; .tv column bar -width 60 ; # slack created + update idletasks ; # redisplay treeview +} -body { + # only some columns are displayed (and in a different order than declared + # in -columns), a non-displayed column becomes stretchable --> nothing + # happens + set origTreeWidth [winfo width .tv] + set res [list [.tv column bar -width] [.tv column colA -width]] + .tv column bar -stretch 1 + update idletasks ; # no change, widget redisplayed + lappend res [.tv column bar -width] [.tv column colA -width] + # this column becomes visible --> widget resizes + .tv configure -displaycolumns {bar colC colA colB} + update idletasks ; # no slack anymore because the widget resizes (shrinks) + lappend res [.tv column bar -width] [.tv column colA -width] \ + [expr {[winfo width .tv] < $origTreeWidth}] +} -cleanup { + destroy .tv +} -result {60 50 60 50 60 50 1} + tcltest::cleanupTests |