summaryrefslogtreecommitdiff
path: root/Clock/ClockWidget.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-02-05 09:44:13 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-02-05 09:44:13 +0100
commitd0078b6574d7022f207f3af492d2d8ec142b9bd1 (patch)
treeea197c8f9a4f5d796c840d9e3c031a06df1685dc /Clock/ClockWidget.cpp
parent41567c5bb91b86b698337340f77275fdc7b0dce4 (diff)
downloadserenity-d0078b6574d7022f207f3af492d2d8ec142b9bd1.zip
Clock: Turns the clock window from guitest2 into a separate program.
We can't not have a desktop clock app. :^)
Diffstat (limited to 'Clock/ClockWidget.cpp')
-rw-r--r--Clock/ClockWidget.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/Clock/ClockWidget.cpp b/Clock/ClockWidget.cpp
new file mode 100644
index 0000000000..94cf55cb35
--- /dev/null
+++ b/Clock/ClockWidget.cpp
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#include <time.h>
+#include <SharedGraphics/Painter.h>
+#include "ClockWidget.h"
+
+ClockWidget::ClockWidget(GWidget* parent)
+ : GWidget(parent)
+{
+ set_relative_rect({ 0, 0, 100, 40 });
+ start_timer(300);
+}
+
+ClockWidget::~ClockWidget()
+{
+}
+
+void ClockWidget::paint_event(GPaintEvent&)
+{
+ auto now = time(nullptr);
+ auto& tm = *localtime(&now);
+
+ char timeBuf[128];
+ sprintf(timeBuf, "%02u:%02u:%02u", tm.tm_hour, tm.tm_min, tm.tm_sec);
+
+ Painter painter(*this);
+ painter.fill_rect(rect(), Color::LightGray);
+ painter.draw_text(rect(), timeBuf, Painter::TextAlignment::Center, Color::Black);
+}
+
+void ClockWidget::timer_event(GTimerEvent&)
+{
+ auto now = time(nullptr);
+ if (now == m_last_time)
+ return;
+ m_last_time = now;
+ update();
+}