wxITEM_CHECK toolbar button synchronized with checkable menu Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
xinvar
Earned a small fee
Earned a small fee
Posts: 23
Joined: Thu Jun 01, 2006 8:01 pm

wxITEM_CHECK toolbar button synchronized with checkable menu

Post by xinvar » Fri Jun 09, 2006 10:53 pm

Hi,

What I have:

A toolbar with a checkable button created by:

Code: Select all

toolBar->AddTool(MENU_VIEW_CONTROL_WINDOW, _T("Controls"), toolBarBitmaps[Tool_cut], _T("View Control Panel"), wxITEM_CHECK);
And a menu item created by:

Code: Select all

menuView->AppendCheckItem(MENU_VIEW_CONTROL_WINDOW, _T("Control Window"), _T("Show/hide the control window"));
What I want:

Synchronize them: When the menu item is checked, the toolbar button should be pressed down, and vice versa.

The problem:

First of all, although they have the same ID, they don't come synchronized. So I think I would need to add something. I know how to check/uncheck a menu item, but don't know how to make a toolbar button look pressed or not pressed.

Thanks a lot.
Last edited by xinvar on Fri Jun 09, 2006 11:35 pm, edited 1 time in total.

theigor
Experienced Solver
Experienced Solver
Posts: 78
Joined: Thu Jan 12, 2006 6:51 pm

Post by theigor » Fri Jun 09, 2006 11:07 pm

Code: Select all

BEGIN_EVENT_TABLE() 
  EVT_UPDATE_UI(MENU_VIEW_CONTROL_WINDOW, AFrame::OnUpdateItem) 
END_EVENT_TABLE() 

void AFrame::OnUpdateItem(wxUpdateUIEvent& event){ 
  event.Check(true); 
} 
Last edited by theigor on Fri Jun 09, 2006 11:16 pm, edited 1 time in total.

xinvar
Earned a small fee
Earned a small fee
Posts: 23
Joined: Thu Jun 01, 2006 8:01 pm

Post by xinvar » Fri Jun 09, 2006 11:15 pm

Thanks a lot theigor.

The code you gave will always make both menu item and toolbar button checked. I played with it and found the following is doing what I need:

Code: Select all

event.Check( GetMenuBar()->IsChecked(MENU_VIEW_CONTROL_WINDOW)); 
This is probably not an elegant solution though.

xinvar
Earned a small fee
Earned a small fee
Posts: 23
Joined: Thu Jun 01, 2006 8:01 pm

Post by xinvar » Fri Jun 09, 2006 11:36 pm

Oops, this doesn't work. It acts as if the menu item is the master switch. The toolbar check item has no effect on the menu item.

xinvar
Earned a small fee
Earned a small fee
Posts: 23
Joined: Thu Jun 01, 2006 8:01 pm

Post by xinvar » Fri Jun 09, 2006 11:47 pm

Okay, this is solved by combining EVT_MENU and UI.

I have a variable: bool isPressed. In the EVT_MENU function, it's set: isPressed = event.IsChecked(). In the UI function, I use this to set the items: event.Check(isPressed );

Post Reply