summaryrefslogtreecommitdiff
path: root/Applications/Clock/ClockWidget.cpp
blob: 97001edbb5576faf92166c6a55fa3f082788ff63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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, TextAlignment::Center, Color::Black);
}

void ClockWidget::timer_event(GTimerEvent&)
{
    auto now = time(nullptr);
    if (now == m_last_time)
        return;
    m_last_time = now;
    update();
}