[Solved] Persisting a registered event change in a document.

Creating a macro - Writing a Script - Using the API

[Solved] Persisting a registered event change in a document.

Postby pitonyak » Fri Sep 02, 2011 4:49 am

Suppose I want to know what events are available globally for documents, or for a specific document, I can do something like this:
Code: Select all   Expand viewCollapse view
Sub DisplayAvailableEvents
  Dim oGEB   ' GlobalEventBroadcaster
  Dim oDoc
  Dim s$
  Dim oText
 
  oDoc = ThisComponent
  'oDoc = StarDesktop.LoadComponentFromUrl("private:factory/swriter", "", 0, Array())
  oGEB = createUnoservice("com.sun.star.frame.GlobalEventBroadcaster")
  s = Join(oGEB.Events.getElementNames(), CHR$(10))
  oText = oDoc.Text
  oText.InsertString(oText.End, "===Global Events" & CHR$(10), False)
  oText.InsertString(oText.End, s, False)

  s = Join(oDoc.Events.getElementNames(), CHR$(10))
  oText.InsertString(oText.End, CHR$(10) & CHR$(10) & "===Writer Events" & CHR$(10), False)
  oText.InsertString(oText.End, s, False)
End Sub


I see that the following events are available for my specific document:
  • OnStartApp
  • OnCloseApp
  • OnCreate
  • OnNew
  • OnLoadFinished
  • OnLoad
  • OnPrepareUnload
  • OnUnload
  • OnSave
  • OnSaveDone
  • OnSaveFailed
  • OnSaveAs
  • OnSaveAsDone
  • OnSaveAsFailed
  • OnCopyTo
  • OnCopyToDone
  • OnCopyToFailed
  • OnFocus
  • OnUnfocus
  • OnPrint
  • OnViewCreated
  • OnPrepareViewClosing
  • OnViewClosed
  • OnModifyChanged
  • OnTitleChanged
  • OnVisAreaChanged
  • OnModeChanged
  • OnStorageChanged
  • OnPageCountChange
  • OnMailMerge
  • OnMailMergeFinished
  • OnFieldMerge
  • OnFieldMergeFinished
  • OnLayoutFinished

So, now, I decide that I will replace the standard "OnSave" event handler with one that is stored in a My Macros (in the OpenOffice.org library container).

Code: Select all   Expand viewCollapse view
Sub StealAnEvent
  Dim mEventProps(1) as new com.sun.star.beans.PropertyValue
  mEventProps(0).Name = "EventType"
  mEventProps(0).Value = "StarBasic"
  mEventProps(1).Name = "Script"
  mEventProps(1).Value = "macro:///Standard.Module1.MySave()"
 

  'oGlobalEventBroadcaster = createUnoservice("com.sun.star.frame.GlobalEventBroadcaster")
  'oGlobalEventBroadcaster.Events.ReplaceByName("OnStartApp", mEventProps())
  ThisComponent.Events.ReplaceByName("OnSave", mEventProps())
End Sub


This works great. I can close the document, then open the document again, and everything is wired in great. So, I decide to move the macro INTO my document. So, first I try the format that I would use if I were wiring in a button on a form and I set the value to:

Code: Select all   Expand viewCollapse view
mEventProps(1).Value = "vnd.sun.star.script:Standard.Module1.LocalMySave?language=Basic&location=document"


Oops, that does not work. So I try this:

Code: Select all   Expand viewCollapse view
mEventProps(1).Value = "macro://MyDocumentName/Standard.Module1.LocalMySave()"


Yeah! that works. Well, it works until I close the document and then open the document again. Any thoughts on a simple way to hook into this.

As a side note, even though I do this, the document still saves just fine, which probably would not happen if I tried to intercept the global dispatch. Couple of issues with that. I think that I would need to do my desired behavior, AND, I would also need to trigger the usual save behavior. Oh, and if i did that, I doubt that the change would persist of a document change (but I did not test it, since it is not really what I wanted to do).

Any thoughts?
Last edited by pitonyak on Sat Sep 10, 2011 5:52 am, edited 1 time in total.
Andrew Pitonyak
http://www.pitonyak.org/oo.php
LO and AOO on Fedora
pitonyak
Volunteer
 
Posts: 186
Joined: Sun Oct 07, 2007 9:13 pm
Location: Columbus, Ohio, USA

Re: Persisting a registered event change in a document.

Postby Villeroy » Fri Sep 02, 2011 8:27 am

As far as I know, we can register our own components as document event handlers. As we all know, components can not be written in Basic.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 27287
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Persisting a registered event change in a docum

Postby pitonyak » Sat Sep 10, 2011 5:54 am

I will call it a bug that the handler is not persisted with a save when the handler is contained in the document as opposed to saving the handler in "My Macros". I can even speculate as to why, but, I hate to do so when I have not yet poked through the code.
Andrew Pitonyak
http://www.pitonyak.org/oo.php
LO and AOO on Fedora
pitonyak
Volunteer
 
Posts: 186
Joined: Sun Oct 07, 2007 9:13 pm
Location: Columbus, Ohio, USA

Re: [Solved] Persisting a registered event change in a docum

Postby Villeroy » Sat Sep 10, 2011 12:56 pm

This is a job.xcu registered together with an addon.xcu to load some component (an object, nothing Basic) which intecepts OnViewCreated and OnViewClosed permanently.
Code: Select all   Expand viewCollapse view
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE oor:component-data SYSTEM "../../../../component-update.dtd">
<oor:component-data oor:name="Jobs" oor:package="org.openoffice.Office" xmlns:oor="http://openoffice.org/2001/registry" x
  <node oor:name="Jobs">
    <node oor:name="InterceptJob" oor:op="replace">
      <prop oor:name="Service">
        <value>name.AndreasSaeger.CalcInterceptor.InterceptJob</value>
      </prop>
    </node>
  </node>
  <node oor:name="Events">
    <node oor:name="OnViewCreated" oor:op="replace">
      <node oor:name="JobList">
        <node oor:name="InterceptJob" oor:op="replace"/>
      </node>
    </node>
    <node oor:name="OnViewClosed" oor:op="replace">
      <node oor:name="JobList">
        <node oor:name="InterceptJob" oor:op="replace"/>
      </node>
    </node>
  </node>
</oor:component-data>


This is the associated addon.xcu
Code: Select all   Expand viewCollapse view
<?xml version='1.0' encoding='UTF-8'?>
<oor:node xmlns:oor="http://openoffice.org/2001/registry" xmlns:xs="http://www.w3.org/2001/XMLSchema" oor:name="Addons" o
  <node oor:name="AddonUI">
    <node oor:name="OfficeMenuBar">
      <node oor:name="name.AndreasSaeger.CalcInterceptor.InterceptJob" oor:op="replace">
        <prop oor:name="Context" oor:type="xs:string">
          <value>com.sun.star.sheet.SpreadsheetDocument</value>
        </prop>
        <prop oor:name="Title" oor:type="xs:string">
          <value xml:lang="en">CalcIntercept</value>
        </prop>
        <prop oor:name="URL" oor:type="xs:string">
          <value>service:name.AndreasSaeger.CalcInterceptor.InterceptJob?HELP</value>
        </prop>
      </node>
    </node>
  </node>
</oor:node>
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 27287
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 4 guests