diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-02-05 09:44:13 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-02-05 09:44:13 +0100 |
commit | d0078b6574d7022f207f3af492d2d8ec142b9bd1 (patch) | |
tree | ea197c8f9a4f5d796c840d9e3c031a06df1685dc /Clock | |
parent | 41567c5bb91b86b698337340f77275fdc7b0dce4 (diff) | |
download | serenity-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')
-rw-r--r-- | Clock/.gitignore | 3 | ||||
-rw-r--r-- | Clock/ClockWidget.cpp | 37 | ||||
-rw-r--r-- | Clock/ClockWidget.h | 16 | ||||
-rw-r--r-- | Clock/Makefile | 35 | ||||
-rw-r--r-- | Clock/main.cpp | 21 |
5 files changed, 112 insertions, 0 deletions
diff --git a/Clock/.gitignore b/Clock/.gitignore new file mode 100644 index 0000000000..6710497633 --- /dev/null +++ b/Clock/.gitignore @@ -0,0 +1,3 @@ +*.o +*.d +Clock 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(); +} diff --git a/Clock/ClockWidget.h b/Clock/ClockWidget.h new file mode 100644 index 0000000000..d9702a575f --- /dev/null +++ b/Clock/ClockWidget.h @@ -0,0 +1,16 @@ +#pragma once + +#include <LibGUI/GWidget.h> + +class ClockWidget final : public GWidget { +public: + explicit ClockWidget(GWidget* parent = nullptr); + virtual ~ClockWidget() override; + +private: + virtual void paint_event(GPaintEvent&) override; + virtual void timer_event(GTimerEvent&) override; + + time_t m_last_time { 0 }; +}; + diff --git a/Clock/Makefile b/Clock/Makefile new file mode 100644 index 0000000000..50865fd3a7 --- /dev/null +++ b/Clock/Makefile @@ -0,0 +1,35 @@ +OBJS = \ + ClockWidget.o \ + main.o + +APP = Clock + +ARCH_FLAGS = +STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib -nostdinc +USERLAND_FLAGS = -ffreestanding -fno-stack-protector -fno-ident +WARNING_FLAGS = -Wextra -Wall -Wundef -Wcast-qual -Wwrite-strings +FLAVOR_FLAGS = -march=i386 -mregparm=3 -m32 -fno-exceptions -fno-rtti -fmerge-all-constants -fno-unroll-loops -fno-pie -fno-pic +OPTIMIZATION_FLAGS = -Oz -fno-asynchronous-unwind-tables +INCLUDE_FLAGS = -I.. -I. -I../LibC + +DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND + +CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(USERLAND_FLAGS) $(FLAVOR_FLAGS) $(ARCH_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES) +CXX = clang +LD = ld +AR = ar +LDFLAGS = -static --strip-debug -melf_i386 --build-id=none -z norelro -z now -e _start --gc-sections + +all: $(APP) + +$(APP): $(OBJS) + $(LD) -o $(APP) $(LDFLAGS) $(OBJS) ../LibGUI/LibGUI.a ../LibC/LibC.a + +.cpp.o: + @echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $< + +-include $(OBJS:%.o=%.d) + +clean: + @echo "CLEAN"; rm -f $(APPS) $(OBJS) *.d + diff --git a/Clock/main.cpp b/Clock/main.cpp new file mode 100644 index 0000000000..483d5cc88f --- /dev/null +++ b/Clock/main.cpp @@ -0,0 +1,21 @@ +#include <LibGUI/GEventLoop.h> +#include <LibGUI/GWindow.h> +#include "ClockWidget.h" + +int main(int, char**) +{ + GEventLoop loop; + + auto* window = new GWindow; + window->set_title("Clock"); + window->set_rect({ 100, 100, 100, 40 }); + + auto* clock_widget = new ClockWidget; + clock_widget->set_relative_rect({ 0, 0, 100, 40 }); + window->set_main_widget(clock_widget); + + window->show(); + return loop.exec(); +} + + |