Get calculated size of widget in GTK 4.0 - gtk

Is it possible to find out the calculated size of a widget, say, GtkButton and when the size changes under GTK 4.0? Ive tried a bunch of different things and none seem to work.
It looks like under GTK 3.0 it was possible to use a size-allocate signal, but that has been removed. I appreciate that this is very rarely needed, but surely there must be some way to know a) when the size has changed and b) what that new allocated size is.

size_allocate is a virtual method now. It is intended to be used only by the widget implementation itself, to assign sizes and locations to its children.
To get the size of a widget outside size_allocate, you can use get_allocation.
If you're trying to keep another widget's size synced to the size of your button, you should use a GtkSizeGroup instead.

Related

Show a different child in a Row depending on if it fits or not

Is there some widget that makes it easy to swap out one child in a Row for another smaller one in case the width is not enough to fit the bigger one?
In the image below, the Widget B is a full width version, and D is a mini version of the same widget.
Note that I do not want the widget to adjust its size seamlessly like a Flexible or Expanded - I want it to be either as wide as B, or if that doesn't fit I want it to be as wide as D - not anything in between.
I know I can do this using a LayoutBuilder and a hardcoded condition to check if the available width is at least X pixels. But is there a widget out there that does it without needing a hardcoded min-width? Just adapting to the actual width of B?
You can use a stateful widget and use addPostFrameCallback to get the size from the context:
WidgetsBinding.instance?.addPostFrameCallback((_) {
if (context.size is Size) {
print('My context size is: ${context.size}');
}
});
If you want to know the sizes of both widgets, you can assign a global key to each, and then use a RenderBox to get their sizes.
Any solution that you go with to dynamically get the size of widgets will necessarily mean that they will get laid out, which may not be cheap, depending on the widgets themselves.

Flutter, is it bad habit to use MediaQuery for sizing every widgets?

I almost use like
double number = 0.2;
someWidgetSize : MediaQuery.of(context).size."width or height" * number
for every widgets
but suddenly I thought that it is too easy to make widgets for all device screen sizes
so I`m just curious is it bad habits?(it may have side effects or something)
thanks
No. There is no side effects about this.
We all do this when it is important to have a dynamic widget size. Just be aware that this uses the device size. If you want to use the parent widget's dize use LayoutBuilder widget that gives you the constraints of the parent wiget.

How to make a window fit the width of a screen in GTK+/GTKmm

I have trouble making a window in GTKmm (C++, GTK+, GTKmm 3.22).
The window itself is empty (containing "Hello world").
What I want is that the window's height get set to the maximum height possible for a window, as if it is maximized, while the width is kept at an arbitrary value (200px).
I have huge trouble finding anything in this documentation https://developer.gnome.org/gtkmm-tutorial/stable/ or even the gtkmm doc from my linux distro.
Where can I find this kind of info?
And what should I do?
Maybe you may do this by setting geometry hints:
void Gtk::Window::set_geometry_hints ( Widget& geometry_widget,
const Gdk::Geometry& geometry, Gdk::WindowHints geom_mask )
This function sets up hints about how a window can be resized by the
user.
You can set a minimum and maximum size; allowed resize increments
(e.g. for xterm, you can only resize by the size of a character);
aspect ratios; and more. See the Gdk::Geometry struct.
Parameters
geometry_widget Widget the geometry hints used to be applied to or nullptr. Since 3.20 this argument is ignored and GTK behaves as if
nullptr was set.
geometry Struct containing geometry information or nullptr.
geom_mask Mask indicating which struct fields should be paid attention to.
Cited from here
You could use the Gtk::Widget::set_size_request() to scale your window anyway you like.
Gdk::Display *gdkDisplay = Gdk::Display::get_default().get();
Gdk::Screen *gdkScreen = gdkDisplay->get_default_screen().get();
myWindow->set_size_request(200, gdkScreen->get_height() - 1);
Note that myWindow may be a Gtk::Window derived class, or an instance of Gtk::Window* .

Get image width and height in pixels

so i have looked at a couple other questions like this and from what i saw none of the answers seemed to answer my question. I created a program that creates ASCII art, which is basically a picture of text instead of colors. the way i have the program set up at the moment you have to manually set the Width and Height of the pixels. If the width and height of the pixels is too large it simply wont work. so basically what i want to do is have a function to automatically set the width and height to the size of the picture. http://www.mediafire.com/?3nb8jfb8bhj8d is the link to the program now. I looked into pixel grabber but the constructor methods all needed a range of pixels. I also have another folder for the classes, http://www.mediafire.com/?2u7qt21xhbwtp
on another note this program is incredibly inefficient, i know that it is inefficient in the grayscaleValue() method, but i dont know if there is any better way to do this. Any suggestions on this program would be awesome too. Thanks in advance! (this program was all done on eclipse)
After you read the image into your BufferedImage, you can call getWidth() and getHeight() on it to get this information dynamically. See the JavaDocs. Also, Use a constructor for GetPixelColor to create the BufferedImage once and for all. This will avoid reading the entire file from disk for each channel of each pixel.
For further code clean up, change series of if statements to a switch construct, or an index into an array, whichever is more natural. See this for an explanation of the switch construct.
One last comment: anything inside a class that logically represents the state of an object should be declared non static. If, say, you wanted to render two images side by side, you would need to create to instances if GetPixelColor, and each one should have its own height and width attributes. Since they're currently declared static, each instance would be sharing the same data, which is clearly not desireable behavior.

Get dpi settings via GTK

Using GTK, how do I query the current screen's dpi settings?
The current accepted answer is for PHPGTK, which feels a bit odd to me. The pure GDK library has this call: gdk_screen_get_resolution(), which sounds like a better match. Haven't worked with it myself, don't know if it's generally reliable.
The resolution height and width returned by screen includes the full multi-monitor sizes (e.g. combined width and length of the displayer buffer used to render multi-monitor setup). I've not check of the mm (millimeter width/height) calls returns the actual physical sizes but if it report combined physical sizes then the dpi computed from dividing one with another would be meaningless, e.g. to draw a box on screen that can be measured using a physical ruler.
See GdkScreen. You should be able to compute it using the get_height and get_height_mm or with get_width and get_width_mm.