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

Enabling/Disabling Buttons Dynamically

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Aaron
General User
General User


Joined: 14 Nov 2003
Posts: 34
Location: Cincinnati, OH

PostPosted: Tue Nov 18, 2003 12:24 pm    Post subject: Enabling/Disabling Buttons Dynamically Reply with quote

I am creating a macro which presents the user with a dialog window. In this dialog they will need to enter text into a Textfield control. What I would like to do is use the "Text Modified" event handler to check the length of the text in the Textfield. If the length of the text is less than or equal to 0, it should disable the "OK" button, otherwise it should enable it.

I made it all the way to the part where it needs to enable/disable the button, and couldn't figure out how to do this. I know you can set the enabled/disabled state of the Button control when designing the dialog, but is there a way to set this programatically? According to the API documentation there isn't: http://api.openoffice.org/docs/common/ref/com/sun/star/awt/XButton.html. Is this correct? Is there really no way to do this?

If not, there really should be. I am in the process of converting a Word macro into a Write macro, and this is a feature that I could really use.

Thanks in advance for any help!

-Aaron
Back to top
View user's profile Send private message AIM Address
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Nov 18, 2003 12:34 pm    Post subject: Reply with quote

You CAN enable or disable the state of buttons in a dialog box.

Here is an example of some code that does it.

1. Go to http://OOoMacros.org

2. Download "Danny's Draw Power Tools". It is a draw document.
3. Open Danny's Draw Power Tools. (Click Yes to enable macros to run if asked.)
4. Click the button labeled "Flower Gears". This brings up a dialog box -- which is what you want to see.

The particular tool here makes drawings based upon gears. Notice how the "Create" button is enabled and the "Stop" button is disabled.

5. Click the Create button.

DDPT starts producing a drawing. It takes awhile to draw, even on a fast machine.

Move the newly created drawing out of the way so you can see the dialog box underneath. Notice how the Stop button is now enabled, and the Create button is now disabled.

Click Stop. (unless you want to see the drawing complete.) Either way, by clicking stop or by letting the drawing complete, the Create button will become enabled and the Stop button will become disabled.

Here's how it works.

Go into the Macros for DDPT. Find the FlowerGears1 library. Then select the FlowerGears1Dlg module and click Edit.

Scroll down until you find the subroutine named.... Sub btnCreate_Clicked

Scroll down more until you find these lines...

Code:
......
......
   lRunning = TRUE
   
   oDialog.getControl( "btnCreate" ).setEnable( Not lRunning )
   oDialog.getControl( "btnStop" ).setEnable( lRunning )
......


This sets a global flag to mean that the routine is now running. Based on this global flag, it enables one control and disables another control.

Scroll down further, and find where it changes the states of the two controls and the global flag....
Code:
......
......
   lRunning = FALSE
   
   oDialog.getControl( "btnCreate" ).setEnable( Not lRunning )
   oDialog.getControl( "btnStop" ).setEnable( lRunning )
......


Hope this helps.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Aaron
General User
General User


Joined: 14 Nov 2003
Posts: 34
Location: Cincinnati, OH

PostPosted: Tue Nov 18, 2003 12:44 pm    Post subject: Reply with quote

Danny, you rock!
I was trying to use setEnabled() instead of setEnable() (Doh!). Is that function documented somewhere that might save me from asking stupid questions in the future?

For anyone who is interested, here is the code I ended up using. Note: The Button control is initially set to Disabled when designing the dialog. This subroutine is called in the "Text Modified" event handler of the Textfield control:

Code:
Sub ActivateGlossaryInsertButton
   Dim strTerm As String
   
   DialogLibraries.LoadLibrary("Standard")
   strTerm = objDlgGlossaryTerm.getControl("fldGlossaryTerm").getText()
      
   If len(strTerm) > 0 Then
      objDlgGlossaryTerm.getControl("btnInsertTerm").setEnable(TRUE)
   Else
      objDlgGlossaryTerm.getControl("btnInsertTerm").setEnable(FALSE)
   End If
End Sub



Thanks again,
Aaron
Back to top
View user's profile Send private message AIM Address
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Nov 18, 2003 5:10 pm    Post subject: Reply with quote

Aaron wrote:
Danny, you rock!

Thanks. It's always nice to know that I'm being helpful vs. just wondering. Or sometimes just hoping that it is helpful to someone out there, like a note in a bottle.


Aaron wrote:
I was trying to use setEnabled() instead of setEnable() (Doh!). Is that function documented somewhere that might save me from asking stupid questions in the future?

On dialogs, getControl() gets you the control not the model. It seems like just today or yesterday I explained to someone the difference between the control and the model. When you do a getByName() on a form of a document, what you get back is the model and not the control.

So looking at the button control (not the model)....

http://api.openoffice.org/docs/common/ref/com/sun/star/awt/UnoControlButton.html

....we see that it....
* Includes the UnoControl service
* Implements the XButton interface
* Implements the XLayout interface

What properties it includes.....
This means that, it has any properties of UnoControlButton, and UnoControl, plus any properties of any services that UnoControl might include. (Not interfaces, just services. Properties can ONLY be in services, NEVER interfaces.)

What methods it includes....
This means that it has any methods of XButton, and XLayout since it directly implements those interfaces.
It also has any methods of any interfaces implemented by UnoControl.
It also has any methods of any interfaced implemented by any service that is included or inherited from UnoControl.
Methods ALWAYS come from Interfaces, NEVER from Services.

Did you follow all that. So from the link I gave you above for UnoControlButton, there are a number of links you can follow, and from linked pages, even more links to more services and interfaces, etc.

Since I happen to know where I'm going, we'll just take the direct route to the correct branch of the tree.

Notice that UnoControlbutton includes the UnoControl service, documented here....

http://api.openoffice.org/docs/common/ref/com/sun/star/awt/UnoControl.html

(An aside, from the above discussion, remember I said that any methods from any interfaces of any services included or inherited by UnoControlButton are part of UnoControlButton.)

Notice that UnoControl implements the XWindow interface, among other interfaces....

http://api.openoffice.org/docs/common/ref/com/sun/star/awt/XWindow.html

Note that the XWindow interface includes a method....

setEnable( boolean ).

Therefore, by following the above reasoning and indirect route to here, any UnoControlButton has setEnable.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Aaron
General User
General User


Joined: 14 Nov 2003
Posts: 34
Location: Cincinnati, OH

PostPosted: Tue Nov 18, 2003 7:13 pm    Post subject: Reply with quote

Danny, your explanation makes perfect sense. I was able to trace through the included services until I found setEnable(). I think that once I start learning how to move myself around in the API documentation I will be very well off.

Thanks again, people like you make the open source community the great thing that it is! Now I just need to convince my boss. Wink
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API 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