Named sizes for standard/wide screen

Is there any named size for displayable items in GTK with standard/wide screens ?

For instance NORMAL_WINDOW :
Standard screen : Height: 0.2 Width: .28

Where ratio(like 0.2) means: item_display_size/monitor_height_size in pixels.

No, there is no such thing in either the GTK API nor the GNOME HIG.

The size of a window is dictated by the size of its contents, and the size of its contents are dictated by things like UI layout, text (which can be translated), and image data. It’s rare to have different layouts depending on the screen size or its ratio, as most screens are 16:9 or 16:10, these days.

Following my first post. This idea is given so that , as screen resolution increases over time the visible size of displayable items remain the same. For instance instead of :

gtk_widget_set_size_request (window, 200, 50);

Something like this is usable :

gtk_widget_set_size_request (window, NORMAL_SIZE);

Where NORMAL_SIZE is one name of several names giving different ratios per screen.

First of all, gtk_widget_set_size_request() should not be used at all. It’s typically a “code smell”, an indication that you’re doing something wrong at the conceptual level. For historical reasons, setting the size request only sets the minimum size of a widget in pixels; the only time when you want to set the size of a widget is for labels and entries, and set the size in characters at the current font size.

Additionally, setting the size of a widget is an exercise in futility; as I said in my earlier answer, the content of a widget might change depending on its contents, and its contents might not be under your, or your users, control—for instance the localisation of your UI might be longer or shorter than your original text.

In general, never set the size of widget explicitly. Always use the expand/align flags, and let the content dictate the size of a window. If you wish to change the UI layout depending on screen orientation, setting the size won’t do you any good: you’ll typically need much more complex functionality, like the one provided by libhandy.

Who defines what’s a “normal” size? Or what kind of different ratios are supported?

The only widget that may be sized differently depending on the screen ratio is the application window, and sizing of top levels is kind of a contested territory, because it’s typically influenced by the window manager rules, not just by the application code. For instance—and referring to an actual real case—on smaller screens, the window manager might decide that all windows that set a size greater than 60% of the screen should automatically be maximised.

gtk_widget_set_size_request() was used in the book " Foundations of GTK+ Development". Which seems a valuable resource to me.

It can be defined as following:
Suppose there is a FHD screen with resolution 1920×1080 pixels. On this screen “normal” size could be for instance 400x300 pixels. So ratios would be 0.208x0.277 . Consequently on a 4K 3840×2160 resolution monitor using ratios , “normal” size would be 800x600 pixels.

There could be other sizes like small, large , and so on.

Which was written in 2007.

I’m not saying that you should never use it; I’m saying that you should not normally use it, and if you use it, you should know exactly what you’re doing and which limitations it has. In general, people using it are either porting code from GTK2 or have a very niche use case, and are typically better served by using the toolkit the way it’s intended to be used, instead of fighting against it.

But, hey: what do I know, I’m just the guy writing the toolkit you’re using.

This is entirely up to you to define as “normal”, and you can definitely achieve this policy in your code base. It could also be part of a specific platform’s human interface guidelines, if you were so inclined. The toolkit should not enforce this, though.

Additionally, it’s not like we can change the API for GTK3, and for GTK4 I’d rather we removed gtk_widget_set_size_request() entirely, given its poor fit with the existing layout management API.

Finally, this is not really correct; on HiDPI displays we use logical pixels, so on a 4K display you’d still see a work area of 1920×1080 logical pixels, and a size of 400×300 logical pixels, the same as you would with a non-HiDPI 1920×1080 display.

Is there any book on GTK+ 3 that I can use ?

Also I am somehow working on an open source GUI project with a Makefile. It uses GTK+ 2.

Not really; unless somebody writes one now, GTK3 moved too fast to allow writing a book on it. These days, GTK3 is feature frozen and stable, so it would be possible to write a book on GTK3; of course, now we’re almost done with GTK4, so it’d be kind of pointless to do so.

The truth is that books on GUI libraries must be written by somebody being paid to do so, and they won’t ever come out from a volunteer-driven project like GTK. They also have a fairly explicit expiration date, given that GTK is not a dead project.

First of all, you should use a proper build system. Second of all, writing GTK2 code in 2020 is kind of pointless—you’re missing out on HiDPI, touch/touchpad events, hardware accelerated rendering, Wayland support, etc.

Now I understand why you’re thinking in terms of physical resolution, instead of logical pixels; GTK3 does all the HiDPI handling for you, so you don’t have to code your sizes depending on the physical size of the screen; I also understand why you think using gtk_widget_set_size_request() is a good idea: GTK2 does not have height-for-width layout management, which means you’re getting weirdly sized text, containers, and windows.

My strong recommendation is to use GTK3 and stop writing code that was meant for computers and UIs as they were defined in 2001-2004.

1 Like

Good Night Ebassi

You said that gtk_widget_set_size_request has been removed from Gtk4
, but I use it together with GSettings, in case the window is not maximized to adjust it to the last size used by the user. If it was removed from Gtk4 what option should I use instead?

No, I didn’t. I said:

But the method is still there as of GTK 3.98.0.

Never use gtk_widget_set_size_request() on a GtkWindow. You must use gtk_window_set_default_size().

If you’re saving and restoring the state of an application window, you should follow these instructions.

Thanks a lot for the help.
I will review my code

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.