Branch libreoffice-5-0-4
[LibreOffice.git] / scripting / examples / basic / InsertColouredText.xba
blobe97b5dfd8a87d674d0acf8977f3d9f679b470779
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <!--
4 * This file is part of the LibreOffice project.
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 * This file incorporates work covered by the following license notice:
12 * Licensed to the Apache Software Foundation (ASF) under one or more
13 * contributor license agreements. See the NOTICE file distributed
14 * with this work for additional information regarding copyright
15 * ownership. The ASF licenses this file to you under the Apache
16 * License, Version 2.0 (the "License"); you may not use this file
17 * except in compliance with the License. You may obtain a copy of
18 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 -->
20 <script:module xmlns:script="http://openoffice.org/2000/script" script:name="InsertColouredText" script:language="StarBasic">&apos; ***
21 &apos; InsertColouredText basic script
22 &apos; Uses a user interface to insert text of a specified colour to the
23 &apos; start and end of a document
24 &apos;
25 &apos; author Neil Montgomery
26 &apos; created August 12, 2002
27 &apos; ***
30 &apos; Main subprocedure to start script
31 Sub Main
32 dialogShow()
33 End Sub
36 &apos; Global reference to the dialog object
37 Dim oDialog as Object
40 &apos; Uses the loadDialog subprocedure to load and execute the dialog box
41 Sub dialogShow
42 oDialog = loadDialog(&quot;Standard&quot;,&quot;InsertColouredTextDialog&quot;)
43 oDialog.execute()
44 End Sub
47 &apos; ***
48 &apos; Loads the dialog from the dialog library
49 &apos;
50 &apos; param Libname the library name where dialog is stored
51 &apos; param DialogName the name of the dialog
52 &apos; param oLibContainer library container to hold the loaded dialog library (optional)
53 &apos; return runtime dialog object
54 &apos; ***
55 Function loadDialog(Libname as String, DialogName as String, Optional oLibContainer)
56 Dim oLib as Object
57 Dim oLibDialog as Object
58 Dim oRuntimeDialog as Object
60 &apos; If the optional oLibContainer is not passed to the function then
61 &apos; DialogLibraries is loaded by default
62 If isMissing(oLibContainer ) then
63 oLibContainer = DialogLibraries
64 End If
66 &apos; Loads the specified library, then loads the dialog
67 oLibContainer.loadLibrary(LibName)
68 oLib = oLibContainer.getByName(Libname)
69 oLibDialog = oLib.getByName(DialogName)
70 oRuntimeDialog = createUnoDialog(oLibDialog)
72 &apos; Returns the runtime dialog object
73 loadDialog() = oRuntimeDialog
74 End Function
78 &apos; ***
79 &apos; Gets the RGB integer values and new text string from the dialog
80 &apos; then writes the new coloured text to the start and end of the document
81 &apos;
82 &apos; ***
83 Sub getFromDialog
84 Dim oDocument As Object
85 Dim oText As Object
86 Dim oCursor As Object
88 &apos; Create a document object for the current document then create text and
89 &apos; cursor objects
90 oDocument = StarDesktop.ActiveFrame.Controller.Model
91 oText = oDocument.Text
92 oCursor = oText.createTextCursor()
94 &apos; Write the coloured text to the start and end of the document
95 oCursor.gotoStart(false)
96 oCursor.CharColor = getColor()
97 oCursor.setString(&quot;New text at start: &quot; + getNewText())
98 oCursor.gotoEnd(false)
99 oCursor.CharColor = getColor()
100 oCursor.setString(&quot;New text at end: &quot; + getNewText())
101 End Sub
105 &apos; ***
106 &apos; Reads the RGB integer values from the dialog
107 &apos;
108 &apos; returns long representing the RGB value
109 &apos; ***
110 Function getColor() as Long
111 Dim oRedText as Object
112 Dim oGreenText as Object
113 Dim oBlueText as Object
114 Dim nColor As Long
116 &apos; Get the three RGB values
117 oRedText = oDialog.GetControl(&quot;RedTextBox&quot;)
118 oGreenText = oDialog.GetControl(&quot;GreenTextBox&quot;)
119 oBlueText = oDialog.GetControl(&quot;BlueTextBox&quot;)
121 &apos; Convert the values to long type and return the value
122 nColor = RGB(oRedText.Text,oGreenText.Text,oBlueText.Text)
123 getColor = nColor
124 End Function
128 &apos; ***
129 &apos; Reads the new text from the dialog
130 &apos;
131 &apos; returns string the new text
132 &apos; ***
133 Function getNewText() as String
134 Dim oNewText As Object
135 Dim sNewText As String
137 &apos; Gets the string from dialog and returns the new text
138 oNewText = oDialog.GetControl(&quot;NewTextBox&quot;)
139 sNewText = oNewText.Text
140 getNewText = sNewText
141 End Function</script:module>