]> icculus.org git repositories - dana/openbox.git/blob - src/buttonwidget.cc
new render system in effect. now ot make it look right
[dana/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   otk::Widget::update();
84 }
85
86 void ButtonWidget::renderForeground()
87 {
88   otk::PixmapMask *pm;
89   int width;
90   bool draw = _dirty;
91
92   printf("ButtonWidget::renderForeground()\n");
93   otk::Widget::renderForeground();
94
95   if (draw) {
96     switch (type()) {
97     case Type_StickyButton:
98       pm = _style->stickyMask();
99       break;
100     case Type_CloseButton:
101       pm = _style->closeMask();
102       break;
103     case Type_MaximizeButton:
104       pm = _style->maximizeMask();
105       break;
106     case Type_IconifyButton:
107       pm = _style->iconifyMask();
108       break;
109     case Type_LeftGrip:
110     case Type_RightGrip:
111       return; // no drawing
112     default:
113       assert(false); // there's no other button widgets!
114     }
115
116     if (pm->mask == None) return; // no mask for the button, leave it empty
117
118     width = _rect.width();
119
120     otk::RenderColor *color = (_focused ? _style->buttonFocusColor() :
121                                _style->buttonUnfocusColor());
122
123     // set the clip region
124     XSetClipMask(**otk::display, color->gc(), pm->mask);
125     XSetClipOrigin(**otk::display, color->gc(),
126                    (width - pm->w)/2, (width - pm->h)/2);
127
128     // fill in the clipped region
129     XFillRectangle(**otk::display, _window, color->gc(),
130                    (width - pm->w)/2, (width - pm->h)/2,
131                    (width + pm->w)/2, (width + pm->h)/2);
132
133     // unset the clip region
134     XSetClipMask(**otk::display, color->gc(), None);
135     XSetClipOrigin(**otk::display, color->gc(), 0, 0);
136   }
137 }
138
139
140 void ButtonWidget::adjust()
141 {
142   // nothing to adjust. no children.
143 }
144
145
146 void ButtonWidget::focus()
147 {
148   otk::Widget::focus();
149   setTextures();
150 }
151
152
153 void ButtonWidget::unfocus()
154 {
155   otk::Widget::unfocus();
156   setTextures();
157 }
158
159
160 void ButtonWidget::buttonPressHandler(const XButtonEvent &e)
161 {
162   otk::Widget::buttonPressHandler(e);
163   if (_button) return;
164   _button = e.button;
165   _pressed = true;
166   setTextures();
167   update();
168 }
169
170
171 void ButtonWidget::buttonReleaseHandler(const XButtonEvent &e)
172 {
173   otk::Widget::buttonPressHandler(e);
174   if (e.button != _button) return;
175   _button = 0;
176   _pressed = false;
177   setTextures();
178   update();
179 }
180
181 }