]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/gtkutil/xorrectangle.cpp
Merge branch 'TimePath/gtk++' into 'master'
[xonotic/netradiant.git] / libs / gtkutil / xorrectangle.cpp
index e4f46c6b8b171a838841c651b4ce216b125e89b9..c638e0e21fcaa61ffd01d77da3f7ef32c5942d98 100644 (file)
@@ -1,22 +1,50 @@
-/*
-   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.
+bool XORRectangle::initialised() const
+{
+    return !!cr;
+}
 
-   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.
+void XORRectangle::lazy_init()
+{
+    if (!initialised()) {
+        cr = gdk_cairo_create(gtk_widget_get_window(m_widget));
+    }
+}
 
-   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
- */
+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);
+}
 
-#include "xorrectangle.h"
+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)) {
+        lazy_init();
+        draw();
+        m_rectangle = rectangle;
+        draw();
+    }
+}