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