1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3<script:module xmlns:script="http://openoffice.org/2000/script" script:name="ScriptBinding" script:language="StarBasic">REM  *****  BASIC  *****
4
5REM ----- Global Variables -----
6
7&apos;bindingDialog can refer to either KeyBinding or MenuBinding dialog
8private languages() as String
9private extensions() as Object
10private locations() as String
11private filesysScripts() as String
12private filesysCount as integer
13private bindingDialog as object
14private helpDialog as object
15&apos;Couldn&apos;t get redim to work, so scriptDisplayList is and array of arrays
16&apos;where the one and only array in scriptDisplayList is an array
17&apos;of com.sun.star.beans.PropertyValue, where Name = [logicalName][FunctionName]
18&apos;and value is ScriptStorage object
19private scriptDisplayList(0)
20private testArray() as String
21&apos;Array to store lines from the xml file
22private xmlFile() as string
23&apos;Name of the xml file [writer/calc][menubar/keybindings].xml
24private xmlFileName as string
25&apos;Number of lines in the xml file
26private numberOfLines as integer
27
28&apos;Parallel arrays to store all top-level menu names and line positions
29private menuItems() as string
30private menuItemLinePosition() as integer
31&apos;Counter for the number of top-level menus
32private menuCount as integer
33
34&apos;Parallel arrays to store all sub-menu names and line positions for a particular top-level menu
35private subMenuItems() as string
36private subMenuItemLinePosition() as integer
37&apos;Counter for the number of sub-menus
38private subMenuCount as integer
39
40&apos;Parallel arrays to store all script names and line positions
41private scriptNames() as string
42private scriptLinePosition() as integer
43&apos;Counter for the number of scripts
44private scriptCount as integer
45
46&apos;Array to store all combinations of key bindings
47private allKeyBindings() as string
48
49&apos;Array of Arrays
50&apos;KeyBindArrayOfArrays(0)  contains array of &quot;SHIFT + CONTROL + F Keys&quot; data
51&apos;Similarly
52&apos;KeyBindArrayOfArrays(1) contains SHIFT + CONTROL + digits
53&apos;KeyBindArrayOfArrays(2) contains SHIFT + CONTROL + letters
54&apos;KeyBindArrayOfArrays(3) contains CONTROL + F keys
55&apos;KeyBindArrayOfArrays(4) contains CONTROL + digits
56&apos;KeyBindArrayOfArrays(5) contains CONTROL + letters
57&apos;KeyBindArrayOfArrays(6) contains SHIFT + F keys
58private KeyBindArrayOfArrays(6)
59
60&apos;Each PropertyValue represents a key, Name member contains the script (if a binding exists)
61&apos; the Value contains and integer
62&apos;       0  means no script bound
63&apos;       1  script is bound to an office function
64&apos;       &gt;1 line number of entry in xmlfile array
65private keyAllocationMap(6,25) as new com.sun.star.beans.PropertyValue
66&apos;array to store key group descriptions
67private AllKeyGroupsArray(6) as String
68
69
70&apos;Array of props to store all event bindings for the Applications
71private	allEventTypesApp( 14 ) as new com.sun.star.beans.PropertyValue
72&apos;Array of props to store all event bindings for the Document
73private	allEventTypesDoc( 14 ) as new com.sun.star.beans.PropertyValue
74&apos;Array of props to store all event types (Name) and textual description (Value)
75private allEventTypes( 14 ) as new com.sun.star.beans.PropertyValue
76
77
78private dialogName as String
79REM ------ Storage Refresh Function ------
80
81
82sub RefreshUserScripts()
83&apos; TDB - change Menu bindings to allow user to refresh all, user, share or document script
84   RefreshAppScripts( &quot;USER&quot; )
85end sub
86
87sub RefreshAllScripts()
88   RefreshAppScripts( &quot;USER&quot; )
89   RefreshAppScripts( &quot;SHARE&quot; )
90   RefreshDocumentScripts
91end sub
92
93sub RefreshAppScripts( appName as String )
94   On Error Goto ErrorHandler
95   smgr = getProcessServiceManager()
96   context = smgr.getPropertyValue( &quot;DefaultContext&quot; )
97   scriptstoragemgr = context.getValueByName( &quot;/singletons/drafts.com.sun.star.script.framework.storage.theScriptStorageManager&quot; )
98
99   scriptstoragemgr.refreshScriptStorage( appName )
100
101   Exit sub
102
103   ErrorHandler:
104   reset
105   MsgBox (&quot;Error: Unable to refresh Java (scripts)&quot; + chr$(10) + chr$(10)+ &quot;Detail: &quot; &amp; error$ + chr$(10) + chr$(10)+ &quot;Action: Please restart Office&quot;,0,&quot;Error&quot; )
106
107end sub
108
109sub RefreshDocumentScripts()
110   On Error Goto ErrorHandler
111   smgr = getProcessServiceManager()
112   context = smgr.getPropertyValue( &quot;DefaultContext&quot; )
113   scriptstoragemgr = context.getValueByName( &quot;/singletons/drafts.com.sun.star.script.framework.storage.theScriptStorageManager&quot; )
114
115   oDocURL = ThisComponent.GetCurrentController.getModel.getURL
116
117   On Error Goto ErrorHandlerDoc
118   scriptstoragemgr.refreshScriptStorage( oDocURL )
119
120   Exit sub
121
122   ErrorHandlerDoc:
123   reset
124   &apos; Ignore document script errors as it will happen when refreshing an unsaved doc
125   Exit sub
126
127   ErrorHandler:
128   reset
129   MsgBox (&quot;Error: Unable to refresh Java (scripts)&quot; + chr$(10) + chr$(10)+ &quot;Detail: &quot; &amp; error$ + chr$(10) + chr$(10)+ &quot;Action: Please restart Office&quot;,0,&quot;Error&quot; )
130
131end sub
132
133
134REM ----- Launch Functions -----
135
136Sub createAndPopulateKeyArrays()
137	&apos;Create SHIFT + CONTROL + F keys array
138	&apos;Dim keyGroupProp as new com.sun.star.beans.PropertyValue
139
140	Dim SCFKey( 11 )
141	for FKey = 1 to 12
142		SCFKey( FKey - 1 ) = &quot;SHIFT + CONTROL + F&quot; + FKey
143	next FKey
144
145	KeyBindArrayOfArrays(0) = SCFKey()
146
147	&apos;Create SHIFT + CONTROL + digits
148	Dim SCDKey( 9 )
149	for Digit = 0 to 9
150		SCDKey( Digit ) = &quot;SHIFT + CONTROL + &quot; + Digit
151	next Digit
152	KeyBindArrayOfArrays(1) = SCDKey()
153
154	&apos;Create SHIFT + CONTROL + letters
155
156	Dim SCLKey( 25 )
157	for Alpha = 65 to 90
158		SCLKey( Alpha - 65 ) = &quot;SHIFT + CONTROL + &quot; + chr$( Alpha )
159	next Alpha
160	KeyBindArrayOfArrays(2) = SCLKey()
161
162	&apos;Create CONTROL + F keys
163	Dim CFKey( 11 )
164	for FKey = 1 to 12
165		CFKey( Fkey - 1 ) = &quot;CONTROL + F&quot; + FKey
166	next FKey
167	KeyBindArrayOfArrays(3) = CFKey()
168
169	&apos;Create CONTROL + digits
170	Dim CDKey( 9 )
171	for Digit = 0 to 9
172		CDKey( Digit ) = &quot;CONTROL + &quot; + Digit
173	next Digit
174	KeyBindArrayOfArrays(4) = CDKey()
175
176	&apos;Create CONTROL + letters
177	Dim CLKey( 25 )
178	for Alpha = 65 to 90
179		CLKey( Alpha - 65 ) = &quot;CONTROL + &quot; + chr$( Alpha )
180	next Alpha
181	KeyBindArrayOfArrays(5) = CLKey()
182
183	&apos;Create SHIFT + F Keys
184	Dim SFKey( 11 )
185	for FKey = 1 to 12
186		SFKey( Fkey - 1 ) = &quot;SHIFT + F&quot; + FKey
187	next FKey
188	KeyBindArrayOfArrays(6) = SFKey()
189
190End Sub
191
192Sub updateMapWithDisabledKeys()
193	&apos;disable CONTROL + F1 &amp;
194	keyAllocationMap( 3, 0 ).Value = 1
195	keyAllocationMap( 3, 0 ).Name = &quot;&quot;
196	&apos;disable CONTROL + F4 &amp;
197	keyAllocationMap( 3, 3 ).Value = 1
198	keyAllocationMap( 3, 3 ).Name = &quot;&quot;
199	&apos;disable CONTROL + F6
200	keyAllocationMap( 3, 5 ).Value = 1
201	keyAllocationMap( 3, 5 ).Name = &quot;&quot;
202
203
204	&apos;disable SHIFT + F1 &amp;
205	keyAllocationMap( 6, 0 ).Value = 1
206	keyAllocationMap( 6, 0 ).Name = &quot;&quot;
207	&apos;disable SHIFT + F2 &amp;
208	keyAllocationMap( 6, 1 ).Value = 1
209	keyAllocationMap( 6, 1 ).Name = &quot;&quot;
210	&apos;disable SHIFT + F6 &amp;
211	keyAllocationMap( 6, 5 ).Value = 1
212	keyAllocationMap( 6, 5 ).Name = &quot;&quot;
213
214End Sub
215
216Sub initialiseFileExtensions()
217	ReDim extensions(ubound(languages())+1) as Object
218	oConfigProvider = CreateUnoService( &quot;com.sun.star.configuration.ConfigurationProvider&quot; )
219    Dim configArgs(1) as new com.sun.star.beans.PropertyValue
220    configargs(0).Name = &quot;nodepath&quot;
221    configArgs(0).Value = &quot;org.openoffice.Office.Scripting/ScriptRuntimes&quot;
222    configargs(1).Name = &quot;lazywrite&quot;
223    configArgs(1).Value = false
224    oConfigAccess = oConfigProvider.createInstanceWithArguments(&quot;com.sun.star.configuration.ConfigurationAccess&quot;, configArgs())
225    for index = 0 to ubound(languages())
226    		if(languages(index) &lt;&gt; &quot;Java&quot;) then
227            	xPropSet = oConfigAccess.getByName(languages(index))
228            	extns() = xPropSet.getPropertyValue(&quot;SupportedFileExtensions&quot;)
229            	extensions(index) = extns()
230            endif
231    next index
232end sub
233
234Sub ExecuteEditDebug()
235
236	locations = Array ( &quot;User&quot;, &quot;Share&quot;, &quot;Document&quot;, &quot;Filesystem&quot; )
237	languages = Array ( &quot;BeanShell&quot;, &quot;JavaScript&quot; )
238	dialogName = &quot;EditDebug&quot;
239	initialiseFileExtensions()
240	bindingDialog = LoadDialog( &quot;ScriptBindingLibrary&quot;, &quot;EditDebug&quot; )
241
242	PopulateLanguageCombo()
243	PopulateLocationCombo()
244	PopulateScriptList( languages(0), locations(0) )
245
246	bindingDialog.execute()
247End Sub
248
249Sub ExecuteKeyBinding()
250	dialogName = &quot;Key&quot;
251    createAndPopulateKeyArrays()
252    updateMapWithDisabledKeys()
253	xmlFileName = GetDocumentType( &quot;Key&quot; )
254
255	if not (ReadXMLToArray( &quot;Key&quot; )) then
256		Exit Sub
257	endif
258
259	bindingDialog = LoadDialog( &quot;ScriptBindingLibrary&quot;, &quot;KeyBinding&quot; )
260	PopulateKeyBindingList(0)
261	initialiseNavigationComboArrays()
262    PopulateLanguageCombo()
263    PopulateLocationCombo()
264	PopulateScriptList( languages(0), locations(0) )
265	PopulateTopLevelKeyBindingList()
266	bindingDialog.execute()
267end Sub
268
269
270Sub initialiseNavigationComboArrays()
271    locations = Array ( &quot;User&quot;, &quot;Share&quot;, &quot;Document&quot;, &quot;Filesystem&quot; )
272    ReDim languages(0) as String
273    ReDim extensions(0) as Object
274	languages(0) = &quot;Java&quot;
275	REM extensions(0) = &quot;&quot;
276
277    &apos; Setup languages array for all supported languages
278    oServiceManager = GetProcessServiceManager()
279    svrArray = oServiceManager.getAvailableServiceNames
280
281    langCount = 1
282    for index = 0 to ubound(svrArray)
283        iPos = inStr(svrArray(index), &quot;ScriptProviderFor&quot;)
284
285        if (iPos &gt; 0) then
286            lang = Mid(svrArray(index), iPos + Len(&quot;ScriptProviderFor&quot;)
287
288            if not (lang = &quot;Java&quot;) then
289                &apos;Add to language vector
290                ReDim Preserve languages(langCount) as String
291                languages(langCount) = lang
292                langCount = langCount + 1
293            endif
294        endif
295    next index
296    initialiseFileExtensions()
297End Sub
298
299
300Sub ExecuteEventBinding
301	dialogName = &quot;Event&quot;
302	createAllEventTypes()
303	createAllEventBindings()
304
305	&apos;Populate application event bindings array (from config xml file)
306	if not (ReadXMLToArray( &quot;Event&quot; )) then
307		Exit Sub
308	endif
309	&apos;Populate document event bindings array (using Office API calls)
310	ReadEventsFromDoc()
311
312	bindingDialog = LoadDialog( &quot;ScriptBindingLibrary&quot;, &quot;EventsBinding&quot; )
313	initialiseNavigationComboArrays()
314    PopulateLanguageCombo()
315    PopulateLocationCombo()
316	PopulateScriptList( languages(0), locations(0) )
317	populateEventList( 0 )
318	EventListListener()
319	bindingDialog.execute()
320End Sub
321
322Sub ExecuteMenuBinding()
323	dialogName = &quot;Menu&quot;
324	xmlFileName = GetDocumentType( &quot;Menu&quot; )
325	if not (ReadXMLToArray( &quot;Menu&quot; )) then
326		Exit Sub
327	endif
328
329	bindingDialog = LoadDialog( &quot;ScriptBindingLibrary&quot;, &quot;MenuBinding&quot; )
330	initialiseNavigationComboArrays()
331    PopulateLanguageCombo()
332    PopulateLocationCombo()
333	PopulateScriptList( languages(0), locations(0) )
334	PopulateMenuCombo()
335	PopulateSubMenuList( 1 )
336
337	subMenuList = bindingDialog.getControl(&quot;SubMenuList&quot;)
338
339	subMenuList.selectItemPos( 0, true )
340
341	bindingDialog.execute()
342end Sub
343
344
345REM ----- Initialising functions -----
346
347
348function LoadDialog( libName as string, dialogName as string ) as object
349	dim library as object
350	dim libDialog as object
351	dim runtimeDialog as object
352	libContainer = DialogLibraries
353	libContainer.LoadLibrary( libName )
354	library = libContainer.getByName( libname )
355	libDialog = library.getByName( dialogName )
356	runtimeDialog = CreateUnoDialog( libDialog )
357	LoadDialog() = runtimeDialog
358
359end function
360
361
362function GetDocumentType( bindingType as string ) as string
363	document = StarDesktop.ActiveFrame.Controller.Model
364	Dim errornumber As Integer
365	errornumber = 111
366	Error errornumber
367	if document.SupportsService(&quot;com.sun.star.sheet.SpreadsheetDocument&quot;) then
368		if bindingType = &quot;Key&quot; then
369			GetDocumentType() = &quot;calckeybinding.xml&quot;
370		else
371			if bindingType = &quot;Menu&quot; then
372				GetDocumentType() = &quot;calcmenubar.xml&quot;
373			end if
374		end if
375	elseif document.SupportsService(&quot;com.sun.star.text.TextDocument&quot;) then
376		if bindingType = &quot;Key&quot; then
377			GetDocumentType() = &quot;writerkeybinding.xml&quot;
378		else
379			if bindingType = &quot;Menu&quot; then
380				GetDocumentType() = &quot;writermenubar.xml&quot;
381			end if
382		end if
383	elseif document.SupportsService(&quot;com.sun.star.presentation.PresentationDocument&quot;) then
384		if bindingType = &quot;Key&quot; then
385			GetDocumentType() = &quot;impresskeybinding.xml&quot;
386		else
387			if bindingType = &quot;Menu&quot; then
388				GetDocumentType() = &quot;impressmenubar.xml&quot;
389			end if
390		end if
391	elseif document.SupportsService(&quot;com.sun.star.presentation.PresentationDocument&quot;) then
392		if bindingType = &quot;Key&quot; then
393			GetDocumentType() = &quot;impresskeybinding.xml&quot;
394		else
395			if bindingType = &quot;Menu&quot; then
396				GetDocumentType() = &quot;impressmenubar.xml&quot;
397			end if
398		end if
399	elseif document.SupportsService(&quot;com.sun.star.drawing.DrawingDocument&quot;) then
400		if bindingType = &quot;Key&quot; then
401			GetDocumentType() = &quot;drawkeybinding.xml&quot;
402		else
403			if bindingType = &quot;Menu&quot; then
404				GetDocumentType() = &quot;drawmenubar.xml&quot;
405			end if
406		end if
407	else
408		MsgBox (&quot;Error: Couldn&apos;t determine configuration file type&quot; + chr$(10) + chr$(10) + &quot;Action: Please reinstall Scripting Framework&quot;,0,&quot;Error&quot; )
409	end if
410end function
411
412function lastIndexOf( targetStr as String, substr as String ) as Integer
413	copyStr = targetStr
414	while instr(copyStr, substr) &gt; 0
415		pos = instr(copyStr, substr)
416		tpos = tpos + pos
417		copyStr = mid(copyStr, pos+1, len(copyStr)-pos )
418	wend
419	lastIndexOf() = tpos
420end function
421
422function getScriptURI( selectedScript as String ) as String
423	combo = bindingDialog.getControl( &quot;LocationCombo&quot; )
424	location = combo.text
425	if ( location = &quot;User&quot; ) then
426		location = &quot;user&quot;
427	elseif ( location = &quot;Share&quot; ) then
428		location = &quot;share&quot;
429	elseif ( location = &quot;Filesystem&quot; ) then
430		location = &quot;filesystem&quot;
431	else
432		location = &quot;document&quot;
433	end if
434
435
436
437	if ( location = &quot;filesystem&quot; ) then
438		REM need to build URI here - dcf
439		combo = bindingDialog.getControl( &quot;LanguageCombo&quot; )
440		language = combo.text
441		url = selectedscript
442		pos = lastIndexOf( url, &quot;/&quot; )
443		locationPath =  mid( url, 1, pos)
444        url = mid( url, pos+1, len( url ) - pos )
445		functionName = url
446		pos = lastIndexOf( url, &quot;.&quot; )
447		logicalName = mid( url, 1, pos - 1 )
448		getScriptURI() = &quot;script://&quot; + logicalName + &quot;?language=&quot; _
449			+ language + &quot;&amp;amp;function=&quot; + functionName _
450			+ &quot;&amp;amp;location=filesystem:&quot; + locationPath
451	else
452		Dim scriptInfo as Object
453		scripts() = scriptDisplayList(0)
454		for n = LBOUND( scripts() ) to UBOUND( scripts() )
455
456			if ( scripts( n ).Name = selectedScript ) then
457				scriptInfo = scripts( n ).Value
458				exit for
459			end if
460		next n
461		getScriptURI() = &quot;script://&quot; + scriptInfo.getLogicalName + &quot;?language=&quot; _
462			+ scriptInfo.getLanguage() + &quot;&amp;amp;function=&quot; + _
463			scriptInfo.getFunctionName() + &quot;&amp;amp;location=&quot; + location
464	end if
465
466end function
467
468function GetOfficePath() as string
469	REM Error check and prompt user to manually input Office Path
470	settings =  CreateUnoService( &quot;com.sun.star.frame.Settings&quot; )
471	path = settings.getByName( &quot;PathSettings&quot; )
472	unformattedOfficePath = path.getPropertyValue( &quot;UserPath&quot; )
473
474	dim officePath as string
475	const removeFromEnd = &quot;/user&quot;
476	const removeFromEndWindows = &quot;\user&quot;
477
478	REM If Solaris or Linux
479	if not ( instr( unformattedOfficePath, removeFromEnd ) = 0 ) then
480		endPosition = instr( unformattedOfficePath, removeFromEnd )
481		officePath = mid( unformattedOfficePath, 1, endPosition )
482	REM If Windows
483	else if not ( instr( unformattedOfficePath, removeFromEndWindows ) = 0 ) then
484			endPosition = instr( unformattedOfficePath, removeFromEndWindows )
485			officePath = mid( unformattedOfficePath, 1, endPosition )
486			while instr( officePath, &quot;\&quot; ) &gt; 0
487				backSlash = instr( officePath, &quot;\&quot; )
488				startPath = mid( officePath, 1, backSlash - 1 )
489				endPath = mid( officePath, backslash + 1, len( officePath ) - backSlash )
490				officePath = startPath + &quot;/&quot; + endPath
491			wend
492		else
493			MsgBox (&quot;Error: Office path not found&quot; + chr$(10) + chr$(10) + &quot;Action: Please reinstall Scripting Framework&quot;,0,&quot;Error&quot; )
494			REM Prompt user
495		end if
496	end if
497
498	GetOfficePath() = officePath
499end function
500
501
502
503REM ----- File I/O functions -----
504
505
506function ReadXMLToArray( bindingType as string ) as boolean
507	On Error Goto ErrorHandler
508	if ( bindingType = &quot;Event&quot; ) then
509		xmlfilename = &quot;eventbindings.xml&quot;
510	endif
511
512	simplefileaccess = CreateUnoService( &quot;com.sun.star.ucb.SimpleFileAccess&quot; )
513	filestream = simplefileaccess.openFileRead( &quot;file://&quot; + GetOfficePath() + &quot;user/config/soffice.cfg/&quot; + xmlFileName )
514
515    textin = CreateUnoService( &quot;com.sun.star.io.TextInputStream&quot; )
516    textin.setInputStream( filestream )
517
518    redim xmlFile( 400 ) as String
519    redim menuItems( 30 ) as String
520    redim menuItemLinePosition( 30 ) as Integer
521    redim scriptNames( 120 ) as string
522    redim scriptLinePosition( 120) as integer
523
524    lineCount = 1
525    menuCount = 1
526    scriptCount = 1
527
528    do while not textin.isEOF()
529    	xmlline = textin.readLine()
530    	xmlFile( lineCount ) = xmlline
531
532 		const menuItemWhiteSpace = 2
533 		const menuXMLTag = &quot;&lt;menu:menu&quot;
534
535		if bindingType = &quot;Menu&quot; then
536			evaluateForMenu( xmlline, lineCount )
537		elseif bindingType = &quot;Key&quot; then
538			processKeyXMLLine( lineCount, xmlline )
539		elseif bindingType = &quot;Event&quot; then
540			evaluateForEvent( xmlline, lineCount )
541		else
542			MsgBox (&quot;Error: Couldn&apos;t determine file type&quot; + chr$(10) + chr$(10) + &quot;Action: Please reinstall Scripting Framework&quot;,0,&quot;Error&quot; )
543		end if
544        lineCount = lineCount + 1
545    loop
546
547	&apos;Set global variable numberOfLines (lineCount is one too many at end of the loop)
548	numberOfLines = lineCount - 1
549	&apos;Set global variable menuCount (it is one too many at end of the loop)
550	menuCount = menuCount - 1
551
552    filestream.closeInput()
553    ReadXMLToArray( ) = true
554	Exit function
555
556	ErrorHandler:
557	reset
558	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; )
559    ReadXMLToArray( ) = false
560end function
561
562
563
564sub evaluateForMenu( xmlline as string, lineCount as integer )
565	const menuItemWhiteSpace = 2
566 	const menuXMLTag = &quot;&lt;menu:menu&quot;
567	&apos;If the xml line is a top-level menu
568	if instr( xmlline, menuXMLTag ) = menuItemWhiteSpace then
569		menuLabel = ExtractLabelFromXMLLine( xmlline )
570		menuItems( menuCount ) = menuLabel
571		menuItemLinePosition( menuCount ) = lineCount
572		menuCount = menuCount + 1
573	end if
574end sub
575
576sub evaluateForEvent( xmlline as string, lineCount as integer )
577	dim eventName as String
578	&apos;if the xml line identifies a script or SB macro
579	dim scriptName as string
580	dim lineNumber as integer
581	if instr( xmlline, &quot;event:language=&quot; + chr$(34) + &quot;Script&quot; ) &gt; 0 then
582		eventName = ExtractEventNameFromXMLLine( xmlline )
583		scriptName = ExtractEventScriptFromXMLLine( xmlline )
584		lineNumber = lineCount
585	elseif instr( xmlline, &quot;event:language=&quot; + chr$(34) + &quot;StarBasic&quot; ) &gt; 0 then
586		eventName = ExtractEventNameFromXMLLine( xmlline )
587		scriptName = &quot;Allocated to Office function&quot;
588		lineNumber = 1
589	end if
590
591	&apos;Need to sequence to find the corresponding index for the event type
592	for n = 0 to ubound( allEventTypesApp() )
593		if ( eventName = allEventTypes( n ).Name ) then
594			allEventTypesApp( n ).Name = scriptName
595			allEventTypesApp( n ).Value = lineNumber
596		end if
597	next n
598end sub
599
600
601function isOKscriptProps( props() as Object, eventName as string ) as Boolean
602	On Error Goto ErrorHandler
603	props  = ThisComponent.getEvents().getByName( eventName )
604	test = ubound( props() )
605	isOKscriptProps() = true
606	exit function
607
608	ErrorHandler:
609	isOKscriptProps() = false
610end function
611
612sub ReadEventsFromDoc()
613	On Error Goto ErrorHandler
614
615	eventSupplier = ThisComponent
616	for n = 0 to ubound( allEventTypes() )
617		Dim scriptProps() as Object
618		if (isOKscriptProps( scriptProps(), allEventTypes( n ).Name) ) then
619			if ( ubound( scriptProps ) &gt; 0 ) then
620				if ( scriptProps(0).Value = &quot;Script&quot; ) then
621					&apos;Script binding
622					allEventTypesDoc(n).Name = scriptProps(1).Value
623					allEventTypesDoc(n).value = 2
624				elseif( scriptProps(0).Value = &quot;StarBasic&quot; ) then
625					&apos;StarBasic macro
626					allEventTypesDoc(n).Name = &quot;Allocated to Office function&quot;
627					allEventTypesDoc(n).value = 1
628				end if
629			end if
630		end if
631	next n
632
633	exit sub
634
635	&apos; eventProps is undefined if there are no event bindings in the doc
636	ErrorHandler:
637	reset
638end sub
639
640
641sub WriteEventsToDoc()
642	On Error Goto ErrorHandler
643
644	eventSupplier = ThisComponent
645	for n = 0 to ubound( allEventTypes() )
646		scriptName = allEventTypesDoc( n ).Name
647		eventName = allEventTypes( n ).Name
648		if( allEventTypesDoc( n ).Value &gt; 1 ) then &apos;script
649			&apos;add to doc
650			AddEventToDocViaAPI( scriptName, eventName )
651		elseif( allEventTypesDoc( n ).Value = 0 ) then &apos;blank (this will &quot;remove&quot; already blank entries)
652			&apos;remove from doc
653			RemoveEventFromDocViaAPI( eventName )
654		endif
655		&apos;Otherwise it is a StarBasic binding - leave alone
656	next n
657	&apos;Mark document as modified ( should happen automatically as a result of calling the API )
658	ThisComponent.CurrentController.getModel().setModified( True )
659	exit sub
660
661	ErrorHandler:
662	reset
663	msgbox( &quot;Error calling UNO API for writing event bindings to the document&quot; )
664end sub
665
666
667sub RemoveEventFromDocViaAPI( event as string )
668	dim document   as object
669	dim dispatcher as object
670	dim parser     as object
671	dim url        as new com.sun.star.util.URL
672
673	document = ThisComponent.CurrentController.Frame
674	parser   = createUnoService(&quot;com.sun.star.util.URLTransformer&quot;)
675	dim args(0) as new com.sun.star.beans.PropertyValue
676	args(0).Name = &quot;&quot;
677	args(0).Value = event
678
679	url.Complete = &quot;script://_$ScriptFrmwrkHelper.removeEvent?&quot; _
680					+ &quot;language=Java&amp;function=ScriptFrmwrkHelper.removeEvent&quot; _
681					+ &quot;&amp;location=share&quot;
682
683	parser.parseStrict(url)
684	disp = document.queryDispatch(url,&quot;&quot;,0)
685	disp.dispatch(url,args())
686end sub
687
688
689sub AddEventToDocViaAPI( scriptName as string, eventName as string )
690	dim properties( 1 ) as new com.sun.star.beans.PropertyValue
691	properties( 0 ).Name = &quot;EventType&quot;
692	properties( 0 ).Value = &quot;Script&quot;
693	properties( 1 ).Name = &quot;Script&quot;
694	properties( 1 ).Value = scriptName
695
696	eventSupplier = ThisComponent
697	nameReplace = eventSupplier.getEvents()
698	nameReplace.replaceByName( eventName, properties() )
699end sub
700
701
702&apos; returns 0 for Fkey
703&apos;         1 for digit
704&apos;         2 for letter
705
706function getKeyTypeOffset( key as String ) as integer
707	length = Len( key )
708	if ( length &gt; 1 ) then
709 		getKeyTypeOffset() = 0
710
711	elseif  ( key &gt;= &quot;0&quot; AND key &lt;= &quot;9&quot; )  then
712 		getKeyTypeOffset() = 1
713	else
714 		getKeyTypeOffset() = 2
715 	end if
716end function
717
718function getKeyGroupIndex( key as String, offset as Integer ) as Integer
719    &apos; Keys we are interested in are A - Z, F2 - F12, 0 - 9 anything else should
720    &apos; ensure -1 is returned
721	cutKey = mid( key,2 )
722
723	if ( cutKey &lt;&gt; &quot;&quot; ) then
724		acode = asc ( mid( cutKey,1,1) )
725        if ( acode &gt; 57 ) then
726			getKeyGroupIndex() = -1
727			exit function
728		end if
729	end if
730
731	select case offset
732	case 0:
733			num = cint( cutKey )
734			getKeyGroupIndex() = num - 1
735			exit function
736	case 1:
737			num = asc( key ) - 48
738			getKeyGroupIndex() = num
739			exit function
740	case 2:
741			num = asc( key ) - 65
742			getKeyGroupIndex() = num
743			exit function
744	end select
745	getKeyGroupIndex() = -1
746end function
747
748Sub processKeyXMLLine( lineCount as Integer, xmlline as String )
749
750	if instr( xmlline, &quot;&lt;accel:item&quot; ) &gt; 0 then
751		shift = false
752		control = false
753		if instr( xmlline, &quot;accel:shift=&quot;+chr$(34)+&quot;true&quot;+chr$(34) ) &gt; 0 then
754			shift = true
755		end if
756		if instr( xmlFile( lineCount ), &quot;accel:mod1=&quot;+chr$(34)+&quot;true&quot;+chr$(34) ) &gt; 0 then
757			control = true
758		end if
759		offsetIntoArrayOfArrays = -1 &apos;default unknown
760		if ( control AND shift ) then
761			offsetIntoArrayOfArrays = 0
762		elseif ( control ) then
763			offsetIntoArrayOfArrays = 3
764		elseif ( shift ) then
765			offsetIntoArrayOfArrays = 6
766		endif
767		&apos; Calculate which of the 7 key group arrays we need to point to
768		key = ExtractKeyCodeFromXMLLine( xmlline )
769		keyTypeOffset = getKeyTypeOffset( key  )
770		offsetIntoArrayOfArrays = offsetIntoArrayOfArrays + keyTypeOffset
771
772		&apos; Calculate from the key the offset into key group array we need to point to
773		KeyGroupIndex = getKeyGroupIndex( key, keyTypeOffset )
774		if ( offsetIntoArrayOfArrays = -1 ) then
775			&apos;Unknown key group, no processing necessary
776			Exit Sub
777		end if
778		if ( KeyGroupIndex &gt; -1 ) then
779
780			&apos; Determine if a script framework binding is present or not
781			if  instr( xmlline, &quot;script://&quot; ) &gt; 0 then
782				&apos; its one of ours so update its details
783				scriptName = ExtractScriptIdFromXMLLine( xmlline )
784
785				keyAllocationMap( offsetIntoArrayOfArrays, KeyGroupIndex ).Value = lineCount
786				keyAllocationMap( offsetIntoArrayOfArrays, KeyGroupIndex ).Name = scriptName
787			else
788				keyAllocationMap( offsetIntoArrayOfArrays, KeyGroupIndex ).Value = 1
789				keyAllocationMap( offsetIntoArrayOfArrays, KeyGroupIndex ).Name = &quot;&quot;
790
791			end if
792		end if
793	end if
794End Sub
795
796Sub WriteXMLFromArray()
797	On Error Goto ErrorHandler
798	cfgFile =  GetOfficePath() + &quot;user/config/soffice.cfg/&quot; + xmlFileName
799	updateCfgFile( cfgFile )
800	&apos;if ( false ) then&apos; config stuff not in build yet
801	if ( true ) then
802		updateConfig( xmlFileName )
803	else
804		msgbox (&quot;Office must be restarted before your changes will take effect.&quot;+ chr$(10)+&quot;Also close the Office QuickStarter (Windows and Linux)&quot;, 48, &quot;Assign Script (Java) To Menu&quot; )
805	endif
806	Exit Sub
807
808	ErrorHandler:
809	reset
810	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; )
811end Sub
812
813
814Sub UpdateCfgFile ( fileName as String )
815	dim ScriptProvider as Object
816	dim Script as Object
817	dim args(1)
818	dim displayDialogFlag as boolean
819	displayDialogFlag = false
820	args(0) = ThisComponent
821	args(1) = displayDialogFlag
822
823	ScriptProvider = createUnoService(&quot;drafts.com.sun.star.script.framework.provider.MasterScriptProvider&quot;)
824	ScriptProvider.initialize( args() )
825	Script = ScriptProvider.getScript(&quot;script://_$ScriptFrmwrkHelper.updateCfgFile?&quot; _
826		+ &quot;language=Java&amp;function=ScriptFrmwrkHelper.updateCfgFile&amp;location=share&quot;)
827	Dim inArgs(2)
828	Dim outArgs()
829	Dim outIndex()
830	dim localNumLines as integer
831
832	inArgs(0) = xmlFile()
833	inArgs(1) = fileName
834	inArgs(2) = numberOfLines
835	Script.invoke( inArgs(), outIndex(), outArgs() )
836End Sub
837
838sub UpdateConfig( a$ )
839       dim document   as object
840       dim dispatcher as object
841       dim parser     as object
842       dim disp     as object
843       dim url        as new com.sun.star.util.URL
844       document = ThisComponent.CurrentController.Frame
845       parser   = createUnoService(&quot;com.sun.star.util.URLTransformer&quot;)
846       dim args1(0) as new com.sun.star.beans.PropertyValue
847       args1(0).Name = &quot;StreamName&quot;
848       args1(0).Value = a$
849       url.Complete = &quot;.uno:UpdateConfiguration&quot;
850       parser.parseStrict(url)
851       disp = document.queryDispatch(url,&quot;&quot;,0)
852       disp.dispatch(url,args1())
853
854End Sub
855
856
857sub AddNewEventBinding( scriptName as string, eventPosition as integer, isApp as boolean )
858	event = allEventTypes( eventPosition ).Name
859	&apos;dim scriptProp as new com.sun.star.beans.PropertyValue
860	if isApp then
861		&apos;scriptProp.Name = scriptName
862		&apos;scriptProp.Value = numberOfLines
863		allEventTypesApp( eventPosition ).Name = scriptName
864		allEventTypesApp( eventPosition ).Value = numberOfLines
865
866		newline = &quot; &lt;event:event event:name=&quot; + chr$(34) + event + chr$(34)
867		newline = newline + &quot; event:language=&quot; + chr$(34) + &quot;Script&quot; + chr$(34) + &quot; xlink:href=&quot; + chr$(34)
868		newline = newline + scriptName + chr$(34) + &quot; xlink:type=&quot; + chr$(34) + &quot;simple&quot; + chr$(34) + &quot;/&gt;&quot;
869		xmlFile( numberOfLines ) = newline
870		xmlFile( numberOfLines + 1 ) = &quot;&lt;/event:events&gt;&quot;
871		numberOfLines = numberOfLines + 1
872	else
873		&apos;scriptProp.Name = scriptName
874		&apos;scriptProp.Value = 2
875		allEventTypesDoc( eventPosition ).Name = scriptName
876		allEventTypesDoc( eventPosition ).Value = 2
877	end if
878end sub
879
880REM ----- Array update functions -----
881
882
883sub AddNewMenuBinding( newScript as string, newMenuLabel as string, newLinePosition as integer )
884	dim newXmlFile( 400 ) as string
885	dim newLineInserted as boolean
886	dim lineCounter as integer
887	lineCounter = 1
888
889	do while lineCounter &lt;= numberOfLines
890		if not newLineInserted then
891			REM If the line number is the position at which to insert the new line
892			if lineCounter = newLinePosition then
893				if( instr( xmlFile( lineCounter ), &quot;&lt;menu:menupopup&gt;&quot; ) &gt; 0 ) then
894					indent = GetMenuWhiteSpace( xmlFile( newLinePosition + 1 ) )
895					newXmlFile( lineCounter ) = xmlFile( lineCounter )
896					newXmlFile( lineCounter + 1 ) = ( indent + &quot;&lt;menu:menuitem menu:id=&quot;+chr$(34) + 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; )
897				else
898					indent = GetMenuWhiteSpace( xmlFile( newLinePosition - 1 ) )
899					newXmlFile( lineCounter ) = ( indent + &quot;&lt;menu:menuitem menu:id=&quot;+chr$(34) + 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; )
900					newXmlFile( lineCounter + 1 ) = xmlFile( lineCounter )
901				end if
902			REM added -1 for debug --&gt;
903			&apos;	indent = GetMenuWhiteSpace( xmlFile( newLinePosition ) )
904			&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; )
905			&apos;	newXmlFile( lineCounter + 1 ) = xmlFile( lineCounter )
906				newLineInserted = true
907			else
908				newXmlFile( lineCounter ) = xmlFile( lineCounter )
909			end if
910		else
911			REM if the new line has been inserted the read from one position behind
912			newXmlFile( lineCounter + 1 ) = xmlFile( lineCounter )
913		end if
914		lineCounter = lineCounter + 1
915	loop
916
917	numberOfLines = numberOfLines + 1
918
919	REM read the new file into the global array
920	for n = 1 to numberOfLines
921		xmlFile( n ) = newXmlFile( n )
922	next n
923
924end sub
925
926
927sub AddNewKeyBinding( scriptName as string, shift as boolean, control as boolean, key as string )
928
929	dim keyCombo as string
930	newLine = &quot; &lt;accel:item accel:code=&quot;+chr$(34)+&quot;KEY_&quot; + key +chr$(34)
931	if shift then
932		keyCombo = &quot;SHIFT + &quot;
933		newLine = newLine + &quot; accel:shift=&quot;+chr$(34)+&quot;true&quot;+chr$(34)
934	end if
935	if control then
936		keyCombo = keyCombo + &quot;CONTROL + &quot;
937		newLine = newLine + &quot; accel:mod1=&quot;+chr$(34)+&quot;true&quot;+chr$(34)
938	end if
939	keyCombo = keyCombo + key
940	newLine = newLine + &quot; xlink:href=&quot;+chr$(34)+ scriptName +chr$(34) +&quot;/&gt;&quot;
941
942	if ( control AND shift ) then
943		offsetIntoArrayOfArrays = 0
944	elseif ( control ) then
945		offsetIntoArrayOfArrays = 3
946	elseif ( shift ) then
947		offsetIntoArrayOfArrays = 6
948	endif
949
950	keyTypeOffset = getKeyTypeOffset( key  )
951	offsetIntoArrayOfArrays = offsetIntoArrayOfArrays + keyTypeOffset
952	&apos; Calculate from the key the offset into key group array we need to point to
953	KeyGroupIndex = getKeyGroupIndex( key, keyTypeOffset )
954
955	&apos; if key is allready allocated to a script then just reallocate
956	if ( keyAllocationMap( offsetIntoArrayOfArrays, KeyGroupIndex ).Value &gt; 1 ) then
957
958		keyAllocationMap( offsetIntoArrayOfArrays, KeyGroupIndex ).Name = scriptName
959		&apos;replace line in xml file
960		xmlFile( keyAllocationMap( offsetIntoArrayOfArrays, KeyGroupIndex ).Value ) = newLine
961	else
962		&apos; this is a new binding, create a new line in xml file
963		for n = 1 to numberOfLines
964			if n = numberOfLines then
965				xmlFile( n ) = newLine
966				xmlFile( n + 1 ) = &quot;&lt;/accel:acceleratorlist&gt;&quot;
967				exit for
968			else
969				xmlFile( n ) = xmlFile( n )
970			end if
971		next n
972
973		keyAllocationMap( offsetIntoArrayOfArrays, KeyGroupIndex ).Value = n
974		keyAllocationMap( offsetIntoArrayOfArrays, KeyGroupIndex ).Name = scriptName
975		numberOfLines = numberOfLines + 1
976	endif
977
978end sub
979
980
981Sub RemoveBinding( lineToRemove as Integer )
982	xmlFile( lineToRemove ) = &quot;&quot;
983end Sub
984
985REM Adds or removes the starting xml line positions for each top-level menu after the menu with the added script
986sub UpdateTopLevelMenus( topLevelMenuPosition as integer, addLine as boolean )
987	for n = topLevelMenuPosition to 8
988		if addLine then
989			menuItemLinePosition( n ) = menuItemLinePosition( n ) + 1
990
991		end if
992	next n
993end sub
994
995
996REM Remove scriptNames and scriptLinePosition entries
997sub RemoveScriptNameAndPosition( keyComboPosition )
998	dim updatedScriptNames( 120 ) as string
999	dim updatedScriptLinePosition( 120 ) as integer
1000	dim removedScript as boolean
1001	removedScript = false
1002
1003	for n = 1 to scriptCount
1004		if not removedScript then
1005			if not( n = keyComboPosition ) then
1006				updatedScriptNames( n ) = scriptNames( n )
1007			else
1008				removedScript = true
1009			end if
1010		else
1011			updatedScriptNames( n - 1 ) = scriptNames( n )
1012		end if
1013	next n
1014	scriptCount = scriptCount - 1
1015
1016	for n = 1 to scriptCount
1017		scriptNames( n ) = updatedScriptNames( n )
1018	next n
1019end sub
1020
1021
1022
1023REM ----- Populating Dialog Controls -----
1024
1025Sub PopulateLanguageCombo()
1026	langCombo =  bindingDialog.getControl( &quot;LanguageCombo&quot; )
1027	langCombo.removeItems( 0, langCombo.getItemCount() )
1028	for n = LBOUND( languages() ) to UBOUND ( languages() )
1029		langCombo.addItem( languages( n ), n )
1030	next n
1031	langCombo.setDropDownLineCount( n )
1032	langCombo.text = langCombo.getItem( 0 )
1033End Sub
1034
1035Sub PopulateLocationCombo()
1036	dim ScriptProvider as Object
1037	dim args(1)
1038	dim displayDialogFlag as boolean
1039	displayDialogFlag = false
1040	args(0) = ThisComponent
1041	args(1) = displayDialogFlag
1042
1043	ScriptProvider = createUnoService(&quot;drafts.com.sun.star.script.framework.provider.MasterScriptProvider&quot;)
1044	ScriptProvider.initialize( args() )
1045
1046	locCombo =  bindingDialog.getControl( &quot;LocationCombo&quot; )
1047	locCombo.removeItems( 0, locCombo.getItemCount() )
1048	for n = LBOUND( locations() ) to UBOUND ( locations() )
1049		locCombo.addItem( locations( n ), n )
1050	next n
1051	locCombo.setDropDownLineCount( n )
1052	locCombo.text = locCombo.getItem( 0 )
1053End Sub
1054
1055sub PopulateScriptList( lang as String, loc as String )
1056    Dim detailedView as boolean
1057    detailedView = bindingDialog.Model.detail.state
1058	scriptList = bindingDialog.getControl( &quot;ScriptList&quot; )
1059	scriptList.removeItems( 0, scriptList.getItemCount() )
1060
1061	smgr = getProcessServiceManager()
1062   	context = smgr.getPropertyValue( &quot;DefaultContext&quot; )
1063   	scriptstoragemgr = context.getValueByName( &quot;/singletons/drafts.com.sun.star.script.framework.storage.theScriptStorageManager&quot; )
1064	scriptLocationURI = &quot;USER&quot;
1065	if ( loc = &quot;Share&quot; ) then
1066		scriptLocationURI = &quot;SHARE&quot;
1067	elseif ( loc = &quot;Document&quot; )then
1068		document = StarDesktop.ActiveFrame.Controller.Model
1069		scriptLocationURI = document.getURL()
1070	elseif ( loc = &quot;Filesystem&quot; ) then
1071		REM populate the list from the filesysScripts list
1072		if(lang = &quot;Java&quot; ) then
1073			exit sub
1074		endif
1075		length = UBOUND( filesysScripts() )
1076		if(length = -1) then
1077			exit sub
1078		endif
1079        for langIndex = lbound(languages()) to ubound(languages())
1080            if ( lang = languages(langIndex)) then
1081                extns = extensions(langIndex)
1082                exit for
1083            endif
1084        next langIndex
1085		dim locnDisplayList( length ) as new com.sun.star.beans.PropertyValue
1086		for index = lbound(filesysScripts()) to ubound(filesysScripts())
1087			scriptextn = filesysScripts( index )
1088			pos = lastIndexOf( scriptextn, &quot;.&quot; )
1089            scriptextn = mid( scriptextn, pos + 1, len( scriptextn ) - pos )
1090
1091            for extnsIndex = lbound(extns()) to ubound(extns())
1092                extn = extns(extnsIndex)
1093                if ( scriptextn = extn ) then
1094                	if ( detailedView ) then
1095                    	locnDisplayList( index ).Name = filesysScripts( index )
1096                    	locnDisplayList( index ).Value = filesysScripts( index )
1097                	else
1098						REM replace name with simplified view
1099						locnDisplayList( index ).Name = filesysScripts( index )
1100						locnDisplayList( index ).Value = filesysScripts( index )
1101					end if
1102				scriptList.addItem( locnDisplayList( index ).Name, index )
1103				exit for
1104                end if
1105            next extnsIndex
1106		next index
1107		ScriptDisplayList(0) = locnDisplayList()
1108		scriptList.selectItemPos( 0, true )
1109
1110        REM !!!!At this point we exit the sub!!!!
1111		exit sub
1112
1113	endif
1114
1115	scriptStorageID = scriptstoragemgr.getScriptStorageID( scriptLocationURI )
1116	dim resultList() as Object
1117	if ( scriptStorageID &gt; -1 ) then
1118		storage = scriptstoragemgr.getScriptStorage( scriptStorageID )
1119		implementations() = storage.getAllImplementations()
1120		length = UBOUND( implementations() )
1121		reservedScriptTag = &quot;_$&quot;
1122    	if ( length &gt; -1 ) then
1123    		dim tempDisplayList( length ) as new com.sun.star.beans.PropertyValue
1124			for n = LBOUND( implementations() ) to UBOUND( implementations() )
1125				logicalName = implementations( n ).getLogicalName()
1126				firstTwoChars = LEFT( logicalName, 2 )
1127				&apos;Only display scripts whose logicalnames don&apos;t begin with &quot;_$&quot;
1128				if ( firstTwoChars &lt;&gt; reservedScriptTag ) then
1129					if ( lang = implementations( n ).getLanguage() ) then
1130						if ( detailedView ) then
1131							tempDisplayList( n ).Name = logicalName _
1132								+ &quot; [&quot; + implementations( n ).getFunctionName() + &quot;]&quot;
1133							tempDisplayList( n ).Value = implementations( n )
1134						else
1135							tempDisplayList( n ).Name = logicalName
1136							tempDisplayList( n ).Value = implementations( n )
1137						endif
1138						scriptList.addItem( tempDisplayList( n ).Name, n )
1139					endif
1140				endif
1141			next n
1142			resultList = tempDisplayList()
1143		endif
1144	ScriptDisplayList(0) = resultList()
1145	endif
1146	scriptList.selectItemPos( 0, true )
1147
1148end sub
1149
1150sub PopulateMenuCombo()
1151	menuComboBox = bindingDialog.getControl( &quot;MenuCombo&quot; )
1152	menuComboBox.removeItems( 0, menuComboBox.getItemCount() )
1153	for n = 1 to menuCount
1154		menuComboBox.addItem( menuItems( n ), n - 1 )
1155	next n
1156	menuComboBox.setDropDownLineCount( 8 )
1157	menuComboBox.text = menuComboBox.getItem( 0 )
1158end sub
1159
1160
1161sub PopulateSubMenuList( menuItemPosition as integer )
1162	redim subMenuItems( 100 ) as string
1163	redim subMenuItemLinePosition( 100 ) as integer
1164	dim lineNumber as integer
1165	const menuItemWhiteSpace = 4
1166	const menuXMLTag = &quot;&lt;menu:menu&quot;
1167	subMenuCount = 1
1168
1169	REM xmlStartLine and xmlEndLine refer to the first and last lines
1170	&apos; menuItemPosition of a top-level menu ( 1=File to 8=Help ) add one line
1171	xmlStartLine = menuItemLinePosition( menuItemPosition ) + 1
1172
1173	REM If last menu item is chosen
1174	if menuItemPosition = menuCount then
1175		xmlEndLine = numberOfLines
1176	else
1177		REM Other wise get the line before the next top-level menu begins
1178		xmlEndLine = menuItemLinePosition( menuItemPosition + 1 ) - 1
1179	end if
1180
1181	for lineNumber = xmlStartLine to xmlEndLine
1182		REM Insert all sub-menus and sub-popupmenus
1183		if not( instr( xmlFile( lineNumber ), menuXMLTag ) = 0 ) and instr( xmlFile( lineNumber ), &quot;menupopup&quot;) = 0 then
1184			subMenuIndent = GetMenuWhiteSpace( xmlFile( lineNumber ) )
1185			if subMenuIndent = &quot; &quot; then
1186				subMenuIndent = &quot;&quot;
1187			else
1188				subMenuIndent = subMenuIndent + subMenuIndent
1189			end if
1190			if not( instr( xmlFile( lineNumber ), &quot;menuseparator&quot; ) = 0 ) then
1191				subMenuItems( subMenuCount ) = subMenuIndent + &quot;----------------&quot;
1192			else
1193				subMenuName = ExtractLabelFromXMLLine( xmlFile( lineNumber ) )
1194				REM Add script Name if there is one bound to menu item
1195				if instr( xmlFile( lineNumber ), &quot;script://&quot; ) &gt; 0 then
1196					script = ExtractScriptIdFromXMLLine( xmlFile( lineNumber ) )
1197					subMenuItems( subMenuCount ) = ( subMenuIndent + subMenuName + &quot; [&quot; + script + &quot;]&quot; )
1198				else
1199					subMenuItems( subMenuCount ) = subMenuIndent + subMenuName
1200				end if
1201			end if
1202			subMenuItemLinePosition( subMenuCount ) = lineNumber
1203			subMenuCount = subMenuCount + 1
1204		end if
1205	next lineNumber
1206
1207	subMenuList = bindingDialog.getControl( &quot;SubMenuList&quot; )
1208
1209	currentPosition = subMenuList.getSelectedItemPos()
1210
1211	subMenuList.removeItems( 0, subMenuList.getItemCount() )
1212	&apos;If there are no sub-menus i.e. a dynamically generated menu like Format
1213	&apos;if subMenuCount = 1 then
1214	if menuItems( menuItemPosition ) = &quot;Format&quot; then
1215			subMenuList.addItem( &quot;Unable to Assign Scripts to this menu&quot;, 0 )
1216	else
1217		for n = 1 to subMenuCount - 1
1218			subMenuList.addItem( subMenuItems( n ), n - 1 )
1219		next n
1220	end if
1221
1222	subMenuList.selectItemPos( currentPosition, true )
1223
1224	SubMenuListListener()
1225	MenuLabelBoxListener()
1226end sub
1227
1228
1229
1230sub PopulateTopLevelKeyBindingList()
1231
1232    allKeyGroupsArray(0) =  &quot;SHIFT + CONTROL + F keys&quot;
1233	allKeyGroupsArray(1) = &quot;SHIFT + CONTROL + digits&quot; &apos; CURRENTLY DISABLED
1234	allKeyGroupsArray(2) = &quot;SHIFT + CONTROL + letters&quot;
1235	allKeyGroupsArray(3) = &quot;CONTROL + F keys&quot;
1236	allKeyGroupsArray(4) = &quot;CONTROL + digits&quot;
1237	allKeyGroupsArray(5) = &quot;CONTROL + letters&quot;
1238	allKeyGroupsArray(6) = &quot;SHIFT + F keys&quot;
1239
1240	keyCombo = bindingDialog.getControl( &quot;KeyCombo&quot; )
1241	keyCombo.removeItems( 0, keyCombo.getItemCount() )
1242	pos = 0
1243	for n = LBOUND( allKeyGroupsArray() ) to UBOUND( allKeyGroupsArray() )
1244	&apos; SHIFT + CONTROL + digits group is disabled at the moment, so skip
1245	&apos; it
1246		if ( n &lt;&gt; 1 ) then
1247			keyCombo.addItem( allKeyGroupsArray( n ), pos )
1248			pos = pos +1
1249		endif
1250	next n
1251	keyCombo.text = keyCombo.getItem( 0 )
1252end sub
1253
1254sub PopulateKeyBindingList( keyGroupIndex as Integer )
1255	keyList = bindingDialog.getControl( &quot;KeyList&quot; )
1256	selectedPos = keyList.getSelectedItemPos()
1257	keyList.removeItems( 0, keyList.getItemCount() )
1258
1259	ShortCutKeyArray() = KeyBindArrayOfArrays( keyGroupIndex )
1260
1261	Dim keyProp as new com.sun.star.beans.PropertyValue
1262	for n = lbound( ShortCutKeyArray() )  to ubound( ShortCutKeyArray() )
1263		keyName = ShortCutKeyArray( n )
1264		if ( keyAllocationMap( keyGroupIndex, n ).Value = 1 ) then
1265			keyName = keyName + &quot; [Allocated to Office function]&quot;
1266
1267		elseif ( keyAllocationMap( keyGroupIndex, n ).Value &gt; 1 ) then
1268			keyName = keyName + &quot; &quot; + keyAllocationMap( keyGroupIndex, n ).Name
1269		endif
1270		keyList.addItem( keyName, n )
1271	next n
1272
1273	if ( selectedPos &lt;&gt; -1 )then
1274		keyList.selectItemPos( selectedPos, true )
1275	else
1276		keyList.selectItemPos( 0, true )
1277	end if
1278	KeyListListener()
1279end sub
1280
1281sub populateEventList( focusPosition as integer )
1282	allApps = bindingDialog.getControl( &quot;AllAppsOption&quot; )
1283	eventList = bindingDialog.getControl( &quot;EventList&quot; )
1284	eventList.removeItems( 0, eventList.getItemCount() )
1285
1286	dim isApp as boolean
1287	if allApps.state = true then  &apos; Application event
1288		isApp = true
1289	else
1290		isApp = false
1291	end if
1292
1293	&apos; use allEventTypes() to fill list box
1294	&apos; for each element compare with allEventTypesApp
1295	dim scriptName as string
1296	dim lineNumber as integer
1297	for n = 0 to ubound( allEventTypes() )
1298		&apos; If the line number is 1 then SB macro
1299		&apos; more than 1 it is the line number of the script
1300		if isApp and n &gt; 12 then
1301			exit for
1302		endif
1303		if isApp then
1304			lineNumber = allEventTypesApp( n ).Value
1305			scriptName = allEventTypesApp( n ).Name
1306		else
1307			lineNumber = allEventTypesDoc( n ).Value
1308			scriptName = allEventTypesDoc( n ).Name
1309		end if
1310		stringToAdd = &quot;&quot;
1311		if ( lineNumber &gt;= 1 ) then
1312			stringToAdd = &quot; [&quot; + scriptName + &quot;]&quot;
1313		end if
1314		eventList.addItem( allEventTypes( n ).Value + &quot; &quot; + stringToAdd, n )
1315	next n
1316
1317	eventList.selectItemPos( focusPosition, true )
1318end sub
1319
1320
1321
1322sub CreateAllKeyBindings()
1323	reDim allKeyBindings( 105 ) as string
1324	keyBindingPosition = 1
1325
1326	for FKey = 2 to 12
1327		allKeyBindings( keyBindingPosition ) = &quot;SHIFT + CONTROL + F&quot; + FKey
1328		keyBindingPosition = keyBindingPosition + 1
1329	next FKey
1330	for Digit = 0 to 9
1331		allKeyBindings( keyBindingPosition ) = &quot;SHIFT + CONTROL + &quot; + Digit
1332			keyBindingPosition = keyBindingPosition + 1
1333	next Digit
1334	for Alpha = 65 to 90
1335		allKeyBindings( keyBindingPosition ) = &quot;SHIFT + CONTROL + &quot; + chr$( Alpha )
1336		keyBindingPosition = keyBindingPosition + 1
1337	next Alpha
1338
1339	for FKey = 2 to 12
1340		allKeyBindings( keyBindingPosition ) = &quot;CONTROL + F&quot; + FKey
1341		keyBindingPosition = keyBindingPosition + 1
1342	next FKey
1343	for Digit = 0 to 9
1344		allKeyBindings( keyBindingPosition ) = &quot;CONTROL + &quot; + Digit
1345		keyBindingPosition = keyBindingPosition + 1
1346	next Digit
1347	for Alpha = 65 to 90
1348		allKeyBindings( keyBindingPosition ) = &quot;CONTROL + &quot; + chr$( Alpha )
1349		keyBindingPosition = keyBindingPosition + 1
1350	next Alpha
1351
1352	for FKey = 2 to 12
1353		allKeyBindings( keyBindingPosition ) = &quot;SHIFT + F&quot; + FKey
1354		keyBindingPosition = keyBindingPosition + 1
1355	next FKey
1356end sub
1357
1358
1359sub createAllEventTypes()
1360	allEventTypes( 0 ).Name = &quot;OnStartApp&quot;
1361	allEventTypes( 0 ).Value = &quot;Start Application&quot;
1362	allEventTypes( 1 ).Name = &quot;OnCloseApp&quot;
1363	allEventTypes( 1 ).Value = &quot;Close Application&quot;
1364	allEventTypes( 2 ).Name = &quot;OnNew&quot;
1365	allEventTypes( 2 ).Value = &quot;Create Document&quot;
1366	allEventTypes( 3 ).Name = &quot;OnLoad&quot;
1367	allEventTypes( 3 ).Value = &quot;Open Document&quot;
1368	allEventTypes( 4 ).Name = &quot;OnSaveAs&quot;
1369	allEventTypes( 4 ).Value = &quot;Save Document As&quot;
1370	allEventTypes( 5 ).Name = &quot;OnSaveAsDone&quot;
1371	allEventTypes( 5 ).Value = &quot;Document has been saved as&quot;
1372	allEventTypes( 6 ).Name = &quot;OnSave&quot;
1373	allEventTypes( 6 ).Value = &quot;Save Document&quot;
1374	allEventTypes( 7 ).Name = &quot;OnSaveDone&quot;
1375	allEventTypes( 7 ).Value = &quot;Document has been saved&quot;
1376	allEventTypes( 8 ).Name = &quot;OnPrepareUnload&quot;
1377	allEventTypes( 8 ).Value = &quot;Close Document&quot;
1378	allEventTypes( 9 ).Name = &quot;OnUnload&quot;
1379	allEventTypes( 9 ).Value = &quot;Close Document&quot;
1380	allEventTypes( 10 ).Name = &quot;OnFocus&quot;
1381	allEventTypes( 10 ).Value = &quot;Activate document&quot;
1382	allEventTypes( 11 ).Name = &quot;OnUnfocus&quot;
1383	allEventTypes( 11 ).Value = &quot;DeActivate document&quot;
1384	allEventTypes( 12 ).Name = &quot;OnPrint&quot;
1385	allEventTypes( 12 ).Value = &quot;Print Document&quot;
1386	REM The following are document-only events
1387	allEventTypes( 13 ).Name = &quot;OnMailMerge&quot;
1388	allEventTypes( 13 ).Value = &quot;Print form letters&quot;
1389	allEventTypes( 14 ).Name = &quot;OnPageCountChange&quot;
1390	allEventTypes( 14 ).Value = &quot;Changing the page count&quot;
1391end sub
1392
1393
1394sub createAllEventBindings()
1395	&apos;dim props as new com.sun.star.beans.PropertyValue
1396	&apos;props.Name = &quot;&quot; &apos;Name = script name
1397	&apos;props.Value = 0 &apos;Value = 0 for empty, 1 for macro, linenumber for script
1398
1399	&apos; Creates all types of event bindings for both Application and Document
1400	&apos; Initially both arrays have no bindings allocated to the events
1401	&apos; The value for Doc is only Script/macro name (no need for line number)
1402	for n = 0 to ubound( allEventTypes() )
1403		allEventTypesApp( n ).Name = &quot;&quot;
1404		allEventTypesApp( n ).Value = 0
1405		allEventTypesDoc( n ).Name = &quot;&quot;
1406		allEventTypesDoc( n ).Value = 0
1407	next n
1408end sub
1409
1410
1411REM ----- Text Handling Functions -----
1412
1413
1414function ExtractLabelFromXMLLine( XMLLine as string ) as string
1415	labelStart = instr( XMLLine, &quot;label=&quot;+chr$(34)) + 7
1416	labelEnd = instr( XMLLine, chr$(34)+&quot;&gt;&quot; )
1417	if labelEnd = 0 then
1418	    labelEnd = instr( XMLLine, chr$(34)+&quot;/&gt;&quot; )
1419	end if
1420	labelLength = labelEnd - labelStart
1421
1422	menuLabelUnformatted = mid( XMLLine, labelStart, labelLength )
1423	tildePosition = instr( menuLabelUnformatted, &quot;~&quot; )
1424	select case tildePosition
1425		case 0
1426			menuLabel = menuLabelUnformatted
1427		case 1
1428			menuLabel = right( menuLabelUnformatted, labelLength - 1 )
1429		case else
1430			menuLabelLeft = left( menuLabelUnformatted, tildePosition - 1 )
1431			menuLabelRight = right( menuLabelUnformatted, labelLength - tildePosition )
1432			menuLabel = menuLabelLeft + menuLabelRight
1433	end select
1434
1435	ExtractLabelFromXMLLine() = menuLabel
1436end function
1437
1438
1439function ExtractScriptIdFromXMLLine( XMLLine as string ) as string
1440	idStart = instr( XMLLine, &quot;script://&quot;) + 9
1441	if instr( XMLLine, chr$(34)+&quot; menu:helpid=&quot; ) = 0 then
1442		idEnd = instr( XMLLIne, &quot;?location=&quot; )
1443	else
1444		idEnd = instr( XMLLine, &quot;&quot;+chr$(34)+&quot; menu:helpid=&quot; )
1445	end if
1446	idLength = idEnd - idStart
1447	scriptId = mid( XMLLine, idStart, idLength )
1448
1449	ExtractScriptIdFromXMLLine() = scriptId
1450end function
1451
1452function ExtractEventScriptFromXMLLine( xmlline as string )
1453	if instr( xmlline, &quot;script://&quot; ) &gt; 0 then
1454		idStart = instr( xmlline, &quot;script://&quot;) + 9
1455		idEnd = instr( xmlline, chr$(34)+&quot; xlink:type=&quot; )
1456		idLength = idEnd - idStart
1457		scriptId = mid( xmlline, idStart, idLength )
1458	end if
1459	ExtractEventScriptFromXMLLine() = scriptId
1460end function
1461
1462
1463function ExtractEventNameFromXMLLine(  xmlline as string )
1464	idStart = instr( xmlline, &quot;event:name=&quot; + chr$(34) ) + 12
1465	idEnd = instr( xmlline, chr$(34)+&quot; event:language&quot; )
1466	idLength = idEnd - idStart
1467	event =  mid( xmlline, idStart, idLength )
1468
1469	ExtractEventNameFromXMLLine() = event
1470end function
1471
1472function ExtractKeyCodeFromXMLLine( XMLLine as string ) as string
1473	keyStart = instr( XMLLine, &quot;code=&quot;+chr$(34)+&quot;KEY_&quot;) + 10
1474	keyCode = mid( XMLLine, keyStart, ( len( XMLLine ) - keyStart ) )
1475	keyEnd = instr( keyCode, chr$(34) )
1476	keyCode = mid( keyCode, 1, keyEnd - 1 )
1477
1478	ExtractKeyCodeFromXMLLine() = keyCode
1479end function
1480
1481
1482function GetMenuWhiteSpace( MenuXMLLine as string ) as string
1483	whiteSpace = &quot;&quot;
1484	numberOfSpaces = instr( MenuXMLLine, &quot;&lt;&quot; ) - 1
1485	for i = 1 to numberOfSpaces
1486		whiteSpace = whiteSpace + &quot; &quot;
1487	next i
1488
1489	GetMenuWhiteSpace() = whiteSpace
1490end function
1491
1492function IsAllocatedMenuItem( script as string ) as boolean
1493	foundMenuItem = false
1494	Allocated = false
1495	count = 0
1496	do
1497		count = count + 1
1498		if strcomp( script, subMenuItems( count ) ) = 0 then
1499			foundMenuItem = true
1500		end if
1501	loop while not( foundMenuItem ) and count &lt; subMenuCount
1502
1503	linePosition = subMenuItemLinePosition( count )
1504
1505	if not( instr( xmlFile( linePosition ), &quot;script://&quot; ) = 0 ) then
1506		Allocated = true
1507	end if
1508
1509	isAllocatedMenuItem() = Allocated
1510end Function
1511
1512
1513function HasShiftKey( keyCombo ) as boolean
1514	if instr( keyCombo, &quot;SHIFT&quot; ) = 0 then
1515		hasShift = false
1516	else
1517		hasShift = true
1518	end if
1519
1520	HasShiftKey = hasShift
1521end function
1522
1523
1524function HasControlKey( keyCombo ) as boolean
1525	if instr( keyCombo, &quot;CONTROL&quot; ) = 0 then
1526		hasControl = false
1527	else
1528		hasControl = true
1529	end if
1530
1531	HasControlKey = hasControl
1532end function
1533
1534
1535function ExtractKeyFromCombo( keyString as string ) as string
1536	while not( instr( keyString, &quot;+&quot; ) = 0 )
1537		removeTo = instr( keyString, &quot;+ &quot; ) + 2
1538		keyString = mid( keyString, removeTo, ( len( keyString ) - removeTo ) + 1 )
1539	wend
1540	ExtractKeyFromCombo() = keyString
1541end function
1542
1543
1544
1545REM ------ Event Handling Functions (Listeners) ------
1546
1547
1548sub KeyListListener()
1549	keyShortCutList = bindingDialog.getControl( &quot;KeyList&quot; )
1550	selectedShortCut = keyShortCutList.getSelectedItem()
1551	combo = bindingDialog.getControl( &quot;KeyCombo&quot; )
1552
1553	menuScriptList = bindingDialog.getControl( &quot;ScriptList&quot; )
1554	selectedScript = menuScriptList.getSelectedItem()
1555
1556	keyGroup = combo.text
1557    dim keyGroupIndex as Integer
1558    dim selectedKeyIndex as Integer
1559	for n = lbound ( allKeyGroupsArray() ) to ubound ( allKeyGroupsArray() )
1560		if ( allKeyGroupsArray( n ) = keyGroup )then
1561			keyGroupIndex = n
1562			exit for
1563		end if
1564    next n
1565    selectedKeyIndex = keyShortCutList.getSelectedItemPos()
1566
1567	if  keyAllocationMap( keyGroupIndex, selectedKeyIndex ).Value &gt; 1 then
1568		bindingDialog.Model.Delete.enabled = true
1569		bindingDialog.Model.AddOn.enabled = true
1570		if selectedScript &lt;&gt; &quot;&quot; then
1571			bindingDialog.Model.NewButton.enabled = true
1572		endif
1573
1574	else
1575
1576		if keyAllocationMap( keyGroupIndex, selectedKeyIndex ).Value  = 1 then
1577			bindingDialog.Model.Delete.enabled = false
1578			bindingDialog.Model.AddOn.enabled = false
1579			bindingDialog.Model.NewButton.enabled = false
1580		else
1581			bindingDialog.Model.Delete.enabled = false
1582			bindingDialog.Model.AddOn.enabled = false
1583			if selectedScript &lt;&gt; &quot;&quot; then
1584				bindingDialog.Model.NewButton.enabled = true
1585			end if
1586		end if
1587	end if
1588end sub
1589
1590
1591sub SubMenuListListener()
1592	scriptList = bindingDialog.getControl( &quot;ScriptList&quot; )
1593	subMenuList = bindingDialog.getControl( &quot;SubMenuList&quot; )
1594	selectedMenuItem = subMenuList.getSelectedItem()
1595	if IsAllocatedMenuItem( selectedMenuItem ) then
1596		bindingDialog.Model.Delete.enabled = true
1597		bindingDialog.Model.AddOn.enabled = true
1598	else
1599		bindingDialog.Model.Delete.enabled = false
1600		bindingDialog.Model.AddOn.enabled = false
1601	end if
1602end sub
1603
1604REM a keypress listener that in turn fires the MenuCL on a return key even only
1605sub fireMenuComboListernerOnRet( eventobj as object )
1606	if (eventobj.KeyCode = 1280 ) then
1607		MenuComboListener()
1608	endif
1609end sub
1610
1611&apos;Populates the SubMenuList with the appropriate menu items from the Top-level menu selected from the combo box
1612sub MenuComboListener()
1613	combo = bindingDialog.getControl( &quot;MenuCombo&quot; )
1614	newToplevelMenu = combo.text
1615	counter = 0
1616	do
1617		counter = counter + 1
1618	loop while not( newToplevelMenu = menuItems( counter ) )
1619
1620	PopulateSubMenuList( counter )
1621end sub
1622
1623REM a keypress listener that in turn fires the LLCL on a return key even only
1624sub fireLangLocComboListernerOnRet( eventobj as object )
1625	if (eventobj.KeyCode = 1280 ) then
1626		LangLocComboListener()
1627	endif
1628end sub
1629
1630sub LangLocComboListener()
1631
1632	combo = bindingDialog.getControl( &quot;LanguageCombo&quot; )
1633	language = combo.text
1634	combo = bindingDialog.getControl( &quot;LocationCombo&quot; )
1635	location = combo.text
1636
1637	PopulateScriptList( language,location )
1638
1639    &apos;Enable/disable Assign button
1640    scriptList = bindingDialog.getControl( &quot;ScriptList&quot; )
1641    if not (dialogName = &quot;EditDebug&quot;) then
1642        if scriptList.getSelectedItem() = &quot;&quot; then
1643            bindingDialog.Model.NewButton.enabled = false
1644        end if
1645    end if
1646
1647    if ( location = &quot;Filesystem&quot; ) and ( language &lt;&gt; &quot;Java&quot; ) then
1648    	bindingDialog.Model.Browse.enabled = true
1649    	if not (dialogName = &quot;EditDebug&quot;) then
1650    		bindingDialog.Model.fsonly.enabled = true
1651    	end if
1652    else
1653    	bindingDialog.Model.Browse.enabled = false
1654    	if not (dialogName = &quot;EditDebug&quot;) then
1655    		bindingDialog.Model.fsonly.enabled = false
1656    	end if
1657    endif
1658
1659    &apos; extra dialog dependant processing
1660    if dialogName = &quot;Menu&quot; then
1661    	&apos; will set New button to false if no text in LableBox
1662    	MenuLabelBoxListener()
1663    elseif dialogName = &quot;Key&quot; then
1664    	&apos; will set Assigne button to false if appropriate
1665    	KeyListListener()
1666    elseif dialogName = &quot;Event&quot; then
1667    	EventListListener()
1668    end if
1669
1670end sub
1671
1672REM a keypress listener that in turn fires the KeyCL on a return key even only
1673sub fireKeyComboListernerOnRet( eventobj as object )
1674	if (eventobj.KeyCode = 1280 ) then
1675		KeyComboListener()
1676	endif
1677end sub
1678
1679&apos;Populates the KeyList with the appropriate key combos from the Top-level key group selected from the combo box
1680sub KeyComboListener()
1681	combo = bindingDialog.getControl( &quot;KeyCombo&quot; )
1682	keyGroup = combo.text
1683	for n = lbound ( allKeyGroupsArray() ) to ubound ( allKeyGroupsArray() )
1684		if ( allKeyGroupsArray( n ) = keyGroup )then
1685			keyGroupIndex = n
1686			exit for
1687		end if
1688    next n
1689	PopulateKeyBindingList( keyGroupIndex )
1690end sub
1691
1692
1693sub MenuLabelBoxListener()
1694	menuScriptList = bindingDialog.getControl( &quot;ScriptList&quot; )
1695	selectedScript = menuScriptList.getSelectedItem()
1696	&apos;if the SubMenuList is from a dynamically created menu (e.g. Format)
1697	&apos;or if the Menu Label text box is empty
1698	subMenuList = bindingDialog.getControl( &quot;SubMenuList&quot; )
1699	firstItem = subMenuList.getItem( 0 )
1700	if bindingDialog.Model.MenuLabelBox.text = &quot;&quot; OR firstItem = &quot;Unable to Assign Scripts to this menu&quot; OR  selectedScript = &quot;&quot; then
1701		bindingDialog.Model.NewButton.enabled = false
1702	else
1703		bindingDialog.Model.NewButton.enabled = true
1704	end if
1705end sub
1706
1707sub AppDocEventListener()
1708	populateEventList( 0 )
1709	EventListListener()
1710end sub
1711
1712
1713sub EventListListener()
1714	on error goto ErrorHandler
1715
1716    eventList = bindingDialog.getControl( &quot;EventList&quot; )
1717    eventPos = eventList.getSelectedItemPos()
1718
1719    allApps = bindingDialog.getControl( &quot;AllAppsOption&quot; )
1720
1721	menuScriptList = bindingDialog.getControl( &quot;ScriptList&quot; )
1722	selectedScript = menuScriptList.getSelectedItem()
1723
1724    dim binding as integer
1725    if allApps.state = true then
1726    	binding = allEventTypesApp( eventPos ).Value
1727    else
1728	    binding = allEventTypesDoc( eventPos ).Value
1729    endif
1730
1731    if ( binding &gt; 1 ) then
1732       	bindingDialog.Model.Delete.enabled = true
1733    else
1734       	bindingDialog.Model.Delete.enabled = false
1735    end if
1736
1737	if ( binding = 1 ) then
1738		&apos; staroffice binding, can&apos;t assign
1739		bindingDialog.Model.NewButton.enabled = false
1740	elseif (  selectedScript &lt;&gt; &quot;&quot; ) then
1741		bindingDialog.Model.NewButton.enabled = true
1742	end if
1743    exit sub
1744
1745    ErrorHandler:
1746    reset
1747    bindingDialog.Model.Delete.enabled = false
1748
1749end sub
1750
1751
1752REM ------ Event Handling Functions (Buttons) ------
1753
1754function getFilePicker() as Object
1755    REM file dialog
1756    oFilePicker = CreateUnoService( &quot;com.sun.star.ui.dialogs.FilePicker&quot; )
1757
1758    combo = bindingDialog.getControl( &quot;LanguageCombo&quot; )
1759    language = combo.text
1760    currentFilter = &quot;&quot;
1761
1762    for langIndex = 0 to ubound(languages())
1763        if( languages(langIndex) &lt;&gt; &quot;Java&quot; ) then
1764        	filterName = languages(langIndex) + &quot; (&quot;
1765        	filterVal=&quot;&quot;
1766        	extns = extensions(langIndex)
1767        	for extnIndex = lbound(extns()) to ubound(extns())
1768            	filterName = filterName + &quot;*.&quot; + extns(extnIndex) + &quot;,&quot;
1769           	 	filterVal = filterVal + &quot;*.&quot; + extns(extnIndex) + &quot;,&quot;
1770        	next extnIndex
1771        	filterName = left(filterName, len(filterName) -1) + &quot;)&quot;
1772        	filterVal = left(filterVal, len(filterVal) -1)
1773        	if(instr(filterName,language) = 1 ) then
1774            	currentFilter = filterName
1775        	end if
1776        	oFilePicker.AppendFilter(filterName, filterVal)
1777        end if
1778    next langIndex
1779    if(len(currentFilter) &gt; 0 ) then
1780        oFilePicker.SetCurrentFilter( currentFilter )
1781    end if
1782
1783    If sFileURL = &quot;&quot; Then
1784        oSettings = CreateUnoService( &quot;com.sun.star.frame.Settings&quot; )
1785        oPathSettings = oSettings.getByName( &quot;PathSettings&quot; )
1786        sFileURL = oPathSettings.getPropertyValue( &quot;Work&quot; )
1787    End If
1788
1789    REM set display directory
1790    oSimpleFileAccess = CreateUnoService( &quot;com.sun.star.ucb.SimpleFileAccess&quot; )
1791
1792    If oSimpleFileAccess.exists( sFileURL ) And oSimpleFileAccess.isFolder( sFileURL ) Then
1793        oFilePicker.setDisplayDirectory( sFileURL )
1794    End If
1795    getFilePicker() = oFilePicker
1796end function
1797
1798Sub DoBrowseAndEdit()
1799    Dim oFilePicker As Object, oSimpleFileAccess As Object
1800    Dim oSettings As Object, oPathSettings As Object
1801    Dim sFileURL As String
1802    Dim sFiles As Variant
1803
1804    oFilePicker = getFilePicker()
1805    REM execute file dialog
1806    If oFilePicker.execute() Then
1807        sFiles = oFilePicker.getFiles()
1808
1809        sFileURL = sFiles(0)
1810    	oSimpleFileAccess = CreateUnoService( &quot;com.sun.star.ucb.SimpleFileAccess&quot; )
1811        If oSimpleFileAccess.exists( sFileURL ) Then
1812            for langIndex = 0 to ubound(languages())
1813                If (instr(oFilePicker.GetCurrentFilter, languages(langIndex)) = 1 ) then
1814                	RunDebugger(languages(langIndex), sFileURL, &quot;&quot;)
1815            	End If
1816            next langIndex
1817        End If
1818        bindingDialog.endExecute()
1819    End If
1820End Sub
1821
1822Sub RunDebugger(lang as String, uri as String, filename as String)
1823    dim document   as object
1824    dim dispatcher as object
1825    dim parser     as object
1826    dim url        as new com.sun.star.util.URL
1827
1828    document = ThisComponent.CurrentController.Frame
1829    parser   = createUnoService(&quot;com.sun.star.util.URLTransformer&quot;)
1830    dim args(2) as new com.sun.star.beans.PropertyValue
1831    args(0).Name = &quot;language&quot;
1832    args(0).Value = lang
1833    args(1).Name = &quot;uri&quot;
1834    args(1).Value = uri
1835    args(2).Name = &quot;filename&quot;
1836    args(2).Value = filename
1837
1838    url.Complete = &quot;script://_$DebugRunner.Debug?&quot; _
1839        + &quot;language=Java&amp;function=DebugRunner.go&quot; _
1840        + &quot;&amp;location=share&quot;
1841
1842    parser.parseStrict(url)
1843    disp = document.queryDispatch(url,&quot;&quot;,0)
1844    disp.dispatch(url, args())
1845End Sub
1846
1847sub DoEdit()
1848    Dim scriptInfo as Object
1849
1850    menuScriptList = bindingDialog.getControl( &quot;ScriptList&quot; )
1851    selectedScript = menuScriptList.getSelectedItem()
1852
1853    if not (selectedScript = &quot;&quot;) then
1854        scripts() = scriptDisplayList(0)
1855        for n = LBOUND( scripts() ) to UBOUND( scripts() )
1856            if ( scripts( n ).Name = selectedScript ) then
1857                scriptInfo = scripts( n ).Value
1858                exit for
1859            end if
1860        next n
1861
1862        RunDebugger(scriptInfo.getLanguage, scriptInfo.getParcelURI, scriptInfo.getFunctionName)
1863        bindingDialog.endExecute()
1864    end if
1865end sub
1866
1867sub MenuOKButton()
1868	WriteXMLFromArray()
1869	bindingDialog.endExecute()
1870end sub
1871
1872
1873sub MenuCancelButton()
1874	bindingDialog.endExecute()
1875end sub
1876
1877
1878sub MenuHelpButton()
1879	helpDialog = LoadDialog( &quot;ScriptBindingLibrary&quot;, &quot;HelpBinding&quot; )
1880	helpDialog.execute()
1881end sub
1882
1883
1884sub MenuDeleteButton()
1885	subMenuList = bindingDialog.getControl( &quot;SubMenuList&quot; )
1886	linePos = subMenuItemLinePosition( subMenuList.getSelectedItemPos() + 1 )
1887
1888	RemoveBinding( linePos )
1889
1890	REM Update the top-level menu&apos;s line positions
1891	combo = bindingDialog.getControl( &quot;MenuCombo&quot; )
1892	newToplevelMenu = combo.text
1893	counter = 0
1894	do
1895		counter = counter + 1
1896	loop while not( newToplevelMenu = menuItems( counter ) )
1897	UpdateTopLevelMenus( counter + 1, false )
1898
1899	MenuComboListener()
1900
1901	subMenuList.selectItemPos( subMenuList.getSelectedItemPos(), true )
1902end sub
1903
1904
1905sub MenuNewButton()
1906	menuScriptList = bindingDialog.getControl( &quot;ScriptList&quot; )
1907	selectedScript = menuScriptList.getSelectedItem()
1908    scriptURI = getScriptURI( selectedScript )
1909	newMenuLabel = bindingDialog.Model.MenuLabelBox.text
1910
1911	subMenuList = bindingDialog.getControl( &quot;SubMenuList&quot; )
1912
1913	REM Update the top-level menu&apos;s line positions
1914	combo = bindingDialog.getControl( &quot;MenuCombo&quot; )
1915	newToplevelMenu = combo.text
1916	counter = 0
1917	do
1918		counter = counter + 1
1919	loop while not( newToplevelMenu = menuItems( counter ) )
1920	UpdateTopLevelMenus( counter + 1, true )
1921
1922	REM New line position is one ahead of the selected sub menu item
1923	linePos = subMenuItemLinePosition( subMenuList.getSelectedItemPos() + 1 ) + 1
1924
1925	AddNewMenuBinding( scriptURI, newMenuLabel, linePos )
1926
1927	MenuComboListener()
1928	subMenuList.selectItemPos( subMenuList.getSelectedItemPos() + 1, true )
1929	SubMenuListListener()
1930end sub
1931
1932sub BrowseButton()
1933	Dim oFilePicker As Object, oSimpleFileAccess As Object
1934	Dim oSettings As Object, oPathSettings As Object
1935	Dim sFileURL As String
1936	Dim sFiles As Variant
1937
1938	oFilePicker = getFilePicker()
1939
1940	REM execute file dialog
1941	If oFilePicker.execute() Then
1942		sFiles = oFilePicker.getFiles()
1943		sFileURL = sFiles(0)
1944		oSimpleFileAccess = CreateUnoService( &quot;com.sun.star.ucb.SimpleFileAccess&quot; )
1945		If oSimpleFileAccess.exists( sFileURL ) Then
1946			REM add sFileURL to the list
1947			ReDim preserve filesysScripts(filesysCount) as String
1948			filesysScripts( filesysCount ) = sFileURL
1949			filesysCount=filesysCount+1
1950			&apos; if user changed filter in file picker then populate
1951			&apos; language with language associated with that in file picker
1952			sFilter = oFilePicker.getCurrentFilter()
1953			langCombo = bindingDialog.getControl( &quot;LanguageCombo&quot; )
1954			dim items() as String
1955			items() = langCombo.getItems()
1956			for index = lbound(items()) to ubound(items())
1957				iPos = inStr(sFilter,&quot; &quot;)
1958				Dim theLanguage as String
1959				if( iPos &gt; 0 ) then
1960					theLanguage = Left( sFilter, iPos - 1)
1961				    if ( theLanguage = items( index ) ) then
1962				 		langCombo.text = items( index )
1963				 		exit for
1964				 	end if
1965				end if
1966			next index
1967		End If
1968	End If
1969	LangLocComboListener()
1970End Sub
1971
1972sub KeyOKButton()
1973	WriteXMLFromArray()
1974	bindingDialog.endExecute()
1975end sub
1976
1977
1978sub KeyCancelButton()
1979	bindingDialog.endExecute()
1980end sub
1981
1982
1983sub KeyHelpButton()
1984	helpDialog = LoadDialog( &quot;ScriptBindingLibrary&quot;, &quot;HelpBinding&quot; )
1985	helpDialog.execute()
1986end sub
1987
1988
1989sub KeyNewButton()
1990	combo = bindingDialog.getControl( &quot;KeyCombo&quot; )
1991	keyGroup = combo.text
1992	for n = lbound ( allKeyGroupsArray() ) to ubound ( allKeyGroupsArray() )
1993		if ( allKeyGroupsArray( n ) = keyGroup )then
1994			keyGroupIndex = n
1995			exit for
1996		end if
1997	next n
1998	menuScriptList = bindingDialog.getControl( &quot;ScriptList&quot; )
1999	script = menuScriptList.getSelectedItem()
2000	scriptURI = getScriptURI( script )
2001
2002	keyList = bindingDialog.getControl( &quot;KeyList&quot; )
2003	keyIndex = keyList.getSelectedItemPos()
2004	ShortCutKeyArray() = KeyBindArrayOfArrays( keyGroupIndex )
2005	keyText = ShortCutKeyArray( keyIndex )
2006
2007	AddNewKeyBinding( scriptURI, HasShiftKey( keyText ), HasControlKey( keyText ), ExtractKeyFromCombo( keyText ) )
2008
2009	KeyComboListener()
2010end sub
2011
2012
2013sub KeyDeleteButton()
2014
2015	keyShortCutList = bindingDialog.getControl( &quot;KeyList&quot; )
2016	selectedShortCut = keyShortCutList.getSelectedItem()
2017	combo = bindingDialog.getControl( &quot;KeyCombo&quot; )
2018
2019	keyGroup = combo.text
2020	dim keyGroupIndex as Integer
2021	dim selectedKeyIndex as Integer
2022	for n = lbound ( allKeyGroupsArray() ) to ubound ( allKeyGroupsArray() )
2023		if ( allKeyGroupsArray( n ) = keyGroup )then
2024			keyGroupIndex = n
2025			exit for
2026		end if
2027	next n
2028	selectedKeyIndex = keyShortCutList.getSelectedItemPos()
2029	linePosition = keyAllocationMap( keyGroupIndex, selectedKeyIndex ).Value
2030	keyAllocationMap( keyGroupIndex, selectedKeyIndex ).Value = 0
2031	keyAllocationMap( keyGroupIndex, selectedKeyIndex ).Name = &quot;&quot;
2032	RemoveBinding( linePosition )
2033	KeyComboListener()
2034end sub
2035
2036
2037sub EventNewButton()
2038	eventScriptList = bindingDialog.getControl( &quot;ScriptList&quot; )
2039	selectedScript = eventScriptList.getSelectedItem()
2040	scriptURI = getScriptURI( selectedScript )
2041	eventList = bindingDialog.getControl( &quot;EventList&quot; )
2042	eventPosition = eventList.getSelectedItemPos()
2043
2044	allApps = bindingDialog.getControl( &quot;AllAppsOption&quot; )
2045	dim isApp as boolean
2046	if allApps.state = true then &apos;Application
2047		isApp = true
2048	else &apos;Document
2049		isApp = false
2050	end if
2051	AddNewEventBinding( scriptURI, eventPosition, isApp )
2052
2053	populateEventList( eventPosition )
2054	EventListListener()
2055end sub
2056
2057
2058sub EventDeleteButton()
2059	eventList = bindingDialog.getControl( &quot;EventList&quot; )
2060	REM Check that combo is a script
2061	eventPosition = eventList.getSelectedItemPos()
2062
2063	allApps = bindingDialog.getControl( &quot;AllAppsOption&quot; )
2064	if allApps.state = true then &apos;Application
2065		linePosition = allEventTypesApp( eventPosition ).Value
2066		&apos;dim eventProp as new com.sun.star.beans.PropertyValue
2067		&apos;eventProp.Name = &quot;&quot;
2068		&apos;eventProp.Value = 0
2069		allEventTypesApp( eventPosition ).Name = &quot;&quot;
2070		allEventTypesApp( eventPosition ).Value = 0
2071		RemoveBinding( linePosition )
2072	else &apos;Document
2073		&apos;DeleteEvent( allEventTypes( eventPosition ) )
2074		allEventTypesDoc( eventPosition ).Name = &quot;&quot;
2075		allEventTypesDoc( eventPosition ).Value = 0
2076	end if
2077
2078	PopulateEventList( eventPosition )
2079	EventListListener()
2080end sub
2081
2082
2083sub EventOKButton
2084	WriteEventsToDoc()
2085	WriteXMLFromArray()
2086	bindingDialog.endExecute()
2087end sub
2088
2089
2090sub HelpOKButton()
2091	helpDialog.endExecute()
2092end sub
2093</script:module>
2094