diff options
Diffstat (limited to 'src/ClockTool.cc')
-rw-r--r-- | src/ClockTool.cc | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/src/ClockTool.cc b/src/ClockTool.cc new file mode 100644 index 0000000..cc74b35 --- /dev/null +++ b/src/ClockTool.cc | |||
@@ -0,0 +1,158 @@ | |||
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.5 2003/08/15 15:30:18 fluxgen Exp $ | ||
24 | |||
25 | #include "ClockTool.hh" | ||
26 | |||
27 | #include "ToolTheme.hh" | ||
28 | #include "Screen.hh" | ||
29 | |||
30 | #include "FbTk/SimpleCommand.hh" | ||
31 | #include "FbTk/ImageControl.hh" | ||
32 | |||
33 | #ifdef HAVE_CONFIG_H | ||
34 | #include "config.h" | ||
35 | #endif // HAVE_CONFIG_H | ||
36 | |||
37 | #include <ctime> | ||
38 | |||
39 | ClockTool::ClockTool(const FbTk::FbWindow &parent, | ||
40 | ToolTheme &theme, BScreen &screen): | ||
41 | ToolbarItem(ToolbarItem::FIXED), | ||
42 | m_button(parent, theme.font(), ""), | ||
43 | m_theme(theme), | ||
44 | m_screen(screen), | ||
45 | m_pixmap(0), | ||
46 | m_timeformat(screen.resourceManager(), std::string("%k:%M"), | ||
47 | screen.name() + ".strftimeFormat", screen.altName() + ".StrftimeFormat") { | ||
48 | // attach signals | ||
49 | theme.reconfigSig().attach(this); | ||
50 | |||
51 | // setup timer to update the graphics each second | ||
52 | timeval delay; | ||
53 | delay.tv_sec = 1; | ||
54 | delay.tv_usec = 0; | ||
55 | m_timer.setTimeout(delay); | ||
56 | FbTk::RefCount<FbTk::Command> update_graphic(new FbTk::SimpleCommand<ClockTool>(*this, | ||
57 | &ClockTool::updateTime)); | ||
58 | m_timer.setCommand(update_graphic); | ||
59 | m_timer.start(); | ||
60 | |||
61 | m_button.setGC(m_theme.textGC()); | ||
62 | |||
63 | update(0); | ||
64 | } | ||
65 | |||
66 | ClockTool::~ClockTool() { | ||
67 | // remove cached pixmap | ||
68 | if (m_pixmap) | ||
69 | m_screen.imageControl().removeImage(m_pixmap); | ||
70 | } | ||
71 | |||
72 | void ClockTool::move(int x, int y) { | ||
73 | m_button.move(x, y); | ||
74 | } | ||
75 | |||
76 | void ClockTool::resize(unsigned int width, unsigned int height) { | ||
77 | m_button.resize(width, height); | ||
78 | renderTheme(); | ||
79 | } | ||
80 | |||
81 | void ClockTool::moveResize(int x, int y, | ||
82 | unsigned int width, unsigned int height) { | ||
83 | m_button.moveResize(x, y, width, height); | ||
84 | renderTheme(); | ||
85 | } | ||
86 | |||
87 | void ClockTool::show() { | ||
88 | m_button.show(); | ||
89 | } | ||
90 | |||
91 | void ClockTool::hide() { | ||
92 | m_button.hide(); | ||
93 | } | ||
94 | |||
95 | void ClockTool::update(FbTk::Subject *subj) { | ||
96 | updateTime(); | ||
97 | |||
98 | // + 2 to make the entire text fit inside | ||
99 | std::string text; | ||
100 | for (size_t i=0; i<m_button.text().size() + 2; ++i) | ||
101 | text += '0'; | ||
102 | |||
103 | resize(m_theme.font().textWidth(text.c_str(), text.size()), m_button.height()); | ||
104 | } | ||
105 | |||
106 | unsigned int ClockTool::borderWidth() const { | ||
107 | return m_button.borderWidth(); | ||
108 | } | ||
109 | |||
110 | unsigned int ClockTool::width() const { | ||
111 | return m_button.width(); | ||
112 | } | ||
113 | |||
114 | unsigned int ClockTool::height() const { | ||
115 | return m_button.height(); | ||
116 | } | ||
117 | |||
118 | void ClockTool::updateTime() { | ||
119 | |||
120 | // update clock | ||
121 | time_t the_time = time(0); | ||
122 | |||
123 | if (the_time != -1) { | ||
124 | char time_string[255]; | ||
125 | struct tm *time_type = localtime(&the_time); | ||
126 | if (time_type == 0) | ||
127 | return; | ||
128 | |||
129 | #ifdef HAVE_STRFTIME | ||
130 | if (!strftime(time_string, 255, m_timeformat->c_str(), time_type)) | ||
131 | return; | ||
132 | m_button.setText(time_string); | ||
133 | #else // dont have strftime so we have to set it to hour:minut | ||
134 | // sprintf(time_string, "%d:%d", ); | ||
135 | #endif // HAVE_STRFTIME | ||
136 | } | ||
137 | |||
138 | m_button.clear(); | ||
139 | } | ||
140 | |||
141 | void ClockTool::renderTheme() { | ||
142 | Pixmap old_pm = m_pixmap; | ||
143 | if (m_theme.texture().type() == (FbTk::Texture::FLAT | FbTk::Texture::SOLID)) { | ||
144 | m_pixmap = 0; | ||
145 | m_button.setBackgroundColor(m_theme.texture().color()); | ||
146 | } else { | ||
147 | m_pixmap = m_screen.imageControl().renderImage(m_button.width(), m_button.height(), m_theme.texture()); | ||
148 | m_button.setBackgroundPixmap(m_pixmap); | ||
149 | } | ||
150 | |||
151 | if (old_pm) | ||
152 | m_screen.imageControl().removeImage(old_pm); | ||
153 | |||
154 | m_button.setJustify(m_theme.justify()); | ||
155 | m_button.setBorderWidth(m_theme.border().width()); | ||
156 | m_button.setBorderColor(m_theme.border().color()); | ||
157 | m_button.clear(); | ||
158 | } | ||