From 95d565ba62df3bd98aa026a98b9d3d6d8d6a509e Mon Sep 17 00:00:00 2001
From: fluxgen <fluxgen>
Date: Thu, 4 Dec 2003 23:02:23 +0000
Subject: toggle clock format via menu

---
 src/ClockTool.cc   | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 src/ClockTool.hh   |  8 ++++--
 src/ToolFactory.cc |  4 +--
 3 files changed, 78 insertions(+), 7 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 @@
 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 // DEALINGS IN THE SOFTWARE.
 
-// $Id: ClockTool.cc,v 1.5 2003/08/15 15:30:18 fluxgen Exp $
+// $Id: ClockTool.cc,v 1.6 2003/12/04 23:02:23 fluxgen Exp $
 
 #include "ClockTool.hh"
 
@@ -29,6 +29,8 @@
 
 #include "FbTk/SimpleCommand.hh"
 #include "FbTk/ImageControl.hh"
+#include "FbTk/Menu.hh"
+#include "FbTk/MenuItem.hh"
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -36,8 +38,68 @@
 
 #include <ctime>
 
+class ClockMenuItem: public FbTk::MenuItem {
+public:
+    ClockMenuItem::ClockMenuItem(ClockTool &tool):
+        FbTk::MenuItem(""), m_tool(tool) { 
+        // determine 12/24 hour format
+        if (m_tool.timeFormat().find("%k") != std::string::npos ||
+            m_tool.timeFormat().find("%H") != std::string::npos ||
+            m_tool.timeFormat().find("%T") != std::string::npos)
+            setLabel("Clock: 24h");
+        else
+            setLabel("Clock: 12h");
+    }
+
+    void click(int button, int time) {
+        std::string newformat = m_tool.timeFormat();
+        size_t pos = newformat.find("%k");
+        std::string newstr;
+        bool clock24hour = true;
+        if (pos != std::string::npos)
+            newstr = "%l";
+        else if ((pos = newformat.find("%H")) != std::string::npos)
+            newstr = "%I";
+        else if ((pos = newformat.find("%T")) != std::string::npos)
+            newstr = "%r";
+        
+        // 12 hour
+        if (newstr.empty()) {
+            clock24hour = false;
+            if ((pos = newformat.find("%l")) != std::string::npos)
+                newstr = "%k";
+            else if ((pos = newformat.find("%I")) != std::string::npos)
+                newstr = "%H";
+            else if ((pos = newformat.find("%r")) != std::string::npos)
+                newstr = "%T";
+            
+        }
+        
+        if (!newstr.empty()) {
+
+            newformat.replace(pos, 2, newstr);
+            if (!clock24hour) { // erase %P/%p (AM|PM / am|pm)
+                pos = newformat.find("%p");
+                if (pos != std::string::npos)
+                    newformat.erase(pos, 2);
+                else if ((pos = newformat.find("%P")) != std::string::npos)
+                    newformat.erase(pos, 2);
+            }
+            if (clock24hour)
+                setLabel("Clock: 24h");
+            else
+                setLabel("Clock: 12h");
+        
+            m_tool.setTimeFormat(newformat);
+        } // else some other strange format...so we don't do anything
+        FbTk::MenuItem::click(button, time);
+    }
+private:
+    ClockTool &m_tool;
+};
+
 ClockTool::ClockTool(const FbTk::FbWindow &parent,
-                     ToolTheme &theme, BScreen &screen):
+                     ToolTheme &theme, BScreen &screen, FbTk::Menu &menu):
     ToolbarItem(ToolbarItem::FIXED),
     m_button(parent, theme.font(), ""),
     m_theme(theme),
@@ -59,7 +121,7 @@ ClockTool::ClockTool(const FbTk::FbWindow &parent,
     m_timer.start();
 
     m_button.setGC(m_theme.textGC());
-
+    menu.insert(new ClockMenuItem(*this));
     update(0);
 }
 
@@ -92,6 +154,11 @@ void ClockTool::hide() {
     m_button.hide();
 }
 
+void ClockTool::setTimeFormat(const std::string &format) {
+    *m_timeformat = format;
+    update(0);
+}
+
 void ClockTool::update(FbTk::Subject *subj) {
     updateTime();
 
diff --git a/src/ClockTool.hh b/src/ClockTool.hh
index 9978f27..70f80cc 100644
--- a/src/ClockTool.hh
+++ b/src/ClockTool.hh
@@ -20,7 +20,7 @@
 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 // DEALINGS IN THE SOFTWARE.
 
-// $Id: ClockTool.hh,v 1.3 2003/08/19 16:12:43 fluxgen Exp $
+// $Id: ClockTool.hh,v 1.4 2003/12/04 23:02:23 fluxgen Exp $
 
 #ifndef CLOCKTOOL_HH
 #define CLOCKTOOL_HH
@@ -41,11 +41,12 @@ class BScreen;
 namespace FbTk {
 class ImageControl;
 class Subject;
+class Menu;
 }
 
 class ClockTool:public ToolbarItem, public FbTk::Observer {
 public:
-    ClockTool(const FbTk::FbWindow &parent, ToolTheme &theme, BScreen &screen);
+    ClockTool(const FbTk::FbWindow &parent, ToolTheme &theme, BScreen &screen, FbTk::Menu &menu);
     virtual ~ClockTool();
 
     void move(int x, int y);
@@ -55,9 +56,12 @@ public:
 
     void show();
     void hide();
+    void setTimeFormat(const std::string &format);
+    // accessors
     unsigned int width() const;
     unsigned int height() const;
     unsigned int borderWidth() const;
+    inline const std::string &timeFormat() const { return *m_timeformat; }
 private:
     void updateTime();
     void update(FbTk::Subject *subj);
diff --git a/src/ToolFactory.cc b/src/ToolFactory.cc
index 24ab499..522c339 100644
--- a/src/ToolFactory.cc
+++ b/src/ToolFactory.cc
@@ -19,7 +19,7 @@
 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 // DEALINGS IN THE SOFTWARE.
 
-// $Id: ToolFactory.cc,v 1.1 2003/10/13 23:37:41 fluxgen Exp $
+// $Id: ToolFactory.cc,v 1.2 2003/12/04 23:02:23 fluxgen Exp $
 
 #include "ToolFactory.hh"
 
@@ -100,7 +100,7 @@ ToolbarItem *ToolFactory::create(const std::string &name, const FbTk::FbWindow &
     } else if (name == "systemtray") {
         return new SystemTray(parent);
     } else if (name == "clock") {
-        return new ClockTool(parent, m_clock_theme, screen());
+        return new ClockTool(parent, m_clock_theme, screen(), tbar.menu());
     } else if (name == "nextworkspace" || 
                name == "prevworkspace") {
 
-- 
cgit v0.11.2