1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-22 20:45:08 +00:00
serenity/Clock/ClockWidget.cpp
Andreas Kling d0078b6574 Clock: Turns the clock window from guitest2 into a separate program.
We can't not have a desktop clock app. :^)
2019-02-05 09:44:13 +01:00

37 lines
798 B
C++

#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();
}