Reputation: 159
Why does set_size_request(800,600) called on DrawingArea widget (also tested with Gtk.Button) result in a Window of size 958 x 791 px, whereas set_default_size(800,600) called on the Window result in an appropriately sized window (as measured by taking a screenshot of just the Window and looking at it's resolution)? I would understand if there would be a little discrepancy in height, due to the window header bar, but such a difference in both width and height doesn't make sense to me. The documentation doesn't hint at anything like that. If someone could enlighten me, that'd be great!
Here's a code example written in vala:
using Gtk;
using Cairo;
public class SizeTest : Gtk.Window {
public SizeTest () {
this.title = "Size test";
this.destroy.connect (Gtk.main_quit);
//set_default_size (800, 600); // Window size -> 800 x 600 px
var drawing_area = new DrawingArea ();
drawing_area.set_size_request (800, 600); // Window size -> 958 x 791 px
add (drawing_area);
}
static int main (string[] args) {
Gtk.init (ref args);
var test = new SizeTest ();
test.show_all ();
Gtk.main ();
return 0;
}
}
@Zongren Zhang, thank you for testing it. I tried it again, splitting it in two applications, like you did. The size of both windows is quite different for me. I tried it on both my monitors - same thing.
Btw, I am using elementary OS 0.4.1 Loki, installed libgtk-3-0 version is 3.18.9.
Upvotes: 4
Views: 4021
Reputation: 4114
From the comments it seems you already checked that the size is the same but translating this to an actual answer.
Lets add a signal to inform us of the window size:
this.configure_event.connect ((event) => {
print ("Window::Size (px) Width: %d Height: %d\n", event.width, event.height);
return false;
});
The resulting code will be:
using Gtk;
public class SizeTest : Gtk.Window {
public SizeTest () {
this.title = "Size test";
this.destroy.connect (Gtk.main_quit);
this.configure_event.connect ((event) => {
print ("Window::Size (px) Width: %d Height: %d\n", event.width, event.height);
return false;
});
set_default_size (800, 600); // Window size -> 800 x 600 px
var drawing_area = new DrawingArea ();
//drawing_area.set_size_request (800, 600); // Window size -> 800 x 600 px
add (drawing_area);
}
static int main (string[] args) {
Gtk.init (ref args);
var test = new SizeTest ();
test.show_all ();
Gtk.main ();
return 0;
}
}
Running the test application using either of the options (Gtk.Widget.set_default_size
vs Gtk.Window.set_default_size
) will output:
Window::Size (px) Width: 800 Height: 600
There's a difference though, using Gtk.Widget.set_size_request
will not allow the widget to be smaller than the given size (and the Gtk.Window
as a consequence) while using Gtk.Window.set_default_size
will permit the window to "shrink" below the initial size.
From the API:
Sets the default size of a window.
If the window’s “natural” size (its size request) is larger than the default, the default will be ignored. More generally, if the default size does not obey the geometry hints for the window ( set_geometry_hints can be used to set these explicitly), the default size will be clamped to the nearest permitted size.
Unlike set_size_request, which sets a size request for a widget and thus would keep users from shrinking the window, this function only sets the initial size, just as if the user had resized the window themselves. Users can still shrink the window again as they normally would. Setting a default size of -1 means to use the “natural” default size (the size request of the window).
Sets the minimum size of a widget; that is, the widget’s size request will be at least width by height.
You can use this function to force a widget to be larger than it normally would be.
In most cases, set_default_size is a better choice for toplevel windows than this function; setting the default size will still allow users to shrink the window. Setting the size request will force them to leave the window at least as large as the size request. When dealing with window sizes, set_geometry_hints can be a useful function as well.
Note the inherent danger of setting any fixed size - themes, translations into other languages, different fonts, and user action can all change the appropriate size for a given widget. So, it's basically impossible to hardcode a size that will always be correct.
The size request of a widget is the smallest size a widget can accept while still functioning well and drawing itself correctly. However in some strange cases a widget may be allocated less than its requested size, and in many cases a widget may be allocated more space than it requested. ... The size request set here does not include any margin from the Widget properties margin-left, margin-right, margin-top, and margin-bottom, but it does include pretty much all other padding or border properties set by any subclass of Widget.
Upvotes: 3