]> Pileus Git - ~andy/gtk/blob - tests/testapplication.c
gtk: Add gtk_widget_queue_draw_region()
[~andy/gtk] / tests / testapplication.c
1 /* GTK - The GIMP Toolkit
2  * testapplication.c: Using GtkApplication
3  * Copyright (C) 2010, Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24
25 #include <gtk/gtk.h>
26 #include <gio/gdesktopappinfo.h>
27
28 static const char *builder_data =
29 "<interface>"
30 "<object class=\"GtkAboutDialog\" id=\"about_dialog\">"
31 "  <property name=\"program-name\">Test Application</property>"
32 "  <property name=\"website\">http://gtk.org</property>"
33 "</object>"
34 "<object class=\"GtkActionGroup\" id=\"actions\">"
35 "  <child>"
36 "      <object class=\"GtkAction\" id=\"About\">"
37 "          <property name=\"name\">About</property>"
38 "          <property name=\"stock_id\">gtk-about</property>"
39 "      </object>"
40 "  </child>"
41 "</object>"
42 "</interface>";
43
44 static GtkWidget *about_dialog;
45
46 static void
47 about_activate (GtkAction *action,
48                 gpointer   user_data)
49 {
50   gtk_dialog_run (GTK_DIALOG (about_dialog));
51   gtk_widget_hide (GTK_WIDGET (about_dialog));
52 }
53
54 static void
55 launch_myself (void)
56 {
57   GAppInfo *ai;
58
59   g_type_init ();
60
61   ai = (GAppInfo*)g_desktop_app_info_new_from_filename ("./testapplication.desktop");
62   g_app_info_launch (ai, NULL, NULL, NULL);
63 }
64
65 int
66 main (int argc, char **argv)
67 {
68   GtkApplication *app;
69   GtkWindow *window;
70   GtkWidget *window2;
71   GtkBuilder *builder;
72   GtkAction *action;
73   GtkActionGroup *actions;
74
75   if (argc > 1 && g_strcmp0 (argv[1], "--launch-yourself") == 0)
76     {
77       launch_myself ();
78       exit (0);
79     }
80
81   app = gtk_application_new ("org.gtk.TestApp", &argc, &argv);
82   builder = gtk_builder_new ();
83   if (!gtk_builder_add_from_string (builder, builder_data, -1, NULL))
84     g_error ("failed to parse UI");
85   actions = GTK_ACTION_GROUP (gtk_builder_get_object (builder, "actions"));
86   gtk_application_set_action_group (app, actions);
87
88   action = gtk_action_group_get_action (actions, "About");
89   g_signal_connect (action, "activate", G_CALLBACK (about_activate), app);
90
91   about_dialog = GTK_WIDGET (gtk_builder_get_object (builder, "about_dialog"));
92
93   gtk_builder_connect_signals (builder, app);
94   g_object_unref (builder);
95
96   window = gtk_application_get_window (app);
97   gtk_container_add (GTK_CONTAINER (window), gtk_label_new ("Hello world"));
98   gtk_widget_show_all (GTK_WIDGET (window));
99
100   window2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
101   gtk_container_add (GTK_CONTAINER (window2), gtk_label_new ("Hello again"));
102   gtk_widget_show_all (window2);
103   gtk_application_add_window (app, GTK_WINDOW (window2));
104
105   gtk_application_run (app);
106
107   return 0;
108 }