comparison gtk4/dw.c @ 2300:6e47d510dbbb

GTK4: Since GTK4 needs to add the popup menu to the parent widget... We need to remove it after the popup closes, so we trap the "close" signal and try to remove the popup from the parent. Since the "close" signal gets called before the menu's "activate" signal, we have to delay unparenting until after the "activate" signal handler is run. Otherwise the menu objects won't be valid for the "activate" handler, therefore we add an idle callback to unparent the widget when the rest of the processing is done.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 08 Feb 2021 10:49:50 +0000
parents 27c20fa1615e
children 69b06073a87d
comparison
equal deleted inserted replaced
2299:27c20fa1615e 2300:6e47d510dbbb
2543 ret = DW_ERROR_NONE; 2543 ret = DW_ERROR_NONE;
2544 } 2544 }
2545 return ret; 2545 return ret;
2546 } 2546 }
2547 2547
2548 /* Delayed unparent of the popup menu from the parent */
2549 gboolean _dw_idle_popover_unparent(gpointer data)
2550 {
2551 GtkWidget *self = GTK_WIDGET(data);
2552
2553 gtk_widget_unparent(self);
2554 return false;
2555 }
2556
2557 void _dw_popover_menu_closed(GtkPopover *self, gpointer data)
2558 {
2559 GtkWidget *parent = GTK_WIDGET(data);
2560
2561 /* Can't unparent immediately, since the "activate" signal happens second...
2562 * so we have to delay unparenting until the "activate" handler runs.
2563 */
2564 if(GTK_IS_WIDGET(parent) && GTK_IS_POPOVER(self))
2565 g_idle_add(G_SOURCE_FUNC(_dw_idle_popover_unparent), (gpointer)self);
2566 }
2567
2548 /* 2568 /*
2549 * Pops up a context menu at given x and y coordinates. 2569 * Pops up a context menu at given x and y coordinates.
2550 * Parameters: 2570 * Parameters:
2551 * menu: The handle the the existing menu. 2571 * menu: The handle the the existing menu.
2552 * parent: Handle to the window initiating the popup. 2572 * parent: Handle to the window initiating the popup.
2559 { 2579 {
2560 GtkWidget *tmp = gtk_popover_menu_new_from_model_full(G_MENU_MODEL(*menu), GTK_POPOVER_MENU_NESTED); 2580 GtkWidget *tmp = gtk_popover_menu_new_from_model_full(G_MENU_MODEL(*menu), GTK_POPOVER_MENU_NESTED);
2561 2581
2562 gtk_widget_set_parent(tmp, GTK_WIDGET(parent)); 2582 gtk_widget_set_parent(tmp, GTK_WIDGET(parent));
2563 _dw_menu_set_group_recursive(*menu, GTK_WIDGET(tmp)); 2583 _dw_menu_set_group_recursive(*menu, GTK_WIDGET(tmp));
2584 gtk_popover_set_autohide(GTK_POPOVER(tmp), TRUE);
2585 #if 0
2564 gtk_popover_set_offset(GTK_POPOVER(tmp), x, y); 2586 gtk_popover_set_offset(GTK_POPOVER(tmp), x, y);
2565 gtk_popover_set_autohide(GTK_POPOVER(tmp), TRUE); 2587 #endif
2588 g_signal_connect(G_OBJECT(tmp), "closed", G_CALLBACK(_dw_popover_menu_closed), (gpointer)parent);
2566 gtk_popover_popup(GTK_POPOVER(tmp)); 2589 gtk_popover_popup(GTK_POPOVER(tmp));
2567 *menu = NULL; 2590 *menu = NULL;
2568 } 2591 }
2569 } 2592 }
2570 2593