diff options
Diffstat (limited to 'src/FbTk/Button.cc')
-rw-r--r-- | src/FbTk/Button.cc | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/src/FbTk/Button.cc b/src/FbTk/Button.cc new file mode 100644 index 0000000..03b5283 --- /dev/null +++ b/src/FbTk/Button.cc | |||
@@ -0,0 +1,150 @@ | |||
1 | // Button.cc for FbTk - fluxbox toolkit | ||
2 | // Copyright (c) 2002 Henrik Kinnunen (fluxgen at users.sourceforge.net) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | // $Id: Button.cc,v 1.8 2003/06/05 12:42:31 fluxgen Exp $ | ||
23 | |||
24 | #include "Button.hh" | ||
25 | |||
26 | #include "Command.hh" | ||
27 | #include "EventManager.hh" | ||
28 | #include "App.hh" | ||
29 | |||
30 | namespace FbTk { | ||
31 | |||
32 | Button::Button(int screen_num, int x, int y, | ||
33 | unsigned int width, unsigned int height): | ||
34 | m_win(screen_num, x, y, width, height, | ||
35 | ExposureMask | ButtonPressMask | ButtonReleaseMask), | ||
36 | m_foreground_pm(0), | ||
37 | m_background_pm(0), | ||
38 | m_pressed_pm(0), | ||
39 | m_gc(DefaultGC(FbTk::App::instance()->display(), screen_num)), | ||
40 | m_pressed(false) { | ||
41 | |||
42 | // add this to eventmanager | ||
43 | FbTk::EventManager::instance()->add(*this, m_win); | ||
44 | } | ||
45 | |||
46 | Button::Button(const FbWindow &parent, int x, int y, | ||
47 | unsigned int width, unsigned int height): | ||
48 | m_win(parent, x, y, width, height, | ||
49 | ExposureMask | ButtonPressMask | ButtonReleaseMask), | ||
50 | m_foreground_pm(0), | ||
51 | m_background_pm(0), | ||
52 | m_pressed_pm(0), | ||
53 | m_gc(DefaultGC(FbTk::App::instance()->display(), m_win.screenNumber())), | ||
54 | m_pressed(false) { | ||
55 | // add this to eventmanager | ||
56 | FbTk::EventManager::instance()->add(*this, m_win); | ||
57 | } | ||
58 | |||
59 | Button::~Button() { | ||
60 | FbTk::EventManager::instance()->remove(m_win); | ||
61 | } | ||
62 | |||
63 | void Button::setOnClick(RefCount<Command> &cmd, int button) { | ||
64 | // we only handle buttons 1 to 5 | ||
65 | if (button > 5 || button == 0) | ||
66 | return; | ||
67 | //set on click command for the button | ||
68 | m_onclick[button - 1] = cmd; | ||
69 | } | ||
70 | |||
71 | void Button::move(int x, int y) { | ||
72 | m_win.move(x, y); | ||
73 | } | ||
74 | |||
75 | void Button::resize(unsigned int w, unsigned int h) { | ||
76 | m_win.resize(w, h); | ||
77 | } | ||
78 | |||
79 | void Button::moveResize(int x, int y, unsigned int width, unsigned int height) { | ||
80 | m_win.moveResize(x, y, width, height); | ||
81 | } | ||
82 | |||
83 | void Button::setPixmap(Pixmap pm) { | ||
84 | m_foreground_pm = pm; | ||
85 | } | ||
86 | |||
87 | void Button::setPressedPixmap(Pixmap pm) { | ||
88 | m_pressed_pm = pm; | ||
89 | } | ||
90 | |||
91 | void Button::setBackgroundColor(const Color &color) { | ||
92 | m_win.setBackgroundColor(color); | ||
93 | m_background_color = color; | ||
94 | clear(); | ||
95 | } | ||
96 | |||
97 | void Button::setBackgroundPixmap(Pixmap pm) { | ||
98 | m_win.setBackgroundPixmap(pm); | ||
99 | m_background_pm = pm; | ||
100 | clear(); | ||
101 | } | ||
102 | |||
103 | void Button::show() { | ||
104 | m_win.show(); | ||
105 | } | ||
106 | |||
107 | void Button::hide() { | ||
108 | m_win.hide(); | ||
109 | } | ||
110 | |||
111 | void Button::buttonPressEvent(XButtonEvent &event) { | ||
112 | if (m_pressed_pm != 0) | ||
113 | m_win.setBackgroundPixmap(m_pressed_pm); | ||
114 | m_pressed = true; | ||
115 | clear(); | ||
116 | |||
117 | } | ||
118 | |||
119 | void Button::buttonReleaseEvent(XButtonEvent &event) { | ||
120 | m_pressed = false; | ||
121 | if (m_background_pm) | ||
122 | m_win.setBackgroundPixmap(m_background_pm); | ||
123 | else | ||
124 | m_win.setBackgroundColor(m_background_color); | ||
125 | clear(); // clear background | ||
126 | |||
127 | if (m_foreground_pm) { // draw foreground | ||
128 | Display *disp = App::instance()->display(); | ||
129 | |||
130 | if (m_gc == 0) // get default gc | ||
131 | m_gc = DefaultGC(disp, m_win.screenNumber()); | ||
132 | |||
133 | XCopyArea(disp, m_foreground_pm, m_win.window(), m_gc, 0, 0, width(), height(), 0, 0); | ||
134 | } | ||
135 | |||
136 | if (event.x < 0 || event.y < 0 || | ||
137 | event.x > width() || event.y > height()) | ||
138 | return; | ||
139 | |||
140 | if (event.button > 0 && event.button <= 5 && | ||
141 | m_onclick[event.button -1].get() != 0) | ||
142 | m_onclick[event.button - 1]->execute(); | ||
143 | |||
144 | } | ||
145 | |||
146 | void Button::exposeEvent(XExposeEvent &event) { | ||
147 | m_win.clear(); | ||
148 | } | ||
149 | |||
150 | }; // end namespace FbTk | ||