exampleapp.c 2.91 KB
Newer Older
Matthias Clasen's avatar
Matthias Clasen committed
1
2
3
4
5
6
#include <gtk/gtk.h>

#include "exampleapp.h"
#include "exampleappwin.h"
#include "exampleappprefs.h"

7
struct _ExampleApp
Matthias Clasen's avatar
Matthias Clasen committed
8
9
{
  GtkApplication parent;
Matthias Clasen's avatar
Matthias Clasen committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
};

G_DEFINE_TYPE(ExampleApp, example_app, GTK_TYPE_APPLICATION);

static void
example_app_init (ExampleApp *app)
{
}

static void
preferences_activated (GSimpleAction *action,
                       GVariant      *parameter,
                       gpointer       app)
{
Matthias Clasen's avatar
Matthias Clasen committed
24
25
  ExampleAppPrefs *prefs;
  GtkWindow *win;
Matthias Clasen's avatar
Matthias Clasen committed
26

Matthias Clasen's avatar
Matthias Clasen committed
27
28
29
  win = gtk_application_get_active_window (GTK_APPLICATION (app));
  prefs = example_app_prefs_new (EXAMPLE_APP_WINDOW (win));
  gtk_window_present (GTK_WINDOW (prefs));
Matthias Clasen's avatar
Matthias Clasen committed
30
31
32
33
34
35
36
37
38
39
}

static void
quit_activated (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       app)
{
  g_application_quit (G_APPLICATION (app));
}

Matthias Clasen's avatar
Matthias Clasen committed
40
41
42
43
static GActionEntry app_entries[] =
{
  { "preferences", preferences_activated, NULL, NULL, NULL },
  { "quit", quit_activated, NULL, NULL, NULL }
Matthias Clasen's avatar
Matthias Clasen committed
44
45
46
47
48
};

static void
example_app_startup (GApplication *app)
{
Matthias Clasen's avatar
Matthias Clasen committed
49
50
  GtkBuilder *builder;
  GMenuModel *app_menu;
51
  const gchar *quit_accels[2] = { "<Ctrl>Q", NULL };
Matthias Clasen's avatar
Matthias Clasen committed
52

Matthias Clasen's avatar
Matthias Clasen committed
53
  G_APPLICATION_CLASS (example_app_parent_class)->startup (app);
Matthias Clasen's avatar
Matthias Clasen committed
54

Matthias Clasen's avatar
Matthias Clasen committed
55
56
57
  g_action_map_add_action_entries (G_ACTION_MAP (app),
                                   app_entries, G_N_ELEMENTS (app_entries),
                                   app);
58
59
60
  gtk_application_set_accels_for_action (GTK_APPLICATION (app),
                                         "app.quit",
                                         quit_accels);
Matthias Clasen's avatar
Matthias Clasen committed
61

Matthias Clasen's avatar
Matthias Clasen committed
62
63
64
65
  builder = gtk_builder_new_from_resource ("/org/gtk/exampleapp/app-menu.ui");
  app_menu = G_MENU_MODEL (gtk_builder_get_object (builder, "appmenu"));
  gtk_application_set_app_menu (GTK_APPLICATION (app), app_menu);
  g_object_unref (builder);
Matthias Clasen's avatar
Matthias Clasen committed
66
67
68
69
70
}

static void
example_app_activate (GApplication *app)
{
Matthias Clasen's avatar
Matthias Clasen committed
71
  ExampleAppWindow *win;
Matthias Clasen's avatar
Matthias Clasen committed
72

Matthias Clasen's avatar
Matthias Clasen committed
73
74
  win = example_app_window_new (EXAMPLE_APP (app));
  gtk_window_present (GTK_WINDOW (win));
Matthias Clasen's avatar
Matthias Clasen committed
75
76
77
78
79
80
81
82
}

static void
example_app_open (GApplication  *app,
                  GFile        **files,
                  gint           n_files,
                  const gchar   *hint)
{
Matthias Clasen's avatar
Matthias Clasen committed
83
84
85
  GList *windows;
  ExampleAppWindow *win;
  int i;
Matthias Clasen's avatar
Matthias Clasen committed
86

Matthias Clasen's avatar
Matthias Clasen committed
87
88
89
90
91
  windows = gtk_application_get_windows (GTK_APPLICATION (app));
  if (windows)
    win = EXAMPLE_APP_WINDOW (windows->data);
  else
    win = example_app_window_new (EXAMPLE_APP (app));
Matthias Clasen's avatar
Matthias Clasen committed
92

Matthias Clasen's avatar
Matthias Clasen committed
93
94
  for (i = 0; i < n_files; i++)
    example_app_window_open (win, files[i]);
Matthias Clasen's avatar
Matthias Clasen committed
95

Matthias Clasen's avatar
Matthias Clasen committed
96
  gtk_window_present (GTK_WINDOW (win));
Matthias Clasen's avatar
Matthias Clasen committed
97
98
99
100
101
}

static void
example_app_class_init (ExampleAppClass *class)
{
Matthias Clasen's avatar
Matthias Clasen committed
102
103
104
  G_APPLICATION_CLASS (class)->startup = example_app_startup;
  G_APPLICATION_CLASS (class)->activate = example_app_activate;
  G_APPLICATION_CLASS (class)->open = example_app_open;
Matthias Clasen's avatar
Matthias Clasen committed
105
106
107
108
109
}

ExampleApp *
example_app_new (void)
{
Matthias Clasen's avatar
Matthias Clasen committed
110
111
112
113
  return g_object_new (EXAMPLE_APP_TYPE,
                       "application-id", "org.gtk.exampleapp",
                       "flags", G_APPLICATION_HANDLES_OPEN,
                       NULL);
Matthias Clasen's avatar
Matthias Clasen committed
114
}