commit 73d2f3ae0010b382cfe11b10ee2a17dbfdb962a7
parent 3d512a29e74d89d4eb5c01fdfd8c145701e61b0f
Author: cfillion <cfillion@users.noreply.github.com>
Date: Sun, 31 Jan 2016 02:16:20 -0500
centralize ui control's life and notification management
Diffstat:
7 files changed, 89 insertions(+), 38 deletions(-)
diff --git a/src/control.hpp b/src/control.hpp
@@ -0,0 +1,44 @@
+/* ReaPack: Package manager for REAPER
+ * Copyright (C) 2015-2016 Christian Fillion
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef REAPACK_CONTROL_HPP
+#define REAPACK_CONTROL_HPP
+
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include <swell/swell.h>
+#endif
+
+class Dialog;
+
+class Control {
+public:
+ Control(HWND handle) : m_handle(handle) {}
+ virtual ~Control() {}
+
+ HWND handle() const { return m_handle; }
+
+protected:
+ friend Dialog;
+ virtual void onNotify(LPNMHDR, LPARAM) = 0;
+
+private:
+ HWND m_handle;
+};
+
+#endif
diff --git a/src/dialog.cpp b/src/dialog.cpp
@@ -19,6 +19,8 @@
#include <algorithm>
+#include "control.hpp"
+
using namespace std;
DialogMap Dialog::s_instances;
@@ -85,6 +87,9 @@ Dialog::Dialog(const int templateId)
Dialog::~Dialog()
{
+ for(const auto &controlPair : m_controls)
+ delete controlPair.second;
+
DestroyWindow(m_handle);
s_instances.erase(m_handle);
}
@@ -186,8 +191,12 @@ void Dialog::onCommand(int)
{
}
-void Dialog::onNotify(LPNMHDR, LPARAM)
+void Dialog::onNotify(LPNMHDR info, LPARAM lParam)
{
+ const auto it = m_controls.find((int)info->idFrom);
+
+ if(it != m_controls.end())
+ it->second->onNotify(info, lParam);
}
void Dialog::onContextMenu(HWND, int, int)
diff --git a/src/dialog.hpp b/src/dialog.hpp
@@ -26,6 +26,8 @@
class Dialog;
typedef std::map<HWND, Dialog *> DialogMap;
+class Control;
+
class Dialog {
public:
enum Modality {
@@ -81,6 +83,21 @@ protected:
HWND getControl(const int idc);
+ template<class T, class... Args>
+ T *createControl(int id, Args&&... args)
+ {
+ if(m_controls.count(id))
+ return nullptr;
+
+ HWND handle = getControl(id);
+
+ T *ctrl = new T(args..., handle);
+ m_controls[id] = ctrl;
+
+ return ctrl;
+ }
+
+
virtual void onInit() = 0;
virtual void onShow();
virtual void onHide();
@@ -97,6 +114,8 @@ private:
const int m_template;
bool m_isVisible;
+ std::map<int, Control *> m_controls;
+
REAPER_PLUGIN_HINSTANCE m_instance;
HWND m_parent;
HWND m_handle;
diff --git a/src/listview.cpp b/src/listview.cpp
@@ -24,13 +24,13 @@
using namespace std;
ListView::ListView(const Columns &columns, HWND handle)
- : m_handle(handle), m_columnSize(0), m_rowSize(0)
+ : Control(handle), m_columnSize(0), m_rowSize(0)
{
for(const Column &col : columns)
addColumn(col);
// For some reason FULLROWSELECT doesn't work from the resource file
- ListView_SetExtendedListViewStyleEx(m_handle,
+ ListView_SetExtendedListViewStyleEx(handle,
LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
}
@@ -44,7 +44,7 @@ void ListView::addColumn(const Column &col)
item.mask |= LVCF_TEXT;
item.pszText = const_cast<auto_char *>(col.text.c_str());
- ListView_InsertColumn(m_handle, m_columnSize++, &item);
+ ListView_InsertColumn(handle(), m_columnSize++, &item);
}
int ListView::addRow(const Row &content)
@@ -52,7 +52,7 @@ int ListView::addRow(const Row &content)
LVITEM item{};
item.iItem = m_rowSize++;
- ListView_InsertItem(m_handle, &item);
+ ListView_InsertItem(handle(), &item);
replaceRow(item.iItem, content);
@@ -68,13 +68,13 @@ void ListView::replaceRow(const int index, const Row &content)
for(int i = 0; i < cols; i++) {
auto_char *text = const_cast<auto_char *>(content[i].c_str());
- ListView_SetItemText(m_handle, item.iItem, i, text);
+ ListView_SetItemText(handle(), item.iItem, i, text);
}
}
void ListView::removeRow(const int index)
{
- ListView_DeleteItem(m_handle, index);
+ ListView_DeleteItem(handle(), index);
}
ListView::Row ListView::getRow(const int rowIndex) const
@@ -83,7 +83,7 @@ ListView::Row ListView::getRow(const int rowIndex) const
for(int i = 0; i < m_columnSize; i++) {
auto_char buf[4096];
- ListView_GetItemText(m_handle, rowIndex, i, buf, sizeof(buf));
+ ListView_GetItemText(handle(), rowIndex, i, buf, sizeof(buf));
row[i] = buf;
}
@@ -93,7 +93,7 @@ ListView::Row ListView::getRow(const int rowIndex) const
void ListView::clear()
{
for(int i = 0; i < m_rowSize; i++)
- ListView_DeleteItem(m_handle, 0);
+ ListView_DeleteItem(handle(), 0);
m_rowSize = 0;
}
@@ -105,7 +105,7 @@ bool ListView::hasSelection() const
int ListView::currentIndex() const
{
- return ListView_GetNextItem(m_handle, -1, LVNI_SELECTED);
+ return ListView_GetNextItem(handle(), -1, LVNI_SELECTED);
}
void ListView::onNotify(LPNMHDR info, LPARAM)
diff --git a/src/listview.hpp b/src/listview.hpp
@@ -18,18 +18,14 @@
#ifndef REAPACK_LISTVIEW_HPP
#define REAPACK_LISTVIEW_HPP
-#ifdef _WIN32
-#include <windows.h>
-#else
-#include <swell/swell.h>
-#endif
+#include "control.hpp"
#include <boost/signals2.hpp>
#include <vector>
#include "encoding.hpp"
-class ListView {
+class ListView : public Control {
public:
struct Column { auto_string text; int width; };
typedef std::vector<Column> Columns;
@@ -40,8 +36,6 @@ public:
ListView(const Columns &, HWND handle);
- HWND handle() const { return m_handle; }
-
int addRow(const Row &);
Row getRow(const int index) const;
void replaceRow(const int index, const Row &);
@@ -52,12 +46,13 @@ public:
int currentIndex() const;
void onSelect(const Callback &callback) { m_onSelect.connect(callback); }
- void onNotify(LPNMHDR, LPARAM);
+
+protected:
+ void onNotify(LPNMHDR, LPARAM) override;
private:
void addColumn(const Column &);
- HWND m_handle;
int m_columnSize;
int m_rowSize;
diff --git a/src/manager.cpp b/src/manager.cpp
@@ -33,11 +33,6 @@ Manager::Manager(ReaPack *reapack)
{
}
-Manager::~Manager()
-{
- delete m_list;
-}
-
void Manager::onInit()
{
// It would be better to not hard-code the column sizes like that...
@@ -47,11 +42,11 @@ void Manager::onInit()
const int URL_WIDTH = 300;
#endif
- m_list = new ListView({
+ m_list = createControl<ListView>(IDC_LIST, ListView::Columns{
{AUTO_STR("Name"), 110},
{AUTO_STR("URL"), URL_WIDTH},
{AUTO_STR("State"), 60},
- }, getControl(IDC_LIST));
+ });
}
void Manager::onCommand(const int id)
@@ -84,15 +79,6 @@ void Manager::onCommand(const int id)
}
}
-void Manager::onNotify(LPNMHDR info, LPARAM lParam)
-{
- switch(info->idFrom) {
- case IDC_LIST:
- m_list->onNotify(info, lParam);
- break;
- }
-}
-
void Manager::onContextMenu(HWND target, const int x, const int y)
{
if(target != m_list->handle())
diff --git a/src/manager.hpp b/src/manager.hpp
@@ -31,14 +31,12 @@ class Remote;
class Manager : public Dialog {
public:
Manager(ReaPack *);
- ~Manager();
void refresh();
protected:
void onInit() override;
void onCommand(int) override;
- void onNotify(LPNMHDR, LPARAM) override;
void onContextMenu(HWND, int x, int y) override;
private: