Linux.com

Feature

OOoBasic crash course: Adding GUI goodness

By Dmitri Popov on March 21, 2007 (7:00:00 AM)

Share    Print    Comments   

Some OpenOffice.org macros have rudimentary dialog boxes that allow you to define a few parameters. If you're ready to take your macro programming skills to a new level, you can learn how to create graphical interfaces for your macros. Once you know how to do that, you can build advanced macros that are close to full-blown applications.

Previously, we created a simple macro that allows you to look up the currently selected word in online references. While the macro is useful as it is, it has one serious drawback: since the user can't interact with it, you need to create a separate routine for each online reference. This means that if you wish to have 10 online references, you are looking at creating 10 different macros where the only difference is the URL of the online references. Obviously, a more efficient solution would be a macro that allows users to choose a reference from a list and then pick up the appropriate URL.

OOoBasic keeps macros and dialogs (which act as the macros' GUI) separately, so to keep things tidy, start by creating a new library for a new lookup macro and its dialog. To do this, choose Tools -> Macros -> Organize Macros -> OpenOffice.org Basic, press the Organizer button, select My Macros, and create a new library called GUI. Press the Edit button to open the OOoBasic IDE.

Before you start writing the actual macro, you have to build a dialog. To do this, right-click on the Module1 tab at the bottom of the window and select Insert -> BASIC Dialog. This creates a new dialog and opens it for editing. Use the provided tools to create a simple dialog containing three elements: a list box and OK and Cancel buttons. Double-click on the list box and make sure that its name is ListBox1. In the List entries, enter the names of the references you want to use in the macro (for example, "Wikipedia," "Cambridge Dictionary," and "Google Define"). To see what the created dialog looks like in action, press the Test Mode button.

Once you're done with the dialog, you can write the macro. Switch to the Module1 tab, and, as always, start by defining variables:

  Sub LookupWithGUI()

  Dim Dialog As Object, Library As Object
  Dim TheDialog As Object, DialogField As Object
  Dim exitOK As String , CurrentItemPos As Integer

You will need the Dialog, Library, TheDialog, and DialogField variables to link the macro to the created dialog, initiate it, and point the macro to the right field. The CurrentItemPos variable will be used to store the position of the item selected from the list.

The next step is to specify the URL of the references you want to use in the macro. There are several ways to do this; in this case, we use a simple array to store all the URLs:

  URLArray=Array("http://en.wikipedia.org/wiki/",_
  "http://dictionary.cambridge.org/results.asp?searchword=",_
  "http://www.google.com/search?q=define%3A")

We've used the next code block in almost every macro we've created so far in this series of articles, so it hardly needs any further explanation:

  ThisDoc=ThisComponent
  LookupWord=ThisDoc.CurrentController.getViewCursor

Next step is to point the macro to the created dialog:

  exitOK=com.sun.star.ui.dialogs.ExecutableDialogResults.OK
  Library=DialogLibraries.GetByName("GUI")
  TheDialog=Library.GetByName("Dialog1")

Screenshot
Click to enlarge
As you can see, linking the macro to the dialog is straightforward: first, the DialogLibraries.GetByName() function selects the appropriate library (GUI, in this case), then the Library.GetByName() function retrieves the name of the dialog (Dialog1). Once the macro knows which dialog to use, it must initiate it and choose the list box field:

  Dialog=CreateUnoDialog(TheDialog)
  DialogField=Dialog.GetControl("ListBox1")
  DialogField.SelectItemPos(1, True)

When the user selects an item from the list and clicks on the OK button, the macro obtains the position of the selected item:

  If Dialog.Execute = exitOK Then
  CurrentItemPos = DialogField.SelectedItemPos

Finally, the macro uses the position to get the correct URL from the URLArray and sends it to the browser, then disposes of the dialog window:

  Shell("C:\Program Files\Mozilla Firefox\firefox.exe",1, URLArray(CurrentItemPos) & LookupWord.String)
  End If

  Dialog.Dispose

  End Sub

And here is the final result:

  Sub LookupWithGUI()

  Dim Dialog As Object, Library As Object
  Dim TheDialog As Object, DialogField As Object
  Dim exitOK As String, CurrentItemPos As Integer

  URLArray=Array("http://en.wikipedia.org/wiki/",_
  "http://dictionary.cambridge.org/results.asp?searchword=",_
  "http://www.google.com/search?q=define%3A")

  ThisDoc=ThisComponent
  LookupWord=ThisDoc.CurrentController.getViewCursor

  exitOK=com.sun.star.ui.dialogs.ExecutableDialogResults.OK
  Library=DialogLibraries.GetByName("GUI")
  TheDialog=Library.GetByName("Dialog1")

  Dialog=CreateUnoDialog(TheDialog)
  DialogField=Dialog.GetControl("ListBox1")
  DialogField.SelectItemPos(1, True)

  If Dialog.Execute=exitOK Then
  CurrentItemPos=DialogField.SelectedItemPos
  Shell("C:\Program Files\Mozilla Firefox\firefox.exe",1, URLArray(CurrentItemPos) & LookupWord.String)
  End If

  Dialog.Dispose

  End Sub

With this GUI version of the macro, you don't have to write a new routine for every new online reference in the macro. Instead, simply add the reference's URL to the array, and add the related item to the list box in the dialog.

Dmitri Popov is a freelance writer whose articles have appeared in Russian, British, US, German, and Danish computer magazines.

Dmitri Popov is a freelance writer whose articles have appeared in Russian, British, US, German, and Danish computer magazines.

Share    Print    Comments   

Comments

on OOoBasic crash course: Adding GUI goodness

Note: Comments are owned by the poster. We are not responsible for their content.

vb

Posted by: Anonymous Coward on March 22, 2007 12:44 AM
why oh why is Visual Basicish syntax infecting OOo? (why is it called that anyway? isn't it just openoffice? where the heck is the other o coming from?)

ugh, stupid lameness filter, blah, blah, blah
Reason: please don't use so many caps. using caps is like yelling! wtf that wasn't enough?! christ, how much more do i have to type

moo, cow, baa sheep

#

Re:vb

Posted by: Anonymous Coward on March 22, 2007 08:31 AM
I agree, we do not need a vb infection in Open Office! OO can also do macros in Python and JavaScript.

Linux.com: Please publish a tutorial on Python in OO and join us in lobbying OO to dropping basic from its next version.

#

Python macros

Posted by: Administrator on March 23, 2007 04:25 PM
I agree: A tutorial in writing Python macros for OOo would be very good.

#

Re:vb

Posted by: Anonymous Coward on March 22, 2007 07:34 PM
OpenOffice is getting, and should have VBA compatibility for two reasons.
  • Open Office needs to be compatible with Microsoft Office the defacto standard and 800 pound gorilla in office suites. If it fails to adequately mimic MS Office it is pointless like Word Pro, Word Perfect, Abiword, and all the others that no longer exist.

  • OpenOffice needs a scripting language and the vast majority of people who will be using it are already familiar with VBA. This speeds adoption, reduces the learning curve and reduces cost.


(OpenOffice is casually referred to as OOo which stands for OpenOffice.org I don't like for it either but, it is easier to type and many many people take that shortcut.)

#

OOo

Posted by: Anonymous Coward on March 22, 2007 07:43 PM

It's not so much "casually" referred to as OOo... OpenOffice.org is the correct name of the software and OpenOffice is an unwelcome contraction of that name. OOo is a contraction of that name.


History lesson: the project was originally called Open Office (and why not?) but then Sun started getting a little nervous about the M$ trademark on Office (which is almost as silly as their trademark of Windows). Was "Open Office" treading on that trademark? The solution was to call the software suite OpenOffice.org.

#

Re:OOo

Posted by: Administrator on March 23, 2007 11:25 PM
OpenOffice.org explained (some years ago) on its web site that the more concise abbreviation was not available because it had been trademarked by a Korean company.

#

Ok cannot tell different between starbasic

Posted by: Anonymous Coward on March 24, 2007 06:47 PM
Starbasic is older the VB basic.

Qbasic is the basic its close to.

Not like everyone knows the difference.

#

Re:Example code assumes a Windows machine

Posted by: Anonymous Coward on April 10, 2007 08:32 PM
This even won't work on a german verison of windowze , where it should read: "c:\Programme\...."

Even on US windows installations, it might be "D:\Program Files\..." if one chooses to install windows into drive D:

It also assumes that firefox is installed there.

Conclusion: This is unportable code, that is to be run on a particular machine. It might run on similar machines (English Windowze Machines with windowze installes on C:, and firefox installed in the defualt location).

If i write a script for MY PERSONAL USE, i do use simmilar code. But i won`t release such code<nobr> <wbr></nobr>:O

#

Example code assumes a Windows machine

Posted by: Administrator on March 31, 2007 02:45 AM
I don't think the following line will work on a Linux system:
<tt>Shell("C:\Program Files\Mozilla Firefox\firefox.exe",1, URLArray(CurrentItemPos) & LookupWord.String)</tt>
Is there a cross-platform way to launch a web browser from OOo?

#

ListBox values

Posted by: oz123 on March 06, 2008 12:01 PM
I tried following this tutorial.
However, the values of the ListBox are not separated. See pictures:

[URL=http://img524.imageshack.us/my.php?image=screenshotvclsalframewo2.png][img]http://img524.imageshack.us/img524/4561/screenshotvclsalframewo2.th.png[/img][/URL]

[URL=http://img301.imageshack.us/my.php?image=screenshotpropertiesliskh3.png][img]http://img301.imageshack.us/img301/1148/screenshotpropertiesliskh3.th.png[/img][/URL]

What is the right syntax to add values and separate them so each values will be selected in a new line (similar to the search engine dialog (or listbox) in Firefox)

10x.
[Modified by: oz123 on March 06, 2008 12:01 PM]

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya