Search code examples
c++user-interfacewxwidgets

Design of class for a GUI frame


I have a class MainFrame (subclass of wxFrame) Now this class does most of the work like creating Menubar, toolbar, panel etc.

Now for each event in menu/ toolbar there is a associated function like OnSave, OnQuit, OnHelp etc. Currently (initial stage) i have made all the OnXYZ members of MainFrame. But such a design is making me uneasy. Soon there will be too many functions inside a class.

My Question : How should i split the above method in several classes / files?

Or more generally how should i design my MainFrame?

NOTE : I have checked few question regarding GUI desgin on SO, but they are all for Python, tkinter so i didn't understand them well.


Solution

  • Your concern is readability. However, if you split the 'natural' MyFrame class, with all the little methods that look after each user action, up into separate classes then you may perhaps gain readability for each part, but you will risk making things harder to comprehend, since you have a collection of small classes that have no real reason to exist.

    The best help for readability is good documentation!

    Note that the methods of a class can be implemented in different files - it does not need to be all one source file. So if the file with all the method implementations and their documentation becomes so long that it loads slowly in your editor, then you can split up the source file without splitting up the class.

    I wonder if you are using a poor, or non-existent IDE? Without a good IDE you are condemned to be constantly scrolling through the source file - and that can get tedious if the files becomes long.

    OK, now just how many methods are we talking about here? Are there a hundred or more? If so, then I would suggest that you should be looking at your GUI design. Your users are going to be pretty confused if they are confronted with a hundred possible actions every time they open you application! If you split your GUI into several screens, each with a well defined and logical purpose, you users will be much more comfortable and your classes will be smaller!