]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/ownerdrw/ownerdrw.cpp
Rebaked makefiles after htmllistbox and collapsable panel additions.
[wxWidgets.git] / samples / ownerdrw / ownerdrw.cpp
index f27293e7eaf303c2360ed7959f02b371649761ca..3446f71c14b058938eb111d6db9b2b9fd976b981 100644 (file)
@@ -2,7 +2,7 @@
 // Name:        ownerdrw.cpp
 // Purpose:     Owner-draw sample, for Windows
 // Author:      Vadim Zeitlin
 // Name:        ownerdrw.cpp
 // Purpose:     Owner-draw sample, for Windows
 // Author:      Vadim Zeitlin
-// Modified by: 
+// Modified by:
 // Created:     13.11.97
 // RCS-ID:      $Id$
 // Copyright:   (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
 // Created:     13.11.97
 // RCS-ID:      $Id$
 // Copyright:   (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
@@ -26,7 +26,7 @@
 
 #include  "wx/ownerdrw.h"
 #include  "wx/menuitem.h"
 
 #include  "wx/ownerdrw.h"
 #include  "wx/menuitem.h"
-#include  "wx/msw/checklst.h"
+#include  "wx/checklst.h"
 
 // Define a new application type
 class OwnerDrawnApp: public wxApp
 
 // Define a new application type
 class OwnerDrawnApp: public wxApp
@@ -40,16 +40,17 @@ class OwnerDrawnFrame : public wxFrame
 {
 public:
     // ctor & dtor
 {
 public:
     // ctor & dtor
-    OwnerDrawnFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
-    ~OwnerDrawnFrame();
+    OwnerDrawnFrame(wxFrame *frame, const wxChar *title, int x, int y, int w, int h);
+    ~OwnerDrawnFrame(){};
 
     // notifications
     void OnQuit             (wxCommandEvent& event);
 
     // notifications
     void OnQuit             (wxCommandEvent& event);
+    void OnMenuToggle       (wxCommandEvent& event);
     void OnAbout            (wxCommandEvent& event);
     void OnListboxSelect    (wxCommandEvent& event);
     void OnCheckboxToggle   (wxCommandEvent& event);
     void OnListboxDblClick  (wxCommandEvent& event);
     void OnAbout            (wxCommandEvent& event);
     void OnListboxSelect    (wxCommandEvent& event);
     void OnCheckboxToggle   (wxCommandEvent& event);
     void OnListboxDblClick  (wxCommandEvent& event);
-    bool OnClose            ()                        { return TRUE; }
+    bool OnClose            ()                        { return true; }
 
     DECLARE_EVENT_TABLE()
 
 
     DECLARE_EVENT_TABLE()
 
@@ -57,148 +58,175 @@ private:
     void InitMenu();
 
     wxCheckListBox *m_pListBox;
     void InitMenu();
 
     wxCheckListBox *m_pListBox;
+    wxMenuItem *pAboutItem;
 };
 
 };
 
-enum 
+enum
 {
 {
-    Menu_Quit = 1, 
+    Menu_Quit = 1,
     Menu_First = 100,
     Menu_First = 100,
-    Menu_Test1, Menu_Test2, Menu_Test3, 
-    Menu_Bitmap, Menu_Bitmap2, 
+    Menu_Test1, Menu_Test2, Menu_Test3,
+    Menu_Bitmap, Menu_Bitmap2,
     Menu_Submenu, Menu_Sub1, Menu_Sub2, Menu_Sub3,
     Menu_Submenu, Menu_Sub1, Menu_Sub2, Menu_Sub3,
+    Menu_Toggle, Menu_About,
     Control_First = 1000,
     Control_First = 1000,
-    Control_Listbox, Control_Listbox2,
+    Control_Listbox, Control_Listbox2
 };
 
 BEGIN_EVENT_TABLE(OwnerDrawnFrame, wxFrame)
 };
 
 BEGIN_EVENT_TABLE(OwnerDrawnFrame, wxFrame)
+    EVT_MENU(Menu_Toggle, OwnerDrawnFrame::OnMenuToggle)
+    EVT_MENU(Menu_About, OwnerDrawnFrame::OnAbout)
     EVT_MENU(Menu_Quit, OwnerDrawnFrame::OnQuit)
     EVT_LISTBOX(Control_Listbox, OwnerDrawnFrame::OnListboxSelect)
     EVT_CHECKLISTBOX(Control_Listbox, OwnerDrawnFrame::OnCheckboxToggle)
     EVT_MENU(Menu_Quit, OwnerDrawnFrame::OnQuit)
     EVT_LISTBOX(Control_Listbox, OwnerDrawnFrame::OnListboxSelect)
     EVT_CHECKLISTBOX(Control_Listbox, OwnerDrawnFrame::OnCheckboxToggle)
-    EVT_COMMAND(Control_Listbox, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, 
+    EVT_COMMAND(Control_Listbox, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED,
                 OwnerDrawnFrame::OnListboxDblClick)
 END_EVENT_TABLE()
 
                 OwnerDrawnFrame::OnListboxDblClick)
 END_EVENT_TABLE()
 
-IMPLEMENT_APP(OwnerDrawnApp);
+IMPLEMENT_APP(OwnerDrawnApp)
 
 // init our app: create windows
 bool OwnerDrawnApp::OnInit(void)
 {
     OwnerDrawnFrame *pFrame
 
 // init our app: create windows
 bool OwnerDrawnApp::OnInit(void)
 {
     OwnerDrawnFrame *pFrame
-        = new OwnerDrawnFrame(NULL, "wxWindows Ownerdraw Sample",
+        = new OwnerDrawnFrame(NULL, _T("wxWidgets Ownerdraw Sample"),
                               50, 50, 450, 340);
 
     SetTopWindow(pFrame);
 
                               50, 50, 450, 340);
 
     SetTopWindow(pFrame);
 
-    return TRUE;
+    return true;
 }
 
 // create the menu bar for the main frame
 void OwnerDrawnFrame::InitMenu()
 {
     // Make a menubar
 }
 
 // create the menu bar for the main frame
 void OwnerDrawnFrame::InitMenu()
 {
     // Make a menubar
-    wxMenu *file_menu = new wxMenu,
-           *sub_menu  = new wxMenu;
+    wxMenuItem *pItem;
+    wxMenu *file_menu = new wxMenu;
+
+#ifndef __WXUNIVERSAL__
+    wxMenu *sub_menu  = new wxMenu;
 
     // vars used for menu construction
 
     // vars used for menu construction
-    wxMenuItem *pItem;
-    wxFont fontLarge(18, wxROMAN, wxNORMAL, wxBOLD, FALSE),
-           fontUlined(12, wxDEFAULT, wxNORMAL, wxNORMAL, TRUE),
-           fontItalic(12, wxMODERN, wxITALIC, wxBOLD, FALSE),
+    wxFont fontLarge(18, wxROMAN, wxNORMAL, wxBOLD, false),
+           fontUlined(12, wxDEFAULT, wxNORMAL, wxNORMAL, true),
+           fontItalic(12, wxMODERN, wxITALIC, wxBOLD, false),
            // should be at least of the size of bitmaps
            // should be at least of the size of bitmaps
-           fontBmp(14, wxDEFAULT, wxNORMAL, wxNORMAL, FALSE);
+           fontBmp(14, wxDEFAULT, wxNORMAL, wxNORMAL, false);
 
     // sorry for my artistic skills...
 
     // sorry for my artistic skills...
-    wxBitmap bmpBell("bell"), bmpSound("sound"), bmpNoSound("nosound");
+    wxBitmap bmpBell(_T("bell")),
+             bmpSound(_T("sound")),
+             bmpNoSound(_T("nosound")),
+             bmpInfo(_T("info")),
+             bmpInfo_mono(_T("info_mono"));
 
     // construct submenu
 
     // construct submenu
-    pItem = new wxMenuItem(sub_menu, Menu_Sub1, "Submenu &first", "large");
+    pItem = new wxMenuItem(sub_menu, Menu_Sub1, _T("Submenu &first"), _T("large"));
 
     pItem->SetFont(fontLarge);
     sub_menu->Append(pItem);
 
 
     pItem->SetFont(fontLarge);
     sub_menu->Append(pItem);
 
-    pItem = new wxMenuItem(sub_menu, Menu_Sub2, "Submenu &second", "italic",
+    pItem = new wxMenuItem(sub_menu, Menu_Sub2, _T("Submenu &second"), _T("italic"),
                            wxITEM_CHECK);
     pItem->SetFont(fontItalic);
     sub_menu->Append(pItem);
 
                            wxITEM_CHECK);
     pItem->SetFont(fontItalic);
     sub_menu->Append(pItem);
 
-    pItem = new wxMenuItem(sub_menu, Menu_Sub3, "Submenu &third", "underlined",
+    pItem = new wxMenuItem(sub_menu, Menu_Sub3, _T("Submenu &third"), _T("underlined"),
                            wxITEM_CHECK);
     pItem->SetFont(fontUlined);
     sub_menu->Append(pItem);
 
     // construct menu
                            wxITEM_CHECK);
     pItem->SetFont(fontUlined);
     sub_menu->Append(pItem);
 
     // construct menu
-    pItem = new wxMenuItem(file_menu, Menu_Test1, "&Uncheckable", "red item");
+    pItem = new wxMenuItem(file_menu, Menu_Test1, _T("&Uncheckable"), _T("red item"));
     pItem->SetFont(*wxITALIC_FONT);
     pItem->SetTextColour(wxColor(255, 0, 0));
     pItem->SetFont(*wxITALIC_FONT);
     pItem->SetTextColour(wxColor(255, 0, 0));
-    pItem->SetMarginWidth(23);
     file_menu->Append(pItem);
 
     file_menu->Append(pItem);
 
-    pItem = new wxMenuItem(file_menu, Menu_Test2, "&Checkable",
-                           "checkable item", wxITEM_CHECK);
+    pItem = new wxMenuItem(file_menu, Menu_Test2, _T("&Checkable"),
+                           _T("checkable item"), wxITEM_CHECK);
     pItem->SetFont(*wxSMALL_FONT);
     file_menu->Append(pItem);
     pItem->SetFont(*wxSMALL_FONT);
     file_menu->Append(pItem);
-    file_menu->Check(Menu_Test2, TRUE);
+    file_menu->Check(Menu_Test2, true);
 
 
-    pItem = new wxMenuItem(file_menu, Menu_Test3, "&Disabled", "disabled item");
+    pItem = new wxMenuItem(file_menu, Menu_Test3, _T("&Disabled"), _T("disabled item"));
     pItem->SetFont(*wxNORMAL_FONT);
     file_menu->Append(pItem);
     pItem->SetFont(*wxNORMAL_FONT);
     file_menu->Append(pItem);
-    file_menu->Enable(Menu_Test3, FALSE);
+    file_menu->Enable(Menu_Test3, false);
 
     file_menu->AppendSeparator();
 
 
     file_menu->AppendSeparator();
 
-    pItem = new wxMenuItem(file_menu, Menu_Bitmap, "&Bell",
-                           "check/uncheck me!", wxITEM_CHECK);
+    pItem = new wxMenuItem(file_menu, Menu_Bitmap, _T("&Bell"),
+                           _T("check/uncheck me!"), wxITEM_CHECK);
     pItem->SetFont(fontBmp);
     pItem->SetBitmaps(bmpBell);
     file_menu->Append(pItem);
 
     pItem->SetFont(fontBmp);
     pItem->SetBitmaps(bmpBell);
     file_menu->Append(pItem);
 
-    pItem = new wxMenuItem(file_menu, Menu_Bitmap2, "So&und",
-                           "icon changes!", wxITEM_CHECK);
+    pItem = new wxMenuItem(file_menu, Menu_Bitmap2, _T("So&und"),
+                           _T("icon changes!"), wxITEM_CHECK);
     pItem->SetFont(fontBmp);
     pItem->SetBitmaps(bmpSound, bmpNoSound);
     file_menu->Append(pItem);
 
     file_menu->AppendSeparator();
 
     pItem->SetFont(fontBmp);
     pItem->SetBitmaps(bmpSound, bmpNoSound);
     file_menu->Append(pItem);
 
     file_menu->AppendSeparator();
 
-    pItem = new wxMenuItem(file_menu, Menu_Submenu, "&Sub menu", "",
+    pItem = new wxMenuItem(file_menu, Menu_Submenu, _T("&Sub menu"), _T(""),
                            wxITEM_CHECK, sub_menu);
     pItem->SetFont(*wxSWISS_FONT);
     file_menu->Append(pItem);
 
     file_menu->AppendSeparator();
                            wxITEM_CHECK, sub_menu);
     pItem->SetFont(*wxSWISS_FONT);
     file_menu->Append(pItem);
 
     file_menu->AppendSeparator();
-    file_menu->Append(Menu_Quit, "&Quit", "Normal item");
+    pItem = new wxMenuItem(file_menu, Menu_Toggle, _T("&Disable/Enable\tCtrl+D"),
+                          _T("enables/disables the About-Item"), wxITEM_NORMAL);
+    pItem->SetFont(*wxNORMAL_FONT);
+    file_menu->Append(pItem);
+
+    // Of course Ctrl+RatherLongAccel will not work in this example:
+    pAboutItem = new wxMenuItem(file_menu, Menu_About, _T("&About\tCtrl+RatherLongAccel"),
+                                _T("display program information"), wxITEM_NORMAL);
+    pAboutItem->SetBitmap(bmpInfo);
+    pAboutItem->SetDisabledBitmap(bmpInfo_mono);
+    file_menu->Append(pAboutItem);
+
+    file_menu->AppendSeparator();
+#endif
+
+    pItem = new wxMenuItem(file_menu, Menu_Quit, _T("&Quit"), _T("Normal item"),
+                           wxITEM_NORMAL);
+    file_menu->Append(pItem);
 
     wxMenuBar *menu_bar = new wxMenuBar;
 
 
     wxMenuBar *menu_bar = new wxMenuBar;
 
-    menu_bar->Append(file_menu, "&File");
+    menu_bar->Append(file_menu, _T("&File"));
     SetMenuBar(menu_bar);
 }
 
 // main frame constructor
     SetMenuBar(menu_bar);
 }
 
 // main frame constructor
-OwnerDrawnFrame::OwnerDrawnFrame(wxFrame *frame, char *title,
+OwnerDrawnFrame::OwnerDrawnFrame(wxFrame *frame, const wxChar *title,
                                  int x, int y, int w, int h)
                                  int x, int y, int w, int h)
-         : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
+         : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
 {
     // set the icon
 {
     // set the icon
-    SetIcon(wxIcon("mondrian"));
+    SetIcon(wxIcon(_T("mondrian")));
 
     // create the menu
     InitMenu();
 
 
     // create the menu
     InitMenu();
 
+#if wxUSE_STATUSBAR
     // create the status line
     const int widths[] = { -1, 60 };
     CreateStatusBar(2);
     SetStatusWidths(2, widths);
     // create the status line
     const int widths[] = { -1, 60 };
     CreateStatusBar(2);
     SetStatusWidths(2, widths);
-    SetStatusText("no selection", 0);
+    SetStatusText(_T("no selection"), 0);
+#endif // wxUSE_STATUSBAR
 
     // make a panel with some controls
 
     // make a panel with some controls
-    wxPanel *pPanel = new wxPanel(this, -1, wxPoint(0, 0), 
-                                  wxSize(400, 200), wxTAB_TRAVERSAL);
+    wxPanel *pPanel = new wxPanel(this);
 
     // check list box
 
     // check list box
-    static const char* aszChoices[] = { "Hello", "world", "and", 
-                                      "goodbye", "cruel", "world",
-                                      "-------", "owner-drawn", "listbox" };
+    static const wxChar* aszChoices[] = { _T("Hello"), _T("world"), _T("and"),
+                                          _T("goodbye"), _T("cruel"), _T("world"),
+                                          _T("-------"), _T("owner-drawn"), _T("listbox") };
 
     wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)];
     unsigned int ui;
 
     wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)];
     unsigned int ui;
@@ -219,21 +247,17 @@ OwnerDrawnFrame::OwnerDrawnFrame(wxFrame *frame, char *title,
 
     for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 )
     {
 
     for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 )
     {
+#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
         m_pListBox->GetItem(ui)->SetBackgroundColour(wxColor(200, 200, 200));
         m_pListBox->GetItem(ui)->SetBackgroundColour(wxColor(200, 200, 200));
+#endif
     }
 
     m_pListBox->Check(2);
 
     // normal (but owner-drawn) listbox
     }
 
     m_pListBox->Check(2);
 
     // normal (but owner-drawn) listbox
-    static const char* aszColors[] = { "Red", "Blue", "Pink",
-                                       "Green", "Yellow", 
-                                       "Black", "Violet"  };
-    struct { unsigned int r, g, b; } aColors[] =
-        {
-            {255,0,0}, {0,0,255}, {255,128,192},
-            {0,255,0}, {255,255,128}, 
-            {0,0,0}, {128,0,255}
-        };
+    static const wxChar* aszColors[] = { _T("Red"), _T("Blue"), _T("Pink"),
+                                         _T("Green"), _T("Yellow"),
+                                         _T("Black"), _T("Violet")  };
 
     astrChoices = new wxString[WXSIZEOF(aszColors)];
 
 
     astrChoices = new wxString[WXSIZEOF(aszColors)];
 
@@ -247,72 +271,94 @@ OwnerDrawnFrame::OwnerDrawnFrame(wxFrame *frame, char *title,
             pPanel,              // parent
             Control_Listbox2,    // control id
             wxPoint(220, 10),    // listbox position
             pPanel,              // parent
             Control_Listbox2,    // control id
             wxPoint(220, 10),    // listbox position
-            wxDefaultSize,       // listbox size
-            WXSIZEOF(aszColors),   // number of strings
+            wxSize(200, 200),    // listbox size
+            WXSIZEOF(aszColors), // number of strings
             astrChoices,         // array of strings
             astrChoices,         // array of strings
-            wxLB_OWNERDRAW,      // owner-drawn
-            wxDefaultValidator,  //
-            wxListBoxNameStr
+            wxLB_OWNERDRAW       // owner-drawn
         );
 
         );
 
+#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
+
+    struct { unsigned char r, g, b; } aColors[] =
+        {
+            {255,0,0}, {0,0,255}, {255,128,192},
+            {0,255,0}, {255,255,128},
+            {0,0,0}, {128,0,255}
+        };
+
     for ( ui = 0; ui < WXSIZEOF(aszColors); ui++ )
     {
         pListBox->GetItem(ui)->SetTextColour(wxColor(aColors[ui].r,
     for ( ui = 0; ui < WXSIZEOF(aszColors); ui++ )
     {
         pListBox->GetItem(ui)->SetTextColour(wxColor(aColors[ui].r,
-                                                     aColors[ui].g, 
+                                                     aColors[ui].g,
                                                      aColors[ui].b));
         // yellow on white is horrible...
         if ( ui == 4 )
         {
             pListBox->GetItem(ui)->SetBackgroundColour(wxColor(0, 0, 0));
         }
                                                      aColors[ui].b));
         // yellow on white is horrible...
         if ( ui == 4 )
         {
             pListBox->GetItem(ui)->SetBackgroundColour(wxColor(0, 0, 0));
         }
-
     }
 
     }
 
+#else
+    wxUnusedVar( pListBox );
+#endif
+
     delete[] astrChoices;
 
     delete[] astrChoices;
 
-    Show(TRUE);
+    Show(true);
 }
 
 }
 
-OwnerDrawnFrame::~OwnerDrawnFrame()
+void OwnerDrawnFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
 {
 {
+    Close(true);
 }
 
 }
 
-void OwnerDrawnFrame::OnQuit(wxCommandEvent& event)
+void OwnerDrawnFrame::OnMenuToggle(wxCommandEvent& WXUNUSED(event))
 {
 {
-    Close(TRUE);
+    // This example shows the use of bitmaps in ownerdrawn menuitems and is not a good
+    // example on how to enable and disable menuitems - this should be done with the help of
+    // EVT_UPDATE_UI and EVT_UPDATE_UI_RANGE !
+    pAboutItem->Enable( pAboutItem->IsEnabled() ? false : true );
 }
 
 }
 
-void OwnerDrawnFrame::OnAbout(wxCommandEvent& event)
+void OwnerDrawnFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
 {
     wxMessageDialog dialog(this,
 {
     wxMessageDialog dialog(this,
-                           "Demo of owner-drawn controls\n",
-                           "About wxOwnerDrawn", wxYES_NO | wxCANCEL);
+                           _T("Demo of owner-drawn controls\n"),
+                           _T("About wxOwnerDrawn"), wxYES_NO | wxCANCEL);
     dialog.ShowModal();
 }
 
 void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent& event)
 {
     dialog.ShowModal();
 }
 
 void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent& event)
 {
+#if wxUSE_STATUSBAR
     wxString strSelection;
     unsigned int nSel = event.GetSelection();
     wxString strSelection;
     unsigned int nSel = event.GetSelection();
-    strSelection.sprintf(wxT("item %d selected (%schecked)"), nSel,
-                         m_pListBox->IsChecked(nSel) ? wxT("") : wxT("not "));
+    strSelection.Printf(wxT("item %d selected (%schecked)"), nSel,
+                        m_pListBox->IsChecked(nSel) ? wxT("") : wxT("not "));
     SetStatusText(strSelection);
     SetStatusText(strSelection);
+#else
+    wxUnusedVar(event);
+#endif // wxUSE_STATUSBAR
 }
 
 }
 
-void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent& event)
+void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent& WXUNUSED(event))
 {
     wxString strSelection;
 {
     wxString strSelection;
-    strSelection.sprintf(wxT("item %d double clicked"),
-                         m_pListBox->GetSelection());
+    strSelection.Printf(wxT("item %d double clicked"),
+                        m_pListBox->GetSelection());
     wxMessageDialog dialog(this, strSelection);
     dialog.ShowModal();
 }
 
 void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent& event)
 {
     wxMessageDialog dialog(this, strSelection);
     dialog.ShowModal();
 }
 
 void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent& event)
 {
+#if wxUSE_STATUSBAR
     wxString strSelection;
     unsigned int nItem = event.GetInt();
     wxString strSelection;
     unsigned int nItem = event.GetInt();
-    strSelection.sprintf(wxT("item %d was %schecked"), nItem,
-                         m_pListBox->IsChecked(nItem) ? wxT("") : wxT("un"));
+    strSelection.Printf(wxT("item %d was %schecked"), nItem,
+                        m_pListBox->IsChecked(nItem) ? wxT("") : wxT("un"));
     SetStatusText(strSelection);
     SetStatusText(strSelection);
+#else
+    wxUnusedVar(event);
+#endif // wxUSE_STATUSBAR
 }
 }