summaryrefslogtreecommitdiff
path: root/scripting/workben/bindings/ScriptBinding.xba
blob: 23135dc90a926472c636f00f7cb304baec324728 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="ScriptBinding" script:language="StarBasic">REM  *****  BASIC  *****

REM ----- Global Variables -----

&apos;bindingDialog can refer to either KeyBinding or MenuBinding dialog
private bindingDialog as object
private helpDialog as object

&apos;Array to store lines from the xml file
private xmlFile() as string
&apos;Name of the xml file [writer/calc][menubar/keybindings].xml
private xmlFileName as string
&apos;Number of lines in the xml file
private numberOfLines as integer

&apos;Parallel arrays to store all top-level menu names and line positions
private menuItems() as string
private menuItemLinePosition() as integer
&apos;Counter for the number of top-level menus
private menuCount as integer

&apos;Parallel arrays to store all sub-menu names and line positions for a particular top-level menu
private subMenuItems() as string
private subMenuItemLinePosition() as integer
&apos;Counter for the number of sub-menus
private subMenuCount as integer

&apos;Parallel arrays to store all script names and line positions
private scriptNames() as string
private scriptLinePosition() as integer
&apos;Counter for the number of scripts
private scriptCount as integer

&apos;Array to store all combinations of key bindings
private allKeyBindings() as string


REM ------ Storage Refresh Function ------


sub RefreshUserScripts()
   smgr = getProcessServiceManager()
   context = smgr.getPropertyValue( &quot;DefaultContext&quot; )
   scriptstoragemgr = context.getValueByName( &quot;/singletons/drafts.com.sun.star.script.framework.storage.theScriptStorageManager&quot; )

   storage = scriptstoragemgr.getScriptStorage( 1 )
   storage.refresh()
end sub 


REM ----- Launch Functions -----

Sub ExecuteKeyBinding()
	xmlFileName = GetDocumentType( &quot;Key&quot; )
	
	if not (ReadXMLToArray( &quot;Key&quot; )) then
		Exit Sub
	endif
	
	bindingDialog = LoadDialog( &quot;ScriptBindingLibrary&quot;, &quot;KeyBinding&quot; )
	PopulateScriptList()
	CreateAllKeyBindings()
	PopulateTopLevelKeyBindingList()
	PopulateKeyBindingList( 1, 11 )
	bindingDialog.execute()
end Sub


Sub ExecuteMenuBinding()
	xmlFileName = GetDocumentType( &quot;Menu&quot; )
	if not (ReadXMLToArray( &quot;Menu&quot; )) then
		Exit Sub
	endif

	bindingDialog = LoadDialog( &quot;ScriptBindingLibrary&quot;, &quot;MenuBinding&quot; )
	PopulateScriptList()
	PopulateMenuCombo()
	PopulateSubMenuList( 1 )
	MenuLabelBoxListener()
	bindingDialog.execute()	
end Sub


REM ----- Initialising functions -----


function LoadDialog( libName as string, dialogName as string ) as object
	dim library as object
	dim libDialog as object
	dim runtimeDialog as object

	libContainer = DialogLibraries
	libContainer.LoadLibrary( libName )
	library = libContainer.getByName( libname )
	libDialog = library.getByName( dialogName )
	runtimeDialog = CreateUnoDialog( libDialog )
	
    LoadDialog() = runtimeDialog
end function


function GetDocumentType( bindingType as string ) as string
	document = StarDesktop.ActiveFrame.Controller.Model
	Dim errornumber As Integer
	errornumber = 111
	Error errornumber
	if document.SupportsService(&quot;com.sun.star.sheet.SpreadsheetDocument&quot;) then
		if bindingType = &quot;Key&quot; then
			GetDocumentType() = &quot;calckeybinding.xml&quot;
		else
			if bindingType = &quot;Menu&quot; then
				GetDocumentType() = &quot;calcmenubar.xml&quot;
			end if
		end if
	else 
		if document.SupportsService(&quot;com.sun.star.text.TextDocument&quot;) then
			if bindingType = &quot;Key&quot; then
				GetDocumentType() = &quot;writerkeybinding.xml&quot;
			else
				if bindingType = &quot;Menu&quot; then
					GetDocumentType() = &quot;writermenubar.xml&quot;
				end if
			end if
		else
			msgbox &quot;Couldn&apos;t determine file type&quot;
		end if
	end if
end function


function GetOfficePath() as string
	REM Error check and prompt user to manually input Office Path
	settings =  CreateUnoService( &quot;com.sun.star.frame.Settings&quot; )
	path = settings.getByName( &quot;PathSettings&quot; )
	unformattedOfficePath = path.getPropertyValue( &quot;UserPath&quot; )

	dim officePath as string 
	const removeFromEnd = &quot;/user&quot;
	const removeFromEndWindows = &quot;\user&quot;

	REM If Solaris or Linux
	if not ( instr( unformattedOfficePath, removeFromEnd ) = 0 ) then
		endPosition = instr( unformattedOfficePath, removeFromEnd )
		officePath = mid( unformattedOfficePath, 1, endPosition )
	REM If Windows
	else if not ( instr( unformattedOfficePath, removeFromEndWindows ) = 0 ) then
			endPosition = instr( unformattedOfficePath, removeFromEndWindows )
			officePath = mid( unformattedOfficePath, 1, endPosition )
			while instr( officePath, &quot;\&quot; ) &gt; 0
				backSlash = instr( officePath, &quot;\&quot; )
				startPath = mid( officePath, 1, backSlash - 1 )
				endPath = mid( officePath, backslash + 1, len( officePath ) - backSlash )
				officePath = startPath + &quot;/&quot; + endPath
			wend
		else
			msgbox &quot;Office path not found&quot;
			REM Prompt user
		end if
	end if
	
	GetOfficePath() = officePath
end function



REM ----- File I/O functions -----


function ReadXMLToArray( bindingType as string ) as boolean
	On Error Goto ErrorHandler
	simplefileaccess = CreateUnoService( &quot;com.sun.star.ucb.SimpleFileAccess&quot; )
	filestream = simplefileaccess.openFileRead( &quot;file://&quot; + GetOfficePath() + &quot;user/config/soffice.cfg/&quot; + xmlFileName )

    textin = CreateUnoService( &quot;com.sun.star.io.TextInputStream&quot; )
    textin.setInputStream( filestream )

    redim xmlFile( 400 ) as String
    redim menuItems( 30 ) as String
    redim menuItemLinePosition( 30 ) as Integer
    redim scriptNames( 120 ) as string
    redim scriptLinePosition( 120) as integer
    
    lineCount = 1
    menuCount = 1
    scriptCount = 1
    
    do while not textin.isEOF()
    	xmlline = textin.readLine()
    	xmlFile( lineCount ) = xmlline

 		const menuItemWhiteSpace = 2
 		const menuXMLTag = &quot;&lt;menu:menu&quot;

		&apos;If line read from XML file is a Menu title
		if bindingType = &quot;Menu&quot; then
			&apos;If the xml line is a top-level menu
			if instr( xmlline, menuXMLTag ) = menuItemWhiteSpace then
				menuLabel = ExtractLabelFromXMLLine( xmlline )
				menuItems( menuCount ) = menuLabel
				menuItemLinePosition( menuCount ) = lineCount
				menuCount = menuCount + 1
			end if
		else if bindingType = &quot;Key&quot; then
				&apos;If the xml line is a key binding
				if instr( xmlFile( lineCount ), &quot;&lt;accel:item&quot; ) &gt; 0 then
					scriptName = &quot;&quot;
					if instr( xmlFile( lineCount ), &quot;accel:shift=&quot;+chr$(34)+&quot;true&quot;+chr$(34) ) &gt; 0 then
						scriptName = scriptName + &quot;SHIFT + &quot;
					end if
					if instr( xmlFile( lineCount ), &quot;accel:mod1=&quot;+chr$(34)+&quot;true&quot;+chr$(34) ) &gt; 0 then
						scriptName = scriptName + &quot;CONTROL + &quot;
					end if				
					scriptName = scriptName + ExtractKeyCodeFromXMLLine( xmlline )
					scriptNames( scriptCount ) = scriptName
				&apos;	msgbox( &quot;scriptNames( &quot; + scriptCount + &quot; )  &quot; + scriptNames( scriptCount ) )
					scriptLinePosition( scriptCount ) = lineCount
					scriptCount = scriptCount + 1
				end if
			end if
		end if

        lineCount = lineCount + 1
    loop
	
	&apos;Set global variable numberOfLines (lineCount is one too many at end of the loop)
	numberOfLines = lineCount - 1
	&apos;Set global variable scriptCount (it is one too many at end of the loop)
	scriptCount = scriptCount - 1
	&apos;Set global variable menuCount (it is one too many at end of the loop)
	menuCount = menuCount - 1
	
    filestream.closeInput()
    ReadXMLToArray( ) = true
	Exit function

	ErrorHandler:
	reset
	MsgBox (&quot;Error: Unable to read Star Office configuration file - &quot; + xmlFileName + chr$(10) + chr$(10) + &quot;Action: Please reinstall Scripting Framework&quot;,0,&quot;Error&quot; )
    ReadXMLToArray( ) = false
end function


Sub WriteXMLFromArray()
	On Error Goto ErrorHandler
	originalFile = &quot;file:///&quot; + GetOfficePath() + &quot;user/config/soffice.cfg/&quot; + xmlFileName 
	backupFile = originalFile + &quot;.tmp&quot;

    simplefileaccess = CreateUnoService( &quot;com.sun.star.ucb.SimpleFileAccess&quot; )
    simplefileaccess.move( originalFile, backupFile ) 
    outfilestream = simplefileaccess.openFileWrite( originalFile )
        
    REM Alternative Debug Lines
    &apos;simplefileaccess.kill( &quot;file:///&quot; + GetOfficePath() + &quot;user/config/soffice.cfg/&quot; + xmlFileName + &quot;.temp&quot; )
    &apos;outfilestream = simplefileaccess.openFileWrite( &quot;file:///&quot;+ GetOfficePath() + &quot;user/config/soffice.cfg/&quot; + xmlFileName + &quot;.temp&quot; )
        
    textout = CreateUnoService( &quot;com.sun.star.io.TextOutputStream&quot; )
    textout.setOutputStream( outfilestream )

	for n = 1 to numberOfLines
		&apos;If writing the last line then no new line character added
        if n = numberOfLines then
        	textout.writeString( xmlFile(n) )
        else
        	textout.writeString( xmlFile(n) + chr$(10) )
        end if
	next n
		
    outfilestream.flush()
    outfilestream.closeOutput()

    if simplefileaccess.exists( backupFile ) then
    	simplefileaccess.kill( backupFile)
    endif

	Exit Sub
	
	ErrorHandler:
	reset
	MsgBox (&quot;Error: Unable to write to Star Office configuration file&quot; + chr$(10) + &quot;/&quot; + GetOfficePath() + &quot;user/config/soffice.cfg/&quot; +xmlFileName + chr$(10) + chr$(10) + &quot;Action: Please make sure you have write access to this file&quot;,0,&quot;Error&quot; )
    if simplefileaccess.exists( backupFile ) then
    	simplefileaccess.move( backupFile,  originalFile)
    endif
end Sub



REM ----- Array update functions -----


sub AddNewMenuBinding( newScript as string, newMenuLabel as string, newLinePosition as integer )
	dim newXmlFile( 400 ) as string
	dim newLineInserted as boolean
	dim lineCounter as integer
	lineCounter = 1
		
	do while lineCounter &lt;= numberOfLines
		if not newLineInserted then
			REM If the line number is the position at which to insert the new line
			if lineCounter = newLinePosition then
				if( instr( xmlFile( lineCounter ), &quot;&lt;menu:menupopup&gt;&quot; ) &gt; 0 ) then
					msgbox(&quot;It is &lt;menu:menupopup&gt;&quot;)
					indent = GetMenuWhiteSpace( xmlFile( newLinePosition + 1 ) )
					newXmlFile( lineCounter ) = xmlFile( lineCounter )
					newXmlFile( lineCounter + 1 ) = ( indent + &quot;&lt;menu:menuitem menu:id=&quot;+chr$(34)+&quot;script://&quot; + newScript + chr$(34)+&quot; menu:helpid=&quot;+chr$(34)+&quot;1929&quot;+chr$(34)+&quot; menu:label=&quot;+chr$(34)+ newMenuLabel + chr$(34)+&quot;/&gt;&quot; )
				else
					msgbox(&quot;It is NOT &lt;menu:menupopup&gt;&quot;)
					indent = GetMenuWhiteSpace( xmlFile( newLinePosition - 1 ) )
					newXmlFile( lineCounter ) = ( indent + &quot;&lt;menu:menuitem menu:id=&quot;+chr$(34)+&quot;script://&quot; + newScript + chr$(34)+&quot; menu:helpid=&quot;+chr$(34)+&quot;1929&quot;+chr$(34)+&quot; menu:label=&quot;+chr$(34)+ newMenuLabel + chr$(34)+&quot;/&gt;&quot; )
					newXmlFile( lineCounter + 1 ) = xmlFile( lineCounter )									
				end if
			REM added -1 for debug --&gt;
			&apos;	indent = GetMenuWhiteSpace( xmlFile( newLinePosition ) )
			&apos;	newXmlFile( lineCounter ) = ( indent + &quot;&lt;menu:menuitem menu:id=&quot;+chr$(34)+&quot;script://&quot; + newScript + chr$(34)+&quot; menu:helpid=&quot;+chr$(34)+&quot;1929&quot;+chr$(34)+&quot; menu:label=&quot;+chr$(34)+ newMenuLabel + chr$(34)+&quot;/&gt;&quot; )
			&apos;	newXmlFile( lineCounter + 1 ) = xmlFile( lineCounter )
				newLineInserted = true
			else
				newXmlFile( lineCounter ) = xmlFile( lineCounter )
			end if
		else
			REM if the new line has been inserted the read from one position behind
			newXmlFile( lineCounter + 1 ) = xmlFile( lineCounter )
		end if
		lineCounter = lineCounter + 1
	loop
		
	numberOfLines = numberOfLines + 1
	
	REM read the new file into the global array
	for n = 1 to numberOfLines
		xmlFile( n ) = newXmlFile( n )
	next n
	
end sub


sub AddNewKeyBinding( scriptName as string, shift as boolean, control as boolean, key as string )

	dim keyCombo as string
	newLine = &quot; &lt;accel:item accel:code=&quot;+chr$(34)+&quot;KEY_&quot; + key +chr$(34)
	if shift then
		keyCombo = &quot;SHIFT + &quot;
		newLine = newLine + &quot; accel:shift=&quot;+chr$(34)+&quot;true&quot;+chr$(34)
	end if
	if control then
		keyCombo = keyCombo + &quot;CONTROL + &quot;
		newLine = newLine + &quot; accel:mod1=&quot;+chr$(34)+&quot;true&quot;+chr$(34)
	end if
	keyCombo = keyCombo + key
	newLine = newLine + &quot; xlink:href=&quot;+chr$(34)+&quot;script://&quot; + scriptName
	
	REM For this release of the scripting framework only use application binding
	applicationBinding = true
	if applicationBinding then
		newLine = newLine + &quot;?location=application&quot;+chr$(34)+&quot;/&gt;&quot;
	else
		newLine = newLine + &quot;?location=document&quot;+chr$(34)+&quot;/&gt;&quot;
	end if
	
	scriptNames( scriptCount ) = keyCombo
	scriptLinePosition( scriptCount ) = numberOfLines

	scriptCount = scriptCount + 1

	for n = 1 to numberOfLines 
		if n = numberOfLines then
			xmlFile( n ) = newLine
			xmlFile( n + 1 ) = &quot;&lt;/accel:acceleratorlist&gt;&quot;
			exit for
		else
			xmlFile( n ) = xmlFile( n )
		end if
	next n

	numberOfLines = numberOfLines + 1

end sub


sub RemoveBinding( removeLinePosition as integer )
	dim newXmlFile( 400 ) as string
	dim lineRemoved as boolean
	lineRemoved = false

	for n = 1 to numberOfLines
		if not lineRemoved then
			if not( n = removeLinePosition ) then
				newXmlFile( n ) = xmlFile( n )
			else
				newXmlFile( n ) = xmlFile( n + 1 )
				lineRemoved = true
			end if
		else
			newXmlFile( n ) = xmlFile( n + 1 )
		end if
	next n

	numberOfLines = numberOfLines - 1
	
	REM read the new file into the global array
	for n = 1 to numberOfLines
		xmlFile( n ) = newXmlFile( n )
	next n
end sub


REM Adds or removes the starting xml line positions for each top-level menu after the menu with the added script
sub UpdateTopLevelMenus( topLevelMenuPosition as integer, addLine as boolean )
	for n = topLevelMenuPosition to 8
		if addLine then
			menuItemLinePosition( n ) = menuItemLinePosition( n ) + 1
		else
			menuItemLinePosition( n ) = menuItemLinePosition( n ) - 1
		end if
	next n
end sub


REM Remove scriptNames and scriptLinePosition entries
sub RemoveScriptNameAndPosition( keyComboPosition )
	dim updatedScriptNames( 120 ) as string
	dim updatedScriptLinePosition( 120 ) as integer
	dim removedScript as boolean
	removedScript = false
	
	for n = 1 to scriptCount
		if not removedScript then
			if not( n = keyComboPosition ) then
				updatedScriptNames( n ) = scriptNames( n )
			else
				removedScript = true
			end if
		else
			updatedScriptNames( n - 1 ) = scriptNames( n )
		end if
	next n
	scriptCount = scriptCount - 1
	
	for n = 1 to scriptCount
		scriptNames( n ) = updatedScriptNames( n )
	next n
end sub



REM ----- Populating Dialog Controls -----


sub PopulateScriptList()
	scriptList = bindingDialog.getControl( &quot;ScriptList&quot; )
	scriptList.removeItems( 0, scriptList.getItemCount() )

	smgr = getProcessServiceManager()
   	context = smgr.getPropertyValue( &quot;DefaultContext&quot; )
   	scriptstoragemgr = context.getValueByName( &quot;/singletons/drafts.com.sun.star.script.framework.storage.theScriptStorageManager&quot; ) 

   	storage = scriptstoragemgr.getScriptStorage( 1 )
	logicalNames() = storage.getScriptLogicalNames()

	for n = 1 to ubound( logicalNames() ) + 1
		scriptList.addItem( logicalNames( n - 1 ), n )
	next n
	
	scriptList.selectItemPos( 0, true )
end sub


sub PopulateMenuCombo()
	menuComboBox = bindingDialog.getControl( &quot;MenuCombo&quot; )
	menuComboBox.removeItems( 0, menuComboBox.getItemCount() )
	for n = 1 to menuCount
		menuComboBox.addItem( menuItems( n ), n - 1 )
	next n
	menuComboBox.setDropDownLineCount( 8 ) 
	menuComboBox.text = menuComboBox.getItem( 0 )
end sub


sub PopulateSubMenuList( menuItemPosition as integer )
	redim subMenuItems( 100 ) as string
	redim subMenuItemLinePosition( 100 ) as integer
	dim lineNumber as integer
	const menuItemWhiteSpace = 4
	const menuXMLTag = &quot;&lt;menu:menu&quot;
	subMenuCount = 1

	REM xmlStartLine and xmlEndLine refer to the first and last lines
	&apos; menuItemPosition of a top-level menu ( 1=File to 8=Help ) add one line
	xmlStartLine = menuItemLinePosition( menuItemPosition ) + 1

	REM If last menu item is chosen
	if menuItemPosition = menuCount then
		xmlEndLine = numberOfLines
	else
		REM Other wise get the line before the next top-level menu begins
		xmlEndLine = menuItemLinePosition( menuItemPosition + 1 ) - 1
	end if
	
	for lineNumber = xmlStartLine to xmlEndLine
		REM Insert all sub-menus and sub-popupmenus
		if not( instr( xmlFile( lineNumber ), menuXMLTag ) = 0 ) and instr( xmlFile( lineNumber ), &quot;menupopup&quot;) = 0 then
			subMenuIndent = GetMenuWhiteSpace( xmlFile( lineNumber ) )
			if subMenuIndent = &quot; &quot; then
				subMenuIndent = &quot;&quot;
			else
				subMenuIndent = subMenuIndent + subMenuIndent 
			end if
			if not( instr( xmlFile( lineNumber ), &quot;menuseparator&quot; ) = 0 ) then
				subMenuItems( subMenuCount ) = subMenuIndent + &quot;----------------&quot;	
			else
				subMenuName = ExtractLabelFromXMLLine( xmlFile( lineNumber ) )
				REM Add script Name if there is one bound to menu item
				if instr( xmlFile( lineNumber ), &quot;script://&quot; ) &gt; 0 then
					script = ExtractScriptIdFromXMLLine( xmlFile( lineNumber ) )
					subMenuItems( subMenuCount ) = ( subMenuIndent + subMenuName + &quot; [&quot; + script + &quot;]&quot; )
				else
					subMenuItems( subMenuCount ) = subMenuIndent + subMenuName
				end if
			end if
			subMenuItemLinePosition( subMenuCount ) = lineNumber
			subMenuCount = subMenuCount + 1
		end if
	next lineNumber

	subMenuList = bindingDialog.getControl( &quot;SubMenuList&quot; )

	currentPosition = subMenuList.getSelectedItemPos()

	subMenuList.removeItems( 0, subMenuList.getItemCount() )
	for n = 1 to subMenuCount - 1
		subMenuList.addItem( subMenuItems( n ), n - 1 )
	next n

	subMenuList.selectItemPos( currentPosition, true )

	SubMenuListListener()
end sub



sub PopulateTopLevelKeyBindingList()
	keyCombo = bindingDialog.getControl( &quot;KeyCombo&quot; )
	keyCombo.removeItems( 0, keyCombo.getItemCount() )
	keyCombo.addItem( &quot;SHIFT + CONTROL + F keys&quot;, 0 )
	keyCombo.addItem( &quot;SHIFT + CONTROL + digits&quot;, 1 )
	keyCombo.addItem( &quot;SHIFT + CONTROL + letters&quot;, 2 )
	keyCombo.addItem( &quot;CONTROL + F keys&quot;, 3 )
	keyCombo.addItem( &quot;CONTROL + digits&quot;, 4 )
	keyCombo.addItem( &quot;CONTROL + letters&quot;, 5 )
	keyCombo.addItem( &quot;SHIFT + F keys&quot;, 6 )
	
	keyCombo.setDropDownLineCount( 7 ) 
	keyCombo.text = keyCombo.getItem( 0 )
end sub


sub PopulateKeyBindingList( startPosition as integer, endPosition as integer )
	dim formattedKeyBinding( 47 ) as string
	counter = 1
	
	keyList = bindingDialog.getControl( &quot;KeyList&quot; )
	
	for n = startPosition to endPosition
		REM If key combo is a script the value returned is the line position
		if IsAllocatedKeyCombo( allKeyBindings( n ) ) &gt; 1 then
			formattedKeyBinding( counter ) =  ( allKeyBindings( n ) + &quot; [Allocated to &quot; + ExtractScriptIdFromXMLLine( xmlFile( isAllocatedKeyCombo( allKeyBindings( n ) ) ) ) + &quot;]&quot; )
		REM If key combo is an Office function 1 is returned
		else if IsAllocatedKeyCombo( allKeyBindings( n ) ) = 1 then
				formattedKeyBinding( counter ) =  ( allKeyBindings( n ) + &quot; [Allocated to Office function]&quot; )
			REM If key combo is unallocated 0 is returned
			else
				formattedKeyBinding( counter ) = allKeyBindings( n )
			end if
		end if
		counter = counter + 1
	next n
	
	currentPosition = keyList.getSelectedItemPos()
	
	keyList.removeItems( 0, keyList.getItemCount() )
	for n = 1 to counter - 1
		keyList.addItem( formattedKeyBinding( n ), n - 1 )
	next n

	keyList.selectItemPos( currentPosition, true )

	KeyListListener()
end sub



sub CreateAllKeyBindings()
	reDim allKeyBindings( 105 ) as string
	keyBindingPosition = 1

	for FKey = 2 to 12
		allKeyBindings( keyBindingPosition ) = &quot;SHIFT + CONTROL + F&quot; + FKey
		keyBindingPosition = keyBindingPosition + 1
	next FKey
	for Digit = 0 to 9
		allKeyBindings( keyBindingPosition ) = &quot;SHIFT + CONTROL + &quot; + Digit
			keyBindingPosition = keyBindingPosition + 1
	next Digit
	for Alpha = 65 to 90
		allKeyBindings( keyBindingPosition ) = &quot;SHIFT + CONTROL + &quot; + chr$( Alpha )
		keyBindingPosition = keyBindingPosition + 1
	next Alpha

	for FKey = 2 to 12
		allKeyBindings( keyBindingPosition ) = &quot;CONTROL + F&quot; + FKey
		keyBindingPosition = keyBindingPosition + 1
	next FKey
	for Digit = 0 to 9
		allKeyBindings( keyBindingPosition ) = &quot;CONTROL + &quot; + Digit
		keyBindingPosition = keyBindingPosition + 1
	next Digit
	for Alpha = 65 to 90
		allKeyBindings( keyBindingPosition ) = &quot;CONTROL + &quot; + chr$( Alpha )
		keyBindingPosition = keyBindingPosition + 1
	next Alpha

	for FKey = 2 to 12
		allKeyBindings( keyBindingPosition ) = &quot;SHIFT + F&quot; + FKey
		keyBindingPosition = keyBindingPosition + 1
	next FKey
end sub



REM ----- Text Handling Functions -----


function ExtractLabelFromXMLLine( XMLLine as string ) as string
	labelStart = instr( XMLLine, &quot;label=&quot;+chr$(34)) + 7
	labelEnd = instr( XMLLine, chr$(34)+&quot;&gt;&quot; )
	if labelEnd = 0 then
	    labelEnd = instr( XMLLine, chr$(34)+&quot;/&gt;&quot; )
	end if
	labelLength = labelEnd - labelStart

	menuLabelUnformatted = mid( XMLLine, labelStart, labelLength )
	tildePosition = instr( menuLabelUnformatted, &quot;~&quot; )
	select case tildePosition
		case 0
			menuLabel = menuLabelUnformatted
		case 1
			menuLabel = right( menuLabelUnformatted, labelLength - 1 )
		case else
			menuLabelLeft = left( menuLabelUnformatted, tildePosition - 1 )
			menuLabelRight = right( menuLabelUnformatted, labelLength - tildePosition )
			menuLabel = menuLabelLeft + menuLabelRight
	end select

	ExtractLabelFromXMLLine() = menuLabel
end function


function ExtractScriptIdFromXMLLine( XMLLine as string ) as string
	idStart = instr( XMLLine, &quot;script://&quot;) + 9
	if instr( XMLLine, &quot;&quot;+chr$(34)+&quot; menu:helpid=&quot; ) = 0 then
		idEnd = instr( XMLLIne, &quot;?location=&quot; )
	else
		idEnd = instr( XMLLine, &quot;&quot;+chr$(34)+&quot; menu:helpid=&quot; )
	end if
	idLength = idEnd - idStart
	scriptId = mid( XMLLine, idStart, idLength )

	ExtractScriptIdFromXMLLine() = scriptId
end function


function ExtractKeyCodeFromXMLLine( XMLLine as string ) as string
	keyStart = instr( XMLLine, &quot;code=&quot;+chr$(34)+&quot;KEY_&quot;) + 10
	keyCode = mid( XMLLine, keyStart, ( len( XMLLine ) - keyStart ) )
	keyEnd = instr( keyCode, chr$(34) )
	keyCode = mid( keyCode, 1, keyEnd - 1 )
	
	ExtractKeyCodeFromXMLLine() = keyCode
end function


function GetMenuWhiteSpace( MenuXMLLine as string ) as string
	whiteSpace = &quot;&quot;
	numberOfSpaces = instr( MenuXMLLine, &quot;&lt;&quot; ) - 1
	for i = 1 to numberOfSpaces
		whiteSpace = whiteSpace + &quot; &quot;
	next i
	
	GetMenuWhiteSpace() = whiteSpace
end function


function IsAllocatedKeyCombo( script as string ) as integer
	const NotAllocated = 0
	const AllocatedToOfficeFunction = 1
	const AllocatedToScript = 2
	dim Allocation as integer
	
	if instr( script, &quot; [Allocated&quot; ) &gt; 0 then
		endPosition = instr( script, &quot; [Allocated&quot; ) - 1
		script = mid( script, 1, endPosition )
	end if
	
	Allocation = NotAllocated
	count = 1
	while Allocation = NotAllocated and count &lt; scriptCount
		linePosition = scriptLinePosition( count )
		if strcomp( script, scriptNames( count ) ) = 0 then
			if instr( xmlFile( linePosition ), &quot;script://&quot; ) &gt; 0 then
				Allocation = linePosition
			else 
				Allocation = AllocatedToOfficeFunction
			end if
		end if
		count = count + 1
	wend
	msgbox( &quot; IsAllocatedKeyCombo() script: &quot; + script + &quot; Allocation: &quot; + Allocation )
	IsAllocatedKeyCombo() = Allocation
end Function


function IsAllocatedMenuItem( script as string ) as boolean
	foundMenuItem = false
	Allocated = false
	count = 0
	do 
		count = count + 1
		if strcomp( script, subMenuItems( count ) ) = 0 then
			foundMenuItem = true
		end if	
	loop while not( foundMenuItem ) and count &lt; subMenuCount

	linePosition = subMenuItemLinePosition( count )

	if not( instr( xmlFile( linePosition ), &quot;script://&quot; ) = 0 ) then
		Allocated = true
	end if

	isAllocatedMenuItem() = Allocated
end Function


function HasShiftKey( keyCombo ) as boolean
	if instr( keyCombo, &quot;SHIFT&quot; ) = 0 then
		hasShift = false
	else
		hasShift = true
	end if
		
	HasShiftKey = hasShift
end function


function HasControlKey( keyCombo ) as boolean
	if instr( keyCombo, &quot;CONTROL&quot; ) = 0 then
		hasControl = false
	else
		hasControl = true
	end if
		
	HasControlKey = hasControl
end function


function ExtractKeyFromCombo( keyString as string ) as string
	while not( instr( keyString, &quot;+&quot; ) = 0 )
		removeTo = instr( keyString, &quot;+ &quot; ) + 2
		keyString = mid( keyString, removeTo, ( len( keyString ) - removeTo ) + 1 )
	wend

	ExtractKeyFromCombo() = keyString
end function



REM ------ Event Handling Functions (Listeners) ------


sub KeyListListener()
	keyList = bindingDialog.getControl( &quot;KeyList&quot; )
	selectedKeyCombo = keyList.getSelectedItem()
	
	if isAllocatedKeyCombo( selectedKeyCombo ) &gt; 1 then
		bindingDialog.Model.Delete.enabled = true
		bindingDialog.Model.NewButton.enabled = false
	else
		if isAllocatedKeyCombo( selectedKeyCombo ) = 1 then
			bindingDialog.Model.Delete.enabled = false
			bindingDialog.Model.NewButton.enabled = false
		else
			bindingDialog.Model.Delete.enabled = false
			bindingDialog.Model.NewButton.enabled = true
		end if
	end if
end sub


sub SubMenuListListener()
	subMenuList = bindingDialog.getControl( &quot;SubMenuList&quot; )
	selectedMenuItem = subMenuList.getSelectedItem()
	if IsAllocatedMenuItem( selectedMenuItem ) then
		bindingDialog.Model.Delete.enabled = true
	else
		bindingDialog.Model.Delete.enabled = false
	end if
end sub


&apos;Populates the SubMenuList with the appropriate menu items from the Top-level menu selected from the combo box
sub MenuComboListener()
	combo = bindingDialog.getControl( &quot;MenuCombo&quot; ) 
	newToplevelMenu = combo.text
	counter = 0
	do
		counter = counter + 1
	loop while not( newToplevelMenu = menuItems( counter ) )
	
	PopulateSubMenuList( counter )
end sub


&apos;Populates the KeyList with the appropriate key combos from the Top-level key group selected from the combo box
sub KeyComboListener()
	combo = bindingDialog.getControl( &quot;KeyCombo&quot; ) 
	itemSelected = combo.text

	select case itemSelected
	case &quot;SHIFT + CONTROL + F keys&quot;
		PopulateKeyBindingList( 1, 11 )
	case &quot;SHIFT + CONTROL + digits&quot;
		PopulateKeyBindingList( 12, 21 )
	case &quot;SHIFT + CONTROL + letters&quot;
		PopulateKeyBindingList( 22, 47 )
	case &quot;CONTROL + F keys&quot;
		PopulateKeyBindingList( 48, 58 )
	case &quot;CONTROL + digits&quot;
		PopulateKeyBindingList( 59, 68 )
	case &quot;CONTROL + letters&quot;
		PopulateKeyBindingList( 69, 94 )
	case &quot;SHIFT + F keys&quot;
		PopulateKeyBindingList( 95, 105 )
	case else
		msgbox &quot;Error&quot;
	end select
end sub


sub MenuLabelBoxListener()
	if bindingDialog.Model.MenuLabelBox.text = &quot;&quot; then
		bindingDialog.Model.NewButton.enabled = false
	else
		bindingDialog.Model.NewButton.enabled = true
	end if
end sub



REM ------ Event Handling Functions (Buttons) ------


sub MenuOKButton()
	msgbox (&quot;Office must be restarted before your changes will take effect.&quot;+ chr$(10)+&quot;Also close the Office QuickStarter (Windows and some Linux)&quot;, 48, &quot;Assign Script (Java) To Menu&quot; )
	WriteXMLFromArray()
	bindingDialog.endExecute()
end sub


sub MenuCancelButton()
	bindingDialog.endExecute()
end sub


sub MenuHelpButton()
	helpDialog = LoadDialog( &quot;ScriptBindingLibrary&quot;, &quot;HelpBinding&quot; )
	helpDialog.execute()
end sub


sub MenuDeleteButton()
	subMenuList = bindingDialog.getControl( &quot;SubMenuList&quot; )
	linePos = subMenuItemLinePosition( subMenuList.getSelectedItemPos() + 1 )
	
	RemoveBinding( linePos )

	REM Update the top-level menu&apos;s line positions	
	combo = bindingDialog.getControl( &quot;MenuCombo&quot; ) 
	newToplevelMenu = combo.text
	counter = 0
	do
		counter = counter + 1
	loop while not( newToplevelMenu = menuItems( counter ) )
	UpdateTopLevelMenus( counter + 1, false )	
	
	MenuComboListener()
end sub


sub MenuNewButton()
	menuScriptList = bindingDialog.getControl( &quot;ScriptList&quot; )
	script = menuScriptList.getSelectedItem()

	newMenuLabel = bindingDialog.Model.MenuLabelBox.text

	subMenuList = bindingDialog.getControl( &quot;SubMenuList&quot; )

	REM Update the top-level menu&apos;s line positions
	combo = bindingDialog.getControl( &quot;MenuCombo&quot; ) 
	newToplevelMenu = combo.text
	counter = 0
	do
		counter = counter + 1
	loop while not( newToplevelMenu = menuItems( counter ) )
	UpdateTopLevelMenus( counter + 1, true )

	REM New line position is one ahead of the selected sub menu item		
	linePos = subMenuItemLinePosition( subMenuList.getSelectedItemPos() + 1 ) + 1
	
	AddNewMenuBinding( script, newMenuLabel, linePos )

	MenuComboListener()
end sub


sub KeyOKButton()	
	msgbox (&quot;Office must be restarted before your changes will take effect.&quot;+ chr$(10)+&quot;Also close the Office QuickStarter (Windows and some Linux)&quot;, 48, &quot;Assign Script (Java) To Key&quot; )
	WriteXMLFromArray()
	bindingDialog.endExecute()
end sub


sub KeyCancelButton()
	bindingDialog.endExecute()
end sub


sub KeyHelpButton()
	helpDialog = LoadDialog( &quot;ScriptBindingLibrary&quot;, &quot;HelpBinding&quot; )
	helpDialog.execute()
end sub


sub KeyNewButton()
	menuScriptList = bindingDialog.getControl( &quot;ScriptList&quot; )
	script = menuScriptList.getSelectedItem()
	
	keyList = bindingDialog.getControl( &quot;KeyList&quot; )
	keyCombo = keyList.getSelectedItem()
	
	AddNewKeyBinding( script, HasShiftKey( keyCombo ), HasControlKey( keyCombo ), ExtractKeyFromCombo( keyCombo ) ) 

	KeyComboListener()
end sub


sub KeyDeleteButton()
	keyList = bindingDialog.getControl( &quot;KeyList&quot; )
	REM Check that combo is a script
	keyCombo = keyList.getSelectedItem()
	
	if instr( keyCombo, &quot; [Allocated&quot; ) &gt; 0 then
		endPosition = instr( keyCombo, &quot; [Allocated&quot; ) - 1
		keyCombo = mid( keyCombo, 1, endPosition )
	end if
	
	for n = 1 to scriptCount
		if strcomp( keyCombo, scriptNames( n ) ) = 0 then
			keyComboPosition = n
			exit for
		end if
	next n
	
	linePosition = scriptLinePosition( keyComboPosition )

	REM remove scriptNames and scriptLinePosition entries
	RemoveScriptNameAndPosition( keyComboPosition )

	RemoveBinding( linePosition )
	
	script = ExtractScriptIdFromXMLLine( xmlFile( linePosition ) )
	KeyComboListener()
end sub


sub HelpOKButton()
	helpDialog.endExecute()
end sub</script:module>