MVC and encapsulation

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
denarced
Knows some wx things
Knows some wx things
Posts: 47
Joined: Sun Dec 04, 2011 7:25 am

MVC and encapsulation

Post by denarced » Mon Jan 16, 2012 8:41 am

Hello,

I've been thinking about not encapsulating my classes; this sounds very wrong to me but I'd like to know if you agree with my reasoning.
Let's say we have a GUI, which consists of a lot widgets which can be represented in a hierarchy. For example:

Code: Select all

wxApp
    wxFrame
        wxMenu
            wxMenuItem (probably more than one)
        wxNoteBook
            wxPanel
                wxSizer (part of the content of the page)
                    wxTextCtrl
                    wxButton
            wxPanel
                ...
            wxPanel
                ...
    wxFrame (maybe, don't why there'd be another)
Here's where the problem starts: you need to add some event handlers so you inherit some of the classes. As it's done in a good object-oriented design, the containers hide their secrets. Now consider the Controller's position. It wants to get the text in the wxTextCtrl, then the command stack looks something like this:

Code: Select all

Controller => ::wxGetApp().getTextCtrlText();
  MyApp => wxFrame.getTextCtrlText();
    wxFrame => wxNoteBook.getTextCtrlText();
      and so on ...
A huge stack of boiler plate methods! This is insane. How do you guys avoid this? Just create the entire GUI in the wxApp ? It then contains all the pointers to all the widgets?

DerKleineNik
Knows some wx things
Knows some wx things
Posts: 29
Joined: Fri Sep 09, 2011 9:59 am

Re: MVC and encapsulation

Post by DerKleineNik » Mon Jan 16, 2012 12:24 pm

Well i dont know a way to avoid the boilerplate methods just to avoid the writing of them by myself.

I'm currently working on a program with a frame several menus and a window diveded in many child windows with notebooks and everything just as you described and i created all that using wxFormBuilder where i just can drag and drop the elements and the code is then generated by wxFormBuilder and all i have to do is create a subclass of this code with my own event handlers.
The names of the event handlers can already be chosen in wxFormBuilder so you just have to override those functions in your inherited class then.

Basically that means exactly creating all of that in one class so all of the pointers are available directly in the inherited class.

The other possibility is to think about where you need the widgets and just use them in own classes, for example a class derived from wxPanel where you can use your own event handlers. This panelclass is then just added to the object above in the hierarchy wich normally doesnt have to know about the widgets in the panel.

denarced
Knows some wx things
Knows some wx things
Posts: 47
Joined: Sun Dec 04, 2011 7:25 am

Re: MVC and encapsulation

Post by denarced » Mon Jan 16, 2012 1:27 pm

DerKleineNik wrote:Basically that means exactly creating all of that in one class so all of the pointers are available directly in the inherited class.
Thanks for your reply!
Your description fits precisely on how I did it in my last project. The good thing was that modifying things was extremely easy. The downside was that I never seemed to learn how wxWidgets functions, I just learned how to use the tool. Also, it creates a monster of a class! Even with a modest sized GUI there were dozens of widgets plus their event handlers. There goes abstraction; I wouldn't want to be the guy doing the maintenance on that. Plus I now have a lot of dynamic behavior which can't be modeled in the tool.

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Re: MVC and encapsulation

Post by Auria » Mon Jan 16, 2012 2:59 pm

you need to add some event handlers so you inherit some of the classes.
that is incorrect : with Connect (and in 2.9 Bind), you do not need to use inheritance to use event handlers ( http://wxwidgets.blogspot.com/2007/01/i ... nnect.html )
"Keyboard not detected. Press F1 to continue"
-- Windows

denarced
Knows some wx things
Knows some wx things
Posts: 47
Joined: Sun Dec 04, 2011 7:25 am

Re: MVC and encapsulation

Post by denarced » Mon Jan 16, 2012 5:03 pm

Auria wrote:
you need to add some event handlers so you inherit some of the classes.
that is incorrect : with Connect (and in 2.9 Bind), you do not need to use inheritance to use event handlers ( http://wxwidgets.blogspot.com/2007/01/i ... nnect.html )
Oops, that was a slip up. The problem still remains. I actually prefer Bind these days.

DerKleineNik
Knows some wx things
Knows some wx things
Posts: 29
Joined: Fri Sep 09, 2011 9:59 am

Re: MVC and encapsulation

Post by DerKleineNik » Tue Jan 17, 2012 8:50 am

denarced wrote:The downside was that I never seemed to learn how wxWidgets functions, I just learned how to use the tool. Also, it creates a monster of a class! Even with a modest sized GUI there were dozens of widgets plus their event handlers. There goes abstraction; I wouldn't want to be the guy doing the maintenance on that. Plus I now have a lot of dynamic behavior which can't be modeled in the tool.
Well i saw tht problem too. My luck is that im working on that project just for an internship and i dont have to do the maintenance but im interested in a smart solution as you are. If you find a good solution i'd be glad to try it myself too.
The problem with my project is that i have lots and lots of GUIs, dialogs etcetera so creating them all by myself would be hell of a work.
I hope that you'll find agood solution

Post Reply