-
Notifications
You must be signed in to change notification settings - Fork 1
/
codeviewedit.cpp
519 lines (490 loc) · 16.8 KB
/
codeviewedit.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include "stdafx.h"
#include "codeviewedit.h"
UserData::UserData()
{
LineNum = 0;
eTyping = eUnknown;
}
UserData::UserData(const int LineNo, const string &Desc, const string &Name, const WordType TypeIn)
{
LineNum = LineNo;
Description = Desc;
KeyName = Name;
eTyping = TypeIn;
}
UserData::~UserData()
{
}
/* FindUD - Now a human Search!
0 =Found & UDiterOut set to point at UD in list.
-1 =Not Found
1 =Not Found
-2 =Zero Length string or error*/
int UserData::FindUD(vector<UserData>* ListIn, string &strIn, vector<UserData>::iterator& UDiterOut, int &Pos)
{
int result = -2;
RemovePadding(strIn);
if (strIn.size() == 0 || (!ListIn) || ListIn->size() == 0) return -2;
Pos = -1;
const int KeyResult = FindUDbyKey(ListIn, strIn, UDiterOut, Pos);
//If it's a top level construct it will have no parents and therefore have a unique key.
if (KeyResult == 0) return 0;
//Now see if it's in the Name list
//Jumpdelta should be intalised to the maximum count of am individual KeyName
//But for the momment the biggest is 64 x's in AMH
int iNewPos = Pos + KeyResult; //Start Very close to the result of key search
if (iNewPos < 0) iNewPos = 0;
//Find the start of other instances of strIn by crawling up list
//Usually (but not always) FindUDbyKey returns top of the list so its fast
const string strSearchData = lowerCase(strIn);
const size_t SearchWidth = strSearchData.size();
while (true)
{
iNewPos--;
if (iNewPos < 0) break;
const string strTableData = lowerCase(ListIn->at(iNewPos).UniqueKey).substr(0, SearchWidth);
if (strSearchData.compare(strTableData) != 0) break;
}
++iNewPos;
// now walk down list of Keynames looking for what we want.
while (true)
{
string strTableData = lowerCase(ListIn->at(iNewPos).KeyName);
result = strSearchData.compare(strTableData);
if (result == 0) break; //Found
++iNewPos;
if (iNewPos == ListIn->size()) break;
strTableData = lowerCase(ListIn->at(iNewPos).KeyName).substr(0, SearchWidth);
result = strSearchData.compare(strTableData);
if (result != 0) break; //EO SubList
}
UDiterOut = ListIn->begin() + iNewPos;
Pos = iNewPos;
return result;
}
//Finds the closest UD from CurrentLine in ListIn
//On entry CurrentIdx must be set to the UD in the line
int UserData::FindClosestUD(vector<UserData>* ListIn, const int CurrentLine, const int CurrentIdx)
{
const string strSearchData = lowerCase(ListIn->at(CurrentIdx).KeyName);
const size_t SearchWidth = strSearchData.size();
//Find the start of other instances of strIn by crawling up list
int iNewPos = CurrentIdx;
while (true)
{
iNewPos--;
if (iNewPos < 0) break;
const string strTableData = lowerCase(ListIn->at(iNewPos).UniqueKey).substr(0, SearchWidth);
if (strSearchData.compare(strTableData) != 0) break;
}
++iNewPos;
//Now at top of list
//find nearest definition above current line
int ClosestLineNum = 0;
int ClosestPos = CurrentIdx;
int Delta = -(INT_MAX - 1);
while (true)
{
const int NewLineNum = ListIn->at(iNewPos).LineNum;
const int NewDelta = NewLineNum - CurrentLine;
if (NewDelta >= Delta && NewLineNum <= CurrentLine)
{
if (lowerCase(ListIn->at(iNewPos).KeyName).compare(strSearchData) == 0)
{
Delta = NewDelta;
ClosestLineNum = NewLineNum;
ClosestPos = iNewPos;
}
}
++iNewPos;
if (iNewPos == ListIn->size()) break;
const string strTableData = lowerCase(ListIn->at(iNewPos).KeyName).substr(0, SearchWidth);
if (strSearchData.compare(strTableData) != 0) break;
}
--iNewPos;
return ClosestPos;
}
int UserData::FindUDbyKey(vector<UserData>* ListIn, const string &strIn, vector<UserData>::iterator &UDiterOut, int &PosOut)
{
int result = -2;
if (ListIn && (ListIn->size() > 0) && (strIn.size() > 0))// Sanity chq.
{
const unsigned int ListSize = (int)ListIn->size();
UINT32 iCurPos = (ListSize >> 1);
int iNewPos = 1u << 30;
while ((!(iNewPos & ListSize)) && (iNewPos > 1))
{
iNewPos >>= 1;
}
int iJumpDelta = ((iNewPos) >> 1);
--iNewPos;//Zero Base
const string strSearchData = lowerCase(strIn);
while (true)
{
iCurPos = iNewPos;
if (iCurPos >= ListSize) { result = -1; }
else
{
const string strTableData = lowerCase(ListIn->at(iCurPos).UniqueKey);
result = strSearchData.compare(strTableData);
}
if (iJumpDelta == 0 || result == 0) break;
if (result < 0) { iNewPos = iCurPos - iJumpDelta; }
else { iNewPos = iCurPos + iJumpDelta; }
iJumpDelta >>= 1;
}
UDiterOut = ListIn->begin() + iCurPos;
PosOut = iCurPos;
}
return result;
}
//Returns current Index of strIn in ListIn based on UniqueKey, or -1 if not found
int UserData::UDKeyIndex(vector<UserData>* ListIn, const string &strIn)
{
if ((!ListIn) || (ListIn->size() == 0) || (strIn.size() == 0)) return -1;
int result = -2;
const unsigned int ListSize = (int)ListIn->size();
UINT32 iCurPos = (ListSize >> 1);
UINT32 iNewPos = 1u << 30;
while ((!(iNewPos & ListSize)) && (iNewPos > 1))
{
iNewPos >>= 1;
}
int iJumpDelta = ((iNewPos) >> 1);
--iNewPos;//Zero Base
const string strSearchData = lowerCase(strIn);
while (true)
{
iCurPos = iNewPos;
if (iCurPos >= ListSize) { result = -1; }
else
{
const string strTableData = lowerCase(ListIn->at(iCurPos).UniqueKey);
result = strSearchData.compare(strTableData);
}
if (iJumpDelta == 0 || result == 0) break;
if (result < 0) { iNewPos = iCurPos - iJumpDelta; }
else { iNewPos = iCurPos + iJumpDelta; }
iJumpDelta >>= 1;
}
///TODO: neads to consider children?
if (result == 0)
return iCurPos;
else
return -1;
}
//Returns current Index of strIn in ListIn based on KeyName, or -1 if not found
int UserData::UDIndex(vector<UserData>* ListIn, const string &strIn)
{
if ((!ListIn) || (ListIn->size() == 0) || (strIn.size() == 0)) return -1;
int result = -2;
const unsigned int ListSize = (int)ListIn->size();
UINT32 iCurPos = (ListSize >> 1);
UINT32 iNewPos = 1u << 30;
while ((!(iNewPos & ListSize)) && (iNewPos > 1))
{
iNewPos >>= 1;
}
int iJumpDelta = ((iNewPos) >> 1);
--iNewPos;//Zero Base
const string strSearchData = lowerCase(strIn);
while (true)
{
iCurPos = iNewPos;
if (iCurPos >= ListSize) { result = -1; }
else
{
const string strTableData = lowerCase(ListIn->at(iCurPos).KeyName);
result = strSearchData.compare(strTableData);
}
if (iJumpDelta == 0 || result == 0) break;
if (result < 0) { iNewPos = iCurPos - iJumpDelta; }
else { iNewPos = iCurPos + iJumpDelta; }
iJumpDelta >>= 1;
}
///TODO: neads to consider children?
if (result == 0)
return iCurPos;
else
return -1;
}
//Needs speeding up.
UserData UserData::GetUDfromUniqueKey(vector<UserData>* ListIn, const string &UniKey)
{
UserData RetVal;
RetVal.eTyping = eUnknown;
size_t i = 0;
const size_t ListSize = ListIn->size();
while ((RetVal.eTyping == eUnknown) && (i < ListSize))
{
if (UniKey == ListIn->at(i).UniqueKey)
{
RetVal = ListIn->at(i);
}
++i;
}
return RetVal;
}
//TODO: Needs speeding up.
size_t UserData::GetUDPointerfromUniqueKey(vector<UserData>* ListIn, const string &UniKey)
{
size_t i = 0;
const size_t ListSize = ListIn->size();
while (i < ListSize)
{
if (UniKey == ListIn->at(i).UniqueKey)
{
return i;
}
++i;
}
return -1;
}
//Assumes case insensitive sorted list
//Returns index or insertion point (-1 == error)
size_t UserData::FindOrInsertUD(vector<UserData>* ListIn, UserData &udIn)
{
if (ListIn->size() == 0) //First in
{
ListIn->push_back(udIn);
return 0;
}
vector<UserData>::iterator iterFound = ListIn->begin();
int Pos = 0;
const int KeyFound = FindUDbyKey(ListIn, udIn.UniqueKey, iterFound, Pos);
if (KeyFound == 0)
{
//Same name, different parents.
const int ParentResult = udIn.UniqueParent.compare(iterFound->UniqueParent);
if (ParentResult == -1)
{
ListIn->insert(iterFound, udIn);
}
else if (ParentResult == 1)
{
++iterFound;
++Pos;
ListIn->insert(iterFound, udIn);
}
return Pos;
}
if (KeyFound == -1) //insert before, somewhere in the middle
{
ListIn->insert(iterFound, udIn);
return Pos;
}
else
{
if (KeyFound == 1)//insert Above last element - Special case
{
++iterFound;
ListIn->insert(iterFound, udIn);
return ++Pos;
}
else
if (iterFound == (ListIn->end() - 1))
{//insert at end
ListIn->push_back(udIn);
return ListIn->size() - 1;//Zero Base
}
}
return -1;
}
bool UserData::FindOrInsertStringIntoAutolist(vector<string>* ListIn, const string &strIn)
{
//First in the list
if (ListIn->empty())
{
ListIn->push_back(strIn);
return true;
}
vector<string>::iterator i = ListIn->begin();
int result = -2;
const unsigned int ListSize = (unsigned int)ListIn->size();
UINT32 iCurPos = (ListSize >> 1);
UINT32 iNewPos = 1u << 31;
while ((!(iNewPos & ListSize)) && (iNewPos > 1))
{
iNewPos >>= 1;
}
int iJumpDelta = ((iNewPos) >> 1);
--iNewPos;//Zero Base
const string strSearchData = lowerCase(strIn);
while (true)
{
iCurPos = iNewPos;
if (iCurPos >= ListSize) { result = -1; }
else
{
const string strTableData = lowerCase(ListIn->at(iCurPos));
result = strSearchData.compare(strTableData);
}
if (iJumpDelta == 0 || result == 0) break;
if (result < 0) { iNewPos = iCurPos - iJumpDelta; }
else { iNewPos = iCurPos + iJumpDelta; }
iJumpDelta >>= 1;
}
i = ListIn->begin() + iCurPos;
if (result == 0) return false; // Already in list.
if (result == -1) //insert before, somewhere in the middle
{
ListIn->insert(i, strIn);
return true;
}
if (i == (ListIn->end() - 1))//insert Above last element - Special case
{
ListIn->push_back(strIn);
return true;
}
if (result == 1)
{
++i;
ListIn->insert(i, strIn);
return true;
}
if (i == (ListIn->end() - 1))//insert Above last element - Special case
{//insert at end
ListIn->push_back(strIn);
return true;
}
return false;//Oh pop poop, never should hit here.
}
////////////////Preferences
CVPrefrence::CVPrefrence()
{
szControlName = nullptr;
rgb = 0;
Highlight = false;
szRegName = nullptr;
SciKeywordID = 0;
}
CVPrefrence* CVPrefrence::FillCVPreference(
const char* szCtrlNameIn, const COLORREF crTextColor,
const bool bDisplay, const char* szRegistryName,
const int szScintillaKeyword, const int IDC_ChkBox,
const int IDC_ColorBut, const int IDC_Font)
{
szControlName = szCtrlNameIn;
rgb = crTextColor;
Highlight = bDisplay;
szRegName = szRegistryName;
SciKeywordID = szScintillaKeyword;
IDC_ChkBox_code = IDC_ChkBox;
IDC_ColorBut_code = IDC_ColorBut;
IDC_Font_code = IDC_Font;
return (CVPrefrence *)this;
}
void CVPrefrence::SetCheckBox(const HWND hwndDlg)
{
const HWND hChkBox = GetDlgItem(hwndDlg, this->IDC_ChkBox_code);
SNDMSG(hChkBox, BM_SETCHECK, this->Highlight ? BST_CHECKED : BST_UNCHECKED, 0L);
}
void CVPrefrence::ReadCheckBox(const HWND hwndDlg)
{
this->Highlight = !!IsDlgButtonChecked(hwndDlg, this->IDC_ChkBox_code);
}
void CVPrefrence::GetPrefsFromReg()
{
char RegEntry[33] = {};
strcpy_s(RegEntry, this->szRegName);
this->Highlight = LoadValueBoolWithDefault("CVEdit", RegEntry, this->Highlight);
ZeroMemory(RegEntry, 33);
strcpy_s(RegEntry, this->szRegName);
strcat_s(RegEntry, "_color");
this->rgb = LoadValueIntWithDefault("CVEdit", RegEntry, this->rgb);
ZeroMemory(RegEntry, 33);
strcpy_s(RegEntry, this->szRegName);
strcat_s(RegEntry, "_FontPointSize");
this->PointSize = LoadValueIntWithDefault("CVEdit", RegEntry, this->PointSize);
ZeroMemory(RegEntry, 33);
strcpy_s(RegEntry, this->szRegName);
strcat_s(RegEntry, "_Font");
char bakupFaceName[LF_FACESIZE]; // to save the default font name, in case the corresponding registry entry is empty
strcpy_s(bakupFaceName, this->LogFont.lfFaceName);
if (LoadValueString("CVEdit", RegEntry, this->LogFont.lfFaceName, LF_FACESIZE) != S_OK)
strcpy_s(this->LogFont.lfFaceName, bakupFaceName);
ZeroMemory(RegEntry, 33);
strcpy_s(RegEntry, this->szRegName);
strcat_s(RegEntry, "_FontWeight");
this->LogFont.lfWeight = LoadValueIntWithDefault("CVEdit", RegEntry, this->LogFont.lfWeight);
ZeroMemory(RegEntry, 33);
strcpy_s(RegEntry, this->szRegName);
strcat_s(RegEntry, "_FontItalic");
this->LogFont.lfItalic = LoadValueIntWithDefault("CVEdit", RegEntry, this->LogFont.lfItalic);
ZeroMemory(RegEntry, 33);
strcpy_s(RegEntry, this->szRegName);
strcat_s(RegEntry, "_FontUnderline");
this->LogFont.lfUnderline = LoadValueIntWithDefault("CVEdit", RegEntry, this->LogFont.lfUnderline);
ZeroMemory(RegEntry, 33);
strcpy_s(RegEntry, this->szRegName);
strcat_s(RegEntry, "_FontStrike");
this->LogFont.lfStrikeOut = LoadValueIntWithDefault("CVEdit", RegEntry, this->LogFont.lfStrikeOut);
}
void CVPrefrence::SetPrefsToReg()
{
char RegEntry[33] = {};
strcpy_s(RegEntry, this->szRegName);
SaveValueBool("CVEdit", RegEntry, this->Highlight);
ZeroMemory(RegEntry, 33);
strcpy_s(RegEntry, this->szRegName);
strcat_s(RegEntry, "_color");
SaveValueInt("CVEdit", RegEntry, this->rgb);
ZeroMemory(RegEntry, 33);
strcpy_s(RegEntry, this->szRegName);
strcat_s(RegEntry, "_FontPointSize");
SaveValueInt("CVEdit", RegEntry, this->PointSize);
ZeroMemory(RegEntry, 33);
strcpy_s(RegEntry, this->szRegName);
strcat_s(RegEntry, "_Font");
SaveValueString("CVEdit", RegEntry, this->LogFont.lfFaceName);
ZeroMemory(RegEntry, 33);
strcpy_s(RegEntry, this->szRegName);
strcat_s(RegEntry, "_FontWeight");
SaveValueInt("CVEdit", RegEntry, this->LogFont.lfWeight);
ZeroMemory(RegEntry, 33);
strcpy_s(RegEntry, this->szRegName);
strcat_s(RegEntry, "_FontItalic");
SaveValueInt("CVEdit", RegEntry, this->LogFont.lfItalic);
ZeroMemory(RegEntry, 33);
strcpy_s(RegEntry, this->szRegName);
strcat_s(RegEntry, "_FontUnderline");
SaveValueInt("CVEdit", RegEntry, this->LogFont.lfUnderline);
ZeroMemory(RegEntry, 33);
strcpy_s(RegEntry, this->szRegName);
strcat_s(RegEntry, "_FontStrike");
SaveValueInt("CVEdit", RegEntry, this->LogFont.lfStrikeOut);
}
void CVPrefrence::SetDefaultFont(const HWND hwndDlg)
{
LOGFONT* const plfont = &this->LogFont;
memset(&this->LogFont, 0, sizeof(LOGFONT));
HFONT hFont = (HFONT)GetStockObject(ANSI_FIXED_FONT);
if (hFont == NULL)
hFont = (HFONT)GetStockObject(SYSTEM_FONT);
GetObject(hFont, sizeof(LOGFONT), plfont);
this->PointSize = 10;
this->GetHeightFromPointSize(hwndDlg);
}
int CVPrefrence::GetHeightFromPointSize(const HWND hwndDlg)
{
const HDC hdc = GetDC(hwndDlg);
const int Height = -MulDiv(this->PointSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
ReleaseDC(hwndDlg, hdc);
return Height;
}
void CVPrefrence::ApplyPreferences(const HWND hwndScin, const CVPrefrence* DefaultPref)
{
const int id = this->SciKeywordID;
const bool HL = this->Highlight;
SendMessage(hwndScin, SCI_STYLESETFORE, id, HL ? (LPARAM)this->rgb : (LPARAM)DefaultPref->rgb);
SendMessage(hwndScin, SCI_STYLESETFONT, id, HL ? (LPARAM)this->LogFont.lfFaceName : (LPARAM)DefaultPref->LogFont.lfFaceName);
SendMessage(hwndScin, SCI_STYLESETSIZE, id, HL ? (LPARAM)(this->PointSize) : (LPARAM)DefaultPref->PointSize);
SendMessage(hwndScin, SCI_STYLESETWEIGHT, id, HL ? (LPARAM)this->LogFont.lfWeight : (LPARAM)DefaultPref->LogFont.lfWeight);
SendMessage(hwndScin, SCI_STYLESETITALIC, id, HL ? (LPARAM)this->LogFont.lfItalic : (LPARAM)DefaultPref->LogFont.lfItalic);
SendMessage(hwndScin, SCI_STYLESETUNDERLINE, id, HL ? (LPARAM)this->LogFont.lfUnderline : (LPARAM)DefaultPref->LogFont.lfUnderline);
// There is no strike through in Scintilla (yet!)
}
CVPrefrence::~CVPrefrence()
{
//everything should be automatically destroyed.
}