00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #if !defined (INCLUDED_XORRECTANGLE_H)
00023 #define INCLUDED_XORRECTANGLE_H
00024
00025 #include <gtk/gtkwidget.h>
00026 #include "math/FloatTools.h"
00027
00028 class Rectangle
00029 {
00030 public:
00031 Rectangle () :
00032 x(0), y(0), w(0), h(0)
00033 {
00034 }
00035 Rectangle (float _x, float _y, float _w, float _h) :
00036 x(_x), y(_y), w(_w), h(_h)
00037 {
00038 }
00039 float x;
00040 float y;
00041 float w;
00042 float h;
00043 };
00044
00045 struct Coord2D
00046 {
00047 float x, y;
00048 Coord2D (float _x, float _y) :
00049 x(_x), y(_y)
00050 {
00051 }
00052 };
00053
00054 inline Coord2D coord2d_device2screen (const Coord2D& coord, unsigned int width, unsigned int height)
00055 {
00056 return Coord2D(((coord.x + 1.0f) * 0.5f) * width, ((coord.y + 1.0f) * 0.5f) * height);
00057 }
00058
00059 inline Rectangle rectangle_from_area (const float min[2], const float max[2], unsigned int width, unsigned int height)
00060 {
00061 Coord2D botleft(coord2d_device2screen(Coord2D(min[0], min[1]), width, height));
00062 Coord2D topright(coord2d_device2screen(Coord2D(max[0], max[1]), width, height));
00063 return Rectangle(botleft.x, botleft.y, topright.x - botleft.x, topright.y - botleft.y);
00064 }
00065
00066 class XORRectangle
00067 {
00068 Rectangle m_rectangle;
00069
00070 GtkWidget* m_widget;
00071 GdkGC* m_gc;
00072
00073 bool initialised () const
00074 {
00075 return m_gc != 0;
00076 }
00077 void lazy_init ()
00078 {
00079 if (!initialised()) {
00080 m_gc = gdk_gc_new(m_widget->window);
00081
00082 GdkColor color = { 0, 0xffff, 0xffff, 0xffff, };
00083 GdkColormap* colormap = gdk_window_get_colormap(m_widget->window);
00084 gdk_colormap_alloc_color(colormap, &color, FALSE, TRUE);
00085 gdk_gc_copy(m_gc, m_widget->style->white_gc);
00086 gdk_gc_set_foreground(m_gc, &color);
00087 gdk_gc_set_background(m_gc, &color);
00088
00089 gdk_gc_set_function(m_gc, GDK_INVERT);
00090 }
00091 }
00092 void draw () const
00093 {
00094 const int x = float_to_integer(m_rectangle.x);
00095 const int y = float_to_integer(m_rectangle.y);
00096 const int w = float_to_integer(m_rectangle.w);
00097 const int h = float_to_integer(m_rectangle.h);
00098 gdk_draw_rectangle(m_widget->window, m_gc, FALSE, x, -(h) - (y - m_widget->allocation.height), w, h);
00099 }
00100
00101 public:
00102 XORRectangle (GtkWidget* widget) :
00103 m_widget(widget), m_gc(0)
00104 {
00105 }
00106 ~XORRectangle ()
00107 {
00108 if (initialised()) {
00109 gdk_gc_unref(m_gc);
00110 }
00111 }
00112 void set (Rectangle rectangle)
00113 {
00114 if (GTK_WIDGET_REALIZED(m_widget)) {
00115 lazy_init();
00116 draw();
00117 m_rectangle = rectangle;
00118 draw();
00119 }
00120 }
00121 };
00122
00123 #endif