GTK+ IOStream  Beta
<< GTK+ >> add C++ IOStream operators to GTK+. Now with extra abilities ... like network serialisation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DrawingArea.H
Go to the documentation of this file.
1 /* Copyright 2000-2013 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 "Pixmap.H"
22 
29 class DrawingArea : public Pixmap {
30 
36  static gint button_press_event (GtkWidget *widget, GdkEventButton *event, gpointer data) {
37  DrawingArea *da=static_cast<DrawingArea*>(data);
38  if (event->button == 1 && da->getPixmap() != NULL)
39  da->draw_brush(widget, event->x, event->y);
40  return TRUE;
41  }
42 
45  static gint motion_notify_event (GtkWidget *widget, GdkEventMotion *event, gpointer data) {
46  DrawingArea *da=static_cast<DrawingArea*>(data);
47  int x, y;
48  GdkModifierType state;
49 
50  if (event->is_hint)
51  gdk_window_get_pointer (event->window, &x, &y, &state);
52  else {
53  x = event->x;
54  y = event->y;
55  state = (GdkModifierType)event->state;
56  }
57 
58  if (state & GDK_BUTTON1_MASK && da->getPixmap() != NULL)
59  da->draw_brush (widget, x, y);
60 
61  return TRUE;
62  }
63 
68  void init(void){
69  g_signal_connect (GTK_OBJECT(widget), "motion_notify_event", (GtkSignalFunc) motion_notify_event, this);
70  g_signal_connect (GTK_OBJECT(widget), "button_press_event", (GtkSignalFunc) button_press_event, this);
71 
72  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);
73  }
74 
75 public:
76 
80  DrawingArea(void) : Pixmap() {
81  init();
82  }
83 
87  DrawingArea(int width, int height) : Pixmap(width, height) {
88  init();
89  }
90 
93  virtual ~DrawingArea(void) {
94  }
95 
97  void draw_brush(GtkWidget *widget, gdouble x, gdouble y) {
98  GdkRectangle update_rect;
99 
100  update_rect.x = x - 5;
101  update_rect.y = y - 5;
102  update_rect.width = 10;
103  update_rect.height = 10;
104  gdk_draw_rectangle (pixmap, widget->style->white_gc, TRUE, update_rect.x, update_rect.y, update_rect.width, update_rect.height);
105  gtk_widget_queue_draw_area (widget, update_rect.x, update_rect.y, update_rect.width, update_rect.height);
106  }
107 
108 };
109 
110 #endif // DRAWINGAREA_H_