25th: Do event distribution processing in Soui

Source: Internet
Author: User

Different Soui controls can produce different events. Two types of event handling are available in the Soui system: Event subscription + Event handling mapping table (see Eighth: Response to control events in Soui)

Event subscriptions because of the direct connection of events and event handlers, there is no issue of event distribution, which mainly describes event distribution when using event mapping tables.

Before you answer this question, first look at what is event distribution.

In a large project, the program logic can be very complex, and if you set the event handling of controls in all the UI in a message/event map table, the maintainability of the code becomes very poor. The common way to solve this problem is to classify events (such as classification by source), and different categories of events are handled by a separate event handler, which is the core of event distribution.

The current popular UI usually uses the tab control to organize the UI, different functions are placed in different tab pages, different tab pages may be irrelevant to the functional modules, for similar situations it is natural to think of the event distribution mechanism to implement the logic between modules decoupling (such as the UI used in Sotool).

In the UI above, although the entire UI is tabbed into 6 pages, 6 pages exist in the same host window.

In general, if the UI is relatively straightforward, we recommend that you handle control events uniformly in the event-handling mapping table of the host window.

However, when such a complex interface is present, it is best to handle the event handling of different function pages separately in different objects.

In MFC, a class to process messages, this class is usually derived from ccmdtarget (may be mistaken, too long without MFC), the message received by the main window is automatically routed to this message processing object.

In WTL, WTL provides a set of message-map macros: Chain_msg_map,chain_msg_map_member, and so on, to distribute messages to any C + + object that also implements the message map table.

Soui event Distribution uses a similar mechanism for WTL message distribution, and also constructs event mapping tables using event map macros, which are some of the major and event distribution related macros in Soui:

#defineEvent_map_begin ()protected:                                              VirtualBOOL _handleevent (Soui::eventargs *pevt) {UINT UCode= pevt->GetID (); #defineEvent_map_declear ()protected:                                              VirtualBOOL _handleevent (Soui::eventargs *pevt); #defineEvent_map_begin2 (classname) \BOOL classname::_handleevent (Soui::eventargs*pevt) {UINT UCode= pevt->GetID (); #defineEvent_map_end ()return__super::_handleevent (PEVT); }                                               #defineEvent_map_break ()returnFALSE; }                                                #defineChain_event_map (Chainclass)if(Chainclass::_handleevent (pevt))returnTRUE; #defineChain_event_map_member (Thechainmember) \    {                                                   if(Thechainmember._handleevent (pevt))returnTRUE; }#defineEvent_check_sender_root (proot) \{Swindow*pwnd = sobj_cast<swindow> (pevt->sender); if(!pwnd->isdescendant (proot))returnFALSE; }//void OnEvent (EventArgs *pevt)#defineEvent_handler (CD, func)if(cd = =UCode) {func (pevt);returnTRUE; } 

The following is the event handling in Maindlg in Sotool:

    //Soui MessageEvent_map_begin () Event_name_command (L"Btn_close", OnClose) Event_name_command (L"Btn_min", Onminimize) Event_name_command (L"Btn_max", Onmaximize) Event_name_command (L"Btn_restore", Onrestore) Chain_event_map_member (M_imgmergerhandler) chain_event_map_member (m_codelinecounter) CHAIN_EVENT_MAP_MEMBER ( M_2unicodehandler) Chain_event_map_member (M_folderscanhandler) chain_event_map_member (M_calcMd5Handler) E Vent_map_end ()

In the above code, the two macros, Event_map_begin () and Event_map_end (), construct an empty event handler that automatically handles the unhandled event to the base class's event handler.

If there is no event handler in the base class, it is clear that this event mapping table compilation cannot pass, at which point Soui provides another event_map_break () instead.

In the above event sub-publication, I used the Chain_event_map_member macro to pass control events from different pages to different event-handling objects.

The following code is the M_imgmergerhandler object header file.

classCimagemergerhandler: PublicIfiledrophandler{friendclassCmaindlg; Public: Cimagemergerhandler (void); ~cimagemergerhandler (void); voidOnInit (Swindow *proot); voidAddFile (lpcwstr pszfilename);protected:    Virtual voidOnfiledropdown (Hdrop hdrop); voidOnSave (); voidonclear (); voidOnmodehorz (); voidOnmodevert (); Event_map_begin () event_check_sender_root (m_ppageroot) Event_name_command (L"Btn_save", OnSave) Event_name_command (L"Btn_clear", OnClear) Event_name_command (L"Radio_horz", Onmodehorz) Event_name_command (L"Radio_vert", Onmodevert) Event_map_break () Swindow*M_ppageroot; Simgcanvas*M_pimgcanvas;};

You can see that the event mapping table here uses the Event_map_break to end.

In Soui, it is recommended to use the Name property of the control to identify a control (the Name property is a wchar* string, and using name is less efficient than an integer id attribute compared to a string comparison on event distribution, the advantage is that the code is readable), How do you identify controls in different pages if the same name appears?

A little trick is used in Soui: Implement an OnInit function in the event handler, which is called when processing Wm_initdialog in Maindlg, and a pointer to the root node of the page is saved in the OnInit:

Swindow *m_ppageroot;

At the beginning of the event mapping table, we used the Event_check_sender_root (M_PPAGEROOT) macro to identify events from this page. If the event is from another page, it is not processed.

25th: Do event distribution processing in Soui

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.