Framework/Article/Easy To Use Message Boxes

From Apache OpenOffice Wiki
Jump to: navigation, search

Easy to use message boxes

OpenOffice.org 2.2 and later provide better support for 3rd party developers to use UNO AWT. Easy to use message boxes is one of our efforts to make the uno awt much more appealing. The new way to create a message box is much easier:

The entry point is the well known UNO AWT service com.sun.star.awt.Toolkit

published service Toolkit
{
   interface XToolkit;
 
   [optional] interface com::sun::star::awt::XDataTransferProviderAccess;
 
   [optional] interface com::sun::star::awt::XSystemChildFactory;
 
   [optional] interface com::sun::star::awt::XMessageBoxFactory;
};

This service now support one more optional interface called com.sun.star.awt.XMessageBoxFactory. The message box factory creates messages boxes and initialize them with one call. The caller has to provide all necessary arguments and gets back a reference to the newly created message box.

/** specifies a factory interface for creating message boxes.
*/
published interface XMessageBoxFactory : com::sun::star::uno::XInterface
{
   //-------------------------------------------------------------------------
     /** creates a message box.
 
       @returns
       the created message box or a null reference if it cannot be
       created.
 
       @param aParent
       a valid XWindowPeer reference which is used as a parent. This parameter
       must not be null.
 
       @param aPosSize
       a rectangle which defines the position and size of the message
       box in pixel.
 
       @param aType
       a string which determines the message box type.
       The following strings are defined.
 
       infobox    A message box to inform the user about a certain event.
                  Attention:
                  This type of message box ignores the argument aButton
                  because a infobox always shows a OK button.
       warningbox A message to warn the user about a certain problem.
       errorbox   A message box to provide an error message to the user.
       querybox   A message box to query information from the user.
       messbox    A normal message box.
 
       @param aButtons
       specifies which buttons should be available on the
       message box. A combination of com::sun::star::awt::MessageBoxButtons. 
       An infobox ignores this paramter and always use button "OK".
 
       @param aTitle
       specifies the title of the message box.
 
       @param aMessage
       specifies text which will be shown by the message box.
       Line-breaks must be added using 'CR' or 'CR+LF'.
   */
   XMessageBox createMessageBox( [in] com::sun::star::awt::XWindowPeer aParent, 
                                 [in] com::sun::star::awt::Rectangle aPosSize, 
                                 [in] string aType, 
                                 [in] long aButtons, 
                                 [in] string aTitle, 
                                 [in] string aMessage );
};
/** defines constants for the possible message box button
   combinations.
 
*/
constants MessageBoxButtons
{
  //-------------------------------------------------------------------------
 
   /** specifies a message with "OK" button.
    */
   const long BUTTONS_OK                   = 1;
 
   /** specifies a message box with "OK" and "CANCEL" button.
    */
   const long BUTTONS_OK_CANCEL            = 2;
 
   /** specifies a message box with "YES" and "NO" button.
    */
   const long BUTTONS_YES_NO               = 3;
 
   /** specifies a message box with "YES", "NO" and "CANCEL" button.
    */
   const long BUTTONS_YES_NO_CANCEL        = 4;
 
   /** specifies a message box with "RETRY" and "CANCEL" button.
    */
   const long BUTTONS_RETRY_CANCEL         = 5;
 
   /** specifies a message box with "ABORT", "IGNORE" and "RETRY" button.
    */
   const long BUTTONS_ABORT_IGNORE_RETRY   = 6;
 
   /** specifies that OK is the default button.
    */
   const long DEFAULT_BUTTON_OK            = 0x10000;
      /** specifies that CANCEL is the default button.
    */
   const long DEFAULT_BUTTON_CANCEL        = 0x20000;
 
   /** specifies that RETRY is the default button.
    */
   const long DEFAULT_BUTTON_RETRY         = 0x30000;
      /** specifies that YES is the default button.
    */
   const long DEFAULT_BUTTON_YES           = 0x40000;
 
   /** specifies that NO is the default button.
    */
   const long DEFAULT_BUTTON_NO            = 0x50000;
 
   /** specifies that IGNORE is the default button.
    */
   const long DEFAULT_BUTTON_IGNORE        = 0x60000;
};

Below you can see a simple example using Apache OpenOffice Basic.

REM  *****  BASIC  *****
 
Sub Main
    oMsgBoxFactory = createUnoService( "com.sun.star.awt.Toolkit" )
    oDesktop       = createUnoService( "com.sun.star.frame.Desktop" )
 
    REM ***** Empty rectangle means use default values *****
    Dim aRectangle as new com.sun.star.awt.Rectangle
 
    oButtons = com.sun.star.awt.MessageBoxButtons.BUTTONS_YES_NO_CANCEL+com.sun.star.awt.MessageBoxButtons.DEFAULT_BUTTON_YES
 
    REM ***** Don not use getCurrentFrame() in your relase code!! *****
    REM ***** This example uses it just to be small *****
    oParent  = oDesktop.getCurrentFrame().getContainerWindow()
 
    oMsgBox  = oMsgBoxFactory.createMessageBox( oParent, aRectangle, "messbox", oButtons, "Title", "This is the message box text" )
 
    oMsgBox.execute()
End Sub
Personal tools