commit 08a4241336d1a95e7c65a4affcdb50b2a8b8cb72
parent 78cdf4f0a0723c2225f7c14fe7353bdf107dfe6b
Author: cfillion <cfillion@users.noreply.github.com>
Date: Thu, 31 Dec 2015 17:49:54 -0500
add a context menu to the configuration dialog
Diffstat:
8 files changed, 115 insertions(+), 8 deletions(-)
diff --git a/src/dialog.cpp b/src/dialog.cpp
@@ -61,6 +61,9 @@ WDL_DLGRET Dialog::Proc(HWND handle, UINT msg, WPARAM wParam, LPARAM lParam)
case WM_NOTIFY:
dlg->onNotify((LPNMHDR)lParam, lParam);
break;
+ case WM_CONTEXTMENU:
+ dlg->onContextMenu((HWND)wParam, lParam);
+ break;
case WM_DESTROY:
dlg->onDestroy();
break;
@@ -185,6 +188,10 @@ void Dialog::onNotify(LPNMHDR, LPARAM)
{
}
+void Dialog::onContextMenu(HWND, LPARAM)
+{
+}
+
void Dialog::onDestroy()
{
}
diff --git a/src/dialog.hpp b/src/dialog.hpp
@@ -81,6 +81,7 @@ protected:
virtual void onTimer();
virtual void onCommand(WPARAM, LPARAM);
virtual void onNotify(LPNMHDR, LPARAM);
+ virtual void onContextMenu(HWND, LPARAM);
virtual void onDestroy();
private:
diff --git a/src/listview.cpp b/src/listview.cpp
@@ -70,7 +70,12 @@ void ListView::clear()
m_rowSize = 0;
}
-int ListView::selectedIndex() const
+bool ListView::hasSelection() const
+{
+ return currentIndex() > -1;
+}
+
+int ListView::currentIndex() const
{
return ListView_GetNextItem(m_handle, -1, LVNI_SELECTED);
}
diff --git a/src/listview.hpp b/src/listview.hpp
@@ -45,7 +45,8 @@ public:
void addRow(const Row &);
void clear();
- int selectedIndex() const;
+ bool hasSelection() const;
+ int currentIndex() const;
void onSelect(const Callback &callback) { m_onSelect.connect(callback); }
void onNotify(LPNMHDR, LPARAM);
diff --git a/src/manager.cpp b/src/manager.cpp
@@ -19,9 +19,13 @@
#include "config.hpp"
#include "encoding.hpp"
+#include "menu.hpp"
#include "reapack.hpp"
#include "resource.hpp"
+static const int ACTION_ENABLE = 300;
+static const int ACTION_DISABLE = 301;
+
using namespace std;
Manager::Manager(ReaPack *reapack)
@@ -73,6 +77,34 @@ void Manager::onNotify(LPNMHDR info, LPARAM lParam)
}
}
+void Manager::onContextMenu(HWND target, LPARAM lParam)
+{
+ if(target != m_list->handle())
+ return;
+
+ Menu menu;
+
+ menu.addAction(AUTO_STR("Enable"), ACTION_ENABLE);
+ const UINT disableAction =
+ menu.addAction(AUTO_STR("Disable"), ACTION_DISABLE);
+
+ menu.addSeparator();
+
+ const UINT uninstallAction =
+ menu.addAction(AUTO_STR("Uninstall"), IDC_UNINSTALL);
+
+ menu.disable();
+
+ const int index = m_list->currentIndex();
+
+ if(index > -1) {
+ menu.enable(disableAction);
+ menu.enable(uninstallAction);
+ }
+
+ menu.show(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), handle());
+}
+
void Manager::refresh()
{
m_list->clear();
@@ -88,12 +120,12 @@ void Manager::refresh()
void Manager::selectionChanged()
{
- setEnabled(m_list->selectedIndex() > -1, m_uninstall);
+ setEnabled(m_list->hasSelection(), m_uninstall);
}
void Manager::uninstall()
{
- const int index = m_list->selectedIndex();
+ const int index = m_list->currentIndex();
if(index < 0)
return;
diff --git a/src/manager.hpp b/src/manager.hpp
@@ -22,6 +22,7 @@
#include "listview.hpp"
+class Menu;
class ReaPack;
class Manager : public Dialog {
@@ -35,6 +36,7 @@ protected:
void onInit() override;
void onCommand(WPARAM, LPARAM) override;
void onNotify(LPNMHDR, LPARAM) override;
+ void onContextMenu(HWND, LPARAM) override;
private:
void selectionChanged();
diff --git a/src/menu.cpp b/src/menu.cpp
@@ -20,13 +20,16 @@
Menu::Menu(HMENU handle)
: m_handle(handle)
{
+ if(!handle)
+ m_handle = CreatePopupMenu();
+
m_size = GetMenuItemCount(m_handle);
if(!empty())
addSeparator();
}
-void Menu::addAction(const auto_char *label, const int commandId)
+UINT Menu::addAction(const auto_char *label, const int commandId)
{
MENUITEMINFO mii{};
mii.cbSize = sizeof(MENUITEMINFO);
@@ -38,7 +41,9 @@ void Menu::addAction(const auto_char *label, const int commandId)
mii.fMask |= MIIM_ID;
mii.wID = commandId;
+ const int index = m_size;
append(mii);
+ return index;
}
void Menu::addSeparator()
@@ -73,3 +78,48 @@ void Menu::append(MENUITEMINFO &mii)
{
InsertMenuItem(m_handle, m_size++, true, &mii);
}
+
+void Menu::show(const int x, const int y, HWND parent) const
+{
+ TrackPopupMenu(m_handle, TPM_BOTTOMALIGN | TPM_LEFTALIGN,
+ x, y, 0, parent, nullptr);
+}
+
+void Menu::disable()
+{
+ setEnabled(false);
+}
+
+void Menu::disable(const UINT index)
+{
+ setEnabled(false, index);
+}
+
+void Menu::enable()
+{
+ setEnabled(true);
+}
+
+void Menu::enable(const UINT index)
+{
+ setEnabled(true, index);
+}
+
+void Menu::setEnabled(const bool enabled)
+{
+ for(UINT i = 0; i < m_size; i++)
+ setEnabled(enabled, i);
+}
+
+void Menu::setEnabled(const bool enabled, const UINT index)
+{
+ MENUITEMINFO mii{};
+ mii.cbSize = sizeof(MENUITEMINFO);
+
+ GetMenuItemInfo(m_handle, index, true, &mii);
+
+ mii.fMask |= MIIM_STATE;
+ mii.fState |= enabled ? MFS_ENABLED : MFS_DISABLED;
+
+ SetMenuItemInfo(m_handle, index, true, &mii);
+}
diff --git a/src/menu.hpp b/src/menu.hpp
@@ -28,20 +28,29 @@
class Menu {
public:
- Menu(HMENU handle);
+ Menu(HMENU handle = 0);
unsigned int size() { return m_size; }
bool empty() const { return m_size == 0; }
- void addAction(const auto_char *label, const int commandId);
+ UINT addAction(const auto_char *label, const int commandId);
void addSeparator();
Menu addMenu(const auto_char *label);
+ void show(const int x, const int y, HWND parent) const;
+
+ void enable();
+ void enable(const UINT);
+ void disable();
+ void disable(const UINT);
+ void setEnabled(const bool);
+ void setEnabled(const bool, const UINT);
+
private:
void append(MENUITEMINFO &);
HMENU m_handle;
- unsigned int m_size;
+ UINT m_size;
};
#endif