gtkapplication.c 40.4 KB
Newer Older
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1
2
/*
 * Copyright © 2010 Codethink Limited
Allison Karlitskaya's avatar
Allison Karlitskaya committed
3
 * Copyright © 2013 Canonical Limited
Matthias Clasen's avatar
Matthias Clasen committed
4
5
6
7
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
Allison Karlitskaya's avatar
Allison Karlitskaya committed
8
 * version 2 of the licence, or (at your option) any later version.
Matthias Clasen's avatar
Matthias Clasen committed
9
10
11
12
13
14
15
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Matthias Clasen's avatar
Matthias Clasen committed
17
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
18
 * Author: Ryan Lortie <desrt@desrt.ca>
Matthias Clasen's avatar
Matthias Clasen committed
19
20
21
22
 */

#include "config.h"

23
#include "gtkapplication.h"
24
25
26
27
28
#include "gdkprofilerprivate.h"

#ifdef G_OS_UNIX
#include <gio/gunixfdlist.h>
#endif
29

Matthias Clasen's avatar
Matthias Clasen committed
30
#include <stdlib.h>
31
32

#ifdef HAVE_UNISTD_H
Matthias Clasen's avatar
Matthias Clasen committed
33
#include <unistd.h>
34
35
#endif

36
#include "gtkapplicationprivate.h"
37
#include "gtkmarshalers.h"
Matthias Clasen's avatar
Matthias Clasen committed
38
#include "gtkmain.h"
39
#include "gtkrecentmanager.h"
40
#include "gtkicontheme.h"
Allison Karlitskaya's avatar
Allison Karlitskaya committed
41
#include "gtkbuilder.h"
42
#include "gtkshortcutswindow.h"
43
#include "gtkprivate.h"
44

Allison Karlitskaya's avatar
Allison Karlitskaya committed
45
46
47
/* NB: please do not add backend-specific GDK headers here.  This should
 * be abstracted via GtkApplicationImpl.
 */
Matthias Clasen's avatar
Matthias Clasen committed
48
49

/**
Matthias Clasen's avatar
Matthias Clasen committed
50
 * GtkApplication:
Matthias Clasen's avatar
Matthias Clasen committed
51
 *
Matthias Clasen's avatar
Matthias Clasen committed
52
53
54
55
 * `GtkApplication` is a high-level API for writing applications.
 *
 * It supports many aspects of writing a GTK application in a convenient
 * fashion, without enforcing a one-size-fits-all model.
Matthias Clasen's avatar
Matthias Clasen committed
56
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
57
 * Currently, `GtkApplication` handles GTK initialization, application
Matthias Clasen's avatar
Matthias Clasen committed
58
59
60
61
 * uniqueness, session management, provides some basic scriptability and
 * desktop shell integration by exporting actions and menus and manages a
 * list of toplevel windows whose life-cycle is automatically tied to the
 * life-cycle of your application.
62
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
63
64
 * While `GtkApplication` works fine with plain [class@Gtk.Window]s, it is
 * recommended to use it together with [class@Gtk.ApplicationWindow].
Matthias Clasen's avatar
Matthias Clasen committed
65
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
66
 * ## Automatic resources
67
 *
Matthias Clasen's avatar
Matthias Clasen committed
68
 * `GtkApplication` will automatically load menus from the `GtkBuilder`
69
 * resource located at "gtk/menus.ui", relative to the application's
70
 * resource base path (see [method@Gio.Application.set_resource_base_path]).
71
 * The menu with the ID "menubar" is taken as the application's
Emmanuele Bassi's avatar
Emmanuele Bassi committed
72
73
 * menubar. Additional menus (most interesting submenus) can be named
 * and accessed via [method@Gtk.Application.get_menu_by_id] which allows for
74
75
 * dynamic population of a part of the menu structure.
 *
76
 * It is also possible to provide the menubar manually using
Emmanuele Bassi's avatar
Emmanuele Bassi committed
77
 * [method@Gtk.Application.set_menubar].
Matthias Clasen's avatar
Matthias Clasen committed
78
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
79
 * `GtkApplication` will also automatically setup an icon search path for
80
 * the default icon theme by appending "icons" to the resource base
Emmanuele Bassi's avatar
Emmanuele Bassi committed
81
82
 * path. This allows your application to easily store its icons as
 * resources. See [method@Gtk.IconTheme.add_resource_path] for more
83
84
 * information.
 *
85
86
 * If there is a resource located at `gtk/help-overlay.ui` which
 * defines a [class@Gtk.ShortcutsWindow] with ID `help_overlay` then
Matthias Clasen's avatar
Matthias Clasen committed
87
88
 * `GtkApplication` associates an instance of this shortcuts window with
 * each [class@Gtk.ApplicationWindow] and sets up the keyboard accelerator
Emmanuele Bassi's avatar
Emmanuele Bassi committed
89
90
91
 * <kbd>Control</kbd>+<kbd>?</kbd> to open it. To create a menu item that
 * displays the shortcuts window, associate the item with the action
 * `win.show-help-overlay`.
92
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
93
 * ## A simple application
94
 *
95
 * [A simple example](https://gitlab.gnome.org/GNOME/gtk/tree/main/examples/bp/bloatpad.c)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
96
 * is available in the GTK source code repository
Matthias Clasen's avatar
Matthias Clasen committed
97
 *
Matthias Clasen's avatar
Matthias Clasen committed
98
99
 * `GtkApplication` optionally registers with a session manager of the
 * users session (if you set the [property@Gtk.Application:register-session]
Matthias Clasen's avatar
Matthias Clasen committed
100
101
102
103
 * property) and offers various functionality related to the session
 * life-cycle.
 *
 * An application can block various ways to end the session with
Emmanuele Bassi's avatar
Emmanuele Bassi committed
104
 * the [method@Gtk.Application.inhibit] function. Typical use cases for
Matthias Clasen's avatar
Matthias Clasen committed
105
106
107
108
109
 * this kind of inhibiting are long-running, uninterruptible operations,
 * such as burning a CD or performing a disk backup. The session
 * manager may not honor the inhibitor, but it can be expected to
 * inform the user about the negative consequences of ending the
 * session while inhibitors are present.
110
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
111
112
 * ## See Also
 *
113
 * [HowDoI: Using GtkApplication](https://wiki.gnome.org/HowDoI/GtkApplication),
Emmanuele Bassi's avatar
Emmanuele Bassi committed
114
 * [Getting Started with GTK: Basics](getting_started.html#basics)
Matthias Clasen's avatar
Matthias Clasen committed
115
116
 */

117
118
119
enum {
  WINDOW_ADDED,
  WINDOW_REMOVED,
120
  QUERY_END,
121
122
123
124
125
  LAST_SIGNAL
};

static guint gtk_application_signals[LAST_SIGNAL];

126
127
enum {
  PROP_ZERO,
128
  PROP_REGISTER_SESSION,
129
  PROP_SCREENSAVER_ACTIVE,
130
  PROP_MENUBAR,
131
132
  PROP_ACTIVE_WINDOW,
  NUM_PROPERTIES
133
134
};

135
136
static GParamSpec *gtk_application_props[NUM_PROPERTIES];

137
typedef struct
Matthias Clasen's avatar
Matthias Clasen committed
138
{
Allison Karlitskaya's avatar
Allison Karlitskaya committed
139
  GtkApplicationImpl *impl;
140
  GtkApplicationAccels *accels;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
141

142
  GList *windows;
143

144
  GMenuModel      *menubar;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
145
  guint            last_window_id;
146

147
  gboolean         register_session;
148
  gboolean         screensaver_active;
149
  GtkActionMuxer  *muxer;
150
  GtkBuilder      *menus_builder;
Benjamin Otte's avatar
Benjamin Otte committed
151
  char            *help_overlay_path;
152
} GtkApplicationPrivate;
Matthias Clasen's avatar
Matthias Clasen committed
153

154
155
G_DEFINE_TYPE_WITH_PRIVATE (GtkApplication, gtk_application, G_TYPE_APPLICATION)

156
157
158
159
static void
gtk_application_window_active_cb (GtkWindow      *window,
                                  GParamSpec     *pspec,
                                  GtkApplication *application)
160
{
161
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);
162
163
  GList *link;

164
165
166
  if (!gtk_window_is_active (window))
    return;

167
168
169
170
171
172
173
174
  /* Keep the window list sorted by most-recently-focused. */
  link = g_list_find (priv->windows, window);
  if (link != NULL && link != priv->windows)
    {
      priv->windows = g_list_remove_link (priv->windows, link);
      priv->windows = g_list_concat (link, priv->windows);
    }

175
176
  if (priv->impl)
    gtk_application_impl_active_window_changed (priv->impl, window);
177

178
  g_object_notify_by_pspec (G_OBJECT (application), gtk_application_props[PROP_ACTIVE_WINDOW]);
179
180
}

181
182
183
static void
gtk_application_load_resources (GtkApplication *application)
{
184
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);
Benjamin Otte's avatar
Benjamin Otte committed
185
  const char *base_path;
186
  const char *optional_slash = "/";
187
188
189
190
191
192

  base_path = g_application_get_resource_base_path (G_APPLICATION (application));

  if (base_path == NULL)
    return;

193
194
195
  if (base_path[strlen (base_path) - 1] == '/')
    optional_slash = "";

196
197
198
  /* Expand the icon search path */
  {
    GtkIconTheme *default_theme;
Benjamin Otte's avatar
Benjamin Otte committed
199
    char *iconspath;
200

201
    default_theme = gtk_icon_theme_get_for_display (gdk_display_get_default ());
202
    iconspath = g_strconcat (base_path, optional_slash, "icons/", NULL);
203
204
205
206
    gtk_icon_theme_add_resource_path (default_theme, iconspath);
    g_free (iconspath);
  }

207
208
  /* Load the menus */
  {
Benjamin Otte's avatar
Benjamin Otte committed
209
    char *menuspath;
210

211
    menuspath = g_strconcat (base_path, optional_slash, "gtk/menus.ui", NULL);
212
    if (g_resources_get_info (menuspath, G_RESOURCE_LOOKUP_FLAGS_NONE, NULL, NULL, NULL))
213
      priv->menus_builder = gtk_builder_new_from_resource (menuspath);
214
215
    g_free (menuspath);

216
    if (priv->menus_builder)
217
218
219
      {
        GObject *menu;

220
        menu = gtk_builder_get_object (priv->menus_builder, "menubar");
221
222
223
224
        if (menu != NULL && G_IS_MENU_MODEL (menu))
          gtk_application_set_menubar (application, G_MENU_MODEL (menu));
      }
  }
225
226
227

  /* Help overlay */
  {
Benjamin Otte's avatar
Benjamin Otte committed
228
    char *path;
229

230
    path = g_strconcat (base_path, optional_slash, "gtk/help-overlay.ui", NULL);
231
    if (g_resources_get_info (path, G_RESOURCE_LOOKUP_FLAGS_NONE, NULL, NULL, NULL))
Sébastien Wilmet's avatar
Sébastien Wilmet committed
232
      {
Benjamin Otte's avatar
Benjamin Otte committed
233
        const char * const accels[] = { "<Control>question", NULL };
234

235
        priv->help_overlay_path = path;
Sébastien Wilmet's avatar
Sébastien Wilmet committed
236
237
238
239
240
241
        gtk_application_set_accels_for_action (application, "win.show-help-overlay", accels);
      }
    else
      {
        g_free (path);
      }
242
  }
243
244
245
}


Matthias Clasen's avatar
Matthias Clasen committed
246
static void
247
gtk_application_startup (GApplication *g_application)
248
{
249
  GtkApplication *application = GTK_APPLICATION (g_application);
250
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);
251
252
253
254
  gint64 before G_GNUC_UNUSED;
  gint64 before2 G_GNUC_UNUSED;

  before = GDK_PROFILER_CURRENT_TIME;
255

256
  G_APPLICATION_CLASS (gtk_application_parent_class)->startup (g_application);
257

258
  gtk_action_muxer_insert (priv->muxer, "app", G_ACTION_GROUP (application));
259

260
  before2 = GDK_PROFILER_CURRENT_TIME;
Matthias Clasen's avatar
Matthias Clasen committed
261
  gtk_init ();
262
  gdk_profiler_end_mark (before2, "gtk init", NULL);
263

264
265
  priv->impl = gtk_application_impl_new (application, gdk_display_get_default ());
  gtk_application_impl_startup (priv->impl, priv->register_session);
266
267

  gtk_application_load_resources (application);
268

269
  gdk_profiler_end_mark (before, "gtk application startup", NULL);
270
271
}

272
static void
Allison Karlitskaya's avatar
Allison Karlitskaya committed
273
gtk_application_shutdown (GApplication *g_application)
274
{
Allison Karlitskaya's avatar
Allison Karlitskaya committed
275
  GtkApplication *application = GTK_APPLICATION (g_application);
276
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);
277

278
  if (priv->impl == NULL)
279
280
    return;

281
282
  gtk_application_impl_shutdown (priv->impl);
  g_clear_object (&priv->impl);
283

284
  gtk_action_muxer_remove (priv->muxer, "app");
285

286
  gtk_main_sync ();
287

288
  G_APPLICATION_CLASS (gtk_application_parent_class)->shutdown (g_application);
289
290
}

291
292
static gboolean
gtk_application_local_command_line (GApplication   *application,
Benjamin Otte's avatar
Benjamin Otte committed
293
                                    char         ***arguments,
Benjamin Otte's avatar
Benjamin Otte committed
294
                                    int            *exit_status)
295
{
296
297
298
  /* We need to call setlocale() here so --help output works */
  setlocale_initialization ();

299
300
301
  return G_APPLICATION_CLASS (gtk_application_parent_class)->local_command_line (application, arguments, exit_status);
}

Matthias Clasen's avatar
Matthias Clasen committed
302
static void
Allison Karlitskaya's avatar
Allison Karlitskaya committed
303
304
gtk_application_add_platform_data (GApplication    *application,
                                   GVariantBuilder *builder)
Matthias Clasen's avatar
Matthias Clasen committed
305
{
306
  GdkDisplay *display;
Matthias Clasen's avatar
Matthias Clasen committed
307

Allison Karlitskaya's avatar
Allison Karlitskaya committed
308
309
310
311
312
313
314
  /* This is slightly evil.
   *
   * We don't have an impl here because we're remote so we can't figure
   * out what to do on a per-display-server basis.
   *
   * So we do all the things... which currently is just one thing.
   */
315
316
317
  display = gdk_display_get_default ();
  if (display)
    {
Benjamin Otte's avatar
Benjamin Otte committed
318
      const char *startup_id;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
319

320
321
322
323
324
      startup_id = gdk_display_get_startup_notification_id (display);
      if (startup_id && g_utf8_validate (startup_id, -1, NULL))
        g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
                               g_variant_new_string (startup_id));
    }
Matthias Clasen's avatar
Matthias Clasen committed
325
326
327
}

static void
Allison Karlitskaya's avatar
Allison Karlitskaya committed
328
gtk_application_before_emit (GApplication *g_application,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
329
                             GVariant     *platform_data)
Matthias Clasen's avatar
Matthias Clasen committed
330
{
Allison Karlitskaya's avatar
Allison Karlitskaya committed
331
  GtkApplication *application = GTK_APPLICATION (g_application);
332
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);
Matthias Clasen's avatar
Matthias Clasen committed
333

334
  gtk_application_impl_before_emit (priv->impl, platform_data);
Matthias Clasen's avatar
Matthias Clasen committed
335
336
337
}

static void
Allison Karlitskaya's avatar
Allison Karlitskaya committed
338
339
gtk_application_after_emit (GApplication *application,
                            GVariant     *platform_data)
Matthias Clasen's avatar
Matthias Clasen committed
340
{
341
342
343
344
345
346
347
348
349
350
351
  const char *startup_notification_id = NULL;

  g_variant_lookup (platform_data, "desktop-startup-id", "&s", &startup_notification_id);
  if (startup_notification_id)
    {
      GdkDisplay *display;

      display = gdk_display_get_default ();
      if (display)
        gdk_display_notify_startup_complete (display, startup_notification_id);
    }
Matthias Clasen's avatar
Matthias Clasen committed
352
353
354
355
356
}

static void
gtk_application_init (GtkApplication *application)
{
357
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);
358

359
  priv->muxer = gtk_action_muxer_new (NULL);
360
361

  priv->accels = gtk_application_accels_new ();
Matthias Clasen's avatar
Matthias Clasen committed
362
363
}

364
365
366
367
static void
gtk_application_window_added (GtkApplication *application,
                              GtkWindow      *window)
{
368
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);
369

Allison Karlitskaya's avatar
Allison Karlitskaya committed
370
  if (GTK_IS_APPLICATION_WINDOW (window))
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    {
      gtk_application_window_set_id (GTK_APPLICATION_WINDOW (window), ++priv->last_window_id);
      if (priv->help_overlay_path)
        {
          GtkBuilder *builder;
          GtkWidget *help_overlay;

          builder = gtk_builder_new_from_resource (priv->help_overlay_path);
          help_overlay = GTK_WIDGET (gtk_builder_get_object (builder, "help_overlay"));
          if (GTK_IS_SHORTCUTS_WINDOW (help_overlay))
            gtk_application_window_set_help_overlay (GTK_APPLICATION_WINDOW (window),
                                                     GTK_SHORTCUTS_WINDOW (help_overlay));
          g_object_unref (builder);
        }
    }
Allison Karlitskaya's avatar
Allison Karlitskaya committed
386

387
388
389
390
  priv->windows = g_list_prepend (priv->windows, window);
  gtk_window_set_application (window, application);
  g_application_hold (G_APPLICATION (application));

391
392
  g_signal_connect (window, "notify::is-active",
                    G_CALLBACK (gtk_application_window_active_cb),
393
                    application);
394

395
396
397
  gtk_application_impl_window_added (priv->impl, window);

  gtk_application_impl_active_window_changed (priv->impl, window);
398

399
  g_object_notify_by_pspec (G_OBJECT (application), gtk_application_props[PROP_ACTIVE_WINDOW]);
400
401
402
403
404
405
}

static void
gtk_application_window_removed (GtkApplication *application,
                                GtkWindow      *window)
{
406
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);
407
408
409
  gpointer old_active;

  old_active = priv->windows;
410

411
412
  if (priv->impl)
    gtk_application_impl_window_removed (priv->impl, window);
413

414
  g_signal_handlers_disconnect_by_func (window,
415
                                        gtk_application_window_active_cb,
416
417
418
419
                                        application);

  g_application_release (G_APPLICATION (application));
  priv->windows = g_list_remove (priv->windows, window);
420
  gtk_window_set_application (window, NULL);
421

422
  if (priv->windows != old_active && priv->impl)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
423
    {
424
      gtk_application_impl_active_window_changed (priv->impl, priv->windows ? priv->windows->data : NULL);
425
      g_object_notify_by_pspec (G_OBJECT (application), gtk_application_props[PROP_ACTIVE_WINDOW]);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
426
    }
427
428
}

429
430
431
432
433
434
435
static void
gtk_application_get_property (GObject    *object,
                              guint       prop_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  GtkApplication *application = GTK_APPLICATION (object);
436
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);
437
438
439
440

  switch (prop_id)
    {
    case PROP_REGISTER_SESSION:
441
      g_value_set_boolean (value, priv->register_session);
442
443
      break;

444
445
446
447
    case PROP_SCREENSAVER_ACTIVE:
      g_value_set_boolean (value, priv->screensaver_active);
      break;

448
449
450
451
    case PROP_MENUBAR:
      g_value_set_object (value, gtk_application_get_menubar (application));
      break;

452
453
454
455
    case PROP_ACTIVE_WINDOW:
      g_value_set_object (value, gtk_application_get_active_window (application));
      break;

456
457
458
459
460
461
462
463
464
465
466
467
468
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_application_set_property (GObject      *object,
                              guint         prop_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
  GtkApplication *application = GTK_APPLICATION (object);
469
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);
470
471
472
473

  switch (prop_id)
    {
    case PROP_REGISTER_SESSION:
474
      priv->register_session = g_value_get_boolean (value);
475
476
      break;

477
478
479
480
    case PROP_MENUBAR:
      gtk_application_set_menubar (application, g_value_get_object (value));
      break;

481
482
483
484
485
486
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

487
488
489
490
static void
gtk_application_finalize (GObject *object)
{
  GtkApplication *application = GTK_APPLICATION (object);
491
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);
492

493
494
495
496
  g_clear_object (&priv->menus_builder);
  g_clear_object (&priv->menubar);
  g_clear_object (&priv->muxer);
  g_clear_object (&priv->accels);
497

498
  g_free (priv->help_overlay_path);
499
500

  G_OBJECT_CLASS (gtk_application_parent_class)->finalize (object);
501
502
}

503
504
505
static gboolean
gtk_application_dbus_register (GApplication     *application,
                               GDBusConnection  *connection,
Yuri Chornoivan's avatar
Yuri Chornoivan committed
506
                               const char       *object_path,
507
508
509
510
511
512
513
514
                               GError          **error)
{
  return TRUE;
}

static void
gtk_application_dbus_unregister (GApplication     *application,
                                 GDBusConnection  *connection,
Yuri Chornoivan's avatar
Yuri Chornoivan committed
515
                                 const char       *object_path)
516
517
518
{
}

Matthias Clasen's avatar
Matthias Clasen committed
519
static void
Allison Karlitskaya's avatar
Allison Karlitskaya committed
520
gtk_application_class_init (GtkApplicationClass *class)
Matthias Clasen's avatar
Matthias Clasen committed
521
{
522
  GObjectClass *object_class = G_OBJECT_CLASS (class);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
523
  GApplicationClass *application_class = G_APPLICATION_CLASS (class);
Matthias Clasen's avatar
Matthias Clasen committed
524

525
526
  object_class->get_property = gtk_application_get_property;
  object_class->set_property = gtk_application_set_property;
527
  object_class->finalize = gtk_application_finalize;
528

529
  application_class->local_command_line = gtk_application_local_command_line;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
530
531
532
533
  application_class->add_platform_data = gtk_application_add_platform_data;
  application_class->before_emit = gtk_application_before_emit;
  application_class->after_emit = gtk_application_after_emit;
  application_class->startup = gtk_application_startup;
534
  application_class->shutdown = gtk_application_shutdown;
535
536
  application_class->dbus_register = gtk_application_dbus_register;
  application_class->dbus_unregister = gtk_application_dbus_unregister;
537

538
539
540
541
542
  class->window_added = gtk_application_window_added;
  class->window_removed = gtk_application_window_removed;

  /**
   * GtkApplication::window-added:
Emmanuele Bassi's avatar
Emmanuele Bassi committed
543
544
   * @application: the `GtkApplication` which emitted the signal
   * @window: the newly-added [class@Gtk.Window]
545
   *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
546
547
   * Emitted when a [class@Gtk.Window] is added to `application` through
   * [method@Gtk.Application.add_window].
548
549
   */
  gtk_application_signals[WINDOW_ADDED] =
550
    g_signal_new (I_("window-added"), GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
551
552
                  G_STRUCT_OFFSET (GtkApplicationClass, window_added),
                  NULL, NULL,
553
                  NULL,
554
555
556
557
                  G_TYPE_NONE, 1, GTK_TYPE_WINDOW);

  /**
   * GtkApplication::window-removed:
Emmanuele Bassi's avatar
Emmanuele Bassi committed
558
559
   * @application: the `GtkApplication` which emitted the signal
   * @window: the [class@Gtk.Window] that is being removed
560
   *
Matthias Clasen's avatar
Matthias Clasen committed
561
562
563
564
   * Emitted when a [class@Gtk.Window] is removed from `application`.
   *
   * This can happen as a side-effect of the window being destroyed
   * or explicitly through [method@Gtk.Application.remove_window].
565
566
   */
  gtk_application_signals[WINDOW_REMOVED] =
567
    g_signal_new (I_("window-removed"), GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
568
569
                  G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
                  NULL, NULL,
570
                  NULL,
571
                  G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
572

573
574
  /**
   * GtkApplication::query-end:
Emmanuele Bassi's avatar
Emmanuele Bassi committed
575
   * @application: the `GtkApplication` which emitted the signal
576
   *
Matthias Clasen's avatar
Matthias Clasen committed
577
578
579
580
581
582
   * Emitted when the session manager is about to end the session.
   *
   * This signal is only emitted if [property@Gtk.Application:register-session]
   * is `TRUE`. Applications can connect to this signal and call
   * [method@Gtk.Application.inhibit] with `GTK_APPLICATION_INHIBIT_LOGOUT`
   * to delay the end of the session until state has been saved.
583
584
585
586
587
588
589
   */
  gtk_application_signals[QUERY_END] =
    g_signal_new (I_("query-end"), GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
                  0,
                  NULL, NULL,
                  NULL,
                  G_TYPE_NONE, 0);
Matthias Clasen's avatar
Matthias Clasen committed
590

591
  /**
592
   * GtkApplication:register-session:
593
   *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
594
   * Set this property to `TRUE` to register with the session manager.
Matthias Clasen's avatar
Matthias Clasen committed
595
   *
596
   * This will make GTK track the session state (such as the
Emmanuele Bassi's avatar
Emmanuele Bassi committed
597
   * [property@Gtk.Application:screensaver-active] property).
598
   */
599
  gtk_application_props[PROP_REGISTER_SESSION] =
600
    g_param_spec_boolean ("register-session", NULL, NULL,
601
602
                          FALSE,
                          G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS);
603

604
605
606
  /**
   * GtkApplication:screensaver-active:
   *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
607
   * This property is `TRUE` if GTK believes that the screensaver is
Matthias Clasen's avatar
Matthias Clasen committed
608
609
610
611
   * currently active.
   *
   * GTK only tracks session state (including this) when
   * [property@Gtk.Application:register-session] is set to %TRUE.
612
   *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
613
614
   * Tracking the screensaver state is currently only supported on
   * Linux.
615
616
   */
  gtk_application_props[PROP_SCREENSAVER_ACTIVE] =
617
    g_param_spec_boolean ("screensaver-active", NULL, NULL,
618
619
620
                          FALSE,
                          G_PARAM_READABLE|G_PARAM_STATIC_STRINGS);

Emmanuele Bassi's avatar
Emmanuele Bassi committed
621
  /**
Matthias Clasen's avatar
Matthias Clasen committed
622
   * GtkApplication:menubar: (attributes org.gtk.Property.get=gtk_application_get_menubar org.gtk.Property.set=gtk_application_set_menubar)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
623
624
625
   *
   * The `GMenuModel` to be used for the application's menu bar.
   */
626
  gtk_application_props[PROP_MENUBAR] =
627
    g_param_spec_object ("menubar", NULL, NULL,
628
                         G_TYPE_MENU_MODEL,
629
                         G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS);
630

Emmanuele Bassi's avatar
Emmanuele Bassi committed
631
  /**
Matthias Clasen's avatar
Matthias Clasen committed
632
   * GtkApplication:active-window: (attributes org.gtk.Property.get=gtk_application_get_active_window)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
633
634
635
   *
   * The currently focused window of the application.
   */
636
  gtk_application_props[PROP_ACTIVE_WINDOW] =
637
    g_param_spec_object ("active-window", NULL, NULL,
638
                         GTK_TYPE_WINDOW,
639
640
641
                         G_PARAM_READABLE|G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, NUM_PROPERTIES, gtk_application_props);
642
643
}

Matthias Clasen's avatar
Matthias Clasen committed
644
645
/**
 * gtk_application_new:
Emmanuele Bassi's avatar
Emmanuele Bassi committed
646
 * @application_id: (nullable): The application ID
Matthias Clasen's avatar
Matthias Clasen committed
647
648
 * @flags: the application flags
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
649
 * Creates a new `GtkApplication` instance.
Matthias Clasen's avatar
Matthias Clasen committed
650
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
651
 * When using `GtkApplication`, it is not necessary to call [func@Gtk.init]
Matthias Clasen's avatar
Matthias Clasen committed
652
653
 * manually. It is called as soon as the application gets registered as
 * the primary instance.
Matthias Clasen's avatar
Matthias Clasen committed
654
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
655
656
657
658
 * Concretely, [func@Gtk.init] is called in the default handler for the
 * `GApplication::startup` signal. Therefore, `GtkApplication` subclasses should
 * always chain up in their `GApplication::startup` handler before using any GTK
 * API.
659
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
660
 * Note that commandline arguments are not passed to [func@Gtk.init].
661
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
662
663
 * If `application_id` is not %NULL, then it must be valid. See
 * `g_application_id_is_valid()`.
664
 *
665
 * If no application ID is given then some features (most notably application
666
 * uniqueness) will be disabled.
Matthias Clasen's avatar
Matthias Clasen committed
667
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
668
 * Returns: a new `GtkApplication` instance
Matthias Clasen's avatar
Matthias Clasen committed
669
 */
670
GtkApplication *
Benjamin Otte's avatar
Benjamin Otte committed
671
gtk_application_new (const char        *application_id,
672
673
                     GApplicationFlags  flags)
{
674
  g_return_val_if_fail (application_id == NULL || g_application_id_is_valid (application_id), NULL);
675
676
677
678
679
680
681
682

  return g_object_new (GTK_TYPE_APPLICATION,
                       "application-id", application_id,
                       "flags", flags,
                       NULL);
}

/**
Javier Jardón's avatar
Javier Jardón committed
683
 * gtk_application_add_window:
Emmanuele Bassi's avatar
Emmanuele Bassi committed
684
685
 * @application: a `GtkApplication`
 * @window: a `GtkWindow`
686
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
687
 * Adds a window to `application`.
688
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
689
 * This call can only happen after the `application` has started;
690
 * typically, you should add new application windows in response
Emmanuele Bassi's avatar
Emmanuele Bassi committed
691
 * to the emission of the `GApplication::activate` signal.
692
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
693
694
 * This call is equivalent to setting the [property@Gtk.Window:application]
 * property of `window` to `application`.
695
 *
696
697
 * Normally, the connection between the application and the window
 * will remain until the window is destroyed, but you can explicitly
Emmanuele Bassi's avatar
Emmanuele Bassi committed
698
 * remove it with [method@Gtk.Application.remove_window].
699
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
700
 * GTK will keep the `application` running as long as it has
701
 * any windows.
702
703
704
705
706
 **/
void
gtk_application_add_window (GtkApplication *application,
                            GtkWindow      *window)
{
707
708
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);

709
  g_return_if_fail (GTK_IS_APPLICATION (application));
Sébastien Wilmet's avatar
Sébastien Wilmet committed
710
  g_return_if_fail (GTK_IS_WINDOW (window));
711

712
713
714
715
716
717
718
  if (!g_application_get_is_registered (G_APPLICATION (application)))
    {
      g_critical ("New application windows must be added after the "
                  "GApplication::startup signal has been emitted.");
      return;
    }

719
  if (!g_list_find (priv->windows, window))
720
721
    g_signal_emit (application,
                   gtk_application_signals[WINDOW_ADDED], 0, window);
722
723
724
725
}

/**
 * gtk_application_remove_window:
Emmanuele Bassi's avatar
Emmanuele Bassi committed
726
727
 * @application: a `GtkApplication`
 * @window: a `GtkWindow`
728
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
729
 * Remove a window from `application`.
730
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
731
732
733
 * If `window` belongs to `application` then this call is equivalent to
 * setting the [property@Gtk.Window:application] property of `window` to
 * `NULL`.
734
735
 *
 * The application may stop running as a result of a call to this
Emmanuele Bassi's avatar
Emmanuele Bassi committed
736
 * function, if `window` was the last window of the `application`.
737
738
739
740
741
 **/
void
gtk_application_remove_window (GtkApplication *application,
                               GtkWindow      *window)
{
742
743
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);

744
  g_return_if_fail (GTK_IS_APPLICATION (application));
Sébastien Wilmet's avatar
Sébastien Wilmet committed
745
  g_return_if_fail (GTK_IS_WINDOW (window));
746

747
  if (g_list_find (priv->windows, window))
748
749
    g_signal_emit (application,
                   gtk_application_signals[WINDOW_REMOVED], 0, window);
750
751
752
753
}

/**
 * gtk_application_get_windows:
Emmanuele Bassi's avatar
Emmanuele Bassi committed
754
 * @application: a `GtkApplication`
755
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
756
 * Gets a list of the [class@Gtk.Window] instances associated with `application`.
757
 *
758
 * The list is sorted by most recently focused window, such that the first
Matthias Clasen's avatar
Matthias Clasen committed
759
 * element is the currently focused window. (Useful for choosing a parent
760
761
762
763
764
 * for a transient window.)
 *
 * The list that is returned should not be modified in any way. It will
 * only remain valid until the next focus change or window creation or
 * deletion.
765
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
766
767
 * Returns: (element-type GtkWindow) (transfer none): a `GList` of `GtkWindow`
 *   instances
768
769
770
771
 **/
GList *
gtk_application_get_windows (GtkApplication *application)
{
772
773
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);

774
775
  g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);

776
  return priv->windows;
Matthias Clasen's avatar
Matthias Clasen committed
777
}
778

779
780
/**
 * gtk_application_get_window_by_id:
Emmanuele Bassi's avatar
Emmanuele Bassi committed
781
 * @application: a `GtkApplication`
782
783
 * @id: an identifier number
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
784
 * Returns the [class@Gtk.ApplicationWindow] with the given ID.
785
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
786
787
 * The ID of a `GtkApplicationWindow` can be retrieved with
 * [method@Gtk.ApplicationWindow.get_id].
788
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
789
 * Returns: (nullable) (transfer none): the window for the given `id`
790
791
792
793
794
 */
GtkWindow *
gtk_application_get_window_by_id (GtkApplication *application,
                                  guint           id)
{
795
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);
796
797
798
799
  GList *l;

  g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);

800
  for (l = priv->windows; l != NULL; l = l->next)
801
802
803
804
805
806
807
808
809
    {
      if (GTK_IS_APPLICATION_WINDOW (l->data) &&
          gtk_application_window_get_id (GTK_APPLICATION_WINDOW (l->data)) == id)
        return l->data;
    }

  return NULL;
}

810
/**
Matthias Clasen's avatar
Matthias Clasen committed
811
 * gtk_application_get_active_window: (attributes org.gtk.Method.get_property=active-window)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
812
 * @application: a `GtkApplication`
813
 *
William Jon McCann's avatar
William Jon McCann committed
814
 * Gets the “active” window for the application.
815
816
817
 *
 * The active window is the one that was most recently focused (within
 * the application).  This window may not have the focus at the moment
818
 * if another application has it — this is just the most
819
820
 * recently-focused window within this application.
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
821
 * Returns: (transfer none) (nullable): the active window
822
823
824
825
 **/
GtkWindow *
gtk_application_get_active_window (GtkApplication *application)
{
826
827
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);

828
829
  g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);

830
  return priv->windows ? priv->windows->data : NULL;
831
832
}

833
834
835
static void
gtk_application_update_accels (GtkApplication *application)
{
836
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);
837
838
  GList *l;

839
  for (l = priv->windows; l != NULL; l = l->next)
840
841
842
    _gtk_window_notify_keys_changed (l->data);
}

843
/**
Matthias Clasen's avatar
Matthias Clasen committed
844
 * gtk_application_set_menubar: (attributes org.gtk.Method.set_property=menubar)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
845
846
 * @application: a `GtkApplication`
 * @menubar: (nullable): a `GMenuModel`
847
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
848
 * Sets or unsets the menubar for windows of `application`.
849
850
851
 *
 * This is a menubar in the traditional sense.
 *
852
 * This can only be done in the primary instance of the application,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
853
 * after it has been registered. `GApplication::startup` is a good place
854
855
 * to call this.
 *
856
857
858
859
 * Depending on the desktop environment, this may appear at the top of
 * each window, or at the top of the screen.  In some environments, if
 * both the application menu and the menubar are set, the application
 * menu will be presented as if it were the first item of the menubar.
860
861
862
 * Other environments treat the two as completely separate — for example,
 * the application menu may be rendered by the desktop shell while the
 * menubar (if set) remains in each individual window.
863
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
864
 * Use the base `GActionMap` interface to add actions, to respond to the
865
 * user selecting these menu items.
866
867
868
 */
void
gtk_application_set_menubar (GtkApplication *application,
869
                             GMenuModel     *menubar)
870
{
871
872
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);

873
  g_return_if_fail (GTK_IS_APPLICATION (application));
874
875
  g_return_if_fail (g_application_get_is_registered (G_APPLICATION (application)));
  g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
Sébastien Wilmet's avatar
Sébastien Wilmet committed
876
  g_return_if_fail (menubar == NULL || G_IS_MENU_MODEL (menubar));
877

878
  if (g_set_object (&priv->menubar, menubar))
879
    {
880
      gtk_application_impl_set_menubar (priv->impl, menubar);
881

882
      g_object_notify_by_pspec (G_OBJECT (application), gtk_application_props[PROP_MENUBAR]);
883
    }
884
885
886
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
887
 * gtk_application_get_menubar: (attributes org.gtk.Method.get_property=menubar)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
888
 * @application: a `GtkApplication`
889
890
 *
 * Returns the menu model that has been set with
Emmanuele Bassi's avatar
Emmanuele Bassi committed
891
 * [method@Gtk.Application.set_menubar].
892
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
893
 * Returns: (nullable) (transfer none): the menubar for windows of `application`
894
895
896
897
 */
GMenuModel *
gtk_application_get_menubar (GtkApplication *application)
{
898
899
  GtkApplicationPrivate *priv = gtk_application_get_instance_private (application);

Allison Karlitskaya's avatar