OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Document model, controller and frame

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 4021
Location: Lawrence, Kansas, USA

PostPosted: Tue Jan 20, 2004 9:38 am    Post subject: Document model, controller and frame Reply with quote

There are three objects that you typically associate with a document. There is:
1. the document model
2. the document controller
3. the document frame.

When you need to do something to a document, you need one of these three objects. Which one of the three you need depends on what you intend to do.

Calling loadComponentFromURL() always gives you the document model object. OOo Basic also has a "variable" called ThisComponent which returns the document model of the document that contains the currently running macro code. So using either technique: loadComponentFromURL() or ThisComponent, always results in the document model.

Sometimes, for instnace, to use the Dispatcher, you need the document frame. Other times, you need the document controller (for instance, to manipulate which shapes of a drawing are selected).

The following three functions from my library are how I easily move between the three different objects.



Code:
'############################################################
'   API navigation convenience
'############################################################


'----------
' This will always return the document's controller.
' Pass in any one of...
'   * the document's model (subclass of com.sun.star.document.OfficeDocument)
'   * the document's controller
'   * the document's frame
Function GetDocumentController( oDoc As Object ) As Object
   Dim oCtrl As Object
   
   ' If the caller gave us the document model...
   If oDoc.supportsService( "com.sun.star.document.OfficeDocument" ) Then
      ' ...then get the controller from that.
      oCtrl = oDoc.getCurrentController()

   ' If the caller gave us a document controller...
   ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XController" ) Then
      ' ...thanks!  That's just what we wanted!
      oCtrl = oDoc
   
   ' If the caller gave us the document frame...
   ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XFrame" ) Then
      oFrame = oDoc
      ' ...then get the controller from the frame.
      oCtrl = oFrame.getController()
   
   Else
      ' The caller did not give us what we expected!
      MsgBox( "GetDocController called with incorrect parameter." )
   EndIf
   
   GetDocumentController() = oCtrl
End Function


'----------
' This will always return the document's frame.
' Pass in any one of...
'   * the document's model (subclass of com.sun.star.document.OfficeDocument)
'   * the document's controller
'   * the document's frame
Function GetDocumentFrame( oDoc As Object ) As Object
   Dim oFrame As Object
   
   ' If the caller gave us the document model...
   If oDoc.supportsService( "com.sun.star.document.OfficeDocument" ) Then
      ' ...then get the controller from that.
      oCtrl = oDoc.getCurrentController()
      ' ...then get the frame from the controller.
      oFrame = oCtrl.getFrame()

   ' If the caller gave us a document controller...
   ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XController" ) Then
      oCtrl = oDoc
      ' ...then get the frame from the controller.
      oFrame = oCtrl.getFrame()
   
   ' If the caller gave us the document frame...
   ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XFrame" ) Then
      ' ...thanks!  That's just what we wanted!
      oFrame = oDoc
   
   Else
      ' The caller did not give us what we expected!
      MsgBox( "GetDocumentFrame called with incorrect parameter." )
   EndIf
   
   GetDocumentFrame() = oFrame
End Function


'----------
' This will always return the document's model.
' Pass in any one of...
'   * the document's model (subclass of com.sun.star.document.OfficeDocument)
'   * the document's controller
'   * the document's frame
Function GetDocumentModel( oDoc As Object ) As Object
   Dim oDocModel As Object
   
   ' If the caller gave us the document model...
   If oDoc.supportsService( "com.sun.star.document.OfficeDocument" ) Then
      ' ...thanks!  That's just what we wanted!
      oDocModel = oDoc

   ' If the caller gave us a document controller...
   ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XController" ) Then
      oCtrl = oDoc
      ' ...then get the model from the controller.
      oDocModel = oCtrl.getModel()
   
   ' If the caller gave us the document frame...
   ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XFrame" ) Then
      oFrame = oDoc
      ' ...then get the controller from the frame.
      oCtrl = oFrame.getController()
      ' ...then get the model from the controller.
      oDocModel = oCtrl.getModel()
      
   Else
      ' The caller did not give us what we expected!
      MsgBox( "GetDocumentModel called with incorrect parameter." )
   EndIf
   
   GetDocumentModel() = oDocModel
End Function



Now I can do something like...

Code:
oDoc = loadComponentFromURL( ..... )


Then later, I need the frame, I can just call...

Code:
oDocFrame = GetDocumentFrame( oDoc )


The above code provides three functions...

Code:
Function GetDocumentModel     ( oDoc As Object ) As Object
Function GetDocumentController( oDoc As Object ) As Object
Function GetDocumentFrame     ( oDoc As Object ) As Object


All three functions accept any of the model, frame or controller. Each function will "convert" to the desired object. For instance, if I need the model, I'll call GetDocumentModel(), but I can pass it the model, the frame or the controller. (Obviously, if I pass the document model to GetDocumentModel(), it doesn't have to do anything but just return what I gave it.)

Where these routines are really useful is in writing more general subroutines. I could write a routine to, say, copy to the clipboard. The routine takes a single parameter oDoc. But you could pass in any of the three objects (model, controller, frame). This makes my clipboardCopy() function easier to call. You just pass it whichever of the three document objects that you conveniently have on hand. If internally, the clipboardCopy() function needs a Frame, then it will use my GetDocumentFrame() function to get the frame from whichever one of the three you passed it.

Hope this is helpful to someone.


2005-04-02
The latest version of the code in this thread is at....
http://www.oooforum.org/forum/viewtopic.phtml?p=73054#73054
_________________
Want to make OOo Drawings like the colored flower design to the left?


Last edited by DannyB on Sat Apr 02, 2005 1:21 pm; edited 1 time in total
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 4021
Location: Lawrence, Kansas, USA

PostPosted: Mon Jun 21, 2004 6:31 am    Post subject: Reply with quote

Here is some additional information....
http://www.oooforum.org/forum/viewtopic.php?p=37485#37485
http://www.oooforum.org/forum/viewtopic.php?p=37399#37399
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group