| 1 |
#include <gio/gio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
|
| 5 |
static void |
| 6 |
activate (GApplication *application) |
| 7 |
{ |
| 8 |
g_application_hold (application); |
| 9 |
g_print ("activated\n"); |
| 10 |
g_application_release (application); |
| 11 |
} |
| 12 |
|
| 13 |
static void |
| 14 |
activate_action (GAction *action, |
| 15 |
GVariant *parameter, |
| 16 |
gpointer data) |
| 17 |
{ |
| 18 |
GApplication *application = G_APPLICATION (data); |
| 19 |
|
| 20 |
g_application_hold (application); |
| 21 |
g_print ("action %s activated\n", g_action_get_name (action)); |
| 22 |
g_application_release (application); |
| 23 |
} |
| 24 |
|
| 25 |
static void |
| 26 |
activate_toggle_action (GSimpleAction *action, |
| 27 |
GVariant *parameter, |
| 28 |
gpointer data) |
| 29 |
{ |
| 30 |
GApplication *application = G_APPLICATION (data); |
| 31 |
GVariant *state; |
| 32 |
gboolean b; |
| 33 |
|
| 34 |
g_print ("action %s activated\n", g_action_get_name (G_ACTION (action))); |
| 35 |
|
| 36 |
g_application_hold (application); |
| 37 |
state = g_action_get_state (G_ACTION (action)); |
| 38 |
b = g_variant_get_boolean (state); |
| 39 |
g_variant_unref (state); |
| 40 |
g_simple_action_set_state (action, g_variant_new_boolean (!b)); |
| 41 |
g_print ("state change %d -> %d\n", b, !b); |
| 42 |
g_application_release (application); |
| 43 |
} |
| 44 |
|
| 45 |
static void |
| 46 |
add_actions (GApplication *app) |
| 47 |
{ |
| 48 |
GSimpleAction *action; |
| 49 |
|
| 50 |
action = g_simple_action_new ("simple-action", NULL); |
| 51 |
g_signal_connect (action, "activate", G_CALLBACK (activate_action), app); |
| 52 |
g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action)); |
| 53 |
g_object_unref (action); |
| 54 |
|
| 55 |
action = g_simple_action_new_stateful ("toggle-action", NULL, |
| 56 |
g_variant_new_boolean (FALSE)); |
| 57 |
g_signal_connect (action, "activate", G_CALLBACK (activate_toggle_action), app); |
| 58 |
g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action)); |
| 59 |
g_object_unref (action); |
| 60 |
} |
| 61 |
|
| 62 |
static void |
| 63 |
describe_and_activate_action (GActionGroup *group, |
| 64 |
const gchar *name) |
| 65 |
{ |
| 66 |
const GVariantType *param_type; |
| 67 |
GVariant *state; |
| 68 |
gboolean enabled; |
| 69 |
gchar *tmp; |
| 70 |
|
| 71 |
param_type = g_action_group_get_action_parameter_type (group, name); |
| 72 |
state = g_action_group_get_action_state (group, name); |
| 73 |
enabled = g_action_group_get_action_enabled (group, name); |
| 74 |
|
| 75 |
g_print ("action name: %s\n", name); |
| 76 |
tmp = param_type ? g_variant_type_dup_string (param_type) : NULL; |
| 77 |
g_print ("parameter type: %s\n", tmp ? tmp : "<none>"); |
| 78 |
g_free (tmp); |
| 79 |
g_print ("state type: %s\n", |
| 80 |
state ? g_variant_get_type_string (state) : "<none>"); |
| 81 |
tmp = state ? g_variant_print (state, FALSE) : NULL; |
| 82 |
g_print ("state: %s\n", tmp ? tmp : "<none>"); |
| 83 |
g_free (tmp); |
| 84 |
g_print ("enabled: %s\n", enabled ? "true" : "false"); |
| 85 |
|
| 86 |
if (state != NULL) |
| 87 |
g_variant_unref (state); |
| 88 |
|
| 89 |
g_action_group_activate_action (group, name, NULL); |
| 90 |
} |
| 91 |
|
| 92 |
int |
| 93 |
main (int argc, char **argv) |
| 94 |
{ |
| 95 |
GApplication *app; |
| 96 |
int status; |
| 97 |
|
| 98 |
app = g_application_new ("org.gtk.TestApplication", 0); |
| 99 |
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); |
| 100 |
g_application_set_inactivity_timeout (app, 10000); |
| 101 |
|
| 102 |
add_actions (app); |
| 103 |
|
| 104 |
if (argc > 1 && strcmp (argv[1], "--simple-action") == 0) |
| 105 |
{ |
| 106 |
g_application_register (app, NULL, NULL); |
| 107 |
describe_and_activate_action (G_ACTION_GROUP (app), "simple-action"); |
| 108 |
exit (0); |
| 109 |
} |
| 110 |
else if (argc > 1 && strcmp (argv[1], "--toggle-action") == 0) |
| 111 |
{ |
| 112 |
g_application_register (app, NULL, NULL); |
| 113 |
describe_and_activate_action (G_ACTION_GROUP (app), "toggle-action"); |
| 114 |
exit (0); |
| 115 |
} |
| 116 |
|
| 117 |
status = g_application_run (app, argc, argv); |
| 118 |
|
| 119 |
g_object_unref (app); |
| 120 |
|
| 121 |
return status; |
| 122 |
} |