forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectPanel.pb
750 lines (608 loc) · 26 KB
/
ProjectPanel.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
;--------------------------------------------------------------------------------------------
; 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.
;--------------------------------------------------------------------------------------------
;
; We add/remove only the changed files from the Panel, this way the
; selection, scroll position and open nodes stay consistent
;
; For this, we store the ProjectFiles() pointer in the GadgetItemData
; Spechal values for the GadgetItemData:
;
#ProjectPanel_Directory = 0
#ProjectPanel_InternalBase = -1
#ProjectPanel_ExternalBase = -2
Global ProjectPanelVisible
Global ProjectPanelMenuGadget
; Delete a file entry and all its parents if empty (recursive)
;
Procedure ProjectPanel_RemoveFile(Index)
Sublevel = GetGadgetItemAttribute(#GADGET_ProjectPanel, Index, #PB_Tree_SubLevel)
; Only empty Items can be removed
;
Last = CountGadgetItems(#GADGET_ProjectPanel)-1
For i = Index+1 To Last
Level = GetGadgetItemAttribute(#GADGET_ProjectPanel, i, #PB_Tree_SubLevel)
If Level <= Sublevel
Break
ElseIf Level > Sublevel
; found a subitem, so abort
ProcedureReturn
EndIf
Next i
; This item is empty, so remove it
; Do not remove the InternalBase item, so the gadget is only full empty when no project is open
;
If GetGadgetItemData(#GADGET_ProjectPanel, Index) <> #ProjectPanel_InternalBase
RemoveGadgetItem(#GADGET_ProjectPanel, Index)
; Find the parent and recurse
If Sublevel > 0
For ParentIndex = Index-1 To 0 Step -1
If GetGadgetItemAttribute(#GADGET_ProjectPanel, ParentIndex, #PB_Tree_SubLevel) < Sublevel
ProjectPanel_RemoveFile(ParentIndex)
ProcedureReturn
EndIf
Next ParentIndex
EndIf
EndIf
EndProcedure
; Add all directories for a file, and the file entry itself (recursive)
;
Procedure ProjectPanel_AddFile(Filename$, ParentIndex, *File.ProjectFile)
NewLevel = GetGadgetItemAttribute(#GADGET_ProjectPanel, ParentIndex, #PB_Tree_SubLevel)+1
Count = CountGadgetItems(#GADGET_ProjectPanel)
NewItem$ = StringField(Filename$, 1, #Separator)
Separator = FindString(Filename$, #Separator, 1)
; Look if we can find the Item$ on the current sublevel, else look for a place to insert
;
FoundIndex = -1
InsertIndex = -1
For Index = ParentIndex+1 To Count-1
Level = GetGadgetItemAttribute(#GADGET_ProjectPanel, Index, #PB_Tree_SubLevel)
If Level = NewLevel
*NewFile = GetGadgetItemData(#GADGET_ProjectPanel, Index)
If Separator And *NewFile <> #ProjectPanel_Directory
; directories come before files
InsertIndex = Index
Break
ElseIf Separator = 0 And *NewFile = #ProjectPanel_Directory
; files come after directories
Continue
Else
Item$ = GetGadgetItemText(#GADGET_ProjectPanel, Index)
Select CompareMemoryString(@NewItem$, @Item$, #PATH_CaseInsensitive)
Case #PB_String_Lower
InsertIndex = Index
Break
Case #PB_String_Equal
FoundIndex = Index
Break
EndSelect
EndIf
ElseIf Level < NewLevel
InsertIndex = Index
Break
EndIf
Next Index
If FoundIndex = -1 And InsertIndex = -1
InsertIndex = Count
EndIf
If Separator
; Its a directory
;
Filename$ = Right(Filename$, Len(Filename$)-Separator)
If FoundIndex <> -1
ProjectPanel_AddFile(Filename$, FoundIndex, *File)
; do not adjust the expanded state of this directory:
; the stored state was used when it was first added, and if it changed since,
; then the user did it, so do not alter that
ElseIf InsertIndex <> -1
AddGadgetItem(#GADGET_ProjectPanel, InsertIndex, NewItem$, OptionalImageID(#IMAGE_ProjectPanel_Directory), NewLevel)
SetGadgetItemData(#GADGET_ProjectPanel, InsertIndex, #ProjectPanel_Directory)
ProjectPanel_AddFile(Filename$, InsertIndex, *File)
; set the expanded state from the information for the added file.
; this should match the information for all other files in this directory, so we only
; need to set this on creation of the directory
;
If Mid(*File\PanelState$, NewLevel+1, 1) <> "-" ; expanded is the default if no info was found
SetGadgetItemState(#GADGET_ProjectPanel, InsertIndex, #PB_Tree_Expanded)
EndIf
EndIf
Else
; Its a file
;
If InsertIndex <> -1
If *File\AutoScan
ImageID = OptionalImageID(#IMAGE_ProjectPanel_FileScanned)
Else
ImageID = OptionalImageID(#IMAGE_ProjectPanel_File)
EndIf
AddGadgetItem(#GADGET_ProjectPanel, InsertIndex, Filename$, ImageID, NewLevel)
SetGadgetItemData(#GADGET_ProjectPanel, InsertIndex, *File)
EndIf
EndIf
EndProcedure
; Get the full path from a panel item
Procedure.s ProjectPanel_FullPath(Index)
*File.ProjectFile = GetGadgetItemData(#GADGET_ProjectPanel, Index)
Select *File
Case #ProjectPanel_Directory
ParentLevel = GetGadgetItemAttribute(#GADGET_ProjectPanel, Index, #PB_Tree_SubLevel)-1
ParentIndex = Index-1
While ParentIndex > 0 And ParentLevel < GetGadgetItemAttribute(#GADGET_ProjectPanel, ParentIndex, #PB_Tree_SubLevel)
ParentIndex - 1
Wend
ProcedureReturn ProjectPanel_FullPath(ParentIndex) + GetGadgetItemText(#GADGET_ProjectPanel, Index) + #Separator
Case #ProjectPanel_InternalBase
ProcedureReturn GetPathPart(ProjectFile$)
Case #ProjectPanel_ExternalBase
ProcedureReturn ""
Default ; its a ProjectFile structure
ProcedureReturn *File\Filename$
EndSelect
EndProcedure
Procedure ProjectPanel_InBasePath(Base$, Filename$)
If Len(Base$) < Len(Filename$) And CompareMemoryString(@Base$, @Filename$, #PATH_CaseInsensitive, Len(Base$)) = #PB_String_Equal
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure ProjectPanel_IsFile(*File.ProjectFile)
If *File = 0 Or *File = #ProjectPanel_Directory Or *File = #ProjectPanel_InternalBase Or *File = #ProjectPanel_ExternalBase
ProcedureReturn #False
Else
ProcedureReturn #True
EndIf
EndProcedure
Procedure StoreProjectPanelStates()
If ProjectPanelVisible
; This is needed to ensures consistency (project files could have been removed) as we used raw pointers in list item user data.
UpdateProjectPanel()
;
; Each file stores the states expanded states of all its parent directories
; This stores some redundant information (common directories are stored with multiple files), but
; this way we can always reconstruct the visibility even if files are removed from the tree or added
;
State$ = ""
Count = CountGadgetItems(#GADGET_ProjectPanel)
Level = 0
For i = 0 To Count-1
NewLevel = GetGadgetItemAttribute(#GADGET_ProjectPanel, i, #PB_Tree_SubLevel)
If NewLevel > Level
; entered a directory (one level deeper at a time) ("i" must be > 0 now)
If GetGadgetItemState(#GADGET_ProjectPanel, i-1) & #PB_Tree_Expanded
State$ + "+" ; open
Else
State$ + "-" ; closed
EndIf
ElseIf NewLevel < Level
; left a directory (can be many levels at a time here), cut the states of the left directories
State$ = Left(State$, NewLevel)
EndIf
Level = NewLevel
*File.ProjectFile = GetGadgetItemData(#GADGET_ProjectPanel, i)
If ProjectPanel_IsFile(*File)
*File\PanelState$ = State$
EndIf
Next i
EndIf
EndProcedure
Procedure UpdateProjectPanel()
If ProjectPanelVisible
If IsProject
Base$ = GetPathPart(ProjectFile$)
BaseLength = Len(Base$)
; Find the "internal" base item
;
BaseIndex = -1
BaseNew = 0
last = CountGadgetItems(#GADGET_ProjectPanel)-1
For i = 0 To last
If GetGadgetItemData(#GADGET_ProjectPanel, i) = #ProjectPanel_InternalBase
BaseIndex = i
Break
EndIf
Next i
; Add any "internal" files that are not displayed yet
;
ForEach ProjectFiles()
If ProjectFiles()\ShowPanel And ProjectPanel_InBasePath(Base$, ProjectFiles()\FileName$)
found = 0
last = CountGadgetItems(#GADGET_ProjectPanel)-1
For i = 0 To last
If GetGadgetItemData(#GADGET_ProjectPanel, i) = @ProjectFiles()
; update the image in case the AutoScan setting was changed
If ProjectFiles()\AutoScan
If IsImage(#IMAGE_ProjectPanel_FileScanned)
SetGadgetItemImage(#GADGET_ProjectPanel, i, ImageID(#IMAGE_ProjectPanel_FileScanned))
EndIf
Else
If IsImage(#IMAGE_ProjectPanel_File)
SetGadgetItemImage(#GADGET_ProjectPanel, i, ImageID(#IMAGE_ProjectPanel_File))
EndIf
EndIf
found = 1
Break
EndIf
Next i
If found = 0
; Add the base item if not present yet
;
If BaseIndex = -1
AddGadgetItem(#GADGET_ProjectPanel, 0, Language("Project","InternalFiles"), OptionalImageID(#IMAGE_ProjectPanel_InternalFiles), 0)
SetGadgetItemData(#GADGET_ProjectPanel, 0, #ProjectPanel_InternalBase)
BaseIndex = 0
BaseNew = 1
BaseState$ = Left(ProjectFiles()\PanelState$, 1)
EndIf
ProjectPanel_AddFile(Right(ProjectFiles()\Filename$, Len(ProjectFiles()\Filename$)-BaseLength), BaseIndex, @ProjectFiles())
EndIf
EndIf
Next ProjectFiles()
If BaseNew And BaseState$ <> "-" ; expanded is the default if no state info available
SetGadgetItemState(#GADGET_ProjectPanel, BaseIndex, #PB_Tree_Expanded)
EndIf
; Find the "external" base item
;
BaseIndex = -1
BaseNew = 0
last = CountGadgetItems(#GADGET_ProjectPanel)-1
For i = 0 To last
If GetGadgetItemData(#GADGET_ProjectPanel, i) = #ProjectPanel_ExternalBase
BaseIndex = i
Break
EndIf
Next i
; Add any external items that are not displayed yet
;
ForEach ProjectFiles()
If ProjectFiles()\ShowPanel And ProjectPanel_InBasePath(Base$, ProjectFiles()\FileName$) = #False
found = 0
last = CountGadgetItems(#GADGET_ProjectPanel)-1
For i = 0 To last
If GetGadgetItemData(#GADGET_ProjectPanel, i) = @ProjectFiles()
; update the image in case the AutoScan setting was changed
If ProjectFiles()\AutoScan
If IsImage(#IMAGE_ProjectPanel_FileScanned)
SetGadgetItemImage(#GADGET_ProjectPanel, i, ImageID(#IMAGE_ProjectPanel_FileScanned))
EndIf
Else
If IsImage(#IMAGE_ProjectPanel_File)
SetGadgetItemImage(#GADGET_ProjectPanel, i, ImageID(#IMAGE_ProjectPanel_File))
EndIf
EndIf
found = 1
Break
EndIf
Next i
If found = 0
; Now add the Base if not present yet
;
If BaseIndex = -1
BaseIndex = CountGadgetItems(#GADGET_ProjectPanel)
AddGadgetItem(#GADGET_ProjectPanel, BaseIndex, Language("Project","ExternalFiles"), OptionalImageID(#IMAGE_ProjectPanel_ExternalFiles), 0)
SetGadgetItemData(#GADGET_ProjectPanel, BaseIndex, #ProjectPanel_ExternalBase)
BaseNew = 1
BaseState$ = Left(ProjectFiles()\PanelState$, 1)
EndIf
; Now, make sure the directory is present (as one node)
;
Directory$ = GetPathPart(ProjectFiles()\FileName$)
If Right(Directory$, 1) = #Separator
Directory$ = Left(Directory$, Len(Directory$)-1)
EndIf
FoundIndex = -1
InsertIndex = -1
last = CountGadgetItems(#GADGET_ProjectPanel)-1
For i = BaseIndex+1 To last
If GetGadgetItemData(#GADGET_ProjectPanel, i) = #ProjectPanel_Directory
Select CompareDirectories(Directory$, GetGadgetItemText(#GADGET_ProjectPanel, i))
Case #PB_String_Lower
InsertIndex = i
Break
Case #PB_String_Equal
FoundIndex = i
Break
EndSelect
EndIf
Next i
; if no index found yet, we must append at the end
If FoundIndex = -1 And InsertIndex = -1
InsertIndex = CountGadgetItems(#GADGET_ProjectPanel)
EndIf
If FoundIndex = -1
FoundIndex = InsertIndex
AddGadgetItem(#GADGET_ProjectPanel, FoundIndex, Directory$, OptionalImageID(#IMAGE_ProjectPanel_Directory), 1) ; always level 1
SetGadgetItemData(#GADGET_ProjectPanel, FoundIndex, #ProjectPanel_Directory)
; add the file
ProjectPanel_AddFile(GetFilePart(ProjectFiles()\FileName$), FoundIndex, @ProjectFiles())
; apply the expand state of the added directory (its always at sublevel 1 here)
If Mid(ProjectFiles()\PanelState$, 2, 1) <> "-"
SetGadgetItemState(#GADGET_ProjectPanel, FoundIndex, #PB_Tree_Expanded)
EndIf
Else
; add the file
ProjectPanel_AddFile(GetFilePart(ProjectFiles()\FileName$), FoundIndex, @ProjectFiles())
EndIf
EndIf
EndIf
Next ProjectFiles()
If BaseNew And BaseState$ <> "-" ; expanded is default
SetGadgetItemState(#GADGET_ProjectPanel, BaseIndex, #PB_Tree_Expanded)
EndIf
; Remove any no longer present files
; Do this after the adding, so we do not remove, then re-add subdirectories if not needed
; Step backwards, so removing an item does not change the indexes of other items
;
For i = CountGadgetItems(#GADGET_ProjectPanel)-1 To 0 Step -1
*File = GetGadgetItemData(#GADGET_ProjectPanel, i)
If ProjectPanel_IsFile(*File)
found = 0
ForEach ProjectFiles()
If *File = @ProjectFiles()
found = 1
Break
EndIf
Next ProjectFiles()
If found = 0
ProjectPanel_RemoveFile(i)
; If we remove an item, it may remove its parents too, so we must check the index
Count = CountGadgetItems(#GADGET_ProjectPanel)
If i > Count ; the next loop will decrease by 1 and it will be perfect then
i = Count
EndIf
EndIf
EndIf
Next i
Else
; project was closed
ClearGadgetItems(#GADGET_ProjectPanel)
EndIf
EndIf
EndProcedure
Procedure ProjectPanel_CreateFunction(*Entry.ToolsPanelEntry, PanelItemID)
; Note: The ProjectPanel menu is created in CreateIDEPopupMenu() as the ProjectInfo uses it too
;
TreeGadget(#GADGET_ProjectPanel, 0, 0, 0, 0)
ProjectPanelVisible = #True
If *Entry\IsSeparateWindow = 0 Or NoIndependentToolsColors = 0
ToolsPanel_ApplyColors(#GADGET_ProjectPanel)
EndIf
UpdateProjectPanel()
EndProcedure
Procedure ProjectPanel_DestroyFunction(*Entry.ToolsPanelEntry)
StoreProjectPanelStates() ; store expanded states
FreeGadget(#GADGET_ProjectPanel)
ProjectPanelVisible = #False
EndProcedure
Procedure ProjectPanel_ResizeHandler(*Entry.ToolsPanelEntry, PanelWidth, PanelHeight)
If *Entry\IsSeparateWindow
ResizeGadget(#GADGET_ProjectPanel, 5, 5, PanelWidth-10, PanelHeight-10)
Else
ResizeGadget(#GADGET_ProjectPanel, 0, 0, PanelWidth, PanelHeight)
EndIf
EndProcedure
; The Menu is re-used by the ProjectInfo file list
; *Entry is 0 in this case
;
Procedure DisplayProjectPanelMenu(*Entry.ToolsPanelEntry, SourceGadget)
ProjectPanelMenuGadget = SourceGadget
Index = GetGadgetState(SourceGadget)
; The Add entry is always on
If Index = -1
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_Open, #True)
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_OpenViewer, #True)
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_OpenExplorer, #True)
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_Rescan, #True)
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_Remove, #True)
Else
*File.ProjectFile = GetGadgetItemData(SourceGadget, Index)
If ProjectPanel_IsFile(*File)
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_Open, #False)
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_OpenViewer, #False)
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_OpenExplorer, #False)
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_Remove, #False)
; In ProjectInfo mode, there is multiselect, so do not disable this item then
If *Entry = 0 Or *File\AutoScan
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_Rescan, #False)
Else
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_Rescan, #True)
EndIf
Else
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_Open, #False)
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_OpenViewer, #False)
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_Remove, #True)
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_Rescan, #False) ; can re-scan a full directory
If *File = #ProjectPanel_ExternalBase
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_OpenExplorer, #True)
Else
DisableMenuItem(#POPUPMENU_ProjectPanel, #MENU_ProjectPanel_OpenExplorer, #False)
EndIf
EndIf
EndIf
If *Entry And *Entry\IsSeparateWindow
DisplayPopupMenu(#POPUPMENU_ProjectPanel, WindowID(*Entry\ToolWindowID))
Else
DisplayPopupMenu(#POPUPMENU_ProjectPanel, WindowID(#WINDOW_Main))
EndIf
EndProcedure
Procedure ProjectPanelMenuEvent(MenuItemID)
; Make a list of all files that are selected with this entry (subitems)
; This handler is only called with real menu events, so there is no slowdown here
; (unlike the ProjectPanel_EventHandler() which gets called with other gadget events)
;
Protected NewList *Files.ProjectFile()
SelectedFolder$ = ""
Index = GetGadgetState(ProjectPanelMenuGadget)
If Index <> -1
If ProjectPanelMenuGadget = #GADGET_ProjectPanel
; ProjectPanel mode (its a tree gadget)
*File.ProjectFile = GetGadgetItemData(ProjectPanelMenuGadget, Index)
If ProjectPanel_IsFile(*File)
AddElement(*Files())
*Files() = *File
SelectedFolder$ = GetPathPart(*File\FileName$)
Else
SelectedFolder$ = ProjectPanel_FullPath(Index)
Sublevel = GetGadgetItemAttribute(ProjectPanelMenuGadget, Index, #PB_Tree_SubLevel)
Count = CountGadgetItems(ProjectPanelMenuGadget)
Index + 1
While Index < Count And GetGadgetItemAttribute(ProjectPanelMenuGadget, Index, #PB_Tree_SubLevel) > Sublevel
*File = GetGadgetItemData(ProjectPanelMenuGadget, Index)
If ProjectPanel_IsFile(*File)
AddElement(*Files())
*Files() = *File
EndIf
Index + 1
Wend
EndIf
Else
; ProjectInfo mode (its a multiselect ListIcon)
Last = CountGadgetItems(ProjectPanelMenuGadget) - 1
For i = 0 To Last
If GetGadgetItemState(ProjectPanelMenuGadget, i) & #PB_ListIcon_Selected
AddElement(*Files())
*Files() = GetGadgetItemData(ProjectPanelMenuGadget, i) ; stores the ProjectFile pointer
; The option applies only to the first file here
If SelectedFolder$ = ""
SelectedFolder$ = GetPathPart(*Files()\Filename$)
EndIf
EndIf
Next i
EndIf
EndIf
Select MenuItemID
Case #MENU_ProjectPanel_Open
ForEach *Files()
LoadSourceFile(*Files()\FileName$)
Next *Files()
Case #MENU_ProjectPanel_OpenViewer
ForEach *Files()
FileViewer_OpenFile(*Files()\FileName$)
Next *Files()
Case #MENU_ProjectPanel_OpenExplorer
If SelectedFolder$
ShowExplorerDirectory(SelectedFolder$)
EndIf
Case #MENU_ProjectPanel_Rescan
ForEach *Files()
If *Files()\AutoScan And *Files()\Source = 0 ; no need to re-scan if currently open
ScanFile(*Files()\Filename$, @*Files()\Parser)
EndIf
Next *Files()
Case #MENU_ProjectPanel_Add
FileName$ = OpenFileRequester(Language("FileStuff","OpenFileTitle"), GetPathPart(ProjectFile$), Language("FileStuff","Pattern"), SelectedFilePattern, #PB_Requester_MultiSelection)
If FileName$ <> ""
SelectedFilePattern = SelectedFilePattern()
While FileName$ <> ""
found = 0
ForEach ProjectFiles()
If IsEqualFile(ProjectFiles()\FileName$, FileName$)
found = 1
Break
EndIf
Next ProjectFiles()
If found = 0
Debug FileName$
; If the file doesn't exist, ask to create it, to ease the integration
; https://www.purebasic.fr/english/viewtopic.php?f=4&t=47521
;
If FileSize(FileName$) < 0
If MessageRequester(Language("FileStuff","AddNewFileTitle"), LanguagePattern("FileStuff","AddNewFileQuestion", "%filename%", Filename$), #PB_MessageRequester_YesNo | #FLAG_Question) = #PB_MessageRequester_Yes
NewFile = CreateFile(#PB_Any, FileName$)
If NewFile
CloseFile(NewFile)
Else
MessageRequester(Language("FileStuff","AddNewFileTitle"), LanguagePattern("FileStuff","AddNewFileError", "%filename%", Filename$), #PB_MessageRequester_Ok | #FLAG_Error)
EndIf
EndIf
EndIf
LastElement(ProjectFiles())
AddElement(ProjectFiles())
ProjectFiles()\FileName$ = FileName$
ProjectFiles()\AutoLoad = 0
ProjectFiles()\AutoScan = IsCodeFile(Filename$)
ProjectFiles()\ShowPanel = 1
ProjectFiles()\ShowWarning = 1
UpdateProjectFile(@ProjectFiles())
EndIf
FileName$ = NextSelectedFileName()
Wend
UpdateMenuStates()
UpdateProjectInfo()
UpdateProjectPanel()
EndIf
Case #MENU_ProjectPanel_Remove
If ListSize(*Files()) <= 1 Or MessageRequester(#ProductName$, LanguagePattern("Project","RemoveMany", "%count%", Str(ListSize(*Files()))), #PB_MessageRequester_YesNo|#FLAG_Question) = #PB_MessageRequester_Yes
ForEach *Files()
If *Files()\Source
UnlinkSourceFromProject(*Files()\Source, #False)
EndIf
ClearProjectFile(*Files())
ChangeCurrentElement(ProjectFiles(), *Files())
DeleteElement(ProjectFiles())
Next *Files()
UpdateMenuStates() ; reflect the changed settings
UpdateProjectInfo()
UpdateProjectPanel()
EndIf
EndSelect
EndProcedure
Procedure ProjectPanel_EventHandler(*Entry.ToolsPanelEntry, EventGadgetID)
If EventGadgetID = #GADGET_ProjectPanel
Index = GetGadgetState(#GADGET_ProjectPanel)
Select EventType()
Case #PB_EventType_DragStart
If Index <> -1
*File.ProjectFile = GetGadgetItemData(#GADGET_ProjectPanel, Index)
If ProjectPanel_IsFile(*File)
; its a single file
DragFiles(*File\FileName$)
ElseIf *File = #ProjectPanel_Directory
; its a directory
Files$ = ""
Sublevel = GetGadgetItemAttribute(#GADGET_ProjectPanel, Index, #PB_Tree_SubLevel)
Count = CountGadgetItems(#GADGET_ProjectPanel)
Index + 1
While Index < Count And GetGadgetItemAttribute(#GADGET_ProjectPanel, Index, #PB_Tree_SubLevel) > Sublevel
*File = GetGadgetItemData(#GADGET_ProjectPanel, Index)
If ProjectPanel_IsFile(*File)
Files$ + *File\FileName$ + Chr(10)
EndIf
Index + 1
Wend
If Files$ <> ""
DragFiles(Left(Files$, Len(Files$)-1)) ; cut the last Chr(10)
EndIf
EndIf
EndIf
Case #PB_EventType_LeftDoubleClick
If Index <> -1
*File.ProjectFile = GetGadgetItemData(#GADGET_ProjectPanel, Index)
If ProjectPanel_IsFile(*File)
LoadSourceFile(*File\FileName$) ; will just switch if open
EndIf
EndIf
Case #PB_EventType_RightClick
DisplayProjectPanelMenu(*Entry, #GADGET_ProjectPanel)
EndSelect
EndIf
EndProcedure
;- Initialisation code
; This will make this Tool available to the editor
;
ProjectPanel_VT.ToolsPanelFunctions
ProjectPanel_VT\CreateFunction = @ProjectPanel_CreateFunction()
ProjectPanel_VT\DestroyFunction = @ProjectPanel_DestroyFunction()
ProjectPanel_VT\ResizeHandler = @ProjectPanel_ResizeHandler()
ProjectPanel_VT\EventHandler = @ProjectPanel_EventHandler()
AddElement(AvailablePanelTools())
AvailablePanelTools()\FunctionsVT = @ProjectPanel_VT
AvailablePanelTools()\NeedPreferences = 0
AvailablePanelTools()\NeedConfiguration = 0
AvailablePanelTools()\NeedDestroyFunction = 1
AvailablePanelTools()\ToolID$ = "ProjectPanel"
AvailablePanelTools()\PanelTitle$ = "ProjectPanelShort"
AvailablePanelTools()\ToolName$ = "ProjectPanelLong"