Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

pandatool/src/gtk-stats/scribble.cc

Go to the documentation of this file.
00001 
00002 
00003 
00004 /* GTK - The GIMP Toolkit
00005  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Library General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Library General Public
00018  * License along with this library; if not, write to the
00019  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020  * Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /* Modifed for gtk-- by sac@transmeta.com */
00025 /* Modified (slightly) for gdk-- by freyd@uni-muenster.de */
00026 #include <gtk--/main.h>
00027 #include <gtk--/style.h>
00028 #include <gtk--/window.h>
00029 #include <gtk--/button.h>
00030 #include <gtk--/box.h>
00031 #include <gtk--/drawingarea.h>
00032 #include <iostream>
00033 
00034 
00035 class ScribbleDrawingArea  : public Gtk::DrawingArea
00036 {
00037   /* Backing pixmap for drawing area */
00038 
00039   Gdk_Pixmap pixmap;
00040   Gdk_GC gc;
00041   Gdk_Window win;
00042   Gdk_Visual visual;
00043 
00044   virtual gint configure_event_impl (GdkEventConfigure *event);
00045   virtual gint expose_event_impl (GdkEventExpose *event);
00046   virtual gint button_press_event_impl (GdkEventButton *event);
00047   virtual gint motion_notify_event_impl (GdkEventMotion *event);
00048   void draw_brush (gdouble x, gdouble y);
00049 
00050 public:
00051   ScribbleDrawingArea ();
00052 
00053 };
00054 
00055 ScribbleDrawingArea::ScribbleDrawingArea()
00056     : Gtk::DrawingArea(), pixmap (0)
00057   {
00058     set_events (GDK_EXPOSURE_MASK
00059                 | GDK_LEAVE_NOTIFY_MASK
00060                 | GDK_BUTTON_PRESS_MASK
00061                 | GDK_POINTER_MOTION_MASK
00062                 | GDK_POINTER_MOTION_HINT_MASK);
00063   }
00064 
00065 
00066 /* Create a new backing pixmap of the appropriate size */
00067 int ScribbleDrawingArea::configure_event_impl (GdkEventConfigure * /* event */)
00068   {
00069     win = get_window();
00070     visual = win.get_visual();
00071 
00072     if (pixmap)
00073       pixmap.release();
00074       gc = get_style()->gtkobj()->white_gc;
00075       // Gtk::Style has no access to its data members, so use gtk objekt.
00076       // Some access functions would be nice like GtkStyle::get_white_gc() etc.
00077     pixmap.create(get_window(),  width(), height());
00078 
00079     pixmap.draw_rectangle (gc,
00080                         TRUE,
00081                         0, 0,
00082                         width(),
00083                         height());
00084 
00085     return TRUE;
00086   }
00087 
00088 /* Redraw the screen from the backing pixmap */
00089 int ScribbleDrawingArea::expose_event_impl (GdkEventExpose *event)
00090   {
00091     
00092     gc = get_style()->gtkobj()->fg_gc[GTK_WIDGET_STATE (GTK_WIDGET(gtkobj()))];
00093     // Same like above, + Gtk::Widget has set_state function but no get_state 
00094     // function. 
00095     win.draw_pixmap(gc ,
00096                     pixmap,
00097                     event->area.x, event->area.y,
00098                     event->area.x, event->area.y,
00099                     event->area.width, event->area.height);
00100 
00101     return FALSE;
00102   }
00103 
00104 /* Draw a rectangle on the screen */
00105 void ScribbleDrawingArea::draw_brush (gdouble x, gdouble y)
00106   {
00107     GdkRectangle update_rect;
00108     update_rect.x = (int)x - 5;
00109     update_rect.y = (int)y - 5;
00110     update_rect.width = 10;
00111     update_rect.height = 10;
00112     gc = get_style()->gtkobj()->black_gc;
00113     pixmap.draw_rectangle(
00114                         gc,
00115                         TRUE,
00116                         update_rect.x, update_rect.y,
00117                         update_rect.width, update_rect.height);
00118     draw(&update_rect);
00119     //draw (&update_rect);
00120   }
00121 
00122 gint ScribbleDrawingArea::button_press_event_impl (GdkEventButton *event)
00123   {
00124     if (event->button == 1 && pixmap)
00125       draw_brush (event->x, event->y);
00126 
00127     return TRUE;
00128   }
00129 
00130 gint ScribbleDrawingArea::motion_notify_event_impl (GdkEventMotion *event)
00131   {
00132     int x, y;
00133     GdkModifierType state;
00134     if (event->is_hint)
00135       gdk_window_get_pointer (event->window, &x, &y, &state);
00136     else
00137       {
00138         x = (int)event->x;
00139         y = (int)event->y;
00140         state = (GdkModifierType) event->state;
00141       }
00142     
00143     if (state & GDK_BUTTON1_MASK && pixmap)
00144       draw_brush (x, y);
00145   
00146     return TRUE;
00147   }
00148 
00149 
00150 class ScribbleWindow : public Gtk::Window
00151 {
00152 
00153   Gtk::VBox vbox;
00154   ScribbleDrawingArea drawing_area;
00155   Gtk::Button button;
00156   void quit ();
00157 public:  
00158   ScribbleWindow ();
00159 }; 
00160 
00161 void ScribbleWindow::quit ()
00162   {
00163     Gtk::Main::quit();
00164   }
00165 
00166 ScribbleWindow::ScribbleWindow ()
00167     :  Gtk::Window(GTK_WINDOW_TOPLEVEL),
00168        vbox (FALSE, 0),
00169        button ("quit")
00170   {
00171     add (vbox);
00172 
00173     /* Create the drawing area */
00174     drawing_area.size (400, 400);
00175     vbox.pack_start (drawing_area, TRUE, TRUE, 0);
00176 
00177 
00178     /* Add the button */
00179     vbox.pack_start (button, FALSE, FALSE, 0);
00180 
00181     button.clicked.connect(slot(*this, &ScribbleWindow::quit));
00182     destroy.connect(slot(*this, &ScribbleWindow::quit));
00183 
00184     drawing_area.show();
00185     button.show();
00186     vbox.show();
00187   }
00188 
00189 int
00190 main (int argc, char *argv[])
00191 {
00192   ScribbleWindow *window;
00193   Gtk::Main myapp(argc, argv);
00194 
00195   window = new ScribbleWindow;
00196   window->show();
00197 
00198   myapp.run();
00199 
00200   return 0;
00201 }

Generated on Fri May 2 03:20:05 2003 for Panda-Tool by doxygen1.3