forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowsMisc.pb
851 lines (654 loc) · 34.8 KB
/
WindowsMisc.pb
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
;--------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
;--------------------------------------------------------------------------------------------
;windows only
CompilerIf #CompileWindows
#NM_CUSTOMDRAW = #NM_FIRST - 12
#CDDS_ITEM = $10000
#CDDS_PREPAINT = $1
#CDDS_ITEMPREPAINT = #CDDS_ITEM | #CDDS_PREPAINT
#CDRF_DODEFAULT = $0
#CDRF_NOTIFYITEMDRAW = $20
#ODS_NOACCEL = $100
Prototype GlobalMemoryStatusExPrototype(*MemoryStatus.MEMORYSTATUSEX)
Global DebuggerSystemMenu
#ERROR_ALREADY_EXISTS = $B7
#WM_UNINITMENUPOPUP = $125
Global RunOnceMessageID, RunOnceMutex
Global AutoCompleteWindowProc
Global SessionMutex
Global StatusbarBoldFontID
Global OSVersion = OSVersion() ; Used to disactivate the ownerdraw menus on NT4
Procedure UpdateRegistryKey(Key, Path$, Value$)
NeedUpdate = 1 ; By default, the key is changed, unless we found exactly the same string
If RegCreateKeyEx_(Key, Path$, 0, #Null$, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, 0, @NewKey, @KeyInfo) = #ERROR_SUCCESS
; Get the current value, and check if we need to update it
;
CurrentValue$ = Space(#MAX_PATH*2) ; Ensure it will be big enough for a path + some infos
CurrentValueSize = Len(CurrentValue$)*#CharSize
If RegQueryValueEx_(NewKey, "", 0, @Type, @CurrentValue$, @CurrentValueSize) = #ERROR_SUCCESS
If Value$ = CurrentValue$
NeedUpdate = 0
EndIf
EndIf
If NeedUpdate
RegSetValueEx_(NewKey, "", 0, #REG_SZ, @Value$, (Len(Value$)+1) * #CharSize)
EndIf
RegCloseKey_(NewKey)
EndIf
ProcedureReturn NeedUpdate
EndProcedure
Procedure OSStartupCode()
Shared DontCreateExtensions
; This creates the mutex, or opens an existing one, which is perfect for us.
; The mutex will exist until all programs that called this have ended.
; There is no CloseHandle_(), so the mutex stays open until the program ends
; (the Global\ part is for Terminal Services only, but ignored else)
;
; NOTE: Do not use any namespace prefix, although "Global\" would make sense here,
; as \ is an illegal character for the name on NT and Win9x.
;
CreateMutex_(#Null, #False, #ProductName$+"_Running")
LineNumbersCursor = LoadCursor_(0, #IDC_ARROW)
; Ensure it won't happen in release build !
CompilerIf #PB_Compiler_Debugger
CompilerIf Defined(FredLocalCompile, #PB_Constant) ; Fred config
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
CompilerIf #SpiderBasic
PureBasicPath$ = "C:\PureBasic\Svn\"+#SVNVersion+"\Build\SpiderBasic_x64\"
CompilerElse
PureBasicPath$ = "C:\PureBasic\Svn\"+#SVNVersion+"\Build\PureBasic_x64\"
CompilerEndIf
CompilerElse
CompilerIf #SpiderBasic
PureBasicPath$ = "C:\PureBasic\Svn\"+#SVNVersion+"\Build\SpiderBasic_x86\"
CompilerElse
PureBasicPath$ = "C:\PureBasic\Svn\"+#SVNVersion+"\Build\PureBasic_x86\"
CompilerEndIf
CompilerEndIf
CompilerEndIf
PureBasicPath$ = #PB_Compiler_Home
CompilerEndIf
; Set the default Path Values
;
If PureBasicPath$ = "" ; Only change if not set by commandline
PureBasicPath$ = Space(#MAX_PATH)
GetModuleFileName_(GetModuleHandle_(#Null$), @PureBasicPath$, #MAX_PATH)
PureBasicPath$ = GetPathPart(PureBasicPath$)
EndIf
; initialize the scintilla dll. if it does not work, output a proper message,
; otherwise the ide acts quite weird.
;
If InitScintilla(PureBasicPath$+"Compilers\Scintilla.dll") = 0
MessageRequester(#ProductName$, "Cannot initialize Scintilla engine!"+#NewLine+"Make sure the 'Scintilla.dll' is placed in the 'Compilers' subdirectory of your "+#ProductName$+" setup.", #FLAG_Error)
End
EndIf
TempPath$ = GetTemporaryDirectory()
If PreferencesFile$ = "" ; Only change if not set by commandline
PreferencesFile$ = PureBasicConfigPath() + #PreferenceFileName$
EndIf
If AddToolsFile$ = "" ; Only change if not set by commandline
AddToolsFile$ = PureBasicConfigPath() + "Tools.prefs"
EndIf
If TemplatesFile$ = "" ; Only change if not set by commandline
TemplatesFile$ = PureBasicConfigPath() + "Templates.prefs"
EndIf
If HistoryDatabaseFile$ = "" ; Only change if not set by commandline
HistoryDatabaseFile$ = PureBasicConfigPath() + "History.db"
EndIf
If SourcePathSet = 0 ; Only change if not set by commandline
; Starting with Windows Vista, we can't write in ProgramFiles anymore, so the examples are copied in the common app user data by the installer.
; It's important to point on the examples when the path is not set as when your run PureBasic for the first
; time, you want to open examples to test them. This value can be changed in the prefs and won't be
; used if another pref is found
;
If SHGetSpecialFolderLocation_(0, #CSIDL_COMMON_APPDATA, @*pidlMyDocuments) = #S_OK
SourcePath$ = Space(#MAX_PATH)
If SHGetPathFromIDList_(*pidlMyDocuments, @SourcePath$)
SourcePath$ + "\" + #ProductName$ + "\Examples\" ; We be something like: C:\ProgramData\SpiderBasic\Examples
Else ; Failed
SourcePath$ = PureBasicPath$
EndIf
Else
SourcePath$ = PureBasicPath$
EndIf
If Right(SourcePath$, 1) <> #Separator
SourcePath$ + #Separator
EndIf
EndIf
If DontCreateExtensions = 0
ExecutableName$ = GetFilePart(ProgramFilename()) ; PureBasic.exe or SpiderBasic.exe
; Create .pb, .pbi, .pbp and .pbf association
; We include the position of preferences etc in the registry commandline
; so switches like /LOCAL will also effect later opened files.
;
CommandLine$ = Chr(34) + ProgramFilename() + Chr(34)
CommandLine$ + " " + Chr(34) + "%1" + Chr(34)
CommandLine$ + " /P " + Chr(34) + PreferencesFile$ + Chr(34)
CommandLine$ + " /T " + Chr(34) + TemplatesFile$ + Chr(34)
CommandLine$ + " /A " + Chr(34) + AddToolsFile$ + Chr(34)
CommandLine$ + " /H " + Chr(34) + HistoryDatabaseFile$ + Chr(34)
; Note: since we now include the /P /A etc into the registry commandline,
; Windows does not seem to correctly display the PB icon for files anymore.
; So we must add this to the registry as well
;
; The icon is at index 1. It is the PBSourceFile.ico that is added in the
; resource script created by makeversion.exe
;
Icon$ = ProgramFilename() + ",1"
; Vista and newer - only HKEY_CURRENT_USER is allowed!
; Now also used on XP, as else if an admin runs the IDE, the user will never be able
; to overwrite the admin's setting in HKEY_CLASSES_ROOT!
If OSVersion() >= #PB_OS_Windows_XP
; Funny, this is the exact same code as for Win9x, only with
; #HKEY_CURRENT_USER instead of #HKEY_LOCA_MACHINE ;)
;
NeedUpdate | UpdateRegistryKey(#HKEY_CURRENT_USER, "Software\Classes\" + ExecutableName$ + "\shell\open\command", CommandLine$)
NeedUpdate | UpdateRegistryKey(#HKEY_CURRENT_USER, "Software\Classes\" + ExecutableName$ + "\DefaultIcon", Icon$)
NeedUpdate | UpdateRegistryKey(#HKEY_CURRENT_USER, "Software\CLASSES\" + #SourceFileExtension, ExecutableName$)
NeedUpdate | UpdateRegistryKey(#HKEY_CURRENT_USER, "Software\CLASSES\" + #IncludeFileExtension, ExecutableName$)
NeedUpdate | UpdateRegistryKey(#HKEY_CURRENT_USER, "Software\CLASSES\" + #ProjectFileExtension, ExecutableName$)
NeedUpdate | UpdateRegistryKey(#HKEY_CURRENT_USER, "Software\CLASSES\" + #FormFileExtension, ExecutableName$)
; Until 4.10, XP used the #HKEY_CLASSES_ROOT way like NT below, but this seems
; to dominate the new location in #HKEY_CURRENT_USER, so the 4.10 path will dominate
; any newer setting. Try to delete these keys
;
If OSVersion() = #PB_OS_Windows_XP
; This fails if the key does not exist, and deletes all children if it does exist,
; which is perfect for us.
If SHDeleteKey_(#HKEY_CLASSES_ROOT, "Applications\" + ExecutableName$) = #ERROR_SUCCESS
NeedUpdate | 1
EndIf
If SHDeleteKey_(#HKEY_CLASSES_ROOT, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" + #SourceFileExtension) = #ERROR_SUCCESS
NeedUpdate | 1
EndIf
If SHDeleteKey_(#HKEY_CLASSES_ROOT, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" + #IncludeFileExtension) = #ERROR_SUCCESS
NeedUpdate | 1
EndIf
EndIf
ElseIf GetVersion_() & $ff0000 ; Windows NT
NeedUpdate | UpdateRegistryKey(#HKEY_CLASSES_ROOT, "Applications\" + ExecutableName$ + "\shell\open\command", CommandLine$)
NeedUpdate | UpdateRegistryKey(#HKEY_CLASSES_ROOT, "Applications\" + ExecutableName$ + "\DefaultIcon", Icon$)
NeedUpdate | UpdateRegistryKey(#HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" + #SourceFileExtension, ExecutableName$)
NeedUpdate | UpdateRegistryKey(#HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" + #IncludeFileExtension, ExecutableName$)
NeedUpdate | UpdateRegistryKey(#HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" + #ProjectFileExtension, ExecutableName$)
NeedUpdate | UpdateRegistryKey(#HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" + #FormFileExtension, ExecutableName$)
Else ; The same for Win9x
NeedUpdate | UpdateRegistryKey(#HKEY_LOCAL_MACHINE, "Software\Classes\" + ExecutableName$ + "\shell\open\command", CommandLine$)
NeedUpdate | UpdateRegistryKey(#HKEY_LOCAL_MACHINE, "Software\Classes\" + ExecutableName$ + "\DefaultIcon", Icon$)
NeedUpdate | UpdateRegistryKey(#HKEY_LOCAL_MACHINE, "Software\CLASSES\" + #SourceFileExtension, ExecutableName$)
NeedUpdate | UpdateRegistryKey(#HKEY_LOCAL_MACHINE, "Software\CLASSES\" + #IncludeFileExtension, ExecutableName$)
NeedUpdate | UpdateRegistryKey(#HKEY_LOCAL_MACHINE, "Software\CLASSES\" + #ProjectFileExtension, ExecutableName$)
NeedUpdate | UpdateRegistryKey(#HKEY_LOCAL_MACHINE, "Software\CLASSES\" + #FormFileExtension, ExecutableName$)
EndIf
; We don't do it at every start, as it can take some time, and a bad desktop flikering (if the registry hasn't
; changed since the last run, there is no need refresh anyway)
;
If NeedUpdate
; Notify the shell of the extension change
;
#SHCNE_ASSOCCHANGED = $08000000
#SHCNF_IDLIST = $0000
SHChangeNotify_(#SHCNE_ASSOCCHANGED, #SHCNF_IDLIST, #Null, #Null)
EndIf
EndIf
ButtonBackgroundColor = GetButtonBackgroundColor()
; RunOnce
;
RunOnceMessageID = RegisterWindowMessage_(#ProductName$ + "_RunOnce")
ProcedureReturn 1
EndProcedure
Procedure OSEndCode()
ClosePlatformSDKWindow()
If RunOnceMutex
CloseHandle_(RunOnceMutex)
EndIf
EndProcedure
; windows only helper function. applies the Debugger menu states
; to the systemmenu-debuggermenu
;
Procedure ApplySYSTEMMenuState(MenuItem)
#MIIM_STATE = $1
info.MENUITEMINFO\cbSize = SizeOf(MENUITEMINFO)
info\fMask = #MIIM_STATE
GetMenuItemInfo_(*MainMenu, MenuItem, #False, @info)
If info\fState & #MFS_DISABLED
EnableMenuItem_(DebuggerSystemMenu, MenuItem<<4, #MF_BYCOMMAND|#MF_GRAYED)
Else
EnableMenuItem_(DebuggerSystemMenu, MenuItem<<4, #MF_BYCOMMAND|#MF_ENABLED)
EndIf
EndProcedure
Procedure MainWindowCallback(Window, Message, wParam, lParam)
Shared IsMenuOpen
Shared StatusBarOwnerDrawText$
Result = #PB_ProcessPureBasicEvents
If Message = #WM_DROPFILES ; drag and drop stuff
*hdrop = wParam
count = DragQueryFile_(*hdrop, $FFFFFFFF, 0, 0)
For i = 0 To count-1
size = DragQueryFile_(*hdrop, i, 0, 0)
FileName$ = Space(size)
DragQueryFile_(*hdrop, i, @FileName$, size+1) ; include the 0 byte in the count!
LoadSourceFile(FileName$)
; Flush events. So when many sources are opened at once, the User can see a bit the
; progress, instead of just an unresponsive window for quite a while.
; There is almost no flicker anymore, so it actually looks quite good.
;
; Note: don't put this in the LoadSourceFile() routine as it can be call from the debugger and flushing the event will get another debug event !
FlushEvents()
Next i
DragFinish_(*hdrop)
ElseIf Message = #WM_SYSCOMMAND
wParam & $FFF0 ; mask out the windows internal 4 bits
If wParam < $F000 ; check for our own commands
PostMessage_(WindowID(#WINDOW_Main), #WM_COMMAND, wParam>>4, 0) ; simulate the normal menu command
result = 0
EndIf
;ElseIf Message = #WM_UNINITMENUPOPUP ; This one is Windows 98/2000/XP only (ie: not NT4)
ElseIf Message = #WM_ENTERMENULOOP ; For compatibility with NT4
IsMenuOpen = 1
ElseIf Message = #WM_EXITMENULOOP ; For compatibility with NT4
IsMenuOpen = 0
ElseIf Message = #WM_INITMENUPOPUP ; enable/disable the special systemenu debugger submenu
If wParam = DebuggerSystemMenu
; we simply apply the same state as the real debugger menu has.
ApplySYSTEMMenuState(#MENU_Stop)
ApplySYSTEMMenuState(#MENU_Run)
ApplySYSTEMMenuState(#MENU_Step)
ApplySYSTEMMenuState(#MENU_StepX)
ApplySYSTEMMenuState(#MENU_StepOver)
ApplySYSTEMMenuState(#MENU_StepOut)
ApplySYSTEMMenuState(#MENU_Kill)
ApplySYSTEMMenuState(#MENU_DebugOutput)
ApplySYSTEMMenuState(#MENU_Watchlist)
ApplySYSTEMMenuState(#MENU_VariableList)
ApplySYSTEMMenuState(#MENU_History)
ApplySYSTEMMenuState(#MENU_Memory)
ApplySYSTEMMenuState(#MENU_DebugAsm)
ApplySYSTEMMenuState(#MENU_Profiler)
ApplySYSTEMMenuState(#MENU_LibraryViewer)
Result = 0
EndIf
; allow external programs to access the Log (ie TailBite)
; Now also handles the RunOnce feature's data
;
ElseIf Message = #WM_COPYDATA
*copydata.COPYDATASTRUCT = lParam
If *copydata\dwData = AsciiConst3('L', 'O', 'G') And *ActiveSource And *copydata\cbData > 0
Message$ = PeekS(*copydata\lpData, *copydata\cbData, #PB_Ascii)
Debugger_AddLog_BySource(*ActiveSource, Message$, Date())
result = #True
ElseIf *copydata\dwData = AsciiConst('O', 'N', 'C', 'E') And *copydata\cbData > 0 ; RunOnce files are sent
List$ = PeekS(*copydata\lpData, *copydata\cbData/#CharSize)
Sender$ = StringField(List$, 1, Chr(10))
; Check if this message is for this instance
;
If IsEqualFile(Sender$, ProgramFilename())
; Send a response back to the sender that it can quit now
PostMessage_(wParam, RunOnceMessageID, AsciiConst('D', 'O', 'N', 'E'), WindowID(#WINDOW_Main))
; Open the files
Count = Val(StringField(List$, 2, Chr(10)))
Line = Val(StringField(List$, 3, Chr(10))) ; for -l option
For i = 1 To Count
FileName$ = StringField(List$, 3+i, Chr(10))
If FileName$ <> ""
LoadSourceFile(FileName$)
; Flush events. So when many sources are opened at once, the User can see a bit the
; progress, instead of just an unresponsive window for quite a while.
; There is almost no flicker anymore, so it actually looks quite good.
;
; Note: don't put this in the LoadSourceFile() routine as it can be call from the debugger and flushing the event will get another debug event !
FlushEvents()
EndIf
Next i
If Line > 0 And Line < GetLinesCount(*ActiveSource) ; apply the -l option
ChangeActiveLine(Line, -5)
EndIf
; NOTE:
; Do not call SetWindowForeground_Real() as it does the ugly hack to attach to the foreground
; Window's queue, which will crash the IDE if the foreground window hangs.
; Steps to reproduce:
; - run a PB program (with GUI) which does "RunProgram(some PB source)"
; - the program and the IDE will crash. (https://www.purebasic.fr/english/viewtopic.php?t=36934)
;
; Instead we use the normal SetWindowForeground() here, and it even works:
; - the starting program has focus
; - when it launches the RunOnce IDE instance, that instance has foreground rights too
; - the RunOnce instance calls AllowWindowSetForeground() to give the rights to this instance
; - all works (the old IDE ends up with focus)
;
; The only case where this does not work is if a background program tries to run the IDE
; but this is the problem of the background program, not of the IDE. This works fine with
; Explorer double-clicks and the like which is the important part
;
SetWindowForeground(#WINDOW_Main)
EndIf
result = #True
ElseIf *copydata\dwData = AsciiConst('A', 'U', 'T', '1') And *copydata\cbData > 0
; Automation request. Use new values like 'AUT2' for new/incompatible requests
; that are added at a later time (there is no 'version check' or similar)
; See also DispatchEvent() in UserInterface.pb
ProcessAutomationRequest(wParam, lParam)
result = #True
ElseIf *copydata\dwData = AsciiConst('A', 'E', 'X', 'E') And *copydata\cbData > 0
; Request to check executable filename. This is done during the connection of the
; automation, so it is processed specially (for simplicity)
Filename$ = PeekS(*copydata\lpData, -1, #PB_UTF8)
If IsEqualFile(Filename$, ProgramFilename())
; send positive response
PostMessage_(wParam, RunOnceMessageID, AsciiConst3('A', 'O', 'K'), WindowID(#WINDOW_Main))
EndIf
result = #True
EndIf
ElseIf Message = #WM_CONTEXTMENU
If *ActiveSource <> *ProjectInfo And wParam = GadgetID(*ActiveSource\EditorGadget)
DisplayPopupMenu(#POPUPMENU, WindowID(#WINDOW_Main))
EndIf
ElseIf Message = #WM_DRAWITEM
*lpdis.DRAWITEMSTRUCT = lParam
If wParam <> 0 ; ownerdrawn statusbar field
TextStart$ = Left(StatusBarOwnerDrawText$, *lpdis\itemData&$FFFF)
TextBold$ = Mid(StatusBarOwnerDrawText$, (*lpdis\itemData&$FFFF)+1, (*lpdis\itemData>>16)-(*lpdis\itemData&$FFFF)-1)
TextEnd$ = Right(StatusBarOwnerDrawText$, Len(StatusBarOwnerDrawText$)-(*lpdis\itemData>>16)+1)
SetBkMode_(*lpdis\hDC, #TRANSPARENT)
CopyMemory(@*lpdis\rcItem, @DrawTextRect.RECT, SizeOf(RECT))
GetTextExtentPoint32_(*lpdis\hDC, TextStart$, Len(TextStart$), @sizestart.SIZE)
DrawTextRect\left+1
DrawText_(*lpdis\hDC, @TextStart$, Len(TextStart$), DrawTextRect, #DT_VCENTER | #DT_SINGLELINE)
hOldObject = SelectObject_(*lpdis\hDC, StatusbarBoldFontID)
GetTextExtentPoint32_(*lpdis\hDC, TextBold$, Len(TextBold$), @sizebold.SIZE)
DrawTextRect\left+sizestart\cx
DrawText_(*lpdis\hDC, @TextBold$, Len(TextBold$), DrawTextRect, #DT_VCENTER | #DT_SINGLELINE)
If hOldObject
SelectObject_(*lpdis\hDC, hOldObject)
EndIf
DrawTextRect\left+sizebold\cx
DrawText_(*lpdis\hDC, @TextEnd$, Len(TextEnd$), DrawTextRect, #DT_VCENTER | #DT_SINGLELINE)
EndIf
result = #True
ElseIf Message = #WM_QUERYENDSESSION
; session is about to end, check for unsaved sources / close them
; only checks/asks, does not close sources
; returns 1 when not aported, so the shutdown can continue
If Window = WindowID(#WINDOW_Main) ; received on multiple windows, so respond only once!
result = CheckAllSourcesSaved()
EndIf
ElseIf Message = #WM_ENDSESSION
; only if wParam is true there is a real shutdown. (else it is an aborted one)
; do not call ShutdownIDE() for an aborted one, as it leaves the IDE in
; an invalid state (any further user action may cause a crash!)
If wParam And Window = WindowID(#WINDOW_Main)
; orderly shutdown of the IDE.
; Note that asking/saving sources is done on the WM_QUERYENDSESSION already
ShutdownIDE()
EndIf
EndIf
ProcedureReturn Result
EndProcedure
; Windows only:
; Adds the debugger commands to the Systemmenu, making them accessible by rightclicking
; on the TaskbarButton too
;
Procedure CreateSYSTEMMenu()
Static hSubMenu
If hSubMenu
DestroyMenu_(hSubMenu) ; destroy old submenu
hSubMenu = 0
EndIf
GetSystemMenu_(WindowID(#WINDOW_Main), #True) ; revert changes done the last time
hMenu = GetSystemMenu_(WindowID(#WINDOW_Main), #False) ; copy the systemenu
hSubMenu = CreatePopupMenu_()
If hMenu And hSubMenu
; Menu ids in the sysmenu should be below $F000 and above $000F (as the low 4 bits are masked out)
;
AppendMenu_(hSubMenu, #MF_STRING, #MENU_Stop<<4, LanguageStringAddress("MenuItem","Stop"))
AppendMenu_(hSubMenu, #MF_STRING, #MENU_Run<<4, LanguageStringAddress("MenuItem","Run"))
AppendMenu_(hSubMenu, #MF_STRING, #MENU_Kill<<4, LanguageStringAddress("MenuItem","Kill"))
AppendMenu_(hSubMenu, #MF_SEPARATOR, 0, @"")
AppendMenu_(hSubMenu, #MF_STRING, #MENU_Step<<4, LanguageStringAddress("MenuItem","Step"))
AppendMenu_(hSubMenu, #MF_STRING, #MENU_StepX<<4, LanguageStringAddress("MenuItem","StepX"))
AppendMenu_(hSubMenu, #MF_STRING, #MENU_StepOver<<4, LanguageStringAddress("MenuItem","StepOver"))
AppendMenu_(hSubMenu, #MF_STRING, #MENU_StepOut<<4, LanguageStringAddress("MenuItem","StepOut"))
AppendMenu_(hSubMenu, #MF_SEPARATOR, 0, @"")
AppendMenu_(hSubMenu, #MF_STRING, #MENU_DebugOutput<<4, LanguageStringAddress("MenuItem","DebugOutput"))
AppendMenu_(hSubMenu, #MF_STRING, #MENU_Watchlist<<4, LanguageStringAddress("MenuItem","WatchList"))
AppendMenu_(hSubMenu, #MF_STRING, #MENU_VariableList<<4, LanguageStringAddress("MenuItem","VariableList"))
AppendMenu_(hSubMenu, #MF_STRING, #MENU_Profiler<<4, LanguageStringAddress("MenuItem","Profiler"))
AppendMenu_(hSubMenu, #MF_STRING, #MENU_History<<4, LanguageStringAddress("MenuItem","History"))
AppendMenu_(hSubMenu, #MF_STRING, #MENU_Memory<<4, LanguageStringAddress("MenuItem","Memory"))
AppendMenu_(hSubMenu, #MF_STRING, #MENU_LibraryViewer<<4, LanguageStringAddress("MenuItem","LibraryViewer"))
AppendMenu_(hSubMenu, #MF_STRING, #MENU_DebugAsm<<4, LanguageStringAddress("MenuItem","DebugAsm"))
; insert into system menu
;
InsertMenu_(hMenu, #SC_CLOSE, #MF_BYCOMMAND|#MF_POPUP|#MF_STRING, hSubMenu, LanguageStringAddress("MenuTitle","Debugger"))
InsertMenu_(hMenu, #SC_CLOSE, #MF_BYCOMMAND|#MF_SEPARATOR, 0, @"")
DebuggerSystemMenu = hSubMenu ; the value is needed in the mainWindowCallback too
EndIf
EndProcedure
; determine things like StatusBarHeight once after the GUI is created.
Procedure GetWindowMetrics()
GetWindowRect_(*MainToolBar, @Size.RECT)
ToolbarHeight = DesktopUnscaledY(Size\bottom - Size\top)
ToolbarTopOffset = ToolbarHeight
MenuHeight = MenuHeight()
StatusbarHeight = StatusBarHeight(#STATUSBAR)
; GUI is created now, so set up the callback
;
SetWindowCallback(@MainWindowCallback())
; Turn on the drag & drop feature..
;
DragAcceptFiles_(WindowID(#WINDOW_Main), 1)
; Windows only:
; Adds the debugger commands to the Systemmenu, making them accessible by rightclicking
; on the TaskbarButton too
;
CreateSYSTEMMenu()
; get the bold font for the statusbar
If GetObject_(GetStockObject_(#DEFAULT_GUI_FONT), SizeOf(LOGFONT), @fontinfo.LOGFONT)
fontinfo\lfWeight = #FW_BOLD
StatusbarBoldFontID = CreateFontIndirect_(@fontinfo)
EndIf
If StatusbarBoldFontID = 0
; fallback option
StatusbarBoldFontID = FontID(LoadFont(#PB_Any, "MS Sans Serif", 8, #PB_Font_Bold|#PB_Font_HighQuality))
EndIf
EndProcedure
Procedure LoadEditorFonts()
If LoadFont(#FONT_Editor, EditorFontName$, EditorFontSize, EditorFontStyle)
EditorFontID = FontID(#FONT_Editor)
EndIf
EditorBoldFontName$ = EditorFontName$ ; for scintilla
EndProcedure
Procedure UpdateToolbarView()
If ShowMainToolbar
ShowWindow_(*MainToolBar, #SW_SHOWNA)
Else
ShowWindow_(*MainToolBar, #SW_HIDE)
EndIf
ResizeMainWindow()
EndProcedure
Procedure RunOnce_Startup(InitialSourceLine)
Result = #False ; do not quit IDE
; The Mutex name is now constructed from the Program filename.
; This way we have "one instance per installation", which allows multiple
; versions or x86/x64 at once, but only one instance of one installation
;
; The name cannot contain \ etc, so use the MD5 of the FileName$
;
File$ = ProgramFilename()
Mutex$ = #ProductName$ + "_" + StringFingerprint(File$, #PB_Cipher_MD5)
Debug "RunOnce Mutex: " + Mutex$
RunOnceMutex = CreateMutex_(0, 0, Mutex$)
If GetLastError_() = #ERROR_ALREADY_EXISTS
Result = #True ; close IDE after this
;
; We now handle the communication with the old instance here as well,
; so we can make use of Message based communication instead of the old
; file routine.
;
; The problem with the file routine is that if many files are opened at once,
; different instances will overwrite the file, causing files to be ignored.
; (This happens when opening many files from the explorer for example)
; we need a window for communication
If OpenWindow(0, 0, 0, 0, 0, "", #PB_Window_Invisible)
; post message to look for other instances
; Note: the WM_COPYDATA sent is now incompatible to pre 4.30 versions,
; so we use a new ID here ('OPEN' instead of 'LOOK' before)
; This way old IDE versions will not react to this.
;
Debug "broadcast"
PostMessage_(#HWND_BROADCAST, RunOnceMessageID, AsciiConst('O', 'P', 'E', 'N'), WindowID(0))
; wait for a response
Timeout.q = ElapsedMilliseconds() + 10000 ; since it is a hidden process, we can afford a bigger timeout
While Timeout > ElapsedMilliseconds()
If WaitWindowEvent(100) = RunOnceMessageID
If EventwParam() = AsciiConst('H', 'W', 'N', 'D') ; response from an older instance
Target = EventlParam() ; target HWND
; allow the target window to grab the keyboard focus.
; this avoids the ugly taskbar blinking when we steal the focus with SetWindowForeground_Real()
AllowSetForeground = GetProcAddress_(GetModuleHandle_("User32.dll"), "AllowSetForegroundWindow")
If AllowSetForeground
GetWindowThreadProcessId_(Target, @TargetPID)
CallFunctionFast(AllowSetForeground, TargetPID)
EndIf
; Note: We send this to all responding windows (which could be multiple)
; but we include the ProgramFilename() so only the right instance will open the files
;
; Data: ProgramFilename()
; FilenameCount
; InitialSourceLine
; File1
; File2
; ...
;
List$ = ProgramFilename() + Chr(10) + Str(ListSize(OpenFilesCommandline())) + Chr(10) + Str(InitialSourceLine)
ForEach OpenFilesCommandline()
List$ + Chr(10) + OpenFilesCommandline()
Next OpenFilesCommandline()
copydata.COPYDATASTRUCT
copydata\dwData = AsciiConst('O', 'N', 'C', 'E') ; to identify the kind of data ('LOG' is used to access the error log)
copydata\cbData = (Len(List$) + 1)*#CharSize ; include null
copydata\lpData = @List$
SendMessage_(Target, #WM_COPYDATA, WindowID(0), @copydata)
Debug "Sending #WM_COPYDATA: " + List$
; No more loop quitting, as there could be multiple instances answering,
; and we have to send the WM_COPYDATA to all (the right one will react to it)
;
; To not wait the full timeout, the (correct) target will send a 'DONE'
; Message, on which we quit
ElseIf EventwParam() = AsciiConst('D', 'O', 'N', 'E')
Break
EndIf
EndIf
Wend
CloseWindow(0)
EndIf
EndIf
ProcedureReturn Result
EndProcedure
Procedure RunOnce_UpdateSetting()
If Editor_RunOnce And RunOnceMutex = 0
; See RunOnce_Startup()
File$ = ProgramFilename()
RunOnceMutex = CreateMutex_(0, 0, #ProductName$ + "_" + StringFingerprint(File$, #PB_Cipher_MD5))
ElseIf Editor_RunOnce = 0 And RunOnceMutex
CloseHandle_(RunOnceMutex)
RunOnceMutex = 0
EndIf
EndProcedure
; Dead session detection
; Similar to RunOnce, but must support concurrent sessions (to support the case of RunOnce=off)
Procedure Session_IsRunning(OSSessionID$)
Handle = OpenMutex_(#SYNCHRONIZE, #False, @OSSessionID$)
If Handle
CloseHandle_(Handle)
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
Procedure.s Session_Start()
SID$ = "PB_Session_" + History_MakeUniqueId()
SessionMutex = CreateMutex_(0, 0, SID$)
ProcedureReturn SID$
EndProcedure
Procedure Session_End(OSSessionID$)
CloseHandle_(SessionMutex)
SessionMutex = 0
EndProcedure
Procedure AutoComplete_FocusProc(Window, Message, wParam, lParam)
If AutoCompleteWindowOpen
If Message = #WM_KILLFOCUS
AutoComplete_Close()
ElseIf Message = #WM_KEYDOWN Or Message = #WM_KEYUP
; redirect all key messages except up/down, tab and return/esc to the scintilla gadget
If wParam = #VK_LEFT Or wParam = #VK_RIGHT Or wParam = #VK_PRIOR Or wParam = #VK_NEXT Or wParam = #VK_HOME Or wParam = #VK_END
AutoComplete_Close()
PostMessage_(GadgetID(*ActiveSource\EditorGadget), Message, wParam, lParam) ; also post this message!
; handle specially the tab/enter if it is the "confirm" shortcut
ElseIf wParam = #VK_TAB And KeyboardShortcuts(#MENU_AutoComplete_OK) = #PB_Shortcut_Tab
ProcedureReturn CallWindowProc_(AutoCompleteWindowProc, Window, Message, wParam, lParam)
ElseIf wParam = #VK_RETURN And KeyboardShortcuts(#MENU_AutoComplete_OK) = #PB_Shortcut_Return
ProcedureReturn CallWindowProc_(AutoCompleteWindowProc, Window, Message, wParam, lParam)
; Note: for unhandled Enter/Tab, we must first close the AutoComplete window or else
; the event will not be handled by the main window as it checks if the Scintilla has the focus!
ElseIf wParam = #VK_RETURN Or wParam = #VK_TAB
AutoComplete_Close()
PostMessage_(GadgetID(*ActiveSource\EditorGadget), Message, wParam, lParam) ; also post this message!
ProcedureReturn 0
ElseIf wParam <> #VK_UP And wParam <> #VK_DOWN And wParam <> #VK_ESCAPE
PostMessage_(GadgetID(*ActiveSource\EditorGadget), Message, wParam, lParam)
ProcedureReturn 0
EndIf
EndIf
EndIf
ProcedureReturn CallWindowProc_(AutoCompleteWindowProc, Window, Message, wParam, lParam)
EndProcedure
Procedure AutoComplete_SetFocusCallback()
AutoCompleteWindowProc = SetWindowLongPtr_(GadgetID(#GADGET_AutoComplete_List), #GWL_WNDPROC, @AutoComplete_FocusProc())
EndProcedure
Procedure AutoComplete_AdjustWindowSize(MaxWidth, MaxHeight)
;
; Note: on windows there is no horizontal scrollbar in ListViewGadget, so
; we do not resize in this direction at all
;
NewHeight = MaxHeight
ItemHeight = SendMessage_(GadgetID(#GADGET_AutoComplete_List), #LB_GETITEMHEIGHT, 0, 0)
If ItemHeight <> #LB_ERR
NewHeight = ItemHeight * CountGadgetItems(#GADGET_AutoComplete_List) + GetSystemMetrics_(#SM_CYEDGE) * 2
If NewHeight > MaxHeight
NewHeight = MaxHeight
EndIf
EndIf
ResizeGadget(#GADGET_AutoComplete_List, #PB_Ignore, #PB_Ignore, #PB_Ignore, NewHeight)
ResizeWindow(#WINDOW_AutoComplete, #PB_Ignore, #PB_Ignore, #PB_Ignore, NewHeight)
EndProcedure
Procedure ToolsPanel_ApplyColors(Gadget)
If Gadget = propgrid
grid_SetGadgetColor(Gadget, #Grid_Color_Background, ToolsPanelBackColor)
grid_SetDefaultStyle(Gadget,P_FontGrid, #P_FontGridSize, ToolsPanelFrontColor)
Else
If ToolsPanelFontID <> #PB_Default
SetGadgetFont(Gadget, ToolsPanelFontID)
EndIf
If ToolsPanelUseColors
SetGadgetColor(Gadget, #PB_Gadget_FrontColor, ToolsPanelFrontColor)
SetGadgetColor(Gadget, #PB_Gadget_BackColor, ToolsPanelBackColor)
;SetGadgetColor(Gadget, #PB_Gadget_LineColor, ToolsPanelFrontColor) ; just set linecolor too. will be ignored if unsupported
EndIf
EndIf
EndProcedure
Structure PB_MenuStructure
Menu.i ; OS Menu pointer (HMENU)
Window.i ; OS Window pointer on which the menu has been created (HWINDOW)
EndStructure
Procedure IsAdmin()
Shell32 = OpenLibrary(#PB_Any,"shell32.dll")
If Shell32
Result = CallCFunction(Shell32, "IsUserAnAdmin") ; Default to 0 if the function isn't found
CloseLibrary(Shell32)
EndIf
ProcedureReturn Result
EndProcedure
CompilerEndIf