summaryrefslogtreecommitdiff
path: root/LibGUI
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-04-26 21:09:56 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-04-26 21:09:56 +0200
commit8f81a3f9dde15f4bf328c8864e6dcbf75a7090fe (patch)
tree8e0ef21fd86c0e55abb5ff85671e7e5cb9dcc2c8 /LibGUI
parent9ff36afeaab7e583afd3de303bba5ded3b8a448e (diff)
downloadserenity-8f81a3f9dde15f4bf328c8864e6dcbf75a7090fe.zip
LibGUI+WindowServer: Make it possible to have checkable GActions.
They show up as checkable GButtons in GToolBar, and with (or without) check marks in menus. There are a bunch of places to make use of this. This patch only takes advantage of it in the FileManager for the view type actions.
Diffstat (limited to 'LibGUI')
-rw-r--r--LibGUI/GAction.cpp13
-rw-r--r--LibGUI/GAction.h8
-rw-r--r--LibGUI/GButton.cpp3
-rw-r--r--LibGUI/GMenu.cpp3
-rw-r--r--LibGUI/GMenuItem.cpp15
-rw-r--r--LibGUI/GMenuItem.h8
6 files changed, 50 insertions, 0 deletions
diff --git a/LibGUI/GAction.cpp b/LibGUI/GAction.cpp
index a402215a29..90a63260f6 100644
--- a/LibGUI/GAction.cpp
+++ b/LibGUI/GAction.cpp
@@ -106,3 +106,16 @@ void GAction::set_enabled(bool enabled)
item.set_enabled(enabled);
});
}
+
+void GAction::set_checked(bool checked)
+{
+ if (m_checked == checked)
+ return;
+ m_checked = checked;
+ for_each_toolbar_button([checked] (GButton& button) {
+ button.set_checked(checked);
+ });
+ for_each_menu_item([checked] (GMenuItem& item) {
+ item.set_checked(checked);
+ });
+}
diff --git a/LibGUI/GAction.h b/LibGUI/GAction.h
index 1d8d65adce..7dafc464ec 100644
--- a/LibGUI/GAction.h
+++ b/LibGUI/GAction.h
@@ -59,6 +59,12 @@ public:
bool is_enabled() const { return m_enabled; }
void set_enabled(bool);
+ bool is_checkable() const { return m_checkable; }
+ void set_checkable(bool checkable) { m_checkable = checkable; }
+
+ bool is_checked() const { ASSERT(is_checkable()); return m_checked; }
+ void set_checked(bool);
+
void register_button(Badge<GButton>, GButton&);
void unregister_button(Badge<GButton>, GButton&);
void register_menu_item(Badge<GMenuItem>, GMenuItem&);
@@ -79,6 +85,8 @@ private:
RetainPtr<GraphicsBitmap> m_icon;
GShortcut m_shortcut;
bool m_enabled { true };
+ bool m_checkable { false };
+ bool m_checked { false };
ShortcutScope m_scope { ShortcutScope::None };
HashTable<GButton*> m_buttons;
diff --git a/LibGUI/GButton.cpp b/LibGUI/GButton.cpp
index 534327df78..7ca61b85ea 100644
--- a/LibGUI/GButton.cpp
+++ b/LibGUI/GButton.cpp
@@ -133,6 +133,9 @@ void GButton::set_action(GAction& action)
m_action = action.make_weak_ptr();
action.register_button({ }, *this);
set_enabled(action.is_enabled());
+ set_checkable(action.is_checkable());
+ if (action.is_checkable())
+ set_checked(action.is_checked());
}
void GButton::set_icon(RetainPtr<GraphicsBitmap>&& icon)
diff --git a/LibGUI/GMenu.cpp b/LibGUI/GMenu.cpp
index 1bc0fc4f9a..2a1864802d 100644
--- a/LibGUI/GMenu.cpp
+++ b/LibGUI/GMenu.cpp
@@ -90,6 +90,9 @@ int GMenu::realize_menu()
request.menu.menu_id = m_menu_id;
request.menu.identifier = i;
request.menu.enabled = action.is_enabled();
+ request.menu.checkable = action.is_checkable();
+ if (action.is_checkable())
+ request.menu.checked = action.is_checked();
ASSERT(action.text().length() < (ssize_t)sizeof(request.text));
strcpy(request.text, action.text().characters());
request.text_length = action.text().length();
diff --git a/LibGUI/GMenuItem.cpp b/LibGUI/GMenuItem.cpp
index 8cbba6d943..671df2d631 100644
--- a/LibGUI/GMenuItem.cpp
+++ b/LibGUI/GMenuItem.cpp
@@ -16,6 +16,9 @@ GMenuItem::GMenuItem(unsigned menu_id, Retained<GAction>&& action)
{
m_action->register_menu_item({ }, *this);
m_enabled = m_action->is_enabled();
+ m_checkable = m_action->is_checkable();
+ if (m_checkable)
+ m_checked = m_action->is_checked();
}
GMenuItem::~GMenuItem()
@@ -32,6 +35,15 @@ void GMenuItem::set_enabled(bool enabled)
update_window_server();
}
+void GMenuItem::set_checked(bool checked)
+{
+ ASSERT(is_checkable());
+ if (m_checked == checked)
+ return;
+ m_checked = checked;
+ update_window_server();
+}
+
void GMenuItem::update_window_server()
{
auto& action = *m_action;
@@ -40,6 +52,9 @@ void GMenuItem::update_window_server()
request.menu.menu_id = m_menu_id;
request.menu.identifier = m_identifier;
request.menu.enabled = action.is_enabled();
+ request.menu.checkable = action.is_checkable();
+ if (action.is_checkable())
+ request.menu.checked = action.is_checked();
ASSERT(action.text().length() < (ssize_t)sizeof(request.text));
strcpy(request.text, action.text().characters());
request.text_length = action.text().length();
diff --git a/LibGUI/GMenuItem.h b/LibGUI/GMenuItem.h
index 0cda499e2d..e470424eef 100644
--- a/LibGUI/GMenuItem.h
+++ b/LibGUI/GMenuItem.h
@@ -20,6 +20,12 @@ public:
GAction* action() { return m_action.ptr(); }
unsigned identifier() const { return m_identifier; }
+ bool is_checkable() const { return m_checkable; }
+ void set_checkable(bool checkable) { m_checkable = checkable; }
+
+ bool is_checked() const { return m_checked; }
+ void set_checked(bool);
+
bool is_enabled() const { return m_enabled; }
void set_enabled(bool);
@@ -33,6 +39,8 @@ private:
unsigned m_menu_id { 0 };
unsigned m_identifier { 0 };
bool m_enabled { true };
+ bool m_checkable { false };
+ bool m_checked { false };
RetainPtr<GAction> m_action;
};