1 | #include <stdlib.h> |
2 | #include <gtk/gtk.h> |
3 | |
4 | static void |
5 | new_window (GApplication *app, |
6 | GFile *file) |
7 | { |
8 | GtkWidget *window, *scrolled, *view, *overlay; |
9 | GtkWidget *; |
10 | |
11 | window = gtk_application_window_new (GTK_APPLICATION (app)); |
12 | gtk_application_window_set_show_menubar (GTK_APPLICATION_WINDOW (window), TRUE); |
13 | gtk_window_set_default_size (window: (GtkWindow*)window, width: 640, height: 480); |
14 | gtk_window_set_title (GTK_WINDOW (window), title: "Sunny" ); |
15 | gtk_window_set_icon_name (GTK_WINDOW (window), name: "weather-clear-symbolic" ); |
16 | |
17 | header = gtk_header_bar_new (); |
18 | gtk_window_set_titlebar (GTK_WINDOW (window), titlebar: header); |
19 | |
20 | overlay = gtk_overlay_new (); |
21 | gtk_window_set_child (GTK_WINDOW (window), child: overlay); |
22 | |
23 | scrolled = gtk_scrolled_window_new (); |
24 | gtk_widget_set_hexpand (widget: scrolled, TRUE); |
25 | gtk_widget_set_vexpand (widget: scrolled, TRUE); |
26 | view = gtk_text_view_new (); |
27 | |
28 | gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolled), child: view); |
29 | gtk_overlay_set_child (GTK_OVERLAY (overlay), child: scrolled); |
30 | |
31 | if (file != NULL) |
32 | { |
33 | char *contents; |
34 | gsize length; |
35 | |
36 | if (g_file_load_contents (file, NULL, contents: &contents, length: &length, NULL, NULL)) |
37 | { |
38 | GtkTextBuffer *buffer; |
39 | |
40 | buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); |
41 | gtk_text_buffer_set_text (buffer, text: contents, len: length); |
42 | g_free (mem: contents); |
43 | } |
44 | } |
45 | |
46 | gtk_widget_show (GTK_WIDGET (window)); |
47 | } |
48 | |
49 | static void |
50 | activate (GApplication *application) |
51 | { |
52 | new_window (app: application, NULL); |
53 | } |
54 | |
55 | static void |
56 | open (GApplication *application, |
57 | GFile **files, |
58 | int n_files, |
59 | const char *hint) |
60 | { |
61 | int i; |
62 | |
63 | for (i = 0; i < n_files; i++) |
64 | new_window (app: application, file: files[i]); |
65 | } |
66 | |
67 | typedef GtkApplication ; |
68 | typedef GtkApplicationClass ; |
69 | |
70 | static GType menu_button_get_type (void); |
71 | G_DEFINE_TYPE (MenuButton, menu_button, GTK_TYPE_APPLICATION) |
72 | |
73 | static void |
74 | show_about (GSimpleAction *action, |
75 | GVariant *parameter, |
76 | gpointer user_data) |
77 | { |
78 | gtk_show_about_dialog (NULL, |
79 | first_property_name: "program-name" , "Sunny" , |
80 | "title" , "About Sunny" , |
81 | "logo-icon-name" , "weather-clear-symbolic" , |
82 | "comments" , "A cheap Bloatpad clone." , |
83 | NULL); |
84 | } |
85 | |
86 | |
87 | static void |
88 | quit_app (GSimpleAction *action, |
89 | GVariant *parameter, |
90 | gpointer user_data) |
91 | { |
92 | GList *list, *next; |
93 | GtkWindow *win; |
94 | |
95 | g_print (format: "Going down...\n" ); |
96 | |
97 | list = gtk_application_get_windows (GTK_APPLICATION (g_application_get_default ())); |
98 | while (list) |
99 | { |
100 | win = list->data; |
101 | next = list->next; |
102 | |
103 | gtk_window_destroy (GTK_WINDOW (win)); |
104 | |
105 | list = next; |
106 | } |
107 | } |
108 | |
109 | static void |
110 | new_activated (GSimpleAction *action, |
111 | GVariant *parameter, |
112 | gpointer user_data) |
113 | { |
114 | GApplication *app = user_data; |
115 | |
116 | g_application_activate (application: app); |
117 | } |
118 | |
119 | static GActionEntry app_entries[] = { |
120 | { "about" , show_about, NULL, NULL, NULL }, |
121 | { "quit" , quit_app, NULL, NULL, NULL }, |
122 | { "new" , new_activated, NULL, NULL, NULL } |
123 | }; |
124 | |
125 | static void |
126 | startup (GApplication *application) |
127 | { |
128 | GtkBuilder *builder; |
129 | |
130 | G_APPLICATION_CLASS (menu_button_parent_class)->startup (application); |
131 | |
132 | g_action_map_add_action_entries (G_ACTION_MAP (application), entries: app_entries, G_N_ELEMENTS (app_entries), user_data: application); |
133 | |
134 | if (g_getenv (variable: "APP_MENU_FALLBACK" )) |
135 | g_object_set (object: gtk_settings_get_default (), first_property_name: "gtk-shell-shows-app-menu" , FALSE, NULL); |
136 | |
137 | builder = gtk_builder_new (); |
138 | gtk_builder_add_from_string (builder, |
139 | buffer: "<interface>" |
140 | " <menu id='menubar'>" |
141 | " <submenu>" |
142 | " <attribute name='label' translatable='yes'>Sunny</attribute>" |
143 | " <section>" |
144 | " <item>" |
145 | " <attribute name='label' translatable='yes'>_New Window</attribute>" |
146 | " <attribute name='action'>app.new</attribute>" |
147 | " </item>" |
148 | " <item>" |
149 | " <attribute name='label' translatable='yes'>_About Sunny</attribute>" |
150 | " <attribute name='action'>app.about</attribute>" |
151 | " </item>" |
152 | " <item>" |
153 | " <attribute name='label' translatable='yes'>_Quit</attribute>" |
154 | " <attribute name='action'>app.quit</attribute>" |
155 | " <attribute name='accel'><Primary>q</attribute>" |
156 | " </item>" |
157 | " </section>" |
158 | " </submenu>" |
159 | " </menu>" |
160 | "</interface>" , length: -1, NULL); |
161 | gtk_application_set_menubar (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "menubar" ))); |
162 | g_object_unref (object: builder); |
163 | } |
164 | |
165 | static void |
166 | (MenuButton *app) |
167 | { |
168 | } |
169 | |
170 | static void |
171 | (MenuButtonClass *class) |
172 | { |
173 | GApplicationClass *application_class = G_APPLICATION_CLASS (class); |
174 | |
175 | application_class->startup = startup; |
176 | application_class->activate = activate; |
177 | application_class->open = open; |
178 | } |
179 | |
180 | static MenuButton * |
181 | (void) |
182 | { |
183 | return g_object_new (object_type: menu_button_get_type (), |
184 | first_property_name: "application-id" , "org.gtk.Test.Sunny" , |
185 | "flags" , G_APPLICATION_HANDLES_OPEN, |
186 | NULL); |
187 | } |
188 | |
189 | int |
190 | main (int argc, char **argv) |
191 | { |
192 | MenuButton *; |
193 | int status; |
194 | |
195 | menu_button = menu_button_new (); |
196 | status = g_application_run (G_APPLICATION (menu_button), argc, argv); |
197 | g_object_unref (object: menu_button); |
198 | |
199 | return status; |
200 | } |
201 | |