ui/gtk/headerbar.c

branch
newapi
changeset 306
d02e1149cdb9
child 307
301d6c73087a
equal deleted inserted replaced
305:98470af75dcf 306:d02e1149cdb9
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2024 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "headerbar.h"
30
31
32
33 #if GTK_MAJOR_VERSION >= 3
34
35 #ifdef UI_LIBADWAITA
36 #define UI_HEADERBAR AdwHeaderBar*
37 #define UI_HEADERBAR_CAST(h) ADW_HEADER_BAR(h)
38 #define UI_HEADERBAR_PACK_START(h, w) adw_header_bar_pack_start(ADW_HEADER_BAR(h), w)
39 #define UI_HEADERBAR_PACK_END(h, w) adw_header_bar_pack_end(ADW_HEADER_BAR(h), w)
40 #define UI_HEADERBAR_SET_TITLE_WIDGET(h, w) adw_header_bar_set_title_widget(ADW_HEADER_BAR(h), w)
41 #else
42 #define UI_HEADERBAR GtkHeaderBar*
43 #define UI_HEADERBAR_CAST(h) GTK_HEADER_BAR(h)
44 #define UI_HEADERBAR_PACK_START(h, w) gtk_header_bar_pack_start(GTK_HEADER_BAR(h), w)
45 #define UI_HEADERBAR_PACK_END(h, w) gtk_header_bar_pack_end(GTK_HEADER_BAR(h), w)
46 #define UI_HEADERBAR_SET_TITLE_WIDGET(h, w) gtk_header_bar_set_title_widget(GTK_HEADER_BAR(h), w)
47 #endif
48
49 void ui_fill_headerbar(UiObject *obj, GtkWidget *headerbar) {
50 CxMap *items = uic_get_toolbar_items();
51 CxList *left_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_LEFT);
52 CxList *center_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_CENTER);
53 CxList *right_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_RIGHT);
54
55 ui_headerbar_add_items(obj, headerbar, left_defaults, UI_TOOLBAR_LEFT);
56 ui_headerbar_add_items(obj, headerbar, center_defaults, UI_TOOLBAR_CENTER);
57 ui_headerbar_add_items(obj, headerbar, right_defaults, UI_TOOLBAR_RIGHT);
58 }
59
60 static void create_item(UiObject *obj, GtkWidget *headerbar, GtkWidget *box, UiToolbarItemI *i, enum UiToolbarPos pos) {
61 switch(i->type) {
62 case UI_TOOLBAR_ITEM: {
63 ui_add_headerbar_item(headerbar, box, (UiToolbarItem*)i, obj, pos);
64 break;
65 }
66 case UI_TOOLBAR_TOGGLEITEM: {
67 //add_toolitem_toggle_widget(tb, (UiToolbarToggleItem*)i, obj);
68 break;
69 }
70 case UI_TOOLBAR_MENU: {
71 //add_toolitem_menu_widget(tb, (UiToolbarMenuItem*)i, obj);
72 break;
73 }
74 default: fprintf(stderr, "toolbar item type unimplemented: %d\n", (int)i->type);
75 }
76 }
77
78 static void headerbar_add(GtkWidget *headerbar, GtkWidget *box, GtkWidget *item, enum UiToolbarPos pos) {
79 switch(pos) {
80 case UI_TOOLBAR_LEFT: {
81 UI_HEADERBAR_PACK_START(headerbar, item);
82 break;
83 }
84 case UI_TOOLBAR_CENTER: {
85
86 #if GTK_MAJOR_VERSION >= 4
87 gtk_box_append(GTK_BOX(box), item);
88 #else
89 gtk_box_pack_start(GTK_BOX(box), item, 0, 0, 0)
90 #endif
91 break;
92 }
93 case UI_TOOLBAR_RIGHT: {
94 UI_HEADERBAR_PACK_END(headerbar, item);
95 break;
96 }
97 }
98 }
99
100 void ui_headerbar_add_items(UiObject *obj, GtkWidget *headerbar, CxList *items, enum UiToolbarPos pos) {
101 GtkWidget *box = NULL;
102
103 if(pos == UI_TOOLBAR_CENTER && cxListSize(items) > 0) {
104 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
105 UI_HEADERBAR_SET_TITLE_WIDGET(headerbar, box);
106 }
107
108 CxIterator i = pos == UI_TOOLBAR_RIGHT ? cxListBackwardsIterator(items) : cxListIterator(items);
109 cx_foreach(char*, def, i) {
110 UiToolbarItemI* item = uic_toolbar_get_item(def);
111 if (!item) {
112 fprintf(stderr, "unknown toolbar item: %s\n", def);
113 continue;
114 }
115 create_item(obj, headerbar, box, item, pos);
116 }
117 }
118
119 void ui_add_headerbar_item(GtkWidget *headerbar, GtkWidget *box, UiToolbarItem *item, UiObject *obj, enum UiToolbarPos pos) {
120 GtkWidget *button = gtk_button_new_with_label(item->args.label);
121 headerbar_add(headerbar, box, button, pos);
122 }
123
124 #endif

mercurial