1. Switch Bug
- Posted by ghaberek (admin) Jun 05, 2011
- 1712 views
I'd like to implement a simple switch block to handle multiple event IDs from one handler. However, I'm having a problem and I hope I'm not just missing something. In the demo below, regardless of which menu item I select, it always jumps to the "Exit" block, except when I actually choose "Exit", then it jumps to the else block instead. Is this the same bug listed here? This demo was made with Euphoria 4.0.2 and wxEuphoria 0.15.0.
-Greg
include "wxeu/wxeud.e" without warning include "std/text.e" object void constant wxID_CUSTOM1 = new_id(), frmMain = create( wxFrame, {0, -1, "Menu Demo", -1, -1, 480, 360} ), mbrMain = create( wxMenuBar, {frmMain} ), mnuFile = create( wxMenu, {mbrMain, "&File"} ), mnuFile_New = create( wxMenuItem, {mnuFile, wxID_NEW, "&New"} ), mnuFile_Open = create( wxMenuItem, {mnuFile, wxID_OPEN, "&Open..."} ), mnuFile_Save = create( wxMenuItem, {mnuFile, wxID_SAVE, "&Save"} ), mnuFile_SaveAs = create( wxMenuItem, {mnuFile, wxID_SAVEAS, "Save &As..."} ), mnuFile_PageSetup = create( wxMenuItem, {mnuFile, wxID_PRINT_SETUP, "Page Set&up..."} ), mnuFile_Print = create( wxMenuItem, {mnuFile, wxID_PRINT, "&Print..."} ), mnuFile_Custom1 = create( wxMenuItem, {mnuFile, wxID_CUSTOM1, "Custom 1"} ), mnuFile_Exit = create( wxMenuItem, {mnuFile, wxID_EXIT, "E&xit"} ), lstCtrl = create( wxListCtrl, {frmMain} ), $ insert_separator( mnuFile, 4 ) -- after "Save As" insert_separator( mnuFile, 7 ) -- after "Print" insert_separator( mnuFile, 9 ) -- after "Custom 1" void = insert_list_column( lstCtrl, 0, "ID", 0, 160 ) void = insert_list_column( lstCtrl, 1, "Value", 0, 240 ) void = insert_listctrl_item( lstCtrl, 0, {"wxID_NEW",sprint(wxID_NEW)}, 0 ) void = insert_listctrl_item( lstCtrl, 1, {"wxID_OPEN",sprint(wxID_OPEN)}, 0 ) void = insert_listctrl_item( lstCtrl, 2, {"wxID_SAVE",sprint(wxID_SAVE)}, 0 ) void = insert_listctrl_item( lstCtrl, 3, {"wxID_SAVEAS",sprint(wxID_SAVEAS)}, 0 ) void = insert_listctrl_item( lstCtrl, 4, {"wxID_PRINT_SETUP",sprint(wxID_PRINT_SETUP)}, 0 ) void = insert_listctrl_item( lstCtrl, 5, {"wxID_PRINT",sprint(wxID_PRINT)}, 0 ) void = insert_listctrl_item( lstCtrl, 6, {"wxID_CUSTOM1",sprint(wxID_CUSTOM1)}, 0 ) void = insert_listctrl_item( lstCtrl, 7, {"wxID_EXIT",sprint(wxID_EXIT)}, 0 ) procedure menu_handler( atom this, atom event_type, atom id, atom event ) sequence message switch id do case wxID_NEW then message = "New" case wxID_OPEN then message = "Open" case wxID_SAVE then message = "Save" case wxID_SAVEAS then message = "Save As" case wxID_PRINT_SETUP then message = "Page Setup" case wxID_PRINT then message = "Print" case wxID_CUSTOM1 then message = "Custom 1" case wxID_EXIT then message = "Exit" case else message = "Unknown command" end switch void = message_box( sprintf("%s (ID %d)", {message, id}), "Menu Handler", wxICON_INFORMATION ) end procedure set_event_handler( frmMain, -1, wxEVT_COMMAND_MENU_SELECTED, routine_id("menu_handler") ) wxMain( frmMain )
2. Re: Switch Bug
- Posted by mindwalker Jun 05, 2011
- 1667 views
I'd like to implement a simple switch block to handle multiple event IDs from one handler. However, I'm having a problem and I hope I'm not just missing something. In the demo below, regardless of which menu item I select, it always jumps to the "Exit" block, except when I actually choose "Exit", then it jumps to the else block instead. Is this the same bug listed here? This demo was made with Euphoria 4.0.2 and wxEuphoria 0.15.0.
-Greg
I'm not sure I know what I'm talking about, but I think you need a break statement at the end of each case. Otherwise I think it executes all the cases starting at the one that matches. So for example in your code if id is equal to wxID_SAVE then I think message is getting set to "Save" then to "Save As" followed by "Print Setup" all the way down to "Exit".
So add a line that reads break after each message = ... line. For example:
case wxID_OPEN then message = "Open" break
3. Re: Switch Bug
- Posted by ghaberek (admin) Jun 05, 2011
- 1677 views
I'm not sure I know what I'm talking about, but I think you need a break statement at the end of each case. Otherwise I think it executes all the cases starting at the one that matches. So for example in your code if id is equal to wxID_SAVE then I think message is getting set to "Save" then to "Save As" followed by "Print Setup" all the way down to "Exit".
So add a line that reads break after each message = ... line. For example:
case wxID_OPEN then message = "Open" break
I thought of that too, at first; in fact I tried it and it had no effect. The break command should only be necessary when also using with fallthru. From the manual:
By default, control flows to the end of the switch block when the next case is encountered. The default behavior can be modified in two ways. The default for a particular switch block can be changed so that control passes to the next executable statement whenever a new case is encountered by using with fallthru in the switch statement:
switch x with fallthru do case 1 then ? 1 case 2 then ? 2 break case else ? 0 end switch
-Greg
4. Re: Switch Bug
- Posted by Jerome Jun 06, 2011
- 1682 views
I'm not sure of the reason but if you define new_id's for the built in wxID_ constants, things work as expected. I tried the following:
wxID_CUSTOM1 = new_id(), wxID_NEW = new_id(), wxID_OPEN = new_id(), wxID_SAVE = new_id(), wxID_SAVEAS = new_id(), wxID_PRINT_SETUP = new_id(), wxID_PRINT = new_id(), wxID_EXIT = new_id(), ......
Also, probably not necessary but I passed in the Menu id's to the set_event_handler...
constant menuids = {wxID_NEW,wxID_OPEN,wxID_SAVE,wxID_SAVEAS,wxID_PRINT_SETUP,wxID_PRINT,wxID_CUSTOM1,wxID_EXIT} set_event_handler( mnuFile, menuids, wxEVT_COMMAND_MENU_SELECTED, routine_id("menu_handler") )
It sorta defeats the purpose of using the built in id's, but it seems to work!
Hope this helps,
Ira
5. Re: Switch Bug
- Posted by petelomax Jun 06, 2011
- 1657 views
My reaction was that it should be
case mnuFile_New then
not
case wxID_NEW then
etc
HTH,
Pete
6. Re: Switch Bug
- Posted by ghaberek (admin) Jun 06, 2011
- 1635 views
I'm not sure of the reason but if you define new_id's for the built in wxID_ constants, things work as expected. I tried the following:
wxID_CUSTOM1 = new_id(), wxID_NEW = new_id(), wxID_OPEN = new_id(), wxID_SAVE = new_id(), wxID_SAVEAS = new_id(), wxID_PRINT_SETUP = new_id(), wxID_PRINT = new_id(), wxID_EXIT = new_id(), ......
The bug here seems to pertain to the values of the constants. The built-in IDs start numbering from 5000, whereas new_id() starts at 100. When you do this, you get wxID_CUSTOM1 = 100, wxID_NEW = 101, wxID_OPEN = 102, etc. I redefined the constants with their original values (see below) and got the same result: everything jumped to case wxID_EXIT then.
constant wxID_CUSTOM1 = new_id(), wxID_NEW = 5002, wxID_OPEN = 5000, wxID_SAVE = 5003, wxID_SAVEAS = 5004, wxID_PRINT_SETUP = 5011, wxID_PRINT = 5010, wxID_EXIT = 5006
Also, probably not necessary but I passed in the Menu id's to the set_event_handler...
constant menuids = {wxID_NEW,wxID_OPEN,wxID_SAVE,wxID_SAVEAS,wxID_PRINT_SETUP,wxID_PRINT,wxID_CUSTOM1,wxID_EXIT} set_event_handler( mnuFile, menuids, wxEVT_COMMAND_MENU_SELECTED, routine_id("menu_handler") )
It sorta defeats the purpose of using the built in id's, but it seems to work!
Hope this helps,
Ira
That doesn't seem to have any effect. I typically do this when I group event handlers together, like file_menu_handler(), edit_menu_handler(), etc.
My reaction was that it should be
case mnuFile_New then
not
case wxID_NEW then
etc
HTH,
Pete
Nope, this is correct behavior for wxEuphoria. For menu items especially, the "this" object is always the main wxFrame, and the "id" object changes with each event that occurs. This behavior comes from wxWidgets, which uses ID numbers to trigger events rather than object handles. This way, multiple objects can trigger the same event, like the "File > New" menu item and the "New" toolbar button. It makes thin a helluva lot easier.
-Greg
7. Re: Switch Bug
- Posted by SDPringle Jun 06, 2011
- 1623 views
I was able to reproduce this same problem on Windows Vista and Euphoria 4.0.2. However, when I tried it on my latest build this problem doesn't happen. I suggest you get new 4.0 binaries.
8. Re: Switch Bug
- Posted by mattlewis (admin) Jun 07, 2011
- 1528 views
I'd like to implement a simple switch block to handle multiple event IDs from one handler. However, I'm having a problem and I hope I'm not just missing something. In the demo below, regardless of which menu item I select, it always jumps to the "Exit" block, except when I actually choose "Exit", then it jumps to the else block instead. Is this the same bug listed here? This demo was made with Euphoria 4.0.2 and wxEuphoria 0.15.0.
This looks like:
- ticket:662 Switches with all integer cases, but with a range of greater than 1024 between the biggest and smallest were interpreted incorrectly.
Matt