FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Message maps vs. virtual functions

 
Post new topic   Reply to topic     Forum Index -> DFL
View previous topic :: View next topic  
Author Message
nanobyte



Joined: 13 Jul 2006
Posts: 2

PostPosted: Thu Jul 13, 2006 6:12 am    Post subject: Message maps vs. virtual functions Reply with quote

I'm a little confused about which approach is best. Proponents of message maps seem to claim that if you didn't use them, you'd have to create one virtual function per message in a base window class.

Isn't that simply ridiculous? Wouldn't it be possible to create exactly *one* virtual window procedure in a base window class, then create *only* virtual functions for the messages a derived class is interested in, for example "HandleClick" in a button class and leave all other handling to the one and only virtual window procedure?

DFL sems to use this approach, am I correct?
Back to top
View user's profile Send private message
Chris Miller



Joined: 27 Mar 2004
Posts: 514
Location: The Internet

PostPosted: Thu Jul 13, 2006 10:55 am    Post subject: Re: Message maps vs. virtual functions Reply with quote

nanobyte wrote:
Wouldn't it be possible to create exactly *one* virtual window procedure in a base window class, then create *only* virtual functions for the messages a derived class is interested in, for example "HandleClick" in a button class and leave all other handling to the one and only virtual window procedure?

DFL sems to use this approach, am I correct?


DFL has a virtual function for each of the events, called by the virtual window procedure. In a derived class, you can override such virtual functions you wish to handle. You also have the option to simply attach delegates to event handlers, which does not require deriving; you can even attach to events of unrelated objects.

Code:

// Overriding virtual function:
class MyControl: Control
{
   override void onClick(EventArgs ea)
   {
      // override virtual function
      // Note: should also call super.onClick(ea) so that click events fire.
   }
}

// Attaching to event:
Control ctrl = some_control;
ctrl.click ~= &mydelegate;
Back to top
View user's profile Send private message
nanobyte



Joined: 13 Jul 2006
Posts: 2

PostPosted: Thu Jul 13, 2006 11:33 am    Post subject: Reply with quote

Yes. So it is basically like this (?):

Code:


class BasicWindow
{
   protected virtual void OnResize(ResizeEventArgs e) // Introduced by BasicWindow
   {
      // ... fire event here ...
   }

   public virtual void HandleMessage(Message message)
   {
      switch(message.osMessage)
      {
         case OS_RESIZE:
            ResizeEventArgs e = new ResizeEventArgs(message.osParamX, message.osParamY);
            OnResize(e);
            break;
         ...
      }
   }   
}

class MyWindow : BasicWindow
{

   protected override void OnResize(ResizeEventArgs e) // Introduced by BasicWindow
   {
      base.OnResize(e); // let base fire event
      Debug("I have been resized.");
   }

   protected virtual OnMouseOver(MouseOverEventArgs e) // Introduced by MyWindow
   {

   }

   
   public virtual  HandleMessage(Message message)
   {
      base.HandleMessage(message); // OS_RESIZE etc.

      switch(message.osMessage)
      {
         case OS_MOUSEOVER: // BasicWindow not interested in this, but we and our descendents are
            MouseOverEventArgs e = new MouseOverEventArgs(message.osParamX, message.osParamY);
            OnMouseOver(e);
            break;
         ...
      }
   }
}

Back to top
View user's profile Send private message
Chris Miller



Joined: 27 Mar 2004
Posts: 514
Location: The Internet

PostPosted: Thu Jul 13, 2006 8:35 pm    Post subject: Reply with quote

Affirmative Mr. Green
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> DFL All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group