added gtk_dialog_help_disable(), modified gtk_dialog_help_enable() (fixes #19468)
[hildon] / hildon-widgets / hildon-dialoghelp.c
1 /*
2  * This file is part of hildon-libs
3  *
4  * Copyright (C) 2005 Nokia Corporation.
5  *
6  * Contact: Luc Pionchon <luc.pionchon@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 /*
26  *  @file hildon-dialoghelp.c
27  *
28  *  This file provides API for the help dialog with
29  *  the optional icon.
30  *
31  */
32
33 #include <gdk/gdkx.h>
34 #include "hildon-dialoghelp.h"
35
36 static guint help_signal = 0;
37
38 static GdkFilterReturn
39 handle_xevent(GdkXEvent * xevent, GdkEvent * event, gpointer data)
40 {
41     XAnyEvent *eventti = xevent;
42
43     if (eventti->type == ClientMessage) {
44         Atom help_atom, wm_atom;
45         Display *disp;
46         XClientMessageEvent *cm;
47
48         disp = GDK_DISPLAY();
49         cm = xevent;
50
51         help_atom = XInternAtom(disp, "_NET_WM_CONTEXT_HELP", True);
52         wm_atom = XInternAtom(disp, "WM_PROTOCOLS", True);
53
54         if (cm->message_type == wm_atom && cm->data.l[0] == help_atom) {
55             /* XClientMessageEvent *cm = xevent; */
56             g_signal_emit(G_OBJECT(data), help_signal, 0);
57         }
58
59         return GDK_FILTER_REMOVE;       /* Event handled, don't process
60                                            further */
61     }
62
63     return GDK_FILTER_CONTINUE; /* Event not handled */
64 }
65
66 /**
67  * gtk_dialog_help_enable:
68  * @dialog: The dialog of which help is to be enabled.
69  *
70  * Enables context help button for given dialog. The signal "help" can be
71  * connected to handler by normal gtk methods. Note that this function
72  * has to be called before the dialog is shown.
73  *
74  * The "help" signal itself has no other parameters than the dialog where
75  * it is connected to, ie.:
76  * void user_function(GtkDialog *, gpointer user_data);
77  **/
78 void gtk_dialog_help_enable(GtkDialog * dialog)
79 {
80     GdkWindow *window;
81     GdkDisplay *display;
82     Atom protocols[4];
83     int n = 0;
84     
85     if (help_signal == 0) {
86         help_signal = g_signal_new("help", GTK_TYPE_DIALOG,
87                                    G_SIGNAL_ACTION, (guint) - 1, NULL,
88                                    NULL, g_cclosure_marshal_VOID__VOID,
89                                    G_TYPE_NONE, 0);
90     }
91
92     g_return_if_fail(GTK_IS_DIALOG(dialog));
93
94     gtk_widget_realize(GTK_WIDGET(dialog));
95     window = GTK_WIDGET(dialog)->window;
96
97     display = gdk_drawable_get_display (window);
98
99     protocols[n++] = gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW");
100     protocols[n++] = gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS");
101     protocols[n++] = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING");
102     protocols[n++] = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_CONTEXT_HELP");
103
104     XSetWMProtocols (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window), protocols, n);
105     
106     gdk_window_add_filter(window, handle_xevent, dialog);
107 }
108
109
110 /*
111  * gtk_dialog_help_disable:
112  * @dialog: The dialog of which help is to be disabled.
113  * 
114  */
115 void gtk_dialog_help_disable(GtkDialog * dialog)
116 {
117     GdkWindow *window=NULL;
118     GdkDisplay *display;
119     Atom protocols[4];
120     int n = 0;
121     
122     g_return_if_fail(GTK_IS_DIALOG(dialog));
123
124     gtk_widget_realize(GTK_WIDGET(dialog));
125     window = GTK_WIDGET(dialog)->window;
126     display = gdk_drawable_get_display (window);
127     
128     protocols[n++] = gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW");
129     protocols[n++] = gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS");
130     protocols[n++] = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING");
131
132     XSetWMProtocols (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window), protocols, n);
133     
134     gdk_window_add_filter(window, handle_xevent, dialog);
135 }
136
137
138
139