]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/xorrectangle.cpp
Merge branch 'NateEag-master-patch-12920' into 'master'
[xonotic/netradiant.git] / libs / gtkutil / xorrectangle.cpp
1 #include "xorrectangle.h"
2
3 #include <gtk/gtk.h>
4
5 #define GARUX_DISABLE_BAD_XORRECTANGLE
6
7 #if !defined(GARUX_DISABLE_BAD_XORRECTANGLE) && GTK_TARGET == 2
8 #include "gtkutil/glwidget.h"
9 #include "igl.h"
10
11 #include <gtk/gtkglwidget.h>
12 #endif // !GARUX_DISABLE_BAD_XORRECTANGLE && GTK_TARGET == 2
13
14 //#include "stream/stringstream.h"
15
16 bool XORRectangle::initialised() const
17 {
18     return !!cr;
19 }
20
21 void XORRectangle::lazy_init()
22 {
23     if (!initialised()) {
24         cr = gdk_cairo_create(gtk_widget_get_window(m_widget));
25     }
26 }
27
28 void XORRectangle::draw() const
29 {
30 #ifndef WORKAROUND_MACOS_GTK2_DESTROY
31     const int x = float_to_integer(m_rectangle.x);
32     const int y = float_to_integer(m_rectangle.y);
33     const int w = float_to_integer(m_rectangle.w);
34     const int h = float_to_integer(m_rectangle.h);
35     GtkAllocation allocation;
36     gtk_widget_get_allocation(m_widget, &allocation);
37     cairo_rectangle(cr, x, -(h) - (y - allocation.height), w, h);
38     cairo_set_source_rgb(cr, 1, 1, 1);
39     cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
40     cairo_stroke(cr);
41 #endif
42 }
43
44 XORRectangle::XORRectangle(ui::GLArea widget) : m_widget(widget), cr(0)
45 {
46 }
47
48 XORRectangle::~XORRectangle()
49 {
50     if (initialised()) {
51 #ifndef WORKAROUND_MACOS_GTK2_DESTROY
52         cairo_destroy(cr);
53 #endif
54     }
55 }
56
57 void XORRectangle::set(rectangle_t rectangle)
58 {
59     if (gtk_widget_get_realized(m_widget)) {
60 #if !defined(GARUX_DISABLE_BAD_XORRECTANGLE) && GTK_TARGET == 2
61                 if( m_rectangle.w != rectangle.w || m_rectangle.h != rectangle.h ){
62                 //if( !(m_rectangle.w == 0 && m_rectangle.h == 0 && rectangle.w == 0 && rectangle.h == 0) ){
63                 //globalOutputStream() << "m_x" << m_rectangle.x << " m_y" << m_rectangle.y << " m_w" << m_rectangle.w << " m_h" << m_rectangle.h << "\n";
64                 //globalOutputStream() << "__x" << rectangle.x << " __y" << rectangle.y << " __w" << rectangle.w << " __h" << rectangle.h << "\n";
65                         if ( glwidget_make_current( m_widget ) != FALSE ) {
66                                 GlobalOpenGL_debugAssertNoErrors();
67
68                                 gint width, height;
69                                 gdk_gl_drawable_get_size( gtk_widget_get_gl_drawable( m_widget ), &width, &height );
70
71                                 glViewport( 0, 0, width, height );
72                                 glMatrixMode( GL_PROJECTION );
73                                 glLoadIdentity();
74                                 glOrtho( 0, width, 0, height, -100, 100 );
75
76                                 glMatrixMode( GL_MODELVIEW );
77                                 glLoadIdentity();
78
79                                 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
80                                 glDisable( GL_DEPTH_TEST );
81
82                                 glDrawBuffer( GL_FRONT );
83
84                                 glEnable( GL_BLEND );
85                                 glBlendFunc( GL_ONE_MINUS_DST_COLOR, GL_ZERO );
86
87                                 glLineWidth( 2 );
88                                 glColor3f( 1, 1, 1 );
89                                 glDisable( GL_TEXTURE_2D );
90                                 glBegin( GL_LINE_LOOP );
91                                 glVertex2f( m_rectangle.x, m_rectangle.y + m_rectangle.h );
92                                 glVertex2f( m_rectangle.x + m_rectangle.w, m_rectangle.y + m_rectangle.h );
93                                 glVertex2f( m_rectangle.x + m_rectangle.w, m_rectangle.y );
94                                 glVertex2f( m_rectangle.x, m_rectangle.y );
95                                 glEnd();
96
97                                 glBegin( GL_LINE_LOOP );
98                                 glVertex2f( rectangle.x, rectangle.y + rectangle.h );
99                                 glVertex2f( rectangle.x + rectangle.w, rectangle.y + rectangle.h );
100                                 glVertex2f( rectangle.x + rectangle.w, rectangle.y );
101                                 glVertex2f( rectangle.x, rectangle.y );
102                                 glEnd();
103
104                                 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
105                                 glDrawBuffer( GL_BACK );
106                                 GlobalOpenGL_debugAssertNoErrors();
107                                 //glwidget_swap_buffers( m_widget );
108                                 glwidget_make_current( m_widget );
109                         }
110                 }
111                 m_rectangle = rectangle;
112 #else // GARUX_DISABLE_BAD_XORRECTANGLE || GTK_TARGET != 2
113                 lazy_init();
114                 draw();
115                 m_rectangle = rectangle;
116                 draw();
117 #endif // !GARUX_DISABLE_BAD_XORRECTANGLE && GTK_TARGET == 2
118     }
119 }