[SDL] SDL Digest, Vol 14, Issue 15

Onur Tugcu onur.tugcu at gmail.com
Mon Feb 11 20:51:54 PST 2008


>
>
> Hello everyone!
>
> My system works like this:
>
> Control (base class)
>  -- GUI (control container)
>  -- Button (sample control)
>  -- ...other controls
>
> And the event system works like this (in this case we'll use mouse
> clicks): the main application calls the "OnClick()" method in the GUI
> class with the mouse pointer's position as a parameter. The GUI class
> then sees which control was clicked (based on the mouse's position), and
> calls the "OnClick()" method in that control (in this case we'll use a
> button). But here's the problem; how can this control (button) class let
> the main application know that it was clicked?
>
> I tried to place a function pointer called "m_pfnOnClickEvent" and let
> the main application set a function to it when it creates the button...
> but C++ doesn't let you put member methods onto function pointers, and
> my program is 100% object-oriented.
>
> Any ideas? Thanks!
>
>
libsigc++, boost::signals library, etc. Provide ways of binding the object
and member
function pointer toegether and storing it, connecting more signals.

struct button
{
  sigc::signal<void(int)> signal_clicked;
  button(){ signal_clicked.connect(sigc::bind(*this, &button::on_clicked));
};
  virtual void on_clicked(int){}
  virtual ~button(){}
};

struct mybutton: button
{
  void on_clicked(int i){ cout<< i; }
}

and somewhere in the event loop you dispatch the signal_clicked signal like:
signal_clicked(3);


That's essentially how gtkmm works.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20080212/0c1e4611/attachment.htm 


More information about the SDL mailing list