]> icculus.org git repositories - divverent/netradiant.git/blob - libs/gtkutil/xorrectangle.h
correctly support [ and ] keys
[divverent/netradiant.git] / libs / gtkutil / xorrectangle.h
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #if !defined (INCLUDED_XORRECTANGLE_H)
23 #define INCLUDED_XORRECTANGLE_H
24
25 #include <gtk/gtkwidget.h>
26 #include "math/vector.h"
27
28 class rectangle_t
29 {
30 public:
31   rectangle_t()
32     : x(0), y(0), w(0), h(0)
33   {}
34   rectangle_t(float _x, float _y, float _w, float _h)
35     : x(_x), y(_y), w(_w), h(_h)
36   {}
37   float x;
38   float y;
39   float w;
40   float h;
41 };
42
43 struct Coord2D
44 {
45   float x, y;
46   Coord2D(float _x, float _y)
47     : x(_x), y(_y)
48   {
49   }
50 };
51
52 inline Coord2D coord2d_device2screen(const Coord2D& coord, unsigned int width, unsigned int height)
53 {
54   return Coord2D(((coord.x + 1.0f) * 0.5f) * width, ((coord.y + 1.0f) * 0.5f) * height);
55 }
56
57 inline rectangle_t rectangle_from_area(const float min[2], const float max[2], unsigned int width, unsigned int height)
58 {
59   Coord2D botleft(coord2d_device2screen(Coord2D(min[0], min[1]), width, height));
60   Coord2D topright(coord2d_device2screen(Coord2D(max[0], max[1]), width, height));
61   return rectangle_t(botleft.x, botleft.y, topright.x - botleft.x, topright.y - botleft.y);
62 }
63
64 class XORRectangle
65 {
66
67   rectangle_t m_rectangle;
68
69   GtkWidget* m_widget;
70   GdkGC* m_gc;
71
72   bool initialised() const
73   {
74     return m_gc != 0;
75   }
76   void lazy_init()
77   {
78     if(!initialised())
79     {
80       m_gc = gdk_gc_new(m_widget->window);
81
82       GdkColor color = { 0, 0xffff, 0xffff, 0xffff, };
83       GdkColormap* colormap = gdk_window_get_colormap(m_widget->window);
84       gdk_colormap_alloc_color (colormap, &color, FALSE, TRUE);
85       gdk_gc_copy(m_gc, m_widget->style->white_gc);
86       gdk_gc_set_foreground(m_gc, &color);
87       gdk_gc_set_background(m_gc, &color);
88
89       gdk_gc_set_function(m_gc, GDK_INVERT);
90     }
91   }
92   void draw() const
93   {
94     const int x = float_to_integer(m_rectangle.x);
95     const int y = float_to_integer(m_rectangle.y);
96     const int w = float_to_integer(m_rectangle.w);
97     const int h = float_to_integer(m_rectangle.h);
98     gdk_draw_rectangle(m_widget->window, m_gc, FALSE, x, -(h) - (y - m_widget->allocation.height), w, h);
99   }
100
101 public:
102   XORRectangle(GtkWidget* widget) : m_widget(widget), m_gc(0)
103   {
104   }
105   ~XORRectangle()
106   {
107     if(initialised())
108     {
109       gdk_gc_unref(m_gc);
110     }
111   }
112   void set(rectangle_t rectangle)
113   {
114     if(GTK_WIDGET_REALIZED(m_widget))
115     {
116       lazy_init();
117       draw();
118       m_rectangle = rectangle;
119       draw();
120     }
121   }
122 };
123
124
125 #endif