summaryrefslogtreecommitdiff
path: root/src/FbTk
diff options
context:
space:
mode:
authorMark Tiefenbruck <mark@fluxbox.org>2007-12-27 21:55:24 (GMT)
committerMark Tiefenbruck <mark@fluxbox.org>2007-12-27 21:55:24 (GMT)
commitb5c354b994bc06667abe35e2d528c0f025703c4e (patch)
tree081fd3207053a06cb50931ccec3237bc614f10eb /src/FbTk
parent1f5cd12facc662de240b36bf3c5c14f40adf391b (diff)
downloadfluxbox_lack-b5c354b994bc06667abe35e2d528c0f025703c4e.zip
fluxbox_lack-b5c354b994bc06667abe35e2d528c0f025703c4e.tar.bz2
architecture astronomy
Diffstat (limited to 'src/FbTk')
-rw-r--r--src/FbTk/Accessor.hh85
-rw-r--r--src/FbTk/BoolMenuItem.hh62
-rw-r--r--src/FbTk/DefaultValue.hh54
-rw-r--r--src/FbTk/IntMenuItem.hh102
-rw-r--r--src/FbTk/Makefile.am3
-rw-r--r--src/FbTk/Resource.hh19
6 files changed, 275 insertions, 50 deletions
diff --git a/src/FbTk/Accessor.hh b/src/FbTk/Accessor.hh
new file mode 100644
index 0000000..88db1aa
--- /dev/null
+++ b/src/FbTk/Accessor.hh
@@ -0,0 +1,85 @@
1// Accessor.hh
2// Copyright (c) 2007 Fluxbox Team (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_ACCESSOR_HH
23#define FBTK_ACCESSOR_HH
24
25namespace FbTk {
26
27// base class for objects that act like data type T
28template <typename T>
29class Accessor {
30public:
31 virtual Accessor<T> &operator =(const T &val) = 0;
32 virtual operator T() const = 0;
33};
34
35// essentially just a reference
36template <typename T>
37class SimpleAccessor: public Accessor<T> {
38public:
39 SimpleAccessor(T &val): m_val(val) { }
40 inline Accessor<T> &operator =(const T &val) { m_val = val; return *this; }
41 inline operator T() const { return m_val; }
42
43private:
44 T &m_val;
45};
46
47// use object methods as an accessor
48template <typename T, typename Receiver>
49class ObjectAccessor: public Accessor<T> {
50public:
51 typedef T (Receiver:: *Getter)() const;
52 typedef void (Receiver:: *Setter)(T &);
53 ObjectAccessor(Receiver &r, Getter g, Setter s):
54 m_receiver(r), m_getter(g), m_setter(s) { }
55
56 inline operator T() const { return (m_receiver.*m_getter)(); }
57 inline Accessor<T> &operator =(const T &val) {
58 (m_receiver.*m_setter)(val); return *this;
59 }
60
61private:
62 Receiver &m_receiver;
63 Getter m_getter;
64 Setter m_setter;
65};
66
67// same as above but with no set method
68template <typename T, typename Receiver>
69class ConstObjectAccessor: public Accessor<T> {
70public:
71 typedef T (Receiver:: *Getter)() const;
72 ConstObjectAccessor(const Receiver &r, Getter g):
73 m_receiver(r), m_getter(g) { }
74
75 inline operator T() const { return (m_receiver.*m_getter)(); }
76 inline Accessor<T> &operator =(const T &val) { return *this; }
77
78private:
79 const Receiver &m_receiver;
80 Getter m_getter;
81};
82
83}; // end namespace FbTk
84
85#endif // FBTK_ACCESSOR_HH
diff --git a/src/FbTk/BoolMenuItem.hh b/src/FbTk/BoolMenuItem.hh
new file mode 100644
index 0000000..2e8b2c3
--- /dev/null
+++ b/src/FbTk/BoolMenuItem.hh
@@ -0,0 +1,62 @@
1// BoolMenuItem.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_BOOLMENUITEM_HH
23#define FBTK_BOOLMENUITEM_HH
24
25#include "MenuItem.hh"
26#include "Accessor.hh"
27
28namespace FbTk {
29
30/// a bool menu item
31class BoolMenuItem: public FbTk::MenuItem {
32public:
33 BoolMenuItem(const FbTk::FbString &label, Accessor<bool> &item,
34 FbTk::RefCount<FbTk::Command> &cmd):
35 FbTk::MenuItem(label, cmd), m_item(item) {
36 FbTk::MenuItem::setSelected(m_item);
37 setToggleItem(true);
38 setCloseOnClick(false);
39 }
40 BoolMenuItem(const FbTk::FbString &label, Accessor<bool> &item):
41 FbTk::MenuItem(label), m_item(item) {
42 FbTk::MenuItem::setSelected(m_item);
43 setToggleItem(true);
44 setCloseOnClick(false);
45 }
46 bool isSelected() const { return m_item; }
47 // toggle state
48 void click(int button, int time, unsigned int mods) {
49 setSelected(!m_item);
50 FbTk::MenuItem::click(button, time, mods);
51 }
52 void setSelected(bool value) {
53 m_item = value;
54 FbTk::MenuItem::setSelected(m_item);
55 }
56private:
57 Accessor<bool> &m_item;
58};
59
60}; // end namespace FbTk
61
62#endif // FBTK_BOOLMENUITEM_HH
diff --git a/src/FbTk/DefaultValue.hh b/src/FbTk/DefaultValue.hh
index 4e7eb49..95b534a 100644
--- a/src/FbTk/DefaultValue.hh
+++ b/src/FbTk/DefaultValue.hh
@@ -22,58 +22,32 @@
22#ifndef FBTK_DEFAULTVALUE_HH 22#ifndef FBTK_DEFAULTVALUE_HH
23#define FBTK_DEFAULTVALUE_HH 23#define FBTK_DEFAULTVALUE_HH
24 24
25namespace FbTk { 25#include "Accessor.hh"
26
27// classes for overriding default values without having to listen for changes
28template <typename T>
29class DefaultValue {
30public:
31 DefaultValue(const T &def):
32 m_default(def), m_actual(def), m_use_default(true) { }
33 26
34 inline const T &get() const { return m_use_default ? m_default : m_actual; } 27namespace FbTk {
35 inline void set(const T &val) { m_use_default = false; m_actual = val; }
36 inline void restoreDefault() { m_use_default = true; }
37 inline bool isDefault() const { return m_use_default; }
38
39 inline DefaultValue<T> &operator =(const T &val) {
40 set(val); return *this;
41 }
42 28
43 inline operator T() const { return get(); } 29// class for overriding default values and restoring them later
44 30
45private: 31// Ret = type of value that gets returned
46 const T &m_default; 32// Def = type of default value -- may be Accessor<Ret> &, for example
47 T m_actual; 33template <typename Ret, typename Def=Ret &>
48 bool m_use_default; 34class DefaultValue: public Accessor<Ret> {
49};
50
51// designed for use with built-in types T, thus no need to return references
52template <typename T, typename Receiver>
53class DefaultAccessor {
54public: 35public:
55 typedef T (Receiver:: *Accessor)() const; 36 DefaultValue(const Def def):
56 DefaultAccessor(const Receiver &r, Accessor a): 37 m_default(def), m_actual(def), m_use_default(true) { }
57 m_receiver(r), m_accessor(a), m_actual((r.*a)()),
58 m_use_default(true) { }
59 38
60 inline const T get() const {
61 return m_use_default ? (m_receiver.*m_accessor)() : m_actual;
62 }
63 inline void set(const T &val) { m_use_default = false; m_actual = val; }
64 inline void restoreDefault() { m_use_default = true; } 39 inline void restoreDefault() { m_use_default = true; }
65 inline bool isDefault() const { return m_use_default; } 40 inline bool isDefault() const { return m_use_default; }
66 41
67 inline DefaultAccessor<T, Receiver> &operator =(const T &val) { 42 inline DefaultValue<Ret, Def> &operator =(const Ret &val) {
68 set(val); return *this; 43 m_use_default = false; m_actual = val; return *this;
69 } 44 }
70 45
71 inline operator T() const { return get(); } 46 inline operator Ret() const { return m_use_default ? m_default : m_actual; }
72 47
73private: 48private:
74 const Receiver &m_receiver; 49 const Def m_default;
75 Accessor m_accessor; 50 Ret m_actual;
76 T m_actual;
77 bool m_use_default; 51 bool m_use_default;
78}; 52};
79 53
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
30namespace FbTk {
31
32/// Changes an resource integer value between min and max
33class IntMenuItem: public FbTk::MenuItem {
34public:
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
93private:
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
diff --git a/src/FbTk/Makefile.am b/src/FbTk/Makefile.am
index 0de5475..e3e5d76 100644
--- a/src/FbTk/Makefile.am
+++ b/src/FbTk/Makefile.am
@@ -16,7 +16,7 @@ imlib2_SOURCE= ImageImlib2.hh ImageImlib2.cc
16endif 16endif
17 17
18libFbTk_a_SOURCES = App.hh App.cc Color.cc Color.hh Command.hh \ 18libFbTk_a_SOURCES = App.hh App.cc Color.cc Color.hh Command.hh \
19 ObjectRegistry.hh DefaultValue.hh \ 19 ObjectRegistry.hh Accessor.hh DefaultValue.hh \
20 FileUtil.hh FileUtil.cc \ 20 FileUtil.hh FileUtil.cc \
21 EventHandler.hh EventManager.hh EventManager.cc \ 21 EventHandler.hh EventManager.hh EventManager.cc \
22 FbWindow.hh FbWindow.cc Font.cc Font.hh FontImp.hh \ 22 FbWindow.hh FbWindow.cc Font.cc Font.hh FontImp.hh \
@@ -25,6 +25,7 @@ libFbTk_a_SOURCES = App.hh App.cc Color.cc Color.hh Command.hh \
25 LogicCommands.hh LogicCommands.cc \ 25 LogicCommands.hh LogicCommands.cc \
26 MacroCommand.hh MacroCommand.cc \ 26 MacroCommand.hh MacroCommand.cc \
27 Menu.hh Menu.cc MenuItem.hh MenuItem.cc \ 27 Menu.hh Menu.cc MenuItem.hh MenuItem.cc \
28 BoolMenuItem.hh IntMenuItem.hh \
28 MultiButtonMenuItem.hh MultiButtonMenuItem.cc \ 29 MultiButtonMenuItem.hh MultiButtonMenuItem.cc \
29 MenuTheme.hh MenuTheme.cc NotCopyable.hh \ 30 MenuTheme.hh MenuTheme.cc NotCopyable.hh \
30 RefCount.hh SimpleCommand.hh SignalHandler.cc SignalHandler.hh \ 31 RefCount.hh SimpleCommand.hh SignalHandler.cc SignalHandler.hh \
diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh
index 55eeddc..20ce3ba 100644
--- a/src/FbTk/Resource.hh
+++ b/src/FbTk/Resource.hh
@@ -25,6 +25,7 @@
25#define FBTK_RESOURCE_HH 25#define FBTK_RESOURCE_HH
26 26
27#include "NotCopyable.hh" 27#include "NotCopyable.hh"
28#include "Accessor.hh"
28 29
29#include <string> 30#include <string>
30#include <list> 31#include <list>
@@ -160,23 +161,22 @@ private:
160 161
161/// Real resource class 162/// Real resource class
162/** 163/**
163 * usage: Resource<int> someresource(resourcemanager, 10, "someresourcename", "somealternativename"); \n 164 * usage: Resource<int> someresource(resourcemanager, 10, "someresourcename", "somealternativename");
164 * and then implement setFromString and getString \n 165 * and then implement setFromString and getString
165 * example: \n 166 * example:
166 * template <> \n 167 * template <>
167 * void Resource<int>::setFromString(const char *str) { \n 168 * void Resource<int>::setFromString(const char *str) {
168 * *(*this) = atoi(str); \n 169 * *(*this) = atoi(str);
169 * } 170 * }
170 */ 171 */
171template <typename T> 172template <typename T>
172class Resource:public Resource_base 173class Resource:public Resource_base, public Accessor<T> {
173{
174public: 174public:
175 typedef T Type; 175 typedef T Type;
176 Resource(ResourceManager &rm, T val, 176 Resource(ResourceManager &rm, T val,
177 const std::string &name, const std::string &altname): 177 const std::string &name, const std::string &altname):
178 Resource_base(name, altname), 178 Resource_base(name, altname),
179 m_value(val), m_defaultval(val), 179 m_value(val), m_defaultval(val),
180 m_rm(rm) { 180 m_rm(rm) {
181 m_rm.addResource(*this); // add this to resource handler 181 m_rm.addResource(*this); // add this to resource handler
182 } 182 }
@@ -192,6 +192,7 @@ public:
192 /// @return string value of resource 192 /// @return string value of resource
193 std::string getString() const; 193 std::string getString() const;
194 194
195 inline operator T() const { return m_value; }
195 inline T& get() { return m_value; } 196 inline T& get() { return m_value; }
196 inline T& operator*() { return m_value; } 197 inline T& operator*() { return m_value; }
197 inline const T& operator*() const { return m_value; } 198 inline const T& operator*() const { return m_value; }