1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /*
4 * Copyright (C) 2016 Red Hat
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Carlos Garnacho <carlosg@gnome.org>
20 */
21
22 #include "config.h"
23
24 #include "core/window-private.h"
25 #include "meta/meta-close-dialog.h"
26 #include "meta/meta-enum-types.h"
27
28 enum
29 {
30 RESPONSE,
31 N_SIGNALS
32 };
33
34 guint dialog_signals[N_SIGNALS] = { 0 };
35
36 static GQuark quark_visible = 0;
37
G_DEFINE_INTERFACE(MetaCloseDialog,meta_close_dialog,G_TYPE_OBJECT)38 G_DEFINE_INTERFACE (MetaCloseDialog, meta_close_dialog, G_TYPE_OBJECT)
39
40 static void
41 meta_close_dialog_default_init (MetaCloseDialogInterface *iface)
42 {
43 g_object_interface_install_property (iface,
44 g_param_spec_object ("window",
45 "Window",
46 "Window",
47 META_TYPE_WINDOW,
48 G_PARAM_READWRITE |
49 G_PARAM_CONSTRUCT_ONLY));
50 dialog_signals[RESPONSE] =
51 g_signal_new ("response",
52 G_TYPE_FROM_INTERFACE (iface),
53 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
54 0,
55 NULL, NULL, NULL,
56 G_TYPE_NONE, 1, META_TYPE_CLOSE_DIALOG_RESPONSE);
57
58 quark_visible = g_quark_from_static_string ("meta-close-dialog-visible");
59 }
60
61 /**
62 * meta_close_dialog_show:
63 * @dialog: a #MetaCloseDialog
64 *
65 * Shows the close dialog.
66 **/
67 void
meta_close_dialog_show(MetaCloseDialog * dialog)68 meta_close_dialog_show (MetaCloseDialog *dialog)
69 {
70 MetaCloseDialogInterface *iface;
71
72 g_return_if_fail (META_IS_CLOSE_DIALOG (dialog));
73
74 iface = META_CLOSE_DIALOG_GET_IFACE (dialog);
75 iface->show (dialog);
76 g_object_set_qdata (G_OBJECT (dialog), quark_visible, GINT_TO_POINTER (TRUE));
77 }
78
79 /**
80 * meta_close_dialog_hide:
81 * @dialog: a #MetaCloseDialog
82 *
83 * Hides the close dialog.
84 **/
85 void
meta_close_dialog_hide(MetaCloseDialog * dialog)86 meta_close_dialog_hide (MetaCloseDialog *dialog)
87 {
88 MetaCloseDialogInterface *iface;
89
90 g_return_if_fail (META_IS_CLOSE_DIALOG (dialog));
91
92 iface = META_CLOSE_DIALOG_GET_IFACE (dialog);
93 iface->hide (dialog);
94 g_object_steal_qdata (G_OBJECT (dialog), quark_visible);
95 }
96
97 /**
98 * meta_close_dialog_response:
99 * @dialog: a #MetaCloseDialog
100 * @response: a #MetaCloseDialogResponse
101 *
102 * Responds and closes the dialog. To be called by #MetaCloseDialog
103 * implementations.
104 **/
105 void
meta_close_dialog_response(MetaCloseDialog * dialog,MetaCloseDialogResponse response)106 meta_close_dialog_response (MetaCloseDialog *dialog,
107 MetaCloseDialogResponse response)
108 {
109 g_signal_emit (dialog, dialog_signals[RESPONSE], 0, response);
110 meta_close_dialog_hide (dialog);
111 }
112
113 /**
114 * meta_close_dialog_is_visible:
115 * @dialog: a #MetaCloseDialog
116 *
117 * Returns whether @dialog is currently visible.
118 *
119 * Returns: #TRUE if @dialog is visible.
120 **/
121 gboolean
meta_close_dialog_is_visible(MetaCloseDialog * dialog)122 meta_close_dialog_is_visible (MetaCloseDialog *dialog)
123 {
124 return GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (dialog), quark_visible));
125 }
126
127 /**
128 * meta_close_dialog_focus:
129 * @dialog: a #MetaCloseDialog
130 *
131 * Call whenever @dialog should receive keyboard focus,
132 * usually when the window would.
133 **/
134 void
meta_close_dialog_focus(MetaCloseDialog * dialog)135 meta_close_dialog_focus (MetaCloseDialog *dialog)
136 {
137 MetaCloseDialogInterface *iface;
138
139 g_return_if_fail (META_IS_CLOSE_DIALOG (dialog));
140
141 iface = META_CLOSE_DIALOG_GET_IFACE (dialog);
142 if (iface->focus)
143 iface->focus (dialog);
144 }
145