summaryrefslogtreecommitdiff
path: root/LibGUI
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-01-30 20:03:52 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-01-30 20:03:52 +0100
commit37ab7b7a8cd3a9f3987c6d8845f9728cdd07095f (patch)
tree23fff0d31366abbe2a04c2a849ef2d6321c6dfe7 /LibGUI
parent5c25f0c4db9584adacd8b0446fdc0d5524fe7b31 (diff)
downloadserenity-37ab7b7a8cd3a9f3987c6d8845f9728cdd07095f.zip
LibGUI: Implement destroying individual windows without exiting the process.
Diffstat (limited to 'LibGUI')
-rw-r--r--LibGUI/GObject.cpp2
-rw-r--r--LibGUI/GObject.h2
-rw-r--r--LibGUI/GWindow.cpp83
-rw-r--r--LibGUI/GWindow.h8
4 files changed, 67 insertions, 28 deletions
diff --git a/LibGUI/GObject.cpp b/LibGUI/GObject.cpp
index 13320cb265..881f220317 100644
--- a/LibGUI/GObject.cpp
+++ b/LibGUI/GObject.cpp
@@ -69,7 +69,7 @@ void GObject::stopTimer()
m_timerID = 0;
}
-void GObject::deleteLater()
+void GObject::delete_later()
{
GEventLoop::main().post_event(this, make<GEvent>(GEvent::DeferredDestroy));
}
diff --git a/LibGUI/GObject.h b/LibGUI/GObject.h
index 48870defcc..c4d2679101 100644
--- a/LibGUI/GObject.h
+++ b/LibGUI/GObject.h
@@ -27,7 +27,7 @@ public:
void addChild(GObject&);
void removeChild(GObject&);
- void deleteLater();
+ void delete_later();
private:
virtual void timerEvent(GTimerEvent&);
diff --git a/LibGUI/GWindow.cpp b/LibGUI/GWindow.cpp
index 9aff94b40a..6b2e8abeb7 100644
--- a/LibGUI/GWindow.cpp
+++ b/LibGUI/GWindow.cpp
@@ -29,10 +29,29 @@ GWindow* GWindow::from_window_id(int window_id)
GWindow::GWindow(GObject* parent)
: GObject(parent)
{
+ m_rect_when_windowless = { 100, 400, 140, 140 };
+ m_title_when_windowless = "GWindow";
+}
+
+GWindow::~GWindow()
+{
+ hide();
+}
+
+void GWindow::close()
+{
+ delete_later();
+}
+
+void GWindow::show()
+{
+ if (m_window_id)
+ return;
+
GUI_WindowParameters wparams;
- wparams.rect = { { 100, 400 }, { 140, 140 } };
+ wparams.rect = m_rect_when_windowless;
wparams.background_color = 0xffc0c0;
- strcpy(wparams.title, "GWindow");
+ strcpy(wparams.title, m_title_when_windowless.characters());
m_window_id = gui_create_window(&wparams);
if (m_window_id < 0) {
perror("gui_create_window");
@@ -40,33 +59,54 @@ GWindow::GWindow(GObject* parent)
}
windows().set(m_window_id, this);
+ update();
}
-GWindow::~GWindow()
+void GWindow::hide()
{
+ if (!m_window_id)
+ return;
+ windows().remove(m_window_id);
+ int rc = gui_destroy_window(m_window_id);
+ if (rc < 0) {
+ perror("gui_destroy_window");
+ exit(1);
+ }
}
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);
+ m_title_when_windowless = title;
+ if (m_window_id) {
+ int rc = gui_set_window_title(m_window_id, title.characters(), title.length());
+ if (rc < 0) {
+ perror("gui_set_window_title");
+ exit(1);
+ }
+ }
}
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);
+ if (m_window_id) {
+ char buffer[256];
+ int rc = gui_get_window_title(m_window_id, buffer, sizeof(buffer));
+ ASSERT(rc >= 0);
+ return String(buffer, rc);
+ }
+ return m_title_when_windowless;
}
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);
+ m_rect_when_windowless = a_rect;
+ if (m_window_id) {
+ GUI_Rect rect = a_rect;
+ int rc = gui_set_window_rect(m_window_id, &rect);
+ ASSERT(rc == 0);
+ }
}
void GWindow::event(GEvent& event)
@@ -99,9 +139,11 @@ void GWindow::event(GEvent& event)
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);
+ if (m_window_id) {
+ GUI_Rect gui_rect = rect;
+ int rc = gui_notify_paint_finished(m_window_id, &gui_rect);
+ ASSERT(rc == 0);
+ }
return;
}
@@ -126,16 +168,10 @@ bool GWindow::is_visible() const
return false;
}
-void GWindow::close()
-{
-}
-
-void GWindow::show()
-{
-}
-
void GWindow::update(const Rect& a_rect)
{
+ if (!m_window_id)
+ return;
GUI_Rect rect = a_rect;
int rc = gui_invalidate_window(m_window_id, a_rect.is_null() ? nullptr : &rect);
ASSERT(rc == 0);
@@ -168,6 +204,7 @@ void GWindow::set_focused_widget(GWidget* widget)
void GWindow::set_global_cursor_tracking_widget(GWidget* widget)
{
+ ASSERT(m_window_id);
if (widget == m_global_cursor_tracking_widget.ptr())
return;
m_global_cursor_tracking_widget = widget ? widget->makeWeakPtr() : nullptr;
diff --git a/LibGUI/GWindow.h b/LibGUI/GWindow.h
index 7a53e8a8db..dc2549ee9f 100644
--- a/LibGUI/GWindow.h
+++ b/LibGUI/GWindow.h
@@ -35,6 +35,8 @@ public:
bool is_visible() const;
bool is_active() const { return m_is_active; }
+ void show();
+ void hide();
void close();
GWidget* main_widget() { return m_main_widget; }
@@ -45,8 +47,6 @@ public:
const GWidget* focused_widget() const { return m_focused_widget; }
void set_focused_widget(GWidget*);
- void show();
-
void update(const Rect& = Rect());
void set_global_cursor_tracking_widget(GWidget*);
@@ -55,10 +55,12 @@ public:
private:
RetainPtr<GraphicsBitmap> m_backing;
- int m_window_id { -1 };
+ int m_window_id { 0 };
bool m_is_active { false };
GWidget* m_main_widget { nullptr };
GWidget* m_focused_widget { nullptr };
WeakPtr<GWidget> m_global_cursor_tracking_widget;
+ Rect m_rect_when_windowless;
+ String m_title_when_windowless;
};