Home   About   Contact   Log in

GUI Message Passing

January 2nd, 2007 | Filed under Programming, Projects.

OK, I have a button class. We’ll call it GUIButton. It has an “OnClick()” member that gets called when the button is clicked. Currently this just prints “Clicked!” on the screen.

Imagine I want to put five buttons on the screen to do various things. Where do I put the OnClick() code for each button?

I was thinking of making an instance of GUIButton for each real button I want (QuitButton, LoadButton … etc) and overriding the OnClick() in each one. What I can’t work out now is how to link that to the GUI XML I’ve just created.

Take this line:

Code:
<BUTTON x=0, y=0, name=”btnQuit” type=”1″ />

How, in my code, do I make that button link to the “QuitButton” class without having messy code that does this:

Code:
switch (buttontype) {
case 1:
widget = new QuitButton(x,y,z,blah);
break;
case 2:
widget = new OKButton(x,y,z);
break;
}

Since by doing this I’m hardcoding the class types into code that will later be rolled up into a library. Also it doesn’t work very well if I have two OK buttons that do different things.

It feels like I need some sort of table that I register each class with so I can ask for a class by name.

This is the bit I never got to see when writing MFC code. All I did was click on a button, press Ctrl-W and choose “WM_CLICKED” and hit “Edit code”. *something* happened and I got a function to put code in.

Visit my other sites: Photo Gallery | Insane in the Membrane | Main website

Tags:

One Response to “GUI Message Passing”

  1. james | 4/01/07

    This is a reply I got in This thread


    piku wrote:
    OK, I have a button class. We’ll call it GUIButton. It has an “OnClick()” member that gets called when the button is clicked. Currently this just prints “Clicked!” on the screen.

    Imagine I want to put five buttons on the screen to do various things. Where do I put the OnClick() code for each button?

    Personally, I’d do it through delegation, onclick calls a specifically named object in a class that implements a particular interface. Yes, this latter part is a bit tricky in C++. Let’s skip over that. Actually, so’s the first bit, but we can skip over that too, and fake it.

    Basically, all your button should know how to do is to draw itself, to hook itself up to its delegate, and send mesages off to that delegate (if they get handled by it)

    Your delegate can be anything.

    So your delegate base class looks like this:

    Code:

    class delegate_base {
      …
      void do_click() {};
      …
    };
    

    and your button looks a bit like
    Code:

    class button {
      void do_click() {delegate()->do_click();};
      delegate_base * delegate() {return delegate;};
      void set_delegate(delegate_base * del) {delegate = del;};
    };
    

Share Your Thoughts