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