diff options
Diffstat (limited to 'src/ClockTool.cc')
-rw-r--r-- | src/ClockTool.cc | 265 |
1 files changed, 265 insertions, 0 deletions
diff --git a/src/ClockTool.cc b/src/ClockTool.cc new file mode 100644 index 0000000..a65a4ea --- /dev/null +++ b/src/ClockTool.cc | |||
@@ -0,0 +1,265 @@ | |||
1 | // ClockTool.cc | ||
2 | // Copyright (c) 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net) | ||
3 | // and Simon Bowden (rathnor at users.sourceforge.net) | ||
4 | // | ||
5 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
6 | // copy of this software and associated documentation files (the "Software"), | ||
7 | // to deal in the Software without restriction, including without limitation | ||
8 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
9 | // and/or sell copies of the Software, and to permit persons to whom the | ||
10 | // Software is furnished to do so, subject to the following conditions: | ||
11 | // | ||
12 | // The above copyright notice and this permission notice shall be included in | ||
13 | // all copies or substantial portions of the Software. | ||
14 | // | ||
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
21 | // DEALINGS IN THE SOFTWARE. | ||
22 | |||
23 | // $Id: ClockTool.cc,v 1.10 2004/01/13 14:41:32 rathnor Exp $ | ||
24 | |||
25 | #include "ClockTool.hh" | ||
26 | |||
27 | #include "ToolTheme.hh" | ||
28 | #include "Screen.hh" | ||
29 | #include "CommandParser.hh" | ||
30 | #include "CommandDialog.hh" | ||
31 | #include "fluxbox.hh" | ||
32 | |||
33 | #include "FbTk/SimpleCommand.hh" | ||
34 | #include "FbTk/ImageControl.hh" | ||
35 | #include "FbTk/Menu.hh" | ||
36 | #include "FbTk/MenuItem.hh" | ||
37 | |||
38 | #ifdef HAVE_CONFIG_H | ||
39 | #include "config.h" | ||
40 | #endif // HAVE_CONFIG_H | ||
41 | |||
42 | #include <ctime> | ||
43 | #include <string> | ||
44 | #include <iostream> | ||
45 | using namespace std; | ||
46 | |||
47 | class ClockMenuItem: public FbTk::MenuItem { | ||
48 | public: | ||
49 | explicit ClockMenuItem::ClockMenuItem(ClockTool &tool): | ||
50 | FbTk::MenuItem(""), m_tool(tool) { | ||
51 | // determine 12/24 hour format | ||
52 | if (m_tool.timeFormat().find("%k") != std::string::npos || | ||
53 | m_tool.timeFormat().find("%H") != std::string::npos || | ||
54 | m_tool.timeFormat().find("%T") != std::string::npos) | ||
55 | setLabel("Clock: 24h"); | ||
56 | else | ||
57 | setLabel("Clock: 12h"); | ||
58 | } | ||
59 | |||
60 | void click(int button, int time) { | ||
61 | std::string newformat = m_tool.timeFormat(); | ||
62 | size_t pos = newformat.find("%k"); | ||
63 | std::string newstr; | ||
64 | bool clock24hour = true; | ||
65 | if (pos != std::string::npos) | ||
66 | newstr = "%l"; | ||
67 | else if ((pos = newformat.find("%H")) != std::string::npos) | ||
68 | newstr = "%I"; | ||
69 | else if ((pos = newformat.find("%T")) != std::string::npos) | ||
70 | newstr = "%r"; | ||
71 | |||
72 | // 12 hour | ||
73 | if (newstr.empty()) { | ||
74 | clock24hour = false; | ||
75 | if ((pos = newformat.find("%l")) != std::string::npos) | ||
76 | newstr = "%k"; | ||
77 | else if ((pos = newformat.find("%I")) != std::string::npos) | ||
78 | newstr = "%H"; | ||
79 | else if ((pos = newformat.find("%r")) != std::string::npos) | ||
80 | newstr = "%T"; | ||
81 | |||
82 | } | ||
83 | |||
84 | if (!newstr.empty()) { | ||
85 | |||
86 | newformat.replace(pos, 2, newstr); | ||
87 | if (!clock24hour) { // erase %P/%p (AM|PM / am|pm) | ||
88 | pos = newformat.find("%p"); | ||
89 | if (pos != std::string::npos) | ||
90 | newformat.erase(pos, 2); | ||
91 | else if ((pos = newformat.find("%P")) != std::string::npos) | ||
92 | newformat.erase(pos, 2); | ||
93 | } | ||
94 | |||
95 | |||
96 | m_tool.setTimeFormat(newformat); | ||
97 | |||
98 | if (m_tool.timeFormat().find("%k") != std::string::npos || | ||
99 | m_tool.timeFormat().find("%H") != std::string::npos || | ||
100 | m_tool.timeFormat().find("%T") != std::string::npos) | ||
101 | setLabel("Clock: 24h"); | ||
102 | else | ||
103 | setLabel("Clock: 12h"); | ||
104 | |||
105 | } // else some other strange format...so we don't do anything | ||
106 | FbTk::MenuItem::click(button, time); | ||
107 | } | ||
108 | private: | ||
109 | ClockTool &m_tool; | ||
110 | }; | ||
111 | |||
112 | class EditClockFormatCmd: public FbTk::Command { | ||
113 | public: | ||
114 | void execute() { | ||
115 | BScreen *screen = Fluxbox::instance()->mouseScreen(); | ||
116 | if (screen == 0) | ||
117 | return; | ||
118 | std::string resourcename = screen->name() + ".strftimeFormat"; | ||
119 | |||
120 | CommandDialog *dialog = new CommandDialog(*screen, "Edit Clock Format", | ||
121 | "SetResourceValue " + resourcename + " "); | ||
122 | FbTk::RefCount<FbTk::Command> cmd(CommandParser::instance().parseLine("reconfigure")); | ||
123 | dialog->setPostCommand(cmd); | ||
124 | dialog->setText(screen->resourceManager().resourceValue(resourcename)); | ||
125 | dialog->show(); | ||
126 | } | ||
127 | }; | ||
128 | |||
129 | ClockTool::ClockTool(const FbTk::FbWindow &parent, | ||
130 | ToolTheme &theme, BScreen &screen, FbTk::Menu &menu): | ||
131 | ToolbarItem(ToolbarItem::FIXED), | ||
132 | m_button(parent, theme.font(), ""), | ||
133 | m_theme(theme), | ||
134 | m_screen(screen), | ||
135 | m_pixmap(0), | ||
136 | m_timeformat(screen.resourceManager(), std::string("%k:%M"), | ||
137 | screen.name() + ".strftimeFormat", screen.altName() + ".StrftimeFormat") { | ||
138 | // attach signals | ||
139 | theme.reconfigSig().attach(this); | ||
140 | |||
141 | // setup timer to update the graphics each second | ||
142 | timeval delay; | ||
143 | delay.tv_sec = 1; | ||
144 | delay.tv_usec = 0; | ||
145 | m_timer.setTimeout(delay); | ||
146 | FbTk::RefCount<FbTk::Command> update_graphic(new FbTk::SimpleCommand<ClockTool>(*this, | ||
147 | &ClockTool::updateTime)); | ||
148 | m_timer.setCommand(update_graphic); | ||
149 | m_timer.start(); | ||
150 | |||
151 | m_button.setGC(m_theme.textGC()); | ||
152 | |||
153 | // setup menu | ||
154 | FbTk::RefCount<FbTk::Command> saverc(CommandParser::instance().parseLine("saverc")); | ||
155 | FbTk::MenuItem *item = new ClockMenuItem(*this); | ||
156 | item->setCommand(saverc); | ||
157 | menu.insert(item); | ||
158 | FbTk::RefCount<FbTk::Command> editformat_cmd(new EditClockFormatCmd()); | ||
159 | menu.insert("Edit Clock Format", editformat_cmd); | ||
160 | |||
161 | |||
162 | update(0); | ||
163 | } | ||
164 | |||
165 | ClockTool::~ClockTool() { | ||
166 | // remove cached pixmap | ||
167 | if (m_pixmap) | ||
168 | m_screen.imageControl().removeImage(m_pixmap); | ||
169 | } | ||
170 | |||
171 | void ClockTool::move(int x, int y) { | ||
172 | m_button.move(x, y); | ||
173 | } | ||
174 | |||
175 | void ClockTool::resize(unsigned int width, unsigned int height) { | ||
176 | m_button.resize(width, height); | ||
177 | renderTheme(); | ||
178 | } | ||
179 | |||
180 | void ClockTool::moveResize(int x, int y, | ||
181 | unsigned int width, unsigned int height) { | ||
182 | m_button.moveResize(x, y, width, height); | ||
183 | renderTheme(); | ||
184 | } | ||
185 | |||
186 | void ClockTool::show() { | ||
187 | m_button.show(); | ||
188 | } | ||
189 | |||
190 | void ClockTool::hide() { | ||
191 | m_button.hide(); | ||
192 | } | ||
193 | |||
194 | void ClockTool::setTimeFormat(const std::string &format) { | ||
195 | *m_timeformat = format; | ||
196 | update(0); | ||
197 | } | ||
198 | |||
199 | void ClockTool::update(FbTk::Subject *subj) { | ||
200 | updateTime(); | ||
201 | |||
202 | // + 2 to make the entire text fit inside | ||
203 | std::string text; | ||
204 | for (size_t i=0; i<m_button.text().size() + 2; ++i) | ||
205 | text += '0'; | ||
206 | |||
207 | resize(m_theme.font().textWidth(text.c_str(), text.size()), m_button.height()); | ||
208 | } | ||
209 | |||
210 | unsigned int ClockTool::borderWidth() const { | ||
211 | return m_button.borderWidth(); | ||
212 | } | ||
213 | |||
214 | unsigned int ClockTool::width() const { | ||
215 | return m_button.width(); | ||
216 | } | ||
217 | |||
218 | unsigned int ClockTool::height() const { | ||
219 | return m_button.height(); | ||
220 | } | ||
221 | |||
222 | void ClockTool::updateTime() { | ||
223 | |||
224 | // update clock | ||
225 | time_t the_time = time(0); | ||
226 | |||
227 | if (the_time != -1) { | ||
228 | char time_string[255]; | ||
229 | struct tm *time_type = localtime(&the_time); | ||
230 | if (time_type == 0) | ||
231 | return; | ||
232 | |||
233 | #ifdef HAVE_STRFTIME | ||
234 | if (!strftime(time_string, 255, m_timeformat->c_str(), time_type)) | ||
235 | return; | ||
236 | m_button.setText(time_string); | ||
237 | #else // dont have strftime so we have to set it to hour:minut | ||
238 | // sprintf(time_string, "%d:%d", ); | ||
239 | #endif // HAVE_STRFTIME | ||
240 | } | ||
241 | |||
242 | m_button.clear(); | ||
243 | m_button.updateTransparent(); | ||
244 | } | ||
245 | |||
246 | void ClockTool::renderTheme() { | ||
247 | Pixmap old_pm = m_pixmap; | ||
248 | if (!m_theme.texture().usePixmap()) { | ||
249 | m_pixmap = 0; | ||
250 | m_button.setBackgroundColor(m_theme.texture().color()); | ||
251 | } else { | ||
252 | m_pixmap = m_screen.imageControl().renderImage(m_button.width(), m_button.height(), m_theme.texture()); | ||
253 | m_button.setBackgroundPixmap(m_pixmap); | ||
254 | } | ||
255 | |||
256 | if (old_pm) | ||
257 | m_screen.imageControl().removeImage(old_pm); | ||
258 | |||
259 | m_button.setJustify(m_theme.justify()); | ||
260 | m_button.setBorderWidth(m_theme.border().width()); | ||
261 | m_button.setBorderColor(m_theme.border().color()); | ||
262 | m_button.setAlpha(m_theme.alpha()); | ||
263 | m_button.clear(); | ||
264 | m_button.updateTransparent(); | ||
265 | } | ||