Playing with Window Toolkit AWT

From Apache OpenOffice Wiki
Revision as of 17:23, 24 May 2006 by SergeMoutou (Talk | contribs)

Jump to: navigation, search

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)

A very simple Window

We give now a listing to construct a very simple windows : [cpp] //Listing 5 Constructing a simple Window // C++ main( ) {

   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);

// *** essai fenetre if ( rToolkit.is() )

   	{
       // describe window properties.
       WindowDescriptor                aDescriptor;
       aDescriptor.Type              = WindowClass_TOP;
       aDescriptor.WindowServiceName = OUString( RTL_CONSTASCII_USTRINGPARAM(""));
       aDescriptor.ParentIndex       = -1;

aDescriptor.Parent = rToolkit->getDesktopWindow();

       aDescriptor.Bounds            = Rectangle(100,200,300,400);
       aDescriptor.WindowAttributes  = WindowAttribute::BORDER | WindowAttribute::MOVEABLE 						| WindowAttribute::SHOW	| WindowAttribute::CLOSEABLE 

| WindowAttribute::SIZEABLE; Reference< XWindowPeer > xWindowPeer = rToolkit->createWindow( aDescriptor ); Reference< XWindow > xWindow(xWindowPeer,UNO_QUERY); xWindowPeer->setBackground(0xFF00FF);

// 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! // create a new frame Reference< XFrame > xFrame =Reference< XFrame >( rOfficeServiceManager->createInstance(

                                       OUString( RTL_CONSTASCII_USTRINGPARAM(
                                           "com.sun.star.frame.Frame" ))), UNO_QUERY );

xFrame->initialize(xWindow); Reference < XFramesSupplier > xFramesSupplier(Desktop,UNO_QUERY); xFrame->setCreator(xFramesSupplier); xFrame->setName(OUString::createFromAscii("Window 1")); getchar(); }

   return 0;

}

The file picker dialog

We want provide a dialog box for searching a file and returning its name. We start from a Bernard Marcelly's book example. When working with file picker dialog, we are concerned with three IDL files. Here is the first one  :

//Listing 6 XFilePicker Interface : IDL File 
// IDL
module com { module sun { module star { module ui { module dialogs {
interface XFilePicker: com::sun::star::ui::dialogs::XExecutableDialog
{
	void setMultiSelectionMode( [in] boolean bMode );
	void setDefaultName( [in] string aName );
	void setDisplayDirectory( [in] string aDirectory )
		raises( ::com::sun::star::lang::IllegalArgumentException );
	string getDisplayDirectory();
	sequence< string > getFiles();
};
}; }; }; }; };

The beginning of this file indicates we are concerned with XExecutableDialog too :

//Listing 7 XExecutableDialog Interface : IDL File 
// IDL
module com { module sun { module star { module ui { module dialogs {
interface XExecutableDialog: com::sun::star::uno::XInterface
{
	void setTitle( [in] string aTitle );
	short execute();
};
}; }; }; }; };

and what is more difficult to see is we are concerned with :

//Listing 8 ExecutableDialogresults values : IDL File 
// IDL
module com { module sun { module star { module ui { module dialogs {
constants ExecutableDialogResults
{
	const short CANCEL = 0;
	const short OK     = 1;
};
}; }; }; }; };
<pre>
The way to obtain the FilePicker dialog can be explained as follow. First you have to obtain a XFilePicker Interface. After we use a setDisplayDirectory for an example, then execute the dialog box and finally get the first of chosen files.
<code>[cpp]
//Listing 9 a File Picker Dialog
// LINUX C++ (Only the file path is LINUX-like)
// based on Bernard Marcelly's  Example (Book p512)
// Don't forget #include <osl/file.hxx>
	OUString sDocUrl;
	osl::FileBase::getFileURLFromSystemPath(
				OUString::createFromAscii("/home/smoutou/"),sDocUrl);

// Don't forget to add : using namespace com::sun::star::ui::dialogs;
// Don't forget to add : #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
// Don't forget to add "com.sun.star.ui.dialogs/XFilePicker \" in the makefile
// Query the XFilePicker interface

	Reference< XFilePicker > rFilePicker = Reference< XFilePicker >
					( rOfficeServiceManager->createInstance(
                          OUString( RTL_CONSTASCII_USTRINGPARAM(
                          "com.sun.star.ui.dialogs.FilePicker" ))), UNO_QUERY );

	rFilePicker->setDisplayDirectory( sDocUrl);
	short result=rFilePicker->execute();

// Don't forget to add : #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
// Don't forget to add "com.sun.star.ui.dialogs/ExecutableDialogResults \" in the makefile
	if (result == ExecutableDialogResults::OK)
	  ShowMessageBox( rToolkit, rFrame,OUString::createFromAscii("Result") ,
			rFilePicker->getFiles()[0]  );
</code>
It is possible to modify the filter names with the XFilterManager. Here is the corresponding IDL file :
<pre>
//Listing 10 XFilterManager Interface : IDL File 
// IDL
module com { module sun { module star { module ui { module dialogs {
interface XFilterManager: com::sun::star::uno::XInterface
{
	void appendFilter( [in] string aTitle, [in] string aFilter )
		raises( ::com::sun::star::lang::IllegalArgumentException );
	void setCurrentFilter( [in] string aTitle )
		raises( ::com::sun::star::lang::IllegalArgumentException );
	string getCurrentFilter( );
};
}; }; }; }; };

and now a C++ example which use this interface : [cpp] //Listing 11 Using the File Picker // LINUX C++ // Don't forget #include <osl/file.hxx> OUString sDocUrl; osl::FileBase::getFileURLFromSystemPath( OUString::createFromAscii("/home/smoutou/"),sDocUrl); // Don't forget to add : using namespace com::sun::star::ui::dialogs; // Don't forget to add : #include <com/sun/star/ui/dialogs/XFilePicker.hpp> // Don't forget to add "com.sun.star.ui.dialogs/XFilePicker \" in the makefile // Reference< XFilePicker > rFilePicker(rDesktop,UNO_QUERY); Reference< XFilePicker > rFilePicker = Reference< XFilePicker > ( rOfficeServiceManager->createInstance(

                                OUString( RTL_CONSTASCII_USTRINGPARAM(
                                "com.sun.star.ui.dialogs.FilePicker" ))), UNO_QUERY );

rFilePicker->setDisplayDirectory( sDocUrl);

// Don't forget to add : #include <com/sun/star/ui/dialogs/XFilterManager.hpp> // Don't forget to add "com.sun.star.ui.dialogs/XFilterManager \" in the makefile Reference< XFilterManager > rFilterManager (rFilePicker, UNO_QUERY); rFilterManager->appendFilter(OUString::createFromAscii("Texts"), OUString::createFromAscii("*.txt")); rFilterManager->appendFilter(OUString::createFromAscii("Docs OpenOffice"), OUString::createFromAscii("*.sxw;*.sxc")); rFilterManager->setCurrentFilter(OUString::createFromAscii("Docs OpenOffice")); short result=rFilePicker->execute();

// Don't forget to add : #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp> // Don't forget to add "com.sun.star.ui.dialogs/ExecutableDialogResults \" in the makefile if (result == ExecutableDialogResults::OK) ShowMessageBox( rToolkit, rFrame,OUString::createFromAscii("Result") , rFilePicker->getFiles()[0] ); It's time to show how to construct a save as Dialog.

Save as Dialog

We again start from a Bernard Marcelly's example. Our problem is to translate these OOoBasic lines : [oobas] 'Listing 12 Save Dialog REM ***** BASIC ***** FP=CreateUnoService("com.sun.star.ui.dialogs.FilePicker") Dim FPtype(0) As Integer FPtype(0)=com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_SIMPLE With FP .initialize(FPtype()) .... End With particularly the « initialize » one. We look at first for the constant to transform the previous FilePicker dialog. The constant is defined in the TemplateDescription.idl file  :

//Listing 13 TemplateDescription Constants : IDL File 
// IDL
module com { module sun { module star { module ui { module dialogs {
constants TemplateDescription
{
	const short FILEOPEN_SIMPLE                                = 0;
	const short FILESAVE_SIMPLE                                = 1;
	const short FILESAVE_AUTOEXTENSION_PASSWORD                = 2;
	const short FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS  = 3;
	const short FILESAVE_AUTOEXTENSION_SELECTION               = 4;
	const short FILESAVE_AUTOEXTENSION_TEMPLATE                = 5;
	const short FILEOPEN_LINK_PREVIEW_IMAGE_TEMPLATE           = 6;
	const short FILEOPEN_PLAY                                  = 7;
	const short FILEOPEN_READONLY_VERSION                      = 8;
	const short FILEOPEN_LINK_PREVIEW				           = 9;
	const short FILESAVE_AUTOEXTENSION			               = 10;
};
}; }; }; }; };

For initialize we look for in FilePicker.idl file :

//Listing 14 FilePiker service : IDL File 
// IDL
module com { module sun { module star { module ui { module dialogs {
interface XFilePicker;
interface XFilePickerNotifier;
interface XFilePickerControlAccess;
interface XFilterManager;
interface XFilePreview;
interface XFilterGroupManager;

service FilePicker
{
	[optional, property] string HelpURL;
	interface XFilePicker;
	interface XFilePickerNotifier;
	interface XFilterManager;
	[optional] interface XFilePreview;
	[optional] interface XFilePickerControlAccess;
	[optional] interface XFilterGroupManager;
	[optional] interface com::sun::star::lang::XInitialization;
	[optional] interface com::sun::star::util::XCancellable;
	interface com::sun::star::lang::XComponent;
	interface com::sun::star::lang::XServiceInfo;
	interface com::sun::star::lang::XTypeProvider;
};
}; }; }; }; };

where you see an XInitialization interface. Therefore we give the XInitialization.idl file :

//Listing 15 XInitialisation Interface : IDL File 
// IDL
module com {  module sun {  module star {  module lang {
interface XInitialization: com::sun::star::uno::XInterface
{ 
	void initialize( [in] sequence<any> aArguments ) 
			raises( com::sun::star::uno::Exception ); 
 
}; 
}; }; }; };

It's OK, we get it. We see we have to pass an any sequence to the initialize procedure. It's time to write the C++ corresponding code. [cpp] //Listing 16 Invoking the Save Dialog // LINUX C++ // inspired by Bernard Marcelly's Example p515 // Don't forget #include <osl/file.hxx> OUString sDocUrl; osl::FileBase::getFileURLFromSystemPath( OUString::createFromAscii("/home/smoutou/"),sDocUrl); // Don't forget to add : using namespace com::sun::star::ui::dialogs; // Don't forget to add : #include <com/sun/star/ui/dialogs/XFilePicker.hpp> // Don't forget to add "com.sun.star.ui.dialogs/XFilePicker \" in the makefile // Reference< XFilePicker > rFilePicker(rDesktop,UNO_QUERY); Reference< XFilePicker > rFilePicker = Reference< XFilePicker > ( rOfficeServiceManager->createInstance(

                              OUString( RTL_CONSTASCII_USTRINGPARAM(
                              "com.sun.star.ui.dialogs.FilePicker" ))), UNO_QUERY );

rFilePicker->setDisplayDirectory( sDocUrl);

// Don't forget to add : #include <com/sun/star/ui/dialogs/XFilterManager.hpp> // Don't forget to add "com.sun.star.ui.dialogs.XFilterManager \" in the makefile Reference< XFilterManager > rFilterManager (rFilePicker, UNO_QUERY);

// Don't forget to add : #include <com/sun/star/lang/XInitialization.hpp> // Don't forget to add "com.sun.star.lang.XInitialization \" in the makefile Reference< XInitialization > rInitialize (rFilePicker, UNO_QUERY); Sequence < Any > info(1);

// Don't forget to add : #include <com/sun/star/ui/dialogs/TemplateDescription.hpp> // Don't forget to add "com.sun.star.ui.dialogs.TemplateDescription \" in the makefile info[0] <<= (short) TemplateDescription::FILESAVE_SIMPLE; rInitialize-> initialize(info);

rFilterManager->appendFilter(OUString::createFromAscii("Texts"), OUString::createFromAscii("*.txt")); rFilterManager->appendFilter(OUString::createFromAscii("Docs OpenOffice"), OUString::createFromAscii("*.sxw;*.sxc")); rFilterManager->setCurrentFilter(OUString::createFromAscii("Docs OpenOffice")); short result=rFilePicker->execute();

// Don't forget to add : #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp> // Don't forget to add "com.sun.star.ui.dialogs/ExecutableDialogResults \" in the makefile if (result == ExecutableDialogResults::OK) ShowMessageBox( rToolkit, rFrame,OUString::createFromAscii("Result") , rFilePicker->getFiles()[0] ); We are interesting now with generalising our previous work with Dialog Box.

Personal tools