-/*
-Copyright (C) 2001-2006, William Joseph.
-All Rights Reserved.
+#include "xorrectangle.h"
-This file is part of GtkRadiant.
+#include <gtk/gtk.h>
-GtkRadiant is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
+#include "gtkutil/glwidget.h"
+#include "igl.h"
-GtkRadiant is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
+#include <gtk/gtkglwidget.h>
-You should have received a copy of the GNU General Public License
-along with GtkRadiant; if not, write to the Free Software
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-*/
+//#include "stream/stringstream.h"
-#include "xorrectangle.h"
+bool XORRectangle::initialised() const
+{
+ return !!cr;
+}
+
+void XORRectangle::lazy_init()
+{
+ if (!initialised()) {
+ cr = gdk_cairo_create(gtk_widget_get_window(m_widget));
+ }
+}
+
+void XORRectangle::draw() const
+{
+ const int x = float_to_integer(m_rectangle.x);
+ const int y = float_to_integer(m_rectangle.y);
+ const int w = float_to_integer(m_rectangle.w);
+ const int h = float_to_integer(m_rectangle.h);
+ GtkAllocation allocation;
+ gtk_widget_get_allocation(m_widget, &allocation);
+ cairo_rectangle(cr, x, -(h) - (y - allocation.height), w, h);
+ cairo_set_source_rgb(cr, 1, 1, 1);
+ cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
+ cairo_stroke(cr);
+}
+
+XORRectangle::XORRectangle(ui::GLArea widget) : m_widget(widget), cr(0)
+{
+}
+
+XORRectangle::~XORRectangle()
+{
+ if (initialised()) {
+ cairo_destroy(cr);
+ }
+}
+
+void XORRectangle::set(rectangle_t rectangle)
+{
+ if (gtk_widget_get_realized(m_widget)) {
+ if( m_rectangle.w != rectangle.w || m_rectangle.h != rectangle.h ){
+ //if( !(m_rectangle.w == 0 && m_rectangle.h == 0 && rectangle.w == 0 && rectangle.h == 0) ){
+ //globalOutputStream() << "m_x" << m_rectangle.x << " m_y" << m_rectangle.y << " m_w" << m_rectangle.w << " m_h" << m_rectangle.h << "\n";
+ //globalOutputStream() << "__x" << rectangle.x << " __y" << rectangle.y << " __w" << rectangle.w << " __h" << rectangle.h << "\n";
+ if ( glwidget_make_current( m_widget ) != FALSE ) {
+ GlobalOpenGL_debugAssertNoErrors();
+
+ gint width, height;
+ gdk_gl_drawable_get_size( gtk_widget_get_gl_drawable( m_widget ), &width, &height );
+
+ glViewport( 0, 0, width, height );
+ glMatrixMode( GL_PROJECTION );
+ glLoadIdentity();
+ glOrtho( 0, width, 0, height, -100, 100 );
+
+ glMatrixMode( GL_MODELVIEW );
+ glLoadIdentity();
+
+ glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
+ glDisable( GL_DEPTH_TEST );
+
+ glDrawBuffer( GL_FRONT );
+
+ glEnable( GL_BLEND );
+ glBlendFunc( GL_ONE_MINUS_DST_COLOR, GL_ZERO );
+
+ glLineWidth( 2 );
+ glColor3f( 1, 1, 1 );
+ glDisable( GL_TEXTURE_2D );
+ glBegin( GL_LINE_LOOP );
+ glVertex2f( m_rectangle.x, m_rectangle.y + m_rectangle.h );
+ glVertex2f( m_rectangle.x + m_rectangle.w, m_rectangle.y + m_rectangle.h );
+ glVertex2f( m_rectangle.x + m_rectangle.w, m_rectangle.y );
+ glVertex2f( m_rectangle.x, m_rectangle.y );
+ glEnd();
+
+ glBegin( GL_LINE_LOOP );
+ glVertex2f( rectangle.x, rectangle.y + rectangle.h );
+ glVertex2f( rectangle.x + rectangle.w, rectangle.y + rectangle.h );
+ glVertex2f( rectangle.x + rectangle.w, rectangle.y );
+ glVertex2f( rectangle.x, rectangle.y );
+ glEnd();
+ glDrawBuffer( GL_BACK );
+ GlobalOpenGL_debugAssertNoErrors();
+ //glwidget_swap_buffers( m_widget );
+ glwidget_make_current( m_widget );
+ }
+ }
+ m_rectangle = rectangle;
+ }
+}