MatOCAD Logo

Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

plugin.cpp

Go to the documentation of this file.
00001 
00012 #include "../../include/wxIFM/plugin.h"
00013 
00014 #if IFM_USE_WX_RTTI
00015 IMPLEMENT_DYNAMIC_CLASS(wxIFMInterfacePluginBase, wxEvtHandler);
00016 IMPLEMENT_DYNAMIC_CLASS(wxIFMExtensionPluginBase, wxEvtHandler);
00017 #endif
00018 
00019 /*
00020 wxIFMInterfacePluginBase implementation
00021 */
00022 wxIFMInterfacePluginBase::wxIFMInterfacePluginBase()
00023     : wxEvtHandler(),
00024     m_manager(NULL),
00025     m_topExtensionPlugin(NULL)
00026 { }
00027 
00028 wxInterfaceManager *wxIFMInterfacePluginBase::GetManager()
00029 {
00030     return m_manager;
00031 }
00032 
00033 const wxIFMComponentArray &wxIFMInterfacePluginBase::GetComponents() const
00034 {
00035     return m_components;
00036 }
00037 
00038 #if IFM_CANFLOAT
00039 const wxIFMFloatingWindowArray &wxIFMInterfacePluginBase::GetFloatingWindows() const
00040 {
00041     return m_floatingWindows;
00042 }
00043 #endif
00044 
00045 bool wxIFMInterfacePluginBase::Initialize(wxInterfaceManager *manager)
00046 {
00047     m_manager = manager;
00048 
00049     // connect events
00050 #if IFM_USE_NEW_EVENTS
00051     GetManager()->Connect(wxID_ANY, wxEVT_PAINT, wxPaintEventHandler(wxIFMInterfacePluginBase::OnPaint), NULL, this);
00052     GetManager()->Connect(wxID_ANY, wxEVT_SET_CURSOR, wxSetCursorEventHandler(wxIFMInterfacePluginBase::OnSetCursor), NULL, this);
00053     GetManager()->Connect(wxID_ANY, wxEVT_MOTION, wxMouseEventHandler(wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00054     GetManager()->Connect(wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler(wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00055     GetManager()->Connect(wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler(wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00056     GetManager()->Connect(wxID_ANY, wxEVT_MIDDLE_DOWN, wxMouseEventHandler(wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00057     GetManager()->Connect(wxID_ANY, wxEVT_MIDDLE_UP, wxMouseEventHandler(wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00058     GetManager()->Connect(wxID_ANY, wxEVT_RIGHT_DOWN, wxMouseEventHandler(wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00059     GetManager()->Connect(wxID_ANY, wxEVT_RIGHT_UP, wxMouseEventHandler(wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00060     GetManager()->Connect(wxID_ANY, wxEVT_LEFT_DCLICK, wxMouseEventHandler(wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00061     GetManager()->Connect(wxID_ANY, wxEVT_MIDDLE_DCLICK, wxMouseEventHandler(wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00062     GetManager()->Connect(wxID_ANY, wxEVT_RIGHT_DCLICK, wxMouseEventHandler(wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00063     GetManager()->Connect(wxID_ANY, wxEVT_MOUSEWHEEL, wxMouseEventHandler(wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00064     GetManager()->Connect(wxID_ANY, wxEVT_KEY_DOWN, wxKeyEventHandler(wxIFMInterfacePluginBase::OnKeyDown), NULL, this);
00065     GetManager()->Connect(wxID_ANY, wxEVT_KEY_UP, wxKeyEventHandler(wxIFMInterfacePluginBase::OnKeyUp), NULL, this);
00066     GetManager()->Connect(wxID_ANY, wxEVT_SIZE, wxSizeEventHandler(wxIFMInterfacePluginBase::OnSize), NULL, this);
00067     GetManager()->Connect(wxID_ANY, wxEVT_SET_FOCUS, wxFocusEventHandler(wxIFMInterfacePluginBase::OnFocus), NULL, this);
00068     GetManager()->Connect(wxID_ANY, wxEVT_KILL_FOCUS, wxFocusEventHandler(wxIFMInterfacePluginBase::OnFocus), NULL, this);
00069     GetManager()->Connect(wxID_ANY, wxEVT_ACTIVATE, wxActivateEventHandler(wxIFMInterfacePluginBase::OnActivate), NULL, this);
00070 #else
00071     GetManager()->Connect(wxID_ANY, wxEVT_PAINT, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnPaint), NULL, this);
00072     GetManager()->Connect(wxID_ANY, wxEVT_SET_CURSOR, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnSetCursor), NULL, this);
00073     GetManager()->Connect(wxID_ANY, wxEVT_MOTION, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00074     GetManager()->Connect(wxID_ANY, wxEVT_LEFT_DOWN, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00075     GetManager()->Connect(wxID_ANY, wxEVT_LEFT_UP, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00076     GetManager()->Connect(wxID_ANY, wxEVT_MIDDLE_DOWN, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00077     GetManager()->Connect(wxID_ANY, wxEVT_MIDDLE_UP, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00078     GetManager()->Connect(wxID_ANY, wxEVT_RIGHT_DOWN, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00079     GetManager()->Connect(wxID_ANY, wxEVT_RIGHT_UP, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00080     GetManager()->Connect(wxID_ANY, wxEVT_LEFT_DCLICK, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00081     GetManager()->Connect(wxID_ANY, wxEVT_MIDDLE_DCLICK, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00082     GetManager()->Connect(wxID_ANY, wxEVT_RIGHT_DCLICK, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00083     GetManager()->Connect(wxID_ANY, wxEVT_MOUSEWHEEL, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnMouseEvent), NULL, this);
00084     GetManager()->Connect(wxID_ANY, wxEVT_KEY_DOWN, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnKeyDown), NULL, this);
00085     GetManager()->Connect(wxID_ANY, wxEVT_KEY_UP, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnKeyUp), NULL, this);
00086     GetManager()->Connect(wxID_ANY, wxEVT_SIZE, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnSize), NULL, this);
00087     GetManager()->Connect(wxID_ANY, wxEVT_SET_FOCUS, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnFocus), NULL, this);
00088     GetManager()->Connect(wxID_ANY, wxEVT_KILL_FOCUS, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnFocus), NULL, this);
00089     GetManager()->Connect(wxID_ANY, wxEVT_ACTIVATE, (wxObjectEventFunction)(&wxIFMInterfacePluginBase::OnActivate), NULL, this);
00090 #endif
00091     return true;
00092 }
00093 
00094 void wxIFMInterfacePluginBase::Shutdown()
00095 {
00096     // remove extensions
00097     PopAllExtensionPlugins();
00098 }
00099 
00100 bool wxIFMInterfacePluginBase::ProcessPluginEvent(wxIFMPluginEvent &event)
00101 {
00102     // initialize the component member for child events
00103     if( event.GetEventType() == wxEVT_IFM_SHOWCHILD || event.GetEventType() == wxEVT_IFM_HIDECHILD ||
00104         event.GetEventType() == wxEVT_IFM_SETCHILDSIZE || event.GetEventType() == wxEVT_IFM_QUERYCHILD)
00105     {
00106 #if IFM_USE_WX_RTTI
00107         wxIFMChildEvent *evt = wxDynamicCast(&event, wxIFMChildEvent);
00108 #else
00109         wxIFMChildEvent *evt = dynamic_cast<wxIFMChildEvent*>(&event);
00110 #endif
00111         if( evt )
00112         {
00113             evt->SetComponent(FindChildWindow(evt->GetChild()));
00114             return GetTopPlugin()->ProcessEvent(*evt);
00115         }
00116     }
00117 
00118     // process the event
00119     bool ret = GetTopPlugin()->ProcessEvent(event);
00120 
00121     if( event.GetEventType() == wxEVT_IFM_NEWCOMPONENT )
00122     {
00123 #if IFM_USE_WX_RTTI
00124         wxIFMNewComponentEvent *evt = wxDynamicCast(&event, wxIFMNewComponentEvent);
00125 #else
00126         wxIFMNewComponentEvent *evt = dynamic_cast<wxIFMNewComponentEvent*>(&event);
00127 #endif
00128         if( evt )
00129             m_components.push_back(evt->GetComponent());
00130     }
00131     else if( event.GetEventType() == wxEVT_IFM_DELETECOMPONENT )
00132     {
00133 #if IFM_USE_WX_RTTI
00134         wxIFMDeleteComponentEvent *evt = wxDynamicCast(&event, wxIFMDeleteComponentEvent);
00135 #else
00136         wxIFMDeleteComponentEvent *evt = dynamic_cast<wxIFMDeleteComponentEvent*>(&event);
00137 #endif
00138         if( evt )
00139         {
00140             wxIFMComponent *component = evt->GetComponent();
00141             // find and remove this component from the array
00142             //for( int i = 0, count = m_components.GetCount(); i < count; ++i )
00143             for( wxIFMComponentArray::iterator i = m_components.begin(), end = m_components.end(); i != end; ++i )
00144             {
00145                 if( *i /*m_components[i]*/ == component )
00146                 {
00147                     //m_components.RemoveAt(i);
00148                     m_components.erase(i);
00149                     break;
00150                 }
00151             }
00152             delete component;
00153         }
00154     }
00155 
00156     return ret;
00157 }
00158 
00159 void wxIFMInterfacePluginBase::AddPendingPluginEvent(wxIFMPluginEvent &event)
00160 {
00161     GetTopPlugin()->AddPendingEvent(event);
00162 }
00163 
00164 wxIFMInterfacePluginBase::~wxIFMInterfacePluginBase()
00165 {
00166     // memory cleanup
00167     PopAllExtensionPlugins();
00168 
00169     // delete components
00170     for( int i = 0, count = m_components.GetCount(); i < count; ++i )
00171     //for( wxIFMComponentArray::const_iterator i = m_components.begin(), end = m_components.end(); i != end; ++i )
00172         delete m_components[i];
00173         //delete *i;
00174 }
00175 
00176 bool wxIFMInterfacePluginBase::PushExtensionPlugin(wxIFMExtensionPluginBase *plugin)
00177 {
00178     // initialize the plugin
00179     if( !plugin->Initialize(this) )
00180         return false;
00181 
00182     // link event handlers
00183     if( m_topExtensionPlugin )
00184     {
00185         plugin->SetNextHandler(m_topExtensionPlugin);
00186         m_topExtensionPlugin->SetPreviousHandler(plugin);
00187     }
00188     else
00189     {
00190         // link the first plugin to ourself
00191         plugin->SetNextHandler(this);
00192         SetPreviousHandler(plugin);
00193     }
00194 
00195     m_topExtensionPlugin = plugin;
00196     return true;
00197 }
00198 
00199 void wxIFMInterfacePluginBase::PopExtensionPlugin()
00200 {
00201     wxASSERT_MSG(m_topExtensionPlugin, wxT("Popping from an empty plugin list"));
00202 
00203     m_topExtensionPlugin->Shutdown();
00204 
00205     // re-link event handlers
00206     wxEvtHandler *plugin = m_topExtensionPlugin->GetNextHandler();
00207 
00208     if( plugin )
00209         plugin->SetPreviousHandler(NULL);
00210     else
00211         SetPreviousHandler(NULL); // unlink the last plugin from ourself
00212 
00213     delete m_topExtensionPlugin;
00214 #if IFM_USE_WX_RTTI
00215     m_topExtensionPlugin = wxDynamicCast(plugin, wxIFMExtensionPluginBase);
00216 #else
00217     m_topExtensionPlugin = dynamic_cast<wxIFMExtensionPluginBase*>(plugin);
00218 #endif
00219 }
00220 
00221 void wxIFMInterfacePluginBase::PopAllExtensionPlugins()
00222 {
00223     while(m_topExtensionPlugin)
00224         PopExtensionPlugin();
00225 }
00226 
00227 wxEvtHandler *wxIFMInterfacePluginBase::GetTopPlugin()
00228 {
00229     if( m_topExtensionPlugin )
00230         return m_topExtensionPlugin;
00231     else
00232         return this;
00233 }
00234 
00235 void wxIFMInterfacePluginBase::Enable(bool val)
00236 {
00237     wxEvtHandler::SetEvtHandlerEnabled(val);
00238 
00239     // enable or disable all extension plugins
00240     wxIFMExtensionPluginBase *plugin = m_topExtensionPlugin;
00241     while(plugin)
00242     {
00243         plugin->Enable(val);
00244 #if IFM_USE_WX_RTTI
00245         plugin = wxDynamicCast(plugin->GetNextHandler(), wxIFMExtensionPluginBase);
00246 #else
00247         plugin = dynamic_cast<wxIFMExtensionPluginBase*>(plugin->GetNextHandler());
00248 #endif
00249     }
00250 
00251     // enable or disable the default plugin too
00252     GetNextHandler()->SetEvtHandlerEnabled(val);
00253 }
00254 
00255 wxIFMComponent *wxIFMInterfacePluginBase::GetComponentByPos(const wxPoint &pos, wxIFMComponent *component, bool floating)
00256 {
00257 #if IFM_CANFLOAT
00258     if( floating )
00259     {
00260         wxIFMComponent *component = GetFloatingComponentByPos(m_manager->GetParent()->ClientToScreen(pos));
00261         if( component )
00262             return component;
00263     }
00264 #endif
00265 
00266     if( component == NULL )
00267     {
00268         wxIFMComponent *ret;
00269         for( int i = 0, count = m_components.GetCount(); i < count; ++i )
00270         //for( wxIFMComponentArray::const_iterator i = m_components.begin(), end = m_components.end(); i != end; ++i )
00271         {
00272             ret = GetComponentByPos(pos, m_components[i]);
00273             if( ret )
00274                 return ret;
00275         }
00276         return NULL;
00277     }
00278 
00279     //wxIFMHitTestEvent evt(component, IFM_COORDS_ABSOLUTE, pos);
00280     //ProcessPluginEvent(evt);
00281 
00282     //if( evt.GetPassed() )
00283     if( !component->m_hidden && component->m_rect.Inside(pos) )
00284     {
00285         // check children
00286         wxIFMComponent *ret;
00287         const wxIFMComponentArray &components = component->m_children;
00288         for( int i = 0, count = components.GetCount(); i < count; ++i )
00289         //for( wxIFMComponentArray::const_iterator i = components.begin(), end = components.end(); i != end; ++i )
00290         {
00291             ret = GetComponentByPos(pos, components[i]);
00292             if( ret )
00293                 return ret;
00294         }
00295         return component;
00296     }
00297 
00298     return NULL;
00299 }
00300 
00301 #if IFM_CANFLOAT
00302 wxIFMComponent *wxIFMInterfacePluginBase::GetFloatingComponentByPos(const wxPoint &pos)
00303 {
00304     wxIFMFloatingWindowBase *base;
00305     wxIFMComponent *ret;
00306 
00307     for( int i = 0, size = m_floatingWindows.GetCount(); i < size; ++i )
00308     //for( wxIFMFloatingWindowArray::const_iterator i = m_floatingWindows.begin(), end = m_floatingWindows.end(); i != end; ++i )
00309     {
00310         base = m_floatingWindows[i];
00311         //base = *i;
00312 
00313         ret = GetComponentByPos(base->GetWindow()->ScreenToClient(pos), base->GetComponent());
00314         if( ret )
00315             return ret;
00316     }
00317 
00318     return NULL;
00319 }
00320 #endif
00321 
00322 void wxIFMInterfacePluginBase::AddChildWindow(wxWindow *window, wxIFMComponent *component)
00323 {
00324     // store the child window / component pair
00325     m_windows[(int)window] = component;
00326 }
00327 
00328 wxIFMComponent *wxIFMInterfacePluginBase::FindChildWindow(wxWindow *window)
00329 {
00330     return m_windows[(int)window];
00331 }
00332 
00333 void wxIFMInterfacePluginBase::OnMouseEvent(wxMouseEvent &event)
00334 {
00335     wxEventType type = 0, _type = event.GetEventType();
00336 
00337     if( _type == wxEVT_LEFT_DOWN )
00338         type = wxEVT_IFM_LEFTDOWN;
00339     else if( _type == wxEVT_LEFT_UP )
00340         type = wxEVT_IFM_LEFTUP;
00341     else if( _type == wxEVT_LEFT_DCLICK )
00342         type = wxEVT_IFM_LEFTDCLICK;
00343     else if( _type == wxEVT_RIGHT_DOWN )
00344         type = wxEVT_IFM_RIGHTDOWN;
00345     else if( _type == wxEVT_RIGHT_UP )
00346         type = wxEVT_IFM_RIGHTUP;
00347     else if( _type == wxEVT_RIGHT_DCLICK )
00348         type = wxEVT_IFM_RIGHTDCLICK;
00349     else if( _type == wxEVT_MIDDLE_DOWN )
00350         type = wxEVT_IFM_MIDDLEDOWN;
00351     else if( _type == wxEVT_MIDDLE_UP )
00352         type = wxEVT_IFM_MIDDLEUP;
00353     else if( _type == wxEVT_MIDDLE_DCLICK )
00354         type = wxEVT_IFM_MIDDLEDCLICK;
00355     else if( _type == wxEVT_MOTION )
00356         type = wxEVT_IFM_MOTION;
00357     else if( _type == wxEVT_MOUSEWHEEL )
00358         type = wxEVT_IFM_MOUSEWHEEL;
00359 
00360     // figure out which component the mouse is over
00361     wxIFMComponent *component = NULL;
00362 
00363     if( m_manager->IsInputCaptured() )
00364         component = m_manager->GetCapturedComponent();
00365     else
00366         component = GetComponentByPos(event.GetPosition());
00367 
00368     // generate mouse event
00369     wxIFMMouseEvent evt(type, component, event);
00370     if( !ProcessPluginEvent(evt) )
00371         event.Skip();
00372 }
00373 
00374 void wxIFMInterfacePluginBase::OnSetCursor(wxSetCursorEvent &event)
00375 {
00376     wxIFMSetCursorEvent evt(event, GetComponentByPos(wxPoint(event.GetX(), event.GetY())));
00377     if( !ProcessPluginEvent(evt) )
00378         event.Skip();
00379 }
00380 
00381 void wxIFMInterfacePluginBase::OnKeyDown(wxKeyEvent &event)
00382 {
00383     wxIFMKeyEvent evt(wxEVT_IFM_KEYDOWN, m_manager->GetCapturedComponent(), event);
00384     if( !ProcessPluginEvent(evt) )
00385         event.Skip();
00386 }
00387 
00388 void wxIFMInterfacePluginBase::OnKeyUp(wxKeyEvent &event)
00389 {
00390     wxIFMKeyEvent evt(wxEVT_IFM_KEYUP, m_manager->GetCapturedComponent(), event);
00391     if( !ProcessPluginEvent(evt) )
00392         event.Skip();
00393 }
00394 
00395 void wxIFMInterfacePluginBase::OnPaint(wxPaintEvent &event)
00396 {
00397     // send BEGINPAINT message to get a DC with which to paint
00398     wxIFMBeginPaintEvent beginpaint(m_manager->GetParent());
00399     ProcessPluginEvent(beginpaint);
00400 
00401     // make sure we have a valid dc
00402     wxDC *dc = beginpaint.GetDC();
00403     wxASSERT_MSG(dc, wxT("Invalid DC returned by EVT_IFM_BEGINPAINT"));
00404 
00405     // send paint interface event
00406     wxIFMPaintInterfaceEvent paintevt(*dc, GetManager()->GetParent()->GetUpdateRegion());
00407     ProcessPluginEvent(paintevt);
00408 
00409     // send ENDPAINT message to clean up the DC used to paint
00410     wxIFMEndPaintEvent endpaint(dc);
00411     ProcessPluginEvent(endpaint);
00412 
00413     // let the frame paint itself too
00414 #if !defined(__WXMAC__) && !defined(__WXCOCOA)
00415     event.Skip();
00416 #endif
00417 }
00418 
00419 void wxIFMInterfacePluginBase::OnSize(wxSizeEvent &event)
00420 {
00421     // update the interface
00422     if( m_manager->GetFlags() & IFM_NO_RESIZE_UPDATE )
00423         event.Skip();
00424     else
00425         m_manager->Update();
00426 }
00427 
00428 void wxIFMInterfacePluginBase::OnFocus(wxFocusEvent &event)
00429 {
00430     // send focus event
00431     wxIFMFocusEvent evt((event.GetEventType() == wxEVT_SET_FOCUS) ? wxEVT_IFM_SETFOCUS : wxEVT_IFM_KILLFOCUS, event);
00432     if( !ProcessPluginEvent(evt) )
00433         event.Skip();
00434 }
00435 
00436 void wxIFMInterfacePluginBase::OnActivate(wxActivateEvent &event)
00437 {
00438     wxIFMActivateEvent evt(event);
00439     if( !ProcessPluginEvent(evt) )
00440         event.Skip();
00441 }
00442 
00443 /*
00444 wxIFMExtensionPluginBase implementation
00445 */
00446 wxIFMExtensionPluginBase::wxIFMExtensionPluginBase()
00447     : wxEvtHandler(),
00448     m_ip(NULL)
00449 { }
00450 
00451 wxIFMExtensionPluginBase::~wxIFMExtensionPluginBase()
00452 { }
00453 
00454 wxInterfaceManager *wxIFMExtensionPluginBase::GetManager()
00455 {
00456     return m_ip->GetManager();
00457 }
00458 
00459 wxIFMInterfacePluginBase *wxIFMExtensionPluginBase::GetIP()
00460 {
00461     return m_ip;
00462 }
00463 
00464 bool wxIFMExtensionPluginBase::Initialize(wxIFMInterfacePluginBase *plugin)
00465 {
00466     wxASSERT_MSG(!m_ip, wxT("Interface plugin has already been set!"));
00467     m_ip = plugin;
00468     return true;
00469 }
00470 
00471 void wxIFMExtensionPluginBase::Shutdown()
00472 { }
00473 
00474 void wxIFMExtensionPluginBase::Enable(bool val)
00475 {
00476     wxEvtHandler::SetEvtHandlerEnabled(val);
00477 }
00478 
00479 void wxIFMExtensionPluginBase::Disable()
00480 {
00481     Enable(false);
00482 }

 

SourceForge Logo