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
|
#include "GWindow.h"
#include "GEvent.h"
#include "GEventLoop.h"
#include "GWidget.h"
#include <SharedGraphics/GraphicsBitmap.h>
#include <LibC/gui.h>
#include <LibC/stdio.h>
#include <LibC/stdlib.h>
#include <LibC/unistd.h>
#include <AK/HashMap.h>
static HashMap<int, GWindow*>* s_windows;
static HashMap<int, GWindow*>& windows()
{
if (!s_windows)
s_windows = new HashMap<int, GWindow*>;
return *s_windows;
}
GWindow* GWindow::from_window_id(int window_id)
{
auto it = windows().find(window_id);
if (it != windows().end())
return (*it).value;
return nullptr;
}
GWindow::GWindow(GObject* parent)
: GObject(parent)
{
GUI_WindowParameters wparams;
wparams.rect = { { 100, 400 }, { 140, 140 } };
wparams.background_color = 0xffc0c0;
strcpy(wparams.title, "GWindow");
m_window_id = gui_create_window(&wparams);
if (m_window_id < 0) {
perror("gui_create_window");
exit(1);
}
windows().set(m_window_id, this);
}
GWindow::~GWindow()
{
}
void GWindow::set_title(String&& title)
{
dbgprintf("GWindow::set_title \"%s\"\n", title.characters());
int rc = gui_set_window_title(m_window_id, title.characters(), title.length());
ASSERT(rc == 0);
}
String GWindow::title() const
{
char buffer[256];
int rc = gui_get_window_title(m_window_id, buffer, sizeof(buffer));
ASSERT(rc >= 0);
return String(buffer, rc);
}
void GWindow::set_rect(const Rect& a_rect)
{
dbgprintf("GWindow::set_rect! %d,%d %dx%d\n", a_rect.x(), a_rect.y(), a_rect.width(), a_rect.height());
GUI_Rect rect = a_rect;
int rc = gui_set_window_rect(m_window_id, &rect);
ASSERT(rc == 0);
}
void GWindow::event(GEvent& event)
{
if (event.is_mouse_event()) {
if (!m_main_widget)
return;
auto& mouse_event = static_cast<GMouseEvent&>(event);
if (m_main_widget) {
auto result = m_main_widget->hit_test(mouse_event.x(), mouse_event.y());
auto local_event = make<GMouseEvent>(event.type(), Point { result.localX, result.localY }, mouse_event.buttons(), mouse_event.button());
ASSERT(result.widget);
return result.widget->event(*local_event);
}
return;
}
if (event.is_paint_event()) {
if (!m_main_widget)
return;
auto& paint_event = static_cast<GPaintEvent&>(event);
auto rect = paint_event.rect();
if (rect.is_empty())
rect = m_main_widget->rect();
m_main_widget->event(*make<GPaintEvent>(rect));
GUI_Rect gui_rect = rect;
int rc = gui_notify_paint_finished(m_window_id, &gui_rect);
ASSERT(rc == 0);
return;
}
if (event.is_key_event()) {
if (!m_focused_widget)
return;
return m_focused_widget->event(event);
}
if (event.type() == GEvent::WindowBecameActive || event.type() == GEvent::WindowBecameInactive) {
m_is_active = event.type() == GEvent::WindowBecameActive;
if (m_focused_widget)
m_focused_widget->update();
return;
}
GObject::event(event);
}
bool GWindow::is_visible() const
{
return false;
}
void GWindow::close()
{
}
void GWindow::show()
{
}
void GWindow::update(const Rect& a_rect)
{
GUI_Rect rect = a_rect;
int rc = gui_invalidate_window(m_window_id, a_rect.is_null() ? nullptr : &rect);
ASSERT(rc == 0);
}
void GWindow::set_main_widget(GWidget* widget)
{
if (m_main_widget == widget)
return;
m_main_widget = widget;
if (widget)
widget->set_window(this);
update();
}
void GWindow::set_focused_widget(GWidget* widget)
{
if (m_focused_widget == widget)
return;
if (m_focused_widget) {
GEventLoop::main().post_event(m_focused_widget, make<GEvent>(GEvent::FocusOut));
m_focused_widget->update();
}
m_focused_widget = widget;
if (m_focused_widget) {
GEventLoop::main().post_event(m_focused_widget, make<GEvent>(GEvent::FocusIn));
m_focused_widget->update();
}
}
|