Chauk-Mean P
2009-Jan-05 16:09 UTC
[wxruby-development] How to use evt_update_ui wrt RichTextCtrl ?
Hi, 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 ... 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. Currently, I''m using a refresh button to update the formatting buttons. Thanks. Chauk-Mean.
Alex Fenton
2009-Jan-05 18:59 UTC
[wxruby-users] How to use evt_update_ui (wrt RichTextCtrl ?)
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>
Chauk-Mean P
2009-Jan-06 10:24 UTC
[wxruby-development] How to use evt_update_ui (wrt RichTextCtrl ?)
Hi Alex, 2009/1/5 Alex Fenton <alex at pressure.to>:> 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.I hesitated to post on wxruby-users as my script relies on recent svn commits.> Attached is a complete 50-line sample which shows how to use evt_update_ui. > A description below, which roughly approximates the attached code.Your sample works. I modified my script to follow closely your sample but it still does not work. In fact, the handler for evt_update_ui is never called. Can you please try my script ? - type some text - click on a formatting button (e.g. bold), then type some other text - click on the first text => the bold button is not updated (it should have been unchecked as the first text was not in bold) - click on the refresh button => the bold button is updated correctly. Thanks. Chauk-Mean.
Alex Fenton
2009-Jan-06 10:41 UTC
[wxruby-development] How to use evt_update_ui (wrt RichTextCtrl ?)
Chauk-Mean P wrote:> I hesitated to post on wxruby-users as my script relies on recent svn commits. >No probs - just thought that evt_update_ui was of more general interest - and has worked for as long I can remember.> I modified my script to follow closely your sample but it still does not work. > In fact, the handler for evt_update_ui is never called. > > Can you please try my script ?Yes, of course - please attach it and I''ll give it a whirl.> - type some text > - click on a formatting button (e.g. bold), then type some other text > - click on the first text => the bold button is not updated (it should > have been unchecked as the first text was not in bold) > - click on the refresh button => the bold button is updated correctly. >cheers alex
Chauk-Mean P
2009-Jan-06 10:49 UTC
[wxruby-development] How to use evt_update_ui (wrt RichTextCtrl ?)
2009/1/6 Alex Fenton <alex at pressure.to>:>> Can you please try my script ? > > Yes, of course - please attach it and I''ll give it a whirl. >Oups ... Here it is. Chauk-Mean.
Chauk-Mean P
2009-Jan-06 10:57 UTC
[wxruby-development] How to use evt_update_ui (wrt RichTextCtrl ?)
I haven''t forgotten the script ... but I have a problem with attachement on my machine. Here is a copy/paste of the script : # RichTextCtrl sample # Require wxRuby 1.9.9 or superior require ''wx'' class UpdateUIFrame < Wx::Frame def initialize super(nil, :title => "RichTextCtrl sample", :size => [800, 600]) # create the toolbar toolbar = Wx::ToolBar.new(self, :style => Wx::TB_HORIZONTAL | Wx::NO_BORDER | Wx::TB_FLAT | Wx::TB_TEXT) self.tool_bar = toolbar # use a stock bitmap at the moment dummy_bitmap = Wx::ArtProvider.bitmap(Wx::ART_ADD_BOOKMARK) bold_item = toolbar.add_item(dummy_bitmap, :kind => Wx::ITEM_CHECK, :label => "Bold", :short_help => "Bold") italic_item = toolbar.add_item(dummy_bitmap, :kind => Wx::ITEM_CHECK, :label => "Italic", :short_help => "Italic") underline_item = toolbar.add_item(dummy_bitmap, :kind => Wx::ITEM_CHECK, :label => "Underline", :short_help => "Underline") toolbar.add_separator refresh_item = toolbar.add_item(dummy_bitmap, :label => "Refresh", :short_help => "Refresh") toolbar.realize article_editor = Wx::RichTextCtrl.new(self, :style => Wx::WANTS_CHARS) evt_tool(bold_item) do article_editor.apply_bold_to_selection end evt_tool(italic_item) do article_editor.apply_italic_to_selection end evt_tool(underline_item) do article_editor.apply_underline_to_selection end # Refresh automatically the state of each button evt_update_ui(bold_item) do |evt| evt.check(article_editor.selection_bold?) puts "evt_update_ui has been called" end evt_update_ui(italic_item) do evt.check(article_editor.selection_italics?) puts "evt_update_ui has been called" end evt_update_ui(underline_item) do evt.check(article_editor.selection_underlined?) puts "evt_update_ui has been called" end # Refresh manually the state of each button evt_tool(refresh_item) do toolbar.toggle_tool(bold_item.wx_id, article_editor.selection_bold?) toolbar.toggle_tool(italic_item.wx_id, article_editor.selection_italics?) toolbar.toggle_tool(underline_item.wx_id, article_editor.selection_underlined?) end end end Wx::App.run { UpdateUIFrame.new.show } Chauk-Mean.
Alex Fenton
2009-Jan-06 16:41 UTC
[wxruby-development] How to use evt_update_ui (wrt RichTextCtrl ?)
Chauk-Mean P wrote:> I haven''t forgotten the script ... but I have a problem with > attachement on my machine. > > Here is a copy/paste of the script : > > # RichTextCtrl sample > # Require wxRuby 1.9.9 or superiorTry creating the toolbar with "create_tool_bar(...style...)" instead of "ToolBar.new(...); self.tool_bar=". For me (XP, SVN HEAD, ruby 1.8.6-mingw) this gets the UpdateUIEvents flowing to the toolbar buttons and it works like it ought to. I had a quick look at the wxWidgets code and it looks good. I can''t explain for now why this makes a difference - it may well be a Wx bug. cheers a
Chauk-Mean P
2009-Jan-06 17:16 UTC
[wxruby-development] How to use evt_update_ui (wrt RichTextCtrl ?)
2009/1/6 Alex Fenton <alex at pressure.to>:> Try creating the toolbar with "create_tool_bar(...style...)" instead of > "ToolBar.new(...); self.tool_bar=". > > For me (XP, SVN HEAD, ruby 1.8.6-mingw) this gets the UpdateUIEvents flowing > to the toolbar buttons and it works like it ought to.Yes it works with create_tool_bar. I just take a look at the C++ sample and the method CreateToolBar is also used.> I had a quick look at the wxWidgets code and it looks good. I can''t explain > for now why this makes a difference - it may well be a Wx bug.OK. Thanks anyway. Chauk-Mean.
Alex Fenton
2009-Jan-06 21:17 UTC
[wxruby-development] How to use evt_update_ui (wrt RichTextCtrl ?)
Chauk-Mean P wrote:> Here is a copy/paste of the script : > > # RichTextCtrl sample > # Require wxRuby 1.9.9 or superior > >...> # Refresh automatically the state of each button > evt_update_ui(bold_item) do |evt| > evt.check(article_editor.selection_bold?) > puts "evt_update_ui has been called" > endJust a quick follow up: when a ToolBar was created directly by ''new'', the toolbar''s SWIG director was stopping the calls to UpdateWindowUI being routed correctly to the C++ version that calls evt_update_ui for each component button. This is now fixed in SVN, thanks for turning up this bug. alex