This repository was archived by the owner on May 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVSViewer.cpp
More file actions
1814 lines (1536 loc) · 49.1 KB
/
AVSViewer.cpp
File metadata and controls
1814 lines (1536 loc) · 49.1 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
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
// VirtualDub - Video processing and capture application
// Copyright (C) 1998-2001 Avery Lee
//
// 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.
// *******************************************************************
// *** AVS Modification ***
// *** Tobias Minich, August 2002 ***
// *** ***
// *** Since I'm a c++ newbie, i took the HexEditor as a template. ***
// *** Thanks to Toff for cleaning it up :) ***
// Tweakable line modification by christophe.paris@free.fr (Toff)
#pragma warning(disable : 4786)
#include "stdafx.h"
#define f_AVSVIEWER_CPP
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <crtdbg.h>
#include <windows.h>
#include <commctrl.h>
#include <commdlg.h>
#include <shellapi.h>
#include <tchar.h>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#define INCLUDE_DEPRECATED_FEATURES
#include "Scintilla.h"
#include "SciLexer.h"
//#include "avsconst.h"
#include "resource.h"
//#include "oshelper.h"
//#include "gui.h"
//#include <vd2/system/Error.h>
//#include <vd2/system/list.h>
#include "prefs.h"
#include "AVSViewer.h"
#include "api.h"
//#include "ProgressDialog.h"
//#include "virtualdubmod_messages.h"
#include "accel.h"
#include <vd2/system/vdtypes.h>
#include <vd2/system/text.h>
//#include "avisynth.h"
//#include "avisynth_interface.h"
//#include "Tdll.h"
#include "CAviSynth.h"
//#include "I18N.h"
//#include "modplus.h"
extern HINSTANCE g_hInst;
//extern HMODULE g_hAVSLexer;
//extern Tdll *g_dllAVSLexer;
extern CAviSynth *g_dllAviSynth;
HWND g_ScriptEditor = (HWND) -1;
std::vector<class AVSEditor*> g_windows;
std::vector<HWND> g_dialogs;
//extern VDubModPreferences g_VDMPrefs;
void AVSViewerOpen(HWND hwnd);
void AVSViewerChangePrefs(HWND parent);
void init_avs();
void clear_avs();
using namespace std;
//////////////////////////////////////////////////////////////////////////////
extern const char szAVSEditorClassName[]="birdyAVSEditor";
extern const char szAVSViewerClassName[]="birdyAVSViewer";
static const char g_szAVSWarning[]="Script editor warning";
/* struct less_nocase {
static bool compare_chars(char x, char y) {
return toupper(x) < toupper(y);
}
bool operator()(const string& x, const string& y) const {
return lexicographical_compare(x.begin(), x.end(),
y.begin(), y.end(),
compare_chars);
}
};
*/
//vector<string> AVSToken;
//char *avsWords;
LPVOID imKeywords, imInternal, imExternal;
//////////////////////////////////////////////////////////////////////////////
struct FindTextOption {
char szFindString[1024];
bool bFindReverse;
bool bWholeWord;
bool bMatchCase;
bool bRegexp;
bool bWrap;
};
int guiMessageBox(HWND hwnd, UINT idText, UINT idCaption, UINT uType) {
char caption[256];
char text[4096]; // increased by Fizick (for Avisynth functions list)
bool error = false;
// get caption
if (LoadString(g_hInst, idCaption, (LPTSTR)caption, sizeof caption) == 0)
error = true;
// get message body
if (LoadString(g_hInst, idText, (LPTSTR)text, sizeof text) == 0)
error = true;
if (error) {
return MessageBox(hwnd, "Can't retrieve message!", "Internal Error", MB_OK);
}
return MessageBox(hwnd, text, caption, uType);
}
int guiMessageBoxText(HWND hwnd, LPCTSTR lpCaption, UINT uType, const char *text) {
return MessageBox(hwnd, text, lpCaption, uType);
}
const int WM_DEFER_ERROR = WM_USER+1;
class AVSEditor {
private:
const HWND hwnd;
HWND hwndStatus;
//HWND hwndRef;
wchar_t lpszFileName[MAX_PATH];
HFONT hfont;
void* coloredit;
HWND hwndView;
WNDPROC OldAVSViewWinProc; // Toff
bool bEnableWrite;
// bool bTrim;
bool bLineNumbers;
SciFnDirect fnScintilla;
sptr_t ptrScintilla;
int scriptType;
HWND hwndFind;
//ModelessDlgNode mdnFind;
FindTextOption mFind;
bool bModified;
public:
AVSEditor(HWND);
~AVSEditor();
static LRESULT APIENTRY AVSEditorWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) throw();
static LRESULT APIENTRY SubAVSEditorWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) throw();
void UpdatePreferences();
HWND GetHwnd(){ return hwnd; }
bool CheckFilename(const wchar_t* path) {
return _wcsicmp(path,lpszFileName)==0;
}
void Open(const wchar_t* path);
void HandleError(const char* s, int line);
private:
void Init() throw();
bool Commit() throw();
void SetStatus(const char *format, ...) throw();
void UpdateStatus() throw();
void UpdateLineNumbers();
void SetAStyle(int style, COLORREF fore, COLORREF back = RGB(0xff,0xff,0xff), int size = 0, const char *face = NULL);
LRESULT Handle_WM_COMMAND(WPARAM wParam, LPARAM lParam) throw();
LRESULT Handle_WM_SIZE(WPARAM wParam, LPARAM lParam) throw();
LRESULT Handle_WM_NOTIFY(HWND hwndFrom, UINT code, NMHDR *phdr) throw();
LRESULT Handle_WM_DROPFILES(WPARAM wParam, LPARAM lParam);
sptr_t SendMessageSci(int Message, WPARAM wParam = 0, LPARAM lParam = 0)
{return fnScintilla(ptrScintilla, Message, wParam, lParam);}
void SetScriptType(int type);
void CheckBracing();
void DoCalltip();
// Toff ----->
void RemoveLineCommentInRange(int line);
bool CommentUncommentLine(int lineNumber, bool comment);
// <----- Toff
void Find();
void FindNext(bool reverse);
void Jumpto();
static INT_PTR CALLBACK FindDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
static INT_PTR CALLBACK JumpDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
};
////////////////////////////
AVSEditor::AVSEditor(HWND _hwnd) : hwnd(_hwnd), hfont(0)
{
}
AVSEditor::~AVSEditor() {
if (hwndFind)
DestroyWindow(hwndFind);
if (hfont)
DeleteObject(hfont);
}
// Toff ----->
bool AVSEditor::CommentUncommentLine(int lineNumber, bool comment)
{
char buff[2];
// Get start of line index
int line_index = SendMessageSci(SCI_POSITIONFROMLINE, lineNumber, 0);
SendMessageSci(SCI_SETSEL, line_index, line_index+1);
SendMessageSci(SCI_GETSELTEXT, 0, (LPARAM)buff);
if(buff[0] == '#')
{
if(!comment)
{
// This line must be uncommented
buff[0] = '\0';
SendMessageSci(SCI_REPLACESEL, 0, (LPARAM)buff);
return true;
}
}
else
{
if(comment)
{
// This line must be commented
SendMessageSci(SCI_SETSEL, line_index, line_index);
buff[0] = '#';
buff[1] = '\0';
SendMessageSci(SCI_REPLACESEL, 0, (LPARAM)buff);
return true;
}
}
return false; // No change
}
void AVSEditor::RemoveLineCommentInRange(int line)
{
// CHARRANGE chr;
char *buff = NULL;
int charIndex = 0;
int charStart = 0;
int charEnd = 0;
int lineNumber = 0;
// int totalLineNumber = SendMessage(hwndView,EM_GETLINECOUNT ,0,0);
int totalLineNumber = SendMessageSci(SCI_GETLINECOUNT ,0,0);
// Get cursor position
// SendMessage(hwndView, EM_EXGETSEL, 0, (LPARAM)&chr);
// charIndex = chr.cpMin;
charStart = SendMessageSci(SCI_GETSELECTIONSTART, 0, 0);
charEnd = SendMessageSci(SCI_GETSELECTIONEND, 0, 0);
charIndex = charStart;
// lineNumber = SendMessage(hwndView, EM_LINEFROMCHAR, charIndex, 0);
lineNumber = SendMessageSci(SCI_LINEFROMPOSITION, charIndex, 0);
// Go up and find the "#StartTweaking" line
const char* startLineText = "#StartTweaking";
size_t startLineLen = strlen(startLineText);
for( ;lineNumber>=0;lineNumber--) {
// if (SendMessage(hwndView, EM_LINELENGTH, lineNumber, 0) != startLineLen) continue;
buff = new char[SendMessageSci(SCI_LINELENGTH, lineNumber)];
SendMessageSci(SCI_GETLINE, lineNumber, (LPARAM)buff);
if (strncmp(startLineText,buff,startLineLen) != 0 ) {
delete [] buff;
buff = NULL;
continue;
}
break;
}
if (buff) delete [] buff;
buff = NULL;
if (lineNumber<0) return;
lineNumber++;
int firstLine = lineNumber;
const char* stopLineText = "#StopTweaking";
size_t stopLineLen = strlen(stopLineText);
int lastLine = -1;
for( ;lineNumber<totalLineNumber;lineNumber++) {
buff = new char[SendMessageSci(SCI_LINELENGTH, lineNumber)];
SendMessageSci(SCI_GETLINE, lineNumber, (LPARAM)buff);
if (strncmp(stopLineText,buff,stopLineLen) == 0) {
lastLine = lineNumber;
break;
}
// CommentUncommentLine(lineNumber, ((lineNumber - firstLine) != line));
delete [] buff;
buff = NULL;
}
if (buff) delete [] buff;
for (lineNumber = firstLine; lineNumber<lastLine; lineNumber++)
CommentUncommentLine(lineNumber, ((lineNumber - firstLine) != line));
// while( (SendMessage(hwndView, EM_LINELENGTH, charIndex, 0) != startLineLen ||
// SendMessage(hwndView, EM_GETLINE, lineNumber, (LPARAM)buff) > 0) &&
// strncmp(startLineText,buff,startLineLen) != 0 )
/* while( (SendMessageSci(SCI_LINELENGTH, lineNumber, 0) != startLineLen ||
SendMessageSci(SCI_GETLINE, lineNumber, (LPARAM)buff) > 0) &&
strncmp(startLineText,buff,startLineLen) != 0 )
{
lineNumber--;
if(lineNumber < 0)
{
return;
}
// charIndex = SendMessage(hwndView, EM_LINEINDEX, lineNumber, 0);
charIndex = SendMessageSci(SCI_POSITIONFROMLINE, lineNumber, 0);
}
lineNumber++;
int firstLine = lineNumber;
StatusDebug(lineNumber, SendMessageSci(SCI_LINELENGTH, 3, 0), 0);
// charIndex = SendMessage(hwndView, EM_LINEINDEX, firstLine, 0);
charIndex = SendMessageSci(SCI_POSITIONFROMLINE, firstLine, 0);
// Go down and stop on the "#StopTweaking" line
const char* stopLineText = "#StopTweaking";
int stopLineLen = strlen(stopLineText)+1;
// while( (SendMessage(hwndView, EM_LINELENGTH, charIndex, 0) != stopLineLen ||
// SendMessage(hwndView, EM_GETLINE, lineNumber, (LPARAM)buff) > 0) &&
// strncmp(stopLineText,buff,stopLineLen) != 0 )
while( (SendMessageSci(SCI_LINELENGTH, lineNumber, 0) != stopLineLen ||
SendMessageSci(SCI_GETLINE, lineNumber, (LPARAM)buff) > 0) &&
strncmp(stopLineText,buff,stopLineLen) != 0 )
{
CommentUncommentLine(lineNumber, ((lineNumber - firstLine) != line));
lineNumber++;
if(lineNumber >= totalLineNumber)
{
break;
}
// charIndex = SendMessage(hwndView, EM_LINEINDEX, lineNumber, 0);
charIndex = SendMessageSci(SCI_POSITIONFROMLINE, lineNumber, 0);
}
*/
// Restore original position
// SendMessage(hwndView, EM_EXSETSEL, 0, (LPARAM)&chr);
SendMessageSci(SCI_SETSEL, charStart, charEnd);
}
LRESULT AVSEditor::SubAVSEditorWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) throw()
{
AVSEditor *pcd = (AVSEditor *)GetWindowLongPtr(
(HWND)GetWindowLongPtr(hwnd, GWLP_HWNDPARENT), 0);
switch (msg)
{
case WM_KEYDOWN:
if ((GetKeyState(VK_SHIFT)>=0) && (GetKeyState(VK_MENU)>=0) && (GetKeyState(VK_CONTROL) < 0))
{
if(wParam >= VK_F1 && wParam <= VK_F12)
{
pcd->RemoveLineCommentInRange(wParam - VK_F1);
if (pcd->Commit())
VDSendReopen(pcd->lpszFileName, pcd);
}
}
break;
}
return CallWindowProc(pcd->OldAVSViewWinProc, hwnd, msg, wParam, lParam);
}
// <----- Toff
void AVSEditor::Init() {
hwndStatus = CreateStatusWindow(WS_CHILD|WS_VISIBLE|SBARS_SIZEGRIP, "", hwnd, 501);
hwndView = CreateWindowEx(
WS_EX_CLIENTEDGE,
"Scintilla",
"",
WS_VISIBLE|WS_CHILD|WS_VSCROLL|WS_HSCROLL|ES_MULTILINE,
0,0,50,50,
hwnd,
NULL,
g_hInst,
NULL);
lpszFileName[0] = 0;
fnScintilla = (SciFnDirect)SendMessage(hwndView,SCI_GETDIRECTFUNCTION,0,0);
ptrScintilla = (sptr_t)SendMessage(hwndView,SCI_GETDIRECTPOINTER,0,0);
bLineNumbers = TRUE;
UpdateLineNumbers();
// SetAStyle(STYLE_DEFAULT, RGB(0,0,0), RGB(0xff,0xff,0xff), 11, "Courier New");
// SendMessageSci(SCI_STYLECLEARALL); // Copies global style to all others
SetScriptType(SCRIPTTYPE_NONE);
// SetScriptType(SCRIPTTYPE_AVS);
// Toff ----->
OldAVSViewWinProc = (WNDPROC)SetWindowLongPtr(hwndView, GWLP_WNDPROC, (LPARAM)SubAVSEditorWndProc);
// <----- Toff
//if (lpszFileName[0] != 0) Open();
hwndFind = NULL;
mFind.szFindString[0] = 0;
mFind.bFindReverse = false;
mFind.bWholeWord = false;
mFind.bMatchCase = false;
mFind.bRegexp = false;
mFind.bWrap = true;
bModified = false;
DragAcceptFiles(hwnd, TRUE);
AVSViewerLoadSettings(hwnd,REG_WINDOW_MAIN);
SetScriptType(scriptType);
}
void AVSEditor::UpdateLineNumbers() {
SendMessageSci(SCI_SETMARGINTYPEN, 1, SC_MARGIN_NUMBER);
char *gw = "9";
int pixelWidth = 4 + 5 * SendMessageSci(SCI_TEXTWIDTH, STYLE_LINENUMBER, (LPARAM) gw);
SendMessageSci(SCI_SETMARGINWIDTHN, 1, bLineNumbers?pixelWidth:0);
}
void AVSEditor::Open(const wchar_t* path) {
if(path) wcscpy(lpszFileName, path);
unsigned char *lpszBuf;
FILE *f;
size_t n, r;
lpszBuf = NULL;
if ((f = _wfopen(lpszFileName, L"rb")) == NULL) return;
fseek(f, 0, SEEK_END);
r = ftell(f);
fseek(f, 0, SEEK_SET);
lpszBuf = (unsigned char *) malloc(r+1);
n=fread(lpszBuf, sizeof( char ), r, f);
*(lpszBuf+n) = (char) 0;
fclose(f);
// SendMessage(hwndView, WM_SETTEXT, 0, (LPARAM) lpszBuf);
SendMessageSci(SCI_CLEARALL, 0, 0);
if (lpszBuf[0]==0xef && lpszBuf[1]==0xbb && lpszBuf[2]==0xbf) {
SendMessageSci(SCI_SETCODEPAGE, SC_CP_UTF8);
SendMessageSci(SCI_SETTEXT, 0, (LPARAM) lpszBuf+3);
} else {
SendMessageSci(SCI_SETCODEPAGE, 0);
SendMessageSci(SCI_SETTEXT, 0, (LPARAM) lpszBuf);
}
free(lpszBuf);
char buf[512];
wsprintf(buf, "VirtualDub2 Script Editor - [%ls]", lpszFileName);
SetWindowText(hwnd, buf);
SendMessageSci(SCI_SETWRAPMODE, g_VDMPrefs.m_bWrapLines ? SC_WRAP_WORD:SC_WRAP_NONE);
SetAStyle(STYLE_DEFAULT, RGB(0,0,0), RGB(0xff,0xff,0xff), g_VDMPrefs.mAVSViewerFontSize, g_VDMPrefs.mAVSViewerFontFace.c_str());
SendMessageSci(SCI_STYLECLEARALL); // Copies global style to all others
/* if (!_stricmp(strrchr((lpszFileName), (int) '.'), ".avs")) {
SetScriptType(SCRIPTTYPE_AVS);
} else {
SetScriptType(SCRIPTTYPE_NONE);
}*/
SetScriptType(GetScriptType(lpszFileName));
}
void AVSEditor::HandleError(const char* s, int line){
SendMessageSci(SCI_GOTOLINE, line-1);
char* s1 = _strdup(s);
PostMessage(hwnd,WM_DEFER_ERROR,0,(LPARAM)s1);
}
void AVSEditor::SetScriptType(int type){
scriptType = type;
SendMessageSci(SCI_SETWRAPMODE, g_VDMPrefs.m_bWrapLines ? SC_WRAP_WORD:SC_WRAP_NONE);
SetAStyle(STYLE_DEFAULT, RGB(0,0,0), RGB(0xff,0xff,0xff), g_VDMPrefs.mAVSViewerFontSize, g_VDMPrefs.mAVSViewerFontFace.c_str());
SendMessageSci(SCI_STYLECLEARALL); // Copies global style to all others
switch(type) {
case SCRIPTTYPE_AVS: {
init_avs();
/*if (!g_dllAVSLexer->ok) {
guiMessageBox(hwnd, IDS_ERR_NO_AVSLEXER, IDS_ERR_NO_AVSLEXER_CAP, MB_OK|MB_ICONERROR);
}*/
//SendMessageSci(SCI_SETLEXERLANGUAGE, 0, (int) "avslex");
SendMessageSci(SCI_SETLEXER, SCLEX_AVS);
//SendMessageSci(SCI_SETSTYLEBITS, 7);
SendMessageSci(SCI_CLEARREGISTEREDIMAGES);
SendMessageSci(SCI_REGISTERIMAGE, ICO_SCI_AVS_KEYWORDS, (LPARAM)imKeywords);
SendMessageSci(SCI_REGISTERIMAGE, ICO_SCI_AVS_INTERNAL, (LPARAM)imInternal);
SendMessageSci(SCI_REGISTERIMAGE, ICO_SCI_AVS_EXTERNAL, (LPARAM)imExternal);
SendMessageSci(SCI_SETKEYWORDS, 0, (LPARAM) g_dllAviSynth->coKeywords);
SendMessageSci(SCI_SETKEYWORDS, 1, (LPARAM) g_dllAviSynth->coInternal);
if (g_dllAviSynth->coExternal) SendMessageSci(SCI_SETKEYWORDS, 2, (LPARAM) g_dllAviSynth->coExternal);
SendMessageSci(SCI_SETTABWIDTH, 4, 0);
const COLORREF black = RGB(0, 0, 0);
const COLORREF white = RGB(0xFF, 0xFF, 0xFF);
const COLORREF red = RGB(0xFF, 0, 0);
const COLORREF offWhite = RGB(0xFF, 0xFB, 0xF0);
const COLORREF darkGreen = RGB(0, 0x80, 0);
const COLORREF darkBlue = RGB(0, 0, 0x80);
const COLORREF darkRed = RGB(0x80,0,0);
const COLORREF darkViolet = RGB(0x80,0,0x80);
SetAStyle(SCE_AVS_DEFAULT, black, white, g_VDMPrefs.mAVSViewerFontSize, g_VDMPrefs.mAVSViewerFontFace.c_str());
SetAStyle(SCE_AVS_NUMBER, darkBlue);
// Commands
SetAStyle(SCE_AVS_DEFAULT, black);
SendMessageSci(SCI_STYLESETBOLD, SCE_AVS_DEFAULT, 1);
SetAStyle(SCE_AVS_KEYWORD, darkGreen);
SendMessageSci(SCI_STYLESETBOLD, SCE_AVS_KEYWORD, 1);
SetAStyle(SCE_AVS_PLUGIN, darkViolet);
SendMessageSci(SCI_STYLESETBOLD, SCE_AVS_PLUGIN, 1);
SetAStyle(SCE_AVS_FILTER, darkViolet);
SendMessageSci(SCI_STYLESETBOLD, SCE_AVS_FILTER, 1);
// Comment
SetAStyle(SCE_AVS_COMMENTBLOCK, RGB(0xAA,0xAA,0xAA));
SetAStyle(SCE_AVS_COMMENTBLOCKN, RGB(0xAA,0xAA,0xAA));
SetAStyle(SCE_AVS_COMMENTLINE, RGB(0xAA,0xAA,0xAA));
//SendMessageSci(SCI_STYLESETITALIC, SCE_AVS_COMMENT, 1);
SetAStyle(SCE_AVS_OPERATOR, darkBlue);
SetAStyle(SCE_AVS_STRING, darkRed);
//SetAStyle(SCE_AVS_FUZZY, red);
//SetAStyle(SCE_AVS_ERROR, white, red);
SetAStyle(34, white, black);
SetAStyle(35, white, red);
SendMessageSci(SCI_AUTOCSETIGNORECASE, TRUE);
SendMessageSci(SCI_AUTOCSETFILLUPS, 0, (LPARAM) " (,.");
}
break;
case SCRIPTTYPE_DECOMB: {
scriptType = SCRIPTTYPE_DECOMB;
SendMessageSci(SCI_SETLEXER, SCLEX_NULL);
}
break;
case SCRIPTTYPE_VPS: {
scriptType = SCRIPTTYPE_VPS;
SendMessageSci(SCI_SETLEXER, SCLEX_PYTHON);
SendMessageSci(SCI_SETTABWIDTH, 4, 0);
const COLORREF black = RGB(0, 0, 0);
const COLORREF white = RGB(0xFF, 0xFF, 0xFF);
const COLORREF darkGreen = RGB(0, 0x80, 0);
const COLORREF darkBlue = RGB(0, 0, 0x80);
const COLORREF darkRed = RGB(0x80,0,0);
SetAStyle(SCE_P_DEFAULT, black, white, g_VDMPrefs.mAVSViewerFontSize, g_VDMPrefs.mAVSViewerFontFace.c_str());
SendMessageSci(SCI_STYLESETBOLD, SCE_P_DEFAULT, 1);
SetAStyle(SCE_P_WORD, darkGreen);
SendMessageSci(SCI_STYLESETBOLD, SCE_P_WORD, 1);
// Comment
SetAStyle(SCE_P_COMMENTBLOCK, RGB(0xAA,0xAA,0xAA));
SetAStyle(SCE_P_COMMENTLINE, RGB(0xAA,0xAA,0xAA));
SetAStyle(SCE_P_OPERATOR, darkBlue);
SetAStyle(SCE_P_STRING, darkRed);
}
break;
case SCRIPTTYPE_VDSCRIPT: {
scriptType = SCRIPTTYPE_VDSCRIPT;
SendMessageSci(SCI_SETLEXER, SCLEX_NULL);
}
break;
default: {
scriptType = SCRIPTTYPE_NONE;
SendMessageSci(SCI_SETLEXER, SCLEX_NULL);
}
}
SendMessageSci(SCI_COLOURISE, 0, -1);
UpdateStatus();
}
bool AVSEditor::Commit() {
int s;
char *lpszBuf;
FILE *f;
//GETTEXTLENGTHEX gte;
// GETTEXTEX gt;
if (lpszFileName[0] == 0) {
char szName[MAX_PATH];
OPENFILENAME ofn;
szName[0] = 0;
memset(&ofn, 0, sizeof ofn);
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "All files (*.*)\0*.*\0";
ofn.lpstrCustomFilter = NULL;
ofn.nFilterIndex = 1;
ofn.lpstrFile = szName;
ofn.nMaxFile = sizeof szName;
ofn.lpstrFileTitle = NULL;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = NULL;
ofn.Flags = OFN_EXPLORER | OFN_ENABLESIZING;
ofn.lpstrDefExt = NULL;
if (GetSaveFileName(&ofn)) {
VDTextAToW(lpszFileName, MAX_PATH, szName, MAX_PATH);
//wcscpy(lpszFileName, VDTextAToW(szName).c_str());
} else {
return false;
}
}
// s = SendMessage(hwndView, WM_GETTEXTLENGTH, 0, 0);
int cp = SendMessageSci(SCI_GETCODEPAGE);
s = SendMessageSci(SCI_GETTEXTLENGTH, 0, 0);
lpszBuf = (char *) malloc(s+1);
// gt.cb = s;
// gt.flags = GT_USECRLF;
// gt.codepage = CP_ACP;
// gt.lpDefaultChar = NULL;
// gt.lpUsedDefChar = NULL;
// SendMessage(hwndView, EM_GETTEXTEX, (WPARAM) >, (LPARAM) lpszBuf);
SendMessageSci(SCI_GETTEXT, s+1, (LPARAM) lpszBuf);
f = _wfopen(lpszFileName, L"wb");
if (cp==SC_CP_UTF8)
fwrite("\xef\xbb\xbf", 1, 3, f);
fwrite(lpszBuf, sizeof( char ), s, f);
fclose(f);
free(lpszBuf);
bModified = false;
return true;
}
void AVSEditor::SetStatus(const char *format, ...) throw() {
char buf[1024];
va_list val;
va_start(val,format);
_vsnprintf(buf, sizeof buf, format, val);
va_end(val);
SetWindowText(hwndStatus, buf);
}
void AVSEditor::UpdateStatus() throw() {
char buf[1024];
int posx, posy, c;
// POINT pt;
//SendMessage(hwndView, EM_GETSEL, (WPARAM) &posx, (LPARAM) &posy);
// GetCaretPos(&pt);
// c = SendMessage(hwndView, EM_CHARFROMPOS, 0, (LPARAM) &pt);
// posy = SendMessage(hwndView, EM_LINEFROMCHAR, c, 0);
// posx = c - SendMessage(hwndView, EM_LINEINDEX, posy, 0);
c = SendMessageSci(SCI_GETCURRENTPOS, 0, 0);
posy = SendMessageSci(SCI_LINEFROMPOSITION, c, 0);
posx = c - SendMessageSci(SCI_POSITIONFROMLINE, posy, 0);
wsprintf(buf, "%d:%d", posy+1, posx+1);
SendMessage(hwndStatus, SB_SETTEXT, 2, (LPARAM) &buf);
SendMessage(hwndStatus, SB_SETTEXT, 1, (LPARAM) scripttypeName[scriptType]);
}
void AVSEditor::SetAStyle(int style, COLORREF fore, COLORREF back, int size, const char *face) {
SendMessageSci(SCI_STYLESETFORE, style, fore);
SendMessageSci(SCI_STYLESETBACK, style, back);
if (size >= 1)
SendMessageSci(SCI_STYLESETSIZE, style, size);
if (face)
SendMessageSci(SCI_STYLESETFONT, style, (LPARAM)face);
}
LRESULT AVSEditor::Handle_WM_COMMAND(WPARAM wParam, LPARAM lParam) throw() {
if ((HWND) lParam == hwndView) {
/* switch (HIWORD(wParam)) {
case SCEN_CHANGE:
CheckBracing();
break;
}
*/ } else switch(LOWORD(wParam)) {
case ID_FILE_EXIT:
DestroyWindow(hwnd);
break;
case ID_FILE_OPEN:
{
wchar_t szName[MAX_PATH];
OPENFILENAMEW ofn;
szName[0] = 0;
memset(&ofn, 0, sizeof ofn);
ofn.lStructSize = sizeof(OPENFILENAMEW);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = L"All files (*.*)\0*.*\0";
ofn.lpstrCustomFilter = NULL;
ofn.nFilterIndex = 1;
ofn.lpstrFile = szName;
ofn.nMaxFile = sizeof szName;
ofn.lpstrFileTitle = NULL;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = NULL;
ofn.Flags = OFN_EXPLORER | OFN_ENABLESIZING;
ofn.lpstrDefExt = NULL;
if (GetOpenFileNameW(&ofn)) {
//VDTextAToW(lpszFileName, MAX_PATH, szName, MAX_PATH);
//wcscpy(lpszFileName, VDTextAToW(szName).c_str());
Open(szName);
}
}
break;
case ID_FILE_NEW:
{
lpszFileName[0] = 0;
SendMessageSci(SCI_CLEARALL, 0, 0);
// SetScriptType(SCRIPTTYPE_NONE);
SetScriptType(SCRIPTTYPE_AVS);
SetWindowText(hwnd, "VirtualDub2 Script Editor");
}
break;
case ID_REFRESH:
{
if (Commit())
VDSendReopen(lpszFileName, this);
}
break;
case ID_AVS_SAVE_OPEN:
{
if (Commit()) {
VDSetFilename(lpszFileName,this);
}
}
break;
case ID_AVS_FILE_SAVE:
Commit();
break;
case ID_FILE_REVERT:
if (IDOK==guiMessageBox(hwnd, IDS_WARN_AVS_DISCARD_CHANGE, IDS_WARN_AVS_DISCARD_CHANGE_CAP, MB_OKCANCEL)) {
Open(0);
}
break;
case ID_EDIT_UNDO:
{
SendMessageSci(SCI_UNDO, 0, 0);
}
break;
case ID_EDIT_REDO:
{
SendMessageSci(SCI_REDO, 0, 0);
}
break;
case ID_EDIT_GOTO:
Jumpto();
break;
case ID_EDIT_FIND:
Find();
break;
case ID_EDIT_FINDNEXT:
FindNext(false);
break;
case ID_EDIT_FINDPREV:
FindNext(true);
break;
case ID_AVS_INSERT_POS:
{
int64 p = VDRequestPos();
char buf[50];
wsprintf(buf, "%I64d", p);
SendMessage(hwndView, SCI_REPLACESEL, 0, (LPARAM) &buf);
}
break;
case ID_AVS_INSERT_RANGE:
case ID_AVS_INSERT_TRIM:
{
bool trim = false;
if(LOWORD(wParam)==ID_AVS_INSERT_TRIM) trim = true;
int64 r0,r1;
VDRequestRange(r0,r1);
if (r1-r0<=0) {
guiMessageBox(hwnd, IDS_ERR_AVS_NORANGE, IDS_ERR_CAPTION, MB_OK|MB_ICONERROR);
break;
}
char buf[50];
if (scriptType == SCRIPTTYPE_NONE) {
wsprintf(buf, "%I64d-%I64d", r0, r1);
}
if (scriptType == SCRIPTTYPE_VDSCRIPT) {
if (trim)
wsprintf(buf, "VirtualDub.subset.AddRange(%I64d,%I64d);", r0, r1);
else
wsprintf(buf, "%I64d,%I64d", r0, r1);
}
if (scriptType == SCRIPTTYPE_AVS || scriptType == SCRIPTTYPE_DECOMB) {
if (r0 == 0 && r1 == 1)
wsprintf(buf, (trim)?"Trim(%d,%d)":"%d,%d", 0, -1);// special case of very first frame
else
wsprintf(buf, (trim)?"Trim(%I64d,%I64d)":"%I64d,%I64d", r0, r1 -1);
}
if (scriptType == SCRIPTTYPE_VPS) {
if (r1 == r0+1)
wsprintf(buf, (trim)?"clip[%d]":"%d", r0);
else
wsprintf(buf, (trim)?"clip[%I64d:%I64d]":"%I64d:%I64d", r0, r1);
}
SendMessage(hwndView, SCI_REPLACESEL, 0, (LPARAM) &buf);
}
break;
case ID_AVS_INSERT_FRAMESET:
{
vd_frameset set;
vd_basic_range rbuf[1024];
set.ranges = rbuf;
VDRequestFrameset(set,1024);
if (set.count==0) {
guiMessageBox(hwnd, IDS_ERR_AVS_NOFRAMESET, IDS_ERR_CAPTION, MB_OK|MB_ICONERROR);
break;
}
string buffer;
char buf[50];
for(int i = 0; i<set.count; i++) {
vd_basic_range range = set.ranges[i];
if (scriptType == SCRIPTTYPE_NONE) {
wsprintf(buf, "Trim(%I64d,%I64d)", range.from, range.to);
if (i>0) buffer += " + ";
}
if (scriptType == SCRIPTTYPE_AVS || scriptType == SCRIPTTYPE_DECOMB) {
if (range.from == 0 && range.to == 1)
wsprintf(buf, "Trim(%d,%d)", 0, -1); // special case of one very first frame
else
wsprintf(buf, "Trim(%I64d,%I64d)", range.from, range.to - 1);
if (i>0) buffer += " ++ ";
}
if (scriptType == SCRIPTTYPE_VPS) {
if (range.to==range.from+1)
wsprintf(buf, "clip[%I64d]", range.from);
else
wsprintf(buf, "clip[%I64d:%I64d]", range.from, range.to);
if (i>0) buffer += " + ";
}
if (scriptType == SCRIPTTYPE_VDSCRIPT) {
wsprintf(buf, "VirtualDub.subset.AddRange(%I64d,%I64d);", range.from, range.to);
if (i>0) buffer += "\r\n";
}
buffer += buf;
}
SendMessage(hwndView, SCI_REPLACESEL, 0, (LPARAM) buffer.c_str());
}
break;
case ID_AVS_INSERT_FILENAME:
{
char szName[MAX_PATH];
char buf[MAX_PATH+2];
OPENFILENAME ofn;
szName[0] = 0;
memset(&ofn, 0, sizeof ofn);
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "All files (*.*)\0*.*\0";
ofn.lpstrCustomFilter = NULL;
ofn.nFilterIndex = 1;
ofn.lpstrFile = szName;
ofn.nMaxFile = sizeof szName;
ofn.lpstrFileTitle = NULL;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = NULL;
ofn.Flags = OFN_EXPLORER | OFN_ENABLESIZING;
ofn.lpstrDefExt = NULL;
if (GetOpenFileName(&ofn)) {
if (scriptType == SCRIPTTYPE_NONE)
wsprintf(buf, "%s", szName);
else
wsprintf(buf, "\"%s\"", szName);
SendMessageSci(SCI_REPLACESEL, 0, (LPARAM) &buf);
}
}
break;
case ID_AVS_INSERT_CROP:
{
vd_framesize frame;
VDRequestFrameSize(frame);
char buf[50];
wsprintf(buf, "Crop(%d,%d,%d,%d)", frame.frame.left, frame.frame.top, frame.frame.right, frame.frame.bottom);
SendMessage(hwndView, SCI_REPLACESEL, 0, (LPARAM) &buf);
}
break;
case ID_AVS_EDIT_LINE:
{
bLineNumbers = !bLineNumbers;
UpdateLineNumbers();
CheckMenuItem(GetMenu(hwnd), ID_AVS_EDIT_LINE, MF_BYCOMMAND | bLineNumbers?MF_CHECKED:MF_UNCHECKED);
}
break;
case ID_AVS_EDIT_OPTIONS:
ShowPrefs(hwnd);
break;
case ID_AVS_SCRIPT_NONE:
{
SetScriptType(SCRIPTTYPE_NONE);
}
break;
case ID_AVS_SCRIPT_AVS:
{
SetScriptType(SCRIPTTYPE_AVS);
}
break;
case ID_AVS_SCRIPT_DECOMB:
{
SetScriptType(SCRIPTTYPE_DECOMB);
}
break;
case ID_AVS_SCRIPT_VPS: