drawingarea.c 8.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* Drawing Area
 *
 * GtkDrawingArea is a blank area where you can draw custom displays
 * of various kinds.
 *
 * This demo has two drawing areas. The checkerboard area shows
 * how you can just draw something; all you have to do is write
 * a signal handler for expose_event, as shown here.
 *
 * The "scribble" area is a bit more advanced, and shows how to handle
 * events such as button presses and mouse motion. Click the mouse
 * and drag in the scribble area to draw squiggles. Resize the window
 * to clear the area.
 */

#include <gtk/gtk.h>

static GtkWidget *window = NULL;
/* Pixmap for scribble area, to store current scribbles */
20
static cairo_surface_t *surface = NULL;
21

22
/* Create a new surface of the appropriate size to store our scribbles */
23
static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
24 25 26
scribble_configure_event (GtkWidget         *widget,
                          GdkEventConfigure *event,
                          gpointer           data)
27
{
28
  GtkAllocation allocation;
29 30
  cairo_t *cr;

31 32
  if (surface)
    cairo_surface_destroy (surface);
33

34 35
  gtk_widget_get_allocation (widget, &allocation);
  surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
36
                                               CAIRO_CONTENT_COLOR,
37 38
                                               allocation.width,
                                               allocation.height);
39

40 41
  /* Initialize the surface to white */
  cr = cairo_create (surface);
42 43 44 45 46

  cairo_set_source_rgb (cr, 1, 1, 1);
  cairo_paint (cr);

  cairo_destroy (cr);
47 48 49 50 51

  /* We've handled the configure event, no need for further processing. */
  return TRUE;
}

52
/* Redraw the screen from the surface */
53 54
static gboolean
scribble_expose_event (GtkWidget      *widget,
Matthias Clasen's avatar
Matthias Clasen committed
55 56
                       GdkEventExpose *event,
                       gpointer        data)
57
{
58
  cairo_t *cr;
59

60
  cr = gdk_cairo_create (gtk_widget_get_window (widget));
61
  
62
  cairo_set_source_surface (cr, surface, 0, 0);
63 64 65 66
  gdk_cairo_rectangle (cr, &event->area);
  cairo_fill (cr);

  cairo_destroy (cr);
67

68 69 70 71 72 73
  return FALSE;
}

/* Draw a rectangle on the screen */
static void
draw_brush (GtkWidget *widget,
Matthias Clasen's avatar
Matthias Clasen committed
74 75
            gdouble    x,
            gdouble    y)
76 77
{
  GdkRectangle update_rect;
78
  cairo_t *cr;
79 80 81 82 83 84

  update_rect.x = x - 3;
  update_rect.y = y - 3;
  update_rect.width = 6;
  update_rect.height = 6;

85 86
  /* Paint to the surface, where we store our state */
  cr = cairo_create (surface);
87 88 89

  gdk_cairo_rectangle (cr, &update_rect);
  cairo_fill (cr);
90

91 92
  cairo_destroy (cr);

93
  /* Now invalidate the affected region of the drawing area. */
94
  gdk_window_invalidate_rect (gtk_widget_get_window (widget),
Matthias Clasen's avatar
Matthias Clasen committed
95 96
                              &update_rect,
                              FALSE);
97 98 99
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
100 101 102
scribble_button_press_event (GtkWidget      *widget,
                             GdkEventButton *event,
                             gpointer        data)
103
{
104
  if (surface == NULL)
105
    return FALSE; /* paranoia check, in case we haven't gotten a configure event */
106

107 108 109 110 111 112 113 114
  if (event->button == 1)
    draw_brush (widget, event->x, event->y);

  /* We've handled the event, stop processing */
  return TRUE;
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
115 116 117
scribble_motion_notify_event (GtkWidget      *widget,
                              GdkEventMotion *event,
                              gpointer        data)
118 119 120 121
{
  int x, y;
  GdkModifierType state;

122
  if (surface == NULL)
123 124 125 126 127 128 129 130 131 132 133 134
    return FALSE; /* paranoia check, in case we haven't gotten a configure event */

  /* This call is very important; it requests the next motion event.
   * If you don't call gdk_window_get_pointer() you'll only get
   * a single motion event. The reason is that we specified
   * GDK_POINTER_MOTION_HINT_MASK to gtk_widget_set_events().
   * If we hadn't specified that, we could just use event->x, event->y
   * as the pointer location. But we'd also get deluged in events.
   * By requesting the next event as we handle the current one,
   * we avoid getting a huge number of events faster than we
   * can cope.
   */
135

136
  gdk_window_get_pointer (event->window, &x, &y, &state);
137

138 139 140 141 142 143 144 145 146
  if (state & GDK_BUTTON1_MASK)
    draw_brush (widget, x, y);

  /* We've handled it, stop processing */
  return TRUE;
}


static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
147 148 149
checkerboard_expose (GtkWidget      *da,
                     GdkEventExpose *event,
                     gpointer        data)
150
{
151
  GtkAllocation allocation;
152
  gint i, j, xcount, ycount;
153
  cairo_t *cr;
154

155 156
#define CHECK_SIZE 10
#define SPACING 2
157

158 159 160 161 162 163 164
  /* At the start of an expose handler, a clip region of event->area
   * is set on the window, and event->area has been cleared to the
   * widget's background color. The docs for
   * gdk_window_begin_paint_region() give more details on how this
   * works.
   */

165
  cr = gdk_cairo_create (gtk_widget_get_window (da));
166 167
  gdk_cairo_rectangle (cr, &event->area);
  cairo_clip (cr);
168

169
  gtk_widget_get_allocation (da, &allocation);
170 171
  xcount = 0;
  i = SPACING;
172
  while (i < allocation.width)
173 174 175
    {
      j = SPACING;
      ycount = xcount % 2; /* start with even/odd depending on row */
176
      while (j < allocation.height)
Matthias Clasen's avatar
Matthias Clasen committed
177 178
        {
          if (ycount % 2)
179
            cairo_set_source_rgb (cr, 0.45777, 0, 0.45777);
Matthias Clasen's avatar
Matthias Clasen committed
180
          else
181
            cairo_set_source_rgb (cr, 1, 1, 1);
Matthias Clasen's avatar
Matthias Clasen committed
182 183 184 185 186

          /* If we're outside event->area, this will do nothing.
           * It might be mildly more efficient if we handled
           * the clipping ourselves, but again we're feeling lazy.
           */
187 188
          cairo_rectangle (cr, i, j, CHECK_SIZE, CHECK_SIZE);
          cairo_fill (cr);
Matthias Clasen's avatar
Matthias Clasen committed
189 190 191 192

          j += CHECK_SIZE + SPACING;
          ++ycount;
        }
193 194 195 196

      i += CHECK_SIZE + SPACING;
      ++xcount;
    }
197

198
  cairo_destroy (cr);
199

200 201 202 203 204 205
  /* return TRUE because we've handled this event, so no
   * further processing is required.
   */
  return TRUE;
}

206 207 208 209 210
static void
close_window (void)
{
  window = NULL;

211 212 213
  if (surface)
    g_object_unref (surface);
  surface = NULL;
214 215
}

216
GtkWidget *
217
do_drawingarea (GtkWidget *do_widget)
218 219 220 221 222
{
  GtkWidget *frame;
  GtkWidget *vbox;
  GtkWidget *da;
  GtkWidget *label;
223

224 225 226
  if (!window)
    {
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
227
      gtk_window_set_screen (GTK_WINDOW (window),
Matthias Clasen's avatar
Matthias Clasen committed
228
                             gtk_widget_get_screen (do_widget));
229 230
      gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");

231
      g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);
232 233 234 235 236 237 238 239 240 241

      gtk_container_set_border_width (GTK_CONTAINER (window), 8);

      vbox = gtk_vbox_new (FALSE, 8);
      gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
      gtk_container_add (GTK_CONTAINER (window), vbox);

      /*
       * Create the checkerboard area
       */
242

243 244
      label = gtk_label_new (NULL);
      gtk_label_set_markup (GTK_LABEL (label),
Matthias Clasen's avatar
Matthias Clasen committed
245
                            "<u>Checkerboard pattern</u>");
246
      gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
247

248 249 250
      frame = gtk_frame_new (NULL);
      gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
      gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
251

252 253
      da = gtk_drawing_area_new ();
      /* set a minimum size */
254
      gtk_widget_set_size_request (da, 100, 100);
255 256 257

      gtk_container_add (GTK_CONTAINER (frame), da);

258
      g_signal_connect (da, "expose-event",
Matthias Clasen's avatar
Matthias Clasen committed
259
                        G_CALLBACK (checkerboard_expose), NULL);
260 261 262 263

      /*
       * Create the scribble area
       */
264

265 266
      label = gtk_label_new (NULL);
      gtk_label_set_markup (GTK_LABEL (label),
Matthias Clasen's avatar
Matthias Clasen committed
267
                            "<u>Scribble area</u>");
268
      gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
269

270 271 272
      frame = gtk_frame_new (NULL);
      gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
      gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
273

274 275
      da = gtk_drawing_area_new ();
      /* set a minimum size */
276
      gtk_widget_set_size_request (da, 100, 100);
277 278 279

      gtk_container_add (GTK_CONTAINER (frame), da);

280
      /* Signals used to handle backing surface */
281 282

      g_signal_connect (da, "expose-event",
Matthias Clasen's avatar
Matthias Clasen committed
283
                        G_CALLBACK (scribble_expose_event), NULL);
284
      g_signal_connect (da,"configure-event",
Matthias Clasen's avatar
Matthias Clasen committed
285
                        G_CALLBACK (scribble_configure_event), NULL);
286

287
      /* Event signals */
288 289

      g_signal_connect (da, "motion-notify-event",
Matthias Clasen's avatar
Matthias Clasen committed
290
                        G_CALLBACK (scribble_motion_notify_event), NULL);
291
      g_signal_connect (da, "button-press-event",
Matthias Clasen's avatar
Matthias Clasen committed
292
                        G_CALLBACK (scribble_button_press_event), NULL);
293 294 295 296 297 298


      /* Ask to receive events the drawing area doesn't normally
       * subscribe to
       */
      gtk_widget_set_events (da, gtk_widget_get_events (da)
Matthias Clasen's avatar
Matthias Clasen committed
299 300 301 302
                             | GDK_LEAVE_NOTIFY_MASK
                             | GDK_BUTTON_PRESS_MASK
                             | GDK_POINTER_MOTION_MASK
                             | GDK_POINTER_MOTION_HINT_MASK);
303 304 305

    }

306
  if (!gtk_widget_get_visible (window))
307 308 309 310 311 312
      gtk_widget_show_all (window);
  else
      gtk_widget_destroy (window);

  return window;
}