]> Pileus Git - ~andy/gtk/blob - examples/sunny.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / examples / sunny.c
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   GtkSettings *settings;
10   gboolean appmenu;
11
12   window = gtk_application_window_new (GTK_APPLICATION (app));
13   gtk_application_window_set_show_menubar (GTK_APPLICATION_WINDOW (window), FALSE);
14   gtk_window_set_default_size ((GtkWindow*)window, 640, 480);
15   gtk_window_set_title (GTK_WINDOW (window), "Sunny");
16
17   overlay = gtk_overlay_new ();
18   gtk_container_add (GTK_CONTAINER (window), overlay);
19
20   settings = gtk_settings_get_default ();
21   g_object_get (settings, "gtk-shell-shows-app-menu", &appmenu, NULL);
22   if (!appmenu)
23     {
24       GMenuModel *model;
25       GtkWidget *menu;
26       GtkWidget *image;
27
28       model = gtk_application_get_app_menu (GTK_APPLICATION (app));
29       menu = gtk_menu_button_new ();
30       gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menu), model);
31       gtk_widget_set_halign (menu, GTK_ALIGN_END);
32       gtk_widget_set_valign (menu, GTK_ALIGN_START);
33       image = gtk_image_new ();
34       gtk_image_set_from_icon_name (GTK_IMAGE (image),
35                                     "sunny",
36                                     GTK_ICON_SIZE_MENU);
37       gtk_button_set_image (GTK_BUTTON (menu), image);
38       gtk_overlay_add_overlay (GTK_OVERLAY (overlay), menu);
39     }
40
41   scrolled = gtk_scrolled_window_new (NULL, NULL);
42   gtk_widget_set_hexpand (scrolled, TRUE);
43   gtk_widget_set_vexpand (scrolled, TRUE);
44   view = gtk_text_view_new ();
45
46   gtk_container_add (GTK_CONTAINER (scrolled), view);
47   gtk_container_add (GTK_CONTAINER (overlay), scrolled);
48
49   if (file != NULL)
50     {
51       gchar *contents;
52       gsize length;
53
54       if (g_file_load_contents (file, NULL, &contents, &length, NULL, NULL))
55         {
56           GtkTextBuffer *buffer;
57
58           buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
59           gtk_text_buffer_set_text (buffer, contents, length);
60           g_free (contents);
61         }
62     }
63
64   gtk_widget_show_all (GTK_WIDGET (window));
65 }
66
67 static void
68 activate (GApplication *application)
69 {
70   new_window (application, NULL);
71 }
72
73 static void
74 open (GApplication  *application,
75       GFile        **files,
76       gint           n_files,
77       const gchar   *hint)
78 {
79   gint i;
80
81   for (i = 0; i < n_files; i++)
82     new_window (application, files[i]);
83 }
84
85 typedef GtkApplication MenuButton;
86 typedef GtkApplicationClass MenuButtonClass;
87
88 G_DEFINE_TYPE (MenuButton, menu_button, GTK_TYPE_APPLICATION)
89
90 static void
91 show_about (GSimpleAction *action,
92             GVariant      *parameter,
93             gpointer       user_data)
94 {
95   gtk_show_about_dialog (NULL,
96                          "program-name", "Sunny",
97                          "title", "About Sunny",
98                          "logo-icon-name", "sunny",
99                          "comments", "A cheap Bloatpad clone.",
100                          NULL);
101 }
102
103
104 static void
105 quit_app (GSimpleAction *action,
106           GVariant      *parameter,
107           gpointer       user_data)
108 {
109   GList *list, *next;
110   GtkWindow *win;
111
112   g_print ("Going down...\n");
113
114   list = gtk_application_get_windows (GTK_APPLICATION (g_application_get_default ()));
115   while (list)
116     {
117       win = list->data;
118       next = list->next;
119
120       gtk_widget_destroy (GTK_WIDGET (win));
121
122       list = next;
123     }
124 }
125
126 static void
127 new_activated (GSimpleAction *action,
128                GVariant      *parameter,
129                gpointer       user_data)
130 {
131   GApplication *app = user_data;
132
133   g_application_activate (app);
134 }
135
136 static GActionEntry app_entries[] = {
137   { "about", show_about, NULL, NULL, NULL },
138   { "quit", quit_app, NULL, NULL, NULL },
139   { "new", new_activated, NULL, NULL, NULL }
140 };
141
142 static void
143 startup (GApplication *application)
144 {
145   GtkBuilder *builder;
146
147   G_APPLICATION_CLASS (menu_button_parent_class)->startup (application);
148
149   g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries, G_N_ELEMENTS (app_entries), application);
150
151   builder = gtk_builder_new ();
152   gtk_builder_add_from_string (builder,
153                                "<interface>"
154                                "  <menu id='app-menu'>"
155                                "    <section>"
156                                "      <item>"
157                                "        <attribute name='label' translatable='yes'>_New Window</attribute>"
158                                "        <attribute name='action'>app.new</attribute>"
159                                "      </item>"
160                                "      <item>"
161                                "        <attribute name='label' translatable='yes'>_About Sunny</attribute>"
162                                "        <attribute name='action'>app.about</attribute>"
163                                "      </item>"
164                                "      <item>"
165                                "        <attribute name='label' translatable='yes'>_Quit</attribute>"
166                                "        <attribute name='action'>app.quit</attribute>"
167                                "        <attribute name='accel'>&lt;Primary&gt;q</attribute>"
168                                "      </item>"
169                                "    </section>"
170                                "  </menu>"
171                                "</interface>", -1, NULL);
172   gtk_application_set_app_menu (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu")));
173   g_object_unref (builder);
174 }
175
176 static void
177 menu_button_init (MenuButton *app)
178 {
179 }
180
181 static void
182 menu_button_class_init (MenuButtonClass *class)
183 {
184   GApplicationClass *application_class = G_APPLICATION_CLASS (class);
185
186   application_class->startup = startup;
187   application_class->activate = activate;
188   application_class->open = open;
189 }
190
191 MenuButton *
192 menu_button_new (void)
193 {
194   return g_object_new (menu_button_get_type (),
195                        "application-id", "org.gtk.Test.Sunny",
196                        "flags", G_APPLICATION_HANDLES_OPEN,
197                        NULL);
198 }
199
200 int
201 main (int argc, char **argv)
202 {
203   MenuButton *menu_button;
204   int status;
205
206   menu_button = menu_button_new ();
207   status = g_application_run (G_APPLICATION (menu_button), argc, argv);
208   g_object_unref (menu_button);
209
210   return status;
211 }