ui/gtk/menu.c

branch
newapi
changeset 390
b130f80ec7f9
parent 389
d15eca5fd8b3
child 391
fc0df448dfbc
equal deleted inserted replaced
389:d15eca5fd8b3 390:b130f80ec7f9
73 } 73 }
74 74
75 return mb; 75 return mb;
76 } 76 }
77 77
78 GtkMenu* ui_create_menu(UiMenuBuilder *builder, UiObject *obj) {
79 GtkWidget *menu_widget = gtk_menu_new();
80 ui_add_menu_items(menu_widget, 0, builder->menus_begin, obj);
81 return GTK_MENU(menu_widget);
82 }
83
84 void ui_add_menu_items(GtkWidget *parent, int i, UiMenu *menu, UiObject *obj) { 78 void ui_add_menu_items(GtkWidget *parent, int i, UiMenu *menu, UiObject *obj) {
85 UiMenuItemI *it = menu->items_begin; 79 UiMenuItemI *it = menu->items_begin;
86 int index = 0; 80 int index = 0;
87 while(it) { 81 while(it) {
88 createMenuItem[it->type](parent, index, it, obj); 82 createMenuItem[it->type](parent, index, it, obj);
359 353
360 /* 354 /*
361 * widget menu functions 355 * widget menu functions
362 */ 356 */
363 357
358 UIMENU ui_contextmenu_create(UiMenuBuilder *builder, UiObject *obj, UIWIDGET widget) {
359 GtkWidget *menu_widget = gtk_menu_new();
360 ui_add_menu_items(menu_widget, 0, builder->menus_begin, obj);
361 return GTK_MENU(menu_widget);
362 }
363
364 static gboolean ui_button_press_event(GtkWidget *widget, GdkEvent *event, GtkMenu *menu) { 364 static gboolean ui_button_press_event(GtkWidget *widget, GdkEvent *event, GtkMenu *menu) {
365 if(event->type == GDK_BUTTON_PRESS) { 365 if(event->type == GDK_BUTTON_PRESS) {
366 GdkEventButton *e = (GdkEventButton*)event; 366 GdkEventButton *e = (GdkEventButton*)event;
367 if(e->button == 3) { 367 if(e->button == 3) {
368 gtk_widget_show_all(GTK_WIDGET(menu)); 368 gtk_widget_show_all(GTK_WIDGET(menu));
369 ui_contextmenu_popup(menu); 369 ui_contextmenu_popup(menu, widget, 0, 0);
370 return TRUE; 370 return TRUE;
371 } 371 }
372 } 372 }
373 return FALSE; 373 return FALSE;
374 } 374 }
375 375
376 void ui_widget_set_contextmenu(GtkWidget *widget, GtkMenu *menu) { 376 void ui_widget_set_contextmenu(GtkWidget *widget, GtkMenu *menu) {
377 g_signal_connect(widget, "button-press-event", (GCallback) ui_button_press_event, menu); 377 g_signal_connect(widget, "button-press-event", (GCallback) ui_button_press_event, menu);
378 } 378 }
379 379
380 void ui_contextmenu_popup(UIMENU menu) { 380 void ui_contextmenu_popup(UIMENU menu, GtkWidget *widget, int x, int y) {
381 #if GTK_MAJOR_VERSION >= 3 && GTK_MINOR_VERSION >= 16 381 #if GTK_MAJOR_VERSION >= 3 && GTK_MINOR_VERSION >= 16
382 gtk_menu_popup_at_pointer(menu, NULL); 382 gtk_menu_popup_at_pointer(menu, NULL);
383 #else 383 #else
384 gtk_menu_popup(menu, NULL, NULL, 0, 0, 0, gtk_get_current_event_time()); 384 gtk_menu_popup(menu, NULL, NULL, 0, 0, 0, gtk_get_current_event_time());
385 #endif 385 #endif
588 } 588 }
589 589
590 590
591 /* --------------------- context menu / menubuilder --------------------- */ 591 /* --------------------- context menu / menubuilder --------------------- */
592 592
593 GtkPopoverMenu* ui_create_menu(UiMenuBuilder *builder, UiObject *obj) { 593 static void remove_popover(GtkWidget *object, GtkPopoverMenu *menu) {
594 gtk_widget_unparent(GTK_WIDGET(menu));
595 }
596
597 UIMENU ui_contextmenu_create(UiMenuBuilder *builder, UiObject *obj, GtkWidget *widget) {
594 GMenu *menu = g_menu_new(); 598 GMenu *menu = g_menu_new();
595 ui_gmenu_add_menu_items(menu, 0, builder->menus_begin, obj); 599 ui_gmenu_add_menu_items(menu, 0, builder->menus_begin, obj);
596 GtkWidget *contextmenu = gtk_popover_menu_new_from_model(G_MENU_MODEL(menu)); 600 GtkWidget *contextmenu = gtk_popover_menu_new_from_model(G_MENU_MODEL(menu));
597 gtk_popover_set_has_arrow(GTK_POPOVER(contextmenu), FALSE); 601 gtk_popover_set_has_arrow(GTK_POPOVER(contextmenu), FALSE);
598 gtk_widget_set_halign(contextmenu, GTK_ALIGN_START); 602 gtk_widget_set_halign(contextmenu, GTK_ALIGN_START);
603 gtk_widget_set_parent(GTK_WIDGET(contextmenu), widget);
604 g_signal_connect(
605 widget,
606 "destroy",
607 G_CALLBACK(remove_popover),
608 contextmenu);
599 return GTK_POPOVER_MENU(contextmenu); 609 return GTK_POPOVER_MENU(contextmenu);
600 } 610 }
601 611
602 static void gesture_button_press(GtkGestureClick *gesture, gint n_press, gdouble x, gdouble y, gpointer user_data) { 612 static void gesture_button_press(GtkGestureClick *gesture, gint n_press, gdouble x, gdouble y, gpointer user_data) {
603 gtk_popover_set_pointing_to (GTK_POPOVER (user_data), &(GdkRectangle){ x, y, 0, 0 }); 613 gtk_popover_set_pointing_to(GTK_POPOVER(user_data), &(GdkRectangle){ x, y, 0, 0 });
604 gtk_popover_popup(GTK_POPOVER(user_data)); 614 gtk_popover_popup(GTK_POPOVER(user_data));
605 }
606
607 static void remove_popover(GtkWidget *object, GtkPopoverMenu *menu) {
608 gtk_widget_unparent(GTK_WIDGET(menu));
609 } 615 }
610 616
611 void ui_widget_set_contextmenu(GtkWidget *widget, GtkPopoverMenu *menu) { 617 void ui_widget_set_contextmenu(GtkWidget *widget, GtkPopoverMenu *menu) {
612 GtkGesture *gesture = gtk_gesture_click_new(); 618 GtkGesture *gesture = gtk_gesture_click_new();
613 gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(gesture), 3); 619 gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(gesture), 3);
614 gtk_widget_add_controller(widget, GTK_EVENT_CONTROLLER(gesture)); 620 gtk_widget_add_controller(widget, GTK_EVENT_CONTROLLER(gesture));
615 g_signal_connect(gesture, "pressed", G_CALLBACK(gesture_button_press), menu); 621 g_signal_connect(gesture, "pressed", G_CALLBACK(gesture_button_press), menu);
616 gtk_widget_set_parent(GTK_WIDGET(menu), widget); 622 }
617 g_signal_connect( 623
618 widget, 624 void ui_contextmenu_popup(UIMENU menu, UIWIDGET widget, int x, int y) {
619 "destroy", 625 gtk_popover_set_pointing_to(GTK_POPOVER(menu), &(GdkRectangle){ x, y, 0, 0 });
620 G_CALLBACK(remove_popover), 626 gtk_popover_popup(GTK_POPOVER(menu));
621 menu);
622 } 627 }
623 628
624 #endif 629 #endif

mercurial