Difference between revisions of "Playing with Window Toolkit AWT"

From Apache OpenOffice Wiki
Jump to: navigation, search
(The MessageBox Windows)
(The MessageBox Windows)
Line 149: Line 149:
 
the way we can access an interface through a service with a createInstance and UNO_QUERY simultaneously (see how do we obtain rToolkit)
 
the way we can access an interface through a service with a createInstance and UNO_QUERY simultaneously (see how do we obtain rToolkit)
 
we see also how we obtain a frame from a Desktop.
 
we see also how we obtain a frame from a Desktop.
We will present later a Desktop Helper using this message box code. (See chapter 12.1.2)
+
We will present later a Desktop Helper using this message box code. (See [[chapter 12.1.2]])

Revision as of 17:14, 24 May 2006

DannyB's Explanations

There are two different things here. Actually three.

  1. Dialogs
  2. AWT
  3. Forms

Forms are much higher level than the other two.

Of the first two, Dialogs are something using the UnoDialogControl and its model. You can also create AWT windows with controls.

AWT windows are modeless, and are not tied to any other window.

AWT windows are not the same thing as dialogs.

Generally, dialogs are operated modally by calling execute() on the dialog.

I have also had some success with operating dialogs modelessly. Just unhide the dialog, and it can be used modelessly. One drawback is that the modeless dialog seems to be tied to some concept of a "parent" window. I'm not sure, but I think that tends to be whatever window was in front when you created the dialog. So for example, you could create a modeless dialog that allows you to work with your document, but that modeless dialog's behavior appears to be tied to that document. Bring the modeless dialog to the front, and its document also comes to the front.

The MessageBox Windows

The starting code is roughly the same as previously (examples/DevelopersGuide/ProfUNO/CppBinding/). We examine also the “<OpenOffice.org1.1_SDK>/examples/DevelopersGuide/Components/Addons/ProtocolHandlerAddon_cpp” example where we find a MessageBox window. The corresponding code is given here : [cpp] //Listing 1 MessageBox Windows (deprecated) // C++ // Don't forget to add : #include <com/sun/star/awt/WindowDescriptor.hpp> // Don't forget to add "com.sun.star.awt.WindowDescriptor \" in the makefile // Don't forget to add : #include <com/sun/star/awt/WindowAttribute.hpp> // Don't forget to add "com.sun.star.awt.WindowAttribute \" in the makefile // Don't forget to add : #include <com/sun/star/awt/XWindowPeer.hpp> // Don't forget to add "com.sun.star.awt.XWindowPeer \" in the makefile // Don't forget to add : #include <com/sun/star/awt/XMessageBox.hpp> // Don't forget to add "com.sun.star.awt.XMessageBox \" in the makefile

/**

 * Show a message box with the UNO based toolkit
 */

static void ShowMessageBox( const Reference< XToolkit >& rToolkit,

              const Reference< XFrame >& rFrame, const OUString& aTitle, const OUString& aMsgText )

{

   if ( rFrame.is() && rToolkit.is() )
   {
       // describe window properties.
       WindowDescriptor                aDescriptor;
       aDescriptor.Type              = WindowClass_MODALTOP;
       aDescriptor.WindowServiceName = OUString( RTL_CONSTASCII_USTRINGPARAM( "infobox" ));
       aDescriptor.ParentIndex       = -1;
       aDescriptor.Parent            = Reference< XWindowPeer >

( rFrame->getContainerWindow(), UNO_QUERY );

       aDescriptor.Bounds            = Rectangle(300,200,300,200);
       aDescriptor.WindowAttributes  = WindowAttribute::BORDER | 
                                         WindowAttribute::MOVEABLE | WindowAttribute::CLOSEABLE;
       Reference< XWindowPeer > xPeer = rToolkit->createWindow( aDescriptor );
       if ( xPeer.is() )
       {
           Reference< XMessageBox > xMsgBox( xPeer, UNO_QUERY );
           if ( xMsgBox.is() )
           {
               xMsgBox->setCaptionText( aTitle );
               xMsgBox->setMessageText( aMsgText );
               xMsgBox->execute();
           }
       }
   }

} To understand the WindowAttribute::BORDER and other constants, we first have a look at the IDL file “WindowAttribute.idl” describing the windows' attributes :

//Listing 2 WindowAttribute IDL File 
// IDL
module com {  module sun {  module star {  module awt {
constants WindowAttribute
{
	const long SHOW = 1; 
	const long FULLSIZE = 2; 
	const long OPTIMUMSIZE = 4; 
	const long MINSIZE = 8; 
	const long BORDER = 16; 
	const long SIZEABLE = 32; 
	const long MOVEABLE = 64; 
	const long CLOSEABLE = 128; 
	const long SYSTEMDEPENDENT = 256;  
};
}; }; }; };

We have a look at XMessageBox.idl file :

//Listing 3 XMessageBox Interface : IDL File 
// IDL
module com {  module sun {  module star {  module awt {
/** gives access to a message box.
    @deprecated
 */
interface XMessageBox: com::sun::star::uno::XInterface
{
	[oneway] void setCaptionText( [in] string aText );
	string getCaptionText();
	[oneway] void setMessageText( [in] string aText );
	string getMessageText();
	short execute();
};
}; }; }; };

where we learn this interface is deprecated. We give now a complete main in a listing to learn how to use the previous ShowMessageBox procedure : [cpp] //Listing 4 Complete Program showing a Message Box Window //C++ int main( ) { //retrieve an instance of the remote service manager

   Reference< XMultiServiceFactory > rOfficeServiceManager;
   rOfficeServiceManager = ooConnect();
   if( rOfficeServiceManager.is() ){
       printf( "Connected sucessfully to the office\n" );
   }

//get the desktop service using createInstance returns an XInterface type

   Reference< XInterface  > Desktop = rOfficeServiceManager->createInstance(
   OUString::createFromAscii( "com.sun.star.frame.Desktop" ));

//query for the XComponentLoader interface

   Reference< XComponentLoader > rComponentLoader (Desktop, UNO_QUERY);
   if( rComponentLoader.is() ){
       	printf( "XComponentloader successfully instanciated\n" );
   	}

// Don't forget to add : #include <com/sun/star/awt/XToolkit.hpp> // Don't forget to add "com.sun.star.awt.XToolkit \" in the makefile // Query the XTollkit Interface Reference< XToolkit >rToolkit = Reference< XToolkit > ( rOfficeServiceManager->createInstance(

                                       OUString( RTL_CONSTASCII_USTRINGPARAM(
                                           "com.sun.star.awt.Toolkit" ))), UNO_QUERY );

if (rToolkit.is()) { printf ("OK...\n"); } else printf("Toolkit Error\n");

Reference< XDesktop > rDesktop(Desktop,UNO_QUERY); Reference< XFrame > rFrame=rDesktop->getCurrentFrame(); if (rFrame.is()) printf("rFrame ... OK\n"); else printf("rFrame Error\n");

ShowMessageBox( rToolkit, rFrame,OUString::createFromAscii("Hello") , OUString::createFromAscii("Your First Message\n OK ?") );

   return 0;

} We can draw three important things from this code : the way we can use such an IDL file (given above) with constants in C++ : it's not the first time we encounter this kind of IDL file, the way we can access an interface through a service with a createInstance and UNO_QUERY simultaneously (see how do we obtain rToolkit) we see also how we obtain a frame from a Desktop. We will present later a Desktop Helper using this message box code. (See chapter 12.1.2)

Personal tools