]> icculus.org git repositories - mikachu/openbox.git/blob - src/buttonwidget.cc
compiles with the new render subsystem...
[mikachu/openbox.git] / src / buttonwidget.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif
6
7 #include "buttonwidget.hh"
8 #include "otk/gccache.hh" // otk::BPen
9
10 namespace ob {
11
12 ButtonWidget::ButtonWidget(otk::Widget *parent,
13                            WidgetBase::WidgetType type)
14   : otk::Widget(parent),
15     WidgetBase(type),
16     _pressed(false),
17     _button(0)
18 {
19 }
20
21
22 ButtonWidget::~ButtonWidget()
23 {
24 }
25
26
27 void ButtonWidget::setTextures()
28 {
29   switch (type()) {
30   case Type_LeftGrip:
31   case Type_RightGrip:
32     if (_focused)
33       setTexture(_style->gripFocusBackground());
34     else
35       setTexture(_style->gripUnfocusBackground());
36     break;
37   case Type_StickyButton:
38   case Type_CloseButton:
39   case Type_MaximizeButton:
40   case Type_IconifyButton:
41     if (_pressed) {
42       if (_focused)
43         setTexture(_style->buttonPressFocusBackground());
44       else
45         setTexture(_style->buttonPressUnfocusBackground());
46     } else {
47       if (_focused)
48         setTexture(_style->buttonUnpressFocusBackground());
49       else
50         setTexture(_style->buttonUnpressUnfocusBackground());
51     }
52     break;
53   default:
54     assert(false); // there's no other button widgets!
55   }
56 }
57
58
59 void ButtonWidget::setStyle(otk::RenderStyle *style)
60 {
61   otk::Widget::setStyle(style);
62   setTextures();
63
64   switch (type()) {
65   case Type_LeftGrip:
66   case Type_RightGrip:
67     setBorderColor(_style->frameBorderColor());
68     break;
69   case Type_StickyButton:
70   case Type_CloseButton:
71   case Type_MaximizeButton:
72   case Type_IconifyButton:
73     break;
74   default:
75     assert(false); // there's no other button widgets!
76   }
77 }
78
79
80 void ButtonWidget::update()
81 {
82   printf("ButtonWidget::update()\n");
83 }
84
85 void ButtonWidget::renderForeground()
86 {
87   otk::PixmapMask *pm;
88   int width;
89   bool draw = _dirty;
90
91   otk::Widget::renderForeground();
92
93   if (draw) {
94     switch (type()) {
95     case Type_StickyButton:
96       pm = _style->stickyMask();
97       break;
98     case Type_CloseButton:
99       pm = _style->closeMask();
100       break;
101     case Type_MaximizeButton:
102       pm = _style->maximizeMask();
103       break;
104     case Type_IconifyButton:
105       pm = _style->iconifyMask();
106       break;
107     case Type_LeftGrip:
108     case Type_RightGrip:
109       return; // no drawing
110     default:
111       assert(false); // there's no other button widgets!
112     }
113
114     if (pm->mask == None) return; // no mask for the button, leave it empty
115
116     width = _rect.width();
117
118     otk::RenderColor *color = (_focused ? _style->buttonFocusColor() :
119                                _style->buttonUnfocusColor());
120
121     // set the clip region
122     XSetClipMask(**otk::display, color->gc(), pm->mask);
123     XSetClipOrigin(**otk::display, color->gc(),
124                    (width - pm->w)/2, (width - pm->h)/2);
125
126     // fill in the clipped region
127     XFillRectangle(**otk::display, _window, color->gc(),
128                    (width - pm->w)/2, (width - pm->h)/2,
129                    (width + pm->w)/2, (width + pm->h)/2);
130
131     // unset the clip region
132     XSetClipMask(**otk::display, color->gc(), None);
133     XSetClipOrigin(**otk::display, color->gc(), 0, 0);
134   }
135 }
136
137
138 void ButtonWidget::adjust()
139 {
140   // nothing to adjust. no children.
141 }
142
143
144 void ButtonWidget::focus()
145 {
146   otk::Widget::focus();
147   setTextures();
148 }
149
150
151 void ButtonWidget::unfocus()
152 {
153   otk::Widget::unfocus();
154   setTextures();
155 }
156
157
158 void ButtonWidget::buttonPressHandler(const XButtonEvent &e)
159 {
160   otk::Widget::buttonPressHandler(e);
161   if (_button) return;
162   _button = e.button;
163   _pressed = true;
164   setTextures();
165   update();
166 }
167
168
169 void ButtonWidget::buttonReleaseHandler(const XButtonEvent &e)
170 {
171   otk::Widget::buttonPressHandler(e);
172   if (e.button != _button) return;
173   _button = 0;
174   _pressed = false;
175   setTextures();
176   update();
177 }
178
179 }