[wxruby-users] How to use evt_update_ui (wrt RichTextCtrl ?)
Alex Fenton
alex at pressure.to
Mon Jan 5 13:59:38 EST 2009
Hi Chauk-Mean
I'm glad you asked about evt_update_ui. It's a really useful technique
for changing the state of menu items, toolbars, controls etc as the
application changes state. It's saved me a lot of time since I figured
it out a while back, so I'm copying this to wxruby-users so others know
about it.
Attached is a complete 50-line sample which shows how to use
evt_update_ui. A description below, which roughly approximates the
attached code.
Chauk-Mean P wrote:
> The problem I have is I don't know how to update automatically the
> state of each formatting button according to the position of the
> caret.
> The C++ sample rely on EVT_UPDATE_UI. I tried to use evt_update_ui
> without success.
The short answer is that you call methods (eg enable, check) on the
UpdateUIEvent passed into the evt_update_ui handler.
Let's say you have an application with a simple frame, containing a text
field and a checkbox. It also has a menu. The text field can be in
'editing' or 'readonly' mode, and this is controlled by checking or
unchecking the text box.
The frame has an "Edit" menu, with an item called 'Toggle Case' which
sets all the text in the control to uppercase or lowercase. However this
menu option should only be enabled when the text field is in 'editing' mode.
The obvious way to do this is to add something to the event handler for
the toggle button, to enable or disable the menu item accordingly:
evt_checkbox(my_cbx) do | event |
# First, find the relevant item from the "Edit" menu
case_menu_item = menu_bar.find_item("Edit", "Toggle Case")
if event.checked? # if the checkbox was checked to enable editing
menu_bar.enable(case_menu_item, true)
else
menu_bar.enable(case_menu_item, false)
end
end
This works ok, but gets messy as the number of items increases. You have
to remember to update all the relevant items in the checkbox event. You
also have to worry about setting the initial state of the menu item
correctly.
How evt_update_ui works is to be called continuously to update the state
(eg enabled/disabled) of a UI item, eg the menu item. The important bit:
changes are effected by calling methods on the UpdateUIEvent object
passed into this event handler.
For the example above, to disable/enable the menu item depending on
whether the checkbox is checked:
evt_update_ui(case_menu_item) do | event |
if my_cbox.checked?
event.enable(true)
else
event.enable(false)
end
end
Slightly less code, though the biggest advantage for this simple example
is keeping the UI management of the menu item close to the item itself,
rather than in the checkbox code, which may be a long way away.
It's even better when you have, for example, both a menu item and a
checkbox on-screen item which do the same thing, toggle the 'Edit' mode
on and off. Then you can use evt_update_ui to make sure that both items
are always in the correct checked state however the user chooses to
change into editing mode:
# Show the item as checked or unchecked depending on whether in edit_mode
evt_update_ui(case_menu_item) do | event |
event.check(@edit_mode)
end
# Toggle edit_mode on or off according to whether the menu item is checked
evt_menu(case_menu_item) do | event |
@edit_mode = event.checked?
end
Repeat this code for the checkbox button, or, even better, give the
checkbox button and the menu item the same id, then the code will work
for both with no extra work. Happiness: simple and reliable code.
> I'm trying to create a very simple RichTextCtrl sample based on the C++ version.
> This works pretty well now with the fixes for missing
> RichTextCtrl.selection_bold? ... methods.
> See the attached script.
> Given the formatting buttons (bold, italics, underlined), I can type
> text with the proper formatting, select some text and apply a
> formatting later ...
A sample for RichTextCtrl would be very cool, thanks. Did you mean to
attach some code with this?
cheers
alex
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: update_ui_event.rb
URL: <http://rubyforge.org/pipermail/wxruby-users/attachments/20090105/b20f5bd7/attachment.pl>
More information about the wxruby-users
mailing list