-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDlgConsole.cpp
1592 lines (1314 loc) · 43.6 KB
/
DlgConsole.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////////////////////
// //
// NppSnippets - Code Snippets plugin for Notepad++ //
// Copyright (C) 2010-2020 Frank Fesevur //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program; if not, write to the Free Software //
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
// //
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <shlwapi.h>
#include <locale>
#include <codecvt>
#include "NPP/PluginInterface.h"
#include "NPP/menuCmdID.h"
#include "NPP/Docking.h"
#include "NppSnippets.h"
#include "Library.h"
#include "Snippets.h"
#include "Resource.h"
#include "DlgEditSnippet.h"
#include "DlgEditLibrary.h"
#include "DlgEditLanguages.h"
#include "DlgImportLibrary.h"
#include "Options.h"
#include "WaitCursor.h"
#ifdef _MSC_VER
#pragma comment(lib, "shlwapi.lib")
#endif
/////////////////////////////////////////////////////////////////////////////
// Various static variables
static HWND s_hDlg = NULL; // The HWND to the dialog
static HWND s_hList = NULL; // The HWND to the listbox
static HWND s_hFilter = NULL; // The HWND to the filter editbox
static HWND s_hCombo = NULL; // The HWND to the combo
static WNDPROC s_hListboxProcOld = NULL; // The HWND to the original procedure of the listbox
static WNDPROC s_hFilterProcOld = NULL; // The HWND to the original procedure of the filter textbox
static HICON s_hTabIcon = NULL; // The icon on the docking tab
static HBRUSH s_hbrBkgnd = NULL; // The brush to paint the theme on the background of the listbox
static int s_iHeightCombo = 20; // This info should come from Windows
static int s_iHeightFilter = 20; // This info should come from Windows
static bool s_bConsoleInitialized = false; // Is the console initialized?
static bool s_bConsoleVisible = false; // Is the console visible?
static Library* s_curLibrary = NULL; // The currently selected lib
static int s_curLang = -1; // The "LangType" of the currently selected lib
static bool s_HasPluginHome = false; // Does the version of N++ support NPPM_GETPLUGINHOMEPATH
/////////////////////////////////////////////////////////////////////////////
//
static bool AddLibsToCombo(int lang)
{
// First get the last used library for this language
int lastUsed = -1;
SqliteStatement stmt(g_db, "SELECT LibraryID FROM LangLastUsed WHERE Lang = @langid");
stmt.Bind("@langid", lang);
if (stmt.GetNextRecord())
lastUsed = stmt.GetIntColumn("LibraryID");
stmt.Finalize();
// Get all the libraries for this language
stmt.Prepare("SELECT Library.*, LibraryLang.Lang FROM Library INNER JOIN LibraryLang ON Library.LibraryID = LibraryLang.LibraryID WHERE (LibraryLang.Lang = @langid OR LibraryLang.Lang = -2) ORDER BY Library.Name");
stmt.Bind("@langid", lang);
// Go through the rows
int colLang = stmt.GetColumnCount() - 1;
bool first = true;
bool firstLanguage = false;
bool selectedUsersChoice = false; // Did the user select a library?
while (stmt.GetNextRecord())
{
// Store the library data
Library* lib = new Library(&stmt);
// Add the item to the combo
long item = (long) SendDlgItemMessage(s_hDlg, IDC_NAME, CB_ADDSTRING, (WPARAM) 0, (LPARAM) lib->WGetName());
SendDlgItemMessage(s_hDlg, IDC_NAME, CB_SETITEMDATA, (WPARAM) item, (LPARAM) lib);
// Determine if we need to select this library?
bool selectThisLib = first;
// Did we run into the user selected library already?
if (!selectedUsersChoice)
{
if (lib->GetLibraryID() == lastUsed)
{
selectedUsersChoice = true;
selectThisLib = true;
}
else
{
if (!firstLanguage)
{
int libLang = stmt.GetIntColumn(colLang);
if (libLang >= 0)
{
firstLanguage = true;
selectThisLib = true;
}
}
}
}
// Need to select this library?
if (selectThisLib)
{
SendDlgItemMessage(s_hDlg, IDC_NAME, CB_SETCURSEL, (WPARAM) item, (LPARAM) 0);
s_curLibrary = lib;
}
first = false;
}
stmt.Finalize();
return !first;
}
/////////////////////////////////////////////////////////////////////////////
//
static bool AddComboItems()
{
// Add the Libraries to the Combobox for the documents language
if (AddLibsToCombo(g_currentLang))
{
s_curLang = g_currentLang;
return true;
}
// If no library is found for this language, try for the generic language
if (AddLibsToCombo(-1))
{
s_curLang = -1;
return true;
}
// Not even a generic library found in the database
s_curLang = -1;
return false;
}
/////////////////////////////////////////////////////////////////////////////
//
static bool AddListItems()
{
if (s_curLibrary == NULL)
return false;
// Create the proper SQL-statement
std::string sql = "SELECT * FROM Snippets WHERE LibraryID = @libid ";
// Need to add the filter?
std::wstring filter = GetDlgText(s_hDlg, IDC_FILTER);
if (filter.length() >= 0)
sql += "AND Name LIKE @filter ";
// Order the list properly
sql += std::string("ORDER BY ") + (s_curLibrary->GetSortAlphabetic() ? "Name,Sort" : "Sort,Name");
// Prepare this statement
SqliteStatement stmt(g_db, sql.c_str());
// Bind the language-id to the statement
stmt.Bind("@libid", s_curLibrary->GetLibraryID());
// Bind the filter
if (filter.length() >= 0)
{
std::wstring s = std::wstring(L"%") + filter + std::wstring(L"%");
stmt.Bind("@filter", s.c_str());
}
// Go through the rows
while (stmt.GetNextRecord())
{
// Get the snippet from the database
Snippet* snip = new Snippet(&stmt);
// Put in the the list
long item = (long) SendDlgItemMessage(s_hDlg, IDC_LIST, LB_ADDSTRING, (WPARAM) 0, (LPARAM) snip->WGetName());
SendDlgItemMessage(s_hDlg, IDC_LIST, LB_SETITEMDATA, (WPARAM) item, (LPARAM) snip);
}
stmt.Finalize();
return true;
}
/////////////////////////////////////////////////////////////////////////////
//
static void ClearComboItems()
{
// First need to clean up the ItemData!!!
Library* lib = NULL;
int count = (int) SendDlgItemMessage(s_hDlg, IDC_NAME, CB_GETCOUNT, 0, 0);
for (int item = 0; item < count; item++)
{
lib = (Library*) SendDlgItemMessage(s_hDlg, IDC_NAME, CB_GETITEMDATA, (WPARAM) item, 0);
delete lib;
}
s_curLibrary = NULL;
// Now delete the items from the combobox
SendDlgItemMessage(s_hDlg, IDC_NAME, CB_RESETCONTENT, (WPARAM) 0, (LPARAM) 0);
// And empty the filter
SetDlgItemText(s_hDlg, IDC_FILTER, L"");
}
/////////////////////////////////////////////////////////////////////////////
//
static void ClearListItems()
{
// First need to clean up the ItemData!!!
Snippet* snip = NULL;
int count = (int) SendDlgItemMessage(s_hDlg, IDC_LIST, LB_GETCOUNT, 0, 0);
for (int item = 0; item < count; item++)
{
snip = (Snippet*) SendDlgItemMessage(s_hDlg, IDC_LIST, LB_GETITEMDATA, (WPARAM) item, 0);
delete snip;
}
// Now delete the items from the listbox
SendDlgItemMessage(s_hDlg, IDC_LIST, LB_RESETCONTENT, (WPARAM) 0, (LPARAM) 0);
}
/////////////////////////////////////////////////////////////////////////////
//
void UpdateSnippetsList()
{
if (!s_bConsoleVisible)
return;
WaitCursor wait;
ClearComboItems();
ClearListItems();
g_db->Open();
AddComboItems();
AddListItems();
g_db->Close();
}
/////////////////////////////////////////////////////////////////////////////
//
static BOOL DbExist(UINT uMsg, LPWSTR dbFile, LPCWSTR szExtraDir, bool bMainDB)
{
// Get the directory from NP++
WCHAR tmp[MAX_PATH];
SendMessage(g_nppData._nppHandle, uMsg, MAX_PATH, (LPARAM) &tmp);
wcsncpy(dbFile, tmp, MAX_PATH);
// Add the extra dictory (if any)
if (szExtraDir != NULL)
wcsncat(dbFile, szExtraDir, MAX_PATH);
// Add the filename of the database
if (bMainDB)
wcsncat(dbFile, L"\\NppSnippets.sqlite", MAX_PATH);
else
wcsncat(dbFile, L"\\Template.sqlite", MAX_PATH);
// Check if the file exists
return PathFileExists(dbFile);
}
/////////////////////////////////////////////////////////////////////////////
// Get or construct the filename for the database
static bool GetDatabaseFile()
{
// First see if there is a user-defined path for the database
if (PathFileExists(g_Options->GetDBFile().c_str()))
{
g_db->SetFilename(g_Options->GetDBFile().c_str());
return true;
}
// Then try in the Plugin config dir in AppData. Best place to put the database!!!
WCHAR dbFile[MAX_PATH];
if (DbExist(NPPM_GETPLUGINSCONFIGDIR, dbFile, NULL, true))
{
g_db->SetFilename(dbFile);
return true;
}
// If the database is still not found, it could be in the installation directory.
// Putting your database there could result in read-only problems
if (DbExist(NPPM_GETNPPDIRECTORY, dbFile, L"\\plugins\\Config", true))
{
g_db->SetFilename(dbFile);
return true;
}
if (s_HasPluginHome)
{
if (DbExist(NPPM_GETPLUGINHOMEPATH, dbFile, NULL, false))
{
std::wstring from = dbFile;
// Get the user's plugin-config directory
DbExist(NPPM_GETPLUGINSCONFIGDIR, dbFile, NULL, true);
// Copy template to "NPPM_GETPLUGINSCONFIGDIR"
if (CopyFile(from.c_str(), dbFile, TRUE))
{
g_db->SetFilename(dbFile);
return true;
}
}
}
// If it is still not found, we most likely have a fresh installation. Look for the template database
if (DbExist(NPPM_GETNPPDIRECTORY, dbFile, L"\\plugins\\NppSnippets", false))
{
// Store the filename of the template database
WCHAR szFrom[MAX_PATH];
wcsncpy(szFrom, dbFile, MAX_PATH);
// Get the user's plugin-config directory
DbExist(NPPM_GETPLUGINSCONFIGDIR, dbFile, NULL, true);
// Copy template to "NPPM_GETPLUGINSCONFIGDIR"
if (CopyFile(szFrom, dbFile, TRUE))
{
g_db->SetFilename(dbFile);
return true;
}
}
// Still not found, clean the filename
return false;
}
/////////////////////////////////////////////////////////////////////////////
//
static void ShowContextMenu(HWND hwnd, UINT uResID, int xPos, int yPos)
{
// Load the menu resource.
HMENU hMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(uResID));
if (hMenu == NULL)
return;
// TrackPopupMenu cannot display the menu bar so get a handle to the first shortcut menu.
HMENU hPopup = GetSubMenu(hMenu, 0);
// Disable various items of the snippet menu
if (uResID == IDCM_SNIPPET)
{
// Disable the "move up" and "move down" items
// if the library is alphabetticly ordered
if (s_curLibrary->GetSortAlphabetic())
{
EnableMenuItem(hPopup, IDC_SNIPPET_MOVE_UP, MF_DISABLED);
EnableMenuItem(hPopup, IDC_SNIPPET_MOVE_DOWN, MF_DISABLED);
}
// Need to disable the clipboard entry?
bool enable = false;
if (OpenClipboard(hwnd))
{
// Get the content of the clipboard
HANDLE hData = GetClipboardData(CF_UNICODETEXT);
WCHAR* buffer = (WCHAR*) GlobalLock(hData);
enable = (buffer != NULL);
GlobalUnlock(hData);
CloseClipboard();
}
if (!enable)
EnableMenuItem(hPopup, IDC_SNIPPET_ADD_CLIPBOARD, MF_DISABLED);
// Need to disable the selection entry?
if (SendMsg(SCI_GETSELECTIONEND) == SendMsg(SCI_GETSELECTIONSTART))
EnableMenuItem(hPopup, IDC_SNIPPET_ADD_SELECTION, MF_DISABLED);
}
// Display the shortcut menu. Track the right mouse button.
TrackPopupMenu(hPopup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, xPos, yPos, 0, hwnd, NULL);
// Destroy the menu.
DestroyMenu(hMenu);
}
/////////////////////////////////////////////////////////////////////////////
// Set the focus back on the edit window
static void SetFocusOnEditor()
{
SetFocus(getCurrentScintilla());
}
/////////////////////////////////////////////////////////////////////////////
// Start a new document
static void StartNewDocument(LangType lang)
{
// Start a new document, if the current document is not empty
if (SendMsg(SCI_GETLENGTH) > 0)
SendMessage(g_nppData._nppHandle, NPPM_MENUCOMMAND, 0, IDM_FILE_NEW);
// Set the language for the new document
if (lang != L_EXTERNAL)
SendMessage(g_nppData._nppHandle, NPPM_SETCURRENTLANGTYPE, 0, lang);
// Put the focus on the editor
SetFocusOnEditor();
}
/////////////////////////////////////////////////////////////////////////////
// Indent the lines where the new snippet has just been inserted
static void IndentSnippet(int firstLine, int lastLine)
{
// If there was only one line insert, nothing to do
if (firstLine == lastLine)
return;
// If disabled, nothing to do
if (!g_Options->GetIndentSnippet())
return;
// Get the tab width
const int tabWidth = (int) SendMsg(SCI_GETTABWIDTH);
// Go through the lines
for (int line = firstLine; line <= lastLine; line++)
{
// Are we inside a folding block?
const int foldParent = (int) SendMsg(SCI_GETFOLDPARENT, line);
if (foldParent < 0)
continue;
// What is the folding level of this block?
const int parentIndent = (int) SendMsg(SCI_GETLINEINDENTATION, foldParent);
const int lastChild = (int) SendMsg(SCI_GETLASTCHILD, foldParent, -1);
if (foldParent == 0)
{
SendMsg(SCI_SETLINEINDENTATION, line, 0);
}
else if (line == lastChild)
{
SendMsg(SCI_SETLINEINDENTATION, line, parentIndent);
}
else
{
SendMsg(SCI_SETLINEINDENTATION, line, parentIndent + tabWidth);
}
}
}
/////////////////////////////////////////////////////////////////////////////
//
#define SPACER 4
static void OnSize(HWND hWnd, int iWidth, int iHeight)
{
UNREFERENCED_PARAMETER(hWnd);
// Position the languages combobox
SetWindowPos(s_hCombo, 0, 0, 0, iWidth, s_iHeightCombo, SWP_NOACTIVATE | SWP_NOZORDER);
// Position the filter editbox
const int iFilterX = s_iHeightCombo + SPACER;
SetWindowPos(s_hFilter, 0, 0, iFilterX, iWidth, s_iHeightFilter, SWP_NOACTIVATE | SWP_NOZORDER);
// Position the list of snippets
const int iListX = iFilterX + s_iHeightFilter + SPACER;
SetWindowPos(s_hList, 0, 0, iListX, iWidth, iHeight - iListX, SWP_NOACTIVATE | SWP_NOZORDER);
}
/////////////////////////////////////////////////////////////////////////////
//
static void OnSelChange_Combo(HWND hWnd)
{
// Store the previously used LibraryID
int prevLibID = s_curLibrary->GetLibraryID();
// Get current LibraryID from the selected item
int item = (int) SendDlgItemMessage(hWnd, IDC_NAME, CB_GETCURSEL, 0, 0L);
s_curLibrary = (Library*) SendDlgItemMessage(hWnd, IDC_NAME, CB_GETITEMDATA, (WPARAM) item, 0);
// Did the library ID really change?
if (prevLibID == s_curLibrary->GetLibraryID())
return;
// Empty the filter
SetDlgItemText(s_hDlg, IDC_FILTER, L"");
// Open the database
g_db->Open();
// Update the database
char szSQL[MAX_PATH];
_snprintf(szSQL, MAX_PATH, "DELETE FROM LangLastUsed WHERE Lang = %d; INSERT INTO LangLastUsed(Lang,LibraryID) VALUES(%d,%d);", s_curLang, s_curLang, s_curLibrary->GetLibraryID());
g_db->Execute(szSQL);
// Update the list with snippets
ClearListItems();
AddListItems();
g_db->Close();
}
/////////////////////////////////////////////////////////////////////////////
//
static void OnChange_Filter()
{
WaitCursor wait;
ClearListItems();
g_db->Open();
AddListItems();
g_db->Close();
}
/////////////////////////////////////////////////////////////////////////////
// Display a context menu for the selected item, or a general one
// Note that point is in Screen coordinates!
static void OnContextMenu(HWND hWnd, int xPos, int yPos, HWND hChild)
{
if (hChild == s_hCombo)
{
ShowContextMenu(hWnd, IDCM_LIBRARY, xPos, yPos);
return;
}
if (hChild == s_hList)
{
/*
// Find out where the cursor was and select that item from the list
POINT pt;
pt.x = xPos;
pt.y = yPos;
ScreenToClient(&pt);
DWORD dw = (DWORD) SendMessage(m_hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(pt.x, pt.y));
*/
ShowContextMenu(hWnd, IDCM_SNIPPET, xPos, yPos);
return;
}
}
/////////////////////////////////////////////////////////////////////////////
// Cleanup the mess
static void OnClose(HWND hWnd)
{
ClearComboItems();
ClearListItems();
if (s_hTabIcon != NULL)
{
DestroyIcon(s_hTabIcon);
s_hTabIcon = NULL;
}
EndDialog(hWnd, 0);
}
/////////////////////////////////////////////////////////////////////////////
// Convert a wide Unicode string to an UTF8 string
static std::string utf8_encode(const std::wstring &wstr)
{
const int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}
/////////////////////////////////////////////////////////////////////////////
// Insert the snippet (selected from the context menu item) into the text
// in the Scintilla window. Double clicking get redirected to here as well.
static void OnSnippetInsert(HWND hWnd)
{
// Get the current item and its snippet
const int item = (int) SendDlgItemMessage(hWnd, IDC_LIST, LB_GETCURSEL, 0, 0L);
// Was there an item selected?
if (item == LB_ERR)
return;
// Get the data for this item and store it in the local snip-variable
Snippet* pSnip = (Snippet*) SendDlgItemMessage(s_hDlg, IDC_LIST, LB_GETITEMDATA, (WPARAM) item, 0);
Snippet snip = *pSnip;
// Do we need to start a new document?
if (snip.GetNewDocument())
StartNewDocument(snip.GetNewDocumentLang());
// Start an undo action, to make sure this insert is one action to undo
SendMsg(SCI_BEGINUNDOACTION);
// Get the text in current selection
const int selsize = (int) SendMsg(SCI_GETSELECTIONEND) - (int) SendMsg(SCI_GETSELECTIONSTART);
char* pszText = NULL;
if (selsize != 0)
{
pszText = new char[selsize + 2];
if (pszText == NULL)
return;
SendMsg(SCI_GETSELTEXT, 0, (LPARAM) pszText);
}
// Insert the first part of the snippet
std::wstring wstr = ConvertLineEnding(snip.WGetBeforeSelection());
std::string strTo = utf8_encode(wstr);
SendMsg(SCI_REPLACESEL, 0, (LPARAM) strTo.c_str());
const int beforeSel = (int) SendMsg(SCI_GETCURRENTPOS);
const int beforeSelLine = (int) SendMsg(SCI_LINEFROMPOSITION, beforeSel);
// Put back the selection (if any)
if (pszText != NULL)
{
if (!snip.GetReplaceSelection())
SendMsg(SCI_REPLACESEL, 0, (LPARAM) pszText);
delete [] pszText;
}
const int afterSel = (int) SendMsg(SCI_GETCURRENTPOS);
// Is there a select part of the snippet to add?
if (snip.WGetAfterSelection() != NULL)
{
const size_t len = wcslen(snip.WGetAfterSelection());
if (len > 0)
{
wstr = ConvertLineEnding(snip.WGetAfterSelection());
strTo = utf8_encode(wstr);
SendMsg(SCI_REPLACESEL, 0, (LPARAM) strTo.c_str());
}
}
// Get the position and line number after fully inserting the snippet
const int position = (int) SendMsg(SCI_GETCURRENTPOS);
const int afterInsertLine = (int) SendMsg(SCI_LINEFROMPOSITION, position);
// Restore the cursor position and selection
SendMsg(SCI_SETANCHOR, beforeSel);
SendMsg(SCI_SETCURRENTPOS, afterSel);
// Try to indent the inserted snippet
IndentSnippet(beforeSelLine, afterInsertLine);
// End the undo action
SendMsg(SCI_ENDUNDOACTION);
// Put the focus back on the edit window
SetFocusOnEditor();
}
/////////////////////////////////////////////////////////////////////////////
// Get the highest used sort number
static long GetMaxSort()
{
long maxSort = 0;
int count = (int) SendDlgItemMessage(s_hDlg, IDC_LIST, LB_GETCOUNT, 0, 0);
for (int item = 0; item < count; item++)
{
Snippet* snip = (Snippet*) SendDlgItemMessage(s_hDlg, IDC_LIST, LB_GETITEMDATA, (WPARAM) item, 0);
long sort = snip->GetSort();
if (sort > maxSort)
maxSort = sort;
}
return maxSort;
}
/////////////////////////////////////////////////////////////////////////////
// Add a new snippet to the current library.
static void OnSnippetAdd(HWND hWnd)
{
UNREFERENCED_PARAMETER(hWnd);
// Create the new snippet and init some required members
Snippet* snip = new Snippet();
snip->SetLibraryID(s_curLibrary->GetLibraryID());
snip->SetSort(GetMaxSort() + 1);
// Let the user edit the new snippet
bool closedOK = ShowEditSnippetDlg(snip);
// We don't need the object anymore, it was stored in the database if OK was pressed
delete snip;
// If the users pressed OK, refresh everything from the database
if (closedOK)
UpdateSnippetsList();
}
/////////////////////////////////////////////////////////////////////////////
// Edit the currently selected snippet
static void OnSnippetEdit(HWND hWnd)
{
// Get the current item and its snippet
int item = (int) SendDlgItemMessage(hWnd, IDC_LIST, LB_GETCURSEL, 0, 0L);
// Was there an item selected?
if (item == LB_ERR)
return;
// Get the data for this item
Snippet* snip = (Snippet*) SendDlgItemMessage(s_hDlg, IDC_LIST, LB_GETITEMDATA, (WPARAM) item, 0);
if (snip == NULL)
return;
// Let the uses edit the snippet
if (ShowEditSnippetDlg(snip))
{
// Put the (changed?) name back in the list
SendDlgItemMessage(s_hDlg, IDC_LIST, LB_DELETESTRING, (WPARAM) item, (LPARAM) NULL);
SendDlgItemMessage(s_hDlg, IDC_LIST, LB_INSERTSTRING, (WPARAM) item, (LPARAM) snip->WGetName());
SendDlgItemMessage(s_hDlg, IDC_LIST, LB_SETITEMDATA, (WPARAM) item, (LPARAM) snip);
SendDlgItemMessage(s_hDlg, IDC_LIST, LB_SETCURSEL, (WPARAM) item, (LPARAM) NULL);
}
}
/////////////////////////////////////////////////////////////////////////////
// Delete the snippet from the list and database
static void OnSnippetDelete(HWND hWnd)
{
// Get the current item and its snippet
int item = (int) SendDlgItemMessage(hWnd, IDC_LIST, LB_GETCURSEL, 0, 0L);
// Was there an item selected?
if (item == LB_ERR)
return;
// Get the data for this item
Snippet* snip = (Snippet*) SendDlgItemMessage(s_hDlg, IDC_LIST, LB_GETITEMDATA, (WPARAM) item, 0);
if (snip == nullptr)
return;
std::wstring msg = L"Are you sure you want to delete the snippet:\r\n";
msg += snip->WGetName();
// Ask for confirmation
if (!MsgBoxYesNo(msg.c_str()))
return;
// Delete from the database
snip->DeleteFromDB();
// Clean up the item data
delete snip;
// Delete the entry from the list
SendDlgItemMessage(s_hDlg, IDC_LIST, LB_DELETESTRING, (WPARAM) item, (LPARAM) NULL);
}
/////////////////////////////////////////////////////////////////////////////
// Duplicate the currently selected snippet
static void OnSnippetDuplicate(HWND hWnd)
{
// Get the current item and its snippet
int item = (int) SendDlgItemMessage(hWnd, IDC_LIST, LB_GETCURSEL, 0, 0L);
// Was there an item selected?
if (item == LB_ERR)
return;
// Get the data for this item
Snippet* snip = (Snippet*) SendDlgItemMessage(s_hDlg, IDC_LIST, LB_GETITEMDATA, (WPARAM) item, 0);
if (snip == nullptr)
return;
std::wstring s = L"Are you sure you want to duplicate the snippet:\r\n";
s += snip->WGetName();
// Ask for confirmation
if (!MsgBoxYesNo(s.c_str()))
return;
// Make a copy of the selected snippet
Snippet* pNew = new Snippet(*snip);
// Adjust the name of the duplicate
s = snip->WGetName();
s += L" - Dup";
pNew->WSetName(s.c_str());
// Save to the database and refresh the list
pNew->SaveToDB();
UpdateSnippetsList();
}
/////////////////////////////////////////////////////////////////////////////
// Add a new snippet based upon the current content of the clipboard
static void OnSnippetAddClipboard(HWND hWnd)
{
if (!OpenClipboard(hWnd))
return;
// Get the content of the clipboard
HANDLE hData = GetClipboardData(CF_UNICODETEXT);
WCHAR* buffer = (WCHAR*) GlobalLock(hData);
if (buffer != NULL)
{
std::wstring buf = ConvertLineEnding(buffer, SC_EOL_CRLF);
// Create the new snippet and init some required members
Snippet* snip = new Snippet();
snip->SetLibraryID(s_curLibrary->GetLibraryID());
snip->SetSort(GetMaxSort() + 1);
snip->WSetBeforeSelection(buf.c_str());
snip->GuessName();
// Let the user edit the new snippet
bool closedOK = ShowEditSnippetDlg(snip);
// We don't need the object anymore, it was stored in the database if OK was pressed
delete snip;
// If the users pressed OK, refresh everything from the database
if (closedOK)
UpdateSnippetsList();
}
GlobalUnlock(hData);
CloseClipboard();
}
/////////////////////////////////////////////////////////////////////////////
// Add a new snippet based upon the current selection
static void OnSnippetAddSelection(HWND hWnd)
{
UNREFERENCED_PARAMETER(hWnd);
// Get the text in current selection
int selsize = (int) SendMsg(SCI_GETSELECTIONEND) - (int) SendMsg(SCI_GETSELECTIONSTART);
if (selsize == 0)
{
MsgBox("No selection found");
return;
}
char* pszText = new char[selsize + 2];
SendMsg(SCI_GETSELTEXT, 0, (LPARAM) pszText);
// Convert to a proper std::wstring
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wide = converter.from_bytes(pszText);
delete[] pszText;
// Convert the line ending of the selection
std::wstring txt = ConvertLineEnding(wide.c_str(), SC_EOL_CRLF);
// Create the new snippet and init some required members
Snippet* snip = new Snippet();
snip->SetLibraryID(s_curLibrary->GetLibraryID());
snip->SetSort(GetMaxSort() + 1);
snip->WSetBeforeSelection(txt.c_str());
snip->GuessName();
// Let the user edit the new snippet
const bool closedOK = ShowEditSnippetDlg(snip);
// We don't need the object anymore, it was stored in the database if OK was pressed
delete snip;
// If the users pressed OK, refresh everything from the database
if (closedOK)
UpdateSnippetsList();
}
/////////////////////////////////////////////////////////////////////////////
// Update all the items in the database as well, based upon the current list
static void UpdateSortInDB()
{
g_db->Open();
g_db->BeginTransaction();
SqliteStatement stmt(g_db, "UPDATE Snippets SET Sort = @sort WHERE SnippetID = @id");
int colID = stmt.GetBindParameterIndex("@id");
int colSort = stmt.GetBindParameterIndex("@sort");
Snippet* snip = NULL;
int count = (int) SendDlgItemMessage(s_hDlg, IDC_LIST, LB_GETCOUNT, 0, 0);
for (int item = 0; item < count; item++)
{
snip = (Snippet*) SendDlgItemMessage(s_hDlg, IDC_LIST, LB_GETITEMDATA, (WPARAM) item, 0);
snip->SetSort(item + 1);
stmt.Bind(colID, snip->GetSnippetID());
stmt.Bind(colSort, snip->GetSort());
try
{
stmt.SaveRecord();
}
catch(SqliteException e)
{
//MsgBox(sqlite3_errmsg(g_db));
g_db->RollbackTransaction();
g_db->Close();
return;
}
}
stmt.Finalize();
g_db->CommitTransaction();
g_db->Close();
}
/////////////////////////////////////////////////////////////////////////////
//
static void OnSnippetMoveUp(HWND hWnd)
{
// Just in case
if (s_curLibrary->GetSortAlphabetic())
return;
// Get the current item and its snippet
int item = (int) SendDlgItemMessage(hWnd, IDC_LIST, LB_GETCURSEL, 0, 0L);
// Was there an item selected?
if (item == LB_ERR)
return;
// Are we at the top of the list?
if (item == 0)
return;
// Get the data for this item
Snippet* snip = (Snippet*) SendDlgItemMessage(s_hDlg, IDC_LIST, LB_GETITEMDATA, (WPARAM) item, 0);
if (snip == NULL)
return;
WaitCursor wait;
// Put the selected item one lower
SendDlgItemMessage(s_hDlg, IDC_LIST, LB_DELETESTRING, (WPARAM) item, (LPARAM) NULL);
SendDlgItemMessage(s_hDlg, IDC_LIST, LB_INSERTSTRING, (WPARAM) item - 1, (LPARAM) snip->WGetName());
SendDlgItemMessage(s_hDlg, IDC_LIST, LB_SETITEMDATA, (WPARAM) item - 1, (LPARAM) snip);
SendDlgItemMessage(s_hDlg, IDC_LIST, LB_SETCURSEL, (WPARAM) item - 1, (LPARAM) NULL);
UpdateSortInDB();
}
/////////////////////////////////////////////////////////////////////////////
//
static void OnSnippetMoveDown(HWND hWnd)
{
// Just in case
if (s_curLibrary->GetSortAlphabetic())
return;
// Get the current item and its snippet
int item = (int) SendDlgItemMessage(hWnd, IDC_LIST, LB_GETCURSEL, 0, 0L);
// Was there an item selected?
if (item == LB_ERR)
return;
// Are we at the end of the list?
int count = (int) SendDlgItemMessage(s_hDlg, IDC_LIST, LB_GETCOUNT, 0, 0);
if (item == count - 1)
return;
// Get the data for this item
Snippet* snip = (Snippet*) SendDlgItemMessage(s_hDlg, IDC_LIST, LB_GETITEMDATA, (WPARAM) item, 0);