OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Enable/disable menu items at runtime (C++)

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
Nuer
General User
General User


Joined: 29 Nov 2007
Posts: 16

PostPosted: Mon Feb 18, 2008 1:03 am    Post subject: Enable/disable menu items at runtime (C++) Reply with quote

vars:
actions[].name - URL of the disabling/enabling command
actions[].enable - required state of the command

Code:

      Reference< XMultiServiceFactory > xConfigProvider = Reference< XMultiServiceFactory > ( mxMultiServiceFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationProvider" ) ) ), UNO_QUERY );
      PropertyValue propertyValue;
      propertyValue.Name = OUString::createFromAscii( "nodepath" );
      propertyValue.Value <<= OUString::createFromAscii( "/org.openoffice.Office.Commands/Execute/Disabled" );   
      Sequence < Any > lParams( 1 );
      lParams[0] <<= propertyValue;
      Reference< XNameAccess > xAccess = Reference< XNameAccess > ( xConfigProvider->createInstanceWithArguments( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationUpdateAccess" ) ), lParams ), UNO_QUERY );
      Reference < XNameContainer > xNameContainer = Reference < XNameContainer > ( xAccess, UNO_QUERY );
      // Enable all commands
      for ( int i = 0; i < ACTIONS_COUNT; i++ )
      {
         try {
            xNameContainer->removeByName( OUString::createFromAscii( actions[ i ].name ) );
         }
         // Ignore if there are no item to delete
         catch ( ... ) {}
      }
      // Disable required commands
      for ( int i = 0; i < ACTIONS_COUNT; i++ )
      {
         if ( !actions[ i ].enable )
         {
            Reference < XSingleServiceFactory > xSetElementFactory = Reference < XSingleServiceFactory > ( xAccess, UNO_QUERY );
            Sequence < Any > aArgs( 0 );
            Reference< XInterface > xNewElement = xSetElementFactory->createInstanceWithArguments( aArgs );
            Any aNewElement;
            aNewElement <<= xNewElement;
            Reference< XPropertySet > xPropertySet = Reference< XPropertySet > ( xNewElement, UNO_QUERY );
            Any aCommandURL;
            aCommandURL <<= OUString::createFromAscii( actions[ i ].name );
            xPropertySet->setPropertyValue( OUString::createFromAscii( "Command" ), aCommandURL  );
            xNameContainer->insertByName( OUString::createFromAscii( actions[ i ].name ), aNewElement );
         }
      }
      // Save changes
      Reference < XChangesBatch > xFlush = Reference < XChangesBatch > ( xAccess, UNO_QUERY );
      xFlush->commitChanges();
Back to top
View user's profile Send private message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Mon Feb 18, 2008 10:02 am    Post subject: Reply with quote

Thank you for your code.
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
kapoorvinny
Newbie
Newbie


Joined: 01 Jul 2008
Posts: 3
Location: Delhi,India

PostPosted: Tue Jul 01, 2008 11:30 pm    Post subject: can i disable\enable toolbar in java Reply with quote

Can i disable\enable toolbar in java
Back to top
View user's profile Send private message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Fri Jul 04, 2008 8:48 am    Post subject: Reply with quote

Yes with the same way as above. Translate C++ code into Java.
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
talmai
General User
General User


Joined: 11 Jul 2008
Posts: 10
Location: Salvador/BA - Brasil

PostPosted: Mon Jul 14, 2008 11:43 am    Post subject: Reply with quote

For those who - like me - need a little bit more code for things to work, here is what I added to the office_connect.cxx example (located in odk/examples/DevelopersGuide/ProfUNO/CppBinding)

Makefile

Code:

TYPES = \
   com.sun.star.lang.XMultiServiceFactory \
   com.sun.star.lang.XSingleServiceFactory \
   com.sun.star.lang.XComponent \
   com.sun.star.beans.XPropertySet \
   com.sun.star.container.XNameAccess \
   com.sun.star.container.XNameContainer \
   com.sun.star.util.XChangesBatch \
   com.sun.star.registry.XSimpleRegistry \
   com.sun.star.bridge.XUnoUrlResolver \
   com.sun.star.frame.XComponentLoader \
   com.sun.star.lang.XMultiComponentFactory \
   com.sun.star.container.XHierarchicalNameAccess


Includes

Code:

#include <com/sun/star/registry/XSimpleRegistry.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
#include <com/sun/star/util/XChangesBatch.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
using namespace com::sun::star::container;
using namespace com::sun::star::util;


And finally code:

Code:

    ::com::sun::star::uno::Sequence< ::rtl::OUString > lNames(17);
    lNames[0] = ::rtl::OUString::createFromAscii("Quit");
    lNames[1] = ::rtl::OUString::createFromAscii("Save");
    lNames[2] = ::rtl::OUString::createFromAscii("SaveAs");
    lNames[3] = ::rtl::OUString::createFromAscii("SaveAll");
    lNames[4] = ::rtl::OUString::createFromAscii("Open");
    lNames[5] = ::rtl::OUString::createFromAscii("ExportDirectToPDF");
    lNames[6] = ::rtl::OUString::createFromAscii("ExportTo");
    lNames[7] = ::rtl::OUString::createFromAscii("ExportToPDF");
    lNames[8] = ::rtl::OUString::createFromAscii("NewDoc");
    lNames[9] = ::rtl::OUString::createFromAscii("NewPresentation");
    lNames[10] = ::rtl::OUString::createFromAscii("NewGlobalDoc");
    lNames[11] = ::rtl::OUString::createFromAscii("NewHtmlDoc");
    lNames[12] = ::rtl::OUString::createFromAscii("SendMailDocAsPDF");
    lNames[13] = ::rtl::OUString::createFromAscii("SendMail");
    lNames[14] = ::rtl::OUString::createFromAscii("RecentFileList");
    lNames[15] = ::rtl::OUString::createFromAscii("CloseDoc");
    lNames[16] = ::rtl::OUString::createFromAscii("CloseWin");

   Reference< XMultiServiceFactory > mxMultiServiceFactory (xInterface, UNO_QUERY);
    Reference< XMultiServiceFactory > xConfigProvider = Reference< XMultiServiceFactory > ( mxMultiServiceFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationProvider" ) ) ), UNO_QUERY );
    PropertyValue propertyValue;
    propertyValue.Name = OUString::createFromAscii( "nodepath" );
    propertyValue.Value <<= OUString::createFromAscii( "/org.openoffice.Office.Commands/Execute/Disabled" );
    Sequence < Any > lParams( 1 );
    lParams[0] <<= propertyValue;
    Reference< XNameAccess > xAccess = Reference< XNameAccess > ( xConfigProvider->createInstanceWithArguments( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationUpdateAccess" ) ), lParams ), UNO_QUERY );
    Reference < XNameContainer > xNameContainer = Reference < XNameContainer > ( xAccess, UNO_QUERY );
   for (int i = 0; i < lNames.getLength(); i++)
   {
            try {
           xNameContainer->removeByName( lNames[i] );
            }
            // Ignore if there are no item to delete
            catch (  Exception& e  )
            {
          printf("Error: cannot remove item:\n       %s\n",
                   OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr());
            }
       Reference < XSingleServiceFactory > xSetElementFactory = Reference < XSingleServiceFactory > ( xAccess, UNO_QUERY );
       Sequence < Any > aArgs( 0 );
       Reference< XInterface > xNewElement = xSetElementFactory->createInstanceWithArguments( aArgs );
       Any aNewElement;
       aNewElement <<= xNewElement;
       Reference< XPropertySet > xPropertySet = Reference< XPropertySet > ( xNewElement, UNO_QUERY );
       Any aCommandURL;
       aCommandURL <<= lNames[i];
       xPropertySet->setPropertyValue( OUString::createFromAscii( "Command" ), aCommandURL  );
      xNameContainer->insertByName( lNames[i], aNewElement );
   }
    Reference < XChangesBatch > xFlush = Reference < XChangesBatch > ( xAccess, UNO_QUERY );
    xFlush->commitChanges();

_________________
www.talmai.com.br
Back to top
View user's profile Send private message
niceguy78
Newbie
Newbie


Joined: 17 Jul 2008
Posts: 4

PostPosted: Wed Jul 23, 2008 6:18 am    Post subject: Reply with quote

It is very interesting code, but can you provide the same code written in VB or VB.NET?
_________________
Jack, you can visit my home page here
Back to top
View user's profile Send private message Visit poster's website
talmai
General User
General User


Joined: 11 Jul 2008
Posts: 10
Location: Salvador/BA - Brasil

PostPosted: Wed Jul 23, 2008 6:26 am    Post subject: Reply with quote

nope sorry.. Embarassed

try: OO_SOURCE/odk/examples/DevelopersGuide/OfficeDev/DisableCommands

there is an example in Java... maybe the transition will be easier than C++ -> VB.
_________________
www.talmai.com.br
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group