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
gtkInterface.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 GTKINTERFACE_H_
18 #define GTKINTERFACE_H_
19 
175 #include <gtk/gtk.h>
176 #include "Box.H"
177 
178 #define LOCKSEMAPHORE gdk_threads_enter()
179 #define UNLOCKSEMAPHORE gdk_threads_leave()
180 //#define DEBUG_GTKINTERFACE
182  bool fillBorderImplementation(GtkWidget *widget, int borderFillVar=-1){
183  if (borderFillVar==-1) // if no border fill specified, use the class default
184  borderFillVar=borderFill;
185  GtkStyle *wStyle=gtk_widget_get_style(widget); // get the widget's bg colour
186  cairo_t *cr=gdk_cairo_create(win->window);
187  gdk_cairo_set_source_color(cr, &wStyle->bg[GTK_STATE_NORMAL]);
188  int x, y, width, height;
189  if (borderFillVar&BORDER_LEFT){ // fill from the left side of the widget to the edge of the window
190  x=win->allocation.x; y=widget->allocation.y; width=widget->allocation.x-x; height=widget->allocation.height;
191  cairo_rectangle(cr, x, y, width, height);
192  }
193  if (borderFillVar&BORDER_RIGHT){ // fill from the left side of the widget to the edge of the window
194  x=widget->allocation.x+widget->allocation.width; y=widget->allocation.y; width=(win->allocation.x+win->allocation.width)-x; height=widget->allocation.height;
195  cairo_rectangle(cr, x, y, width, height);
196  }
197  if (((borderFillVar&BORDER_LEFT)!=0)&((borderFillVar&BORDER_TOP)!=0)){ // fill the top left corner if both left and top are filled
198  x=win->allocation.x; y=win->allocation.y; width=widget->allocation.x-x; height=widget->allocation.y-win->allocation.y;
199  cairo_rectangle(cr, x, y, width, height);
200  }
201  if (((borderFillVar&BORDER_RIGHT)!=0)&((borderFillVar&BORDER_TOP)!=0)){ // fill the top right corner if both right and top are filled
202  x=widget->allocation.x+widget->allocation.width; y=win->allocation.y; width=(win->allocation.x+win->allocation.width)-x; height=widget->allocation.y-y;
203  cairo_rectangle(cr, x, y, width, height);
204  }
205  if (borderFillVar&BORDER_TOP){ // fill from the left side of the widget to the edge of the window
206  x=widget->allocation.x; y=win->allocation.y; width=widget->allocation.width; height=widget->allocation.y-y;
207  cairo_rectangle(cr, x, y, width, height);
208  }
209  if (borderFillVar&BORDER_BOTTOM){ // fill from the left side of the widget to the edge of the window
210  x=widget->allocation.x; y=widget->allocation.y+widget->allocation.height; width=widget->allocation.width; height=(win->allocation.y+win->allocation.height)-y;
211  cairo_rectangle(cr, x, y, width, height);
212  }
213  if (((borderFillVar&BORDER_LEFT)!=0)&((borderFillVar&BORDER_BOTTOM)!=0)){ // fill the bottom left corner if both left and bottom are filled
214  x=win->allocation.x; y=widget->allocation.y+widget->allocation.height; width=widget->allocation.x-x; height=(win->allocation.y+win->allocation.height)-y;
215  cairo_rectangle(cr, x, y, width, height);
216  }
217  if (((borderFillVar&BORDER_RIGHT)!=0)&((borderFillVar&BORDER_BOTTOM)!=0)){ // fill the bottom right corner if both right and bottom are filled
218  x=widget->allocation.x+widget->allocation.width; y=widget->allocation.y+widget->allocation.height; width=(win->allocation.x+win->allocation.width)-x; height=(win->allocation.y+win->allocation.height)-y;
219  cairo_rectangle(cr, x, y, width, height);
220  }
221  cairo_fill(cr);
222  cairo_destroy(cr);
223  return false;
224  }
225 public:
228 
229  GtkWidget *win;
230 
234  GtkWidget * getWidget(void){return win;}
235 
239 #ifdef DEBUG_GTKINTERFACE
240  cout<<"GtkInterface: init "<<this<<endl;
241 #endif
242  init();
243  }
244 
250  GtkInterface(int width, int height, bool resiseable=true){
251 #ifdef DEBUG_GTKINTERFACE
252  cout<<"GtkInterface: init "<<this<<endl;
253 #endif
254  init(width, height, resiseable);
255  }
256 
262  void init(int width=0, int height=0, bool resiseable=true){
263  win = gtk_window_new( GTK_WINDOW_TOPLEVEL );
264  if (width & height) // only resize if both are non-zero
265  gtk_widget_set_size_request(win, width, height);
266  gtk_signal_connect( GTK_OBJECT (win), "delete_event", GTK_SIGNAL_FUNC (quit), NULL );
267 // gtk_container_set_border_width( GTK_CONTAINER (win), 10 );
268  gtk_widget_show( win );
269  }
270 
274 #ifdef DEBUG_GTKINTERFACE
275  cout<<"GtkInterface::~GtkInterface "<<endl;
276 #endif
277  }
278 
279  static gint quit(GtkWidget *widget, GdkEvent *event, gpointer data){
280  gtk_main_quit();
281  return 0;
282  }
283 
285  return operator<<((Container&)b);
286  }
288  return operator<<((Container*)(b));
289  }
291  return operator<<(static_cast<Container&>(b));
292  }
294  return operator<<(static_cast<Container*>(b));
295  }
296  GtkInterface& operator <<(GtkWidget * w) {
297  gtk_container_add (GTK_CONTAINER (win), w);
298  return *this;
299  }
300  GtkInterface& operator >>(GtkWidget * w) {
301  gtk_container_remove (GTK_CONTAINER (win), w);
302  return *this;
303  }
305  gtk_container_add (GTK_CONTAINER (win), w->getWidget());
306  return *this;
307  }
309  gtk_container_remove (GTK_CONTAINER (win), w->getWidget());
310  return *this;
311  }
313  gtk_container_add (GTK_CONTAINER (win), w.getWidget());
314  return *this;
315  }
317  gtk_container_remove (GTK_CONTAINER (win), w.getWidget());
318  return *this;
319  }
320 
325  void setBackgroundColour(GdkColor *colour, GtkStateType state=GTK_STATE_NORMAL){
326  gtk_widget_modify_bg(win, state, colour);
327  }
328 
333  void setBackgroundColour(const char* colourName, GtkStateType state=GTK_STATE_NORMAL){
334  GdkColor colour; gdk_color_parse (colourName, &colour);
335  setBackgroundColour(&colour, state);
336  }
337 
352  static bool fillBorder(GtkWidget *widget, GdkEventExpose *event, gpointer data){
353  GtkInterface *gtki=static_cast<GtkInterface*>(data);
354  return gtki->fillBorderImplementation(widget);
355  }
356 
363  static bool fillBannerTop(GtkWidget *widget, GdkEventExpose *event, gpointer data){
364  GtkInterface *gtki=static_cast<GtkInterface*>(data);
366  }
367 
374  static bool fillBannerMiddle(GtkWidget *widget, GdkEventExpose *event, gpointer data){
375  GtkInterface *gtki=static_cast<GtkInterface*>(data);
376  return gtki->fillBorderImplementation(widget, BORDER_LEFT|BORDER_RIGHT);
377  }
378 
385  static bool fillBannerBottom(GtkWidget *widget, GdkEventExpose *event, gpointer data){
386  GtkInterface *gtki=static_cast<GtkInterface*>(data);
388  }
389 };
390 #endif