diff options
Diffstat (limited to 'src/ClockTool.cc')
-rw-r--r-- | src/ClockTool.cc | 73 |
1 files changed, 70 insertions, 3 deletions
diff --git a/src/ClockTool.cc b/src/ClockTool.cc index cc74b35..4120749 100644 --- a/src/ClockTool.cc +++ b/src/ClockTool.cc | |||
@@ -20,7 +20,7 @@ | |||
20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
21 | // DEALINGS IN THE SOFTWARE. | 21 | // DEALINGS IN THE SOFTWARE. |
22 | 22 | ||
23 | // $Id: ClockTool.cc,v 1.5 2003/08/15 15:30:18 fluxgen Exp $ | 23 | // $Id: ClockTool.cc,v 1.6 2003/12/04 23:02:23 fluxgen Exp $ |
24 | 24 | ||
25 | #include "ClockTool.hh" | 25 | #include "ClockTool.hh" |
26 | 26 | ||
@@ -29,6 +29,8 @@ | |||
29 | 29 | ||
30 | #include "FbTk/SimpleCommand.hh" | 30 | #include "FbTk/SimpleCommand.hh" |
31 | #include "FbTk/ImageControl.hh" | 31 | #include "FbTk/ImageControl.hh" |
32 | #include "FbTk/Menu.hh" | ||
33 | #include "FbTk/MenuItem.hh" | ||
32 | 34 | ||
33 | #ifdef HAVE_CONFIG_H | 35 | #ifdef HAVE_CONFIG_H |
34 | #include "config.h" | 36 | #include "config.h" |
@@ -36,8 +38,68 @@ | |||
36 | 38 | ||
37 | #include <ctime> | 39 | #include <ctime> |
38 | 40 | ||
41 | class ClockMenuItem: public FbTk::MenuItem { | ||
42 | public: | ||
43 | ClockMenuItem::ClockMenuItem(ClockTool &tool): | ||
44 | FbTk::MenuItem(""), m_tool(tool) { | ||
45 | // determine 12/24 hour format | ||
46 | if (m_tool.timeFormat().find("%k") != std::string::npos || | ||
47 | m_tool.timeFormat().find("%H") != std::string::npos || | ||
48 | m_tool.timeFormat().find("%T") != std::string::npos) | ||
49 | setLabel("Clock: 24h"); | ||
50 | else | ||
51 | setLabel("Clock: 12h"); | ||
52 | } | ||
53 | |||
54 | void click(int button, int time) { | ||
55 | std::string newformat = m_tool.timeFormat(); | ||
56 | size_t pos = newformat.find("%k"); | ||
57 | std::string newstr; | ||
58 | bool clock24hour = true; | ||
59 | if (pos != std::string::npos) | ||
60 | newstr = "%l"; | ||
61 | else if ((pos = newformat.find("%H")) != std::string::npos) | ||
62 | newstr = "%I"; | ||
63 | else if ((pos = newformat.find("%T")) != std::string::npos) | ||
64 | newstr = "%r"; | ||
65 | |||
66 | // 12 hour | ||
67 | if (newstr.empty()) { | ||
68 | clock24hour = false; | ||
69 | if ((pos = newformat.find("%l")) != std::string::npos) | ||
70 | newstr = "%k"; | ||
71 | else if ((pos = newformat.find("%I")) != std::string::npos) | ||
72 | newstr = "%H"; | ||
73 | else if ((pos = newformat.find("%r")) != std::string::npos) | ||
74 | newstr = "%T"; | ||
75 | |||
76 | } | ||
77 | |||
78 | if (!newstr.empty()) { | ||
79 | |||
80 | newformat.replace(pos, 2, newstr); | ||
81 | if (!clock24hour) { // erase %P/%p (AM|PM / am|pm) | ||
82 | pos = newformat.find("%p"); | ||
83 | if (pos != std::string::npos) | ||
84 | newformat.erase(pos, 2); | ||
85 | else if ((pos = newformat.find("%P")) != std::string::npos) | ||
86 | newformat.erase(pos, 2); | ||
87 | } | ||
88 | if (clock24hour) | ||
89 | setLabel("Clock: 24h"); | ||
90 | else | ||
91 | setLabel("Clock: 12h"); | ||
92 | |||
93 | m_tool.setTimeFormat(newformat); | ||
94 | } // else some other strange format...so we don't do anything | ||
95 | FbTk::MenuItem::click(button, time); | ||
96 | } | ||
97 | private: | ||
98 | ClockTool &m_tool; | ||
99 | }; | ||
100 | |||
39 | ClockTool::ClockTool(const FbTk::FbWindow &parent, | 101 | ClockTool::ClockTool(const FbTk::FbWindow &parent, |
40 | ToolTheme &theme, BScreen &screen): | 102 | ToolTheme &theme, BScreen &screen, FbTk::Menu &menu): |
41 | ToolbarItem(ToolbarItem::FIXED), | 103 | ToolbarItem(ToolbarItem::FIXED), |
42 | m_button(parent, theme.font(), ""), | 104 | m_button(parent, theme.font(), ""), |
43 | m_theme(theme), | 105 | m_theme(theme), |
@@ -59,7 +121,7 @@ ClockTool::ClockTool(const FbTk::FbWindow &parent, | |||
59 | m_timer.start(); | 121 | m_timer.start(); |
60 | 122 | ||
61 | m_button.setGC(m_theme.textGC()); | 123 | m_button.setGC(m_theme.textGC()); |
62 | 124 | menu.insert(new ClockMenuItem(*this)); | |
63 | update(0); | 125 | update(0); |
64 | } | 126 | } |
65 | 127 | ||
@@ -92,6 +154,11 @@ void ClockTool::hide() { | |||
92 | m_button.hide(); | 154 | m_button.hide(); |
93 | } | 155 | } |
94 | 156 | ||
157 | void ClockTool::setTimeFormat(const std::string &format) { | ||
158 | *m_timeformat = format; | ||
159 | update(0); | ||
160 | } | ||
161 | |||
95 | void ClockTool::update(FbTk::Subject *subj) { | 162 | void ClockTool::update(FbTk::Subject *subj) { |
96 | updateTime(); | 163 | updateTime(); |
97 | 164 | ||