1 | /* Application Class |
2 | * |
3 | * Demonstrates a simple application. |
4 | * |
5 | * This example uses GtkApplication, GtkApplicationWindow, GtkBuilder |
6 | * as well as GMenu and GResource. Due to the way GtkApplication is structured, |
7 | * it is run as a separate process. |
8 | */ |
9 | |
10 | #include "config.h" |
11 | |
12 | #include <gtk/gtk.h> |
13 | |
14 | static gboolean name_seen; |
15 | static GtkWidget *placeholder; |
16 | |
17 | static void |
18 | on_name_appeared (GDBusConnection *connection, |
19 | const char *name, |
20 | const char *name_owner, |
21 | gpointer user_data) |
22 | { |
23 | name_seen = TRUE; |
24 | } |
25 | |
26 | static void |
27 | on_name_vanished (GDBusConnection *connection, |
28 | const char *name, |
29 | gpointer user_data) |
30 | { |
31 | if (!name_seen) |
32 | return; |
33 | |
34 | g_clear_object (&placeholder); |
35 | } |
36 | |
37 | #ifdef G_OS_WIN32 |
38 | #define APP_EXTENSION ".exe" |
39 | #else |
40 | #define APP_EXTENSION |
41 | #endif |
42 | |
43 | GtkWidget * |
44 | do_application_demo (GtkWidget *toplevel) |
45 | { |
46 | static guint watch = 0; |
47 | |
48 | if (watch == 0) |
49 | watch = g_bus_watch_name (bus_type: G_BUS_TYPE_SESSION, |
50 | name: "org.gtk.Demo4.App" , |
51 | flags: 0, |
52 | name_appeared_handler: on_name_appeared, |
53 | name_vanished_handler: on_name_vanished, |
54 | NULL, NULL); |
55 | |
56 | if (placeholder == NULL) |
57 | { |
58 | const char *command; |
59 | GError *error = NULL; |
60 | |
61 | if (g_file_test (filename: "./gtk4-demo-application" APP_EXTENSION, test: G_FILE_TEST_IS_EXECUTABLE)) |
62 | command = "./gtk4-demo-application" APP_EXTENSION; |
63 | else |
64 | command = "gtk4-demo-application" ; |
65 | |
66 | if (!g_spawn_command_line_async (command_line: command, error: &error)) |
67 | { |
68 | g_warning ("%s" , error->message); |
69 | g_error_free (error); |
70 | } |
71 | |
72 | placeholder = gtk_label_new (str: "" ); |
73 | g_object_ref_sink (placeholder); |
74 | } |
75 | else |
76 | { |
77 | g_dbus_connection_call_sync (connection: g_bus_get_sync (bus_type: G_BUS_TYPE_SESSION, NULL, NULL), |
78 | bus_name: "org.gtk.Demo4.App" , |
79 | object_path: "/org/gtk/Demo4/App" , |
80 | interface_name: "org.gtk.Actions" , |
81 | method_name: "Activate" , |
82 | parameters: g_variant_new (format_string: "(sava{sv})" , "quit" , NULL, NULL), |
83 | NULL, |
84 | flags: 0, |
85 | G_MAXINT, |
86 | NULL, NULL); |
87 | } |
88 | |
89 | return placeholder; |
90 | } |
91 | |