mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-22 17:31:58 -05:00
Add basic GUI API for creating labels and buttons.
This commit is contained in:
parent
17c7bf01a5
commit
b2d86b7597
7 changed files with 99 additions and 8 deletions
|
@ -11,6 +11,12 @@ public:
|
|||
WeakPtr() { }
|
||||
WeakPtr(std::nullptr_t) { }
|
||||
|
||||
template<typename U>
|
||||
WeakPtr(WeakPtr<U>&& other)
|
||||
: m_link(reinterpret_cast<WeakLink<T>*>(other.leakLink()))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
WeakPtr& operator=(WeakPtr<U>&& other)
|
||||
{
|
||||
|
|
|
@ -21,3 +21,17 @@ struct GUI_CreateWindowParameters {
|
|||
unsigned flags;
|
||||
char title[128];
|
||||
};
|
||||
|
||||
enum class GUI_WidgetType : unsigned {
|
||||
Label,
|
||||
Button,
|
||||
};
|
||||
|
||||
struct GUI_CreateWidgetParameters {
|
||||
GUI_WidgetType type;
|
||||
GUI_Rect rect;
|
||||
GUI_Color background_color;
|
||||
bool opaque;
|
||||
unsigned flags;
|
||||
char text[256];
|
||||
};
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <AK/InlineLinkedList.h>
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <AK/WeakPtr.h>
|
||||
|
||||
class FileDescriptor;
|
||||
class PageDirectory;
|
||||
|
@ -18,6 +19,7 @@ class Region;
|
|||
class VMObject;
|
||||
class Zone;
|
||||
class Window;
|
||||
class Widget;
|
||||
|
||||
#define COOL_GLOBALS
|
||||
#ifdef COOL_GLOBALS
|
||||
|
@ -189,6 +191,8 @@ public:
|
|||
|
||||
int gui$create_window(const GUI_CreateWindowParameters*);
|
||||
int gui$destroy_window(int window_id);
|
||||
int gui$create_widget(int window_id, const GUI_CreateWidgetParameters*);
|
||||
int gui$destroy_widget(int widget_id);
|
||||
|
||||
DisplayInfo get_display_info();
|
||||
|
||||
|
@ -338,7 +342,8 @@ private:
|
|||
|
||||
RetainPtr<Region> m_display_framebuffer_region;
|
||||
|
||||
Vector<Window*> m_windows;
|
||||
Vector<WeakPtr<Window>> m_windows;
|
||||
Vector<WeakPtr<Widget>> m_widgets;
|
||||
};
|
||||
|
||||
extern Process* current;
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
#include <Widgets/FrameBuffer.h>
|
||||
#include <Widgets/EventLoop.h>
|
||||
#include <Widgets/Font.h>
|
||||
#include <Widgets/Button.h>
|
||||
#include <Widgets/Label.h>
|
||||
#include <Widgets/Widget.h>
|
||||
#include <Widgets/Window.h>
|
||||
#include <Widgets/WindowManager.h>
|
||||
|
@ -34,7 +36,7 @@ int Process::gui$create_window(const GUI_CreateWindowParameters* user_params)
|
|||
if (!validate_read_typed(user_params))
|
||||
return -EFAULT;
|
||||
|
||||
GUI_CreateWindowParameters params = *user_params;
|
||||
auto params = *user_params;
|
||||
Rect rect { params.rect.x, params.rect.y, params.rect.width, params.rect.height };
|
||||
|
||||
if (rect.is_empty())
|
||||
|
@ -47,7 +49,7 @@ int Process::gui$create_window(const GUI_CreateWindowParameters* user_params)
|
|||
return -ENOMEM;
|
||||
|
||||
int window_id = m_windows.size();
|
||||
m_windows.append(window);
|
||||
m_windows.append(window->makeWeakPtr());
|
||||
|
||||
window->setTitle(params.title);
|
||||
window->setRect(rect);
|
||||
|
@ -64,14 +66,71 @@ int Process::gui$create_window(const GUI_CreateWindowParameters* user_params)
|
|||
|
||||
int Process::gui$destroy_window(int window_id)
|
||||
{
|
||||
wait_for_gui_server();
|
||||
dbgprintf("%s<%u> gui$destroy_window (window_id=%d)\n", name().characters(), pid(), window_id);
|
||||
if (window_id < 0)
|
||||
return -EINVAL;
|
||||
if (window_id >= static_cast<int>(m_windows.size()))
|
||||
return -EBADWIN;
|
||||
auto* window = m_windows[window_id];
|
||||
m_windows.remove(window_id);
|
||||
return -EBADWINDOW;
|
||||
auto* window = m_windows[window_id].ptr();
|
||||
if (!window)
|
||||
return -EBADWINDOW;
|
||||
window->deleteLater();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Process::gui$create_widget(int window_id, const GUI_CreateWidgetParameters* user_params)
|
||||
{
|
||||
if (!validate_read_typed(user_params))
|
||||
return -EFAULT;
|
||||
|
||||
if (window_id < 0)
|
||||
return -EINVAL;
|
||||
if (window_id >= static_cast<int>(m_windows.size()))
|
||||
return -EINVAL;
|
||||
if (!m_windows[window_id])
|
||||
return -EINVAL;
|
||||
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())
|
||||
return -EINVAL;
|
||||
|
||||
Widget* widget = nullptr;
|
||||
switch (params.type) {
|
||||
case GUI_WidgetType::Label:
|
||||
widget = new Label(window.mainWidget());
|
||||
static_cast<Label*>(widget)->setText(params.text);
|
||||
break;
|
||||
case GUI_WidgetType::Button:
|
||||
widget = new Button(window.mainWidget());
|
||||
static_cast<Button*>(widget)->setCaption(params.text);
|
||||
break;
|
||||
}
|
||||
|
||||
int widget_id = m_widgets.size();
|
||||
m_widgets.append(widget->makeWeakPtr());
|
||||
|
||||
widget->setWindowRelativeRect(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());
|
||||
|
||||
return window_id;
|
||||
}
|
||||
|
||||
int Process::gui$destroy_widget(int widget_id)
|
||||
{
|
||||
dbgprintf("%s<%u> gui$destroy_widget (widget_id=%d)\n", name().characters(), pid(), widget_id);
|
||||
if (widget_id < 0)
|
||||
return -EINVAL;
|
||||
if (widget_id >= static_cast<int>(m_widgets.size()))
|
||||
return -EBADWINDOW;
|
||||
auto* widget = m_widgets[widget_id].ptr();
|
||||
if (!widget)
|
||||
return -EBADWIDGET;
|
||||
widget->deleteLater();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -191,6 +191,10 @@ static dword handle(RegisterDump& regs, dword function, dword arg1, dword arg2,
|
|||
return current->gui$create_window((const GUI_CreateWindowParameters*)arg1);
|
||||
case Syscall::SC_gui_destroy_window:
|
||||
return current->gui$destroy_window((int)arg1);
|
||||
case Syscall::SC_gui_create_widget:
|
||||
return current->gui$create_widget((int)arg1, (const GUI_CreateWidgetParameters*)arg2);
|
||||
case Syscall::SC_gui_destroy_widget:
|
||||
return current->gui$destroy_widget((int)arg1);
|
||||
default:
|
||||
kprintf("<%u> int0x80: Unknown function %u requested {%x, %x, %x}\n", current->pid(), function, arg1, arg2, arg3);
|
||||
break;
|
||||
|
|
|
@ -68,6 +68,8 @@
|
|||
__ENUMERATE_SYSCALL(sync) \
|
||||
__ENUMERATE_SYSCALL(gui_create_window) \
|
||||
__ENUMERATE_SYSCALL(gui_destroy_window) \
|
||||
__ENUMERATE_SYSCALL(gui_create_widget) \
|
||||
__ENUMERATE_SYSCALL(gui_destroy_widget) \
|
||||
|
||||
namespace Syscall {
|
||||
|
||||
|
|
|
@ -42,7 +42,8 @@
|
|||
__ERROR(ENOTIMPL, "Not implemented") \
|
||||
__ERROR(EAFNOSUPPORT, "Address family not supported") \
|
||||
__ERROR(EWHYTHO, "Failed without setting an error code (Bug!)") \
|
||||
__ERROR(EBADWIN, "Bad WindowID") \
|
||||
__ERROR(EBADWINDOW, "Bad Window ID") \
|
||||
__ERROR(EBADWIDGET, "Bad Widget ID") \
|
||||
|
||||
enum __errno_values {
|
||||
#undef __ERROR
|
||||
|
|
Loading…
Reference in a new issue