diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-01-19 23:49:56 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-01-19 23:49:56 +0100 |
commit | a026da47e7a7c6e072e068be6b66d3c76962cedf (patch) | |
tree | 472dd635d3772e73243745055d3c4a64ce90afd1 /Widgets | |
parent | 7e5b81fe4807f89868af61ab27b53f003d607167 (diff) | |
download | serenity-a026da47e7a7c6e072e068be6b66d3c76962cedf.zip |
Move Widget & friends into LibGUI.
Diffstat (limited to 'Widgets')
-rw-r--r-- | Widgets/.gitignore | 3 | ||||
-rw-r--r-- | Widgets/Button.cpp | 89 | ||||
-rw-r--r-- | Widgets/Button.h | 27 | ||||
-rw-r--r-- | Widgets/CheckBox.cpp | 102 | ||||
-rw-r--r-- | Widgets/CheckBox.h | 26 | ||||
-rw-r--r-- | Widgets/ClockWidget.cpp | 37 | ||||
-rw-r--r-- | Widgets/ClockWidget.h | 16 | ||||
-rw-r--r-- | Widgets/Event.h | 170 | ||||
-rw-r--r-- | Widgets/EventLoop.cpp | 95 | ||||
-rw-r--r-- | Widgets/EventLoop.h | 37 | ||||
-rw-r--r-- | Widgets/Label.cpp | 35 | ||||
-rw-r--r-- | Widgets/Label.h | 22 | ||||
-rw-r--r-- | Widgets/ListBox.cpp | 68 | ||||
-rw-r--r-- | Widgets/ListBox.h | 25 | ||||
-rw-r--r-- | Widgets/MsgBox.cpp | 61 | ||||
-rw-r--r-- | Widgets/MsgBox.h | 8 | ||||
-rw-r--r-- | Widgets/Object.cpp | 76 | ||||
-rw-r--r-- | Widgets/Object.h | 40 | ||||
-rw-r--r-- | Widgets/TerminalWidget.cpp | 175 | ||||
-rw-r--r-- | Widgets/TerminalWidget.h | 41 | ||||
-rw-r--r-- | Widgets/TextBox.cpp | 139 | ||||
-rw-r--r-- | Widgets/TextBox.h | 29 | ||||
-rw-r--r-- | Widgets/Widget.cpp | 164 | ||||
-rw-r--r-- | Widgets/Widget.h | 99 | ||||
-rw-r--r-- | Widgets/Window.cpp | 99 | ||||
-rw-r--r-- | Widgets/Window.h | 58 | ||||
-rw-r--r-- | Widgets/test.cpp | 100 |
27 files changed, 0 insertions, 1841 deletions
diff --git a/Widgets/.gitignore b/Widgets/.gitignore deleted file mode 100644 index 677b78a50b..0000000000 --- a/Widgets/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -*.o -*.swp -test diff --git a/Widgets/Button.cpp b/Widgets/Button.cpp deleted file mode 100644 index 7f6b7cfc89..0000000000 --- a/Widgets/Button.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include "Button.h" -#include "Painter.h" - -Button::Button(Widget* parent) - : Widget(parent) -{ - setFillWithBackgroundColor(false); -} - -Button::~Button() -{ -} - -void Button::setCaption(String&& caption) -{ - if (caption == m_caption) - return; - m_caption = move(caption); - update(); -} - -void Button::paintEvent(PaintEvent&) -{ - Color buttonColor = Color::LightGray; - Color highlightColor = Color::White; - Color shadowColor = Color(96, 96, 96); - - Painter painter(*this); - - painter.draw_line({ 1, 0 }, { width() - 2, 0 }, Color::Black); - painter.draw_line({ 1, height() - 1 }, { width() - 2, height() - 1}, Color::Black); - painter.draw_line({ 0, 1 }, { 0, height() - 2 }, Color::Black); - painter.draw_line({ width() - 1, 1 }, { width() - 1, height() - 2 }, Color::Black); - - if (m_beingPressed) { - // Base - painter.fill_rect({ 1, 1, width() - 2, height() - 2 }, buttonColor); - - // Sunken shadow - painter.draw_line({ 1, 1 }, { width() - 2, 1 }, shadowColor); - painter.draw_line({ 1, 2 }, {1, height() - 2 }, shadowColor); - } else { - // Base - painter.fill_rect({ 3, 3, width() - 5, height() - 5 }, buttonColor); - - // White highlight - painter.draw_line({ 1, 1 }, { width() - 2, 1 }, highlightColor); - painter.draw_line({ 1, 2 }, { width() - 3, 2 }, highlightColor); - painter.draw_line({ 1, 3 }, { 1, height() - 2 }, highlightColor); - painter.draw_line({ 2, 3 }, { 2, height() - 3 }, highlightColor); - - // Gray shadow - painter.draw_line({ width() - 2, 1 }, { width() - 2, height() - 4 }, shadowColor); - painter.draw_line({ width() - 3, 2 }, { width() - 3, height() - 4 }, shadowColor); - painter.draw_line({ 1, height() - 2 }, { width() - 2, height() - 2 }, shadowColor); - painter.draw_line({ 2, height() - 3 }, { width() - 2, height() - 3 }, shadowColor); - } - - if (!caption().is_empty()) { - auto textRect = rect(); - if (m_beingPressed) - textRect.move_by(1, 1); - painter.draw_text(textRect, caption(), Painter::TextAlignment::Center, Color::Black); - } -} - -void Button::mouseDownEvent(MouseEvent& event) -{ - printf("Button::mouseDownEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button()); - - m_beingPressed = true; - - update(); - Widget::mouseDownEvent(event); -} - -void Button::mouseUpEvent(MouseEvent& event) -{ - printf("Button::mouseUpEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button()); - - m_beingPressed = false; - - update(); - Widget::mouseUpEvent(event); - - if (onClick) - onClick(*this); -} - diff --git a/Widgets/Button.h b/Widgets/Button.h deleted file mode 100644 index cdbc522ecf..0000000000 --- a/Widgets/Button.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include "Widget.h" -#include <AK/AKString.h> -#include <AK/Function.h> - -class Button final : public Widget { -public: - explicit Button(Widget* parent); - virtual ~Button() override; - - String caption() const { return m_caption; } - void setCaption(String&&); - - Function<void(Button&)> onClick; - -private: - virtual void paintEvent(PaintEvent&) override; - virtual void mouseDownEvent(MouseEvent&) override; - virtual void mouseUpEvent(MouseEvent&) override; - - virtual const char* class_name() const override { return "Button"; } - - String m_caption; - bool m_beingPressed { false }; -}; - diff --git a/Widgets/CheckBox.cpp b/Widgets/CheckBox.cpp deleted file mode 100644 index 2340d382bb..0000000000 --- a/Widgets/CheckBox.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include "CheckBox.h" -#include "Painter.h" -#include "CharacterBitmap.h" - -CheckBox::CheckBox(Widget* parent) - : Widget(parent) -{ -} - -CheckBox::~CheckBox() -{ -} - -void CheckBox::setCaption(String&& caption) -{ - if (caption == m_caption) - return; - m_caption = move(caption); - update(); -} - -void CheckBox::setIsChecked(bool b) -{ - if (m_isChecked == b) - return; - m_isChecked = b; - update(); -} - -static const char* uncheckedBitmap = { - "###########" - "# #" - "# #" - "# #" - "# #" - "# #" - "# #" - "# #" - "# #" - "# #" - "###########" -}; - -#if 0 -static const char* checkedBitmap = { - "############" - "# #" - "# ## #" - "# ## #" - "# ## #" - "# ## #" - "# ## #" - "# ## ## #" - "# ## ## #" - "# ### #" - "# #" - "############" -}; -#endif - -static const char* checkedBitmap = { - "###########" - "## ##" - "# # # #" - "# # # #" - "# # # #" - "# # #" - "# # # #" - "# # # #" - "# # # #" - "## ##" - "###########" -}; - -void CheckBox::paintEvent(PaintEvent&) -{ - Painter painter(*this); - auto bitmap = CharacterBitmap::create_from_ascii(isChecked() ? checkedBitmap : uncheckedBitmap, 11, 11); - - auto textRect = rect(); - textRect.set_left(bitmap->width() + 4); - textRect.set_top(height() / 2 - font().glyph_height() / 2); - - Point bitmapPosition; - bitmapPosition.set_x(2); - bitmapPosition.set_y(height() / 2 - bitmap->height() / 2 - 1); - - painter.fill_rect(rect(), backgroundColor()); - painter.draw_bitmap(bitmapPosition, *bitmap, foregroundColor()); - - if (!caption().is_empty()) { - painter.draw_text(textRect, caption(), Painter::TextAlignment::TopLeft, foregroundColor()); - } -} - -void CheckBox::mouseDownEvent(MouseEvent& event) -{ - printf("CheckBox::mouseDownEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button()); - - setIsChecked(!isChecked()); -} - diff --git a/Widgets/CheckBox.h b/Widgets/CheckBox.h deleted file mode 100644 index 638b5fb629..0000000000 --- a/Widgets/CheckBox.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include "Widget.h" -#include <AK/AKString.h> - -class CheckBox final : public Widget { -public: - explicit CheckBox(Widget* parent); - virtual ~CheckBox() override; - - String caption() const { return m_caption; } - void setCaption(String&&); - - bool isChecked() const { return m_isChecked; } - void setIsChecked(bool); - -private: - virtual void paintEvent(PaintEvent&) override; - virtual void mouseDownEvent(MouseEvent&) override; - - virtual const char* class_name() const override { return "CheckBox"; } - - String m_caption; - bool m_isChecked { false }; -}; - diff --git a/Widgets/ClockWidget.cpp b/Widgets/ClockWidget.cpp deleted file mode 100644 index c1f800bd79..0000000000 --- a/Widgets/ClockWidget.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "ClockWidget.h" -#include "Painter.h" -#include <time.h> - -ClockWidget::ClockWidget(Widget* parent) - : Widget(parent) -{ - setWindowRelativeRect({ 0, 0, 100, 40 }); - startTimer(250); -} - -ClockWidget::~ClockWidget() -{ -} - -void ClockWidget::paintEvent(PaintEvent&) -{ - 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::MidGray); - painter.draw_text(rect(), timeBuf, Painter::TextAlignment::Center, Color::Black); -} - -void ClockWidget::timerEvent(TimerEvent&) -{ - auto now = time(nullptr); - if (now == m_lastSeenTimestamp) - return; - m_lastSeenTimestamp = now; - update(); -} - diff --git a/Widgets/ClockWidget.h b/Widgets/ClockWidget.h deleted file mode 100644 index a7326984ec..0000000000 --- a/Widgets/ClockWidget.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include "Widget.h" - -class ClockWidget final : public Widget { -public: - explicit ClockWidget(Widget* parent = nullptr); - virtual ~ClockWidget() override; - -private: - virtual void paintEvent(PaintEvent&) override; - virtual void timerEvent(TimerEvent&) override; - - dword m_lastSeenTimestamp { 0 }; -}; - diff --git a/Widgets/Event.h b/Widgets/Event.h deleted file mode 100644 index 1314a4dd61..0000000000 --- a/Widgets/Event.h +++ /dev/null @@ -1,170 +0,0 @@ -#pragma once - -#include "Point.h" -#include "Rect.h" -#include <AK/AKString.h> -#include <AK/Types.h> - -static const char* eventNames[] = { - "Invalid", - "Quit", - "Show", - "Hide", - "Paint", - "MouseMove", - "MouseDown", - "MouseUp", - "KeyDown", - "KeyUp", - "Timer", - "DeferredDestroy", -}; - -class Event { -public: - enum Type { - Invalid = 0, - Quit, - Show, - Hide, - Paint, - MouseMove, - MouseDown, - MouseUp, - KeyDown, - KeyUp, - Timer, - DeferredDestroy, - WindowBecameInactive, - WindowBecameActive, - WM_Compose, - }; - - Event() { } - explicit Event(Type type) : m_type(type) { } - virtual ~Event() { } - - Type type() const { return m_type; } - - const char* name() const { return eventNames[(unsigned)m_type]; } - - bool isMouseEvent() const { return m_type == MouseMove || m_type == MouseDown || m_type == MouseUp; } - bool isKeyEvent() const { return m_type == KeyUp || m_type == KeyDown; } - bool isPaintEvent() const { return m_type == Paint; } - -private: - Type m_type { Invalid }; -}; - -class DeferredDestroyEvent final : public Event { -public: - DeferredDestroyEvent() - : Event(Event::DeferredDestroy) - { - } -}; - -class QuitEvent final : public Event { -public: - QuitEvent() - : Event(Event::Quit) - { - } -}; - -class PaintEvent final : public Event { -public: - explicit PaintEvent(const Rect& rect = Rect()) - : Event(Event::Paint) - , m_rect(rect) - { - } - - const Rect& rect() const { return m_rect; } -private: - friend class WindowManager; - Rect m_rect; -}; - -class ShowEvent final : public Event { -public: - ShowEvent() - : Event(Event::Show) - { - } -}; - -class HideEvent final : public Event { -public: - HideEvent() - : Event(Event::Hide) - { - } -}; - -enum class MouseButton : byte { - None = 0, - Left, - Middle, - Right, -}; - -enum KeyboardKey { - Invalid, - LeftArrow, - RightArrow, - UpArrow, - DownArrow, - Backspace, - Return, -}; - -class KeyEvent final : public Event { -public: - KeyEvent(Type type, int key) - : Event(type) - , m_key(key) - { - } - - int key() const { return m_key; } - bool ctrl() const { return m_ctrl; } - bool alt() const { return m_alt; } - bool shift() const { return m_shift; } - String text() const { return m_text; } - -private: - friend class EventLoop; - friend class AbstractScreen; - int m_key { 0 }; - bool m_ctrl { false }; - bool m_alt { false }; - bool m_shift { false }; - String m_text; -}; - -class MouseEvent final : public Event { -public: - MouseEvent(Type type, int x, int y, MouseButton button = MouseButton::None) - : Event(type) - , m_position(x, y) - , m_button(button) - { - } - - Point position() const { return m_position; } - int x() const { return m_position.x(); } - int y() const { return m_position.y(); } - MouseButton button() const { return m_button; } - -private: - Point m_position; - MouseButton m_button { MouseButton::None }; -}; - -class TimerEvent final : public Event { -public: - TimerEvent() : Event(Event::Timer) { } - ~TimerEvent() { } -}; - diff --git a/Widgets/EventLoop.cpp b/Widgets/EventLoop.cpp deleted file mode 100644 index cbc9c9b0bc..0000000000 --- a/Widgets/EventLoop.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#include "EventLoop.h" -#include "Event.h" -#include "Object.h" -#include "WindowManager.h" -#include "AbstractScreen.h" -#include "PS2MouseDevice.h" -#include "Scheduler.h" - -static EventLoop* s_mainEventLoop; - -void EventLoop::initialize() -{ - s_mainEventLoop = nullptr; -} - -EventLoop::EventLoop() -{ - if (!s_mainEventLoop) - s_mainEventLoop = this; -} - -EventLoop::~EventLoop() -{ -} - -EventLoop& EventLoop::main() -{ - ASSERT(s_mainEventLoop); - return *s_mainEventLoop; -} - -int EventLoop::exec() -{ - m_server_process = current; - m_running = true; - for (;;) { - if (m_queuedEvents.is_empty()) - waitForEvent(); - Vector<QueuedEvent> events; - { - InterruptDisabler disabler; - events = move(m_queuedEvents); - } - for (auto& queuedEvent : events) { - auto* receiver = queuedEvent.receiver; - auto& event = *queuedEvent.event; - //printf("EventLoop: Object{%p} event %u (%s)\n", receiver, (unsigned)event.type(), event.name()); - if (!receiver) { - switch (event.type()) { - case Event::Quit: - ASSERT_NOT_REACHED(); - return 0; - default: - printf("event type %u with no receiver :(\n", event.type()); - ASSERT_NOT_REACHED(); - return 1; - } - } else { - receiver->event(event); - } - } - } -} - -void EventLoop::postEvent(Object* receiver, OwnPtr<Event>&& event) -{ - //printf("EventLoop::postEvent: {%u} << receiver=%p, event=%p\n", m_queuedEvents.size(), receiver, event.ptr()); - m_queuedEvents.append({ receiver, move(event) }); -} - -void EventLoop::waitForEvent() -{ - auto& mouse = PS2MouseDevice::the(); - auto& screen = AbstractScreen::the(); - bool prev_left_button = screen.left_mouse_button_pressed(); - bool prev_right_button = screen.right_mouse_button_pressed(); - int dx = 0; - int dy = 0; - while (mouse.can_read(*m_server_process)) { - signed_byte data[3]; - ssize_t nread = mouse.read(*m_server_process, (byte*)data, 3); - ASSERT(nread == 3); - bool left_button = data[0] & 1; - bool right_button = data[0] & 2; - dx += data[1]; - dy += -data[2]; - if (left_button != prev_left_button || right_button != prev_right_button || !mouse.can_read(*m_server_process)) { - prev_left_button = left_button; - prev_right_button = right_button; - screen.on_receive_mouse_data(dx, dy, left_button, right_button); - dx = 0; - dy = 0; - } - } -} diff --git a/Widgets/EventLoop.h b/Widgets/EventLoop.h deleted file mode 100644 index 289a6f2f02..0000000000 --- a/Widgets/EventLoop.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include "Event.h" -#include <AK/OwnPtr.h> -#include <AK/Vector.h> - -class Object; -class Process; - -class EventLoop { -public: - EventLoop(); - ~EventLoop(); - - int exec(); - - void postEvent(Object* receiver, OwnPtr<Event>&&); - - static EventLoop& main(); - - static void initialize(); - - bool running() const { return m_running; } - Process& server_process() { return *m_server_process; } - -private: - void waitForEvent(); - - struct QueuedEvent { - Object* receiver { nullptr }; - OwnPtr<Event> event; - }; - Vector<QueuedEvent> m_queuedEvents; - - Process* m_server_process { nullptr }; - bool m_running { false }; -}; diff --git a/Widgets/Label.cpp b/Widgets/Label.cpp deleted file mode 100644 index 3516514dd6..0000000000 --- a/Widgets/Label.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "Label.h" -#include "Painter.h" - -Label::Label(Widget* parent) - : Widget(parent) -{ -} - -Label::~Label() -{ -} - -void Label::setText(String&& text) -{ - if (text == m_text) - return; - m_text = move(text); - update(); -} - -void Label::paintEvent(PaintEvent&) -{ - Painter painter(*this); - if (fillWithBackgroundColor()) - painter.fill_rect({ 0, 0, width(), height() }, backgroundColor()); - if (!text().is_empty()) - painter.draw_text({ 4, 4, width(), height() }, text(), Painter::TextAlignment::TopLeft, foregroundColor()); -} - -void Label::mouseMoveEvent(MouseEvent& event) -{ - printf("Label::mouseMoveEvent: x=%d, y=%d\n", event.x(), event.y()); - Widget::mouseMoveEvent(event); -} - diff --git a/Widgets/Label.h b/Widgets/Label.h deleted file mode 100644 index 062c14a99a..0000000000 --- a/Widgets/Label.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "Widget.h" -#include <AK/AKString.h> - -class Label final : public Widget { -public: - explicit Label(Widget* parent); - virtual ~Label() override; - - String text() const { return m_text; } - void setText(String&&); - -private: - virtual void paintEvent(PaintEvent&) override; - virtual void mouseMoveEvent(MouseEvent&) override; - - virtual const char* class_name() const override { return "Label"; } - - String m_text; -}; - diff --git a/Widgets/ListBox.cpp b/Widgets/ListBox.cpp deleted file mode 100644 index 1a0525252c..0000000000 --- a/Widgets/ListBox.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include "ListBox.h" -#include "Painter.h" -#include "Font.h" -#include "Window.h" - -ListBox::ListBox(Widget* parent) - : Widget(parent) -{ -} - -ListBox::~ListBox() -{ -} - -Rect ListBox::item_rect(int index) const -{ - int item_height = font().glyph_height() + 2; - return Rect { 2, 2 + (index * item_height), width() - 4, item_height }; -} - -void ListBox::paintEvent(PaintEvent&) -{ - Painter painter(*this); - - // FIXME: Reduce overdraw. - painter.fill_rect(rect(), Color::White); - painter.draw_rect(rect(), Color::Black); - - if (isFocused()) - painter.draw_focus_rect(rect()); - - for (int i = m_scrollOffset; i < static_cast<int>(m_items.size()); ++i) { - auto itemRect = item_rect(i); - Rect textRect(itemRect.x() + 1, itemRect.y() + 1, itemRect.width() - 2, itemRect.height() - 2); - - Color itemTextColor = foregroundColor(); - if (m_selectedIndex == i) { - if (isFocused()) - painter.fill_rect(itemRect, Color(0, 32, 128)); - else - painter.fill_rect(itemRect, Color(96, 96, 96)); - itemTextColor = Color::White; - } - painter.draw_text(textRect, m_items[i], Painter::TextAlignment::TopLeft, itemTextColor); - } -} - -void ListBox::mouseDownEvent(MouseEvent& event) -{ - printf("ListBox::mouseDownEvent %d,%d\n", event.x(), event.y()); - for (int i = m_scrollOffset; i < static_cast<int>(m_items.size()); ++i) { - auto itemRect = item_rect(i); - if (itemRect.contains(event.position())) { - m_selectedIndex = i; - printf("ListBox: selected item %u (\"%s\")\n", i, m_items[i].characters()); - update(); - return; - } - } -} - -void ListBox::addItem(String&& item) -{ - m_items.append(move(item)); - if (m_selectedIndex == -1) - m_selectedIndex = 0; -} - diff --git a/Widgets/ListBox.h b/Widgets/ListBox.h deleted file mode 100644 index ca227f2b54..0000000000 --- a/Widgets/ListBox.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include "Widget.h" - -class ListBox final : public Widget { -public: - explicit ListBox(Widget* parent); - virtual ~ListBox() override; - - void addItem(String&&); - int selectedIndex() const { return m_selectedIndex; } - -private: - virtual void paintEvent(PaintEvent&) override; - virtual void mouseDownEvent(MouseEvent&) override; - virtual const char* class_name() const override { return "ListBox"; } - - Rect item_rect(int index) const; - - int m_scrollOffset { 0 }; - int m_selectedIndex { -1 }; - - Vector<String> m_items; -}; - diff --git a/Widgets/MsgBox.cpp b/Widgets/MsgBox.cpp deleted file mode 100644 index c6c8ce0c84..0000000000 --- a/Widgets/MsgBox.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include "MsgBox.h" -#include "Font.h" -#include "AbstractScreen.h" -#include "Window.h" -#include "Label.h" -#include "Button.h" -#include "Process.h" - -#if 0 -void MsgBox(Window* owner, String&& text) -{ - Font& font = Font::defaultFont(); - auto screenRect = AbstractScreen::the().rect(); - - int textWidth = text.length() * font.glyphWidth() + 8; - int textHeight = font.glyphHeight() + 8; - int horizontalPadding = 16; - int verticalPadding = 16; - int buttonWidth = 60; - int buttonHeight = 20; - int windowWidth = textWidth + horizontalPadding * 2; - int windowHeight = textHeight + buttonHeight + verticalPadding * 3; - - Rect windowRect( - screenRect.center().x() - windowWidth / 2, - screenRect.center().y() - windowHeight / 2, - windowWidth, - windowHeight - ); - - Rect buttonRect( - windowWidth / 2 - buttonWidth / 2, - windowHeight - verticalPadding - buttonHeight, - buttonWidth, - buttonHeight - ); - - auto* window = new Window(*current, current->make_window_id()); - window->setTitle("MsgBox"); - window->setRect(windowRect); - auto* widget = new Widget; - widget->setWindowRelativeRect({ 0, 0, windowWidth, windowHeight }); - widget->setFillWithBackgroundColor(true); - window->setMainWidget(widget); - auto* label = new Label(widget); - label->setWindowRelativeRect({ - horizontalPadding, - verticalPadding, - textWidth, - textHeight - }); - label->setText(move(text)); - auto* button = new Button(widget); - button->setCaption("OK"); - button->setWindowRelativeRect(buttonRect); - button->onClick = [] (Button& button) { - printf("MsgBox button pressed, closing MsgBox :)\n"); - button.window()->close(); - }; -} -#endif diff --git a/Widgets/MsgBox.h b/Widgets/MsgBox.h deleted file mode 100644 index 9db0766b41..0000000000 --- a/Widgets/MsgBox.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#include <AK/AKString.h> - -class Window; - -void MsgBox(Window* owner, String&&); - diff --git a/Widgets/Object.cpp b/Widgets/Object.cpp deleted file mode 100644 index f2456d1c11..0000000000 --- a/Widgets/Object.cpp +++ /dev/null @@ -1,76 +0,0 @@ -#include "Object.h" -#include "Event.h" -#include "EventLoop.h" -#include <AK/Assertions.h> - -Object::Object(Object* parent) - : m_parent(parent) -{ - if (m_parent) - m_parent->addChild(*this); -} - -Object::~Object() -{ - if (m_parent) - m_parent->removeChild(*this); - auto childrenToDelete = move(m_children); - for (auto* child : childrenToDelete) - delete child; -} - -void Object::event(Event& event) -{ - switch (event.type()) { - case Event::Timer: - return timerEvent(static_cast<TimerEvent&>(event)); - case Event::DeferredDestroy: - delete this; - break; - case Event::Invalid: - ASSERT_NOT_REACHED(); - break; - default: - break; - } -} - -void Object::addChild(Object& object) -{ - m_children.append(&object); -} - -void Object::removeChild(Object& object) -{ - for (unsigned i = 0; i < m_children.size(); ++i) { - if (m_children[i] == &object) { - m_children.remove(i); - return; - } - } -} - -void Object::timerEvent(TimerEvent&) -{ -} - -void Object::startTimer(int ms) -{ - if (m_timerID) { - printf("Object{%p} already has a timer!\n", this); - ASSERT_NOT_REACHED(); - } -} - -void Object::stopTimer() -{ - if (!m_timerID) - return; - m_timerID = 0; -} - -void Object::deleteLater() -{ - EventLoop::main().postEvent(this, make<DeferredDestroyEvent>()); -} - diff --git a/Widgets/Object.h b/Widgets/Object.h deleted file mode 100644 index 4e4f8eb7cc..0000000000 --- a/Widgets/Object.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include <AK/Vector.h> -#include <AK/Weakable.h> - -class Event; -class TimerEvent; - -class Object : public Weakable<Object> { -public: - Object(Object* parent = nullptr); - virtual ~Object(); - - virtual const char* class_name() const { return "Object"; } - - virtual void event(Event&); - - Vector<Object*>& children() { return m_children; } - - Object* parent() { return m_parent; } - const Object* parent() const { return m_parent; } - - void startTimer(int ms); - void stopTimer(); - bool hasTimer() const { return m_timerID; } - - void addChild(Object&); - void removeChild(Object&); - - void deleteLater(); - -private: - virtual void timerEvent(TimerEvent&); - - Object* m_parent { nullptr }; - - int m_timerID { 0 }; - - Vector<Object*> m_children; -}; diff --git a/Widgets/TerminalWidget.cpp b/Widgets/TerminalWidget.cpp deleted file mode 100644 index efa3b2abb6..0000000000 --- a/Widgets/TerminalWidget.cpp +++ /dev/null @@ -1,175 +0,0 @@ -#include "TerminalWidget.h" -#include "Font.h" -#include "Painter.h" -#include <unistd.h> -#include <signal.h> -#include <sys/ioctl.h> -#include <fcntl.h> - -extern int g_fd; -TerminalWidget* g_tw; - -TerminalWidget::TerminalWidget(Widget* parent) - : Widget(parent) -{ - g_tw = this; - - setWindowRelativeRect({ 0, 0, int(columns() * font().glyph_width()) + 4, int(rows() * font().glyph_height()) + 4 }); - - printf("rekt: %d x %d\n", width(), height()); - m_screen = new CharacterWithAttributes[rows() * columns()]; - for (unsigned row = 0; row < m_rows; ++row) { - for (unsigned column = 0; column < m_columns; ++column) { - at(row, column).character = ' '; - at(row, column).attribute = 0x07; - } - } - -#if __APPLE__ - g_fd = posix_openpt(O_RDWR); -#else - g_fd = getpt(); -#endif - - grantpt(g_fd); - unlockpt(g_fd); - char buf[1024]; - ptsname_r(g_fd, buf, sizeof(buf)); - - if (fork() == 0) { - close(g_fd); - setsid(); - int fd = open(buf, O_RDWR); - dup2(fd, 0); - dup2(fd, 1); - dup2(fd, 2); - signal(SIGWINCH, SIG_IGN); - ioctl(fd, TIOCSCTTY); - execl("/bin/bash", "bash", nullptr); - ASSERT_NOT_REACHED(); - } - - signal(SIGCHLD, SIG_IGN); -} - -TerminalWidget::~TerminalWidget() -{ -} - -CharacterWithAttributes& TerminalWidget::at(unsigned row, unsigned column) -{ - ASSERT(m_screen); - ASSERT(row < m_rows); - ASSERT(column < m_columns); - return m_screen[row * columns() + column]; -} - -void TerminalWidget::paintEvent(PaintEvent&) -{ - Painter painter(*this); - painter.fill_rect(rect(), Color::Black); - - char buf[2] = { 0, 0 }; - for (unsigned row = 0; row < m_rows; ++row) { - int y = row * font().glyph_height(); - for (unsigned column = 0; column < m_columns; ++column) { - int x = column * font().glyph_width(); - buf[0] = at(row, column).character; - painter.draw_text({ x + 2, y + 2, width(), font().glyph_height() }, buf, Painter::TextAlignment::TopLeft, Color(0xa0, 0xa0, 0xa0)); - } - } - - if (m_belling) - painter.draw_rect(rect(), Color::Red); -} - -void TerminalWidget::onReceive(const ByteBuffer& buffer) -{ - for (unsigned i = 0; i < buffer.size(); ++i) { - onReceive(buffer[i]); - } -} - -void TerminalWidget::onReceive(byte ch) -{ - //printf("receive %02x\n", ch); - auto scrollScreen = [&] () { - memmove(m_screen, m_screen + columns(), (m_rows - 1) * columns() * sizeof(CharacterWithAttributes)); - memset(m_screen + (m_rows - 1) * columns(), ' ', columns() * sizeof(CharacterWithAttributes)); - }; - - auto addChar = [&] (byte ch) { - at(m_cursorRow, m_cursorColumn).character = ch; - if (++m_cursorColumn >= m_columns) { - m_cursorColumn = 0; - if (m_cursorRow < (m_rows - 1)) { - ++m_cursorRow; - } else { - scrollScreen(); - } - } - }; - - switch (ch) { - case '\n': - if (m_cursorRow < (m_rows - 1)) { - ++m_cursorRow; - } else { - scrollScreen(); - } - break; - case '\r': - m_cursorColumn = 0; - break; - case '\t': - // FIXME: Respect terminal tab stops. - while ((m_cursorColumn % 8) != 0 && m_cursorColumn < m_columns) { - addChar(' '); - break; - case '\a': - bell(); - break; - case 8: - if (m_cursorColumn > 0) { - --m_cursorColumn; - at(m_cursorRow, m_cursorColumn).character = ' '; - } - break; - case 27: - printf("TerminalWidget: got escape!\n"); - break; - default: - addChar(ch); - break; - } - } - update(); -} - -void TerminalWidget::keyDownEvent(KeyEvent& event) -{ - if (event.text().is_empty()) - return; - write(g_fd, event.text().characters(), event.text().length()); -} - -void TerminalWidget::keyUpEvent(KeyEvent& event) -{ - return Widget::keyUpEvent(event); -} - -void TerminalWidget::bell() -{ - if (m_belling) - stopTimer(); - startTimer(250); - m_belling = true; - update(); -} - -void TerminalWidget::timerEvent(TimerEvent&) -{ - m_belling = false; - stopTimer(); - update(); -} diff --git a/Widgets/TerminalWidget.h b/Widgets/TerminalWidget.h deleted file mode 100644 index 78b9415475..0000000000 --- a/Widgets/TerminalWidget.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include "Widget.h" -#include <AK/ByteBuffer.h> - -struct CharacterWithAttributes { - byte character; - byte attribute; -}; - -class TerminalWidget final : public Widget { -public: - explicit TerminalWidget(Widget* parent); - virtual ~TerminalWidget() override; - - unsigned rows() const { return m_rows; } - unsigned columns() const { return m_columns; } - - void onReceive(const ByteBuffer&); - void onReceive(byte); - -private: - CharacterWithAttributes& at(unsigned row, unsigned column); - - virtual void paintEvent(PaintEvent&) override; - virtual void keyDownEvent(KeyEvent&) override; - virtual void keyUpEvent(KeyEvent&) override; - virtual void timerEvent(TimerEvent&) override; - - void bell(); - - unsigned m_columns { 80 }; - unsigned m_rows { 25 }; - - unsigned m_cursorRow { 0 }; - unsigned m_cursorColumn { 0 }; - - CharacterWithAttributes* m_screen { nullptr }; - - bool m_belling { false }; -}; diff --git a/Widgets/TextBox.cpp b/Widgets/TextBox.cpp deleted file mode 100644 index 052bd423c6..0000000000 --- a/Widgets/TextBox.cpp +++ /dev/null @@ -1,139 +0,0 @@ -#include "TextBox.h" -#include "Painter.h" -#include "Font.h" -#include "CharacterBitmap.h" -#include <AK/StdLibExtras.h> - -TextBox::TextBox(Widget* parent) - : Widget(parent) -{ - startTimer(500); -} - -TextBox::~TextBox() -{ -} - -void TextBox::setText(String&& text) -{ - m_text = move(text); - m_cursorPosition = m_text.length(); - update(); -} - -void TextBox::paintEvent(PaintEvent&) -{ - Painter painter(*this); - - // FIXME: Reduce overdraw. - painter.fill_rect(rect(), backgroundColor()); - painter.draw_rect(rect(), foregroundColor()); - - if (isFocused()) - painter.draw_focus_rect(rect()); - - Rect innerRect = rect(); - innerRect.shrink(6, 6); - - size_t maxCharsToPaint = innerRect.width() / font().glyph_width(); - - int firstVisibleChar = max((int)m_cursorPosition - (int)maxCharsToPaint, 0); - size_t charsToPaint = min(m_text.length() - firstVisibleChar, maxCharsToPaint); - - int y = innerRect.center().y() - font().glyph_height() / 2; - for (size_t i = 0; i < charsToPaint; ++i) { - char ch = m_text[firstVisibleChar + i]; - if (ch == ' ') - continue; - int x = innerRect.x() + (i * font().glyph_width()); - auto* bitmap = font().glyph_bitmap(ch); - if (!bitmap) { - printf("TextBox: glyph missing: %02x\n", ch); - ASSERT_NOT_REACHED(); - } - painter.draw_bitmap({x, y}, *bitmap, Color::Black); - } - - if (isFocused() && m_cursorBlinkState) { - unsigned visibleCursorPosition = m_cursorPosition - firstVisibleChar; - Rect cursorRect(innerRect.x() + visibleCursorPosition * font().glyph_width(), innerRect.y(), 1, innerRect.height()); - painter.fill_rect(cursorRect, foregroundColor()); - } -} - -void TextBox::mouseDownEvent(MouseEvent&) -{ -} - -void TextBox::handleBackspace() -{ - if (m_cursorPosition == 0) - return; - - if (m_text.length() == 1) { - m_text = String::empty(); - m_cursorPosition = 0; - update(); - return; - } - - char* buffer; - auto newText = StringImpl::create_uninitialized(m_text.length() - 1, buffer); - - memcpy(buffer, m_text.characters(), m_cursorPosition - 1); - memcpy(buffer + m_cursorPosition - 1, m_text.characters() + m_cursorPosition, m_text.length() - (m_cursorPosition - 1)); - - m_text = move(newText); - --m_cursorPosition; - update(); -} - -void TextBox::keyDownEvent(KeyEvent& event) -{ - switch (event.key()) { - case KeyboardKey::LeftArrow: - if (m_cursorPosition) - --m_cursorPosition; - m_cursorBlinkState = true; - update(); - return; - case KeyboardKey::RightArrow: - if (m_cursorPosition < m_text.length()) - ++m_cursorPosition; - m_cursorBlinkState = true; - update(); - return; - case KeyboardKey::Backspace: - return handleBackspace(); - case KeyboardKey::Return: - if (onReturnPressed) - onReturnPressed(*this); - return; - } - - if (!event.text().is_empty()) { - ASSERT(event.text().length() == 1); - - char* buffer; - auto newText = StringImpl::create_uninitialized(m_text.length() + 1, buffer); - - memcpy(buffer, m_text.characters(), m_cursorPosition); - buffer[m_cursorPosition] = event.text()[0]; - memcpy(buffer + m_cursorPosition + 1, m_text.characters() + m_cursorPosition, m_text.length() - m_cursorPosition); - - m_text = move(newText); - ++m_cursorPosition; - update(); - return; - } -} - -void TextBox::timerEvent(TimerEvent&) -{ - // FIXME: Disable the timer when not focused. - if (!isFocused()) - return; - - m_cursorBlinkState = !m_cursorBlinkState; - update(); -} diff --git a/Widgets/TextBox.h b/Widgets/TextBox.h deleted file mode 100644 index 5236749099..0000000000 --- a/Widgets/TextBox.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include "Widget.h" -#include <AK/Function.h> - -class TextBox final : public Widget { -public: - explicit TextBox(Widget* parent = nullptr); - virtual ~TextBox() override; - - String text() const { return m_text; } - void setText(String&&); - - Function<void(TextBox&)> onReturnPressed; - -private: - virtual const char* class_name() const override { return "TextBox"; } - virtual void paintEvent(PaintEvent&) override; - virtual void mouseDownEvent(MouseEvent&) override; - virtual void keyDownEvent(KeyEvent&) override; - virtual void timerEvent(TimerEvent&) override; - - void handleBackspace(); - - String m_text; - unsigned m_cursorPosition { 0 }; - bool m_cursorBlinkState { false }; -}; - diff --git a/Widgets/Widget.cpp b/Widgets/Widget.cpp deleted file mode 100644 index 18aa00929d..0000000000 --- a/Widgets/Widget.cpp +++ /dev/null @@ -1,164 +0,0 @@ -#include "Widget.h" -#include "Event.h" -#include "EventLoop.h" -#include "GraphicsBitmap.h" -#include "WindowManager.h" -#include "Window.h" -#include "Painter.h" -#include <AK/Assertions.h> - -Widget::Widget(Widget* parent) - : Object(parent) -{ - setFont(nullptr); - m_backgroundColor = Color::White; - m_foregroundColor = Color::Black; -} - -Widget::~Widget() -{ -} - -void Widget::setWindowRelativeRect(const Rect& rect, bool should_update) -{ - // FIXME: Make some kind of event loop driven ResizeEvent? - m_relativeRect = rect; - if (should_update) - update(); -} - -void Widget::repaint(const Rect& rect) -{ - // FIXME: Implement. -} - -void Widget::event(Event& event) -{ - switch (event.type()) { - case Event::Paint: - m_hasPendingPaintEvent = false; - if (auto* win = window()) { - if (win->is_being_dragged()) - return; - if (!win->is_visible()) - return; - } - return paintEvent(static_cast<PaintEvent&>(event)); - case Event::Show: - return showEvent(static_cast<ShowEvent&>(event)); - case Event::Hide: - return hideEvent(static_cast<HideEvent&>(event)); - case Event::KeyDown: - return keyDownEvent(static_cast<KeyEvent&>(event)); - case Event::KeyUp: - return keyUpEvent(static_cast<KeyEvent&>(event)); - case Event::MouseMove: - return mouseMoveEvent(static_cast<MouseEvent&>(event)); - case Event::MouseDown: - // FIXME: Focus self if needed. - return mouseDownEvent(static_cast<MouseEvent&>(event)); - case Event::MouseUp: - return mouseUpEvent(static_cast<MouseEvent&>(event)); - default: - return Object::event(event); - } -} - -void Widget::paintEvent(PaintEvent& event) -{ - //printf("Widget::paintEvent :)\n"); - if (fillWithBackgroundColor()) { - Painter painter(*this); - painter.fill_rect(rect(), backgroundColor()); - } - for (auto* ch : children()) { - auto* child = (Widget*)ch; - child->event(event); - } -} - -void Widget::showEvent(ShowEvent&) -{ -} - -void Widget::hideEvent(HideEvent&) -{ -} - -void Widget::keyDownEvent(KeyEvent&) -{ -} - -void Widget::keyUpEvent(KeyEvent&) -{ -} - -void Widget::mouseDownEvent(MouseEvent&) -{ -} - -void Widget::mouseUpEvent(MouseEvent&) -{ -} - -void Widget::mouseMoveEvent(MouseEvent&) -{ -} - -void Widget::update() -{ - auto* w = window(); - if (!w) - return; - if (m_hasPendingPaintEvent) - return; - m_hasPendingPaintEvent = true; - EventLoop::main().postEvent(w, make<PaintEvent>(relativeRect())); -} - -Widget::HitTestResult Widget::hitTest(int x, int y) -{ - // FIXME: Care about z-order. - for (auto* ch : children()) { - auto* child = (Widget*)ch; - if (child->relativeRect().contains(x, y)) { - return child->hitTest(x - child->relativeRect().x(), y - child->relativeRect().y()); - } - } - return { this, x, y }; -} - -void Widget::setWindow(Window* window) -{ - if (m_window == window) - return; - m_window = window; -} - -bool Widget::isFocused() const -{ - // FIXME: Implement. - return false; -} - -void Widget::setFocus(bool focus) -{ - if (focus == isFocused()) - return; - // FIXME: Implement. -} - -void Widget::setFont(RetainPtr<Font>&& font) -{ - if (!font) - m_font = Font::default_font(); - else - m_font = move(font); -} - -GraphicsBitmap* Widget::backing() -{ - if (auto* w = window()) - return w->backing(); - return nullptr; -} diff --git a/Widgets/Widget.h b/Widgets/Widget.h deleted file mode 100644 index f209f169ad..0000000000 --- a/Widgets/Widget.h +++ /dev/null @@ -1,99 +0,0 @@ -#pragma once - -#include "Event.h" -#include "Object.h" -#include "Rect.h" -#include "Color.h" -#include "Font.h" -#include <AK/AKString.h> - -class GraphicsBitmap; -class Window; - -class Widget : public Object { -public: - explicit Widget(Widget* parent = nullptr); - virtual ~Widget(); - - virtual void event(Event&) override; - virtual void paintEvent(PaintEvent&); - virtual void showEvent(ShowEvent&); - virtual void hideEvent(HideEvent&); - virtual void keyDownEvent(KeyEvent&); - virtual void keyUpEvent(KeyEvent&); - virtual void mouseMoveEvent(MouseEvent&); - virtual void mouseDownEvent(MouseEvent&); - virtual void mouseUpEvent(MouseEvent&); - - Rect relativeRect() const { return m_relativeRect; } - Point relativePosition() const { return m_relativeRect.location(); } - - int x() const { return m_relativeRect.x(); } - int y() const { return m_relativeRect.y(); } - int width() const { return m_relativeRect.width(); } - int height() const { return m_relativeRect.height(); } - - Rect rect() const { return { 0, 0, width(), height() }; } - Size size() const { return m_relativeRect.size(); } - - void update(); - void repaint(const Rect&); - - bool isFocused() const; - void setFocus(bool); - - struct HitTestResult { - Widget* widget { nullptr }; - int localX { 0 }; - int localY { 0 }; - }; - HitTestResult hitTest(int x, int y); - - virtual const char* class_name() const override { return "Widget"; } - - void setWindowRelativeRect(const Rect&, bool should_update = true); - - Color backgroundColor() const { return m_backgroundColor; } - Color foregroundColor() const { return m_foregroundColor; } - - void setBackgroundColor(Color color) { m_backgroundColor = color; } - void setForegroundColor(Color color) { m_foregroundColor = color; } - - Window* window() - { - if (auto* pw = parentWidget()) - return pw->window(); - return m_window; - } - - const Window* window() const - { - if (auto* pw = parentWidget()) - return pw->window(); - return m_window; - } - - void setWindow(Window*); - - Widget* parentWidget() { return static_cast<Widget*>(parent()); } - const Widget* parentWidget() const { return static_cast<const Widget*>(parent()); } - - void setFillWithBackgroundColor(bool b) { m_fillWithBackgroundColor = b; } - bool fillWithBackgroundColor() const { return m_fillWithBackgroundColor; } - - const Font& font() const { return *m_font; } - void setFont(RetainPtr<Font>&&); - - virtual GraphicsBitmap* backing(); - -private: - Window* m_window { nullptr }; - - Rect m_relativeRect; - Color m_backgroundColor { 0xffffff }; - Color m_foregroundColor { 0x000000 }; - RetainPtr<Font> m_font; - - bool m_hasPendingPaintEvent { false }; - bool m_fillWithBackgroundColor { true }; -}; diff --git a/Widgets/Window.cpp b/Widgets/Window.cpp deleted file mode 100644 index 29db30eb74..0000000000 --- a/Widgets/Window.cpp +++ /dev/null @@ -1,99 +0,0 @@ -#include "Window.h" -#include "WindowManager.h" -#include "Event.h" -#include "EventLoop.h" -#include "Process.h" - -Window::Window(Process& process, int window_id) - : m_process(process) - , m_window_id(window_id) -{ - WindowManager::the().addWindow(*this); -} - -Window::~Window() -{ - WindowManager::the().removeWindow(*this); -} - -void Window::set_title(String&& title) -{ - if (m_title == title) - return; - - m_title = move(title); - WindowManager::the().notifyTitleChanged(*this); -} - -void Window::set_rect(const Rect& rect) -{ - if (m_rect == rect) - return; - auto oldRect = m_rect; - m_rect = rect; - dbgprintf("Window::setRect %d,%d %dx%d\n", m_rect.x(), m_rect.y(), m_rect.width(), m_rect.height()); - m_backing = GraphicsBitmap::create(m_process, m_rect.size()); - WindowManager::the().notifyRectChanged(*this, oldRect, m_rect); -} - -// FIXME: Just use the same types. -static GUI_MouseButton to_api(MouseButton button) -{ - switch (button) { - case MouseButton::None: return GUI_MouseButton::NoButton; - case MouseButton::Left: return GUI_MouseButton::Left; - case MouseButton::Right: return GUI_MouseButton::Right; - case MouseButton::Middle: return GUI_MouseButton::Middle; - } -} - -void Window::event(Event& event) -{ - GUI_Event gui_event; - gui_event.window_id = window_id(); - - switch (event.type()) { - case Event::Paint: - gui_event.type = GUI_Event::Type::Paint; - gui_event.paint.rect = static_cast<PaintEvent&>(event).rect(); - break; - case Event::MouseMove: - gui_event.type = GUI_Event::Type::MouseMove; - gui_event.mouse.position = static_cast<MouseEvent&>(event).position(); - break; - case Event::MouseDown: - gui_event.type = GUI_Event::Type::MouseDown; - gui_event.mouse.position = static_cast<MouseEvent&>(event).position(); - gui_event.mouse.button = to_api(static_cast<MouseEvent&>(event).button()); - break; - case Event::MouseUp: - gui_event.type = GUI_Event::Type::MouseUp; - gui_event.mouse.position = static_cast<MouseEvent&>(event).position(); - gui_event.mouse.button = to_api(static_cast<MouseEvent&>(event).button()); - break; - case Event::KeyDown: - gui_event.type = GUI_Event::Type::KeyDown; - gui_event.key.character = static_cast<KeyEvent&>(event).text()[0]; - break; - } - - if (gui_event.type == GUI_Event::Type::Invalid) - return; - - { - LOCKER(m_process.gui_events_lock()); - m_process.gui_events().append(move(gui_event)); - } -} - -bool Window::is_visible() const -{ - return WindowManager::the().isVisible(const_cast<Window&>(*this)); -} - -void Window::close() -{ - WindowManager::the().removeWindow(*this); - deleteLater(); -} - diff --git a/Widgets/Window.h b/Widgets/Window.h deleted file mode 100644 index 1bbe354eb1..0000000000 --- a/Widgets/Window.h +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once - -#include "Object.h" -#include "Rect.h" -#include "GraphicsBitmap.h" -#include <AK/AKString.h> -#include <AK/InlineLinkedList.h> - -class Process; - -class Window final : public Object, public InlineLinkedListNode<Window> { -public: - Window(Process&, int window_id); - virtual ~Window() override; - - int window_id() const { return m_window_id; } - - String title() const { return m_title; } - void set_title(String&&); - - int x() const { return m_rect.x(); } - int y() const { return m_rect.y(); } - int width() const { return m_rect.width(); } - int height() const { return m_rect.height(); } - - const Rect& rect() const { return m_rect; } - void set_rect(const Rect&); - void set_rect_without_repaint(const Rect& rect) { m_rect = rect; } - - Point position() const { return m_rect.location(); } - void set_position_without_repaint(const Point& position) { set_rect_without_repaint({ position.x(), position.y(), width(), height() }); } - - virtual void event(Event&) override; - - bool is_being_dragged() const { return m_is_being_dragged; } - void set_is_being_dragged(bool b) { m_is_being_dragged = b; } - - bool is_visible() const; - - void close(); - - GraphicsBitmap* backing() { return m_backing.ptr(); } - - // For InlineLinkedList. - // FIXME: Maybe make a ListHashSet and then WindowManager can just use that. - Window* m_next { nullptr }; - Window* m_prev { nullptr }; - -private: - String m_title; - Rect m_rect; - bool m_is_being_dragged { false }; - - RetainPtr<GraphicsBitmap> m_backing; - Process& m_process; - int m_window_id { -1 }; -}; - diff --git a/Widgets/test.cpp b/Widgets/test.cpp deleted file mode 100644 index c25c5879a8..0000000000 --- a/Widgets/test.cpp +++ /dev/null @@ -1,100 +0,0 @@ -#include "FrameBuffer.h" -#include "EventLoop.h" -#include "Label.h" -#include "Button.h" -#include "WindowManager.h" -#include "Window.h" -#include "ClockWidget.h" -#include "CheckBox.h" -#include "ListBox.h" -#include "TextBox.h" -#include "MsgBox.h" -#include <cstdio> - -int main(int argc, char** argv) -{ - FrameBuffer fb(800, 600); - fb.show(); - - EventLoop loop; - - auto* fontTestWindow = new Window; - fontTestWindow->set_title("Font test"); - fontTestWindow->set_rect({ 140, 100, 300, 80 }); - - auto* fontTestWindowWidget = new Widget; - fontTestWindow->setMainWidget(fontTestWindowWidget); - fontTestWindowWidget->setWindowRelativeRect({ 0, 0, 300, 80 }); - - auto* l1 = new Label(fontTestWindowWidget); - l1->setWindowRelativeRect({ 0, 0, 300, 20 }); - l1->setText("0123456789"); - - auto* l2 = new Label(fontTestWindowWidget); - l2->setWindowRelativeRect({ 0, 20, 300, 20 }); - l2->setText("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); - - auto* l3 = new Label(fontTestWindowWidget); - l3->setWindowRelativeRect({ 0, 40, 300, 20 }); - l3->setText("abcdefghijklmnopqrstuvwxyz"); - - auto* l4 = new Label(fontTestWindowWidget); - l4->setWindowRelativeRect({ 0, 60, 300, 20 }); - l4->setText("!\"#$%&'()*+,-./:;<=>?@[\\]^_{|}~"); - - { - auto* widgetTestWindow = new Window; - widgetTestWindow->set_title("Widget test"); - widgetTestWindow->set_rect({ 20, 40, 100, 180 }); - - auto* widgetTestWindowWidget = new Widget; - widgetTestWindowWidget->setWindowRelativeRect({ 0, 0, 100, 100 }); - widgetTestWindow->setMainWidget(widgetTestWindowWidget); - - auto* l = new Label(widgetTestWindowWidget); - l->setWindowRelativeRect({ 0, 0, 100, 20 }); - l->setText("Label"); - - auto* b = new Button(widgetTestWindowWidget); - b->setWindowRelativeRect({ 0, 20, 100, 20 }); - b->setCaption("Button"); - - b->onClick = [] (Button& button) { - printf("Button %p clicked!\n", &button); - }; - - auto* c = new CheckBox(widgetTestWindowWidget); - c->setWindowRelativeRect({ 0, 40, 100, 20 }); - c->setCaption("CheckBox"); - - auto *lb = new ListBox(widgetTestWindowWidget); - lb->setWindowRelativeRect({ 0, 60, 100, 100 }); - lb->addItem("This"); - lb->addItem("is"); - lb->addItem("a"); - lb->addItem("ListBox"); - - auto *tb = new TextBox(widgetTestWindowWidget); - tb->setWindowRelativeRect({ 0, 160, 100, 20 }); - tb->setText("Hello!"); - tb->setFocus(true); - - tb->onReturnPressed = [] (TextBox& textBox) { - printf("TextBox %p return pressed: '%s'\n", &textBox, textBox.text().characters()); - MsgBox(nullptr, textBox.text()); - }; - - WindowManager::the().setActiveWindow(widgetTestWindow); - } - -#if 0 - auto* clockWin = new Window; - clockWin->setTitle("Clock"); - clockWin->setRect({ 500, 50, 100, 40 }); - clockWin->setMainWidget(new ClockWidget); -#endif - - MsgBox(nullptr, "This is a message box!"); - - return loop.exec(); -} |