]> Pileus Git - ~andy/gtk/blob - tests/testapplication.c
[GtkApplication] Update for GApplication API changes
[~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 #include <gtk/gtk.h>
23 #include <gio/gdesktopappinfo.h>
24
25 static const char *builder_data =
26 "<interface>"
27 "<object class=\"GtkAboutDialog\" id=\"about_dialog\">"
28 "  <property name=\"program-name\">Test Application</property>"
29 "  <property name=\"website\">http://gtk.org</property>"
30 "</object>"
31 "<object class=\"GtkActionGroup\" id=\"actions\">"
32 "  <child>"
33 "      <object class=\"GtkAction\" id=\"About\">"
34 "          <property name=\"name\">About</property>"
35 "          <property name=\"stock_id\">gtk-about</property>"
36 "      </object>"
37 "  </child>"
38 "</object>"
39 "</interface>";
40
41 static GtkWidget *about_dialog;
42
43 static void
44 about_activate (GtkAction *action,
45                 gpointer   user_data)
46 {
47   gtk_dialog_run (GTK_DIALOG (about_dialog));
48   gtk_widget_hide (GTK_WIDGET (about_dialog));
49 }
50
51 static void
52 launch_myself (void)
53 {
54   GAppInfo *ai;
55
56   g_type_init ();
57
58   ai = (GAppInfo*)g_desktop_app_info_new_from_filename ("./testapplication.desktop");
59   g_app_info_launch (ai, NULL, NULL, NULL);
60 }
61
62 int
63 main (int argc, char **argv)
64 {
65   GtkApplication *app;
66   GtkWindow *window;
67   GtkWindow *window2;
68   GtkBuilder *builder;
69   GtkAction *action;
70   GtkActionGroup *actions;
71
72   if (argc > 1 && strcmp (argv[1], "--launch-yourself") == 0)
73     {
74       launch_myself ();
75       exit (0);
76     }
77
78   app = gtk_application_new ("org.gtk.TestApp", &argc, &argv);
79   builder = gtk_builder_new ();
80   if (!gtk_builder_add_from_string (builder, builder_data, -1, NULL))
81     g_error ("failed to parse UI");
82   actions = GTK_ACTION_GROUP (gtk_builder_get_object (builder, "actions"));
83   gtk_application_set_action_group (app, actions);
84
85   action = gtk_action_group_get_action (actions, "About");
86   g_signal_connect (action, "activate", G_CALLBACK (about_activate), app);
87
88   about_dialog = GTK_WIDGET (gtk_builder_get_object (builder, "about_dialog"));
89
90   gtk_builder_connect_signals (builder, app);
91   g_object_unref (builder);
92
93   window = gtk_application_get_window (app);
94   gtk_container_add (GTK_CONTAINER (window), gtk_label_new ("Hello world"));
95   gtk_widget_show_all (GTK_WIDGET (window));
96
97   window2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
98   gtk_container_add (GTK_CONTAINER (window2), gtk_label_new ("Hello again"));
99   gtk_widget_show_all (GTK_WIDGET (window2));
100   gtk_application_add_window (app, window2);
101
102   gtk_application_run (app);
103
104   return 0;
105 }