drawing.c 4.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <gtk/gtk.h>

/* Surface to store current scribbles */
static cairo_surface_t *surface = NULL;

static void
clear_surface (void)
{
  cairo_t *cr;

  cr = cairo_create (surface);

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

  cairo_destroy (cr);
}

/* Create a new surface of the appropriate size to store our scribbles */
20 21 22 23 24
static void
size_allocate_cb (GtkWidget     *widget,
                  GtkAllocation *alloc,
                  int            baseline,
                  gpointer       data)
25 26
{
  if (surface)
27 28 29 30
    {
      cairo_surface_destroy (surface);
      surface = NULL;
    }
31

32
  if (gtk_native_get_surface (gtk_widget_get_native (widget)))
33
    {
34
      surface = gdk_surface_create_similar_surface (gtk_native_get_surface (gtk_widget_get_native (widget)),
35 36 37
                                                   CAIRO_CONTENT_COLOR,
                                                   gtk_widget_get_width (widget),
                                                   gtk_widget_get_height (widget));
38

39 40 41
      /* Initialize the surface to white */
      clear_surface ();
    }
42 43
}

44 45
/* Redraw the screen from the surface. Note that the draw
 * callback receives a ready-to-be-used cairo_t that is already
46 47
 * clipped to only draw the exposed areas of the widget
 */
48 49 50 51 52 53
static void
draw_cb (GtkDrawingArea *drawing_area,
         cairo_t        *cr,
         int             width,
         int             height,
         gpointer        data)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
{
  cairo_set_source_surface (cr, surface, 0, 0);
  cairo_paint (cr);
}

/* Draw a rectangle on the surface at the given position */
static void
draw_brush (GtkWidget *widget,
            gdouble    x,
            gdouble    y)
{
  cairo_t *cr;

  /* Paint to the surface, where we store our state */
  cr = cairo_create (surface);

  cairo_rectangle (cr, x - 3, y - 3, 6, 6);
  cairo_fill (cr);

  cairo_destroy (cr);

75 76
  /* Now invalidate the drawing area. */
  gtk_widget_queue_draw (widget);
77 78
}

79 80
static double start_x;
static double start_y;
81

82 83 84 85 86 87 88 89
static void
drag_begin (GtkGestureDrag *gesture,
            double          x,
            double          y,
            GtkWidget      *area)
{
  start_x = x;
  start_y = y;
90

91
  draw_brush (area, x, y);
92 93
}

94 95 96 97 98
static void
drag_update (GtkGestureDrag *gesture,
             double          x,
             double          y,
             GtkWidget      *area)
99
{
100 101
  draw_brush (area, start_x + x, start_y + y);
}
Matthias Clasen's avatar
Matthias Clasen committed
102

103 104 105 106 107 108 109 110
static void
drag_end (GtkGestureDrag *gesture,
          double          x,
          double          y,
          GtkWidget      *area)
{
  draw_brush (area, start_x + x, start_y + y);
}
111

112
static void
113 114 115 116 117
pressed (GtkGestureClick *gesture,
         int              n_press,
         double           x,
         double           y,
         GtkWidget       *area)
118 119 120
{
  clear_surface ();
  gtk_widget_queue_draw (area);
121 122 123 124 125 126 127 128 129
}

static void
close_window (void)
{
  if (surface)
    cairo_surface_destroy (surface);
}

130 131 132
static void
activate (GtkApplication *app,
          gpointer        user_data)
133 134 135
{
  GtkWidget *window;
  GtkWidget *frame;
136
  GtkWidget *drawing_area;
137 138
  GtkGesture *drag;
  GtkGesture *press;
139

140
  window = gtk_application_window_new (app);
141 142 143 144 145 146 147 148
  gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");

  g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);

  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_container_add (GTK_CONTAINER (window), frame);

149
  drawing_area = gtk_drawing_area_new ();
150
  /* set a minimum size */
151
  gtk_widget_set_size_request (drawing_area, 100, 100);
152

153
  gtk_container_add (GTK_CONTAINER (frame), drawing_area);
154

155 156 157 158
  gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (drawing_area), draw_cb, NULL, NULL);

  g_signal_connect_after (drawing_area, "size-allocate",
                          G_CALLBACK (size_allocate_cb), NULL);
159

160
  drag = gtk_gesture_drag_new ();
161
  gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (drag), GDK_BUTTON_PRIMARY);
162
  gtk_widget_add_controller (drawing_area, GTK_EVENT_CONTROLLER (drag));
163 164
  g_signal_connect (drag, "drag-begin", G_CALLBACK (drag_begin), drawing_area);
  g_signal_connect (drag, "drag-update", G_CALLBACK (drag_update), drawing_area);
165
  g_signal_connect (drag, "drag-end", G_CALLBACK (drag_end), drawing_area);
166

167
  press = gtk_gesture_click_new ();
168
  gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (press), GDK_BUTTON_SECONDARY);
169
  gtk_widget_add_controller (drawing_area, GTK_EVENT_CONTROLLER (press));
170 171

  g_signal_connect (press, "pressed", G_CALLBACK (pressed), drawing_area);
172

Timm Bäder's avatar
Timm Bäder committed
173
  gtk_widget_show (window);
174 175 176 177 178 179 180 181
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;
182

183 184 185 186
  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);
187

188
  return status;
189
}