2019-01-20 03:49:48 +00:00
|
|
|
#include "GWindow.h"
|
|
|
|
#include "GEvent.h"
|
|
|
|
#include "GEventLoop.h"
|
2019-01-20 06:03:38 +00:00
|
|
|
#include "GWidget.h"
|
2019-01-20 03:49:48 +00:00
|
|
|
#include <SharedGraphics/GraphicsBitmap.h>
|
2019-01-20 04:48:43 +00:00
|
|
|
#include <LibC/gui.h>
|
|
|
|
#include <LibC/stdio.h>
|
|
|
|
#include <LibC/stdlib.h>
|
2019-01-22 15:34:24 +00:00
|
|
|
#include <LibC/unistd.h>
|
2019-01-20 04:48:43 +00:00
|
|
|
#include <AK/HashMap.h>
|
2019-01-20 03:49:48 +00:00
|
|
|
|
2019-01-20 04:48:43 +00:00
|
|
|
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)
|
2019-01-20 03:49:48 +00:00
|
|
|
{
|
2019-01-20 05:02:09 +00:00
|
|
|
GUI_WindowParameters wparams;
|
2019-01-20 04:48:43 +00:00
|
|
|
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);
|
2019-01-20 03:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GWindow::~GWindow()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWindow::set_title(String&& title)
|
|
|
|
{
|
2019-01-20 05:02:09 +00:00
|
|
|
dbgprintf("GWindow::set_title \"%s\"\n", title.characters());
|
|
|
|
GUI_WindowParameters params;
|
2019-01-24 22:40:12 +00:00
|
|
|
int rc = gui_set_window_title(m_window_id, title.characters(), title.length());
|
2019-01-20 05:02:09 +00:00
|
|
|
ASSERT(rc == 0);
|
2019-01-20 03:49:48 +00:00
|
|
|
}
|
2019-01-20 05:02:09 +00:00
|
|
|
|
2019-01-24 22:40:12 +00:00
|
|
|
void GWindow::set_rect(const Rect& a_rect)
|
2019-01-20 03:49:48 +00:00
|
|
|
{
|
2019-01-24 22:40:12 +00:00
|
|
|
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);
|
2019-01-20 05:02:09 +00:00
|
|
|
ASSERT(rc == 0);
|
2019-01-20 03:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GWindow::event(GEvent& event)
|
|
|
|
{
|
2019-01-20 06:03:38 +00:00
|
|
|
if (event.is_mouse_event()) {
|
|
|
|
if (!m_main_widget)
|
|
|
|
return;
|
|
|
|
auto& mouse_event = static_cast<GMouseEvent&>(event);
|
|
|
|
if (m_main_widget) {
|
2019-01-20 23:46:08 +00:00
|
|
|
auto result = m_main_widget->hit_test(mouse_event.x(), mouse_event.y());
|
2019-01-20 06:03:38 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.is_paint_event()) {
|
|
|
|
if (!m_main_widget)
|
|
|
|
return;
|
|
|
|
auto& paint_event = static_cast<GPaintEvent&>(event);
|
2019-01-20 06:56:48 +00:00
|
|
|
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_invalidate_window(m_window_id, &gui_rect);
|
2019-01-20 06:03:38 +00:00
|
|
|
ASSERT(rc == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GObject::event(event);
|
2019-01-20 03:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GWindow::is_visible() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWindow::close()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-20 04:48:43 +00:00
|
|
|
void GWindow::show()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWindow::update()
|
|
|
|
{
|
2019-01-20 06:03:38 +00:00
|
|
|
GEventLoop::main().post_event(this, make<GPaintEvent>());
|
2019-01-20 04:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GWindow::set_main_widget(GWidget* widget)
|
|
|
|
{
|
|
|
|
if (m_main_widget == widget)
|
|
|
|
return;
|
|
|
|
m_main_widget = widget;
|
2019-01-20 06:03:38 +00:00
|
|
|
if (widget)
|
|
|
|
widget->set_window(this);
|
2019-01-20 04:48:43 +00:00
|
|
|
update();
|
|
|
|
}
|