forked from OpenKotOR/mdledit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbead_winctrl.cpp
More file actions
159 lines (136 loc) · 5.65 KB
/
Copy pathbead_winctrl.cpp
File metadata and controls
159 lines (136 loc) · 5.65 KB
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
151
152
153
154
155
156
157
158
159
#include "bead_winctrl.h"
#include <stdexcept> // for standard exceptions
//#include <exception> //for std::exception
HTREEITEM TreeView_GetNthChild(HWND hwndTree, HTREEITEM htiParent, int nChild){
if(hwndTree == NULL || nChild < 0) return NULL;
HTREEITEM htiChild = (htiParent == NULL) ? TreeView_GetRoot(hwndTree) : TreeView_GetChild(hwndTree, htiParent);
for(int n = 0; n < nChild && htiChild != NULL; n++){
htiChild = TreeView_GetNextSibling(hwndTree, htiChild);
}
return htiChild;
}
void TreeView_DeleteAllChildren(HWND hwndTree, HTREEITEM htiParent){
if(hwndTree == NULL || htiParent == NULL) return;
HTREEITEM htiChild = TreeView_GetChild(hwndTree, htiParent);
while(htiChild != NULL){
if(!TreeView_DeleteItem(hwndTree, htiChild)) return;
htiChild = TreeView_GetChild(hwndTree, htiParent);
}
}
bool TreeView_ExpandAll(HWND hwndTV, HTREEITEM hItem, UINT flag){
if(hwndTV == NULL || hItem == NULL) return false;
HTREEITEM htiChild = TreeView_GetChild(hwndTV, hItem);
while(htiChild != NULL){
if(TreeView_GetChild(hwndTV, htiChild) != NULL){
if(!TreeView_ExpandAll(hwndTV, htiChild, flag))
return false;
}
htiChild = TreeView_GetNextSibling(hwndTV, htiChild);
}
if(!(TreeView_GetItemState(hwndTV, hItem, TVIS_EXPANDED) & TVIS_EXPANDED) && flag == TVE_COLLAPSE) return true;
return TreeView_Expand(hwndTV, hItem, flag);
}
namespace {
constexpr int kControlTextChars = 256;
std::string TrimControlText(const std::string & sBuffer){
const std::size_t nTerminator = sBuffer.find('\0');
return nTerminator == std::string::npos ? sBuffer : sBuffer.substr(0, nTerminator);
}
std::wstring TrimControlText(const std::wstring & sBuffer){
const std::size_t nTerminator = sBuffer.find(L'\0');
return nTerminator == std::wstring::npos ? sBuffer : sBuffer.substr(0, nTerminator);
}
}
HTREEITEM TreeView_GetChildByText(HWND hwndTree, HTREEITEM htiParent, const std::string & sText){
HTREEITEM htiChild = TreeView_GetChild(hwndTree, htiParent);
if(htiParent == NULL) htiChild = TreeView_GetRoot(hwndTree);
while(htiChild != NULL){
TVITEM tvi = {};
std::string sBuffer(kControlTextChars, '\0');
tvi.mask = TVIF_TEXT;
tvi.hItem = htiChild;
tvi.pszText = &sBuffer.front();
tvi.cchTextMax = static_cast<int>(sBuffer.size());
if(TreeView_GetItem(hwndTree, &tvi) && TrimControlText(sBuffer) == sText) return htiChild;
htiChild = TreeView_GetNextSibling(hwndTree, htiChild);
}
return NULL;
}
HTREEITEM TreeView_GetChildByText(HWND hwndTree, HTREEITEM htiParent, const std::wstring & sText){
HTREEITEM htiChild = TreeView_GetChild(hwndTree, htiParent);
if(htiParent == NULL) htiChild = TreeView_GetRoot(hwndTree);
while(htiChild != NULL){
TVITEMW tvi = {};
std::wstring sBuffer(kControlTextChars, L'\0');
tvi.mask = TVIF_TEXT;
tvi.hItem = htiChild;
tvi.pszText = &sBuffer.front();
tvi.cchTextMax = static_cast<int>(sBuffer.size());
if(TreeView_GetItem(hwndTree, &tvi) && TrimControlText(sBuffer) == sText) return htiChild;
htiChild = TreeView_GetNextSibling(hwndTree, htiChild);
}
return NULL;
}
int TabCtrl_GetTabIndexByText(HWND hwndTabs, const std::string & sText){
if(hwndTabs == NULL) return -1;
const int nTabs = TabCtrl_GetItemCount(hwndTabs);
for(int n = 0; n < nTabs; n++){
TCITEM tci = {};
std::string sBuffer(kControlTextChars, '\0');
tci.mask = TCIF_TEXT;
tci.pszText = &sBuffer.front();
tci.cchTextMax = static_cast<int>(sBuffer.size());
if(TabCtrl_GetItem(hwndTabs, n, &tci) && TrimControlText(sBuffer) == sText) return n;
}
return -1;
}
bool TabCtrl_AppendTab(HWND hTabControl, const std::string & sName){
if(hTabControl == NULL) return false;
int nTabs = TabCtrl_GetItemCount(hTabControl);
TCITEM tcAdd = {};
tcAdd.mask = TCIF_TEXT;
tcAdd.pszText = const_cast<char*>(sName.c_str());
tcAdd.cchTextMax = static_cast<int>(sName.length());
return (TabCtrl_InsertItem(hTabControl, nTabs, &tcAdd) != -1);
}
std::string TabCtrl_GetCurSelName(HWND hTabcontrol){
if(hTabcontrol == NULL) return std::string();
int nSel = TabCtrl_GetCurSel(hTabcontrol);
if(nSel == -1) return std::string();
TCITEM tcitem = {};
std::string sReturn(kControlTextChars, '\0');
tcitem.mask = TCIF_TEXT;
tcitem.pszText = &sReturn.front();
tcitem.cchTextMax = static_cast<int>(sReturn.size());
if(!TabCtrl_GetItem(hTabcontrol, nSel, &tcitem)) return std::string();
return TrimControlText(sReturn);
}
static int CALLBACK EnumFontFamExProc(ENUMLOGFONTEX* /*lpelfe*/, NEWTEXTMETRICEX* /*lpntme*/, int /*FontType*/, LPARAM lParam){
bool & bReturn = * (bool*) lParam;
bReturn = true;
return TRUE;
}
bool Font_IsInstalled(const std::string & sFont){
// Get the screen DC
HDC hdc = CreateCompatibleDC(NULL);
LOGFONT lf{};
// Any character set will do
lf.lfCharSet = DEFAULT_CHARSET;
// Set the facename to check for
if(sFont.length() > 31) throw std::length_error("The font name '" + sFont + "'is too long!\n");
const char * cFont = sFont.c_str();
char * cFace = lf.lfFaceName;
char * cFaceEnd = lf.lfFaceName + 32;
while(cFace != cFaceEnd && *cFont != '\0'){
*cFace = *cFont;
cFace++;
cFont++;
}
*cFace = '\0';
if(hdc == NULL) return false;
bool bReturn = false;
// Enumerate fonts
::EnumFontFamiliesEx(hdc, &lf, (FONTENUMPROC) EnumFontFamExProc, (LPARAM) &bReturn, 0);
DeleteDC(hdc);
return bReturn;
}