1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* Paint
 *
 * Demonstrates practical handling of drawing tablets in a real world
 * usecase.
 */
#include <ctk/ctk.h>

typedef struct
{
  CtkEventBox parent_instance;
  cairo_surface_t *surface;
  cairo_t *cr;
  CdkRGBA draw_color;

  CtkGesture *stylus_gesture;
} DrawingArea;

typedef struct
{
  CtkEventBoxClass parent_class;
} DrawingAreaClass;

G_DEFINE_TYPE (DrawingArea, drawing_area, CTK_TYPE_EVENT_BOX)<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_TYPE is a macro then please configure it.

static void
drawing_area_ensure_surface (DrawingArea *area,
                             gint         width,
                             gint         height)
{
  if (!area->surface ||
      cairo_image_surface_get_width (area->surface) != width ||
      cairo_image_surface_get_height (area->surface) != height)
    {
      cairo_surface_t *surface;

      surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                            width, height);
      if (area->surface)
        {
          cairo_t *cr;

          cr = cairo_create (surface);
          cairo_set_source_surface (cr, area->surface, 0, 0);
          cairo_paint (cr);

          cairo_surface_destroy (area->surface);
          cairo_destroy (area->cr);
          cairo_destroy (cr);
        }

      area->surface = surface;
      area->cr = cairo_create (surface);
    }
}

static void
drawing_area_size_allocate (CtkWidget     *widget,
                            CtkAllocation *allocation)
{
  DrawingArea *area = (DrawingArea *) widget;

  drawing_area_ensure_surface (area, allocation->width, allocation->height);

  CTK_WIDGET_CLASS (drawing_area_parent_class)->size_allocate (widget, allocation);
}

static void
drawing_area_map (CtkWidget *widget)
{
  CtkAllocation allocation;

  CTK_WIDGET_CLASS (drawing_area_parent_class)->map (widget);

  cdk_window_set_event_compression (ctk_widget_get_window (widget), TRUE);

  ctk_widget_get_allocation (widget, &allocation);
  drawing_area_ensure_surface ((DrawingArea *) widget,
                               allocation.width, allocation.height);
}

static void
drawing_area_unmap (CtkWidget *widget)
{
  DrawingArea *area = (DrawingArea *) widget;

  g_clear_pointer (&area->cr, cairo_destroy);
  g_clear_pointer (&area->surface, cairo_surface_destroy);

  CTK_WIDGET_CLASS (drawing_area_parent_class)->unmap (widget);
}

static gboolean
drawing_area_draw (CtkWidget *widget,
		   cairo_t   *cr)
{
  DrawingArea *area = (DrawingArea *) widget;
  CtkAllocation allocation;

  ctk_widget_get_allocation (widget, &allocation);

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

  cairo_set_source_surface (cr, area->surface, 0, 0);
  cairo_paint (cr);

  cairo_set_source_rgb (cr, 0.6, 0.6, 0.6);
  cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
  cairo_stroke (cr);

  return TRUE;
}

static void
drawing_area_class_init (DrawingAreaClass *klass)
{
  CtkWidgetClass *widget_class = CTK_WIDGET_CLASS (klass);

  widget_class->size_allocate = drawing_area_size_allocate;
  widget_class->draw = drawing_area_draw;
  widget_class->map = drawing_area_map;
  widget_class->unmap = drawing_area_unmap;
}

static void
drawing_area_apply_stroke (DrawingArea   *area,
                           CdkDeviceTool *tool,
                           gdouble        x,
                           gdouble        y,
                           gdouble        pressure)
{
  if (cdk_device_tool_get_tool_type (tool) == CDK_DEVICE_TOOL_TYPE_ERASER)
    {
      cairo_set_line_width (area->cr, 10 * pressure);
      cairo_set_operator (area->cr, CAIRO_OPERATOR_DEST_OUT);
    }
  else
    {
      cairo_set_line_width (area->cr, 4 * pressure);
      cairo_set_operator (area->cr, CAIRO_OPERATOR_SATURATE);
    }

  cairo_set_source_rgba (area->cr, area->draw_color.red,
                         area->draw_color.green, area->draw_color.blue,
                         area->draw_color.alpha * pressure);

  //cairo_set_source_rgba (area->cr, 0, 0, 0, pressure);

  cairo_line_to (area->cr, x, y);
  cairo_stroke (area->cr);
  cairo_move_to (area->cr, x, y);
}

static void
stylus_gesture_down (CtkGestureStylus *gesture,
                     gdouble           x,
                     gdouble           y,
                     DrawingArea      *area)
{
  cairo_new_path (area->cr);
}

static void
stylus_gesture_motion (CtkGestureStylus *gesture,
                       gdouble           x,
                       gdouble           y,
                       DrawingArea      *area)
{
  CdkDeviceTool *tool;
  gdouble pressure;

  tool = ctk_gesture_stylus_get_device_tool (gesture);

  if (!ctk_gesture_stylus_get_axis (gesture, CDK_AXIS_PRESSURE, &pressure))
    pressure = 1;

  drawing_area_apply_stroke (area, tool, x, y, pressure);
  ctk_widget_queue_draw (CTK_WIDGET (area));
}

static void
drawing_area_init (DrawingArea *area)
{
  const CdkRGBA draw_rgba = { 0, 0, 0, 1 };
  ctk_event_box_set_visible_window (CTK_EVENT_BOX (area), TRUE);

  area->stylus_gesture = ctk_gesture_stylus_new (CTK_WIDGET (area));
  g_signal_connect (area->stylus_gesture, "down",
                    G_CALLBACK (stylus_gesture_down), area);
  g_signal_connect (area->stylus_gesture, "motion",
                    G_CALLBACK (stylus_gesture_motion), area);

  area->draw_color = draw_rgba;
}

CtkWidget *
drawing_area_new (void)
{
  return g_object_new (drawing_area_get_type (), NULL);
}

void
drawing_area_set_color (DrawingArea *area,
                        CdkRGBA     *color)
{
  area->draw_color = *color;
}

static void
color_button_color_set (CtkColorButton *button,
                        DrawingArea    *draw_area)
{
  CdkRGBA color;

  ctk_color_chooser_get_rgba (CTK_COLOR_CHOOSER (button), &color);
  drawing_area_set_color (draw_area, &color);
}

CtkWidget *
do_paint (CtkWidget *toplevel)
{
  static CtkWidget *window = NULL;

  if (!window)
    {
      CtkWidget *draw_area, *headerbar, *colorbutton;
      const CdkRGBA draw_rgba = { 0, 0, 0, 1 };

      window = ctk_window_new (CTK_WINDOW_TOPLEVEL);

      draw_area = drawing_area_new ();
      ctk_container_add (CTK_CONTAINER (window), draw_area);

      headerbar = ctk_header_bar_new ();
      ctk_header_bar_set_title (CTK_HEADER_BAR (headerbar), "Paint");
      ctk_header_bar_set_show_close_button (CTK_HEADER_BAR (headerbar), TRUE);

      colorbutton = ctk_color_button_new ();
      g_signal_connect (colorbutton, "color-set",
                        G_CALLBACK (color_button_color_set), draw_area);
      ctk_color_chooser_set_rgba (CTK_COLOR_CHOOSER (colorbutton),
                                  &draw_rgba);

      ctk_header_bar_pack_end (CTK_HEADER_BAR (headerbar), colorbutton);
      ctk_window_set_titlebar (CTK_WINDOW (window), headerbar);

      g_signal_connect (window, "destroy",
                        G_CALLBACK (ctk_widget_destroyed), &window);
    }

  if (!ctk_widget_get_visible (window))
    ctk_widget_show_all (window);
  else
    ctk_widget_destroy (window);

  return window;
}