gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
DrawingArea.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16 */
17 #ifndef DRAWINGAREA_H_
18 #define DRAWINGAREA_H_
19 
20 #include <gtk/gtk.h>
21 #include "Surface.H"
22 
29 class DrawingArea : public Surface {
30 
37  static gint buttonPressEvent(GtkWidget *widget, GdkEventButton *event, gpointer data) {
38  DrawingArea *da=static_cast<DrawingArea*>(data);
39  if (event->button == 1 && da->getSurface() != NULL)
40  da->draw(widget, event->x, event->y);
41  if (event->button == 3 && da->getSurface() != NULL)
42  da->erase(widget, event->x, event->y);
43  return FALSE;
44  }
45 
48  static gint motionNotifyEvent(GtkWidget *widget, GdkEventMotion *event, gpointer data) {
49  DrawingArea *da=static_cast<DrawingArea*>(data);
50  int x, y;
51  GdkModifierType state;
52 
53  if (event->is_hint)
54  gdk_window_get_pointer (event->window, &x, &y, &state);
55  else {
56  x = event->x;
57  y = event->y;
58  state = (GdkModifierType)event->state;
59  }
60 
61  if (state & GDK_BUTTON1_MASK && da->getSurface() != NULL)
62  da->draw (widget, x, y);
63  else if (state & GDK_BUTTON3_MASK && da->getSurface() != NULL)
64  da->erase (widget, x, y);
65  else if (da->getSurface() != NULL)
66  da->move (widget, x, y);
67  return TRUE;
68  }
69 
72  static gint leaveEventStatic(GtkWidget *widget, GdkEventMotion *event, gpointer data) {
73  DrawingArea *da=static_cast<DrawingArea*>(data);
74  da->leaveEvent();
75  return TRUE;
76  }
77 
78 
81  void init(void){
82  connect("motion_notify_event", G_CALLBACK(motionNotifyEvent), this);
83  connectDrawEvent(G_CALLBACK(buttonPressEvent), this);
84  connect("leave_notify_event", G_CALLBACK(leaveEventStatic), this);
85  gtk_widget_set_events (widget, GDK_EXPOSURE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
86  }
87 
94  virtual void move(GtkWidget *widget, gdouble x, gdouble y) {
95  // does nothing.
96  }
97 
100  virtual void leaveEvent() {
101  //cout<<"DrawingArea::leaveEvent"<<endl;
102  // does nothing.
103  }
104 
110  virtual void draw(GtkWidget *widget, gdouble x, gdouble y) {
111  GdkRectangle update_rect;
112 
113  update_rect.x = x - 5;
114  update_rect.y = y - 5;
115  update_rect.width = 10;
116  update_rect.height = 10;
117  cairo_t *cr=cairo_create(surface);
118  cairo_set_source_rgb(cr, 1., 1., 1.); // set the rectangle to white
119  cairo_rectangle(cr, update_rect.x, update_rect.y, update_rect.width, update_rect.height); // draw the rectangle on the surface
120  cairo_fill(cr);
121  cairo_destroy(cr);
122  gtk_widget_queue_draw_area (widget, update_rect.x, update_rect.y, update_rect.width, update_rect.height);
123 // cout<<"cairo_surface_status(surface) " <<cairo_surface_status(surface)<<endl;
124 // cout<<"width = "<<cairo_image_surface_get_width(surface)<<endl;
125 // cout<<"height = "<<cairo_image_surface_get_height(surface)<<endl;
126  }
127 
133  virtual void erase(GtkWidget *widget, gdouble x, gdouble y) {
134  GdkRectangle update_rect;
135 
136  update_rect.x = x - 5;
137  update_rect.y = y - 5;
138  update_rect.width = 10;
139  update_rect.height = 10;
140  cairo_t *cr=cairo_create(surface);
141  cairo_set_source_rgb(cr, 0., 0., 0.); // set the rectangle to white
142  cairo_rectangle(cr, update_rect.x, update_rect.y, update_rect.width, update_rect.height); // draw the rectangle on the surface
143  cairo_fill(cr);
144  cairo_destroy(cr);
145  gtk_widget_queue_draw_area (widget, update_rect.x, update_rect.y, update_rect.width, update_rect.height);
146  }
147 public:
148 
152  DrawingArea(void) : Surface() {
153  init();
154  }
155 
159  DrawingArea(int width, int height) : Surface(width, height) {
160  init();
161  }
162 
165  virtual ~DrawingArea(void) {
166  }
167 
168  void connectDrawEvent(GCallback callBack, gpointer data){
169  connectAfter("button_press_event", G_CALLBACK(callBack), data);
170  }
171 };
172 #endif // DRAWINGAREA_H_
float * x
void init(void)
Definition: DrawingArea.H:81
static gint leaveEventStatic(GtkWidget *widget, GdkEventMotion *event, gpointer data)
Definition: DrawingArea.H:72
static gint motionNotifyEvent(GtkWidget *widget, GdkEventMotion *event, gpointer data)
Definition: DrawingArea.H:48
cairo_surface_t * surface
The backing surface for the drawing area.
Definition: Surface.H:37
virtual void draw(GtkWidget *widget, gdouble x, gdouble y)
Definition: DrawingArea.H:110
virtual void erase(GtkWidget *widget, gdouble x, gdouble y)
Definition: DrawingArea.H:133
void connectDrawEvent(GCallback callBack, gpointer data)
Definition: DrawingArea.H:168
cairo_surface_t * getSurface(void)
Definition: Surface.H:204
GtkWidget * widget
The container based widget.
Definition: Widget.H:33
DrawingArea(void)
Definition: DrawingArea.H:152
float * y
virtual void move(GtkWidget *widget, gdouble x, gdouble y)
Definition: DrawingArea.H:94
DrawingArea(int width, int height)
Definition: DrawingArea.H:159
virtual void leaveEvent()
Definition: DrawingArea.H:100
void connectAfter(const char *event, GCallback callback, gpointer data)
Definition: Widget.H:233
virtual ~DrawingArea(void)
Definition: DrawingArea.H:165
void connect(const char *event, GCallback callback, gpointer data)
Definition: Widget.H:224
static gint buttonPressEvent(GtkWidget *widget, GdkEventButton *event, gpointer data)
Definition: DrawingArea.H:37
gtkIOStream: /tmp/gtkiostream/include/DrawingArea.H Source File
GTK+ IOStream  Beta