Beginning Work with Draw
As a means of teaching myself how to program OO Draw using OO Basic, I am trying to duplicate some of the functionality of a game mapping system I wrote some years ago using C++. I’m going to post my earliest material here just because it took me a bit of effort to find out how to do these simple things.
First, I wanted to monitor the mouse pointer for mouse clicks. This requires creating a MouseClickHandler, which the wonderful document from Andrew Pitonyak’s site, Useful Macro Information For OpenOffice. This is a tremendously useful document, and Andrew Pitonyak has also written a book, OpenOffice.org Macros Explained.
The rest comes from the StarOffice 8 Programming Guide for BASIC, another extremely useful document along with the API documentation at api.openoffice.org. I recommend working your way through both Andrew Pitonyak’s macro document and the Programming Guide before trying to understand the api docs. That’s not advice coming from on high–I’m still working my way up from totally confused to a bit less confused.
In any case, my intent with this code was to monitor the mouse click, and put the coordinates as text into a draw shape. Since I don’t see many Draw macros around the web, and none in the Code Snippets database, I figure it’s worth blogging this. When I get to where things are a bit more tested and more useful, I’ll submit some snippets myself.
In any case, here is the code:
REM ***** BASIC ***** Option Explicit Global oDocView as Object Global oPage As Object Global oMouseClickHandler As Object 'The MouseClickHandler routine is adapted in minor ways from Andrew Pitonyak's macro document 'available from http://www.pitonyak.com/AndrewMacro.odt 'Run RegisterMouseClickHandler and then simply click anywhere in the window. 'Mouse coordinates are not adjusted for the drawing area. That's something I'm working on Sub RegisterMouseClickHandler oDocView = StarDesktop.CurrentComponent.CurrentController oPage = StarDesktop.CurrentComponent.DrawPages(0) oMouseClickHandler = _ createUnoListener("eneMap_", "com.sun.star.awt.XMouseClickHandler") oDocView.addMouseClickHandler(oMouseClickHandler) End Sub Sub UnregisterMouseClickHandler on error resume next oDocView.removeMouseClickHandler(oMouseClickHandler) on error goto 0 End Sub Sub eneMap_disposing(oEvt) End Sub Function eneMap_mousePressed(oEvt) As Boolean eneMap_mousePressed = False End Function Function eneMap_mouseReleased(oEvt) As Boolean Dim sMsg As String With oEvt 'If there are any modifiers or the click is with the right button, we ignore. If .Modifiers = 0 and .Buttons = 1 and .ClickCount = 1 and .PopupTrigger = 0 Then sMsg = "X = " & .X & " / " & "Y = " & .Y 'Set the mouse coordinates as the text for shape "Info" which is on the drawing SetShapeText("Info",sMsg) End If End With eneMap_mouseReleased = False End Function 'This function goes through the list of draw shapes and puts the info text in the one labeled info. 'This is obviously not a very efficient approach, but I haven't found anything to locate shapes by 'label, and I also wanted to show how one can get to the shapes. At least to me it wasn't obvious. Function SetShapeText(sLabel as String, sText as String) Dim iCnt,i as Integer Dim oShape as Object If oPage.hasElements() = True Then iCnt = oPage.getCount() - 1 For i=0 to iCnt oShape = oPage.getByIndex(i) If oShape.Name = sLabel Then oShape.String = sText Exit Function End If Next i End If End Function
The comments should help. There must be a shape with the name “Info” for this to work. Here is the whole test drawing as it stands so you can download it if desired.
Very interesting macro.
You mentioned that you could not find many Draw macros. The Draw/Impress chapter from my published book is available as a free download from the publisher. Just thought that I would mention it!
Thanks! Your book is on my "buy" list, and I'll look for the free chapter. Moving from VBA to OO Basic is an interesting task.