summaryrefslogtreecommitdiff
path: root/Userland/guitest2.cpp
blob: c177049483549b7fd3886aae195265be8ad95990 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <assert.h>
#include <time.h>
#include <Kernel/Syscall.h>
#include <SharedGraphics/GraphicsBitmap.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GWindow.h>
#include <LibGUI/GWidget.h>
#include <LibGUI/GLabel.h>
#include <LibGUI/GButton.h>
#include <LibGUI/GEventLoop.h>
#include <LibGUI/GTextBox.h>
#include <LibGUI/GCheckBox.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;
    virtual void mousedown_event(GMouseEvent&) override;

    dword m_last_time { 0 };
};

ClockWidget::ClockWidget(GWidget* parent)
    : GWidget(parent)
{
    set_relative_rect({ 0, 0, 100, 40 });
    start_timer(250);
}

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

void ClockWidget::mousedown_event(GMouseEvent&)
{
    update();
}

static GWindow* make_font_test_window();
static GWindow* make_launcher_window();
static GWindow* make_clock_window();

int main(int argc, char** argv)
{
    GEventLoop loop;
#if 0
    auto* font_test_window = make_font_test_window();
    font_test_window->show();
#endif

    auto* launcher_window = make_launcher_window();
    launcher_window->show();

    auto* clock_window = make_clock_window();
    clock_window->show();

    return loop.exec();
}

GWindow* make_font_test_window()
{
    auto* window = new GWindow;
    window->set_title("Font test");
    window->set_rect({ 480, 100, 300, 80 });

    auto* widget = new GWidget;
    window->set_main_widget(widget);
    widget->set_relative_rect({ 0, 0, 300, 80 });

    auto* l1 = new GLabel(widget);
    l1->set_relative_rect({ 0, 0, 300, 20 });
    l1->set_text("0123456789");

    auto* l2 = new GLabel(widget);
    l2->set_relative_rect({ 0, 20, 300, 20 });
    l2->set_text("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

    auto* l3 = new GLabel(widget);
    l3->set_relative_rect({ 0, 40, 300, 20 });
    l3->set_text("abcdefghijklmnopqrstuvwxyz");

    auto* l4 = new GLabel(widget);
    l4->set_relative_rect({ 0, 60, 300, 20 });
    l4->set_text("!\"#$%&'()*+,-./:;<=>?@[\\]^_{|}~");

    return window;
}

GWindow* make_launcher_window()
{
    auto* window = new GWindow;
    window->set_title("Launcher");
    window->set_rect({ 100, 400, 100, 230 });

    auto* widget = new GWidget;
    window->set_main_widget(widget);
    widget->set_relative_rect({ 0, 0, 100, 230 });

    auto* label = new GLabel(widget);
    label->set_relative_rect({ 0, 0, 100, 20 });
    label->set_text("Apps");

    auto* terminal_button = new GButton(widget);
    terminal_button->set_relative_rect({ 5, 20, 90, 20 });
    terminal_button->set_caption("Terminal");

    terminal_button->on_click = [label] (GButton&) {
        pid_t child_pid = fork();
        if (!child_pid) {
            execve("/bin/Terminal", nullptr, nullptr);
            ASSERT_NOT_REACHED();
        } else {
            char buffer[32];
            sprintf(buffer, "PID: %d", child_pid);
            label->set_text(buffer);
        }
    };

    auto* guitest_button = new GButton(widget);
    guitest_button->set_relative_rect({ 5, 50, 90, 20 });
    guitest_button->set_caption("guitest");

    guitest_button->on_click = [label] (GButton&) {
        pid_t child_pid = fork();
        if (!child_pid) {
            execve("/bin/guitest", nullptr, nullptr);
            ASSERT_NOT_REACHED();
        } else {
            char buffer[32];
            sprintf(buffer, "PID: %d", child_pid);
            label->set_text(buffer);
        }
    };

    auto* dummy_button = new GButton(widget);
    dummy_button->set_relative_rect({ 5, 80, 90, 20 });
    dummy_button->set_caption("Dummy");

    auto* textbox = new GTextBox(widget);
    textbox->set_relative_rect({ 5, 110, 90, 20 });
    textbox->on_return_pressed = [window] (GTextBox& textbox) {
        window->set_title(textbox.text());
    };

    auto* other_textbox = new GTextBox(widget);
    other_textbox->set_relative_rect({ 5, 140, 90, 20 });

    auto* checkbox = new GCheckBox(widget);
    checkbox->set_relative_rect({ 5, 170, 90, 20 });
    checkbox->set_caption("CheckBox");

    window->set_focused_widget(textbox);

    auto* close_button = new GButton(widget);
    close_button->set_relative_rect({ 5, 200, 90, 20 });
    close_button->set_caption("Close");
    close_button->on_click = [window] (GButton&) {
        window->close();
    };

    return window;
}

GWindow* make_clock_window()
{
    auto* window = new GWindow;
    window->set_title("Clock");
    window->set_rect({ 900, 700, 100, 40 });

    auto* clock_widget = new ClockWidget;
    clock_widget->set_relative_rect({ 0, 0, 100, 40 });
    window->set_main_widget(clock_widget);

    return window;
}