]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/xorrectangle.cpp
[q3map2] Unwind script stack in case of script loading error.
[xonotic/netradiant.git] / libs / gtkutil / xorrectangle.cpp
1 #include "xorrectangle.h"
2
3 #include <gtk/gtk.h>
4
5 bool XORRectangle::initialised() const
6 {
7     return !!cr;
8 }
9
10 void XORRectangle::lazy_init()
11 {
12     if (!initialised()) {
13         cr = gdk_cairo_create(gtk_widget_get_window(m_widget));
14     }
15 }
16
17 void XORRectangle::draw() const
18 {
19     const int x = float_to_integer(m_rectangle.x);
20     const int y = float_to_integer(m_rectangle.y);
21     const int w = float_to_integer(m_rectangle.w);
22     const int h = float_to_integer(m_rectangle.h);
23     GtkAllocation allocation;
24     gtk_widget_get_allocation(m_widget, &allocation);
25     cairo_rectangle(cr, x, -(h) - (y - allocation.height), w, h);
26     cairo_set_source_rgb(cr, 1, 1, 1);
27     cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
28     cairo_stroke(cr);
29 }
30
31 XORRectangle::XORRectangle(ui::GLArea widget) : m_widget(widget), cr(0)
32 {
33 }
34
35 XORRectangle::~XORRectangle()
36 {
37     if (initialised()) {
38         cairo_destroy(cr);
39     }
40 }
41
42 void XORRectangle::set(rectangle_t rectangle)
43 {
44     if (gtk_widget_get_realized(m_widget)) {
45         lazy_init();
46         draw();
47         m_rectangle = rectangle;
48         draw();
49     }
50 }