resizes
[mikachu/openbox.git] / otk / button.cc
1 #include "button.hh"
2
3 namespace otk {
4
5 OtkButton::OtkButton(OtkWidget *parent)
6   : OtkFocusWidget(parent), _text(""), _pressed(false), _dirty(false),
7     _pressed_focus_tx(0), _pressed_unfocus_tx(0), _unpr_focus_tx(0),
8     _unpr_unfocus_tx(0)
9 {
10   setTexture(getStyle()->getButtonFocus());
11   setUnfocusTexture(getStyle()->getButtonUnfocus());
12   _pressed_focus_tx = getStyle()->getButtonPressedFocus();
13   _pressed_unfocus_tx = getStyle()->getButtonPressedUnfocus();
14 }
15
16 OtkButton::~OtkButton()
17 {
18   if (_pressed_focus_tx) delete _pressed_focus_tx;
19   if (_pressed_unfocus_tx) delete _pressed_unfocus_tx;
20 }
21
22 void OtkButton::press(void)
23 {
24   if (_pressed_focus_tx)
25     OtkFocusWidget::setTexture(_pressed_focus_tx);
26   if (_pressed_unfocus_tx)
27     OtkFocusWidget::setUnfocusTexture(_pressed_unfocus_tx);
28   _pressed = true;
29 }
30
31 void OtkButton::release(void)
32 {
33   OtkFocusWidget::setTexture(_unpr_focus_tx);
34   OtkFocusWidget::setUnfocusTexture(_unpr_unfocus_tx);
35   _pressed = false;
36 }
37
38 void OtkButton::setTexture(BTexture *texture)
39 {
40   OtkFocusWidget::setTexture(texture);
41   _unpr_focus_tx = texture;
42 }
43
44 void OtkButton::setUnfocusTexture(BTexture *texture)
45 {
46   OtkFocusWidget::setUnfocusTexture(texture);
47   _unpr_unfocus_tx = texture;
48 }
49
50 void OtkButton::update(void)
51 {
52   if (_dirty) {
53     const BFont ft = getStyle()->getFont();
54     BColor *text_color = (isFocused() ? getStyle()->getTextFocus()
55                           : getStyle()->getTextUnfocus());
56     unsigned int bevel = getStyle()->getBevelWidth();
57
58     OtkFocusWidget::resize(ft.measureString(_text) + bevel * 2,
59                            ft.height() + bevel * 2);
60     OtkFocusWidget::update();
61
62     ft.drawString(getWindow(), bevel, bevel, *text_color, _text);
63   } else
64     OtkFocusWidget::update();
65
66   _dirty = false;
67 }
68
69 bool OtkButton::expose(const XExposeEvent &e)
70 {
71   _dirty = true;
72   return OtkFocusWidget::expose(e);
73 }
74
75 bool OtkButton::configure(const XConfigureEvent &e)
76 {
77   _dirty = true;
78   return OtkFocusWidget::configure(e);
79 }
80
81 }