How to handle window resizes in GTK4? - gtk

I am trying to build a virtualized grid container to display 100k+ files in PyGTK 4 on Fedora 35. I succeeded in creating the layout and scrolling support which works beautifully.
In order to support resizing of my application window, I need to react to changes its size. I already connected to the notify signal of the window and handle the default-width, default-height, maximized and unmaximized events to update the geometry of my layout. However, everytime I invoke get_width() of my widget/app window after the maximized event I only get the width before the window was maximized! Is this by design?
I've been trying pretty hard but I fail to come to a working solution for this simple problem. Can anyone help me?
Edit
Here is some code to outline how I am trying to measure the window size. I modified the code to use connect_after instead of connectas proposed by #GüntherWagner. However, the behavior is the same for both methods:
class TestLayout(Gtk.ScrolledWindow):
def on_realize(self, widget):
...
self.get_root().connect_after('notify', self.on_notify)
def on_notify(self, widget, param):
if param.name in [..., 'maximized', 'unmaximized']:
self.draw()
def draw(self):
self.width = self.get_width()
self.height = self.get_height()

Related

Auto Resizing NatTable in Eclipse RCP Application

I am trying to auto resize my NatTable. I have few things :
1) My NatTable consists of only one column.
2) I am hiding NatTable occasionally.
3) The Part Control surrounding NatTable auto resizes on mouse drag. NatTable should also fit the size.
4) I have custom cell painters for NatTable Cells.
I tried with my bodyDataLayer(Which is based on GlazedListsDataProvider) :
bodyDataLayer.setDefaultRowHeight(35);
bodyDataLayer.setColumnWidthByPosition(0, 100);
bodyDataLayer.setColumnPercentageSizing(true);
But nattable does not show up at all. If I double click the invisible rows, I get required row. But natTable itself does not appear.
NatTable shows up only if
bodyDataLayer.setDefaultColumnWidth(SOME_VALUE);
Only default column width works.
I tried to add a Paint Listener to NatTable to initialize auto resize commands as suggested on Official Documentation. But it has no effect at all.
I tried to add
natTable.setLayerPainter(
new NatGridLayerPainter(natTable, SOME_VALUE));
with auto resizing of columns by position.But it also renders nothing. I am wondering how to resize my NatTable?
Typically DataLayer#setColumnPercentageSizing(true)should be sufficient for percentage sizing.
I can only assume that hiding NatTable occasionally has a negative effect, because that might lead to miscalculation of the percentage values. 100% of 0 is still 0.
Are you calling NatTable#setVisible(false) to hide the NatTable? In that case try to execute the following lines of code
natTable.doCommand(new ClientAreaResizeCommand(natTable));
natTable.redraw();
If that fixes your issue, please open a ticket and report that issue. In that case it seems we need to add a listener for SWT.Show or override setVisible() to execute these lines of code on visibility changes.

Scala Swing: Changing BorderPanel central component new Component not visible

I'm changing the centre area on a Scala Swing Border Panel. The old component disappears, but the new component does not appear, until I resize the window manually with the mouse. I've tried using different components and repainting, but nothing makes the new component appear except manually resizing the window with the mouse.
def splitDisp(mapCanv: VistaIn): Unit =
{
val canv2 = newMapCanv
panel.layout(canv2) = BorderPanel.Position.Center
canv2.repaint
thisScn.repaint //ref to the Frame instance
panel.repaint
thisScn.repaint
canv2.repaint
}
I'm using 2.10.0M5 in Windows 7.
Try calling revalidate() and then repaint().

gtkmm button not maintaining size and location

I have created two gtkmm button and added to HBox object. I called pack_end, and maintained the size as 21,20. But, the sizes are not maintained. Here is the code i have written and the window that i got while running the program.
Note: MYWindow is subclass of Gtk::Window
void MYWindow::customizeTitleBar()
{
//create a vertical box
Gtk::VBox *vBox = new Gtk::VBox(FALSE,0);
//create a horizontal box
Gtk::HBox *hBox = new Gtk::HBox(TRUE,0);
hBox->set_border_width(5);
//create title bar image
Gtk::Image *titleBarImage = new Gtk::Image("src/WindowTitleBar.png");
titleBarImage->set_alignment(Gtk::ALIGN_LEFT);
// hBox->pack_start(*titleBarImage,Gtk::PACK_EXPAND_WIDGET,0);
//create cloze button for window
mButtonClose = new Gtk::Button;
(*mButtonClose).set_size_request(21,20);
Gtk::Image *mImage = new Gtk::Image("src/Maximize.jpeg");
(*mButtonClose).add(*mImage);
(*mButtonClose).set_image_position(Gtk::POS_TOP);
// connecting close window function when cliked on close button
//(*mButtonClose).signal_clicked().connect( sigc::mem_fun(this, &MYWindow::closeWindow));
hBox->pack_end(*mButtonClose,Gtk::PACK_EXPAND_WIDGET,0);
Gtk::Button * mBtton = new Gtk::Button;
mBtton->set_size_request(21,20);
Gtk::Image *img = new Gtk::Image("src/Maximize.jpeg");
mBtton->add(*img);
mBtton->set_image_position(Gtk::POS_TOP);
hBox->pack_end(*mBtton,Gtk::PACK_EXPAND_WIDGET,0);
vBox->add(*hBox);
//drawing area box
Gtk::HBox *hBoxDrawingArea = new Gtk::HBox;
Gtk::DrawingArea *mDrawingArea = new Gtk::DrawingArea;
hBoxDrawingArea->pack_start(*mDrawingArea,Gtk::PACK_EXPAND_WIDGET,0);
vBox->add(*hBoxDrawingArea);
//status bar hBox
Gtk::HBox *hBoxStatusBar = new Gtk::HBox;
vBox->add(*hBoxStatusBar);
this->add(*vBox);
this->show_all();
}
I am not yet a gtk expert (but I'm learning), here's one thing you can try, which is what I've been doing.
Make a little standalone project using glade. Glade makes it really easy to screw around with all the packing settings so you can immediately see the effects of your changes.
I think in the case of resizing the window, you'll have to save the glade file and run your program (using gtkbuilder to render the glade file) and manually resize the window to see the effect, but once you make the standalone project, you can use it for other gtk testing.
And if you're like me, you'll get swayed by the wonderfulness that is glade and build your whole system that way.
But basically, it sounds like a packing issue, because I've got buttons that don't resize all over the place.
As for not moving, I'm not sure you can do that, but again I'm not an expert. I think you should be able to pin the size of some if not all of the hbox pieces so that the button inside them will not move, but I'm not sure what happens if you don't have any hbox parts that can't be variably sized to take up the slack when you grow the window.
Again, sounds like something fun to try in glade. :-)
I think you pack to FALSE , Maybe this is the problem :
Gtk::HBox *hBox = new Gtk::HBox(TRUE,0)
I use python gtk with something like this:
box1.pack_start(box2,False)

GTK detecting window resize from the user

In GTK (or pygtk or gtkmm...)
How can I detect that an application window has been manually resized by the user, as is typically done by dragging the window's edge?
I need to find a way to differentiate manual resizes from resizes that originate from gtk, such as changes in window content.
Have you tried connecting to the GDK_CONFIGURE event?
Check out this example under the
"Moving window" section. The example shows a callback doing something when the window is moved, but the configure event is a catch-all for moving, resizing and stack order events.
I managed to pull this off by watching for size_allocate and size_request signals on the GtkWindow. If size_request ever got smaller, I called resize(1,1). If size_allocate was ever bigger than expected, I turned the system off.
One thing I made sure to handle was size_request returning big, then small, and having size_allocate be big and then small. I don't know if this is possible, but I fixed it by making sure to only decrease the expected values for size_allocate when I got a smaller size_allocate, not when I got a smaller size_request.
Make sure that your size_request handler comes after the base class' handler so that you get the right values. I did this by overriding the method and then calling the base class method first.
I've tried this in both 1 and 2 dimensions and it seems to work either way.
In my case I was trying to distinguish between a user resizing a Gtk.Paned from the user resizing the whole window. Both emitted the notify::position signal.
My solution was, since I can't know if the user is resizing the window from the widget, reverse what I wanted to know. Record if the user has re-positioned the widget and ignore updates if the user didn't initiate them on my widget.
That is to say, instead of testing "if window being resized" I recorded the button-press-event and button-release-event's locally so I could instead test "if widget being re-positioned"
from gi.repository import Gtk
class MyPaned(Gtk.Paned):
_user_activated = False
def on_position(self, _, gparamspec):
if self._user_activated:
# widget touched
else:
# window resized (probably)
def on_button_press(self, *_):
self._user_activated = True
def on_button_release(self, *_):
self._user_activated = False
dev __init__(self, *args):
super(MyPaned, self).__init__(*args)
self.connect('notify::position', self.on_position)
self.connect('button-press-event', self.on_button_press)
self.connect('button-release-event', self.on_button_release)
Effectively by recorded when the user started and ended interacting with my widget directly, I could assume the rest of the time was due to the window being resized. (Until I find more cases)
In PyGTK, I've always watched for the expose_event for a window resize, then use the get_allocation method to get the new size.
You may be able to throw something together by using gdk_window_get_root_origin to get the top left corner of the window and gdk_window_get_geometry to get the width and height. Then you could hook a callback into the GDK_BUTTON_PRESS_MASK and check to see if the button press occurs near/on one of the edges of the window.
Of course, this seems quite hackish and it really bothers me that I couldn't find some simple way in the documentation for GdkWindow to do this. There is a gdk_window_begin_resize_drag function which really makes me think there's a cleaner way to do this, but I didn't see anything more obvious than my answer.

How to get the height of the gtktreeview header?

I've looked in the gtk source code and the header height is private. I've tried something but it didn't work as wanted (the heightWithHeader is 1?!)
Glib::RefPtr<Gdk::Window> pWindow = treeView.get_bin_window();
treeView.set_headers_visible(true);
pWindow->get_size(width, heightWithHeader);
treeView.set_headers_visible(false);
pWindow->get_size(width, heightWithoutHeader);
r_treeView.set_headers_visible(true);
returnValue = heightWithHeader - heightWithoutHeader;
Can you help me with another solution or a fix to my code?
Update: I have to adjust the height of the treeview to display a fixed number of rows. I do this by adjusting the size of the container (a scrolledwindow) to headerHeight + numberRowsToDisplay * heightOfRow.
The reason your code doesn't work is very probably that you're being "too impatient", not giving GTK+ time to do the redraw of the widgets before you make the headers invisible again.
GTK+ doesn't draw immediately when you do a call that requires a redraw. Instead redraws are queued, and then done all at once from the GTK+ main loop. This way, doing two changes to widgets in sequence does not cause two redraws, but only one.
It's a bit of a hack, but you could try the "classic" GTK+ event-flushing trick, by inserting a loop like this after you turn on the headers:
while(gtk_events_pending())
gtk_main_iteration();
This simply loops for as long as there are events in GTK+'s queue (the draw changes mentioned above are events, internally), and flushes them, then gives control back to you. This will very probably result in some visual flicker, though.