Silviu-Marius Ardelean's blog

a software engineer's web log

Dynamic popup and drop down menus for custom representations

Many applications allow dynamic customization for visual objects or data views. For instance, well known Internet Explorer application provides toolbars customization using a popup menu that appears when the user execute right click mouse action in toolbar zone area.

Internet Explorer sample menu

Other sample where this kind of menu is very useful is when it’s used in order to customize database data representation in Windows controls like List control or grid control. These kind of applications allow data filtering and show/hide columns using this kind of menu. The user just right click on control header and gets what he need.

Starting from this idea, I implemented a class CDynamicPopupMenu. This class allows an easy building of this kind of menus. I used if in a demo dialog base application over a list control.

my demo application

Internally, this class uses a STL container (std::map) with a data structure used in order to embed items menu properties. When the menu is built, menu’s behavior is implemented using these properties.

Add new menu item
The new item add menu method has next definition:

where:

  • item_id – represents internal item ID; the ID is used for menu customization, too;
  • parent_id – parent item ID used when we define a new items sub-group (a drop-down menu); the attribute value is 0 if menu item is a part of initial menu;
  • is_visible – this flag is used a item is checked / unchecked. In my demo application this flag is set true for all list control’s columns that we want to display. For “Select All” and “Check All” items this flag is false because we want to create new subgroup that contains new columns, but we don’t have “Select All” or “Check All” columns.
  • check_flag – this flag allow check/uncheck menu property;
  • has_child – if is true allows a subgroup definition (a new drop-down menu);
  • item_name – unicode menu item name;
  • enable_flag – defines if the item is enable or disable.
  • Add separator item
    Add separator item method definition looks like this:

    where:

  • item_id – menu item ID;
  • parent_id – parent item ID from the subgroup has started; the attribute value is 0 if menu item is a part of initial menu.
  • Menu add items sample
    In my demo application, in CtestPopupMenuDlg::SetDefaultMapValues(void) method, among other things, you can find next calls:

    Get menu internal data
    In order to access the internal data container (std::map) that stores all dynamic menu items you just can use next method:

    followed by:

    Create and display menu
    Menu creation must be done just after we add all menu items. The menu is displayed only after TrackPopupCustomMenu() call. The definition of this method looks like this:

    where:

  • point – mouse coordinates where the menu start building;
  • hWnd – parrent window handle where the menu is created.
  • Function’s return value is the menu IDs that was selected. If no item was selected the function returns 0.
    In my demo application, menus creation is called on list control right-click method (NM_RCLICK).

    As you can see, I’m calling TrackPopupCostumMenu(), using mouse point property when the user right-click over list control.
    I am saving list control handler, selected item ID and WM_NOTIFY value into a pointer to message notification structure NMHDR. Then I’m passing this pointer to OnNotify() method.
    Using WM_NOTIFY message and OnNotify() method, I inform parent control window that a new event was generated.

    I am calling GetItemCheckedFlag() if order to detect selected item check status (check / uncheck). Then, if item state means check I apply negation over this bool flag and I’m calling SetCheckedItemFlag() method. Finnaly this method produce changes in my control list, depending on my menu command (FillData() method).

    Menu interaction with parent window (list control)
    In my demo application, the interaction between dynamic menu and list control to be treated by FillData() method.
    In order to use CDynamicPopupMenu’s internal container data is need to initialize a DynamicMenuData pointer with GetDynamicMenuData()’s returned value.

    Using that pointer to internal menu data, I iterate over internal container, and for those items that are visible and selected set on true I insert columns in my list control.
    Similarly, when using such menus, the application can apply filters on real data.
    CDynamicPopupMenu class contains other useful methods. This kind of menu can be used in different situations in order to change application’s behavior.

    Download demo application: testPopupMenu (Visual C++ 2005 project)

    Share

    Versionable Object’s Serialization using MFC in non Document View applications

    Most existing applications operate with data that must be stored and loaded in different times and different locations. The data is stored in text or binary files with a well defined format.
    The Problem
    Initially, in the first version 1.0, an application operates with data structures that can be stored and loaded. But, next version (2.0) these data structures suffers changes. Some structure’s attributes are added and other could be removed. These things change files format and structure when a new file version is saved.
    Question: What happens when you are using an application version 2.0 and you are trying to load old files format (version 1.0)?
    Answer: Most cases should cause incompatibility troubles between the current new application and files in old format. This could throw exceptions and the application could have undefined behavior.
    That’s why the application must be written in order to be able to open both file versions.
    Solution
    In order to solve the compatibility issue exist many solutions, more or less professional. The recommended solution is the serialization.
    The serialization is a write / read object process to/from a persistent storage. Serialization it’s a good choice in order to maintain a good data structure. Many different frameworks offer serialization support. One of these is Microsoft Foundation Classes – MFC.

    If we want to use MFC serialization support, we can use a CArchive instance. This object, combined with a CFile instance provides a strong mechanism for objects serialization.

    Because, in different applications version, the file suffers significant structure changes we have to use MFC’s serialization concept called Versionable Schema.
    Versionable Schema means the using of CArchive class methods GetObjectSchema() and SetObjectSchema() and a constant VERSIONABLE_SCHEMA (that you can find it in afx.h file and has 0x80000000 value) combined with a OR LOGIC operator and the last application version number as a parameter of IMPLEMENT_SERIAL macro.
    The GetObjectSchema() method is used in order to detect stored objects version from a file that is loaded in our application. The complement of this method, SetObjectSchema() method, allow us to save the objects version.
    Different by the C++ I/O standard streams, the CArchive class is special designed only for objects serialization in binary files.

    In order to serialize a class’s objects we have to follow next steps:
    1. The class that we want to serialize has to be derived from the abstract class CObject (or other classes derived from CObject).
    2. Overwrite CObject’s Serialize() method.
    3. Use DECLARE_SERIAL macro in your class declaration.
    4. The serializable class has to have a default constructor, without arguments.
    5. Use IMPLEMENT_SERIAL macro in the implementation file of serializable class.

    More information you can find it here and in links of this page.

    But, from these steps until to a complete serialization and versionable application there are few significant steps to follow.
    Next, I will present to you a Dialog base sample application that supports serialization and is versionable.

      Sample application – SerAddressBook

    Next, I will present to you how you can create an address book application (based on a MFC Dialog application architecture).

    Suppose that initially our client requested us an address book that contaions: name, prename, address and phone number. But, once with the mobile phone and Internet area extensions our client needs two new fields for mobile phone number and for email address.
    File versions structure

    Our application with a file version 2 looks like this:
    Application window

    Because this is a demo application I kept on my window the possibility to save both version, using two radio buttons.
    A good application design helps us if we have new requirements and we have to change the application structure. The code changes have to be done without too many code interactions. Ideally, with add code only.
    That’s why, my application classes design looks like this:
    Classes Hierarchy

    Although Contact class and CAddressBook class are serializable, the objects serialization is implemented into Contact class.

      Contact class

    From Contact’s interface class you can observe:
    • I derived this class from the abstract CObject;
    DECLARE_SERIAL, macro calling;
    Serialize()‘s method declaration in order to overwrite the parent class;
    • Our class attributes.

    The last line represents an “alias” definition for a MFC list definition, used in order to store displayed data. This list is using for Contact object administration.
    In the implementation file we declare the IMPLEMENT_SERIAL macro and we are initializing the static variable with our current application version.

    Into this declaration you can observe the VERSIOABLE_SCHEMA constant combined on OR logic with 2 (my demo application last version). This, the third macro argument is essential for objects versioning, combined with CArchive::GetObjectSchema() and CArchive::SetObjectSchema().
    More details about this constant and it using process or about these methods you can find here.
    The implementation of Contacte::Serialize() looks like this:

    If CArchive constructor sets store-load flag on CArchive::store (save to file) then the code flow will follow true if’s block and objects data is sending to archive and stored in the file (including file version, too).
    When we want to open an existing file, our CArchive’s constructor receives CArcuhive::load flag and enter to else if’s block of Serialize(). Is extracting file version, and after that is loading all Contact objects.

      CAddressBook class

    CAddressBook class make the link between interface dialog class (CSerAddressBookDlg) and the serialized class Contact. This class contains a Contact objects list. CAddressBook class is administrating this contact list and is realizing load/store object.

    The interface of this class is looking like this:

    Into this declaration you can see the existence of a contact list instance (m_cContactsList). This class contains add, update or remove contact methods.

    Because our class has to be a serialized method we have to overwrite Serialize() method, the method has to me used by the client class ( in our case the interface class – CSerAddressBookDlg).

    Because CArchive class doesn’t provides any method or attribute in order to obtain the objects (I’m counting when I’m loading or storing), I decided to save the objects count into my files. That’s why, if I’m storing, I call next line:

    Same story, for file version, before starting Contact objects serialization:

    Then, into a while loop I’m iterating over the contact list. I am serializing the data and I’m storing to my new file.
    If I load a file from disk (else branch) then I follow next steps:
    • I’m cleaning contact list;
    • Get the objects count;
    • Get file version and serialize all objects for load;
    • Add all data to my Contact object list;

      CSerAddressBookDlg – The application interface class

    Once we have implemented this serialization mechanism the using of this one into our application became very easy.

    For instance, when the user wants to save into a file all he’s new data, he will call next method:

    As you can see, I have a CFile object that I’m using it, combined with a CArchive instance, for data storing to a file. Although my local CArchive instance receive as a first parameter the address of the file handler and the store flag CArchive::store.
    Next I call CAddressBook::Serialize() method and I’m closing the store operation.

    Loading file method, based on my Contact serialization mechanism looks like this:

    As you can see, I am creating a local CFile object, needed for reading operation. Although, I’m creating a local CArchive instance that received as constructor parameter the file handler address with CArchive::load flag.
    Then, I’m calling CAddressBook::Serialize() method. Is entering on else branch and finally we are disconnected the object from file.
    The last line contains PopulateList() call and is my populate list method. It populates my list control (a CListCtrl instance) with the file loaded data in order to display it into our dialog.

    Conclusions:
    The MFC’s Document View architecture offers complete serialization support. Each MDI/SDI application contains default serialization support. My demo solution presented is an adapted serialization version for dialog base applications.

    Download demo application: SerAddressBook (Visual C++ 2005 project)

    Share