]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/xorrectangle.cpp
Merge commit 'c3765eb6e7f6879e5a34c48f71e5ee7bc8b8851f' into master-merge
[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 #ifndef WORKAROUND_MACOS_GTK2_DESTROY
20     const int x = float_to_integer(m_rectangle.x);
21     const int y = float_to_integer(m_rectangle.y);
22     const int w = float_to_integer(m_rectangle.w);
23     const int h = float_to_integer(m_rectangle.h);
24     GtkAllocation allocation;
25     gtk_widget_get_allocation(m_widget, &allocation);
26     cairo_rectangle(cr, x, -(h) - (y - allocation.height), w, h);
27     cairo_set_source_rgb(cr, 1, 1, 1);
28     cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
29     cairo_stroke(cr);
30 #endif
31 }
32
33 XORRectangle::XORRectangle(ui::GLArea widget) : m_widget(widget), cr(0)
34 {
35 }
36
37 XORRectangle::~XORRectangle()
38 {
39     if (initialised()) {
40 #ifndef WORKAROUND_MACOS_GTK2_DESTROY
41         cairo_destroy(cr);
42 #endif
43     }
44 }
45
46 void XORRectangle::set(rectangle_t rectangle)
47 {
48     if (gtk_widget_get_realized(m_widget)) {
49         lazy_init();
50         draw();
51         m_rectangle = rectangle;
52         draw();
53     }
54 }