Skip to content

Commit

Permalink
コンボボックスのサブクラス化処理を簡素化する (#1463)
Browse files Browse the repository at this point in the history
* GetComboBoxInfo を使う

* SetWindowSubclass を使う

* SComboBoxItemDeleter を廃止

コンボボックスに CRecent を SetProp して直に紐づけ、コンボボックスのハンドルをサブクラスプロシージャに付属データとして渡す。
サブクラスプロシージャはコンボボックスのハンドルから GetProp して CRecent を取得する。

* レビュー指摘に対応: C-style キャストを使う

* レビュー指摘に対応: リストボックスのサブクラス化をやめる
  • Loading branch information
kengoide authored Nov 22, 2020
1 parent 93db5b2 commit 181b07c
Show file tree
Hide file tree
Showing 17 changed files with 27 additions and 167 deletions.
99 changes: 10 additions & 89 deletions sakura_core/dlg/CDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,8 +699,6 @@ void CDialog::GetItemClientRect( int wID, RECT& rc )
rc.bottom = po.y;
}

static const WCHAR* TSTR_SUBCOMBOBOXDATA = L"SubComboBoxData";

/*! コンボボックスのリストアイテムを関連付けられた履歴と共に削除する
*/
static void DeleteRecentItem(
Expand Down Expand Up @@ -747,109 +745,32 @@ static void DeleteRecentItem(
}
}

LRESULT CALLBACK SubEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
LRESULT CALLBACK SubEditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
SComboBoxItemDeleter* data = (SComboBoxItemDeleter*)::GetProp( hwnd, TSTR_SUBCOMBOBOXDATA );
HWND hwndCombo = GetParent(hwnd);
switch( uMsg ){
case WM_KEYDOWN:
{
if( wParam == VK_DELETE ){
HWND hwndCombo = data->hwndCombo;
BOOL bShow = Combo_GetDroppedState(hwndCombo);
int nIndex = Combo_GetCurSel(hwndCombo);
if( bShow && 0 <= nIndex ){
DeleteRecentItem(hwndCombo, nIndex, data->pRecent);
DeleteRecentItem(hwndCombo, nIndex, (CRecent*)dwRefData);
}
}
break;
}
case WM_DESTROY:
{
::SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)data->pEditWndProc);
::RemoveProp(hwnd, TSTR_SUBCOMBOBOXDATA);
data->pEditWndProc = NULL;
break;
}
default:
break;
}
return CallWindowProc(data->pEditWndProc, hwnd, uMsg, wParam, lParam);
return ::DefSubclassProc(hwnd, uMsg, wParam, lParam);
}

LRESULT CALLBACK SubListBoxProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
void CDialog::SetComboBoxDeleter(HWND hwndCtl, CRecent* pRecent)
{
SComboBoxItemDeleter* data = (SComboBoxItemDeleter*)::GetProp( hwnd, TSTR_SUBCOMBOBOXDATA );
switch( uMsg ){
case WM_KEYDOWN:
{
if( wParam == VK_DELETE ){
HWND hwndCombo = data->hwndCombo;
int nIndex = Combo_GetCurSel(hwndCombo);
if( 0 <= nIndex ){
DeleteRecentItem(hwndCombo, nIndex, data->pRecent);
return 0;
}
}
break;
}
case WM_DESTROY:
{
::SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)data->pListBoxWndProc);
::RemoveProp(hwnd, TSTR_SUBCOMBOBOXDATA);
data->pListBoxWndProc = NULL;
break;
}
default:
break;
}
return CallWindowProc(data->pListBoxWndProc, hwnd, uMsg, wParam, lParam);
}
assert(pRecent);

LRESULT CALLBACK SubComboBoxProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
SComboBoxItemDeleter* data = (SComboBoxItemDeleter*)::GetProp( hwnd, TSTR_SUBCOMBOBOXDATA );
switch( uMsg ){
case WM_CTLCOLOREDIT:
{
if( NULL == data->pEditWndProc ){
HWND hwndCtl = (HWND)lParam;
data->pEditWndProc = (WNDPROC)::GetWindowLongPtr(hwndCtl, GWLP_WNDPROC);
::SetProp(hwndCtl, TSTR_SUBCOMBOBOXDATA, data);
::SetWindowLongPtr(hwndCtl, GWLP_WNDPROC, (LONG_PTR)SubEditProc);
}
break;
}
case WM_CTLCOLORLISTBOX:
{
if( NULL == data->pListBoxWndProc ){
HWND hwndCtl = (HWND)lParam;
data->pListBoxWndProc = (WNDPROC)::GetWindowLongPtr(hwndCtl, GWLP_WNDPROC);
::SetProp(hwndCtl, TSTR_SUBCOMBOBOXDATA, data);
::SetWindowLongPtr(hwndCtl, GWLP_WNDPROC, (LONG_PTR)SubListBoxProc);
}
break;
}
case WM_DESTROY:
{
::SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)data->pComboBoxWndProc);
::RemoveProp(hwnd, TSTR_SUBCOMBOBOXDATA);
data->pComboBoxWndProc = NULL;
break;
}

default:
break;
}
return CallWindowProc(data->pComboBoxWndProc, hwnd, uMsg, wParam, lParam);
}

void CDialog::SetComboBoxDeleter( HWND hwndCtl, SComboBoxItemDeleter* data )
{
if( NULL == data->pRecent ){
COMBOBOXINFO info = { sizeof(COMBOBOXINFO) };
if (!::GetComboBoxInfo(hwndCtl, &info))
return;
}
data->hwndCombo = hwndCtl;
data->pComboBoxWndProc = (WNDPROC)::GetWindowLongPtr(hwndCtl, GWLP_WNDPROC);
::SetProp(hwndCtl, TSTR_SUBCOMBOBOXDATA, data);
::SetWindowLongPtr(hwndCtl, GWLP_WNDPROC, (LONG_PTR)SubComboBoxProc);
::SetWindowSubclass(info.hwndItem, SubEditProc, 0, (DWORD_PTR)pRecent);
}
12 changes: 1 addition & 11 deletions sakura_core/dlg/CDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,6 @@ struct SAnchorList
EAnchorStyle anchor;
};

struct SComboBoxItemDeleter
{
CRecent* pRecent;
HWND hwndCombo;
WNDPROC pComboBoxWndProc;
WNDPROC pEditWndProc;
WNDPROC pListBoxWndProc;
SComboBoxItemDeleter(): pRecent(NULL), hwndCombo(NULL), pComboBoxWndProc(NULL), pEditWndProc(NULL), pListBoxWndProc(NULL){}
};

/*-----------------------------------------------------------------------
クラスの宣言
-----------------------------------------------------------------------*/
Expand Down Expand Up @@ -125,7 +115,7 @@ class CDialog

void ResizeItem( HWND hTarget, const POINT& ptDlgDefalut, const POINT& ptDlgNew, const RECT& rcItemDefault, EAnchorStyle anchor, bool bUpdate = true);
void GetItemClientRect( int wID, RECT& rc );
static void SetComboBoxDeleter( HWND hwndCtl, SComboBoxItemDeleter* data );
static void SetComboBoxDeleter( HWND hwndCtl, CRecent* pRecent );
public:

static bool DirectoryUp(WCHAR* szDir);
Expand Down
9 changes: 2 additions & 7 deletions sakura_core/dlg/CDlgExec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,8 @@ BOOL CDlgExec::OnInitDialog( HWND hwnd, WPARAM wParam, LPARAM lParam )
}

BOOL bRet = CDialog::OnInitDialog(hwnd, wParam, lParam);

m_comboDel = SComboBoxItemDeleter();
m_comboDel.pRecent = &m_cRecentCmd;
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_m_szCommand), &m_comboDel);
m_comboDelCur = SComboBoxItemDeleter();
m_comboDelCur.pRecent = &m_cRecentCur;
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_CUR_DIR), &m_comboDelCur);
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_m_szCommand), &m_cRecentCmd);
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_CUR_DIR), &m_cRecentCur);
return bRet;
}

Expand Down
2 changes: 0 additions & 2 deletions sakura_core/dlg/CDlgExec.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ class CDlgExec final : public CDialog
bool m_bEditable; /* 編集ウィンドウへの入力可能 */ // 2009.02.21 ryoji

protected:
SComboBoxItemDeleter m_comboDel;
CRecentCmd m_cRecentCmd;
SComboBoxItemDeleter m_comboDelCur;
CRecentCurDir m_cRecentCur;

int GetData( void ) override; /* ダイアログデータの取得 */
Expand Down
4 changes: 1 addition & 3 deletions sakura_core/dlg/CDlgFind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ void CDlgFind::ChangeView( LPARAM pcEditView )
BOOL CDlgFind::OnInitDialog( HWND hwnd, WPARAM wParam, LPARAM lParam )
{
BOOL bRet = CDialog::OnInitDialog(hwnd, wParam, lParam);
m_comboDel = SComboBoxItemDeleter();
m_comboDel.pRecent = &m_cRecentSearch;
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_TEXT), &m_comboDel);
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_TEXT), &m_cRecentSearch);

// フォント設定 2012/11/27 Uchi
HFONT hFontOld = (HFONT)::SendMessageAny( GetItemHwnd( IDC_COMBO_TEXT ), WM_GETFONT, 0, 0 );
Expand Down
1 change: 0 additions & 1 deletion sakura_core/dlg/CDlgFind.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class CDlgFind final : public CDialog
CLogicPoint m_ptEscCaretPos_PHY; // 検索開始時のカーソル位置退避エリア

CRecentSearch m_cRecentSearch;
SComboBoxItemDeleter m_comboDel;
CFontAutoDeleter m_cFontText;

protected:
Expand Down
23 changes: 6 additions & 17 deletions sakura_core/dlg/CDlgGrep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,23 +331,12 @@ BOOL CDlgGrep::OnInitDialog( HWND hwndDlg, WPARAM wParam, LPARAM lParam )
g_pOnFolderProc = (WNDPROC)GetWindowLongPtr(hFolder, GWLP_WNDPROC);
SetWindowLongPtr(hFolder, GWLP_WNDPROC, (LONG_PTR)OnFolderProc);

m_comboDelText = SComboBoxItemDeleter();
m_comboDelText.pRecent = &m_cRecentSearch;
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_TEXT), &m_comboDelText);
m_comboDelFile = SComboBoxItemDeleter();
m_comboDelFile.pRecent = &m_cRecentGrepFile;
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_FILE), &m_comboDelFile);
m_comboDelFolder = SComboBoxItemDeleter();
m_comboDelFolder.pRecent = &m_cRecentGrepFolder;
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_FOLDER), &m_comboDelFolder);

m_comboDelExcludeFile = SComboBoxItemDeleter();
m_comboDelExcludeFile.pRecent = &m_cRecentExcludeFile;
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_EXCLUDE_FILE), &m_comboDelExcludeFile);

m_comboDelExcludeFolder = SComboBoxItemDeleter();
m_comboDelExcludeFolder.pRecent = &m_cRecentExcludeFolder;
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_EXCLUDE_FOLDER), &m_comboDelExcludeFolder);
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_TEXT), &m_cRecentSearch);
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_FILE), &m_cRecentGrepFile);
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_FOLDER), &m_cRecentGrepFolder);

SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_EXCLUDE_FILE), &m_cRecentExcludeFile);
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_EXCLUDE_FOLDER), &m_cRecentExcludeFolder);

BOOL bRet = CDialog::OnInitDialog( hwndDlg, wParam, lParam );
if( !bRet ) return bRet;
Expand Down
9 changes: 0 additions & 9 deletions sakura_core/dlg/CDlgGrep.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,10 @@ class CDlgGrep : public CDialog
SFilePathLong m_szExcludeFolder; //!< 除外フォルダ
SFilePath m_szCurrentFilePath;
protected:
SComboBoxItemDeleter m_comboDelText;
CRecentSearch m_cRecentSearch;

SComboBoxItemDeleter m_comboDelFile;
CRecentGrepFile m_cRecentGrepFile;

SComboBoxItemDeleter m_comboDelFolder;
CRecentGrepFolder m_cRecentGrepFolder;

SComboBoxItemDeleter m_comboDelExcludeFile;
CRecentExcludeFile m_cRecentExcludeFile;

SComboBoxItemDeleter m_comboDelExcludeFolder;
CRecentExcludeFolder m_cRecentExcludeFolder;

std::vector<CFontAutoDeleter> m_cFontDeleters;
Expand Down
4 changes: 1 addition & 3 deletions sakura_core/dlg/CDlgGrepReplace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ BOOL CDlgGrepReplace::OnInitDialog( HWND hwndDlg, WPARAM wParam, LPARAM lParam )
/* コンボボックスのユーザー インターフェイスを拡張インターフェースにする */
Combo_SetExtendedUI( GetItemHwnd( IDC_COMBO_TEXT2 ), TRUE );

m_comboDelText2 = SComboBoxItemDeleter();
m_comboDelText2.pRecent = &m_cRecentReplace;
SetComboBoxDeleter( GetItemHwnd( IDC_COMBO_TEXT2 ), &m_comboDelText2 );
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_TEXT2), &m_cRecentReplace);

BOOL bRet = CDlgGrep::OnInitDialog( hwndDlg, wParam, lParam );
if( !bRet ) return bRet;
Expand Down
1 change: 0 additions & 1 deletion sakura_core/dlg/CDlgGrepReplace.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class CDlgGrepReplace final : public CDlgGrep

protected:
CRecentReplace m_cRecentReplace;
SComboBoxItemDeleter m_comboDelText2;
CFontAutoDeleter m_cFontText2;

/*
Expand Down
10 changes: 2 additions & 8 deletions sakura_core/dlg/CDlgOpenFile_CommonFileDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ class CDlgOpenFileData{

bool m_bInitCodePage;

SComboBoxItemDeleter m_combDelFile;
CRecentFile m_cRecentFile;
SComboBoxItemDeleter m_combDelFolder;
CRecentFolder m_cRecentFolder;

OPENFILENAME* m_pOf;
Expand Down Expand Up @@ -373,12 +371,8 @@ UINT_PTR CALLBACK OFNHookProc(
/* ビューモードの初期値セット */
::CheckDlgButton( pData->m_hwndOpenDlg, chx1, pData->m_bViewMode );

pData->m_combDelFile = SComboBoxItemDeleter();
pData->m_combDelFile.pRecent = &pData->m_cRecentFile;
CDialog::SetComboBoxDeleter(pData->m_hwndComboMRU, &pData->m_combDelFile);
pData->m_combDelFolder = SComboBoxItemDeleter();
pData->m_combDelFolder.pRecent = &pData->m_cRecentFolder;
CDialog::SetComboBoxDeleter(pData->m_hwndComboOPENFOLDER, &pData->m_combDelFolder);
CDialog::SetComboBoxDeleter(pData->m_hwndComboMRU, &pData->m_cRecentFile);
CDialog::SetComboBoxDeleter(pData->m_hwndComboOPENFOLDER, &pData->m_cRecentFolder);
}
break;

Expand Down
8 changes: 2 additions & 6 deletions sakura_core/dlg/CDlgReplace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,8 @@ BOOL CDlgReplace::OnInitDialog( HWND hwndDlg, WPARAM wParam, LPARAM lParam )
::CheckDlgButton( GetHwnd(), IDC_RADIO_ALLAREA, TRUE );
}

m_comboDelText = SComboBoxItemDeleter();
m_comboDelText.pRecent = &m_cRecentSearch;
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_TEXT), &m_comboDelText);
m_comboDelText2 = SComboBoxItemDeleter();
m_comboDelText2.pRecent = &m_cRecentReplace;
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_TEXT2), &m_comboDelText2);
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_TEXT), &m_cRecentSearch);
SetComboBoxDeleter(GetItemHwnd(IDC_COMBO_TEXT2), &m_cRecentReplace);

BOOL bRet = CDialog::OnInitDialog( hwndDlg, wParam, lParam );
if( !bRet ) return bRet;
Expand Down
2 changes: 0 additions & 2 deletions sakura_core/dlg/CDlgReplace.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ class CDlgReplace final : public CDialog

protected:
CRecentSearch m_cRecentSearch;
SComboBoxItemDeleter m_comboDelText;
CRecentReplace m_cRecentReplace;
SComboBoxItemDeleter m_comboDelText2;
CFontAutoDeleter m_cFontText;
CFontAutoDeleter m_cFontText2;

Expand Down
4 changes: 1 addition & 3 deletions sakura_core/dlg/CDlgTagJumpList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,7 @@ BOOL CDlgTagJumpList::OnInitDialog( HWND hwndDlg, WPARAM wParam, LPARAM lParam )
bRet = FALSE; //for set focus
}

m_comboDel = SComboBoxItemDeleter();
m_comboDel.pRecent = &m_cRecentKeyword;
SetComboBoxDeleter(hwndKey, &m_comboDel);
SetComboBoxDeleter(hwndKey, &m_cRecentKeyword);

/* 基底クラスメンバ */
CDialog::OnInitDialog( GetHwnd(), wParam, lParam );
Expand Down
1 change: 0 additions & 1 deletion sakura_core/dlg/CDlgTagJumpList.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ class CDlgTagJumpList final : public CDialog
BOOL m_bOldTagJumpICase; //!< 前回の大文字小文字を同一視
BOOL m_bOldTagJumpPartialMatch; //!< 前回の文字列の途中にマッチ

SComboBoxItemDeleter m_comboDel;
CRecentTagjumpKeyword m_cRecentKeyword;

POINT m_ptDefaultSize;
Expand Down
4 changes: 1 addition & 3 deletions sakura_core/window/CMainToolBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,7 @@ void CMainToolBar::CreateToolBar( void )
//検索ボックスを更新 // 関数化 2010/6/6 Uchi
AcceptSharedSearchKey();

m_comboDel = SComboBoxItemDeleter(); // 再表示用の初期化
m_comboDel.pRecent = &m_cRecentSearch;
CDialog::SetComboBoxDeleter(m_hwndSearchBox, &m_comboDel);
CDialog::SetComboBoxDeleter(m_hwndSearchBox, &m_cRecentSearch);

// コンボボックスの位置と幅を調整する
CMyRect rcCombo;
Expand Down
1 change: 0 additions & 1 deletion sakura_core/window/CMainToolBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ class CMainToolBar{
//フォント
HFONT m_hFontSearchBox; //!< 検索コンボボックスのフォント

SComboBoxItemDeleter m_comboDel;
CRecentSearch m_cRecentSearch;
CImageListMgr* m_pcIcons;
};
Expand Down

0 comments on commit 181b07c

Please sign in to comment.