Mercurial > pidgin
annotate plugins/gestures/stroke-draw.c @ 4399:ce8d35b435de
[gaim-migrate @ 4668]
Fixes the problem where doing a gesture and then clicking the right mouse
button causes it to stay in gesture mode forever. It's hacky, though..
hope it doesn't cause other issues.
committer: Tailor Script <tailor@pidgin.im>
| author | Christian Hammond <chipx86@chipx86.com> |
|---|---|
| date | Thu, 23 Jan 2003 00:19:57 +0000 |
| parents | 16540914c963 |
| children | 25c2119deab3 |
| rev | line source |
|---|---|
| 4390 | 1 /* |
| 2 GNOME stroke implementation | |
| 3 Copyright (c) 2000, 2001 Dan Nicolaescu | |
| 4 See the file COPYING for distribution information. | |
| 5 */ | |
| 6 | |
| 7 #include "config.h" | |
| 8 | |
| 9 #include <unistd.h> | |
| 10 #include <stdlib.h> | |
| 11 #include <stdio.h> | |
| 12 #include <glib.h> | |
| 13 #include <gtk/gtk.h> | |
| 14 | |
| 15 #include <gdk/gdkx.h> | |
| 16 #include "gstroke.h" | |
| 17 #include "gstroke-internal.h" | |
| 18 | |
| 19 #include <X11/Xlib.h> | |
| 20 #include <X11/Xutil.h> | |
| 21 | |
| 22 | |
| 23 static void gstroke_invisible_window_init (GtkWidget *widget); | |
| 24 /*FIXME: Maybe these should be put in a structure, and not static...*/ | |
| 25 static Display * gstroke_disp; | |
| 26 static Window gstroke_window; | |
| 27 static GC gstroke_gc; | |
| 28 static int mouse_button = 2; | |
| 29 static gboolean draw_strokes = FALSE; | |
| 30 | |
| 31 #define GSTROKE_TIMEOUT_DURATION 10 | |
| 32 | |
| 33 #define GSTROKE_SIGNALS "gstroke_signals" | |
| 34 | |
| 35 struct gstroke_func_and_data { | |
| 36 void (*func)(GtkWidget *, void *); | |
| 37 gpointer data; | |
| 38 }; | |
| 39 | |
| 40 | |
| 41 /*FIXME: maybe it's better to just make 2 static variables, not a | |
| 42 structure */ | |
| 43 struct mouse_position { | |
| 44 struct s_point last_point; | |
| 45 gboolean invalid; | |
| 46 }; | |
| 47 | |
| 48 | |
| 49 static struct mouse_position last_mouse_position; | |
| 50 static guint timer_id; | |
| 51 | |
| 52 static void gstroke_execute (GtkWidget *widget, const gchar *name); | |
| 53 | |
| 54 static void | |
| 55 record_stroke_segment (GtkWidget *widget) | |
| 56 { | |
| 57 gint x, y; | |
| 58 struct gstroke_metrics *metrics; | |
| 59 | |
| 60 gtk_widget_get_pointer (widget, &x, &y); | |
| 61 | |
| 62 if (last_mouse_position.invalid) | |
| 63 last_mouse_position.invalid = FALSE; | |
| 64 else if (gstroke_draw_strokes()) | |
| 65 { | |
| 66 #if 1 | |
| 67 XDrawLine (gstroke_disp, gstroke_window, gstroke_gc, | |
| 68 last_mouse_position.last_point.x, | |
| 69 last_mouse_position.last_point.y, | |
| 70 x, y); | |
| 71 /* XFlush (gstroke_disp); */ | |
| 72 #else | |
| 73 /* FIXME: this does not work. It will only work if we create a | |
| 74 corresponding GDK window for stroke_window and draw on | |
| 75 that... */ | |
| 76 gdk_draw_line (widget->window, widget->style->fg_gc[GTK_STATE_NORMAL], | |
| 77 last_mouse_position.last_point.x, | |
| 78 last_mouse_position.last_point.y, | |
| 79 x, | |
| 80 y); | |
| 81 #endif | |
| 82 } | |
| 83 | |
| 84 if (last_mouse_position.last_point.x != x | |
| 85 || last_mouse_position.last_point.y != y) | |
| 86 { | |
| 87 last_mouse_position.last_point.x = x; | |
| 88 last_mouse_position.last_point.y = y; | |
| 89 metrics = (struct gstroke_metrics *)g_object_get_data(G_OBJECT(widget), | |
| 90 GSTROKE_METRICS); | |
| 91 _gstroke_record (x, y, metrics); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 static gint | |
| 96 gstroke_timeout (gpointer data) | |
| 97 { | |
| 98 GtkWidget *widget = GTK_WIDGET (data); | |
| 99 record_stroke_segment (widget); | |
| 100 | |
| 101 return TRUE; | |
| 102 } | |
| 103 | |
| 104 static gint | |
| 105 process_event (GtkWidget *widget, GdkEvent *event, gpointer data G_GNUC_UNUSED) | |
| 106 { | |
| 107 static GtkWidget *original_widget = NULL; | |
| 108 switch (event->type) { | |
| 109 case GDK_BUTTON_PRESS: | |
|
4399
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
110 debug_printf("Button press: %d\n", event->button.button); |
| 4390 | 111 if (event->button.button != gstroke_get_mouse_button()) |
| 112 break; | |
| 113 | |
| 114 original_widget = widget; /* remeber the widget where | |
| 115 the stroke started */ | |
| 116 | |
| 117 gstroke_invisible_window_init (widget); | |
| 118 | |
| 119 record_stroke_segment (widget); | |
| 120 | |
| 121 gdk_pointer_grab (widget->window, FALSE, | |
| 122 GDK_BUTTON_RELEASE_MASK, NULL, NULL, | |
| 123 event->button.time); | |
| 124 timer_id = gtk_timeout_add (GSTROKE_TIMEOUT_DURATION, | |
| 125 gstroke_timeout, widget); | |
| 126 return TRUE; | |
| 127 | |
| 128 case GDK_BUTTON_RELEASE: | |
| 129 if ((event->button.button != gstroke_get_mouse_button()) | |
|
4399
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
130 || (original_widget == NULL)) { |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
131 |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
132 /* Nice bug when you hold down one button and press another. */ |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
133 /* We'll just cancel the gesture instead. */ |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
134 last_mouse_position.invalid = TRUE; |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
135 original_widget = NULL; |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
136 |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
137 if (timer_id > 0) |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
138 gtk_timeout_remove (timer_id); |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
139 |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
140 gdk_pointer_ungrab (event->button.time); |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
141 timer_id = 0; |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
142 |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
143 if (gstroke_draw_strokes()) { |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
144 /* get rid of the invisible stroke window */ |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
145 XUnmapWindow (gstroke_disp, gstroke_window); |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
146 XFlush (gstroke_disp); |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
147 } |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
148 |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
149 break; |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
150 |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
151 } |
| 4390 | 152 |
| 153 last_mouse_position.invalid = TRUE; | |
| 154 original_widget = NULL; | |
| 155 gtk_timeout_remove (timer_id); | |
| 156 gdk_pointer_ungrab (event->button.time); | |
| 157 timer_id = 0; | |
| 158 | |
| 159 { | |
| 160 char result[GSTROKE_MAX_SEQUENCE]; | |
| 161 struct gstroke_metrics *metrics; | |
| 162 | |
| 163 metrics = (struct gstroke_metrics *)g_object_get_data(G_OBJECT (widget), | |
| 164 GSTROKE_METRICS); | |
| 165 if (gstroke_draw_strokes()) { | |
| 166 /* get rid of the invisible stroke window */ | |
| 167 XUnmapWindow (gstroke_disp, gstroke_window); | |
| 168 XFlush (gstroke_disp); | |
| 169 } | |
| 170 | |
| 171 _gstroke_canonical (result, metrics); | |
| 172 gstroke_execute (widget, result); | |
| 173 return FALSE; | |
| 174 } | |
| 175 return TRUE; | |
| 176 default: | |
| 177 break; | |
| 178 } | |
| 179 | |
| 180 return FALSE; | |
| 181 } | |
| 182 | |
| 183 void | |
| 184 gstroke_set_draw_strokes(gboolean draw) | |
| 185 { | |
| 186 draw_strokes = draw; | |
| 187 } | |
| 188 | |
| 189 gboolean | |
| 190 gstroke_draw_strokes(void) | |
| 191 { | |
| 192 return draw_strokes; | |
| 193 } | |
| 194 | |
| 195 void | |
| 196 gstroke_set_mouse_button(gint button) | |
| 197 { | |
| 198 mouse_button = button; | |
| 199 } | |
| 200 | |
| 201 int | |
| 202 gstroke_get_mouse_button(void) | |
| 203 { | |
| 204 return mouse_button; | |
| 205 } | |
| 206 | |
| 207 void | |
| 208 gstroke_enable (GtkWidget *widget) | |
| 209 { | |
| 210 struct gstroke_metrics* | |
| 211 metrics = (struct gstroke_metrics *)g_object_get_data(G_OBJECT(widget), | |
| 212 GSTROKE_METRICS); | |
| 213 if (metrics == NULL) | |
| 214 { | |
| 215 metrics = (struct gstroke_metrics *)g_malloc (sizeof | |
| 216 (struct gstroke_metrics)); | |
| 217 metrics->pointList = NULL; | |
| 218 metrics->min_x = 10000; | |
| 219 metrics->min_y = 10000; | |
| 220 metrics->max_x = 0; | |
| 221 metrics->max_y = 0; | |
| 222 metrics->point_count = 0; | |
| 223 | |
| 224 g_object_set_data(G_OBJECT(widget), GSTROKE_METRICS, metrics); | |
| 225 | |
| 226 g_signal_connect(G_OBJECT(widget), "event", | |
| 227 G_CALLBACK(process_event), NULL); | |
| 228 } | |
| 229 else | |
| 230 _gstroke_init (metrics); | |
| 231 | |
| 232 last_mouse_position.invalid = TRUE; | |
| 233 } | |
| 234 | |
| 235 guint | |
| 236 gstroke_signal_connect (GtkWidget *widget, | |
| 237 const gchar *name, | |
| 238 void (*func)(GtkWidget *widget, void *data), | |
| 239 gpointer data) | |
| 240 { | |
| 241 struct gstroke_func_and_data *func_and_data; | |
| 242 GHashTable *hash_table = | |
| 243 (GHashTable*)g_object_get_data(G_OBJECT(widget), GSTROKE_SIGNALS); | |
| 244 | |
| 245 if (!hash_table) | |
| 246 { | |
| 247 hash_table = g_hash_table_new (g_str_hash, g_str_equal); | |
| 248 g_object_set_data(G_OBJECT(widget), GSTROKE_SIGNALS, | |
| 249 (gpointer)hash_table); | |
| 250 } | |
| 251 func_and_data = g_new (struct gstroke_func_and_data, 1); | |
| 252 func_and_data->func = func; | |
| 253 func_and_data->data = data; | |
| 254 g_hash_table_insert (hash_table, (gpointer)name, (gpointer)func_and_data); | |
| 255 return TRUE; | |
| 256 } | |
| 257 | |
| 258 static void | |
| 259 gstroke_execute (GtkWidget *widget, const gchar *name) | |
| 260 { | |
| 261 | |
| 262 GHashTable *hash_table = | |
| 263 (GHashTable*)g_object_get_data(G_OBJECT(widget), GSTROKE_SIGNALS); | |
| 264 | |
| 265 #if 0 | |
| 266 debug_printf("gstroke %s\n", name); | |
| 267 #endif | |
| 268 | |
| 269 if (hash_table) | |
| 270 { | |
| 271 struct gstroke_func_and_data *fd = | |
| 272 (struct gstroke_func_and_data*)g_hash_table_lookup (hash_table, name); | |
| 273 if (fd) | |
| 274 (*fd->func)(widget, fd->data); | |
| 275 } | |
| 276 } | |
| 277 | |
| 278 void | |
| 279 gstroke_cleanup (GtkWidget *widget) | |
| 280 { | |
| 281 struct gstroke_metrics *metrics; | |
| 282 GHashTable *hash_table = | |
| 283 (GHashTable*)g_object_get_data(G_OBJECT(widget), GSTROKE_SIGNALS); | |
| 284 if (hash_table) | |
| 285 /* FIXME: does this delete the elements too? */ | |
| 286 g_hash_table_destroy (hash_table); | |
| 287 | |
| 288 g_object_steal_data(G_OBJECT(widget), GSTROKE_SIGNALS); | |
| 289 | |
| 290 metrics = (struct gstroke_metrics*)g_object_get_data(G_OBJECT(widget), | |
| 291 GSTROKE_METRICS); | |
| 292 if (metrics) | |
| 293 g_free (metrics); | |
| 294 g_object_steal_data(G_OBJECT(widget), GSTROKE_METRICS); | |
| 295 } | |
| 296 | |
| 297 | |
| 298 /* This function should be written using Gtk+ primitives*/ | |
| 299 static void | |
| 300 gstroke_invisible_window_init (GtkWidget *widget) | |
| 301 { | |
| 302 XSetWindowAttributes w_attr; | |
| 303 XWindowAttributes orig_w_attr; | |
| 304 unsigned long mask, col_border, col_background; | |
| 305 unsigned int border_width; | |
| 306 XSizeHints hints; | |
| 307 Display *disp = GDK_WINDOW_XDISPLAY(widget->window); | |
| 308 Window wind = GDK_WINDOW_XWINDOW (widget->window); | |
| 309 int screen = DefaultScreen (disp); | |
| 310 | |
| 311 if (!gstroke_draw_strokes()) | |
| 312 return; | |
| 313 | |
| 314 gstroke_disp = disp; | |
| 315 | |
| 316 /* X server should save what's underneath */ | |
| 317 XGetWindowAttributes (gstroke_disp, wind, &orig_w_attr); | |
| 318 hints.x = orig_w_attr.x; | |
| 319 hints.y = orig_w_attr.y; | |
| 320 hints.width = orig_w_attr.width; | |
| 321 hints.height = orig_w_attr.height; | |
| 322 mask = CWSaveUnder; | |
| 323 w_attr.save_under = True; | |
| 324 | |
| 325 /* inhibit all the decorations */ | |
| 326 mask |= CWOverrideRedirect; | |
| 327 w_attr.override_redirect = True; | |
| 328 | |
| 329 /* Don't set a background, transparent window */ | |
| 330 mask |= CWBackPixmap; | |
| 331 w_attr.background_pixmap = None; | |
| 332 | |
| 333 /* Default input window look */ | |
| 334 col_background = WhitePixel (gstroke_disp, screen); | |
| 335 | |
| 336 /* no border for the window */ | |
|
4399
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
337 #if 0 |
| 4390 | 338 border_width = 5; |
|
4399
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
339 #endif |
| 4390 | 340 border_width = 0; |
|
4399
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
341 |
| 4390 | 342 col_border = BlackPixel (gstroke_disp, screen); |
| 343 | |
| 344 gstroke_window = XCreateSimpleWindow (gstroke_disp, wind, | |
| 345 0, 0, | |
| 346 hints.width - 2 * border_width, | |
| 347 hints.height - 2 * border_width, | |
| 348 border_width, | |
| 349 col_border, col_background); | |
| 350 | |
| 351 gstroke_gc = XCreateGC (gstroke_disp, gstroke_window, 0, NULL); | |
| 352 | |
| 353 XSetFunction (gstroke_disp, gstroke_gc, GXinvert); | |
| 354 | |
| 355 XChangeWindowAttributes (gstroke_disp, gstroke_window, mask, &w_attr); | |
| 356 | |
| 357 XSetLineAttributes (gstroke_disp, gstroke_gc, 2, LineSolid, | |
| 358 CapButt, JoinMiter); | |
| 359 XMapRaised (gstroke_disp, gstroke_window); | |
| 360 | |
| 361 #if 0 | |
| 362 /*FIXME: is this call really needed? If yes, does it need the real | |
| 363 argc and argv? */ | |
| 364 hints.flags = PPosition | PSize; | |
| 365 XSetStandardProperties (gstroke_disp, gstroke_window, "gstroke_test", NULL, | |
| 366 (Pixmap)NULL, NULL, 0, &hints); | |
| 367 | |
| 368 | |
| 369 /* Receive the close window client message */ | |
| 370 { | |
| 371 /* FIXME: is this really needed? If yes, something should be done | |
| 372 with wmdelete...*/ | |
| 373 Atom wmdelete = XInternAtom (gstroke_disp, "WM_DELETE_WINDOW", | |
| 374 False); | |
| 375 XSetWMProtocols (gstroke_disp, gstroke_window, &wmdelete, True); | |
| 376 } | |
| 377 #endif | |
| 378 } |
