Class outline

Previous: popup UI framework ] [ Top: popup UI framework ] [ Next: Implementation of the classes ]

Now, as you may well glean from this code, I don't normally work in C++. In fact, this will be the first serious program I will ever have written from scratch in C++. I've done MFC work, where the MFC Wizard generates a bunch of stupid code that you mostly don't use, then you plug some stuff into it -- but I've never just plain written C++ if you know what I mean.

But wxWindows is a C++ library. So here goes.

The first thing we do is to plan our classes. In a wxWindows app, there are at least two classes; one is the application itself (inherits from wxApp) and the other is a frame (inherits from wxFrame). The child windows of the frame are of various types and can be arranged either by giving them fixed positions, or declaring a sizer and letting it manage things. I'm doing the latter, and for general rich-text kinds of things I'm using wxHtmlWindow, which is a window class able to display a limited subset of HTML (enough to do most things you want to do in dialog-like situations anyway.) Queries and messages are displayed as a modified form of HTML (with embedded special widgets) so this makes a lot of sense.

So let's take care of all our include files first. Note that this is taken from the Life demo, and so there are provisions made for lots of compilers that I don't use. Thus this may or may not work for those compilers. Under Windows, I always use MSVC++ 5.0 because it works and I already own it. Your mileage may vary.
 
#ifndef _POPUP_H_
#define _POPUP_H_

#ifdef __GNUG__
    #pragma interface "life.h"
#endif

// for compilers that support precompilation, includes "wx/wx.h"
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#ifdef __WXMSW__
#include <wx/msw/taskbar.h>
#endif

Now that that's out of the way, let's declare our application class. For Windows, we want this application to sit quietly in the system tray, so we'll take care of that first: the system tray icon is a separate frame of class wxToolBarIcon. Note that off Windows this doesn't work, so we'll compile it conditionally.
 
#ifdef __WXMSW__
class PopupSystemTrayIcon: public wxTaskBarIcon
{
public:
    PopupSystemTrayIcon() {};

    virtual void OnMouseMove(wxEvent&);
    virtual void OnLButtonDown(wxEvent&);
    virtual void OnLButtonUp(wxEvent&);
    virtual void OnRButtonDown(wxEvent&);
    virtual void OnRButtonUp(wxEvent&);
    virtual void OnLButtonDClick(wxEvent&);
    virtual void OnRButtonDClick(wxEvent&);

    void OnMenuRestore(wxCommandEvent&);
    void OnMenuExit(wxCommandEvent&);
    void OnMenuQuery(wxCommandEvent&);

    void ProcessAdaptor(wxCommandEvent&);

DECLARE_EVENT_TABLE()
};
#endif
Now we define the application, which contains the system tray icon as a member (just for neatness).
 
class PopupApp: public wxApp
{
  public:
    bool OnInit();
    int OnExit();

  private:
#ifdef __WXMSW__
    PopupSystemTrayIcon   m_systemtrayicon;
#endif

};

DECLARE_APP(PopupApp)
#endif
In case you haven't seen C++ style before, this merely declares the members of the class which implements the application. The actual implementations of the functions are in the next file, here. Yeah, I'm not used to it, either. What object-oriented work I do is Python or Perl, where everything's done at runtime in a single file.

Onwards to our main window class. It defines some handy methods to handle static events (those events known at compile time, and not associated with dynamically configured adaptors.)
 
class PopupFrame : public wxFrame
{
public:
    PopupFrame();
    ~PopupFrame();

    wxSizer *top_sizer;
    wxHtmlWindow *html;

    // This macro takes care of compile-time events.
    DECLARE_EVENT_TABLE()

    // event handlers
    void OnMenu      (wxCommandEvent& event);
    void OnFileSave  (wxCommandEvent& event);
    void OnFilePrefs (wxCommandEvent& event);
    void OnHide      (wxCommandEvent& event);
    void OnClose     (wxCommandEvent& event);
    void OnQueryList (wxCommandEvent& event);
    void OnHelpAbout (wxCommandEvent& event);

    void ProcessAdaptor (wxCommandEvent& event);

};
To handle events defined at runtime (configurable menu entries and controls on messages or queries) we need a special event handler. This will intercept events not handled by the static event table, and call the appropriate functions. This is where we part company with the nice, safe wxWindows demo code and forge our way into the wilderness.
 
class AdaptorEventHandler : public wxEvtHandler
{
   public:
      virtual bool ProcessEvent(wxEvent& event);
};
So. Let's go implement, shall we?
Previous: popup UI framework ] [ Top: popup UI framework ] [ Next: Implementation of the classes ]


This code and documentation are released under the terms of the GNU license. They are additionally copyright (c) 2001, Vivtek. All rights reserved except those explicitly granted under the terms of the GNU license.