Debian Codegraph @

demos/gtk-demo/example_drawingarea.cc

raw
1 /* Drawing Area
2 *
3 * GtkDrawingArea is a blank area where you can draw custom displays
4 * of various kinds.
5 *
6 * This demo has two drawing areas. The checkerboard area shows
7 * how you can just draw something; all you have to do is write
8 * a signal handler for draw, as shown here.
9 *
10 * The "scribble" area is a bit more advanced, and shows how to handle
11 * events such as button presses and mouse motion. Click the mouse
12 * and drag in the scribble area to draw squiggles. Resize the window
13 * to clear the area.
14 */
15
16 #include <gtkmm.h>
17
18 class Example_DrawingArea : public Gtk::Window
19 {
20 public:
21 Example_DrawingArea();
22 ~Example_DrawingArea() override;
23
24 protected:
25 //signal handlers:
26 bool on_drawingarea_checkerboard_draw(const Cairo::RefPtr<Cairo::Context>& cr);
27 bool on_drawingarea_scribble_draw(const Cairo::RefPtr<Cairo::Context>& cr);
28 bool on_drawingarea_scribble_configure_event(GdkEventConfigure* event);
29 bool on_drawingarea_scribble_motion_notify_event(GdkEventMotion* event);
30 bool on_drawingarea_scribble_button_press_event(GdkEventButton* event);
31
32 void scribble_draw_brush(int x, int y);
33
34 //Member widgets:
35 Gtk::Frame m_Frame_Checkerboard, m_Frame_Scribble;
36 Gtk::Box m_VBox;
37 Gtk::Label m_Label_Checkerboard, m_Label_Scribble;
38 Gtk::DrawingArea m_DrawingArea_Checkerboard, m_DrawingArea_Scribble;
39
40 Cairo::RefPtr<Cairo::Surface> m_surface;
41 };
42
43 //Called by DemoWindow;
44 Gtk::Window* do_drawingarea()
45 {
46 return new Example_DrawingArea();
47 }
48
49 Example_DrawingArea::Example_DrawingArea()
50 :
51 m_VBox(Gtk::ORIENTATION_VERTICAL, 8)
52 {
53 set_title("Drawing Area");
54 set_border_width(8);
55
56 m_VBox.set_border_width(8);
57 add(m_VBox);
58
59 /*
60 * Create the checkerboard area
61 */
62 m_Label_Checkerboard.set_markup("<u>Checkerboard pattern</u>");
63 m_VBox.pack_start(m_Label_Checkerboard, Gtk::PACK_SHRINK);
64
65 m_Frame_Checkerboard.set_shadow_type(Gtk::SHADOW_IN);
66 m_VBox.pack_start(m_Frame_Checkerboard);
67
68 /* set a minimum size */
69 m_DrawingArea_Checkerboard.set_size_request(100, 100);
70 m_Frame_Checkerboard.add(m_DrawingArea_Checkerboard);
71
72 m_DrawingArea_Checkerboard.signal_draw().connect(
73 sigc::mem_fun(*this, &Example_DrawingArea::on_drawingarea_checkerboard_draw));
74
75 /*
76 * Create the scribble area
77 */
78 m_Label_Scribble.set_markup("<u>Scribble area</u>");
79 m_VBox.pack_start(m_Label_Scribble, Gtk::PACK_SHRINK);
80
81 m_Frame_Scribble.set_shadow_type(Gtk::SHADOW_IN);
82 m_VBox.pack_start(m_Frame_Scribble);
83
84 /* set a minimum size */
85 m_DrawingArea_Scribble.set_size_request(100, 100);
86 m_Frame_Scribble.add(m_DrawingArea_Scribble);
87
88 /* Signals used to handle backing pixmap */
89 m_DrawingArea_Scribble.signal_draw().connect(
90 sigc::mem_fun(*this, &Example_DrawingArea::on_drawingarea_scribble_draw));
91 m_DrawingArea_Scribble.signal_configure_event().connect(
92 sigc::mem_fun(*this, &Example_DrawingArea::on_drawingarea_scribble_configure_event));
93
94 /* Event signals */
95 m_DrawingArea_Scribble.signal_motion_notify_event().connect(
96 sigc::mem_fun(*this, &Example_DrawingArea::on_drawingarea_scribble_motion_notify_event));
97 m_DrawingArea_Scribble.signal_button_press_event().connect(
98 sigc::mem_fun(*this, &Example_DrawingArea::on_drawingarea_scribble_button_press_event));
99
100 /* Ask to receive events the drawing area doesn't normally
101 * subscribe to.
102 */
103 m_DrawingArea_Scribble.add_events(Gdk::LEAVE_NOTIFY_MASK |
104 Gdk::BUTTON_PRESS_MASK |
105 Gdk::POINTER_MOTION_MASK |
106 Gdk::POINTER_MOTION_HINT_MASK);
107 show_all();
108 }
109
110 Example_DrawingArea::~Example_DrawingArea()
111 {
112 }
113
114 bool Example_DrawingArea::on_drawingarea_checkerboard_draw(const Cairo::RefPtr<Cairo::Context>& cr)
115 {
116 enum { CHECK_SIZE = 10, SPACING = 2 };
117
118 /* At the start of a draw handler, a clip region has been set on
119 * the Cairo context, and the contents have been cleared to the
120 * widget's background color. The docs for
121 * Gdk::Window::begin_paint_region() give more details on how this
122 * works.
123 */
124
125 int xcount = 0;
126
127 const int width = m_DrawingArea_Checkerboard.get_allocated_width();
128 const int height = m_DrawingArea_Checkerboard.get_allocated_height();
129 int i = SPACING;
130 while (i < width)
131 {
132 int j = SPACING;
133 int ycount = xcount % 2; /* start with even/odd depending on row */
134 while (j < height)
135 {
136 if (ycount % 2)
137 cr->set_source_rgb(0.45777, 0, 0.45777);
138 else
139 cr->set_source_rgb(1, 1, 1);
140
141 /* If we're outside event->area, this will do nothing.
142 * It might be mildly more efficient if we handled
143 * the clipping ourselves, but again we're feeling lazy.
144 */
145 cr->rectangle(i, j, CHECK_SIZE, CHECK_SIZE);
146 cr->fill();
147
148 j += CHECK_SIZE + SPACING;
149 ++ycount;
150 }
151
152 i += CHECK_SIZE + SPACING;
153 ++xcount;
154 }
155
156 /* return true because we've handled this event, so no
157 * further processing is required.
158 */
159 return true;
160 }
161
162 bool Example_DrawingArea::on_drawingarea_scribble_draw(const Cairo::RefPtr<Cairo::Context>& cr)
163 {
164 cr->set_source(m_surface, 0, 0); //TODO: Add =0 default parameters to cairomm.
165 cr->paint();
166
167 return false;
168 }
169
170 bool Example_DrawingArea::on_drawingarea_scribble_configure_event(GdkEventConfigure*)
171 {
172 const Gtk::Allocation allocation = m_DrawingArea_Scribble.get_allocation();
173 m_surface =
174 m_DrawingArea_Scribble.get_window()->create_similar_surface(
175 Cairo::CONTENT_COLOR, allocation.get_width(), allocation.get_height());
176 Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(m_surface);
177 cr->set_source_rgb(1, 1, 1);
178 cr->paint();
179
180 /* We've handled the configure event, no need for further processing. */
181 return true;
182 }
183
184 bool Example_DrawingArea::on_drawingarea_scribble_motion_notify_event(GdkEventMotion* motion_event)
185 {
186 if(!m_surface)
187 return false; // paranoia check, in case we haven't gotten a configure event
188
189 /* This call is very important; it requests the next motion event. If you
190 * don't call Gdk::Window::get_device_position() you'll only get a single motion
191 * event. The reason is that we specified Gdk::POINTER_MOTION_HINT_MASK to
192 * Gtk::Widget::add_events(). If we hadn't specified that, we could just use
193 * motion_event->x, motion_event->y as the pointer location. But we'd also get deluged in
194 * events. By requesting the next event as we handle the current one, we
195 * avoid getting a huge number of events faster than we can cope.
196 */
197 if(motion_event && motion_event->window)
198 {
199 const Glib::RefPtr<Gdk::Window> refWindow =
200 Glib::wrap(motion_event->window, true); // true == take_copy
201
202 if(refWindow)
203 {
204 int x = 0, y = 0;
205 Gdk::ModifierType state = Gdk::ModifierType(0);
206 const Glib::RefPtr<const Gdk::Device> device =
207 Glib::wrap(motion_event->device, true); // true == take_copy
208 refWindow->get_device_position(device, x, y, state);
209
210 if((state & Gdk::BUTTON1_MASK) != 0)
211 scribble_draw_brush(x, y);
212 }
213 }
214
215 // We've handled it, stop processing.
216 return true;
217 }
218
219 bool Example_DrawingArea::on_drawingarea_scribble_button_press_event(GdkEventButton* button_event)
220 {
221 if(!m_surface)
222 return false; // paranoia check, in case we haven't gotten a configure event
223
224 if(button_event->button == 1)
225 scribble_draw_brush(int(button_event->x), int(button_event->y));
226
227 // We've handled the event, stop processing.
228 return true;
229 }
230
231 /* Draw a rectangle on the screen.
232 */
233 void Example_DrawingArea::scribble_draw_brush(int x, int y)
234 {
235 const Gdk::Rectangle update_rect (x - 3, y - 3, 6, 6);
236
237 Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(m_surface);
238 Gdk::Cairo::add_rectangle_to_path(cr, update_rect);
239 cr->fill();
240
241 // Now invalidate the affected region of the drawing area.
242 m_DrawingArea_Scribble.get_window()->invalidate_rect(update_rect, false);
243 }