OO-Snippets: MessageBox with the UNO based toolkit (ooRexx)

Commons

KeywordsMessageBox
LanguageooRexx
ApplicationOffice
AuthorsRony G. Flatscher (initial)
Supported Versions2.0.x  
Supported OSAll  
QuestionHow do I display a message box from an ooRexx script ?

I'm wondering if it is possible to display message boxes (similar to those displayed by Basic's MsgBox function) using the OpenOffice.org API.

(Also you could use ".bsf.dialog~messageBox()", ".bsf.dialog~dialogBox()" and/or

"bsf.dialog~inputBox()".)

Answer

You can use the following function as a replacement for the Msgbox StarBasic function:

Code-Snippet-Listing (snippet-source)

/*
   Modelled (transcribed from) the Python snippet at
   ./Office.MessageBoxWithTheUNOBasedToolkit.snip
   for ooRexx (http://www.ooRexx.org).
*/

   -- needs to be run as a macro:
xScriptContext=uno.getScriptContext()     -- get the xScriptContext object

oDoc=xScriptContext~getDocument           -- get the document service (an XModel object)
-- oDesktop=xScriptContext~getDesktop           -- get the desktop (an XDesktop object)
-- oContext=xScriptContext~getComponentContext  -- get the context(an XComponentContext object)

   -- store a directory proxy for the constants in the .local environment
.local~vwpa=bsf.wrapStaticFields('com.sun.star.awt.VclWindowPeerAttribute')
/* Defined constants, now retrievable by sending their name as a message to the directory proxy,
   retrievable via the environment symbol ".vwpa". Here is the list of available button keys:
   OK, OK_CANCEL, YES_NO, YES_NO_CANCEL, RETRY_CANCEL,
   DEF_OK, DEF_CANCEL, DEF_YES, DEF_NO, DEF_RETRY
*/

call testMessageBox oDoc      -- now test the message box, supply the document object which has the XModel interface

::requires UNO.CLS   -- load UNO support for OpenOffice.org

::routine TestMessageBox
  use arg doc        -- retrieve document component
  numeric digits 10  -- augment significant numbers in arithmetics from 9 to 10 digits

     -- get the container window using the XModel interface to start out with
  parentWin=doc~XModel~getCurrentController~getFrame~getContainerWindow

  s = "This is a test"        -- message text
  t = "Test"                  -- title for the message box
  res = MessageBox(parentWin, s, t, "querybox", .vwpa~YES_NO_CANCEL + .vwpa~DEF_NO)

  call MessageBox parentWin, "The previous querybox returned:" res, t, "infobox"


--# Show a message box with the UNO based toolkit
::routine MessageBox
  use arg ParentWin, MsgText, MsgTitle, MsgType, MsgButtons

  if pos(msgType, "messbox infobox errorbox warningbox querybox")=0 then
     MsgType="messbox"

  if arg(5, "Omitted") then   -- if fifth argument is omitted
     MsgButtons=.vwpa~ok      -- use the "OK" button only

  -- #describe window properties.
  aDescriptor=.bsf~new("com.sun.star.awt.WindowDescriptor")
  aDescriptor~Type = bsf.getConstant("com.sun.star.awt.WindowClass", "MODALTOP")
  aDescriptor~WindowServiceName = MsgType
  aDescriptor~ParentIndex = -1
  aDescriptor~Parent = ParentWin~XWindowPeer
  -- #aDescriptor~Bounds = .bsf~new("com.sun.star.awt.Rectangle")
  aDescriptor~WindowAttributes = MsgButtons

  tk = ParentWin~XWindowPeer~getToolkit
  msgbox = tk~createWindow(aDescriptor)~xMessageBox
  msgbox~setMessageText(MsgText)
  if arg(3,"exists") then     -- if third argument (title) exists, use it
     msgbox~setCaptionText(MsgTitle)

  return msgbox~execute       -- execute the message box and returned pressed key

Changelog

DateUserModification

and