]> Pileus Git - ~andy/gtk/blob - tests/testclientmessage.c
Merge branch 'master' into client-side-windows
[~andy/gtk] / tests / testclientmessage.c
1 /* testclientmessage.c
2  * Copyright (C) 2008  Novell, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <gtk/gtk.h>
21
22 static GdkAtom my_type;
23 static GdkAtom random_type;
24
25 static void
26 send_known (void)
27 {
28   GdkEvent *event = gdk_event_new (GDK_CLIENT_EVENT);
29   static int counter = 42;
30   int i;
31   
32   event->client.window = NULL;
33   event->client.message_type = my_type;
34   event->client.data_format = 32;
35   event->client.data.l[0] = counter++;
36   for (i = 1; i < 5; i++)
37     event->client.data.l[i] = 0;
38
39   gdk_screen_broadcast_client_message (gdk_display_get_default_screen (gdk_display_get_default ()), event);
40   
41   gdk_event_free (event);
42 }
43
44 void
45 send_random (void)
46 {
47   GdkEvent *event = gdk_event_new (GDK_CLIENT_EVENT);
48   static int counter = 1;
49   int i;
50   
51   event->client.window = NULL;
52   event->client.message_type = random_type;
53   event->client.data_format = 32;
54   event->client.data.l[0] = counter++;
55   for (i = 1; i < 5; i++)
56     event->client.data.l[i] = 0;
57
58   gdk_screen_broadcast_client_message (gdk_display_get_default_screen (gdk_display_get_default ()), event);
59   
60   gdk_event_free (event);
61 }
62
63 static GdkFilterReturn
64 filter_func (GdkXEvent *xevent,
65              GdkEvent  *event,
66              gpointer   data)
67 {
68   g_print ("Got matching client message!\n");
69   return GDK_FILTER_REMOVE;
70 }
71
72 int
73 main (int argc, char **argv)
74 {
75   GtkWidget *window;
76   GtkWidget *vbox;
77   GtkWidget *button;
78
79   gtk_init (&argc, &argv);
80
81   my_type = gdk_atom_intern ("GtkTestClientMessage", FALSE);
82   random_type = gdk_atom_intern (g_strdup_printf ("GtkTestClientMessage-%d",
83                                                   g_rand_int_range (g_rand_new (), 1, 99)),
84                                  FALSE);
85
86   g_print ("using random client message type %s\n", gdk_atom_name (random_type));
87
88   window = g_object_connect (g_object_new (gtk_window_get_type (),
89                                            "type", GTK_WINDOW_TOPLEVEL,
90                                            "title", "testclientmessage",
91                                            "border_width", 10,
92                                            NULL),
93                              "signal::destroy", gtk_main_quit, NULL,
94                              NULL);
95   vbox = g_object_new (gtk_vbox_get_type (),
96                        "GtkWidget::parent", window,
97                        "GtkWidget::visible", TRUE,
98                        NULL);
99   button = g_object_connect (g_object_new (gtk_button_get_type (),
100                                            "GtkButton::label", "send known client message",
101                                            "GtkWidget::parent", vbox,
102                                            "GtkWidget::visible", TRUE,
103                                            NULL),
104                              "signal::clicked", send_known, NULL,
105                              NULL);
106   button = g_object_connect (g_object_new (gtk_button_get_type (),
107                                            "GtkButton::label", "send random client message",
108                                            "GtkWidget::parent", vbox,
109                                            "GtkWidget::visible", TRUE,
110                                            NULL),
111                              "signal::clicked", send_random, NULL,
112                              NULL);
113   gdk_display_add_client_message_filter (gdk_display_get_default (),
114                                          my_type,
115                                          filter_func,
116                                          NULL);
117   gtk_widget_show (window);
118
119   gtk_main ();
120
121   return 0;
122 }