1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:08:12 +00:00

LibGUI: Add GInputBox for getting a string from a modal dialog.

Use this to implement some of the toolbar actions in IRCClient. :^)
This commit is contained in:
Andreas Kling 2019-03-19 01:41:00 +01:00
parent b87c099535
commit a6538feed1
11 changed files with 158 additions and 17 deletions

View file

@ -9,7 +9,7 @@
#include <LibGUI/GAction.h> #include <LibGUI/GAction.h>
#include <LibGUI/GMenu.h> #include <LibGUI/GMenu.h>
#include <LibGUI/GMenuBar.h> #include <LibGUI/GMenuBar.h>
#include <LibGUI/GMessageBox.h> #include <LibGUI/GInputBox.h>
#include <stdio.h> #include <stdio.h>
IRCAppWindow::IRCAppWindow() IRCAppWindow::IRCAppWindow()
@ -49,23 +49,26 @@ void IRCAppWindow::setup_client()
void IRCAppWindow::setup_actions() void IRCAppWindow::setup_actions()
{ {
m_join_action = GAction::create("Join channel", GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/16x16/irc-join.rgb", { 16, 16 }), [] (auto&) { m_join_action = GAction::create("Join channel", GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/16x16/irc-join.rgb", { 16, 16 }), [&] (auto&) {
printf("FIXME: Implement join action\n"); GInputBox input_box("Enter nickname:", "Join channel");
if (input_box.exec() == GInputBox::ExecOK)
m_client.handle_join_action(input_box.text_value());
}); });
m_part_action = GAction::create("Part from channel", GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/16x16/irc-part.rgb", { 16, 16 }), [] (auto&) { m_part_action = GAction::create("Part from channel", GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/16x16/irc-part.rgb", { 16, 16 }), [] (auto&) {
printf("FIXME: Implement part action\n"); printf("FIXME: Implement part action\n");
}); });
m_whois_action = GAction::create("Whois user", GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/16x16/irc-whois.rgb", { 16, 16 }), [] (auto&) { m_whois_action = GAction::create("Whois user", GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/16x16/irc-whois.rgb", { 16, 16 }), [&] (auto&) {
printf("FIXME: Implement whois action\n"); GInputBox input_box("Enter nickname:", "IRC WHOIS lookup");
GMessageBox box("Who would you like to WHOIS?", "Whois user"); if (input_box.exec() == GInputBox::ExecOK)
int code = box.exec(); m_client.handle_whois_action(input_box.text_value());
dbgprintf("GMessageBox::exec() returned %d\n", code);
}); });
m_open_query_action = GAction::create("Open query", GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/16x16/irc-open-query.rgb", { 16, 16 }), [] (auto&) { m_open_query_action = GAction::create("Open query", GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/16x16/irc-open-query.rgb", { 16, 16 }), [&] (auto&) {
printf("FIXME: Implement open-query action\n"); GInputBox input_box("Enter nickname:", "Open IRC query with...");
if (input_box.exec() == GInputBox::ExecOK)
m_client.handle_open_query_action(input_box.text_value());
}); });
m_close_query_action = GAction::create("Close query", GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/16x16/irc-close-query.rgb", { 16, 16 }), [] (auto&) { m_close_query_action = GAction::create("Close query", GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/16x16/irc-close-query.rgb", { 16, 16 }), [] (auto&) {

View file

@ -554,3 +554,29 @@ void IRCClient::handle_user_command(const String& input)
return; return;
} }
} }
void IRCClient::handle_whois_action(const String& nick)
{
send_whois(nick);
}
void IRCClient::handle_open_query_action(const String& nick)
{
ensure_query(nick);
}
void IRCClient::handle_close_query_action(const String& nick)
{
m_queries.remove(nick);
m_client_window_list_model->update();
}
void IRCClient::handle_join_action(const String& channel)
{
join_channel(channel);
}
void IRCClient::handle_part_action(const String& channel)
{
part_channel(channel);
}

View file

@ -57,6 +57,12 @@ public:
void handle_user_input_in_query(const String& query_name, const String&); void handle_user_input_in_query(const String& query_name, const String&);
void handle_user_input_in_server(const String&); void handle_user_input_in_server(const String&);
void handle_whois_action(const String&);
void handle_open_query_action(const String&);
void handle_close_query_action(const String&);
void handle_join_action(const String&);
void handle_part_action(const String&);
IRCQuery& ensure_query(const String& name); IRCQuery& ensure_query(const String& name);
IRCChannel& ensure_channel(const String& name); IRCChannel& ensure_channel(const String& name);

View file

@ -70,6 +70,12 @@ void GButton::mousedown_event(GMouseEvent& event)
GWidget::mousedown_event(event); GWidget::mousedown_event(event);
} }
void GButton::click()
{
if (on_click)
on_click(*this);
}
void GButton::mouseup_event(GMouseEvent& event) void GButton::mouseup_event(GMouseEvent& event)
{ {
#ifdef GBUTTON_DEBUG #ifdef GBUTTON_DEBUG
@ -81,10 +87,8 @@ void GButton::mouseup_event(GMouseEvent& event)
m_tracking_cursor = false; m_tracking_cursor = false;
set_global_cursor_tracking(false); set_global_cursor_tracking(false);
update(); update();
if (was_being_pressed) { if (was_being_pressed)
if (on_click) click();
on_click(*this);
}
} }
GWidget::mouseup_event(event); GWidget::mouseup_event(event);
} }

View file

@ -23,6 +23,8 @@ public:
void set_button_style(GButtonStyle style) { m_button_style = style; } void set_button_style(GButtonStyle style) { m_button_style = style; }
GButtonStyle button_style() const { return m_button_style; } GButtonStyle button_style() const { return m_button_style; }
void click();
virtual const char* class_name() const override { return "GButton"; } virtual const char* class_name() const override { return "GButton"; }
private: private:

76
LibGUI/GInputBox.cpp Normal file
View file

@ -0,0 +1,76 @@
#include <LibGUI/GInputBox.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GLabel.h>
#include <LibGUI/GButton.h>
#include <LibGUI/GTextEditor.h>
#include <stdio.h>
GInputBox::GInputBox(const String& prompt, const String& title, GObject* parent)
: GDialog(parent)
, m_prompt(prompt)
{
set_title(title);
build();
}
GInputBox::~GInputBox()
{
}
void GInputBox::build()
{
auto* widget = new GWidget;
set_main_widget(widget);
int text_width = widget->font().width(m_prompt);
set_rect(x(), y(), text_width + 80, 120);
widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
widget->set_fill_with_background_color(true);
widget->layout()->set_margins({ 8, 8, 8, 8 });
widget->layout()->set_spacing(8);
auto* label = new GLabel(m_prompt, widget);
label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
label->set_preferred_size({ text_width, 16 });
auto* text_editor = new GTextEditor(GTextEditor::SingleLine, widget);
text_editor->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
text_editor->set_preferred_size({ 0, 16 });
auto* button_container_outer = new GWidget(widget);
button_container_outer->set_layout(make<GBoxLayout>(Orientation::Vertical));
auto* button_container_inner = new GWidget(button_container_outer);
button_container_inner->set_layout(make<GBoxLayout>(Orientation::Horizontal));
button_container_inner->layout()->set_spacing(8);
auto* cancel_button = new GButton(button_container_inner);
cancel_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
cancel_button->set_preferred_size({ 0, 16 });
cancel_button->set_caption("Cancel");
cancel_button->on_click = [&] (auto&) {
fprintf(stderr, "GInputBox: Cancel button clicked\n");
done(1);
};
auto* ok_button = new GButton(button_container_inner);
ok_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
ok_button->set_preferred_size({ 0, 16 });
ok_button->set_caption("OK");
ok_button->on_click = [&] (auto&) {
fprintf(stderr, "GInputBox: OK button clicked\n");
m_text_value = text_editor->text();
done(0);
};
text_editor->on_return_pressed = [&] (auto&) {
ok_button->click();
};
text_editor->on_escape_pressed = [&] (auto&) {
cancel_button->click();
};
text_editor->set_focus(true);
}

18
LibGUI/GInputBox.h Normal file
View file

@ -0,0 +1,18 @@
#pragma once
#include <LibGUI/GDialog.h>
class GInputBox : public GDialog {
public:
enum ExecResult { ExecOK = 0, ExecCancel = 1 };
explicit GInputBox(const String& prompt, const String& title, GObject* parent = nullptr);
virtual ~GInputBox() override;
String text_value() const { return m_text_value; }
private:
void build();
String m_prompt;
String m_text_value;
};

View file

@ -7,10 +7,8 @@ public:
explicit GMessageBox(const String& text, const String& title, GObject* parent = nullptr); explicit GMessageBox(const String& text, const String& title, GObject* parent = nullptr);
virtual ~GMessageBox() override; virtual ~GMessageBox() override;
String text() const { return m_text; } private:
void build(); void build();
private:
String m_text; String m_text;
}; };

View file

@ -17,6 +17,7 @@ GTextEditor::GTextEditor(Type type, GWidget* parent)
m_ruler_visible = is_multi_line(); m_ruler_visible = is_multi_line();
set_font(GFontDatabase::the().get_by_name("Csilla Thin")); set_font(GFontDatabase::the().get_by_name("Csilla Thin"));
m_lines.append(make<Line>()); m_lines.append(make<Line>());
m_cursor = { 0, 0 };
} }
GTextEditor::~GTextEditor() GTextEditor::~GTextEditor()
@ -227,6 +228,11 @@ void GTextEditor::toggle_selection_if_needed_for_event(const GKeyEvent& event)
void GTextEditor::keydown_event(GKeyEvent& event) void GTextEditor::keydown_event(GKeyEvent& event)
{ {
if (event.key() == KeyCode::Key_Escape) {
if (on_escape_pressed)
on_escape_pressed(*this);
return;
}
if (event.key() == KeyCode::Key_Up) { if (event.key() == KeyCode::Key_Up) {
if (m_cursor.line() > 0) { if (m_cursor.line() > 0) {
int new_line = m_cursor.line() - 1; int new_line = m_cursor.line() - 1;

View file

@ -95,6 +95,7 @@ public:
void paste(); void paste();
Function<void(GTextEditor&)> on_return_pressed; Function<void(GTextEditor&)> on_return_pressed;
Function<void(GTextEditor&)> on_escape_pressed;
virtual const char* class_name() const override { return "GTextEditor"; } virtual const char* class_name() const override { return "GTextEditor"; }

View file

@ -43,6 +43,7 @@ LIBGUI_OBJS = \
GSocket.o \ GSocket.o \
GTCPSocket.o \ GTCPSocket.o \
GMessageBox.o \ GMessageBox.o \
GInputBox.o \
GDialog.o \ GDialog.o \
GWindow.o GWindow.o