gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
Surface.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 SURFACE_H_
18 #define SURFACE_H_
19 
20 #include <gtk/gtk.h>
21 
22 #if GTK_MAJOR_VERSION>2 // if using GTK3 or more recent, then don't use GdkPixmaps, use cairo surfaces
23 #undef PIXMAP_ORIG_VER
24 #endif
25 
26 #ifndef PIXMAP_ORIG_VER
27 #include <cairo/cairo.h>
28 #endif
29 
30 #include "Widget.H"
31 
32 class Surface : public Widget {
33 protected:
34 #ifdef PIXMAP_ORIG_VER
35  GdkPixmap *pixmap;
36 #else
37  cairo_surface_t *surface;
38 #endif
39 
46  static gboolean configureEventStatic(GtkWidget *widget, GdkEventConfigure *event, gpointer data) {
47  //cout<<"Surface::configureEventStatic "<<endl;
48  Surface *da=static_cast<Surface*>(data);
49 #if GTK_MAJOR_VERSION==2
50  int width=widget->allocation.width, height=widget->allocation.height;
51 #else
52  GtkAllocation widgetAlloc; // get the width and height
53  gtk_widget_get_allocation(widget, &widgetAlloc);
54  int width=widgetAlloc.width, height=widgetAlloc.height;
55 #endif
56  //cout<<"Surface::configureSurface width, height "<<width<<','<<height<<endl;
57 
58 #ifdef PIXMAP_ORIG_VER
59  // if using a GdkPixmap, then simply paint it.
60  da->setPixmap(gdk_pixmap_new(widget->window, widget->allocation.width, widget->allocation.height, -1));
61  gdk_draw_rectangle(da->getPixmap(), widget->style->black_gc, TRUE, 0, 0, width, height);
62 #else
63  // set the backing surface
64  da->setSurface(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height));
65  da->clear();
66  // paint the widget
67  cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget)); // create the cairo context and create the backing surface
68  cairo_set_source_surface(cr, da->getSurface(), 0., 0.);
69  cairo_paint(cr);
70  cairo_destroy(cr);
71 #endif
72  return da->configureEvent(event);
73  }
74 
79  virtual gboolean configureEvent(GdkEventConfigure *event) {
80  return false;
81  }
82 
89  static gboolean exposeEventStatic(GtkWidget *widget, GdkEventExpose *event, gpointer data) {
90  //cout<<"Surface::exposeSurfaceStatic"<<endl;
91  Surface *da=static_cast<Surface*>(data);
92 #ifdef PIXMAP_ORIG_VER
93  gdk_draw_drawable(gtk_widget_get_window(widget), widget->style->fg_gc[gtk_widget_get_state (widget)], da->getPixmap(), event->area.x, event->area.y, event->area.x, event->area.y,
94  event->area.width, event->area.height);
95 #else
96  cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget)); // create the cairo context and paint the backing surface
97  cairo_set_source_surface (cr, da->getSurface(), 0, 0);
98  cairo_paint(cr);
99  cairo_destroy(cr);
100 #endif
101  return da->exposeEvent();
102  }
103 
107  virtual bool exposeEvent(){
108  return FALSE;
109  }
110 
111 #ifdef PIXMAP_ORIG_VER
112 
113  void destroyPixmap(void){
114  if (pixmap){
115  g_object_unref(pixmap);
116  pixmap=NULL;
117  }
118  }
119 #else
120 
121  void destroySurface(void){
122  //cout<<"Surface::destroySurface"<<endl;
123  if (surface){
124  cairo_surface_destroy(surface);
125  surface=NULL;
126  }
127  }
128 #endif
129 
134  void init(int width, int height){
135 #ifdef PIXMAP_ORIG_VER
136  pixmap=NULL;
137 #else
138  surface=NULL;
139 #endif
140  widget=gtk_drawing_area_new ();
141  show();
142 
143  g_signal_connect(G_OBJECT(widget), "expose_event", (GCallback) exposeEventStatic, this);
144  g_signal_connect(G_OBJECT(widget), "configure_event", (GCallback) configureEventStatic, this);
145 
146  setSize(width, height);
147  }
148 
153  void connectDamagedCallback(GCallback callback, void *data){
154  g_signal_connect(G_OBJECT(widget), "damage-event", callback, this);
155  }
156 
157 public:
158 
162  init(200, 200);
163  }
168  Surface(int width, int height) {
169  init(width, height);
170  }
171 
174  virtual ~Surface(void) {
175 #ifdef PIXMAP_ORIG_VER
176  destroyPixmap();
177 #else
178  destroySurface();
179 #endif
180  }
181 
185  GtkWidget *getWidget(void){return widget;}
186 
187 #ifdef PIXMAP_ORIG_VER
188 
191  GdkPixmap *getPixmap(void){return pixmap;}
192 
196  void setPixmap(GdkPixmap *pixmap_){
197  destroyPixmap();
198  pixmap=pixmap_;
199  }
200 #else
201 
204  cairo_surface_t *getSurface(void){return surface;}
205 
209  void setSurface(cairo_surface_t *surface_){
210  //cout<<"Surface::setSurface"<<endl;
211  destroySurface();
212  surface=surface_;
213  }
214 
217  void clear(){
218  cairo_t *cr = cairo_create(getSurface());
219  cairo_set_source_rgb(cr, 0., 0., 0.); // set the background to black
220  cairo_paint(cr);
221  cairo_destroy(cr);
222  }
223 
224 #endif
225 };
226 #endif //SURFACE_H_
virtual gboolean configureEvent(GdkEventConfigure *event)
Definition: Surface.H:79
virtual ~Surface(void)
Definition: Surface.H:174
GtkWidget * getWidget(void)
Definition: Surface.H:185
cairo_surface_t * surface
The backing surface for the drawing area.
Definition: Surface.H:37
Surface()
Definition: Surface.H:161
static gboolean exposeEventStatic(GtkWidget *widget, GdkEventExpose *event, gpointer data)
Definition: Surface.H:89
void connectDamagedCallback(GCallback callback, void *data)
Definition: Surface.H:153
static gboolean configureEventStatic(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
Definition: Surface.H:46
void destroySurface(void)
Definition: Surface.H:121
GtkWidget * show(void)
Definition: Widget.H:65
cairo_surface_t * getSurface(void)
Definition: Surface.H:204
virtual void setSize(int width, int height)
Definition: Widget.H:132
GtkWidget * widget
The container based widget.
Definition: Widget.H:33
Definition: Widget.H:31
void setSurface(cairo_surface_t *surface_)
Definition: Surface.H:209
void init(int width, int height)
Definition: Surface.H:134
virtual bool exposeEvent()
Definition: Surface.H:107
Surface(int width, int height)
Definition: Surface.H:168
void clear()
Definition: Surface.H:217
gtkIOStream: /tmp/gtkiostream/include/Surface.H Source File
GTK+ IOStream  Beta