forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelpViewer.pb
536 lines (401 loc) · 19.6 KB
/
HelpViewer.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
;--------------------------------------------------------------------------------------------
; 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.
;--------------------------------------------------------------------------------------------
;
; Crossplatform help viewer that uses the WebGadget for html display
; and a contsnts/index file in xml (generated by docmaker)
;
; Used only on Mac for now
;
CompilerIf #CompileMac
; stores the urls for the index and contents table and search results
Global NewList Help_Index.s()
Global NewList Help_Contents.s()
Global NewList Help_Search.s()
Global HelpDirectory$
Global HelpLanguage$
Procedure SetHelpDirectory()
CompilerIf #SpiderBasic
; For now, we ship only english doc
HelpDirectory$ = PureBasicPath$+"help/"+LCase(#ProductName$)+"/"
CompilerElse
Select UCase(CurrentLanguage$)
Case "FRANCAIS"
HelpDirectory$ = PureBasicPath$+"help/"+LCase(#ProductName$)+"_french/"
Case "DEUTSCH"
HelpDirectory$ = PureBasicPath$+"help/"+LCase(#ProductName$)+"_german/"
Default
HelpDirectory$ = PureBasicPath$+"help/"+LCase(#ProductName$)+"/"
EndSelect
CompilerEndIf
EndProcedure
Procedure LoadHelpPage(Url$)
Debug "current: " + GetGadgetText(#GADGET_Help_Viewer)
Debug "setting: " + Url$
SetGadgetText(#GADGET_Help_Viewer, "file://" + ReplaceString(HelpDirectory$, " ", "%20") + Url$) ; we need escaped space as it's a web path
EndProcedure
Procedure AddXMLHelpEntries(*Node, Sublevel)
; add all subitems of a contents entry to the help
*Child = ChildXMLNode(*Node)
While *Child
If XMLNodeType(*Child) = #PB_XML_Normal And GetXMLNodeName(*Child) = "entry"
; add gadget entry
AddGadgetItem(#GADGET_Help_Tree, -1, GetXMLAttribute(*Child, "title"), 0, Sublevel)
; add url entry
AddElement(Help_Contents())
Help_Contents() = GetXMLAttribute(*Child, "url")
; add any child entries
AddXMLHelpEntries(*Child, Sublevel+1)
EndIf
*Child = NextXMLNode(*Child)
Wend
EndProcedure
Procedure LoadHelpIndex()
; Load the contents tree
;
ClearList(Help_Contents())
ClearGadgetItems(#GADGET_Help_Tree)
If LoadXML(#XML_Help, HelpDirectory$ + "contents.xml")
; do not check XMLStatus, just use whatever got loaded correctly in any case
*Contents = MainXMLNode(#XML_Help)
If *Contents And GetXMLNodeName(*Contents) = "contents"
AddXMLHelpEntries(*Contents, 0)
EndIf
FreeXML(#XML_Help)
EndIf
; Load the index file
;
ClearList(Help_Index())
ClearGadgetItems(#GADGET_Help_Index)
If LoadXML(#XML_Help, HelpDirectory$ + "index.xml")
*Index = MainXMLNode(#XML_Help)
If *Index And GetXMLNodeName(*Index) = "index"
*Child = ChildXMLNode(*Index)
While *Child
If XMLNodeType(*Child) = #PB_XML_Normal And GetXMLNodeName(*Child) = "entry"
; add gadget entry
AddGadgetItem(#GADGET_Help_Index, -1, GetXMLAttribute(*Child, "title"))
; add url entry
AddElement(Help_Index())
Help_Index() = GetXMLAttribute(*Child, "url")
EndIf
*Child = NextXMLNode(*Child)
Wend
EndIf
FreeXML(#XML_Help)
EndIf
EndProcedure
Procedure OpenHelpWindow()
HelpLanguage$ = CurrentLanguage$
SetHelpDirectory()
If IsWindow(#WINDOW_Help) = 0
Flags = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget | #PB_Window_Invisible
If HelpWindowX = -1 And HelpWindowY = -1
Flags | #PB_Window_ScreenCentered
EndIf
If HelpWindowMaximized
Flags | #PB_Window_Maximize
EndIf
If OpenWindow(#WINDOW_Help, HelpWindowX, HelpWindowY, HelpWindowWidth, HelpWindowHeight, Language("Help","Title"), Flags)
ContainerGadget(#GADGET_Help_Container, 0, 0, 0, 0, #PB_Container_BorderLess)
ButtonImageGadget(#GADGET_Help_Back, 0, 0, 0, 0, ImageID(#IMAGE_Help_Back))
ButtonImageGadget(#GADGET_Help_Forward, 0, 0, 0, 0, ImageID(#IMAGE_Help_Forward))
ButtonImageGadget(#GADGET_Help_Home, 0, 0, 0, 0, ImageID(#IMAGE_Help_Home))
ButtonImageGadget(#GADGET_Help_Previous,0, 0, 0, 0, ImageID(#IMAGE_Help_Previous))
ButtonImageGadget(#GADGET_Help_Next, 0, 0, 0, 0, ImageID(#IMAGE_Help_Next))
WebGadget(#GADGET_Help_Viewer, 0, 0, 0, 0, "file://" + ReplaceString(HelpDirectory$, " ", "%20") + "reference/reference.html") ; we need escaped space as it's a web path
CloseGadgetList()
GadgetToolTip(#GADGET_Help_Back, Language("Help","Back"))
GadgetToolTip(#GADGET_Help_Forward, Language("Help","Forward"))
GadgetToolTip(#GADGET_Help_Home, Language("Help","Home"))
GadgetToolTip(#GADGET_Help_Previous,Language("Help","Previous"))
GadgetToolTip(#GADGET_Help_Next, Language("Help","Next"))
PanelGadget(#GADGET_Help_Panel, 0, 0, 0, 0)
AddGadgetItem(#GADGET_Help_Panel, -1, Language("Help", "Contents"))
TreeGadget(#GADGET_Help_Tree, 0, 0, 0, 0)
AddGadgetItem(#GADGET_Help_Panel, -1, Language("Help", "Index"))
ListViewGadget(#GADGET_Help_Index, 0, 0, 0, 0)
AddGadgetItem(#GADGET_Help_Panel, -1, Language("Help", "Search"))
StringGadget(#GADGET_Help_SearchValue, 10, 10, 0, 25, "")
ButtonGadget(#GADGET_Help_SearchGo, 0, 45, 100, 25, Language("Help", "StartSearch"))
ListViewGadget(#GADGET_Help_SearchResults, 0, 80, 0, 0)
CloseGadgetList()
SplitterGadget(#GADGET_Help_Splitter, 5, 10+HelpButtonHeight, HelpWindowWidth-10, HelpWindowHeight-15-HelpButtonHeight, #GADGET_Help_Panel, #GADGET_Help_Container, #PB_Splitter_Vertical)
LoadHelpIndex() ; load the index/contents and fill the gadgets
AddKeyboardShortcut(#WINDOW_Help, #PB_Shortcut_Return, #MENU_Help_Enter)
EnsureWindowOnDesktop(#WINDOW_Help)
HideWindow(#WINDOW_Help, 0)
ResizeGadget(#GADGET_Help_Splitter, 5, 10+HelpButtonHeight, WindowWidth(#WINDOW_Help)-10, WindowHeight(#WINDOW_Help)-15-HelpButtonHeight)
SetGadgetState(#GADGET_Help_Splitter, HelpWindowSplitter)
HelpWindowEvents(#PB_Event_SizeWindow)
EndIf
Else
SetWindowForeground(#WINDOW_Help)
EndIf
EndProcedure
Procedure SearchHelpFile(Url$, Text$)
Length = Len(Text$)
match = 0
title = 0
Title$ = ""
; The html files are small. We can load them fully into memory for a fast search
;
If ReadFile(#FILE_HelpSearch, HelpDirectory$+Url$)
Size = Lof(#FILE_HelpSearch)
If Size > 0 ; just a sanity check
*Buffer = AllocateMemory(Size+SizeOf(Character)) ; null char at the end for for simple CompareMEmoryString
If *Buffer
ReadData(#FILE_HelpSearch, *Buffer, Size)
; Note: this assumes that the IDE is compiled in ascii (as the html file is ascii too)
*Cursor.Ascii = *Buffer
Size - Length
While Size > 0 And (match = 0 Or title = 0) ; need to find a matching word and a title tag
If CompareMemoryString(*Cursor, ToAscii(Text$), #PB_String_NoCase, Length, #PB_Ascii) = #PB_String_Equal
; match
match = 1
*Cursor + Length
Size - Length
ElseIf CompareMemoryString(*Cursor, ToAscii("<title>"), #PB_String_NoCase, 7, #PB_Ascii) = #PB_String_Equal
; title tag
title = 1
*TitleEnd = *Cursor + 7
While Size > (*TitleEnd - *Cursor) And CompareMemoryString(*TitleEnd, ToAscii("</title>"), #PB_String_NoCase, 8, #PB_Ascii) <> #PB_String_Equal
*TitleEnd + 1
Wend
Title$ = Trim(PeekS(*Cursor+7, *TitleEnd - *Cursor - 7, #PB_Ascii))
*Cursor + 7 ; just jump over the <title> so the title text is searched too!
Size - 7
ElseIf *Cursor\a = '<'
; other tag (do not search in tags)
*Cursor + 1
Size - 1
While Size > 0 And *Cursor\a <> '>'
*Cursor + 1
Size - 1
Wend
*Cursor + 1 ; skip >
Size - 1
Else
Size - 1
*Cursor + 1
EndIf
Wend
FreeMemory(*Buffer)
EndIf
EndIf
CloseFile(#FILE_HelpSearch)
EndIf
If match
AddGadgetItem(#GADGET_Help_SearchResults, -1, Title$)
AddElement(Help_Search())
Help_Search() = Url$
EndIf
EndProcedure
Procedure ResizeHelpSplitterContent()
; Panel part
PanelWidth = GetGadgetAttribute(#GADGET_Help_Panel, #PB_Panel_ItemWidth)
PanelHeight = GetGadgetAttribute(#GADGET_Help_Panel, #PB_Panel_ItemHeight)
GetRequiredSize(#GADGET_Help_SearchGo, @ButtonWidth, @ButtonHeight)
ButtonWidth = Max(ButtonWidth, 100)
StringHeight = GetRequiredHeight(#GADGET_Help_SearchValue)
ResizeGadget(#GADGET_Help_Tree , 0, 0, PanelWidth, PanelHeight)
ResizeGadget(#GADGET_Help_Index , 0, 0, PanelWidth, PanelHeight)
ResizeGadget(#GADGET_Help_SearchValue , 10, 10, PanelWidth-20, StringHeight)
ResizeGadget(#GADGET_Help_SearchGo , (PanelWidth-ButtonWidth)/2, 20+StringHeight, ButtonWidth, ButtonHeight)
ResizeGadget(#GADGET_Help_SearchResults, 0, 30+StringHeight+ButtonHeight, PanelWidth, PanelHeight-30-StringHeight-ButtonHeight)
; Viewer part
Width = GadgetWidth(#GADGET_Help_Container)
Height = GadgetHeight(#GADGET_Help_Container)
GetRequiredSize(#GADGET_Help_Back, @ButtonWidth, @ButtonHeight)
ResizeGadget(#GADGET_Help_Back, 2, 0, ButtonWidth, ButtonHeight) ; Don't use 0 as x axis, as on OS X the splitter do some artefacts
ResizeGadget(#GADGET_Help_Forward, 10+ButtonWidth, 0, ButtonWidth, ButtonHeight)
ResizeGadget(#GADGET_Help_Home, 18+ButtonWidth*2, 0, ButtonWidth, ButtonHeight)
ResizeGadget(#GADGET_Help_Previous, Width-13-ButtonWidth*2, 0, ButtonWidth, ButtonHeight)
ResizeGadget(#GADGET_Help_Next, Width-5-ButtonWidth, 0, ButtonWidth, ButtonHeight)
ResizeGadget(#GADGET_Help_Viewer, 2, 10+ButtonHeight, Width-4, Height-10-ButtonHeight)
EndProcedure
Procedure ResizeHelpWindow()
ResizeGadget(#GADGET_Help_Splitter, 5, 10+HelpButtonHeight, WindowWidth(#WINDOW_Help)-10, WindowHeight(#WINDOW_Help)-15-HelpButtonHeight)
EndProcedure
Procedure HelpWindowEvents(EventID)
If EventID = #PB_Event_Gadget
EventGadget = EventGadget()
ElseIf EventID = #PB_Event_Menu
If EventMenu() = #MENU_Help_Enter And GetActiveGadget() = #GADGET_Help_SearchValue
EventID = #PB_Event_Gadget
EventGadget = #GADGET_Help_SearchGo
EndIf
EndIf
Select EventID
Case #PB_Event_CloseWindow
If MemorizeWindow And IsWindowMinimized(#WINDOW_Help) = 0
HelpWindowMaximized = IsWindowMaximized(#WINDOW_Help)
If HelpWindowMaximized = 0
HelpWindowX = WindowX(#WINDOW_Help)
HelpWindowY = WindowY(#WINDOW_Help)
HelpWindowWidth = WindowWidth(#WINDOW_Help)
HelpWindowHeight = WindowHeight(#WINDOW_Help)
EndIf
HelpWindowSplitter = GetGadgetState(#GADGET_Help_Splitter)
EndIf
CloseWindow(#WINDOW_Help)
Case #PB_Event_Gadget
Select EventGadget
Case #GADGET_Help_Tree
index = GetGadgetState(#GADGET_Help_Tree)
If index <> -1
SelectElement(Help_Contents(), index)
LoadHelpPage(Help_Contents())
EndIf
Case #GADGET_Help_Index
index = GetGadgetState(#GADGET_Help_Index)
If index <> -1
SelectElement(Help_Index(), index)
LoadHelpPage(Help_Index())
EndIf
Case #GADGET_Help_Home
LoadHelpPage("reference/reference.html")
Case #GADGET_Help_Back
SetGadgetState(#GADGET_Help_Viewer, #PB_Web_Back)
Case #GADGET_Help_Forward
SetGadgetState(#GADGET_Help_Viewer, #PB_Web_Forward)
Case #GADGET_Help_Previous, #GADGET_Help_Next
Url$ = GetGadgetText(#GADGET_Help_Viewer)
Url$ = ReplaceString(Right(Url$, Len(Url$)-Len(HelpDirectory$)-7), "%20", " ") ; cut the base and "file://", and unescape the URL
; find this url in the contents to get the next/previous chapter entry
ForEach Help_Contents()
If IsEqualFile(Url$, Help_Contents()) ; do not use simple compare, as GetGadgetText on WebGadget/OSX changes the casing of the names
Sublevel = GetGadgetItemAttribute(#GADGET_Help_Tree, ListIndex(Help_Contents()), #PB_Tree_SubLevel)
; the next/previous entry in the chapter is the next/previous in the list, but only
; if it has the same sublevel in the contents tree
;
If EventGadget = #GADGET_Help_Previous
Result = PreviousElement(Help_Contents())
Else
Result = NextElement(Help_Contents())
EndIf
If Result And GetGadgetItemAttribute(#GADGET_Help_Tree, ListIndex(Help_Contents()), #PB_Tree_SubLevel) = Sublevel
LoadHelpPage(Help_Contents())
EndIf
Break
EndIf
Next Help_Contents()
Case #GADGET_Help_SearchGo
ClearGadgetItems(#GADGET_Help_SearchResults)
ClearList(Help_Search())
Search$ = Trim(GetGadgetText(#GADGET_Help_SearchValue))
; The html help is organized in one level of directories with html files inside
;
If Search$ <> ""
If ExamineDirectory(0, HelpDirectory$, "*")
While NextDirectoryEntry(0)
If DirectoryEntryType(0) = #PB_DirectoryEntry_Directory
Directory$ = DirectoryEntryName(0)
If Directory$ <> "." And Directory$ <> ".."
; search directory content
If ExamineDirectory(1, HelpDirectory$ + Directory$, "*.html")
While NextDirectoryEntry(1)
If DirectoryEntryType(1) = #PB_DirectoryEntry_File
; search the file content
SearchHelpFile(Directory$ + "/" + DirectoryEntryName(1), Search$)
EndIf
Wend
FinishDirectory(1)
EndIf
EndIf
EndIf
Wend
FinishDirectory(0)
EndIf
EndIf
If ListSize(Help_Search()) = 0 ; no results found
AddGadgetItem(#GADGET_Help_SearchResults, 0, Language("Help", "NoResults"))
AddElement(Help_Search())
Help_Search() = "reference/reference.html" ; in case the user clicks this
EndIf
Case #GADGET_Help_SearchResults
index = GetGadgetState(#GADGET_Help_SearchResults)
If index <> -1
SelectElement(Help_Search(), index)
LoadHelpPage(Help_Search())
EndIf
Case #GADGET_Help_Splitter
ResizeHelpSplitterContent()
EndSelect
Case #PB_Event_SizeWindow
ResizeHelpWindow()
EndSelect
EndProcedure
Procedure UpdateHelpWindow()
SetWindowTitle(#WINDOW_Help, Language("Help","Title"))
SetGadgetAttribute(#GADGET_Help_Back, #PB_Button_Image, ImageID(#IMAGE_Help_Back)) ; possible theme change
SetGadgetAttribute(#GADGET_Help_Forward, #PB_Button_Image, ImageID(#IMAGE_Help_Forward))
SetGadgetAttribute(#GADGET_Help_Home, #PB_Button_Image, ImageID(#IMAGE_Help_Home))
SetGadgetAttribute(#GADGET_Help_Previous, #PB_Button_Image, ImageID(#IMAGE_Help_Previous))
SetGadgetAttribute(#GADGET_Help_Next, #PB_Button_Image, ImageID(#IMAGE_Help_Next))
GadgetToolTip(#GADGET_Help_Back, Language("Help","Back"))
GadgetToolTip(#GADGET_Help_Forward, Language("Help","Forward"))
GadgetToolTip(#GADGET_Help_Home, Language("Help","Home"))
GadgetToolTip(#GADGET_Help_Previous,Language("Help","Previous"))
GadgetToolTip(#GADGET_Help_Next, Language("Help","Next"))
SetGadgetText(#GADGET_Help_SearchGo, Language("Help", "StartSearch"))
SetGadgetItemText(#GADGET_Help_Panel, 0, Language("Help", "Contents"), 0)
SetGadgetItemText(#GADGET_Help_Panel, 1, Language("Help", "Index"), 0)
SetGadgetItemText(#GADGET_Help_Panel, 2, Language("Help", "Search"), 0)
HelpWindowEvents(#PB_Event_SizeWindow)
; Reload the index, content and show the reference page if the language changed
;
If HelpLanguage$ <> CurrentLanguage$
HelpLanguage$ = CurrentLanguage$
SetHelpDirectory()
LoadHelpIndex()
LoadHelpPage("reference/reference.html")
ClearGadgetItems(#GADGET_Help_SearchResults)
ClearList(Help_Search())
EndIf
EndProcedure
Procedure DisplayHelp(CurrentWord$)
SetHelpDirectory()
If CheckPureBasicKeyWords(CurrentWord$) <> ""
HelpFile$ = CheckPureBasicKeyWords(CurrentWord$)+".html"
Else
CurrentWord$ = LCase(CurrentWord$)
For k=0 To NbBasicFunctions-1
If CurrentWord$ = LCase(BasicFunctions(k)\Name$)
; We don't have the matching table for a function, so scan every directories to find the file
;
DirID = ExamineDirectory(#PB_Any, HelpDirectory$, "*")
If DirID
While NextDirectoryEntry(DirID)
If DirectoryEntryType(DirID) = #PB_DirectoryEntry_Directory
If FileSize(HelpDirectory$+DirectoryEntryName(DirID)+"/"+CurrentWord$+".html") > 0
HelpFile$ = DirectoryEntryName(DirID)+"/"+CurrentWord$+".html"
EndIf
EndIf
Wend
FinishDirectory(DirID)
EndIf
Break
EndIf
Next
EndIf
If HelpFile$ = ""
HelpFile$ = "reference/reference.html"
EndIf
CompilerIf #CompileMacCarbon
; Use the old way on OS X, due to faulty WebGadget() on Carbon (consumes 100% CPU)
;
RunProgram("open", HelpDirectory$ + HelpFile$, "")
CompilerElse
If UseHelpToolF1 And HelpToolOpen
SetGadgetText(#GADGET_HelpTool_Viewer, "file://"+ReplaceString(HelpDirectory$, " ", "%20")+HelpFile$) ; we need escaped space as it's a web path
ActivateTool("HelpTool") ; switches to the tool
Else
OpenHelpWindow()
LoadHelpPage(HelpFile$)
EndIf
CompilerEndIf
EndProcedure
CompilerEndIf