Skip to content

Commit

Permalink
Refactor includes and improved code style
Browse files Browse the repository at this point in the history
  • Loading branch information
funap committed Feb 5, 2024
1 parent e3bae58 commit b24bd5c
Show file tree
Hide file tree
Showing 49 changed files with 6,529 additions and 6,870 deletions.
187 changes: 87 additions & 100 deletions src/Explorer/ComboOrgi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,164 +19,151 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

#include "ComboOrgi.h"

#include "ExplorerResource.h"


#include <utility>

ComboOrgi::ComboOrgi()
: _hCombo(nullptr)
, _hDefaultComboProc(nullptr)
, _hParent(nullptr)
, _onCharHandler(nullptr)
: _hCombo(nullptr)
, _hDefaultComboProc(nullptr)
, _hParent(nullptr)
, _onCharHandler(nullptr)
{
_comboItems.clear();
_comboItems.clear();
}

ComboOrgi::~ComboOrgi()
{
_comboItems.clear();
_comboItems.clear();
}

void ComboOrgi::init(HWND hCombo, HWND parent)
{
_hCombo = hCombo;
_hParent = parent;
_hCombo = hCombo;
_hParent = parent;

/* subclass combo to get edit messages */
COMBOBOXINFO comboBoxInfo;
comboBoxInfo.cbSize = sizeof(COMBOBOXINFO);
/* subclass combo to get edit messages */
COMBOBOXINFO comboBoxInfo {
.cbSize = sizeof(COMBOBOXINFO)
};

::SendMessage(_hCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)&comboBoxInfo);
::SetWindowLongPtr(comboBoxInfo.hwndItem, GWLP_USERDATA, (LONG_PTR)this);
_hDefaultComboProc = reinterpret_cast<WNDPROC>(::SetWindowLongPtr(comboBoxInfo.hwndItem, GWLP_WNDPROC, (LONG_PTR)wndDefaultProc));
::SendMessage(_hCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)&comboBoxInfo);
::SetWindowLongPtr(comboBoxInfo.hwndItem, GWLP_USERDATA, (LONG_PTR)this);
_hDefaultComboProc = reinterpret_cast<WNDPROC>(::SetWindowLongPtr(comboBoxInfo.hwndItem, GWLP_WNDPROC, (LONG_PTR)wndDefaultProc));
}

LRESULT ComboOrgi::runProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch (Message)
{
switch (Message) {
case WM_CHAR:
if (_onCharHandler) {
BOOL handled = _onCharHandler(static_cast<UINT>(wParam), LOWORD(lParam), HIWORD(lParam));
if (handled) {
if (handled != 0) {
return TRUE;
}
}
break;
case WM_KEYUP:
{
switch (wParam) {
case VK_RETURN: {
TCHAR pszText[MAX_PATH];

getText(pszText);
addText(pszText);
::PostMessage(_hParent, WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(_hCombo), CBN_SELCHANGE), reinterpret_cast<LPARAM>(_hCombo));
return TRUE;
}
default:
break;
}
break;
}
default :
break;
}
return ::CallWindowProc(_hDefaultComboProc, hwnd, Message, wParam, lParam);
case WM_KEYUP:
switch (wParam) {
case VK_RETURN: {
WCHAR pszText[MAX_PATH];
getText(pszText);
addText(pszText);
::PostMessage(_hParent, WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(_hCombo), CBN_SELCHANGE), reinterpret_cast<LPARAM>(_hCombo));
return TRUE;
}
default:
break;
}
break;
default :
break;
}
return ::CallWindowProc(_hDefaultComboProc, hwnd, Message, wParam, lParam);
}


void ComboOrgi::addText(LPCTSTR pszText)
{
/* find item */
INT count = (INT)_comboItems.size();
INT i = 0;
INT hasFoundOn = -1;

for (; i < count; i++)
{
if (_tcscmp(pszText, _comboItems[i].c_str()) == 0)
{
hasFoundOn = count - i - 1;
}
}

/* item missed create new and select it correct */
if (hasFoundOn == -1)
{
_comboItems.push_back(pszText);

::SendMessage(_hCombo, CB_RESETCONTENT, 0, 0);
for (i = count; i >= 0 ; --i)
{
::SendMessage(_hCombo, CB_ADDSTRING, MAX_PATH, (LPARAM)_comboItems[i].c_str());
}
}
selectComboText(pszText);
/* find item */
INT count = (INT)_comboItems.size();
bool found = false;

for (int i = 0; i < count; i++) {
if (_comboItems[i] == pszText) {
found = true;
break;
}
}

/* item missed create new and select it correct */
if (!found) {
_comboItems.push_back(pszText);

::SendMessage(_hCombo, CB_RESETCONTENT, 0, 0);
for (int i = count; i >= 0 ; --i) {
::SendMessage(_hCombo, CB_ADDSTRING, 0, (LPARAM)_comboItems[i].c_str());
}
}
selectComboText(pszText);
}


void ComboOrgi::setText(LPCTSTR pszText, UINT size)
{
::SendMessage(_hCombo, WM_SETTEXT, size, (LPARAM)pszText);
::SendMessage(_hCombo, WM_SETTEXT, size, (LPARAM)pszText);
}

void ComboOrgi::getText(LPTSTR pszText, UINT size)
{
::SendMessage(_hCombo, WM_GETTEXT, size, (LPARAM)pszText);
::SendMessage(_hCombo, WM_GETTEXT, size, (LPARAM)pszText);
}

bool ComboOrgi::getSelText(LPTSTR pszText)
{
INT curSel = (INT)::SendMessage(_hCombo, CB_GETCURSEL, 0, 0);

if (curSel != CB_ERR)
{
if (MAX_PATH > ::SendMessage(_hCombo, CB_GETLBTEXTLEN, curSel, 0))
{
::SendMessage(_hCombo, CB_GETLBTEXT, curSel, (LPARAM)pszText);
return true;
}
}
return false;
INT curSel = (INT)::SendMessage(_hCombo, CB_GETCURSEL, 0, 0);

if (curSel != CB_ERR) {
if (MAX_PATH > ::SendMessage(_hCombo, CB_GETLBTEXTLEN, curSel, 0)) {
::SendMessage(_hCombo, CB_GETLBTEXT, curSel, (LPARAM)pszText);
return true;
}
}
return false;
}

void ComboOrgi::setDefaultOnCharHandler(std::function<BOOL(UINT, UINT, UINT)> onCharHandler)
{
_onCharHandler = onCharHandler;
_onCharHandler = std::move(onCharHandler);
}

void ComboOrgi::selectComboText(LPCTSTR pszText)
{
LRESULT lResult = -1;
LRESULT lResult = -1;

lResult = ::SendMessage(_hCombo, CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)pszText);
::SendMessage(_hCombo, CB_SETCURSEL, lResult, 0);
lResult = ::SendMessage(_hCombo, CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)pszText);
::SendMessage(_hCombo, CB_SETCURSEL, lResult, 0);
}

void ComboOrgi::setComboList(const std::vector<std::wstring> &vStrList)
{
SIZE_T iCnt = vStrList.size();
SIZE_T iCnt = vStrList.size();

::SendMessage(_hCombo, CB_RESETCONTENT, 0, 0);
for (SIZE_T i = 0; i < iCnt; i++)
{
addText((LPTSTR)vStrList[i].c_str());
}
::SendMessage(_hCombo, CB_RESETCONTENT, 0, 0);
for (SIZE_T i = 0; i < iCnt; i++) {
addText((LPTSTR)vStrList[i].c_str());
}
}

std::vector<std::wstring> ComboOrgi::getComboList()
{
std::vector<std::wstring> result;

TCHAR szTemp[MAX_PATH] = {};
SIZE_T count = ::SendMessage(_hCombo, CB_GETCOUNT, 0, 0);
for (SIZE_T i = 0; i < count; ++i) {
if (MAX_PATH > ::SendMessage(_hCombo, CB_GETLBTEXTLEN, i, 0)) {
::SendMessage(_hCombo, CB_GETLBTEXT, i, (LPARAM)szTemp);
result.emplace_back(szTemp);
}
}

return result;
std::vector<std::wstring> result;
WCHAR szTemp[MAX_PATH] = {};
SIZE_T count = ::SendMessage(_hCombo, CB_GETCOUNT, 0, 0);
for (SIZE_T i = 0; i < count; ++i) {
if (MAX_PATH > ::SendMessage(_hCombo, CB_GETLBTEXTLEN, i, 0)) {
::SendMessage(_hCombo, CB_GETLBTEXT, i, (LPARAM)szTemp);
result.emplace_back(szTemp);
}
}

return result;
}
75 changes: 28 additions & 47 deletions src/Explorer/ComboOrgi.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,61 +26,42 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <vector>
#include <functional>

#ifndef CB_GETCOMBOBOXINFO
#define CB_GETCOMBOBOXINFO 0x0164
#endif

#if(WINVER <= 0x0400)
struct COMBOBOXINFO
{
int cbSize;
RECT rcItem;
RECT rcButton;
DWORD stateButton;
HWND hwndCombo;
HWND hwndItem;
HWND hwndList;
};
#endif

class ComboOrgi
{
public :
ComboOrgi();
~ComboOrgi ();
virtual void init(HWND hCombo, HWND parent);
virtual void destroy() {
};
ComboOrgi();
~ComboOrgi();
virtual void init(HWND hCombo, HWND parent);
virtual void destroy() {
};

void addText(LPCTSTR pszText);
void setText(LPCTSTR pszText, UINT size = MAX_PATH);
void getText(LPTSTR pszText, UINT size = MAX_PATH);
bool getSelText(LPTSTR pszText);
void addText(LPCTSTR pszText);
void setText(LPCTSTR pszText, UINT size = MAX_PATH);
void getText(LPTSTR pszText, UINT size = MAX_PATH);
bool getSelText(LPTSTR pszText);

void setComboList(const std::vector<std::wstring> &vStrList);
std::vector<std::wstring> getComboList();
void setComboList(const std::vector<std::wstring> &vStrList);
std::vector<std::wstring> getComboList();

void clearComboList(void)
{
_comboItems.clear();
};
void clearComboList()
{
_comboItems.clear();
};

void setDefaultOnCharHandler(std::function<BOOL(UINT /* nChar */, UINT /* nRepCnt */, UINT /* nFlags */)> onCharHandler);
void setDefaultOnCharHandler(std::function<BOOL(UINT /* nChar */, UINT /* nRepCnt */, UINT /* nFlags */)> onCharHandler);

private:
void selectComboText(LPCTSTR pszText);

private :
HWND _hCombo;
WNDPROC _hDefaultComboProc;
HWND _hParent;

std::wstring _currData;
std::vector<std::wstring> _comboItems;
std::function<BOOL(UINT /* nChar */, UINT /* nRepCnt */, UINT /* nFlags */)> _onCharHandler;
void selectComboText(LPCTSTR pszText);
LRESULT runProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK wndDefaultProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
return (((ComboOrgi *)(::GetWindowLongPtr(hwnd, GWLP_USERDATA)))->runProc(hwnd, Message, wParam, lParam));
};

HWND _hCombo;
WNDPROC _hDefaultComboProc;
HWND _hParent;
std::wstring _currData;
std::vector<std::wstring> _comboItems;
std::function<BOOL(UINT /* nChar */, UINT /* nRepCnt */, UINT /* nFlags */)> _onCharHandler;

LRESULT runProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK wndDefaultProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
return (((ComboOrgi *)(::GetWindowLongPtr(hwnd, GWLP_USERDATA)))->runProc(hwnd, Message, wParam, lParam));
};
};
Loading

0 comments on commit b24bd5c

Please sign in to comment.