AWT Dialog-clone

Creating Extension - Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This forum is not for asking questions about writing your own macros.

AWT Dialog-clone

Postby JeJe » Sat Mar 13, 2021 5:16 pm

'The OOBasic IDE doesn't implement dialog resizing.
'One solution is to create a AWT dialog which does resize
'and make it the parent of an IDE dialog, as here with contributions by various authors:

viewtopic.php?f=21&t=92867

'this code leans heavily on that code but uses a different approach
'It loads an IDE dialog, creates an AWT dialog of matching size and title
' and duplicates all the IDE dialog controls.

'This is very skeletal/unfinished/work in progress, see note below

'TO USE
'Create an IDE dialog with some controls on (include a command button)
'Run test with your dialog path

Edit:
The point being that it can be far easier to design a dialog with the IDE.

Code: Select all   Expand viewCollapse view
   'AWT DIALOG-CLONE
   'The OOBasic IDE doesn't implement dialog resizing.
   'One solution is to create a AWT dialog which does resize
   'and make it the parent of an IDE dialog, as here with contributions by various authors:
   'https://forum.openoffice.org/en/forum/viewtopic.php?f=21&t=92867&sid=fa5d04e6bc599e8aca2c366033f80aac
   'this code leans heavily on that code but uses a different approach
   'It loads an IDE dialog, creates an AWT dialog of matching size and title
   ' and duplicates all the IDE dialog controls.

   'TO USE
   'Create an IDE dialog with some controls on  (include a command button)
   'Run test with your dialog path

   'NOTE
   'This is very skeletal/unfinished/work in progress
   'Controls have a lot of properties which haven't been copied over fully
   'lots of listeners could be added - only one for command button execute has been
   'this is for you to develop as required

   'controls which haven't been included below will just be ignored

   'AWT controls have a hyperlink
   'for one of those use a label in your IDE dialog and set the tag to URL=YOUR_URL

   'the AWT scrollbar control doesn't redraw properly for me - so an issue there. There
   'will no doubt be others

   'a non-breaking space chr(&HA0)) is appended to the title of a modal dialog
   'to identify it as modal when the dialog title close button is clicked
   'otherwise it will be treated as a non-modal dialog

'UPDATE
'added borders, font descriptors, and combobox dropdown (these are set when window is created)
'separate out creation of dialog from setting controls
'and allow choice of attributes for new dialog window
'Sorting is an option for listboxes = add the word SORT to the IDE listbox tag

   'test code to be customised
sub test()

   dim Windowlistnr as object 'need to declare but can remain null
   dim   WA
   wa = com.sun.star.awt.WindowAttribute
   dlg = loaddialog("Standard","Dialog1") 'load IDE dialog, replace with your dialog location
   DlgWindowServiceName =modelessdialog
   'show "modelessdialog" or "modaldialog"
   odialog =CreateBaseDialog2("modelessdialog", WA.CLOSEABLE OR WA.MOVEABLE OR WA.SIZEABLE OR WA.BORDER)
   Buttonlistnr = CreateUnoListener("TestButtonExecute_", "com.sun.star.awt.XActionListener") 'create a button listener
   Windowlistnr = CreateUnoListener("TestWindowListener_", "com.sun.star.awt.XWindowListener") 'create a button listener

   AWTDialogClone (dlg,odialog,DlgWindowServiceName,Windowlistnr,Buttonlistnr) 'clone controls from IDE dialog to AWT one

end sub

sub TestButtonExecute_actionPerformed(ev) 'button listener
   msgbox ev.actioncommand 'gives button name
end sub

sub TestButtonExecute_disposing(ev)
end sub

Sub TestWindowListener_windowResized(ev)
   Dim aSize as Variant
   aSize = ev.Source.getSize()
   'resize controls here
End Sub
Sub TestWindowListener_windowMoved(ev)
End Sub
Sub TestWindowListener_windowShown(ev)
End Sub
Sub TestWindowListener_windowHidden(ev)
End Sub
Sub TestWindowListener_disposing(ev)
End Sub


   '
'*********************dialog code

Sub AWTDialogClone(dlg, odialog,DlgWindowServiceName,Windowlistnr,Buttonlistnr)

   aRect = CreateUnoStruct("com.sun.star.awt.Rectangle")

   ps = dlg.getpossize

   oParent = ThisComponent.getCurrentController().getFrame().getContainerWindow()
   oToolkit = oParent.getToolkit()

   '   odialog =CreateBaseDialog2("tabdialog")
   if isnull(Windowlistnr)=false then
      oDialog.addwindowlistener(Windowlistnr)
   end if

   if DlgWindowServiceName= "modaldialog" then
      oDialog.setTitle(dlg.model.title & chr(&HA0))
   else
      odialog.settitle(dlg.model.title)
   end if

   with ps
      odialog.setpossize .x,.y,.width,.height,15
   end with

   for i = 0 to ubound(dlg.controls)
      with dlg.controls(i)

         select case .getimplementationname
         case "stardiv.Toolkit.UnoFileControl"
            WindowServiceName = "filecontrol"
         case "stardiv.Toolkit.UnoProgressBarControl"
            WindowServiceName = "progressbar"
         case "stardiv.Toolkit.UnoEditControl"
            if .model.border then windowAttributes =windowAttributes or 16

            if.model.multiline then
            WindowServiceName = "multilineedit"
            if .model.vscroll then  windowAttributes=windowAttributes or 512
            if .model.hscroll then  windowAttributes=windowAttributes or 256

         else
            WindowServiceName = "edit"
         end if
      case "stardiv.Toolkit.UnoButtonControl"
         WindowServiceName ="pushbutton"
      case "stardiv.Toolkit.UnoListBoxControl"
         WindowServiceName ="listbox"
         if .model.border then windowAttributes =windowAttributes or 16
         if instr(1, .model.tag, "SORT") THEN windowAttributes =16384  'sort flag
         
      case "stardiv.Toolkit.UnoFixedTextControl"
         if .model.border then windowAttributes =windowAttributes or 16

         if left(.model.tag,4) = "URL=" then
            WindowServiceName ="fixedhyperlink"
         else
            WindowServiceName ="fixedtext"
         end if
      case "stardiv.Toolkit.UnoTreeControl"
         WindowServiceName ="tree"
      case "stardiv.Toolkit.UnoScrollBarControl"
         WindowServiceName ="scrollbar"
         if .model.border then windowAttributes =windowAttributes or 16

      case "stardiv.Toolkit.UnoComboBoxControl"
         WindowServiceName ="combobox"
         if .model.dropdown then windowAttributes = 32768
      case "stardiv.Toolkit.UnoCheckBoxControl"
         if .model.tristate then
            WindowServiceName ="tristatebox"
         else
            WindowServiceName ="checkbox"
         end if


      case "stardiv.Toolkit.UnoRadioButtonControl"
         WindowServiceName ="radiobutton"
      case "stardiv.Toolkit.UnoFixedLineControl"
         WindowServiceName= "fixedline"

      case "stardiv.Toolkit.UnoGroupBoxControl"
         WindowServiceName= "groupbox"
      case else
         '            vc =thiscomponent.currentcontroller.viewcursor
         '            vc.string =.getimplementationname
         '            vc.collapsetoend
         goto hr
      end select

      ps = .getpossize
      with arect
         .x = ps.x
         .y = ps.y
         .width = ps.width
         .height = ps.height
      end with




      c=   createControlJe(oToolkit,odialog,WindowServiceName,arect,windowAttributes)

      windowAttributes=0


      select case WindowServiceName

      case "pushbutton"
         c.setactioncommand ( .model.name)
         c.addActionListener(Buttonlistnr)
         c.label = .model.label
         setfd =true

      case "multilineedit"
         c.text = .model.text
         setfd =true

      case "edit"
         c.text = .model.text
         setfd =true
         
      case "listbox"
         c.additems .model.StringItemList ,0
         setfd =true

      case "tristatebox"
         c.label =.model.label
         setfd =true

      case "checkbox"
         c.label =.model.label
         setfd =true

      case "combobox"
         setfd =true
         c.text = .model.text

         c.additems .model.StringItemList ,0

         c.setDropDownLineCount .model.linecount

      case "fixedhyperlink"
         c.seturl right(.model.tag,len(.model.tag) -4)
         c.settext  .model.label
         setfd =true

      case "scrollbar"

         c.orientation=.model.orientation
         c.setvalue .model.scrollvalue
         c.setmaximum .model.scrollvaluemax

      case "tree"

      case "fixedtext"

         with .model
            c.text = .label
            setfd =true
            c.setproperty ("TextColor", .textcolor)
            c.setAlignment .align
            c.setproperty ("BackgroundColor", .backgroundcolor)
         end with

      case "groupbox"

         ps = .getpossize
         c.setproperty("Text",.model.label)

      case "fixedline"

      case "radiobutton"

         c.label = .model.label

      case "progressbar"


         'case "fixedimage"
         'case "fixedbitmap"
         'c.setgraphics .model.graphic.getdib
         'c.setgraphics .model.graphic.getdib

      case else

      end select



      if setfd then
         c.setcontrolfont .model.fontdescriptor
         setfd = false
      end if
   end with
hr:
   next

   'to do
   'TABDIALOG
   '"roadmap"
   'spinfield
   'spinbutton
   '"animatedimages"
   'splitter
   'image
   'several others
   if DlgWindowServiceName= "modaldialog" then
      odialog.execute
   else
      odialog.setvisible true
   end if
end sub

function CreateBaseDialog2(WindowServiceName as string, optional windowattributes) As Variant
   Dim oToolkit as Variant, WA as Variant, oDesc ,oDialog as Variant

   oParent = ThisComponent.getCurrentController().getFrame().getContainerWindow()
   oToolkit = oParent.getToolkit()
   'oToolkit = createUnoService("com.sun.star.awt.Toolkit")
   WA = com.sun.star.awt.WindowAttribute
   aRect = CreateUnoStruct("com.sun.star.awt.Rectangle")
   oDesc = CreateUnoStruct("com.sun.star.awt.WindowDescriptor")

   With oDesc
      .Type = com.sun.star.awt.WindowClass.SIMPLE
      .WindowServiceName =WindowServiceName
      .Parent =  oParent
      .ParentIndex = -1
      .Bounds = aRect
      if ismissing(windowattributes) then
         .WindowAttributes = WA.CLOSEABLE OR WA.MOVEABLE OR WA.SIZEABLE OR WA.BORDER
      else
         .WindowAttributes =WindowAttributes
      end if
   End With
   oDialog = oToolkit.createWindow(oDesc)
   oDialog.addTopWindowListener(CreateUnoListener("AWTTopWindowListener_", "com.sun.star.awt.XTopWindowListener"))
   CreateBaseDialog2 = oDialog
End function



function createControlJe(oToolkit,odialog,WindowServiceName,arect,windowAttributes )
   Dim  WA , oDesc
   WA = com.sun.star.awt.WindowAttribute
   oDesc = CreateUnoStruct("com.sun.star.awt.WindowDescriptor")
   With oDesc
      .Type = com.sun.star.awt.WindowClass.SIMPLE
      .WindowServiceName =WindowServiceName
      .Parent =  oDialog
      .ParentIndex = -1
      .Bounds = aRect
      .windowAttributes = windowAttributes
   End With
   button = oToolkit.createWindow(oDesc)
   on error goto hr
   button.setvisible true
hr:
   createControlJe = button
end function


   'Invoked when a window is in the process of being closed
Sub AWTTopWindowListener_windowClosing(ev)
   'ev.source.dispose '.accessiblecontext.accessibleparent.dispose
   ' Close window using the window bar close button
   dim oModalBaseDialog
   oModalBaseDialog=ev.source
   if right( oModalBaseDialog.title,1)= chr(&HA0) then
      oModalBaseDialog.endExecute()
      '      oModalBaseDialog.dispose
   else
      oModalBaseDialog.dispose
   end if
End Sub
Sub AWTTopWindowListener_windowOpened(ev)
End Sub
Sub AWTTopWindowListener_windowClosed(ev)
End Sub
Sub AWTTopWindowListener_windowMinimized(ev)
End Sub
Sub AWTTopWindowListener_windowNormalized(ev)
End Sub
Sub AWTTopWindowListener_windowActivated(ev)
End Sub
Sub AWTTopWindowListener_windowDeactivated(ev)
End Sub
Sub AWTTopWindowListener_disposing(ev)
End Sub

Function LoadDialog(oLibraryName as String, oDialogName as String)
   Dim oLib as Object
   DialogLibraries.LoadLibrary(oLibraryName)
   oLib = DialogLibraries.GetByName(oLibraryname)
   LoadDialog() =  CreateUnoDialog(oLib.GetByName(oDialogName))
End Function




   ''''''''''''''''''''''top window flags
   'https://www.openoffice.org/api/docs/common/ref/com/sun/star/awt/WindowAttribute.html
   'SHOW    specifies that the window is initially visible.
   'FULLSIZE    specifies that the window fills the complete desktop area.
   'OPTIMUMSIZE    specifies that the window is optimum size.
   'MINSIZE    specifies that the window is minimum size.
   'BORDER    specifies that the window has visible borders.
   'SIZEABLE    specifies that the size of the window can be changed by the user.
   'MOVEABLE    specifies that the window can be moved by the user.
   'CLOSEABLE    specifies that the window can be closed by the user.
   'SYSTEMDEPENDENT    [ DEPRECATED ]
   'specifies that the window should support the XSystemDependentWindowPeer interface.
   'NODECORATION    specifies that the window should have no decoration.


   '''''''''''''''''''''''''''''vcl window flags
   'https://www.openoffice.org/api/docs/common/ref/com/sun/star/awt/VclWindowPeerAttribute.html
   'HSCROLL
   'const long HSCROLL = 256;
   'VSCROLL
   'const long VSCROLL = 512;
   'LEFT
   'const long LEFT = 1024;
   'CENTER
   'const long CENTER = 2048;
   'RIGHT
   'const long RIGHT = 4096;
   'SPIN
   'const long SPIN = 8192;
   'SORT
   'const long SORT = 16384;
   'DROPDOWN
   'const long DROPDOWN = 32768;
   'DEFBUTTON
   'const long DEFBUTTON = 65536;
   'READONLY
   'const long READONLY = 262144;
   'CLIPCHILDREN
   'const long CLIPCHILDREN = 524288;
   'NOBORDER
   'const long NOBORDER = 1048576;
   'GROUP
   'const long GROUP = 2097152;
   'OK
   'const long OK = 4194304;
   'OK_CANCEL
   'const long OK_CANCEL = 8388608;
   'YES_NO
   'const long YES_NO = 16777216;
   'YES_NO_CANCEL
   'const long YES_NO_CANCEL = 33554432;
   'RETRY_CANCEL
   'const long RETRY_CANCEL = 67108864;
   'DEF_OK
   'const long DEF_OK = 134217728;
   'DEF_CANCEL
   'const long DEF_CANCEL = 268435456;
   'DEF_RETRY
   'const long DEF_RETRY = 536870912;
   'DEF_YES
   'const long DEF_YES = 1073741824;
   'DEF_NO
   'const long DEF_NO = -2147483648;
   'NOLABEL
   'const long NOLABEL = 536870912;
   'AUTOHSCROLL
   'const long AUTOHSCROLL = 1073741824;
   'AUTOVSCROLL
   'const long AUTOVSCROLL = -2147483648;



Edit:
correction to line if Dlgwindowservicename= "modaldialog" then

Edit2:
corrected several problems with modal dialog.

Edit 3: update 14 03 21
'added borders, font descriptors, and combobox dropdown (these are set when window is created)
'separate out creation of dialog from setting controls
'and allow choice of attributes for new dialog window
'Sorting is an option for listboxes = add the word SORT to the IDE listbox tag
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1540
Joined: Wed Mar 09, 2016 2:40 pm

Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 4 guests