Passing data between controls

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
joemaniaci
Experienced Solver
Experienced Solver
Posts: 52
Joined: Thu Dec 08, 2016 2:42 pm

Passing data between controls

Post by joemaniaci »

Not sure if this is more of a C++ discussion, wxWidgets, or both. But is there a wxWidgets way to say, take the file name from an wxFileDialog, and get it to a wxTreeCtrl? I started working on an observer pattern, or more specifically, a more generalized Publisher-Subscribe pattern and realized that I was heading towards multiple inheritance. I basically realized at the end of it I would be inheriting from my Observer base class/interface as well as the applicable wxMenu(i.e. File(wxMenu as Base class) derived). I would like a more generalized idea of what to do as it applies to C++, but I was also wondering if I can create an event in the File->OpenFile action, and place an EventHandler in the control I want to manage it(Almost like throwing an exception and letting someone else handle it). Any help would greatly be appreciated.

Would the solution potentially be where I first have my Observer inherit from wxObject(or looking at the hierarchy tree, wxTopLevelWindow), and then I have my all my current top-level classes inherit from Observer? Kind of like how wxWidgets has this crazy long chain of inheritance?

I also want to try to avoid friend functionality to maintain encapsulation.

EDIT: I'm actually wondering if a class template would be an option here. http://imgur.com/jTVH1wf
coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: Passing data between controls

Post by coderrc »

it is probably easiest to simply have an event handler in the control that needs the event,
then in the file->open function use something like

Code: Select all

wxCommandEvent* evt = new wxCommandEvt(MY_CUSTOM_EVENT_TAG);
evt->SetString(fileDialogResult);
controlThatNeedsTheEvent->GetEventHandler()->QueueEvent(evt);
wxcommand event
http://docs.wxwidgets.org/trunk/classwx ... abd913d450

creating a custom event
https://wiki.wxwidgets.org/Custom_Event ... xEventType
joemaniaci
Experienced Solver
Experienced Solver
Posts: 52
Joined: Thu Dec 08, 2016 2:42 pm

Re: Passing data between controls

Post by joemaniaci »

Thanks. I attempted multiple inheritance and got close but I think I had conflicts with wxWidgets managed memory cleanup, so custom events seem to be the best option for anyone that attempt this.

EDIT: Well after looking at the links you sent me I think I'm still at the exact same situation as before. For me to queue an event I need a reference/pointer to the object to handle the event. So doesn't that leave me in the same situation where I have to have everything holding a ptr to each other? So every object I create will have to include pointers to every control it may queue an event for. That's why I went down the path of an observer to maintain encapsulation.

In the Custom Events link you sent it said one one line for posting your event....

Code: Select all

wxPostEvent(pBar, event); // or to a different instance or class
What's that ...or class. part? For both wxQueueEvent and wxPostEvent I only see declarations involving a pointer to whatever derived class(of PostEvent or QueueEvent) you're using. Were there at some point some macros that allowed you to pass a class name that let you link your event to a handler in that class, but were taken out at some point? I'm using 3.0.2.

At this rate I guess I could organize my GUI into three main components/classes that all contain references/ptrs to each other. So then top class A can queue up an event for top class B, then top class B would handle the event, and queue up another event for one of it's component classes? Or is this the point where one might find it useful to derive from wxCommandEvent?
joemaniaci
Experienced Solver
Experienced Solver
Posts: 52
Joined: Thu Dec 08, 2016 2:42 pm

Re: Passing data between controls

Post by joemaniaci »

Okay, so this is a little sad that this wasn't the first thing I tried, but this is an acceptable option right? It works but I just want to make sure there are no hidden gotchas.

Code: Select all

BEGIN_EVENT_TABLE( AnyControl, wxSomeControl)
       ....
       EVT_MENU( ID_ANY, SomeOtherControl::DoSomething)
       ....
END_EVENT_TABLE()
For wxFileDialog it works because it has the GetPaths(wxArrayString) method that allows me to get the data out of it. Just curious about packaging up data for say, a custom control. https://wiki.wxwidgets.org/Beech:Using_custom_dialogs looks like what I need.
coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: Passing data between controls

Post by coderrc »

for linking events to other classes, I would prefer Bind()
http://docs.wxwidgets.org/3.1/overview_ ... vents_bind

keep scrolling on that page to see how events propogate.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Passing data between controls

Post by doublemax »

I think you're overcomplicating things. I've never had to jump to those hoops, especially when dealing with the result from a file dialog. Can you give a practical example of what you're trying to do?
Use the source, Luke!
joemaniaci
Experienced Solver
Experienced Solver
Posts: 52
Joined: Thu Dec 08, 2016 2:42 pm

Re: Passing data between controls

Post by joemaniaci »

doublemax wrote:I think you're overcomplicating things. I've never had to jump to those hoops, especially when dealing with the result from a file dialog. Can you give a practical example of what you're trying to do?
Oh that's my M.O.

I'm just wanting to make sure I'm doing it correctly, and that would be getting messages/data from one object to another. So I have all my menus at top and I just want to make sure I am getting the results of interacting with those menus to the consuming controls correctly and uniformly.
joemaniaci
Experienced Solver
Experienced Solver
Posts: 52
Joined: Thu Dec 08, 2016 2:42 pm

Re: Passing data between controls

Post by joemaniaci »

coderrc wrote:for linking events to other classes, I would prefer Bind()
http://docs.wxwidgets.org/3.1/overview_ ... vents_bind

keep scrolling on that page to see how events propogate.
Bind is just the most up to date to dynamically link right? Statically linking is still valid? I just kind of like having the event tables uniformly at the top of my source files.
Serge_N
Knows some wx things
Knows some wx things
Posts: 40
Joined: Thu Apr 13, 2017 1:34 pm

Re: Passing data between controls

Post by Serge_N »

There is a nice tutorial "Widgets communicate" at the end of page: http://zetcode.com/gui/wxwidgets/firstprograms/
joemaniaci
Experienced Solver
Experienced Solver
Posts: 52
Joined: Thu Dec 08, 2016 2:42 pm

Re: Passing data between controls

Post by joemaniaci »

Serge_N wrote:There is a nice tutorial "Widgets communicate" at the end of page: http://zetcode.com/gui/wxwidgets/firstprograms/
I saw that awhile ago, I figured I would just end up having everything pointing to everything and I'd much rather not do that.

I got this working though mostly.

Code: Select all

Bind(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(AuiManager::OnFileOpen), this, ID_FOPEN);
Did I do something wrong that had my compiler requiring me to use the wxCommandEventHandler(...)? This bind is referring to OnFileOpen in another class.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Passing data between controls

Post by ONEEYEMAN »

Hi,
This is the old syntax with Connect().

The Bind line should be:

Code: Select all

Bind( wxEVT_COMMAND_MENU_SELECTED, &AuiManager::OnFileOpen, this, ID_FOPEN );
And you can of course inter-mix static and dynamic event tables.

Thank you.
Post Reply