2010-08-11 05:23:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2010 Codethink Limited
|
2010-06-07 20:44:58 +00:00
|
|
|
*
|
|
|
|
* 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
|
2010-08-11 05:23:23 +00:00
|
|
|
* version 2 of the licence, or (at your option) any later version.
|
2010-06-07 20:44:58 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*
|
2010-08-11 05:23:23 +00:00
|
|
|
* Author: Ryan Lortie <desrt@desrt.ca>
|
2010-06-07 20:44:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2011-01-02 12:29:23 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2010-06-07 20:44:58 +00:00
|
|
|
#include <unistd.h>
|
2011-01-02 12:29:23 +00:00
|
|
|
#endif
|
2010-08-11 05:23:23 +00:00
|
|
|
#include <string.h>
|
2010-06-07 20:44:58 +00:00
|
|
|
|
|
|
|
#include "gtkapplication.h"
|
2010-06-08 22:06:03 +00:00
|
|
|
#include "gtkmarshalers.h"
|
2010-08-11 05:23:23 +00:00
|
|
|
#include "gtkwindow.h"
|
|
|
|
#include "gtkmain.h"
|
2010-06-07 20:44:58 +00:00
|
|
|
|
|
|
|
#include <gdk/gdk.h>
|
|
|
|
#ifdef GDK_WINDOWING_X11
|
|
|
|
#include <gdk/x11/gdkx.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gtkapplication
|
|
|
|
* @title: GtkApplication
|
|
|
|
* @short_description: Application class
|
|
|
|
*
|
|
|
|
* #GtkApplication is a class that handles many important aspects
|
|
|
|
* of a GTK+ application in a convenient fashion, without enforcing
|
|
|
|
* a one-size-fits-all application model.
|
|
|
|
*
|
2010-10-23 19:24:24 +00:00
|
|
|
* Currently, GtkApplication handles GTK+ initialization, application
|
|
|
|
* uniqueness, provides some basic scriptability by exporting 'actions',
|
2010-11-05 20:24:55 +00:00
|
|
|
* and manages a list of toplevel windows whose life-cycle is automatically
|
|
|
|
* tied to the life-cycle of your application.
|
2010-06-07 20:44:58 +00:00
|
|
|
*
|
|
|
|
* <example id="gtkapplication"><title>A simple application</title>
|
|
|
|
* <programlisting>
|
2011-01-06 06:09:14 +00:00
|
|
|
* <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../examples/bloatpad.c">
|
2010-06-07 20:44:58 +00:00
|
|
|
* <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
|
|
|
|
* </xi:include>
|
|
|
|
* </programlisting>
|
|
|
|
* </example>
|
|
|
|
*/
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
|
|
|
|
|
2010-10-19 19:10:02 +00:00
|
|
|
struct _GtkApplicationPrivate
|
2010-06-07 20:44:58 +00:00
|
|
|
{
|
2010-10-19 19:10:02 +00:00
|
|
|
GList *windows;
|
|
|
|
};
|
2010-06-07 20:44:58 +00:00
|
|
|
|
|
|
|
static void
|
2010-08-11 05:23:23 +00:00
|
|
|
gtk_application_startup (GApplication *application)
|
2010-06-17 14:41:12 +00:00
|
|
|
{
|
2010-08-11 05:23:23 +00:00
|
|
|
gtk_init (0, 0);
|
2010-06-17 14:41:12 +00:00
|
|
|
}
|
|
|
|
|
2010-06-08 22:06:03 +00:00
|
|
|
static void
|
2010-08-11 05:23:23 +00:00
|
|
|
gtk_application_quit_mainloop (GApplication *application)
|
2010-06-08 22:06:03 +00:00
|
|
|
{
|
2010-08-11 05:23:23 +00:00
|
|
|
gtk_main_quit ();
|
2010-06-07 20:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-08-11 05:23:23 +00:00
|
|
|
gtk_application_run_mainloop (GApplication *application)
|
2010-06-07 20:44:58 +00:00
|
|
|
{
|
2010-08-11 05:23:23 +00:00
|
|
|
gtk_main ();
|
2010-06-07 20:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-08-11 05:23:23 +00:00
|
|
|
gtk_application_add_platform_data (GApplication *application,
|
|
|
|
GVariantBuilder *builder)
|
2010-06-07 20:44:58 +00:00
|
|
|
{
|
2010-08-11 05:23:23 +00:00
|
|
|
const gchar *startup_id;
|
2010-06-07 20:44:58 +00:00
|
|
|
|
2010-08-11 05:23:23 +00:00
|
|
|
startup_id = getenv ("DESKTOP_STARTUP_ID");
|
|
|
|
|
|
|
|
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));
|
2010-06-07 20:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-08-11 05:23:23 +00:00
|
|
|
gtk_application_before_emit (GApplication *application,
|
|
|
|
GVariant *platform_data)
|
2010-06-07 20:44:58 +00:00
|
|
|
{
|
2010-08-11 05:23:23 +00:00
|
|
|
GVariantIter iter;
|
|
|
|
const gchar *key;
|
|
|
|
GVariant *value;
|
2010-06-07 20:44:58 +00:00
|
|
|
|
2010-08-11 05:23:23 +00:00
|
|
|
g_variant_iter_init (&iter, platform_data);
|
|
|
|
while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
|
2010-06-07 20:44:58 +00:00
|
|
|
{
|
2010-10-28 18:27:04 +00:00
|
|
|
#ifdef GDK_WINDOWING_X11
|
2010-08-11 05:23:23 +00:00
|
|
|
if (strcmp (key, "desktop-startup-id") == 0)
|
|
|
|
{
|
|
|
|
GdkDisplay *display;
|
|
|
|
const gchar *id;
|
|
|
|
|
|
|
|
display = gdk_display_get_default ();
|
|
|
|
id = g_variant_get_string (value, NULL);
|
|
|
|
gdk_x11_display_set_startup_notification_id (display, id);
|
|
|
|
}
|
2010-10-28 18:27:04 +00:00
|
|
|
#endif
|
2010-06-07 20:44:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-08-11 05:23:23 +00:00
|
|
|
gtk_application_after_emit (GApplication *application,
|
|
|
|
GVariant *platform_data)
|
2010-06-07 20:44:58 +00:00
|
|
|
{
|
2010-08-11 05:23:23 +00:00
|
|
|
gdk_notify_startup_complete ();
|
2010-06-07 20:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_application_init (GtkApplication *application)
|
|
|
|
{
|
2010-10-19 19:10:02 +00:00
|
|
|
application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
|
|
|
|
GTK_TYPE_APPLICATION,
|
|
|
|
GtkApplicationPrivate);
|
2010-06-07 20:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-08-11 05:23:23 +00:00
|
|
|
gtk_application_class_init (GtkApplicationClass *class)
|
2010-06-07 20:44:58 +00:00
|
|
|
{
|
2010-08-11 05:23:23 +00:00
|
|
|
GApplicationClass *application_class = G_APPLICATION_CLASS (class);
|
2010-06-07 20:44:58 +00:00
|
|
|
|
2010-08-11 05:23:23 +00:00
|
|
|
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;
|
2010-06-08 22:06:03 +00:00
|
|
|
|
2010-08-11 05:23:23 +00:00
|
|
|
application_class->quit_mainloop = gtk_application_quit_mainloop;
|
|
|
|
application_class->run_mainloop = gtk_application_run_mainloop;
|
2010-10-19 19:10:02 +00:00
|
|
|
|
|
|
|
g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
|
|
|
|
}
|
|
|
|
|
2010-10-23 19:24:24 +00:00
|
|
|
/**
|
|
|
|
* gtk_application_new:
|
|
|
|
* @application_id: the application id
|
|
|
|
* @flags: the application flags
|
|
|
|
*
|
|
|
|
* Creates a new #GtkApplication instance.
|
|
|
|
*
|
|
|
|
* This function calls g_type_init() for you. gtk_init() is called
|
|
|
|
* as soon as the application gets registered as the primary instance.
|
|
|
|
*
|
|
|
|
* The application id must be valid. See g_application_id_is_valid().
|
|
|
|
*
|
|
|
|
* Returns: a new #GtkApplication instance
|
|
|
|
*/
|
2010-10-19 19:10:02 +00:00
|
|
|
GtkApplication *
|
|
|
|
gtk_application_new (const gchar *application_id,
|
|
|
|
GApplicationFlags flags)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
|
|
|
|
|
|
|
|
g_type_init ();
|
|
|
|
|
|
|
|
return g_object_new (GTK_TYPE_APPLICATION,
|
|
|
|
"application-id", application_id,
|
|
|
|
"flags", flags,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-20 09:06:19 +00:00
|
|
|
* gtk_application_add_window:
|
2010-10-19 19:10:02 +00:00
|
|
|
* @application: a #GtkApplication
|
|
|
|
* @window: a #GtkWindow
|
|
|
|
*
|
|
|
|
* Adds a window from @application.
|
|
|
|
*
|
2010-10-20 09:06:19 +00:00
|
|
|
* This call is equivalent to setting the #GtkWindow:application
|
2010-10-19 19:10:02 +00:00
|
|
|
* property of @window to @application.
|
|
|
|
*
|
2011-01-25 03:20:35 +00:00
|
|
|
* Normally, the connection between the application and the window
|
|
|
|
* will remain until the window is destroyed, but you can explicitly
|
|
|
|
* remove it with gtk_application_remove_window().
|
|
|
|
*
|
|
|
|
* GTK+ will keep the application running as long as it has
|
|
|
|
* any windows.
|
|
|
|
*
|
2010-10-19 19:10:02 +00:00
|
|
|
* Since: 3.0
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
gtk_application_add_window (GtkApplication *application,
|
|
|
|
GtkWindow *window)
|
|
|
|
{
|
2010-10-23 19:24:24 +00:00
|
|
|
GtkApplicationPrivate *priv;
|
|
|
|
|
2010-10-19 19:10:02 +00:00
|
|
|
g_return_if_fail (GTK_IS_APPLICATION (application));
|
|
|
|
|
2010-10-23 19:24:24 +00:00
|
|
|
priv = application->priv;
|
|
|
|
|
|
|
|
if (!g_list_find (priv->windows, window))
|
2010-10-19 19:10:02 +00:00
|
|
|
{
|
2010-10-23 19:24:24 +00:00
|
|
|
priv->windows = g_list_prepend (priv->windows, window);
|
2010-10-19 19:10:02 +00:00
|
|
|
gtk_window_set_application (window, application);
|
|
|
|
g_application_hold (G_APPLICATION (application));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_application_remove_window:
|
|
|
|
* @application: a #GtkApplication
|
|
|
|
* @window: a #GtkWindow
|
|
|
|
*
|
|
|
|
* Remove a window from @application.
|
|
|
|
*
|
|
|
|
* If @window belongs to @application then this call is equivalent to
|
2010-10-20 09:06:19 +00:00
|
|
|
* setting the #GtkWindow:application property of @window to
|
2010-10-19 19:10:02 +00:00
|
|
|
* %NULL.
|
|
|
|
*
|
|
|
|
* The application may stop running as a result of a call to this
|
|
|
|
* function.
|
|
|
|
*
|
|
|
|
* Since: 3.0
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
gtk_application_remove_window (GtkApplication *application,
|
|
|
|
GtkWindow *window)
|
|
|
|
{
|
2010-10-23 19:24:24 +00:00
|
|
|
GtkApplicationPrivate *priv;
|
|
|
|
|
2010-10-19 19:10:02 +00:00
|
|
|
g_return_if_fail (GTK_IS_APPLICATION (application));
|
|
|
|
|
2010-10-23 19:24:24 +00:00
|
|
|
priv = application->priv;
|
|
|
|
if (g_list_find (priv->windows, window))
|
2010-10-19 19:10:02 +00:00
|
|
|
{
|
2010-10-23 19:24:24 +00:00
|
|
|
priv->windows = g_list_remove (priv->windows, window);
|
2010-10-19 19:10:02 +00:00
|
|
|
g_application_release (G_APPLICATION (application));
|
|
|
|
gtk_window_set_application (window, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_application_get_windows:
|
|
|
|
* @application: a #GtkApplication
|
|
|
|
*
|
|
|
|
* Gets a list of the #GtkWindow<!-- -->s associated with @application.
|
|
|
|
*
|
|
|
|
* The list that is returned should not be modified in any way.
|
|
|
|
*
|
2011-01-20 09:37:17 +00:00
|
|
|
* Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
|
2010-10-19 19:10:02 +00:00
|
|
|
*
|
|
|
|
* Since: 3.0
|
|
|
|
**/
|
|
|
|
GList *
|
|
|
|
gtk_application_get_windows (GtkApplication *application)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
|
|
|
|
|
|
|
|
return application->priv->windows;
|
2010-06-07 20:44:58 +00:00
|
|
|
}
|