From f7261d7b263e4b1ebeb3827a14970b379d1dad7f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 13 Jan 2019 05:08:26 +0100 Subject: [PATCH] Let's use the existing Rect and Color types in the GUI API. Any type that doesn't depend on indirect data can probably be used here. --- Kernel/GUITypes.h | 24 +++++++++------------ Kernel/ProcessGUI.cpp | 16 ++++++-------- Userland/.gitignore | 1 + Userland/guitest.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 23 deletions(-) create mode 100644 Userland/guitest.cpp diff --git a/Kernel/GUITypes.h b/Kernel/GUITypes.h index 5a0099c91d..38ecb4d566 100644 --- a/Kernel/GUITypes.h +++ b/Kernel/GUITypes.h @@ -1,13 +1,9 @@ #pragma once -// GUI system call API types. +#include +#include -struct GUI_Rect { - int x; - int y; - int width; - int height; -}; +// GUI system call API types. struct GUI_WindowFlags { enum { Visible = 1 << 0, @@ -16,9 +12,9 @@ struct GUI_WindowFlags { enum { typedef unsigned GUI_Color; struct GUI_CreateWindowParameters { - GUI_Rect rect; - GUI_Color background_color; - unsigned flags; + Rect rect; + Color background_color; + unsigned flags { 0 }; char title[128]; }; @@ -29,9 +25,9 @@ enum class GUI_WidgetType : unsigned { struct GUI_CreateWidgetParameters { GUI_WidgetType type; - GUI_Rect rect; - GUI_Color background_color; - bool opaque; - unsigned flags; + Rect rect; + Color background_color; + bool opaque { true }; + unsigned flags { 0 }; char text[256]; }; diff --git a/Kernel/ProcessGUI.cpp b/Kernel/ProcessGUI.cpp index 69ca6e6fff..d54eb438d4 100644 --- a/Kernel/ProcessGUI.cpp +++ b/Kernel/ProcessGUI.cpp @@ -37,9 +37,8 @@ int Process::gui$create_window(const GUI_CreateWindowParameters* user_params) return -EFAULT; auto params = *user_params; - Rect rect { params.rect.x, params.rect.y, params.rect.width, params.rect.height }; - if (rect.is_empty()) + if (params.rect.is_empty()) return -EINVAL; ProcessPagingScope scope(EventLoop::main().server_process()); @@ -52,14 +51,14 @@ int Process::gui$create_window(const GUI_CreateWindowParameters* user_params) m_windows.append(window->makeWeakPtr()); window->setTitle(params.title); - window->setRect(rect); + window->setRect(params.rect); auto* main_widget = new Widget; window->setMainWidget(main_widget); - main_widget->setWindowRelativeRect({ 0, 0, rect.width(), rect.height() }); + main_widget->setWindowRelativeRect({ 0, 0, params.rect.width(), params.rect.height() }); main_widget->setBackgroundColor(params.background_color); main_widget->setFillWithBackgroundColor(true); - dbgprintf("%s<%u> gui$create_window: %d with rect {%d,%d %dx%d}\n", name().characters(), pid(), window_id, rect.x(), rect.y(), rect.width(), rect.height()); + dbgprintf("%s<%u> gui$create_window: %d with rect {%d,%d %dx%d}\n", name().characters(), pid(), window_id, params.rect.x(), params.rect.y(), params.rect.width(), params.rect.height()); return window_id; } @@ -92,9 +91,8 @@ int Process::gui$create_widget(int window_id, const GUI_CreateWidgetParameters* auto& window = *m_windows[window_id]; auto params = *user_params; - Rect rect { params.rect.x, params.rect.y, params.rect.width, params.rect.height }; - if (rect.is_empty()) + if (params.rect.is_empty()) return -EINVAL; Widget* widget = nullptr; @@ -112,10 +110,10 @@ int Process::gui$create_widget(int window_id, const GUI_CreateWidgetParameters* int widget_id = m_widgets.size(); m_widgets.append(widget->makeWeakPtr()); - widget->setWindowRelativeRect(rect); + widget->setWindowRelativeRect(params.rect); widget->setBackgroundColor(params.background_color); widget->setFillWithBackgroundColor(params.opaque); - dbgprintf("%s<%u> gui$create_widget: %d with rect {%d,%d %dx%d}\n", name().characters(), pid(), widget_id, rect.x(), rect.y(), rect.width(), rect.height()); + dbgprintf("%s<%u> gui$create_widget: %d with rect {%d,%d %dx%d}\n", name().characters(), pid(), widget_id, params.rect.x(), params.rect.y(), params.rect.width(), params.rect.height()); return window_id; } diff --git a/Userland/.gitignore b/Userland/.gitignore index a7e9d82205..94dd3a2a12 100644 --- a/Userland/.gitignore +++ b/Userland/.gitignore @@ -21,3 +21,4 @@ mkdir touch sync more +guitest diff --git a/Userland/guitest.cpp b/Userland/guitest.cpp new file mode 100644 index 0000000000..80f8102926 --- /dev/null +++ b/Userland/guitest.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + GUI_CreateWindowParameters wparams; + wparams.rect = { 200, 200, 300, 200 }; + wparams.background_color = 0xffc0c0; + strcpy(wparams.title, "GUI test app"); + int window_id = syscall(SC_gui_create_window, &wparams); + if (window_id < 0) { + perror("gui_create_window"); + return 1; + } + + GUI_CreateWidgetParameters label_params; + label_params.type = GUI_WidgetType::Label; + label_params.rect = { 20, 20, 260, 20 }; + label_params.background_color = 0xffffff; + label_params.opaque = true; + strcpy(label_params.text, "Hello World!"); + int label_id = syscall(SC_gui_create_widget, window_id, &label_params); + if (label_id < 0) { + perror("gui_create_widget"); + return 1; + } + + GUI_CreateWidgetParameters button_params; + button_params.type = GUI_WidgetType::Button; + button_params.rect = { 60, 60, 120, 20 }; + button_params.background_color = 0xffffff; + button_params.opaque = true; + strcpy(button_params.text, "I'm a button!"); + int button_id = syscall(SC_gui_create_widget, window_id, &button_params); + if (button_id < 0) { + perror("gui_create_widget"); + return 1; + } + + for (;;) { + } + return 0; +}