aboutsummaryrefslogtreecommitdiff
path: root/src/ClockTool.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/ClockTool.cc')
-rw-r--r--src/ClockTool.cc262
1 files changed, 262 insertions, 0 deletions
diff --git a/src/ClockTool.cc b/src/ClockTool.cc
new file mode 100644
index 0000000..889c65e
--- /dev/null
+++ b/src/ClockTool.cc
@@ -0,0 +1,262 @@
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.9 2003/12/19 18:26:48 fluxgen 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>
45using namespace std;
46
47class ClockMenuItem: public FbTk::MenuItem {
48public:
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 }
108private:
109 ClockTool &m_tool;
110};
111
112class EditClockFormatCmd: public FbTk::Command {
113public:
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
129ClockTool::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
165ClockTool::~ClockTool() {
166 // remove cached pixmap
167 if (m_pixmap)
168 m_screen.imageControl().removeImage(m_pixmap);
169}
170
171void ClockTool::move(int x, int y) {
172 m_button.move(x, y);
173}
174
175void ClockTool::resize(unsigned int width, unsigned int height) {
176 m_button.resize(width, height);
177 renderTheme();
178}
179
180void 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
186void ClockTool::show() {
187 m_button.show();
188}
189
190void ClockTool::hide() {
191 m_button.hide();
192}
193
194void ClockTool::setTimeFormat(const std::string &format) {
195 *m_timeformat = format;
196 update(0);
197}
198
199void 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
210unsigned int ClockTool::borderWidth() const {
211 return m_button.borderWidth();
212}
213
214unsigned int ClockTool::width() const {
215 return m_button.width();
216}
217
218unsigned int ClockTool::height() const {
219 return m_button.height();
220}
221
222void 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}
244
245void ClockTool::renderTheme() {
246 Pixmap old_pm = m_pixmap;
247 if (!m_theme.texture().usePixmap()) {
248 m_pixmap = 0;
249 m_button.setBackgroundColor(m_theme.texture().color());
250 } else {
251 m_pixmap = m_screen.imageControl().renderImage(m_button.width(), m_button.height(), m_theme.texture());
252 m_button.setBackgroundPixmap(m_pixmap);
253 }
254
255 if (old_pm)
256 m_screen.imageControl().removeImage(old_pm);
257
258 m_button.setJustify(m_theme.justify());
259 m_button.setBorderWidth(m_theme.border().width());
260 m_button.setBorderColor(m_theme.border().color());
261 m_button.clear();
262}