1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
// CheckBoxListDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FRED.h"
#include "CheckBoxListDlg.h"
#include "cfile/cfile.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CheckBoxListDlg dialog
CheckBoxListDlg::CheckBoxListDlg(CWnd* pParent /*=NULL*/)
: CDialog(CheckBoxListDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CheckBoxListDlg)
//}}AFX_DATA_INIT
m_offline_options.clear();
m_caption = _T("");
}
void CheckBoxListDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CheckBoxListDlg)
DDX_Control(pDX, IDC_EDIT1, m_checklist);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CheckBoxListDlg, CDialog)
//{{AFX_MSG_MAP(CheckBoxListDlg)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CheckBoxListDlg message handlers
BOOL CheckBoxListDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// fix overlapping checkboxes issue
// https://stackoverflow.com/questions/57951333/cchecklistbox-items-get-overlapped-on-selection-if-app-build-using-visual-studi
m_checklist.SetFont(GetFont());
if (!m_offline_options.empty())
SetOptions(m_offline_options);
if (!m_caption.IsEmpty())
SetCaption(m_caption);
return TRUE;
}
void CheckBoxListDlg::OnClose()
{
UpdateData(TRUE);
for (int i = 0; i < m_checklist.GetCount(); i++)
m_offline_options[i].second = m_checklist.GetCheck(i) != BST_UNCHECKED;
CDialog::OnClose();
}
void CheckBoxListDlg::SetOptions(const SCP_vector<CString> &options)
{
if (IsWindow(m_hWnd))
{
m_checklist.ResetContent();
for (const auto& option : options)
m_checklist.AddString((LPCTSTR)option);
UpdateData(FALSE);
}
else
{
m_offline_options.clear();
for (const auto& option : options)
m_offline_options.emplace_back(option, false);
}
}
void CheckBoxListDlg::SetOptions(const SCP_vector<std::pair<CString, bool>> &options)
{
if (IsWindow(m_hWnd))
{
m_checklist.ResetContent();
for (const auto& option : options)
{
m_checklist.AddString((LPCTSTR)option.first);
m_checklist.SetCheck(m_checklist.GetCount() - 1, option.second ? BST_CHECKED : BST_UNCHECKED);
}
UpdateData(FALSE);
}
else
m_offline_options = options;
}
bool CheckBoxListDlg::IsChecked(int index)
{
if (IsWindow(m_hWnd))
{
if (index < 0 || index >= m_checklist.GetCount())
return FALSE;
UpdateData(TRUE);
return m_checklist.GetCheck(index) != BST_UNCHECKED;
}
else
{
if (index < 0 || index >= (int)m_offline_options.size())
return FALSE;
return m_offline_options[index].second;
}
}
void CheckBoxListDlg::SetChecked(int index, bool checked)
{
if (IsWindow(m_hWnd))
{
if (index < 0 || index >= m_checklist.GetCount())
return;
m_checklist.SetCheck(index, checked ? BST_CHECKED : BST_UNCHECKED);
UpdateData(FALSE);
}
else
{
if (index < 0 || index >= (int)m_offline_options.size())
return;
m_offline_options[index].second = checked;
}
}
void CheckBoxListDlg::SetCaption(const CString &caption)
{
m_caption = caption;
if (IsWindow(m_hWnd))
SetWindowText(m_caption);
}
|