[[Category:OOBasicCode]]
site: 
autor: DannyB
	
It is interesting to look at all the stuff in the API under....

com.sun.star.awt.*


It is possible to create new windows, add menu bars to them, and draw any kind of graphics within your window. You can put new controls in your window.

In fact, a window created by a component using the AWT toolkit would look and feel just like the native windows of Calc, Writer, Impress, Draw.

Here is a fun test program to exercise what I have learned there. Just copy, paste and run. I apologize that this is way more comments than actual lines of code. The code would be quite short if you removed the comments.

<pre>
Sub Main
   ' Get an AWT toolkit.
   ' OOo's AWT, not a Java AWT.
   oAwtToolkit = CreateUnoService( "com.sun.star.awt.Toolkit" )
   
   ' Create a top level window.
   oWindow = CreateTopWindow( oAwtToolkit )
   
   ' At this point, you should check out the interfaces
   '  and all of the methods that they give you for
   '  manipulating this new oWindow.
   ' See comment in CreateTopWindow() below.
   
   ' Change the background color of the window to baby blue.
   oWindow.setBackground( RGB( 240, 240, 255 ) )
   
   ' At this point, if you stop the program, you will have a
   '  new OOo window on the screen, but you cannot do anything
   '  with it.  You cannot even close it!
   ' In fact, you have to kill the OOo process, as even OOo
   '  cannot close this window since it has no Frame and
   '  therefore is not integrated into the desktop environment.
   ' So I had to go on writing additional code....
   
   ' Create a new frame.
   oFrame = CreateUnoService( "com.sun.star.frame.Frame" )
   ' Initialize this frame with our new window.
   oFrame.initialize( oWindow )
   ' Tell the frame that its parent is the Desktop.
   oFrame.setCreator( StarDesktop )
   
   ' At this point you have a window that can be resized, moved, or closed.
   
   ' Note that if you wanted to, you could now load a component
   '  into your newly created window!
'   oFrame.loadComponentFromURL( "private:factory/scalc", "_self", 0, Array() )
   
   ' Create a new button.
   oBtnCtrl = MakeButtonCtrl( oAwtToolkit, oWindow, "Meow",_
         MakeRectangle( 20, 30, 100, 40 ) )
   
   ' Create a menu.
'   CreateMenuBar( oWindow )
' menubar to be provided later....
' okay, well maybe not, see below...
End Sub




Function CreateTopWindow( oAwtToolkit )
   oWindowDesc = CreateUnoStruct( "com.sun.star.awt.WindowDescriptor" )
   With oWindowDesc
      ' specifies a top level window on the desktop. It is also a container.
      .Type = com.sun.star.awt.WindowClass.TOP
      ' is a modal top level window on the desktop. It is also a container.
'      .Type = com.sun.star.awt.WindowClass.MODALTOP
      ' is a container that may contain other components. It is not a top window.
'      .Type = com.sun.star.awt.WindowClass.CONTAINER
      ' is the simplest window. It can be a container.
'      .Type = com.sun.star.awt.WindowClass.SIMPLE
      
      ' specifies the name of the component service.
      ' A zero length name means that the vcl creates a blank top,
      '  a container, or a simple window.
      .WindowServiceName = ""
      
      ' specifies the parent of the component.
      ' If Parent == 0 && ParentIndex == -1 , then the window is on the desktop.
'      .Parent = 0
'      .Parent = createUnoValue( "com.sun.star.awt.XWindowPeer", 0 )
      .Parent = oAwtToolkit.getDesktopWindow()
      ' specifies the index of the parent window, if available.
      ' If Parent == 0 and this struct is a member of an array,
      '  then this is the offset from the beginning of the array to the parent.
      '  A value of -1 means desktop.
      .ParentIndex = -1
      
      ' specifies the position and size of the window.
      ' This member is ignored if the window attribute is
      '  WindowAttribute::FULLSIZE .
      .Bounds = MakeRectangle( 100, 200, 300, 400 )
      
      ' specifies the window attributes.
      .WindowAttributes = 0
      ' specifies that the window is initially visible.
      .WindowAttributes = .WindowAttributes + com.sun.star.awt.WindowAttribute.SHOW
      ' specifies that the window fills the complete desktop area.
'      .WindowAttributes = .WindowAttributes + com.sun.star.awt.WindowAttribute.FULLSIZE
      ' specifies that the window is optimum size.
'      .WindowAttributes = .WindowAttributes + com.sun.star.awt.WindowAttribute.OPTIMUMSIZE
      ' specifies that the window is minimum size.
'      .WindowAttributes = .WindowAttributes + com.sun.star.awt.WindowAttribute.MINSIZE
      ' specifies that the window has visible borders.
      .WindowAttributes = .WindowAttributes + com.sun.star.awt.WindowAttribute.BORDER
      ' specifies that the size of the window can be changed by the user.
      .WindowAttributes = .WindowAttributes + com.sun.star.awt.WindowAttribute.SIZEABLE
      ' specifies that the window can be moved by the user.
      .WindowAttributes = .WindowAttributes + com.sun.star.awt.WindowAttribute.MOVEABLE
      ' specifies that the window can be closed by the user.
      .WindowAttributes = .WindowAttributes + com.sun.star.awt.WindowAttribute.CLOSEABLE
      '[ DEPRECATED ] specifies that the window should support the XSystemDependentWindowPeer interface.
'      .WindowAttributes = .WindowAttributes + com.sun.star.awt.WindowAttribute.SYSTEMDEPENDENT
   End With
   
   oXWindowPeer = oAwtToolkit.createWindow( oWindowDesc )
   
   ' Look at the capabilities that these interfaces give you
   '  for manipulating your newly created window.
   ' Interfaces supported by oXWindowPeer...
   '  com.sun.star.awt.XTopWindow
   '  com.sun.star.awt.XWindow
   '  com.sun.star.awt.XWindowPeer
   '  com.sun.star.awt.XVclContainer
   '  com.sun.star.awt.XVclContainerPeer
   '  com.sun.star.awt.XVclWindowPeer
   '  com.sun.star.awt.XLayoutConstraints
   '  com.sun.star.awt.XView
   '  com.sun.star.awt.XDevice
   '  com.sun.star.lang.XEventListener
   '  com.sun.star.lang.XComponent
   '  com.sun.star.lang.XTypeProvider
   '  com.sun.star.accessibility.XAccessible
   '
   ' Properties...
   '  <object> MenuBar
   '  <array>  Windows
   '  <array>  Group
   '  <object> PosSize
   '  Boolean  Visible
   '  Boolean  Enable
   '  <object> Toolkit
   '  <object> Pointer
   '  Long     Background
   '  Boolean  DesignMode
   '  Long     Foreground
   '  <object> ControlFont
   '  <object> MinimumSize
   '  <object> PreferredSize
   '  <object> AccessibleContext
   '  <object> Graphics
   '  <object> Size
   '  <object> Info
   '  <array>  FontDescriptors
   
   CreateTopWindow = oXWindowPeer
End Function





Function MakeButtonCtrl( oAwtToolkit, oWindow, Optional cLabel, Optional oPosSizeRect )
   ' Create a new button model.
   oButtonModel = CreateUnoService( "com.sun.star.awt.UnoControlButtonModel" )
   
   ' Create a new button control.
   oButtonCtrl = CreateUnoService( "com.sun.star.awt.UnoControlButton" )
   
   ' Tell the control that it has a model.
   oButtonCtrl.setModel( oButtonModel )
   
   ' Tell the control to create its window peer.
   ' (To understand what this means, it is helpful to
   '  read some Java documention about how Java's AWT works
   '  even though we're not working with Java AWT here.)
   ' Basically, this puts something visible onto the window.
   oButtonCtrl.createPeer( oAwtToolkit, oWindow )
   
   If Not IsMissing( cName ) Then
      oButtonModel.Label = cLabel
   EndIf
   If Not IsMissing( oPosSizeRect ) Then
      oButtonCtrl.setPosSize( _
                  oPosSizeRect.X, oPosSizeRect.Y,_
                  oPosSizeRect.Width, oPosSizeRect.Height,_
                  com.sun.star.awt.PosSize.POSSIZE )
   EndIf
   
   MakeButtonCtrl = oButtonCtrl
End Function


' ripped from my library....

Function MakeRectangle( ByVal nX As Long, ByVal nY As Long,_
                  ByVal nWidth As Long, ByVal nHeight As Long ) As com.sun.star.awt.Rectangle
   oRectangle = createUnoStruct( "com.sun.star.awt.Rectangle" )
   With oRectangle
      .X = nX
      .Y = nY
      .Width = nWidth
      .Height = nHeight
   End With
   MakeRectangle() = oRectangle
End Function
</pre>

When I provide the MenuBar implementation, it will be painfully obvious that you need a real programming language (i.e. not Basic). Well, this is probably true if you are going to implement a full blown office component anyway.

Here is my proposed menubar implementation. But it doesn't seem to work properly in Basic.

I believe the problem is that you cannot setMenuBar() to an object created by CreateUnoListener. Like I said, you need a language that lets you create real objects.

If you study it, I think you can see the idea behind it.

<pre>	

'##################################################

Global oAppleMenu
Global oOrangeMenu

Sub CreateMenuBar( oWindow )
   oMenubar = CreateUnoListener( "MyMenuBar_", "com.sun.star.awt.XMenu" )
   oAppleMenu = CreateUnoListener( "AppleMenu_", "com.sun.star.awt.XPopupMenu" )
   oOrangeMenu = CreateUnoListener( "OrangeMenu_", "com.sun.star.awt.XPopupMenu" )
   oWindow.setMenuBar( oMenubar )
End Sub

' Our menu bar is hardcoded.
' Menu: Apple
'  has items:  Apple Pie, Apple Juice
' Menu: Orange
'  has items:  Orange Juice, Orange JellO

'##################################################
'   My Menu Bar interface
'
'##################################################
'   com.sun.star.awt.XMenuBar
'
'   An interface like this just plain needs to
'    be written in a language other than Baisc!
'##################################################

Sub MyMenuBar_addMenuListener( xListener )
End Sub

Sub MyMenuBar_removeMenuListener( xListener )
End Sub

' This trivial implementation of the interface
'  does NOT support inserting or removing items.

Sub MyMenuBar_insertItem( nItemId, cText, nItemStyle, nPos )
End Sub

Sub MyMenuBar_removeItem( nPos, nCount )
End Sub

' This joke implementation of the interface
'  has hardcoded the contents of the menu bar.
' Calling insertItem or removeItem does not change
'  what the following routines return.

Function MyMenuBar_getItemCount()
   ' We have two menus.
   MyMenuBar_getItemCount = 2
End Function

Function MyMenuBar_getItemId( nPos )
   MyMenuBar_getItemId = nPos
End Function

Function MyMenuBar_getItemPos( nItemId )
   MyMenuBar_getItemPos = nItemId
End Function

Sub MyMenuBar_enableItem( nItemId, bEnable )
End Sub

Function MyMenuBar_isItemEnabled( nItemId )
   MyMenuBar_isItemEnabled = True
End Function

Sub MyMenuBar_setItemText( nItemId, cText )
End Sub

Function MyMenuBar_getItemText( nItemId )
   cItemName = ""
   If nItemId = 1 Then
      cItemName = "Apple"
   ElseIf nItemId = 2 Then
      cItemName = "Orange"
   EndIf
   MyMenuBar_getItemText = cItemName
End Function

Sub MyMenuBar_setPopupMenu( nItemId, oXPopupMenu )
End Sub

Function MyMenuBar_getPopupMenu( nItemId )
   Dim oPopupMenu As Object
   If nItemId = 1 Then
      oPopupMenu = oAppleMenu
   ElseIf nItemId = 2 Then
      oPopupMenu = oOrangeMenu
   EndIf
   MyMenuBar_getPopupMenu = oPopupMenu
End Function


'##################################################
'   com.sun.star.awt.XPopupMenu
'##################################################

Sub AppleMenu_addMenuListener( xListener )
End Sub

Sub AppleMenu_removeMenuListener( xListener )
End Sub

' This trivial implementation of the interface
'  does NOT support inserting or removing items.

Sub AppleMenu_insertItem( nItemId, cText, nItemStyle, nPos )
End Sub

Sub AppleMenu_removeItem( nPos, nCount )
End Sub

' This joke implementation of the interface
'  has hardcoded the contents of the menu bar.
' Calling insertItem or removeItem does not change
'  what the following routines return.

Function AppleMenu_getItemCount()
   ' We have two menus.
   AppleMenu_getItemCount = 2
End Function

Function AppleMenu_getItemId( nPos )
   AppleMenu_getItemId = nPos
End Function

Function AppleMenu_getItemPos( nItemId )
   AppleMenu_getItemPos = nItemId
End Function

Sub AppleMenu_enableItem( nItemId, bEnable )
End Sub

Function AppleMenu_isItemEnabled( nItemId )
   AppleMenu_isItemEnabled = True
End Function

Sub AppleMenu_setItemText( nItemId, cText )
End Sub

Function AppleMenu_getItemText( nItemId )
   cItemName = ""
   If nItemId = 1 Then
      cItemName = "Apple Pie"
   ElseIf nItemId = 2 Then
      cItemName = "Apple Juice"
   EndIf
   AppleMenu_getItemText = cItemName
End Function

Sub AppleMenu_setPopupMenu( nItemId, oXPopupMenu )
End Sub

Function AppleMenu_getPopupMenu( nItemId )
   Dim oPopupMenu As Object
   AppleMenu_getPopupMenu = oPopupMenu
End Function

Sub AppleMenu_insertSeparator( nPos )
End Sub

Sub AppleMenu_setDefaultItem( nItemId )
End Sub

Function AppleMenu_getDefaultItem()
   AppleMenu_getDefaultItem = 1
End Function

Sub AppleMenu_checkItem( nItemId, bCheckmark )
End Sub

Function AppleMenu_isItemChecked( nItemId )
   AppleMenu_isItemChecked = False
End Function

Function AppleMenu_execute( oWindowPeerParent, oAreaRect, nDirection )
   AppleMenu_execute = 0
End Function



'##################################################
'   com.sun.star.awt.XPopupMenu
'##################################################

Sub OrangeMenu_addMenuListener( xListener )
End Sub

Sub OrangeMenu_removeMenuListener( xListener )
End Sub

' This trivial implementation of the interface
'  does NOT support inserting or removing items.

Sub OrangeMenu_insertItem( nItemId, cText, nItemStyle, nPos )
End Sub

Sub OrangeMenu_removeItem( nPos, nCount )
End Sub

' This joke implementation of the interface
'  has hardcoded the contents of the menu bar.
' Calling insertItem or removeItem does not change
'  what the following routines return.

Function OrangeMenu_getItemCount()
   ' We have two menus.
   OrangeMenu_getItemCount = 2
End Function

Function OrangeMenu_getItemId( nPos )
   OrangeMenu_getItemId = nPos
End Function

Function OrangeMenu_getItemPos( nItemId )
   OrangeMenu_getItemPos = nItemId
End Function

Sub OrangeMenu_enableItem( nItemId, bEnable )
End Sub

Function OrangeMenu_isItemEnabled( nItemId )
   OrangeMenu_isItemEnabled = True
End Function

Sub OrangeMenu_setItemText( nItemId, cText )
End Sub

Function OrangeMenu_getItemText( nItemId )
   cItemName = ""
   If nItemId = 1 Then
      cItemName = "Orange Juice"
   ElseIf nItemId = 2 Then
      cItemName = "Orange JellO"
   EndIf
   OrangeMenu_getItemText = cItemName
End Function

Sub OrangeMenu_setPopupMenu( nItemId, oXPopupMenu )
End Sub

Function OrangeMenu_getPopupMenu( nItemId )
   Dim oPopupMenu As Object
   OrangeMenu_getPopupMenu = oPopupMenu
End Function

Sub OrangeMenu_insertSeparator( nPos )
End Sub

Sub OrangeMenu_setDefaultItem( nItemId )
End Sub

Function OrangeMenu_getDefaultItem()
   OrangeMenu_getDefaultItem = 1
End Function

Sub OrangeMenu_checkItem( nItemId, bCheckmark )
End Sub

Function OrangeMenu_isItemChecked( nItemId )
   OrangeMenu_isItemChecked = False
End Function

Function OrangeMenu_execute( oWindowPeerParent, oAreaRect, nDirection )
   OrangeMenu_execute = 0
End Function
</pre>

'''Atención, probablemente tenga errores'''

your first macro stops at his position
' .Parent = 0
'.Parent = createUnoValue( "com.sun.star.awt.XWindowPeer", 0 )
.Parent = oAwtToolkit.getDesktopWindow()

with the error message

com.sun.star.lang.IllegalArgumentException
MEssage; illegal value given.

Thenew macro stops at
oWindow.setMenuBar( oMenubar )

with the message Argument is not optional.