/src/wrappers/gtk/examples/gtk-demo/drawingarea.e

http://github.com/tybor/Liberty · Specman e · 349 lines · 21 code · 66 blank · 262 comment · 2 complexity · 62b6a3354ab17b9e42e220e05847a08f MD5 · raw file

  1. indexing
  2. description: "."
  3. copyright: "[
  4. Copyright (C) 2006 Paolo Redaelli, GTK+ team
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation; either version 2.1 of
  8. the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA
  17. ]"
  18. class DRAWINGAREA
  19. creation make
  20. feature
  21. -- /* Drawing Area
  22. -- *
  23. -- * GtkDrawingArea is a blank area where you can draw custom displays
  24. -- * of various kinds.
  25. -- *
  26. -- * This demo has two drawing areas. The checkerboard area shows
  27. -- * how you can just draw something; all you have to do is write
  28. -- * a signal handler for expose_event, as shown here.
  29. -- *
  30. -- * The "scribble" area is a bit more advanced, and shows how to handle
  31. -- * events such as button presses and mouse motion. Click the mouse
  32. -- * and drag in the scribble area to draw squiggles. Resize the window
  33. -- * to clear the area.
  34. -- */
  35. -- #include <gtk/gtk.h>
  36. -- static GtkWidget *window = NULL;
  37. -- /* Pixmap for scribble area, to store current scribbles */
  38. -- static GdkPixmap *pixmap = NULL;
  39. -- /* Create a new pixmap of the appropriate size to store our scribbles */
  40. -- static gboolean
  41. -- scribble_configure_event (GtkWidget *widget,
  42. -- GdkEventConfigure *event,
  43. -- gpointer data)
  44. -- {
  45. -- if (pixmap)
  46. -- g_object_unref (pixmap);
  47. -- pixmap = gdk_pixmap_new (widget->window,
  48. -- widget->allocation.width,
  49. -- widget->allocation.height,
  50. -- -1);
  51. -- /* Initialize the pixmap to white */
  52. -- gdk_draw_rectangle (pixmap,
  53. -- widget->style->white_gc,
  54. -- TRUE,
  55. -- 0, 0,
  56. -- widget->allocation.width,
  57. -- widget->allocation.height);
  58. -- /* We've handled the configure event, no need for further processing. */
  59. -- return TRUE;
  60. -- }
  61. -- /* Redraw the screen from the pixmap */
  62. -- static gboolean
  63. -- scribble_expose_event (GtkWidget *widget,
  64. -- GdkEventExpose *event,
  65. -- gpointer data)
  66. -- {
  67. -- /* We use the "foreground GC" for the widget since it already exists,
  68. -- * but honestly any GC would work. The only thing to worry about
  69. -- * is whether the GC has an inappropriate clip region set.
  70. -- */
  71. -- gdk_draw_drawable (widget->window,
  72. -- widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
  73. -- pixmap,
  74. -- /* Only copy the area that was exposed. */
  75. -- event->area.x, event->area.y,
  76. -- event->area.x, event->area.y,
  77. -- event->area.width, event->area.height);
  78. -- return FALSE;
  79. -- }
  80. -- /* Draw a rectangle on the screen */
  81. -- static void
  82. -- draw_brush (GtkWidget *widget,
  83. -- gdouble x,
  84. -- gdouble y)
  85. -- {
  86. -- GdkRectangle update_rect;
  87. -- update_rect.x = x - 3;
  88. -- update_rect.y = y - 3;
  89. -- update_rect.width = 6;
  90. -- update_rect.height = 6;
  91. -- /* Paint to the pixmap, where we store our state */
  92. -- gdk_draw_rectangle (pixmap,
  93. -- widget->style->black_gc,
  94. -- TRUE,
  95. -- update_rect.x, update_rect.y,
  96. -- update_rect.width, update_rect.height);
  97. -- /* Now invalidate the affected region of the drawing area. */
  98. -- gdk_window_invalidate_rect (widget->window,
  99. -- &update_rect,
  100. -- FALSE);
  101. -- }
  102. -- static gboolean
  103. -- scribble_button_press_event (GtkWidget *widget,
  104. -- GdkEventButton *event,
  105. -- gpointer data)
  106. -- {
  107. -- if (pixmap == NULL)
  108. -- return FALSE; /* paranoia check, in case we haven't gotten a configure event */
  109. -- if (event->button == 1)
  110. -- draw_brush (widget, event->x, event->y);
  111. -- /* We've handled the event, stop processing */
  112. -- return TRUE;
  113. -- }
  114. -- static gboolean
  115. -- scribble_motion_notify_event (GtkWidget *widget,
  116. -- GdkEventMotion *event,
  117. -- gpointer data)
  118. -- {
  119. -- int x, y;
  120. -- GdkModifierType state;
  121. -- if (pixmap == NULL)
  122. -- return FALSE; /* paranoia check, in case we haven't gotten a configure event */
  123. -- /* This call is very important; it requests the next motion event.
  124. -- * If you don't call gdk_window_get_pointer() you'll only get
  125. -- * a single motion event. The reason is that we specified
  126. -- * GDK_POINTER_MOTION_HINT_MASK to gtk_widget_set_events().
  127. -- * If we hadn't specified that, we could just use event->x, event->y
  128. -- * as the pointer location. But we'd also get deluged in events.
  129. -- * By requesting the next event as we handle the current one,
  130. -- * we avoid getting a huge number of events faster than we
  131. -- * can cope.
  132. -- */
  133. -- gdk_window_get_pointer (event->window, &x, &y, &state);
  134. -- if (state & GDK_BUTTON1_MASK)
  135. -- draw_brush (widget, x, y);
  136. -- /* We've handled it, stop processing */
  137. -- return TRUE;
  138. -- }
  139. -- static gboolean
  140. -- checkerboard_expose (GtkWidget *da,
  141. -- GdkEventExpose *event,
  142. -- gpointer data)
  143. -- {
  144. -- gint i, j, xcount, ycount;
  145. -- GdkGC *gc1, *gc2;
  146. -- GdkColor color;
  147. -- #define CHECK_SIZE 10
  148. -- #define SPACING 2
  149. -- /* At the start of an expose handler, a clip region of event->area
  150. -- * is set on the window, and event->area has been cleared to the
  151. -- * widget's background color. The docs for
  152. -- * gdk_window_begin_paint_region() give more details on how this
  153. -- * works.
  154. -- */
  155. -- /* It would be a bit more efficient to keep these
  156. -- * GC's around instead of recreating on each expose, but
  157. -- * this is the lazy/slow way.
  158. -- */
  159. -- gc1 = gdk_gc_new (da->window);
  160. -- color.red = 30000;
  161. -- color.green = 0;
  162. -- color.blue = 30000;
  163. -- gdk_gc_set_rgb_fg_color (gc1, &color);
  164. -- gc2 = gdk_gc_new (da->window);
  165. -- color.red = 65535;
  166. -- color.green = 65535;
  167. -- color.blue = 65535;
  168. -- gdk_gc_set_rgb_fg_color (gc2, &color);
  169. -- xcount = 0;
  170. -- i = SPACING;
  171. -- while (i < da->allocation.width)
  172. -- {
  173. -- j = SPACING;
  174. -- ycount = xcount % 2; /* start with even/odd depending on row */
  175. -- while (j < da->allocation.height)
  176. -- {
  177. -- GdkGC *gc;
  178. -- if (ycount % 2)
  179. -- gc = gc1;
  180. -- else
  181. -- gc = gc2;
  182. -- /* If we're outside event->area, this will do nothing.
  183. -- * It might be mildly more efficient if we handled
  184. -- * the clipping ourselves, but again we're feeling lazy.
  185. -- */
  186. -- gdk_draw_rectangle (da->window,
  187. -- gc,
  188. -- TRUE,
  189. -- i, j,
  190. -- CHECK_SIZE,
  191. -- CHECK_SIZE);
  192. -- j += CHECK_SIZE + SPACING;
  193. -- ++ycount;
  194. -- }
  195. -- i += CHECK_SIZE + SPACING;
  196. -- ++xcount;
  197. -- }
  198. -- g_object_unref (gc1);
  199. -- g_object_unref (gc2);
  200. -- /* return TRUE because we've handled this event, so no
  201. -- * further processing is required.
  202. -- */
  203. -- return TRUE;
  204. -- }
  205. -- GtkWidget *
  206. -- do_drawingarea (GtkWidget *do_widget)
  207. -- {
  208. -- GtkWidget *frame;
  209. -- GtkWidget *vbox;
  210. -- GtkWidget *da;
  211. -- GtkWidget *label;
  212. -- if (!window)
  213. -- {
  214. -- window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  215. -- gtk_window_set_screen (GTK_WINDOW (window),
  216. -- gtk_widget_get_screen (do_widget));
  217. -- gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
  218. -- g_signal_connect (window, "destroy", G_CALLBACK (gtk_widget_destroyed), &window);
  219. -- gtk_container_set_border_width (GTK_CONTAINER (window), 8);
  220. -- vbox = gtk_vbox_new (FALSE, 8);
  221. -- gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
  222. -- gtk_container_add (GTK_CONTAINER (window), vbox);
  223. -- /*
  224. -- * Create the checkerboard area
  225. -- */
  226. -- label = gtk_label_new (NULL);
  227. -- gtk_label_set_markup (GTK_LABEL (label),
  228. -- "<u>Checkerboard pattern</u>");
  229. -- gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  230. -- frame = gtk_frame_new (NULL);
  231. -- gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  232. -- gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
  233. -- da = gtk_drawing_area_new ();
  234. -- /* set a minimum size */
  235. -- gtk_widget_set_size_request (da, 100, 100);
  236. -- gtk_container_add (GTK_CONTAINER (frame), da);
  237. -- g_signal_connect (da, "expose_event",
  238. -- G_CALLBACK (checkerboard_expose), NULL);
  239. -- /*
  240. -- * Create the scribble area
  241. -- */
  242. -- label = gtk_label_new (NULL);
  243. -- gtk_label_set_markup (GTK_LABEL (label),
  244. -- "<u>Scribble area</u>");
  245. -- gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  246. -- frame = gtk_frame_new (NULL);
  247. -- gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  248. -- gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
  249. -- da = gtk_drawing_area_new ();
  250. -- /* set a minimum size */
  251. -- gtk_widget_set_size_request (da, 100, 100);
  252. -- gtk_container_add (GTK_CONTAINER (frame), da);
  253. -- /* Signals used to handle backing pixmap */
  254. -- g_signal_connect (da, "expose_event",
  255. -- G_CALLBACK (scribble_expose_event), NULL);
  256. -- g_signal_connect (da,"configure_event",
  257. -- G_CALLBACK (scribble_configure_event), NULL);
  258. -- /* Event signals */
  259. -- g_signal_connect (da, "motion_notify_event",
  260. -- G_CALLBACK (scribble_motion_notify_event), NULL);
  261. -- g_signal_connect (da, "button_press_event",
  262. -- G_CALLBACK (scribble_button_press_event), NULL);
  263. -- /* Ask to receive events the drawing area doesn't normally
  264. -- * subscribe to
  265. -- */
  266. -- gtk_widget_set_events (da, gtk_widget_get_events (da)
  267. -- | GDK_LEAVE_NOTIFY_MASK
  268. -- | GDK_BUTTON_PRESS_MASK
  269. -- | GDK_POINTER_MOTION_MASK
  270. -- | GDK_POINTER_MOTION_HINT_MASK);
  271. -- }
  272. -- if (!GTK_WIDGET_VISIBLE (window))
  273. -- {
  274. -- gtk_widget_show_all (window);
  275. -- }
  276. -- else
  277. -- {
  278. -- gtk_widget_destroy (window);
  279. -- window = NULL;
  280. -- }
  281. -- return window;
  282. -- }
  283. end