Tk Source Code

Artifact [ed0867e6]
Login

Artifact ed0867e694342bb854228ae3d3a81827a20e39f5:

Attachment "min_max.patch4" to ticket [568861ff] added by mdejong 2003-02-20 01:12:03.
2003-02-19  Mo DeJong  <[email protected]>

	* doc/wm.n (minimize, maximize): Remove claim
	that the resizable command keeps scripts from
	changing the size of windows since it is
	not true. The resizable command only applies
	to user sizing via user interaction.
	* tests/unixWm.test: Replace broken tests with
	the nonPortable constraint with new tests for
	maxsize and minsize options. These tests
	verify that setting the minsize and maxsize
	will resize the window if needed, and that
	the wm hints will be updated with the new sizes.
	* tests/wm.test: Add an exhaustive set of tests
	for the wm maxsize and wm minsize commands.
	These tests verify that setting the minsize
	and maxsize will resize the window if needed.
	These tests have only been run under Win98
	and Window Maker, so further tweaking may
	be needed for other systems.
	* unix/tkUnixWm.c (UpdateGeometryInfo, UpdateSizeHints):
	Fixup comments and initialization for the minWidth,
	minHeight, maxWidth, maxHeight, width, and height
 	members of the WmInfo struct. Check to ensure
	that a new toplevel window size is not larger
	than the maxsize or smaller than the minsize
	when updating the geometry at idle time.
	Pass new width and height values to the
	UpdateSizeHints method so that it can properly
	set the window min and max sizes for a window
	that cannot be resized by the user. This fixes
	a bug where the window resizes back to the original
	size when the user clicks on the window border.
	* win/tkWinWm.c (UpdateGeometryInfo):
	Fixup comments and initialization for the minWidth,
	minHeight, maxWidth, maxHeight, width, and height
 	members of the WmInfo struct. Check to ensure
	that a new toplevel window size is not larger
	than the maxsize or smaller than the minsize
	when updating the geometry at idle time.

Index: doc/wm.n
===================================================================
RCS file: /cvsroot/tktoolkit/tk/doc/wm.n,v
retrieving revision 1.11
diff -u -r1.11 wm.n
--- doc/wm.n	16 Jul 2002 18:06:41 -0000	1.11
+++ doc/wm.n	19 Feb 2003 01:15:12 -0000
@@ -312,8 +312,6 @@
 it returns a Tcl list with two elements, which are the
 maximum width and height currently in effect.
 The maximum size defaults to the size of the screen.
-If resizing has been disabled with the \fBwm resizable\fR command,
-then this command has no effect.
 See the sections on geometry management below for more information.
 .TP
 \fBwm minsize \fIwindow\fR ?\fIwidth height\fR?
@@ -328,8 +326,6 @@
 it returns a Tcl list with two elements, which are the
 minimum width and height currently in effect.
 The minimum size defaults to one pixel in each dimension.
-If resizing has been disabled with the \fBwm resizable\fR command,
-then this command has no effect.
 See the sections on geometry management below for more information.
 .TP
 \fBwm overrideredirect \fIwindow\fR ?\fIboolean\fR?
Index: tests/unixWm.test
===================================================================
RCS file: /cvsroot/tktoolkit/tk/tests/unixWm.test,v
retrieving revision 1.29
diff -u -r1.29 unixWm.test
--- tests/unixWm.test	7 Nov 2002 22:27:04 -0000	1.29
+++ tests/unixWm.test	19 Feb 2003 01:15:13 -0000
@@ -988,35 +988,83 @@
     set result
 } {normal 1 icon 0}
 
-test unixWm-28.1 {Tk_WmCmd procedure, "maxsize" option} {nonPortable} {
-    wm maxsize .t
-}  {1137 870}
-
-test unixWm-28.2 {Tk_WmCmd procedure, "maxsize" option} {nonPortable} {
-    # Not portable, because some window managers let applications override
-    # minsize and maxsize.
-
-    wm maxsize .t 200 150
-    wm geom .t 300x200
-    update
-    list [winfo width .t] [winfo height .t]
-} {200 150}
-
 catch {destroy .t}
 catch {destroy .icon}
 toplevel .t -width 100 -height 50
 wm geom .t +0+0
 update
 
-test unixWm-29.1 {Tk_WmCmd procedure, "minsize" option} {nonPortable} {
-    # Not portable, because some window managers let applications override
-    # minsize and maxsize.
+test unixWm-28.1 {Tk_WmCmd procedure, "maxsize" option, setting the
+        maxsize should update WM_NORMAL_HINTS} {
+    destroy .t
+    toplevel .t
+    wm maxsize .t 300 300
+    update
+    set hints [testprop [testwrapper .t] WM_NORMAL_HINTS]
+    format {%d %d} [lindex $hints 7] [lindex $hints 8]
+} {300 300}
+
+test unixWm-28.2 {Tk_WmCmd procedure, "maxsize" option, setting the
+        maxsize to a value smaller than the current size should
+        set the maxsize in WM_NORMAL_HINTS} {
+    destroy .t
+    toplevel .t
+    wm geom .t 400x400
+    wm maxsize .t 300 300
+    update
+    set hints [testprop [testwrapper .t] WM_NORMAL_HINTS]
+    format {%d %d} [lindex $hints 7] [lindex $hints 8]
+} {300 300}
+
+test unixWm-28.3 {Tk_WmCmd procedure, "maxsize" option, setting the
+        maxsize to a value smaller than the current size should
+        set the maxsize in WM_NORMAL_HINTS even if the
+        interactive resizable flag is set to 0} {
+    destroy .t
+    toplevel .t
+    wm geom .t 400x400
+    wm resizable .t 0 0
+    wm maxsize .t 300 300
+    update
+    set hints [testprop [testwrapper .t] WM_NORMAL_HINTS]
+    format {%d %d} [lindex $hints 7] [lindex $hints 8]
+} {300 300}
+
+test unixWm-29.1 {Tk_WmCmd procedure, "minsize" option, setting the
+        minsize should update WM_NORMAL_HINTS} {
+    destroy .t
+    toplevel .t
+    wm minsize .t 300 300
+    update
+    set hints [testprop [testwrapper .t] WM_NORMAL_HINTS]
+    format {%d %d} [lindex $hints 5] [lindex $hints 6]
+} {300 300}
+
+test unixWm-29.2 {Tk_WmCmd procedure, "minsize" option, setting the
+        minsize to a value larger than the current size should
+        set the maxsize in WM_NORMAL_HINTS} {
+    destroy .t
+    toplevel .t
+    wm geom .t 200x200
+    wm minsize .t 300 300
+    update
+    set hints [testprop [testwrapper .t] WM_NORMAL_HINTS]
+    format {%d %d} [lindex $hints 5] [lindex $hints 6]
+} {300 300}
 
-    wm minsize .t 150 100
-    wm geom .t 50x50
+test unixWm-29.3 {Tk_WmCmd procedure, "minsize" option, setting the
+        minsize to a value larger than the current size should
+        set the minsize in WM_NORMAL_HINTS even if the
+        interactive resizable flag is set to 0} {
+    destroy .t
+    toplevel .t
+    wm geom .t 200x200
+    wm resizable .t 0 0
+    wm minsize .t 300 300
     update
-    list [winfo width .t] [winfo height .t]
-} {150 100}
+    set hints [testprop [testwrapper .t] WM_NORMAL_HINTS]
+    format {%d %d} [lindex $hints 5] [lindex $hints 6]
+} {300 300}
 
 catch {destroy .t}
 catch {destroy .icon}
Index: tests/wm.test
===================================================================
RCS file: /cvsroot/tktoolkit/tk/tests/wm.test,v
retrieving revision 1.21
diff -u -r1.21 wm.test
--- tests/wm.test	28 Jan 2003 20:39:19 -0000	1.21
+++ tests/wm.test	19 Feb 2003 01:15:14 -0000
@@ -663,12 +663,84 @@
 test wm-maxsize-1.6 {usage} {
     catch {destroy .t2}
     toplevel .t2
-    wm maxsize .t2 200 150
+    wm maxsize .t2 300 200
     set result [wm maxsize .t2]
     destroy .t2
     set result
+} {300 200}
+
+test wm-maxsize-1.7 {maxsize must be <= screen size} {
+    destroy .t
+    toplevel .t
+    foreach {t_width t_height} [wm maxsize .t] break
+    set s_width [winfo screenwidth .t]
+    set s_height [winfo screenheight .t]
+    expr {($t_width <= $s_width) && ($t_height <= $s_height)}
+} 1
+
+test wm-maxsize-2.1 {setting the maxsize to a value smaller
+        than the current size will resize a toplevel} {
+    destroy .t
+    toplevel .t -width 300 -height 300
+    update
+    wm maxsize .t 200 150
+    # UpdateGeometryInfo invoked at idle
+    update
+    lrange [split [wm geom .t] x+] 0 1
 } {200 150}
 
+test wm-maxsize-2.2 {setting the maxsize to a value smaller
+        than the current size will resize a gridded toplevel} {
+    destroy .t
+    toplevel .t
+    wm grid .t 0 0 50 50
+    wm geometry .t 6x6
+    update
+    wm maxsize .t 4 3
+    # UpdateGeometryInfo invoked at idle
+    update
+    lrange [split [wm geom .t] x+] 0 1
+} {4 3}
+
+test wm-maxsize-2.3 {attempting to resize to a value
+        bigger than the current maxsize will
+        set it to the max size} {
+    destroy .t
+    toplevel .t -width 200 -height 200
+    wm maxsize .t 300 250
+    update
+    wm geom .t 400x300
+    update
+    lrange [split [wm geom .t] x+] 0 1
+} {300 250}
+
+test wm-maxsize-2.4 {attempting to resize to a value
+        bigger than the current maxsize will
+        set it to the max size when gridded} {
+    destroy .t
+    toplevel .t
+    wm grid .t 1 1 50 50
+    wm geom .t 4x4
+    wm maxsize .t 6 5
+    update
+    wm geom .t 8x6
+    update
+    lrange [split [wm geom .t] x+] 0 1
+} {6 5}
+
+test wm-maxsize-2.5 {Use max size if window size is not
+        explicitly set and the reqWidth/reqHeight are
+        bigger than the max size} {
+    destroy .t
+    toplevel .t
+    pack [frame .t.f -width 400 -height 400]
+    update idletasks
+    set req [list [winfo reqwidth .t] \
+                      [winfo reqheight .t]]
+    wm maxsize .t 300 300
+    update
+    list $req [lrange [split [wm geom .t] x+] 0 1]
+} {{400 400} {300 300}}
 
 test wm-minsize-1.1 {usage} {
     list [catch {wm minsize} msg]  $msg
@@ -693,12 +765,75 @@
 test wm-minsize-1.6 {usage} {
     catch {destroy .t2}
     toplevel .t2
-    wm minsize .t2 200 150
+    wm minsize .t2 300 200
     set result [wm minsize .t2]
     destroy .t2
     set result
-} {200 150}
+} {300 200}
 
+test wm-minsize-2.1 {setting the minsize to a value larger
+        than the current size will resize a toplevel} {
+    destroy .t
+    toplevel .t -width 200 -height 200
+    update
+    wm minsize .t 400 300
+    # UpdateGeometryInfo invoked at idle
+    update
+    lrange [split [wm geom .t] x+] 0 1
+} {400 300}
+
+test wm-minsize-2.2 {setting the minsize to a value larger
+        than the current size will resize a gridded toplevel} {
+    destroy .t
+    toplevel .t
+    wm grid .t 1 1 50 50
+    wm geom .t 4x4
+    update
+    wm minsize .t 8 8
+    # UpdateGeometryInfo invoked at idle
+    update
+    lrange [split [wm geom .t] x+] 0 1
+} {8 8}
+
+test wm-minsize-2.3 {attempting to resize to a value
+        smaller than the current minsize will set
+        it to the minsize} {
+    destroy .t
+    toplevel .t -width 400 -height 400
+    wm minsize .t 300 300
+    update
+    wm geom .t 200x200
+    update
+    lrange [split [wm geom .t] x+] 0 1
+} {300 300}
+
+test wm-minsize-2.4 {attempting to resize to a value
+        smaller than the current minsize will set
+        it to the minsize when gridded} {
+    destroy .t
+    toplevel .t
+    wm grid .t 1 1 50 50
+    wm geom .t 8x8
+    wm minsize .t 6 6
+    update
+    wm geom .t 4x4
+    update
+    lrange [split [wm geom .t] x+] 0 1
+} {6 6}
+
+test wm-minsize-2.5 {Use min size if window size is not
+        explicitly set and the reqWidth/reqHeight are
+        smaller than the min size} {
+    destroy .t
+    toplevel .t
+    pack [frame .t.f -width 250 -height 250]
+    update idletasks
+    set req [list [winfo reqwidth .t] \
+                      [winfo reqheight .t]]
+    wm minsize .t 300 300
+    update
+    list $req [lrange [split [wm geom .t] x+] 0 1]
+} {{250 250} {300 300}}
 
 test wm-overrideredirect-1.1 {usage} {
     list [catch {wm overrideredirect} msg]  $msg
Index: unix/tkUnixWm.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/unix/tkUnixWm.c,v
retrieving revision 1.36
diff -u -r1.36 tkUnixWm.c
--- unix/tkUnixWm.c	27 Dec 2002 21:23:04 -0000	1.36
+++ unix/tkUnixWm.c	19 Feb 2003 01:15:17 -0000
@@ -108,9 +108,9 @@
 				 * window is gridded;  otherwise it isn't
 				 * gridded. */
     int minWidth, minHeight;	/* Minimum dimensions of window, in
-				 * grid units, not pixels. */
+				 * pixels or grid units. */
     int maxWidth, maxHeight;	/* Maximum dimensions of window, in
-				 * grid units, not pixels. */
+				 * pixels or grid units. 0 to default.*/
     Tk_Window gridWin;		/* Identifies the window that controls
 				 * gridding for this top-level, or NULL if
 				 * the top-level isn't currently gridded. */
@@ -131,7 +131,7 @@
      */
 
     int width, height;		/* Desired dimensions of window, specified
-				 * in grid units.  These values are
+				 * in pixels or grid units.  These values are
 				 * set by the "wm geometry" command and by
 				 * ConfigureNotify events (for when wm
 				 * resizes window).  -1 means user hasn't
@@ -332,7 +332,8 @@
 static void		UpdateGeometryInfo _ANSI_ARGS_((
 			    ClientData clientData));
 static void		UpdateHints _ANSI_ARGS_((TkWindow *winPtr));
-static void		UpdateSizeHints _ANSI_ARGS_((TkWindow *winPtr));
+static void		UpdateSizeHints _ANSI_ARGS_((TkWindow *winPtr,
+			    int newWidth, int newHeight));
 static void		UpdateVRootGeometry _ANSI_ARGS_((WmInfo *wmPtr));
 static void		UpdateWmProtocols _ANSI_ARGS_((WmInfo *wmPtr));
 static void		WaitForConfigureNotify _ANSI_ARGS_((TkWindow *winPtr,
@@ -537,13 +538,15 @@
     wmPtr->hints.icon_x = wmPtr->hints.icon_y = 0;
     wmPtr->hints.icon_mask = None;
     wmPtr->hints.window_group = None;
-    wmPtr->minWidth = wmPtr->minHeight = 1;
 
     /*
      * Default the maximum dimensions to the size of the display, minus
      * a guess about how space is needed for window manager decorations.
      */
 
+    wmPtr->gridWin = NULL;
+    wmPtr->minWidth = wmPtr->minHeight = 1;
+    wmPtr->maxWidth = wmPtr->maxHeight = 0;
     wmPtr->widthInc = wmPtr->heightInc = 1;
     wmPtr->minAspect.x = wmPtr->minAspect.y = 1;
     wmPtr->maxAspect.x = wmPtr->maxAspect.y = 1;
@@ -3955,7 +3958,7 @@
 {
     register TkWindow *winPtr = (TkWindow *) clientData;
     register WmInfo *wmPtr = winPtr->wmInfoPtr;
-    int x, y, width, height;
+    int x, y, width, height, min, max;
     unsigned long serial;
 
     wmPtr->flags &= ~WM_UPDATE_PENDING;
@@ -3966,9 +3969,10 @@
      * requested depends on (a) the size requested internally
      * by the window's widgets, (b) the size requested by the
      * user in a "wm geometry" command or via wm-based interactive
-     * resizing (if any), and (c) whether or not the window is
-     * gridded.  Don't permit sizes <= 0 because this upsets
-     * the X server.
+     * resizing (if any), (c) whether or not the window is
+     * gridded, and (d) the current min or max size for
+     * the toplevel. Don't permit sizes <= 0 because this
+     * upsets the X server.
      */
 
     if (wmPtr->width == -1) {
@@ -3982,6 +3986,28 @@
     if (width <= 0) {
 	width = 1;
     }
+    /*
+     * Account for window max/min width
+     */
+    if (wmPtr->gridWin != NULL) {
+	min = winPtr->reqWidth
+		+ (wmPtr->minWidth - wmPtr->reqGridWidth)*wmPtr->widthInc;
+	if (wmPtr->maxWidth > 0) {
+	    max = winPtr->reqWidth
+		+ (wmPtr->maxWidth - wmPtr->reqGridWidth)*wmPtr->widthInc;
+	} else {
+	    max = 0;
+	}
+    } else {
+	min = wmPtr->minWidth;
+	max = wmPtr->maxWidth;
+    }
+    if (width < min) {
+	width = min;
+    } else if ((max > 0) && (width > max)) {
+	width = max;
+    }
+
     if (wmPtr->height == -1) {
 	height = winPtr->reqHeight;
     } else if (wmPtr->gridWin != NULL) {
@@ -3993,6 +4019,27 @@
     if (height <= 0) {
 	height = 1;
     }
+    /*
+     * Account for window max/min height
+     */
+    if (wmPtr->gridWin != NULL) {
+	min = winPtr->reqHeight
+		+ (wmPtr->minHeight - wmPtr->reqGridHeight)*wmPtr->heightInc;
+	if (wmPtr->maxHeight > 0) {
+	    max = winPtr->reqHeight
+		+ (wmPtr->maxHeight - wmPtr->reqGridHeight)*wmPtr->heightInc;
+	} else {
+	    max = 0;
+	}
+    } else {
+	min = wmPtr->minHeight;
+	max = wmPtr->maxHeight;
+    }
+    if (height < min) {
+	height = min;
+    } else if ((max > 0) && (height > max)) {
+	height = max;
+    }
 
     /*
      * Compute the new position for the upper-left pixel of the window's
@@ -4029,7 +4076,7 @@
 	wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
     }
     if (wmPtr->flags & WM_UPDATE_SIZE_HINTS) {
-	UpdateSizeHints(winPtr);
+	UpdateSizeHints(winPtr, width, height);
     }
 
     /*
@@ -4163,8 +4210,10 @@
  */
 
 static void
-UpdateSizeHints(winPtr)
+UpdateSizeHints(winPtr, newWidth, newHeight)
     TkWindow *winPtr;
+    int newWidth;
+    int newHeight;
 {
     register WmInfo *wmPtr = winPtr->wmInfoPtr;
     XSizeHints *hintsPtr;
@@ -4226,20 +4275,11 @@
      */
 
     if (wmPtr->flags & WM_WIDTH_NOT_RESIZABLE) {
-	if (wmPtr->width >= 0) {
-	    hintsPtr->min_width = wmPtr->width;
-	} else {
-	    hintsPtr->min_width = winPtr->reqWidth;
-	}
-	hintsPtr->max_width = hintsPtr->min_width;
+	hintsPtr->max_width = hintsPtr->min_width = newWidth;
     }
     if (wmPtr->flags & WM_HEIGHT_NOT_RESIZABLE) {
-	if (wmPtr->height >= 0) {
-	    hintsPtr->min_height = wmPtr->height;
-	} else {
-	    hintsPtr->min_height = winPtr->reqHeight + wmPtr->menuHeight;
-	}
-	hintsPtr->max_height = hintsPtr->min_height;
+	hintsPtr->max_height = hintsPtr->min_height =
+	    newHeight + wmPtr->menuHeight;
     }
 
     XSetWMNormalHints(winPtr->display, wmPtr->wrapperPtr->window, hintsPtr);
Index: win/tkWinWm.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/win/tkWinWm.c,v
retrieving revision 1.54
diff -u -r1.54 tkWinWm.c
--- win/tkWinWm.c	6 Dec 2002 23:29:37 -0000	1.54
+++ win/tkWinWm.c	19 Feb 2003 01:15:19 -0000
@@ -176,9 +176,9 @@
 				 * window is gridded;  otherwise it isn't
 				 * gridded. */
     int minWidth, minHeight;	/* Minimum dimensions of window, in
-				 * grid units, not pixels. */
+				 * pixels or grid units. */
     int maxWidth, maxHeight;	/* Maximum dimensions of window, in
-				 * grid units, not pixels, or 0 to default. */
+				 * pixels or grid units. 0 to default. */
     Tk_Window gridWin;		/* Identifies the window that controls
 				 * gridding for this top-level, or NULL if
 				 * the top-level isn't currently gridded. */
@@ -199,7 +199,7 @@
      */
 
     int width, height;		/* Desired dimensions of window, specified
-				 * in grid units.  These values are
+				 * in pixels or grid units.  These values are
 				 * set by the "wm geometry" command and by
 				 * ConfigureNotify events (for when wm
 				 * resizes window).  -1 means user hasn't
@@ -5018,6 +5018,7 @@
 {
     int x, y;			/* Position of border on desktop. */
     int width, height;		/* Size of client area. */
+    int min, max;
     RECT rect;
     register TkWindow *winPtr = (TkWindow *) clientData;
     register WmInfo *wmPtr = winPtr->wmInfoPtr;
@@ -5056,8 +5057,9 @@
      * requested depends on (a) the size requested internally
      * by the window's widgets, (b) the size requested by the
      * user in a "wm geometry" command or via wm-based interactive
-     * resizing (if any), and (c) whether or not the window is
-     * gridded.  Don't permit sizes <= 0 because this upsets
+     * resizing (if any), (c) whether or not the window is
+     * gridded, and (d) the current min or max size for
+     * the toplevel. Don't permit sizes <= 0 because this upsets
      * the X server.
      */
 
@@ -5072,6 +5074,28 @@
     if (width <= 0) {
 	width = 1;
     }
+    /*
+     * Account for window max/min width
+     */
+    if (wmPtr->gridWin != NULL) {
+	min = winPtr->reqWidth
+		+ (wmPtr->minWidth - wmPtr->reqGridWidth)*wmPtr->widthInc;
+	if (wmPtr->maxWidth > 0) {
+	    max = winPtr->reqWidth
+		+ (wmPtr->maxWidth - wmPtr->reqGridWidth)*wmPtr->widthInc;
+	} else {
+	    max = 0;
+	}
+    } else {
+	min = wmPtr->minWidth;
+	max = wmPtr->maxWidth;
+    }
+    if (width < min) {
+	width = min;
+    } else if ((max > 0) && (width > max)) {
+	width = max;
+    }
+
     if (wmPtr->height == -1) {
 	height = winPtr->reqHeight;
     } else if (wmPtr->gridWin != NULL) {
@@ -5082,6 +5106,27 @@
     }
     if (height <= 0) {
 	height = 1;
+    }
+    /*
+     * Account for window max/min height
+     */
+    if (wmPtr->gridWin != NULL) {
+	min = winPtr->reqHeight
+		+ (wmPtr->minHeight - wmPtr->reqGridHeight)*wmPtr->heightInc;
+	if (wmPtr->maxHeight > 0) {
+	    max = winPtr->reqHeight
+		+ (wmPtr->maxHeight - wmPtr->reqGridHeight)*wmPtr->heightInc;
+	} else {
+	    max = 0;
+	}
+    } else {
+	min = wmPtr->minHeight;
+	max = wmPtr->maxHeight;
+    }
+    if (height < min) {
+	height = min;
+    } else if ((max > 0) && (height > max)) {
+	height = max;
     }
 
     /*