diff options
Diffstat (limited to 'src/FbTk/IntMenuItem.hh')
-rw-r--r-- | src/FbTk/IntMenuItem.hh | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/FbTk/IntMenuItem.hh b/src/FbTk/IntMenuItem.hh new file mode 100644 index 0000000..6246787 --- /dev/null +++ b/src/FbTk/IntMenuItem.hh | |||
@@ -0,0 +1,102 @@ | |||
1 | // IntMenuItem.hh for FbTk | ||
2 | // Copyright (c) 2003-2007 Henrik Kinnunen (fluxgen at fluxbox dot org) | ||
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 | #ifndef FBTK_INTMENUITEM_HH | ||
23 | #define FBTK_INTMENUITEM_HH | ||
24 | |||
25 | #include "MenuItem.hh" | ||
26 | #include "Accessor.hh" | ||
27 | |||
28 | #include <string> | ||
29 | |||
30 | namespace FbTk { | ||
31 | |||
32 | /// Changes an resource integer value between min and max | ||
33 | class IntMenuItem: public FbTk::MenuItem { | ||
34 | public: | ||
35 | IntMenuItem(const FbTk::FbString &label, Accessor<int> &res, | ||
36 | int min_val, int max_val, FbTk::Menu &host_menu) : | ||
37 | FbTk::MenuItem(label, host_menu), m_org_label(FbTk::MenuItem::label()), | ||
38 | m_max(max_val), m_min(min_val), m_res(res) { | ||
39 | updateLabel(); | ||
40 | setCloseOnClick(false); | ||
41 | } | ||
42 | |||
43 | /* Utility, but doesn't get found in anonymous namespace? */ | ||
44 | std::string appendIntValue(const std::string &label, int value) { | ||
45 | char *buff = new char[label.size() + 16]; | ||
46 | sprintf(buff, "%s: %d", label.c_str(), value); | ||
47 | std::string ret(buff); | ||
48 | delete [] buff; | ||
49 | return ret; | ||
50 | } | ||
51 | |||
52 | void click(int button, int time, unsigned int mods) { | ||
53 | static int last_time = -201; | ||
54 | int inc_val = 1; | ||
55 | // check double click | ||
56 | //!! TODO: must have some sort of "global" double click time in FbTk | ||
57 | if (time - last_time <= 200) | ||
58 | inc_val = 5; | ||
59 | |||
60 | last_time = time; | ||
61 | |||
62 | // make sure values stay within bounds _before_ we try to set m_res | ||
63 | // otherwise, this may cause bugs (say, with casting to unsigned char) | ||
64 | if ((button == 4 || button == 3) && m_res < m_max) { // up | ||
65 | if (m_res + inc_val < m_max) | ||
66 | m_res = m_res + inc_val; | ||
67 | else | ||
68 | m_res = m_max; | ||
69 | } else if ((button == 5 || button == 1) && m_res > m_min) { // down | ||
70 | if (m_res - inc_val >= m_min) | ||
71 | m_res = m_res - inc_val; | ||
72 | else | ||
73 | m_res = m_min; | ||
74 | } | ||
75 | |||
76 | // update label | ||
77 | updateLabel(); | ||
78 | // call other commands | ||
79 | FbTk::MenuItem::click(button, time, mods); | ||
80 | |||
81 | // show new value, which for us means forcing a full menu update | ||
82 | // since the text is drawn onto the background! | ||
83 | if (menu()) { | ||
84 | menu()->frameWindow().updateBackground(false); | ||
85 | menu()->clearWindow(); | ||
86 | } | ||
87 | } | ||
88 | |||
89 | void updateLabel() { | ||
90 | setLabel(appendIntValue(m_org_label, m_res)); | ||
91 | } | ||
92 | |||
93 | private: | ||
94 | std::string m_org_label; ///< original label | ||
95 | const int m_max; ///< maximum value the integer can have | ||
96 | const int m_min; ///< minimum value the integer can have | ||
97 | Accessor<int> &m_res; ///< resource item to be changed | ||
98 | }; | ||
99 | |||
100 | }; // end namespace FbTk | ||
101 | |||
102 | #endif // FBTK_INTMENUITEM_HH | ||