forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileViewer.pb
507 lines (386 loc) · 18.2 KB
/
FileViewer.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
;--------------------------------------------------------------------------------------------
; 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.
;--------------------------------------------------------------------------------------------
Structure FileViewer
FileName$; to compare to newly opened files..
Gadget.i ; scrollarea or editor.. this value is only needed for resizing
Image.i ; loaded image if any
EndStructure
Global NewList FileViewer.FileViewer()
UseJPEGImageDecoder()
UsePNGImageDecoder()
UseTGAImageDecoder()
Global FileViewerOpen
Procedure FileViewer_UpdateButtonStates()
count = ListSize(FileViewer())
If count = 0
DisableToolBarButton(#TOOLBAR_FileViewer, #MENU_FileViewer_Close, 1)
DisableToolBarButton(#TOOLBAR_FileViewer, #MENU_FileViewer_Previous, 1)
DisableToolBarButton(#TOOLBAR_FileViewer, #MENU_FileViewer_Next, 1)
Else
DisableToolBarButton(#TOOLBAR_FileViewer, #MENU_FileViewer_Close, 0)
index = ListIndex(FileViewer())
If count = 1
DisableToolBarButton(#TOOLBAR_FileViewer, #MENU_FileViewer_Previous, 1)
DisableToolBarButton(#TOOLBAR_FileViewer, #MENU_FileViewer_Next, 1)
ElseIf index = 0
DisableToolBarButton(#TOOLBAR_FileViewer, #MENU_FileViewer_Previous, 1)
DisableToolBarButton(#TOOLBAR_FileViewer, #MENU_FileViewer_Next, 0)
ElseIf index = count-1
DisableToolBarButton(#TOOLBAR_FileViewer, #MENU_FileViewer_Previous, 0)
DisableToolBarButton(#TOOLBAR_FileViewer, #MENU_FileViewer_Next, 1)
Else
DisableToolBarButton(#TOOLBAR_FileViewer, #MENU_FileViewer_Previous, 0)
DisableToolBarButton(#TOOLBAR_FileViewer, #MENU_FileViewer_Next, 0)
EndIf
EndIf
EndProcedure
Procedure ResizeFileViewer()
ResizeGadget(#GADGET_FileViewer_Panel, 0, ToolbarTopOffset, WindowWidth(#WINDOW_FileViewer), WindowHeight(#WINDOW_FileViewer)-ToolbarHeight-StatusBarHeight)
PanelWidth = GetPanelWidth(#GADGET_FileViewer_Panel)
PanelHeight = GetPanelHeight(#GADGET_FileViewer_Panel)
ForEach FileViewer()
If FileViewer()\Gadget
ResizeGadget(FileViewer()\Gadget, 0, 0, PanelWidth, PanelHeight)
EndIf
Next FileViewer()
EndProcedure
Procedure OpenFileViewerWindow()
If IsWindow(#WINDOW_FileViewer) = 0
; TODO: Something is wrong here with the PB_OpenWindow3_DEBUG call because it
; complains about the ParentID not being valid when it clearly is.
; This happened only with AllocationPreference set to high addresses first (Windows x64)
;
DisableDebugger
If OpenWindow(#WINDOW_FileViewer, FileViewerX, FileViewerY, FileViewerWidth, FileViewerHeight, Language("FileViewer","Title"), #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_Invisible, WindowID(#WINDOW_Main))
EnableDebugger
SmartWindowRefresh(#WINDOW_FileViewer, 1)
If CreateToolBar(#TOOLBAR_FileViewer, WindowID(#WINDOW_FileViewer))
ToolBarImageButton(#MENU_FileViewer_Open, ImageID(#IMAGE_FileViewer_Open))
ToolBarImageButton(#MENU_FileViewer_Close, ImageID(#IMAGE_FileViewer_Close))
ToolBarSeparator()
ToolBarImageButton(#MENU_FileViewer_Previous, ImageID(#IMAGE_FileViewer_Left))
ToolBarImageButton(#MENU_FileViewer_Next, ImageID(#IMAGE_FileViewer_Right))
ToolBarToolTip(#TOOLBAR_FileViewer, #MENU_FileViewer_Open, Language("FileViewer","Open"))
ToolBarToolTip(#TOOLBAR_FileViewer, #MENU_FileViewer_Close, Language("FileViewer","Close"))
ToolBarToolTip(#TOOLBAR_FileViewer, #MENU_FileViewer_Previous, Language("FileViewer","Previous"))
ToolBarToolTip(#TOOLBAR_FileViewer, #MENU_FileViewer_Next, Language("FileViewer","Next"))
EndIf
If CreateStatusBar(#STATUSBAR_FileViewer, WindowID(#WINDOW_FileViewer))
AddStatusBarField(#PB_Ignore)
EndIf
PanelGadget(#GADGET_FileViewer_Panel, 0, ToolbarTopOffset, FileViewerWidth, FileViewerHeight-ToolbarHeight-StatusBarHeight)
CloseGadgetList()
EnableGadgetDrop(#GADGET_FileViewer_Panel, #PB_Drop_Files, #PB_Drag_Copy)
EnableWindowDrop(#WINDOW_FileViewer, #PB_Drop_Files, #PB_Drag_Copy)
EnsureWindowOnDesktop(#WINDOW_FileViewer)
If FileViewerMaximize
ShowWindowMaximized(#WINDOW_FileViewer)
Else
HideWindow(#WINDOW_FileViewer, 0)
EndIf
ResizeFileViewer()
FileViewer_UpdateButtonStates()
EndIf
Else
SetWindowForeground(#WINDOW_FileViewer)
EndIf
EndProcedure
Procedure IsBinaryFile(*Buffer, Length)
; Check if the Data is text only or binary (search for bytes <= 30, except cr,lf,tab)
; Note: we use a binary count as old PB file used a zero char to separate the bottom options from the real code
; Also JaPBe uses some similar trick.
;
BinaryCount = 0
IsBinary = 0
*Pointer.Ascii = *Buffer
While *Pointer < *Buffer+Length
If *Pointer\a < 32 And *Pointer\a <> 10 And *Pointer\a <> 13 And *Pointer\a <> 9
BinaryCount + 1
If BinaryCount > 10
IsBinary = 1
Break
EndIf
EndIf
*Pointer + 1
Wend
ProcedureReturn IsBinary
EndProcedure
Procedure FileViewer_OpenFile(Filename$)
AddTools_RunFileViewer = 1 ; to tell if some tool has been executed
AddTools_File$ = FileName$
Ext$ = LCase(GetExtensionPart(FileName$))
AddTools_Execute(#TRIGGER_FileViewer_Special, 0) ; special file tools have highest priority
If AddTools_RunFileViewer
AddTools_Execute(#TRIGGER_FileViewer_All, 0)
EndIf
If AddTools_RunFileViewer
If Ext$ <> "bmp" And Ext$ <> "png" And Ext$ <> "jpg" And Ext$ <> "jpeg" And Ext$ <> "tga" And Ext$ <> "ico" And Ext$ <> "txt"
AddTools_Execute(#TRIGGER_FileViewer_Unknown, 0)
EndIf
EndIf
If AddTools_RunFileViewer = 0 ; stop if a tool has been run
ProcedureReturn
EndIf
SizeMB.f = FileSize(FileName$)/(1024*1024)
If SizeMB > 10 ; = 10 mb is the max size handled by file viewer, or it will take forever to display in our EditorGadget() based hex viewer
MessageRequester(#ProductName$, Language("FileViewer","SizeError"), #FLAG_Error|#PB_MessageRequester_Ok)
ProcedureReturn
ElseIf SizeMB > 4 ; = 4 mb check for large files which might take long to load
If MessageRequester(#ProductName$, Language("FileViewer","SizeWarning")+" ("+StrF(SizeMB, 1)+" MB)"+#NewLine+Language("FileViewer","SizeQuestion"), #FLAG_Warning|#PB_MessageRequester_YesNo) = #PB_MessageRequester_No
ProcedureReturn
EndIf
EndIf
; check if window is open
;
If IsWindow(#WINDOW_FileViewer) = 0
OpenFileViewerWindow()
Else
SetWindowForeground(#WINDOW_FileViewer)
EndIf
; check if this file was already loaded
;
Found = 0
ForEach FileViewer()
If LCase(FileViewer()\FileName$) = LCase(FileName$)
Found = 1
Break
EndIf
Next FileViewer()
If Found = 1
SetGadgetState(#GADGET_FileViewer_Panel, ListIndex(FileViewer()))
StatusBarText(#STATUSBAR_FileViewer, 0, FileName$)
Else
; find out if it is an image file..
If Ext$ = "bmp" Or Ext$ = "png" Or Ext$ = "jpg" Or Ext$ = "jpeg" Or Ext$ = "tga" Or Ext$ = "ico"
Image = LoadImage(#PB_Any, FileName$)
If Image
LastElement(FileViewer())
AddElement(FileViewer())
FileViewer()\FileName$ = FileName$
FileViewer()\Image = Image
OpenGadgetList(#GADGET_FileViewer_Panel)
AddGadgetItem(#GADGET_FileViewer_Panel, -1, GetFilePart(FileName$))
FileViewer()\Gadget = ScrollAreaGadget(#PB_Any, 0, 0, GetPanelWidth(#GADGET_FileViewer_Panel), GetPanelHeight(#GADGET_FileViewer_Panel), ImageWidth(Image), ImageHeight(Image), 10, #PB_ScrollArea_Center)
ImageGadget = ImageGadget(#PB_Any, 0, 0, ImageWidth(Image), ImageHeight(Image), ImageID(Image))
EnableGadgetDrop(ImageGadget, #PB_Drop_Files, #PB_Drag_Copy)
CloseGadgetList()
CloseGadgetList()
Else
MessageRequester(#ProductName$, Language("FileStuff","MiscLoadError"), #FLAG_Error)
EndIf
CompilerIf #CompileWindows ; the gtk2 is not working well enough on all machines
ElseIf Ext$ = "html" Or Ext$ = "htm"
LastElement(FileViewer())
AddElement(FileViewer())
FileViewer()\FileName$ = FileName$
OpenGadgetList(#GADGET_FileViewer_Panel)
AddGadgetItem(#GADGET_FileViewer_Panel, -1, GetFilePart(FileName$))
FileViewer()\Gadget = WebGadget(#PB_Any, 0, 0, GetPanelWidth(#GADGET_FileViewer_Panel), GetPanelHeight(#GADGET_FileViewer_Panel), "file://" + FileName$)
CloseGadgetList()
CompilerEndIf
Else
If ReadFile(#FILE_FileViewer, FileName$)
Length = Lof(#FILE_FileViewer)
If Length > 0
*Buffer = AllocateMemory(Length)
EndIf
If *Buffer Or Length = 0
; add the editorgadget and prepare it
;
LastElement(FileViewer())
AddElement(FileViewer())
FileViewer()\FileName$ = FileName$
OpenGadgetList(#GADGET_FileViewer_Panel)
AddGadgetItem(#GADGET_FileViewer_Panel, -1, GetFilePart(FileName$))
FileViewer()\Gadget = ScintillaGadget(#PB_Any, 0, 0, GetPanelWidth(#GADGET_FileViewer_Panel), GetPanelHeight(#GADGET_FileViewer_Panel), #Null)
CloseGadgetList()
ScintillaSendMessage(FileViewer()\Gadget, #SCI_USEPOPUP, 1, 0) ; use the scintilla popup
ScintillaSendMessage(FileViewer()\Gadget, #SCI_SETMARGINWIDTHN, 0, 0)
ScintillaSendMessage(FileViewer()\Gadget, #SCI_SETMARGINWIDTHN, 1, 0)
ScintillaSendMessage(FileViewer()\Gadget, #SCI_STYLESETFONT, #STYLE_DEFAULT, ToAscii(EditorFontName$))
ScintillaSendMessage(FileViewer()\Gadget, #SCI_STYLESETSIZE, #STYLE_DEFAULT, EditorFontSize)
ScintillaSendMessage(FileViewer()\Gadget, #SCI_STYLECLEARALL, 0, 0)
ScintillaSendMessage(FileViewer()\Gadget, #SCI_SETCODEPAGE, 0, 0)
EnableGadgetDrop(FileViewer()\Gadget, #PB_Drop_Files, #PB_Drag_Copy)
If Length > 0
; ok, now read the file
;
ReadData(#FILE_FileViewer, *Buffer, Length)
If IsBinaryFile(*Buffer, Length) = #False
ChangeNewLineType(@*Buffer, @Length, #DEFAULT_NewLineType)
Text$ = PeekS(*Buffer, Length, #PB_Ascii)
ScintillaSendMessage(FileViewer()\Gadget, #SCI_SETTEXT, 0, ToAscii(Text$))
Else
*Buffer2 = AllocateMemory(((Length + 16) * 78) / 8) ; Add 16 as the last 16 char block line can be only one character length
*Pointer2 = *Buffer2
CopyMemoryString(@"", @*Pointer2)
; display a nice hex view
;
*Pointer.Ascii = *Buffer
Repeat
HexData$ = RSet(Hex(*Pointer-*Buffer), 8, "0") + " "
String$ = " "
For i = 0 To $F
If *Pointer > *Buffer+Length
HexData$ = LSet(HexData$, 58, " ")
Break
EndIf
HexData$ + RSet(Hex(*Pointer\a, #PB_Byte), 2, "0") + " "
If *Pointer\a < 32
String$ + "."
Else
String$ + Chr(*Pointer\a)
EndIf
*Pointer + 1
Next i
CopyMemoryString(HexData$)
CopyMemoryString(String$)
CopyMemoryString(#NewLine)
Until *Pointer > *Buffer+Length
Text$ = PeekS(*Buffer2, (*Pointer2-*Buffer2) / #CharSize)
ScintillaSendMessage(FileViewer()\Gadget, #SCI_SETTEXT, 0, ToAscii(Text$))
FreeMemory(*Buffer2)
EndIf
FreeMemory(*Buffer)
EndIf
SetGadgetAttribute(FileViewer()\Gadget, #PB_Editor_ReadOnly, 1)
Else
MessageRequester(#ProductName$, Language("FileStuff","MiscLoadError"), #FLAG_Error)
EndIf
CloseFile(#FILE_FileViewer)
Else
MessageRequester(#ProductName$, Language("FileStuff","MiscLoadError"), #FLAG_Error)
EndIf
EndIf
SetGadgetState(#GADGET_FileViewer_Panel, CountGadgetItems(#GADGET_FileViewer_Panel)-1)
If LastElement(FileViewer())
StatusBarText(#STATUSBAR_FileViewer, 0, FileViewer()\FileName$)
EndIf
EndIf
ResizeFileViewer()
FileViewer_UpdateButtonStates()
EndProcedure
Procedure FileViewerWindowEvents(EventID)
If EventID = #PB_Event_GadgetDrop Or EventID = #PB_Event_WindowDrop
Files$ = EventDropFiles()
Count = CountString(Files$, Chr(10))+1
For i = 1 To Count
File$ = StringField(Files$, i, Chr(10))
If File$ <> "" And FileSize(File$) <> -2 ; no directories!
FileViewer_OpenFile(File$)
EndIf
Next i
ElseIf EventID = #PB_Event_Menu
Select EventMenu()
Case #MENU_FileViewer_Open
Pattern$ = Language("FileViewer","Pattern")
CompilerIf #CompileWindows = 0
; have to remove the .ico pattern...
Pattern$ = RemoveString(Pattern$, ", *.ico")
Pattern$ = RemoveString(Pattern$, ",*.ico")
Pattern$ = RemoveString(Pattern$, ";*.ico")
CompilerEndIf
FileName$ = OpenFileRequester(Language("FileStuff","OpenFileTitle"), FileViewerPath$, AddTools_PatternStrings$ + Pattern$, FileViewerPattern, #PB_Requester_MultiSelection)
If FileName$
FileViewerPattern = SelectedFilePattern()
FileViewerPath$ = GetPathPart(FileName$)
While FileName$
FileViewer_OpenFile(FileName$)
FileName$ = NextSelectedFileName()
Wend
EndIf
Case #MENU_FileViewer_Close
If CountGadgetItems(#GADGET_FileViewer_Panel) > 0
SelectElement(FileViewer(), GetGadgetState(#GADGET_FileViewer_Panel))
If FileViewer()\Image
FreeImage(FileViewer()\Image)
EndIf
If ListIndex(FileViewer()) = 0
DeleteElement(FileViewer())
FirstElement(FileViewer())
Else
DeleteElement(FileViewer())
EndIf
RemoveGadgetItem(#GADGET_FileViewer_Panel, GetGadgetState(#GADGET_FileViewer_Panel))
If ListSize(FileViewer()) > 0
StatusBarText(#STATUSBAR_FileViewer, 0, FileViewer()\FileName$)
Else
StatusBarText(#STATUSBAR_FileViewer, 0, "")
EndIf
EndIf
FileViewer_UpdateButtonStates()
Case #MENU_FileViewer_Previous
If ListSize(FileViewer()) > 0
index = GetGadgetState(#GADGET_FileViewer_Panel)
If index = 0
SetGadgetState(#GADGET_FileViewer_Panel, CountGadgetItems(#GADGET_FileViewer_Panel)-1)
LastElement(FileViewer())
Else
SetGadgetState(#GADGET_FileViewer_Panel, index-1)
SelectElement(FileViewer(), index-1)
EndIf
StatusBarText(#STATUSBAR_FileViewer, 0, FileViewer()\FileName$)
EndIf
FileViewer_UpdateButtonStates()
Case #MENU_FileViewer_Next
If ListSize(FileViewer()) > 0
index = GetGadgetState(#GADGET_FileViewer_Panel)
If index = CountGadgetItems(#GADGET_FileViewer_Panel)-1
SetGadgetState(#GADGET_FileViewer_Panel, 0)
FirstElement(FileViewer())
Else
SetGadgetState(#GADGET_FileViewer_Panel, index+1)
SelectElement(FileViewer(), index+1)
EndIf
StatusBarText(#STATUSBAR_FileViewer, 0, FileViewer()\FileName$)
EndIf
FileViewer_UpdateButtonStates()
EndSelect
ElseIf EventID = #PB_Event_Gadget
If EventGadget() = #GADGET_FileViewer_Panel
If SelectElement(FileViewer(), GetGadgetState(#GADGET_FileViewer_Panel))
StatusBarText(#STATUSBAR_FileViewer, 0, FileViewer()\FileName$)
EndIf
FileViewer_UpdateButtonStates()
EndIf
ElseIf EventID = #PB_Event_SizeWindow
ResizeFileViewer()
ElseIf EventID = #PB_Event_CloseWindow
If MemorizeWindow And IsWindowMinimized(#WINDOW_FileViewer) = 0
FileViewerMaximize = IsWindowMaximized(#WINDOW_FileViewer)
If FileViewerMaximize = 0
FileViewerX = WindowX(#WINDOW_FileViewer)
FileViewerY = WindowY(#WINDOW_FileViewer)
FileViewerWidth = WindowWidth(#WINDOW_FileViewer)
FileViewerHeight = WindowHeight(#WINDOW_FileViewer)
EndIf
EndIf
ForEach FileViewer()
If FileViewer()\Image
FreeImage(FileViewer()\Image)
EndIf
Next FileViewer()
ClearList(FileViewer())
CloseWindow(#WINDOW_FileViewer)
EndIf
EndProcedure
Procedure UpdateFileViewerWindow()
SetWindowTitle(#WINDOW_FileViewer, Language("FileViewer","Title"))
; re-create the toolbar to apply theme changes
FreeToolBar(#TOOLBAR_FileViewer)
If CreateToolBar(#TOOLBAR_FileViewer, WindowID(#WINDOW_FileViewer))
ToolBarImageButton(#MENU_FileViewer_Open, ImageID(#IMAGE_FileViewer_Open))
ToolBarImageButton(#MENU_FileViewer_Close, ImageID(#IMAGE_FileViewer_Close))
ToolBarSeparator()
ToolBarImageButton(#MENU_FileViewer_Previous, ImageID(#IMAGE_FileViewer_Left))
ToolBarImageButton(#MENU_FileViewer_Next, ImageID(#IMAGE_FileViewer_Right))
ToolBarToolTip(#TOOLBAR_FileViewer, #MENU_FileViewer_Open, Language("FileViewer","Open"))
ToolBarToolTip(#TOOLBAR_FileViewer, #MENU_FileViewer_Close, Language("FileViewer","Close"))
ToolBarToolTip(#TOOLBAR_FileViewer, #MENU_FileViewer_Previous, Language("FileViewer","Previous"))
ToolBarToolTip(#TOOLBAR_FileViewer, #MENU_FileViewer_Next, Language("FileViewer","Next"))
EndIf
FileViewer_UpdateButtonStates()
EndProcedure