1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
|
/* GTK - The GIMP Toolkit
* Copyright (C) 2019 Red Hat, Inc.
*
* Authors:
* - Matthias Clasen <mclasen@redhat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* SECTION:gtkpopovermenubar
* @Title: GtkPopoverMenuBar
* @Short_description: A menu bar with popovers
* @See_also: #GtkPopover, #GtkPopoverMenu, #GMenuModel
*
* GtkPopoverMenuBar presents a horizontal bar of items that pop
* up popover menus when clicked.
*
* The only way to create instances of GtkPopoverMenuBar is
* from a #GMenuModel.
*
* # CSS nodes
*
* |[<!-- language="plain" -->
* menubar
* ├── item[.active]
* ┊ ╰── popover
* ╰── item
* ╰── popover
* ]|
*
* GtkPopoverMenuBar has a single CSS node with name menubar, below which
* each item has its CSS node, and below that the corresponding popover.
*
* The item whose popover is currently open gets the .active
* style class.
*
* # Accessibility
*
* GtkPopoverMenuBar uses the #GTK_ACCESSIBLE_ROLE_MENU_BAR role,
* the menu items use the #GTK_ACCESSIBLE_ROLE_MENU_ITEM role and
* the menus use the #GTK_ACCESSIBLE_ROLE_MENU role.
*/
#include "config.h"
#include "gtkpopovermenubar.h"
#include "gtkpopovermenubarprivate.h"
#include "gtkpopovermenu.h"
#include "gtkboxlayout.h"
#include "gtklabel.h"
#include "gtkmenubutton.h"
#include "gtkintl.h"
#include "gtkprivate.h"
#include "gtkmarshalers.h"
#include "gtkgestureclick.h"
#include "gtkeventcontrollermotion.h"
#include "gtkactionmuxerprivate.h"
#include "gtkmenutrackerprivate.h"
#include "gtkwidgetprivate.h"
#include "gtkmain.h"
#include "gtknative.h"
#include "gtkbuildable.h"
#define GTK_TYPE_POPOVER_MENU_BAR_ITEM (gtk_popover_menu_bar_item_get_type ())
#define GTK_POPOVER_MENU_BAR_ITEM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_POPOVER_MENU_BAR_ITEM, GtkPopoverMenuBarItem))
#define GTK_IS_POPOVER_MENU_BAR_ITEM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_POPOVER_MENU_BAR_ITEM))
GType gtk_popover_menu_bar_item_get_type (void) G_GNUC_CONST;
typedef struct _GtkPopoverMenuBarItem GtkPopoverMenuBarItem;
struct _GtkPopoverMenuBar
{
GtkWidget parent;
GMenuModel *model;
GtkMenuTracker *tracker;
GtkPopoverMenuBarItem *active_item;
};
typedef struct _GtkPopoverMenuBarClass GtkPopoverMenuBarClass;
struct _GtkPopoverMenuBarClass
{
GtkWidgetClass parent_class;
};
struct _GtkPopoverMenuBarItem
{
GtkWidget parent;
GtkWidget *label;
GtkPopover *popover;
};
typedef struct _GtkPopoverMenuBarItemClass GtkPopoverMenuBarItemClass;
struct _GtkPopoverMenuBarItemClass
{
GtkWidgetClass parent_class;
void (* activate) (GtkPopoverMenuBarItem *item);
};
G_DEFINE_TYPE (GtkPopoverMenuBarItem, gtk_popover_menu_bar_item, GTK_TYPE_WIDGET)
static void
set_active_item (GtkPopoverMenuBar *bar,
GtkPopoverMenuBarItem *item,
gboolean popup)
{
gboolean changed;
gboolean was_popup;
changed = item != bar->active_item;
if (bar->active_item)
was_popup = gtk_widget_get_mapped (GTK_WIDGET (bar->active_item->popover));
else
was_popup = FALSE;
if (was_popup && changed)
{
gtk_accessible_update_state (GTK_ACCESSIBLE (bar->active_item),
GTK_ACCESSIBLE_STATE_EXPANDED, FALSE,
-1);
gtk_popover_popdown (bar->active_item->popover);
}
if (changed)
{
if (bar->active_item)
gtk_widget_unset_state_flags (GTK_WIDGET (bar->active_item), GTK_STATE_FLAG_SELECTED);
bar->active_item = item;
if (bar->active_item)
gtk_widget_set_state_flags (GTK_WIDGET (bar->active_item), GTK_STATE_FLAG_SELECTED, FALSE);
}
if (bar->active_item)
{
if (popup || (was_popup && changed))
{
gtk_popover_popup (bar->active_item->popover);
gtk_accessible_update_state (GTK_ACCESSIBLE (bar->active_item),
GTK_ACCESSIBLE_STATE_EXPANDED, FALSE,
-1);
}
else if (changed)
gtk_widget_grab_focus (GTK_WIDGET (bar->active_item));
}
}
static void
clicked_cb (GtkGesture *gesture,
int n,
double x,
double y,
gpointer data)
{
GtkWidget *target;
GtkPopoverMenuBar *bar;
target = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
bar = GTK_POPOVER_MENU_BAR (gtk_widget_get_ancestor (target, GTK_TYPE_POPOVER_MENU_BAR));
set_active_item (bar, GTK_POPOVER_MENU_BAR_ITEM (target), TRUE);
}
static void
item_enter_cb (GtkEventController *controller,
double x,
double y,
gpointer data)
{
GtkWidget *target;
GtkPopoverMenuBar *bar;
target = gtk_event_controller_get_widget (controller);
bar = GTK_POPOVER_MENU_BAR (gtk_widget_get_ancestor (target, GTK_TYPE_POPOVER_MENU_BAR));
set_active_item (bar, GTK_POPOVER_MENU_BAR_ITEM (target), FALSE);
}
static void
bar_leave_cb (GtkEventController *controller,
gpointer data)
{
GtkWidget *target;
GtkPopoverMenuBar *bar;
target = gtk_event_controller_get_widget (controller);
bar = GTK_POPOVER_MENU_BAR (gtk_widget_get_ancestor (target, GTK_TYPE_POPOVER_MENU_BAR));
if (bar->active_item &&
!gtk_widget_get_mapped (GTK_WIDGET (bar->active_item->popover)))
set_active_item (bar, NULL, FALSE);
}
static gboolean
gtk_popover_menu_bar_focus (GtkWidget *widget,
GtkDirectionType direction)
{
GtkPopoverMenuBar *bar = GTK_POPOVER_MENU_BAR (widget);
GtkWidget *next;
if (bar->active_item &&
gtk_widget_get_mapped (GTK_WIDGET (bar->active_item->popover)))
{
if (gtk_widget_child_focus (GTK_WIDGET (bar->active_item->popover), direction))
return TRUE;
}
if (direction == GTK_DIR_LEFT)
{
if (bar->active_item)
next = gtk_widget_get_prev_sibling (GTK_WIDGET (bar->active_item));
else
next = NULL;
if (next == NULL)
next = gtk_widget_get_last_child (GTK_WIDGET (bar));
}
else if (direction == GTK_DIR_RIGHT)
{
if (bar->active_item)
next = gtk_widget_get_next_sibling (GTK_WIDGET (bar->active_item));
else
next = NULL;
if (next == NULL)
next = gtk_widget_get_first_child (GTK_WIDGET (bar));
}
else
return FALSE;
set_active_item (bar, GTK_POPOVER_MENU_BAR_ITEM (next), FALSE);
return TRUE;
}
static void
gtk_popover_menu_bar_item_init (GtkPopoverMenuBarItem *item)
{
GtkEventController *controller;
gtk_widget_set_focusable (GTK_WIDGET (item), TRUE);
item->label = g_object_new (GTK_TYPE_LABEL,
"use-underline", TRUE,
NULL);
gtk_widget_set_parent (item->label, GTK_WIDGET (item));
controller = GTK_EVENT_CONTROLLER (gtk_gesture_click_new ());
g_signal_connect (controller, "pressed", G_CALLBACK (clicked_cb), NULL);
gtk_widget_add_controller (GTK_WIDGET (item), controller);
controller = gtk_event_controller_motion_new ();
gtk_event_controller_set_propagation_limit (controller, GTK_LIMIT_NONE);
g_signal_connect (controller, "enter", G_CALLBACK (item_enter_cb), NULL);
gtk_widget_add_controller (GTK_WIDGET (item), controller);
}
static void
gtk_popover_menu_bar_item_dispose (GObject *object)
{
GtkPopoverMenuBarItem *item = GTK_POPOVER_MENU_BAR_ITEM (object);
g_clear_pointer (&item->label, gtk_widget_unparent);
g_clear_pointer ((GtkWidget **)&item->popover, gtk_widget_unparent);
G_OBJECT_CLASS (gtk_popover_menu_bar_item_parent_class)->dispose (object);
}
static void
gtk_popover_menu_bar_item_finalize (GObject *object)
{
G_OBJECT_CLASS (gtk_popover_menu_bar_item_parent_class)->finalize (object);
}
static void
gtk_popover_menu_bar_item_measure (GtkWidget *widget,
GtkOrientation orientation,
int for_size,
int *minimum,
int *natural,
int *minimum_baseline,
int *natural_baseline)
{
GtkPopoverMenuBarItem *item = GTK_POPOVER_MENU_BAR_ITEM (widget);
gtk_widget_measure (item->label, orientation, for_size,
minimum, natural,
minimum_baseline, natural_baseline);
}
static void
gtk_popover_menu_bar_item_size_allocate (GtkWidget *widget,
int width,
int height,
int baseline)
{
GtkPopoverMenuBarItem *item = GTK_POPOVER_MENU_BAR_ITEM (widget);
gtk_widget_size_allocate (item->label,
&(GtkAllocation) { 0, 0, width, height },
baseline);
gtk_native_check_resize (GTK_NATIVE (item->popover));
}
static void
gtk_popover_menu_bar_item_activate (GtkPopoverMenuBarItem *item)
{
GtkPopoverMenuBar *bar;
bar = GTK_POPOVER_MENU_BAR (gtk_widget_get_ancestor (GTK_WIDGET (item), GTK_TYPE_POPOVER_MENU_BAR));
set_active_item (bar, item, TRUE);
}
static void
gtk_popover_menu_bar_item_root (GtkWidget *widget)
{
GtkPopoverMenuBarItem *item = GTK_POPOVER_MENU_BAR_ITEM (widget);
GTK_WIDGET_CLASS (gtk_popover_menu_bar_item_parent_class)->root (widget);
gtk_accessible_update_relation (GTK_ACCESSIBLE (widget),
GTK_ACCESSIBLE_RELATION_LABELLED_BY, item->label, NULL,
GTK_ACCESSIBLE_RELATION_CONTROLS, item->popover, NULL,
-1);
gtk_accessible_update_property (GTK_ACCESSIBLE (widget),
GTK_ACCESSIBLE_PROPERTY_HAS_POPUP, TRUE,
-1);
}
static void
gtk_popover_menu_bar_item_class_init (GtkPopoverMenuBarItemClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->dispose = gtk_popover_menu_bar_item_dispose;
object_class->finalize = gtk_popover_menu_bar_item_finalize;
widget_class->root = gtk_popover_menu_bar_item_root;
widget_class->measure = gtk_popover_menu_bar_item_measure;
widget_class->size_allocate = gtk_popover_menu_bar_item_size_allocate;
klass->activate = gtk_popover_menu_bar_item_activate;
widget_class->activate_signal =
g_signal_new (I_("activate"),
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GtkPopoverMenuBarItemClass, activate),
NULL, NULL,
NULL,
G_TYPE_NONE, 0);
gtk_widget_class_set_css_name (widget_class, I_("item"));
gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_MENU_ITEM);
}
enum
{
PROP_0,
PROP_MENU_MODEL,
LAST_PROP
};
static GParamSpec * bar_props[LAST_PROP];
static void gtk_popover_menu_bar_buildable_iface_init (GtkBuildableIface *iface);
G_DEFINE_TYPE_WITH_CODE (GtkPopoverMenuBar, gtk_popover_menu_bar, GTK_TYPE_WIDGET,
G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
gtk_popover_menu_bar_buildable_iface_init))
static void
tracker_remove (int position,
gpointer user_data)
{
GtkWidget *bar = user_data;
GtkWidget *child;
int i;
for (child = gtk_widget_get_first_child (bar), i = 0;
child;
child = gtk_widget_get_next_sibling (child), i++)
{
if (i == position)
{
gtk_widget_unparent (child);
break;
}
}
}
static void
popover_unmap (GtkPopover *popover,
GtkPopoverMenuBar *bar)
{
if (bar->active_item && bar->active_item->popover == popover)
set_active_item (bar, NULL, FALSE);
}
static void
tracker_insert (GtkMenuTrackerItem *item,
int position,
gpointer user_data)
{
GtkPopoverMenuBar *bar = user_data;
if (gtk_menu_tracker_item_get_has_link (item, G_MENU_LINK_SUBMENU))
{
GtkPopoverMenuBarItem *widget;
GMenuModel *model;
GtkWidget *sibling;
GtkWidget *child;
GtkPopover *popover;
int i;
widget = g_object_new (GTK_TYPE_POPOVER_MENU_BAR_ITEM, NULL);
g_object_bind_property (item, "label",
widget->label, "label",
G_BINDING_SYNC_CREATE);
model = _gtk_menu_tracker_item_get_link (item, G_MENU_LINK_SUBMENU);
popover = GTK_POPOVER (gtk_popover_menu_new_from_model_full (model, GTK_POPOVER_MENU_NESTED));
gtk_widget_set_parent (GTK_WIDGET (popover), GTK_WIDGET (widget));
gtk_popover_set_position (popover, GTK_POS_BOTTOM);
gtk_popover_set_has_arrow (popover, FALSE);
gtk_widget_set_halign (GTK_WIDGET (popover), GTK_ALIGN_START);
g_signal_connect (popover, "unmap", G_CALLBACK (popover_unmap), bar);
widget->popover = popover;
sibling = NULL;
for (child = gtk_widget_get_first_child (GTK_WIDGET (bar)), i = 1;
child;
child = gtk_widget_get_next_sibling (child), i++)
{
if (i == position)
{
sibling = child;
break;
}
}
gtk_widget_insert_after (GTK_WIDGET (widget), GTK_WIDGET (bar), sibling);
}
else
g_warning ("Don't know how to handle this item");
}
static void
gtk_popover_menu_bar_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
GtkPopoverMenuBar *bar = GTK_POPOVER_MENU_BAR (object);
switch (property_id)
{
case PROP_MENU_MODEL:
gtk_popover_menu_bar_set_menu_model (bar, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
gtk_popover_menu_bar_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
GtkPopoverMenuBar *bar = GTK_POPOVER_MENU_BAR (object);
switch (property_id)
{
case PROP_MENU_MODEL:
g_value_set_object (value, bar->model);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
gtk_popover_menu_bar_dispose (GObject *object)
{
GtkPopoverMenuBar *bar = GTK_POPOVER_MENU_BAR (object);
GtkWidget *child;
g_clear_pointer (&bar->tracker, gtk_menu_tracker_free);
g_clear_object (&bar->model);
while ((child = gtk_widget_get_first_child (GTK_WIDGET (bar))))
gtk_widget_unparent (child);
G_OBJECT_CLASS (gtk_popover_menu_bar_parent_class)->dispose (object);
}
static GList *
get_menu_bars (GtkWindow *window)
{
return g_object_get_data (G_OBJECT (window), "gtk-menu-bar-list");
}
GList *
gtk_popover_menu_bar_get_viewable_menu_bars (GtkWindow *window)
{
GList *menu_bars;
GList *viewable_menu_bars = NULL;
for (menu_bars = get_menu_bars (window);
menu_bars;
menu_bars = menu_bars->next)
{
GtkWidget *widget = menu_bars->data;
gboolean viewable = TRUE;
while (widget)
{
if (!gtk_widget_get_mapped (widget))
viewable = FALSE;
widget = gtk_widget_get_parent (widget);
}
if (viewable)
viewable_menu_bars = g_list_prepend (viewable_menu_bars, menu_bars->data);
}
return g_list_reverse (viewable_menu_bars);
}
static void
set_menu_bars (GtkWindow *window,
GList *menubars)
{
g_object_set_data (G_OBJECT (window), I_("gtk-menu-bar-list"), menubars);
}
static void
add_to_window (GtkWindow *window,
GtkPopoverMenuBar *bar)
{
GList *menubars = get_menu_bars (window);
set_menu_bars (window, g_list_prepend (menubars, bar));
}
static void
remove_from_window (GtkWindow *window,
GtkPopoverMenuBar *bar)
{
GList *menubars = get_menu_bars (window);
menubars = g_list_remove (menubars, bar);
set_menu_bars (window, menubars);
}
static void
gtk_popover_menu_bar_root (GtkWidget *widget)
{
GtkPopoverMenuBar *bar = GTK_POPOVER_MENU_BAR (widget);
GtkWidget *toplevel;
GTK_WIDGET_CLASS (gtk_popover_menu_bar_parent_class)->root (widget);
toplevel = GTK_WIDGET (gtk_widget_get_root (widget));
add_to_window (GTK_WINDOW (toplevel), bar);
gtk_accessible_update_property (GTK_ACCESSIBLE (bar),
GTK_ACCESSIBLE_PROPERTY_ORIENTATION, GTK_ORIENTATION_HORIZONTAL,
-1);
}
static void
gtk_popover_menu_bar_unroot (GtkWidget *widget)
{
GtkPopoverMenuBar *bar = GTK_POPOVER_MENU_BAR (widget);
GtkWidget *toplevel;
toplevel = GTK_WIDGET (gtk_widget_get_root (widget));
remove_from_window (GTK_WINDOW (toplevel), bar);
GTK_WIDGET_CLASS (gtk_popover_menu_bar_parent_class)->unroot (widget);
}
static void
gtk_popover_menu_bar_class_init (GtkPopoverMenuBarClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->dispose = gtk_popover_menu_bar_dispose;
object_class->set_property = gtk_popover_menu_bar_set_property;
object_class->get_property = gtk_popover_menu_bar_get_property;
widget_class->root = gtk_popover_menu_bar_root;
widget_class->unroot = gtk_popover_menu_bar_unroot;
widget_class->focus = gtk_popover_menu_bar_focus;
/**
* GtkPopoverMenuBar:menu-model:
*
* The #GMenuModel from which the menu bar is created.
*
* The model should only contain submenus as toplevel elements.
*/
bar_props[PROP_MENU_MODEL] =
g_param_spec_object ("menu-model",
P_("Menu model"),
P_("The model from which the bar is made."),
G_TYPE_MENU_MODEL,
GTK_PARAM_READWRITE);
g_object_class_install_properties (object_class, LAST_PROP, bar_props);
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
gtk_widget_class_set_css_name (widget_class, I_("menubar"));
gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_MENU_BAR);
}
static void
gtk_popover_menu_bar_init (GtkPopoverMenuBar *bar)
{
GtkEventController *controller;
controller = gtk_event_controller_motion_new ();
gtk_event_controller_set_propagation_limit (controller, GTK_LIMIT_NONE);
g_signal_connect (controller, "leave", G_CALLBACK (bar_leave_cb), NULL);
gtk_widget_add_controller (GTK_WIDGET (bar), controller);
}
static GtkBuildableIface *parent_buildable_iface;
static void
gtk_popover_menu_bar_buildable_add_child (GtkBuildable *buildable,
GtkBuilder *builder,
GObject *child,
const char *type)
{
if (GTK_IS_WIDGET (child))
{
if (!gtk_popover_menu_bar_add_child (GTK_POPOVER_MENU_BAR (buildable), GTK_WIDGET (child), type))
g_warning ("No such custom attribute: %s", type);
}
else
parent_buildable_iface->add_child (buildable, builder, child, type);
}
static void
gtk_popover_menu_bar_buildable_iface_init (GtkBuildableIface *iface)
{
parent_buildable_iface = g_type_interface_peek_parent (iface);
iface->add_child = gtk_popover_menu_bar_buildable_add_child;
}
/**
* gtk_popover_menu_bar_new_from_model:
* @model: (allow-none): a #GMenuModel, or %NULL
*
* Creates a #GtkPopoverMenuBar from a #GMenuModel.
*
* Returns: a new #GtkPopoverMenuBar
*/
GtkWidget *
gtk_popover_menu_bar_new_from_model (GMenuModel *model)
{
return g_object_new (GTK_TYPE_POPOVER_MENU_BAR,
"menu-model", model,
NULL);
}
/**
* gtk_popover_menu_bar_set_menu_model:
* @bar: a #GtkPopoverMenuBar
* @model: (allow-none): a #GMenuModel, or %NULL
*
* Sets a menu model from which @bar should take
* its contents.
*/
void
gtk_popover_menu_bar_set_menu_model (GtkPopoverMenuBar *bar,
GMenuModel *model)
{
g_return_if_fail (GTK_IS_POPOVER_MENU_BAR (bar));
g_return_if_fail (G_IS_MENU_MODEL (model));
if (g_set_object (&bar->model, model))
{
GtkWidget *child;
GtkActionMuxer *muxer;
while ((child = gtk_widget_get_first_child (GTK_WIDGET (bar))))
gtk_widget_unparent (child);
g_clear_pointer (&bar->tracker, gtk_menu_tracker_free);
if (model)
{
muxer = _gtk_widget_get_action_muxer (GTK_WIDGET (bar), TRUE);
bar->tracker = gtk_menu_tracker_new (GTK_ACTION_OBSERVABLE (muxer),
model,
FALSE,
TRUE,
FALSE,
NULL,
tracker_insert,
tracker_remove,
bar);
}
g_object_notify_by_pspec (G_OBJECT (bar), bar_props[PROP_MENU_MODEL]);
}
}
/**
* gtk_popover_menu_bar_get_menu_model:
* @bar: a #GtkPopoverMenuBar
*
* Returns the model from which the contents of @bar are taken.
*
* Returns: (transfer none): a #GMenuModel
*/
GMenuModel *
gtk_popover_menu_bar_get_menu_model (GtkPopoverMenuBar *bar)
{
g_return_val_if_fail (GTK_IS_POPOVER_MENU_BAR (bar), NULL);
return bar->model;
}
void
gtk_popover_menu_bar_select_first (GtkPopoverMenuBar *bar)
{
GtkPopoverMenuBarItem *item;
item = GTK_POPOVER_MENU_BAR_ITEM (gtk_widget_get_first_child (GTK_WIDGET (bar)));
set_active_item (bar, item, TRUE);
}
/**
* gtk_popover_menu_bar_add_child:
* @bar: a #GtkPopoverMenuBar
* @child: the #GtkWidget to add
* @id: the ID to insert @child at
*
* Adds a custom widget to a generated menubar.
*
* For this to work, the menu model of @bar must have an
* item with a `custom` attribute that matches @id.
*
* Returns: %TRUE if @id was found and the widget added
*/
gboolean
gtk_popover_menu_bar_add_child (GtkPopoverMenuBar *bar,
GtkWidget *child,
const char *id)
{
GtkWidget *item;
g_return_val_if_fail (GTK_IS_POPOVER_MENU_BAR (bar), FALSE);
g_return_val_if_fail (GTK_IS_WIDGET (child), FALSE);
g_return_val_if_fail (id != NULL, FALSE);
for (item = gtk_widget_get_first_child (GTK_WIDGET (bar));
item;
item = gtk_widget_get_next_sibling (item))
{
GtkPopover *popover = GTK_POPOVER_MENU_BAR_ITEM (item)->popover;
if (gtk_popover_menu_add_child (GTK_POPOVER_MENU (popover), child, id))
return TRUE;
}
return FALSE;
}
/**
* gtk_popover_menu_bar_remove_child:
* @bar: a #GtkPopoverMenuBar
* @child: the #GtkWidget to remove
*
* Removes a widget that has previously been added with
* gtk_popover_menu_bar_add_child().
*
* Returns: %TRUE if the widget was removed
*/
gboolean
gtk_popover_menu_bar_remove_child (GtkPopoverMenuBar *bar,
GtkWidget *child)
{
GtkWidget *item;
g_return_val_if_fail (GTK_IS_POPOVER_MENU_BAR (bar), FALSE);
g_return_val_if_fail (GTK_IS_WIDGET (child), FALSE);
for (item = gtk_widget_get_first_child (GTK_WIDGET (bar));
item;
item = gtk_widget_get_next_sibling (item))
{
GtkPopover *popover = GTK_POPOVER_MENU_BAR_ITEM (item)->popover;
if (gtk_popover_menu_remove_child (GTK_POPOVER_MENU (popover), child))
return TRUE;
}
return FALSE;
}
|