drawingarea.c 8.36 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
static gboolean
54 55 56
scribble_draw (GtkWidget *widget,
               cairo_t   *cr,
               gpointer   data)
57
{
58
  cairo_set_source_surface (cr, surface, 0, 0);
59
  cairo_paint (cr);
60

61 62 63 64 65 66
  return FALSE;
}

/* Draw a rectangle on the screen */
static void
draw_brush (GtkWidget *widget,
Matthias Clasen's avatar
Matthias Clasen committed
67 68
            gdouble    x,
            gdouble    y)
69 70
{
  GdkRectangle update_rect;
71
  cairo_t *cr;
72 73 74 75 76 77

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

78 79
  /* Paint to the surface, where we store our state */
  cr = cairo_create (surface);
80 81 82

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

84 85
  cairo_destroy (cr);

86
  /* Now invalidate the affected region of the drawing area. */
87
  gdk_window_invalidate_rect (gtk_widget_get_window (widget),
Matthias Clasen's avatar
Matthias Clasen committed
88 89
                              &update_rect,
                              FALSE);
90 91 92
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
93 94 95
scribble_button_press_event (GtkWidget      *widget,
                             GdkEventButton *event,
                             gpointer        data)
96
{
97
  if (surface == NULL)
98
    return FALSE; /* paranoia check, in case we haven't gotten a configure event */
99

100
  if (event->button == GDK_BUTTON_PRIMARY)
101 102 103 104 105 106 107
    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
108 109 110
scribble_motion_notify_event (GtkWidget      *widget,
                              GdkEventMotion *event,
                              gpointer        data)
111 112 113 114
{
  int x, y;
  GdkModifierType state;

115
  if (surface == NULL)
116 117 118 119 120 121 122 123 124 125 126 127
    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.
   */
128

129
  gdk_window_get_device_position (event->window, event->device, &x, &y, &state);
130

131 132 133 134 135 136 137 138 139
  if (state & GDK_BUTTON1_MASK)
    draw_brush (widget, x, y);

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


static gboolean
140 141 142
checkerboard_draw (GtkWidget *da,
                   cairo_t   *cr,
                   gpointer   data)
143
{
144
  gint i, j, xcount, ycount, width, height;
145

146 147
#define CHECK_SIZE 10
#define SPACING 2
148

149 150
  /* At the start of a draw handler, a clip region has been set on
   * the Cairo context, and the contents have been cleared to the
151 152 153 154 155 156
   * widget's background color. The docs for
   * gdk_window_begin_paint_region() give more details on how this
   * works.
   */

  xcount = 0;
157 158
  width = gtk_widget_get_allocated_width (da);
  height = gtk_widget_get_allocated_height (da);
159
  i = SPACING;
160
  while (i < width)
161 162 163
    {
      j = SPACING;
      ycount = xcount % 2; /* start with even/odd depending on row */
164
      while (j < height)
Matthias Clasen's avatar
Matthias Clasen committed
165 166
        {
          if (ycount % 2)
167
            cairo_set_source_rgb (cr, 0.45777, 0, 0.45777);
Matthias Clasen's avatar
Matthias Clasen committed
168
          else
169
            cairo_set_source_rgb (cr, 1, 1, 1);
Matthias Clasen's avatar
Matthias Clasen committed
170

171
          /* If we're outside the clip, this will do nothing.
Matthias Clasen's avatar
Matthias Clasen committed
172
           */
173 174
          cairo_rectangle (cr, i, j, CHECK_SIZE, CHECK_SIZE);
          cairo_fill (cr);
Matthias Clasen's avatar
Matthias Clasen committed
175 176 177 178

          j += CHECK_SIZE + SPACING;
          ++ycount;
        }
179 180 181 182

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

184 185 186 187 188 189
  /* return TRUE because we've handled this event, so no
   * further processing is required.
   */
  return TRUE;
}

190 191 192 193 194
static void
close_window (void)
{
  window = NULL;

195
  if (surface)
196
    cairo_surface_destroy (surface);
197
  surface = NULL;
198 199
}

200
GtkWidget *
201
do_drawingarea (GtkWidget *do_widget)
202 203 204 205 206
{
  GtkWidget *frame;
  GtkWidget *vbox;
  GtkWidget *da;
  GtkWidget *label;
207

208 209 210
  if (!window)
    {
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
211
      gtk_window_set_screen (GTK_WINDOW (window),
Matthias Clasen's avatar
Matthias Clasen committed
212
                             gtk_widget_get_screen (do_widget));
213 214
      gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");

215 216
      g_signal_connect (window, "destroy",
                        G_CALLBACK (close_window), NULL);
217 218 219

      gtk_container_set_border_width (GTK_CONTAINER (window), 8);

220
      vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
221 222 223 224 225 226
      gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
      gtk_container_add (GTK_CONTAINER (window), vbox);

      /*
       * Create the checkerboard area
       */
227

228 229
      label = gtk_label_new (NULL);
      gtk_label_set_markup (GTK_LABEL (label),
Matthias Clasen's avatar
Matthias Clasen committed
230
                            "<u>Checkerboard pattern</u>");
231
      gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
232

233 234 235
      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);
236

237 238
      da = gtk_drawing_area_new ();
      /* set a minimum size */
239
      gtk_widget_set_size_request (da, 100, 100);
240 241 242

      gtk_container_add (GTK_CONTAINER (frame), da);

243 244
      g_signal_connect (da, "draw",
                        G_CALLBACK (checkerboard_draw), NULL);
245 246 247 248

      /*
       * Create the scribble area
       */
249

250 251
      label = gtk_label_new (NULL);
      gtk_label_set_markup (GTK_LABEL (label),
Matthias Clasen's avatar
Matthias Clasen committed
252
                            "<u>Scribble area</u>");
253
      gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
254

255 256 257
      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);
258

259 260
      da = gtk_drawing_area_new ();
      /* set a minimum size */
261
      gtk_widget_set_size_request (da, 100, 100);
262 263 264

      gtk_container_add (GTK_CONTAINER (frame), da);

265
      /* Signals used to handle backing surface */
266

267 268
      g_signal_connect (da, "draw",
                        G_CALLBACK (scribble_draw), NULL);
269
      g_signal_connect (da,"configure-event",
Matthias Clasen's avatar
Matthias Clasen committed
270
                        G_CALLBACK (scribble_configure_event), NULL);
271

272
      /* Event signals */
273 274

      g_signal_connect (da, "motion-notify-event",
Matthias Clasen's avatar
Matthias Clasen committed
275
                        G_CALLBACK (scribble_motion_notify_event), NULL);
276
      g_signal_connect (da, "button-press-event",
Matthias Clasen's avatar
Matthias Clasen committed
277
                        G_CALLBACK (scribble_button_press_event), NULL);
278 279 280 281 282 283


      /* 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
284 285 286 287
                             | GDK_LEAVE_NOTIFY_MASK
                             | GDK_BUTTON_PRESS_MASK
                             | GDK_POINTER_MOTION_MASK
                             | GDK_POINTER_MOTION_HINT_MASK);
288 289 290

    }

291
  if (!gtk_widget_get_visible (window))
292 293 294 295 296 297
      gtk_widget_show_all (window);
  else
      gtk_widget_destroy (window);

  return window;
}