]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/grid/grid.cpp
Moved the cleanup code to an EVT_WINDOW_DESTROY handler.
[wxWidgets.git] / samples / grid / grid.cpp
index ed7ad8001abc1f37dbd46a1d93ba83fb9834c0b3..3f37efaf5e11984f6f8f21cd4de2374dcc959988 100644 (file)
@@ -1,19 +1,14 @@
 /*
- * File:       grid.cpp
- * Purpose:    wxGrid test
- * Author:     Julian Smart
- * Created:    1995
- * Updated:    
+ * File:    grid.cpp
+ * Purpose: wxGrid test
+ * Author:  Julian Smart
+ * Created: 1995
+ * Updated:
  * Copyright:   (c) 1995, AIAI, University of Edinburgh
  */
 
 static const char sccsid[] = "%W% %G%";
 
-#ifdef __GNUG__
-#pragma implementation
-#pragma interface
-#endif
-
 // For compilers that support precompilation, includes "wx/wx.h".
 #include "wx/wxprec.h"
 
@@ -30,8 +25,10 @@ static const char sccsid[] = "%W% %G%";
 
 // Define a new application type
 class MyApp: public wxApp
-{ public:
-    bool OnInit(void);
+{
+public:
+    virtual bool OnInit(void);
+    virtual int OnExit();
 };
 
 
@@ -90,235 +87,258 @@ IMPLEMENT_APP(MyApp)
 bool MyApp::OnInit(void)
 {
 #ifdef __WXMSW__
-  cellBitmap1 = new wxBitmap("bitmap1");
-  cellBitmap2 = new wxBitmap("bitmap2");
+    cellBitmap1 = new wxBitmap("bitmap1");
+    cellBitmap2 = new wxBitmap("bitmap2");
 #endif
 
-  // Create the main frame window
-  MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "wxGrid Sample", wxPoint(50, 50), wxSize(450, 300));
-  
-  // Give it an icon
+    // Create the main frame window
+    MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "wxGrid Sample", wxPoint(50, 50), wxSize(450, 300));
+
+    // Give it an icon
 #ifdef __WXMSW__
-  frame->SetIcon(wxIcon("mondrian"));
+    frame->SetIcon(wxIcon("mondrian"));
 #endif
 
-  // Make a menubar
-  wxMenu *file_menu = new wxMenu;
-  file_menu->Append(GRID_QUIT, "E&xit");
-
-  wxMenu *settings_menu = new wxMenu;
-  settings_menu->Append(GRID_TOGGLE_EDITABLE, "&Toggle editable");
-  settings_menu->Append(GRID_TOGGLE_EDITINPLACE, "&Toggle edit in place");
-  settings_menu->Append(GRID_TOGGLE_ROW_LABEL, "Toggle ro&w label");
-  settings_menu->Append(GRID_TOGGLE_COL_LABEL, "Toggle co&l label");
-  settings_menu->Append(GRID_TOGGLE_DIVIDERS, "Toggle &dividers");
-  settings_menu->AppendSeparator();
-  settings_menu->Append(GRID_LEFT_CELL, "&Left cell alignment ");
-  settings_menu->Append(GRID_CENTRE_CELL, "&Centre cell alignment ");
-  settings_menu->Append(GRID_RIGHT_CELL, "&Right cell alignment ");
-  settings_menu->AppendSeparator();
-  settings_menu->Append(GRID_COLOUR_LABEL_BACKGROUND, "Choose a label &background colour");
-  settings_menu->Append(GRID_COLOUR_LABEL_TEXT, "Choose a label fore&ground colour");
-  settings_menu->Append(GRID_NORMAL_LABEL_COLOURING, "&Normal label colouring");
-  settings_menu->AppendSeparator();
-  settings_menu->Append(GRID_COLOUR_CELL_BACKGROUND, "Choo&se a cell &background colour");
-  settings_menu->Append(GRID_COLOUR_CELL_TEXT, "Choose &a cell foreground colour");
-  settings_menu->Append(GRID_NORMAL_CELL_COLOURING, "N&ormal cell colouring");
-
-  wxMenuBar *menu_bar = new wxMenuBar;
-  menu_bar->Append(file_menu, "&File");
-  menu_bar->Append(settings_menu, "&Settings");
-  frame->SetMenuBar(menu_bar);
-
-  // Make a grid
-  frame->grid = new wxGrid(frame, 0, 0, 400, 400);
-
-  frame->grid->CreateGrid(10, 8);
-  frame->grid->SetColumnWidth(3, 200);
-  frame->grid->SetRowHeight(4, 45);
-  frame->grid->SetCellValue("First cell", 0, 0);
-  frame->grid->SetCellValue("Another cell", 1, 1);
-  frame->grid->SetCellValue("Yet another cell", 2, 2);
-  frame->grid->SetCellTextFont(* wxTheFontList->FindOrCreateFont(10, wxROMAN, wxITALIC, wxNORMAL), 0, 0);
-  frame->grid->SetCellTextColour(*wxRED, 1, 1);
-  frame->grid->SetCellBackgroundColour(*wxCYAN, 2, 2);
-  if (cellBitmap1 && cellBitmap2)
-  {
-    frame->grid->SetCellAlignment(wxCENTRE, 5, 0);
-    frame->grid->SetCellAlignment(wxCENTRE, 6, 0);
-    frame->grid->SetCellBitmap(cellBitmap1, 5, 0);
-    frame->grid->SetCellBitmap(cellBitmap2, 6, 0);
-  }
-  
-  frame->grid->UpdateDimensions();
-  
-  // Show the frame
-  frame->Show(TRUE);
-
-  SetTopWindow(frame);
-  return TRUE;
+    // Make a menubar
+    wxMenu *file_menu = new wxMenu;
+    file_menu->Append(GRID_QUIT, "E&xit");
+
+    wxMenu *settings_menu = new wxMenu;
+    settings_menu->Append(GRID_TOGGLE_EDITABLE, "&Toggle editable");
+    settings_menu->Append(GRID_TOGGLE_EDITINPLACE, "&Toggle edit in place");
+    settings_menu->Append(GRID_TOGGLE_ROW_LABEL, "Toggle ro&w label");
+    settings_menu->Append(GRID_TOGGLE_COL_LABEL, "Toggle co&l label");
+    settings_menu->Append(GRID_TOGGLE_DIVIDERS, "Toggle &dividers");
+    settings_menu->AppendSeparator();
+    settings_menu->Append(GRID_LEFT_CELL, "&Left cell alignment ");
+    settings_menu->Append(GRID_CENTRE_CELL, "&Centre cell alignment ");
+    settings_menu->Append(GRID_RIGHT_CELL, "&Right cell alignment ");
+    settings_menu->AppendSeparator();
+    settings_menu->Append(GRID_COLOUR_LABEL_BACKGROUND, "Choose a label &background colour");
+    settings_menu->Append(GRID_COLOUR_LABEL_TEXT, "Choose a label fore&ground colour");
+    settings_menu->Append(GRID_NORMAL_LABEL_COLOURING, "&Normal label colouring");
+    settings_menu->AppendSeparator();
+    settings_menu->Append(GRID_COLOUR_CELL_BACKGROUND, "Choo&se a cell &background colour");
+    settings_menu->Append(GRID_COLOUR_CELL_TEXT, "Choose &a cell foreground colour");
+    settings_menu->Append(GRID_NORMAL_CELL_COLOURING, "N&ormal cell colouring");
+
+    wxMenuBar *menu_bar = new wxMenuBar;
+    menu_bar->Append(file_menu, "&File");
+    menu_bar->Append(settings_menu, "&Settings");
+    frame->SetMenuBar(menu_bar);
+
+    // Make a grid
+    frame->grid = new wxGrid(frame, 0, 0, 400, 400);
+
+    frame->grid->CreateGrid(10, 8);
+    frame->grid->SetColumnWidth(3, 200);
+    frame->grid->SetRowHeight(4, 45);
+    frame->grid->SetCellValue("First cell", 0, 0);
+    frame->grid->SetCellValue("Another cell", 1, 1);
+    frame->grid->SetCellValue("Yet another cell", 2, 2);
+    frame->grid->SetCellTextFont(wxFont(10, wxROMAN, wxITALIC, wxNORMAL), 0, 0);
+    frame->grid->SetCellTextColour(*wxRED, 1, 1);
+    frame->grid->SetCellBackgroundColour(*wxCYAN, 2, 2);
+    if (cellBitmap1 && cellBitmap2)
+    {
+        frame->grid->SetCellAlignment(wxCENTRE, 5, 0);
+        frame->grid->SetCellAlignment(wxCENTRE, 6, 0);
+        frame->grid->SetCellBitmap(cellBitmap1, 5, 0);
+        frame->grid->SetCellBitmap(cellBitmap2, 6, 0);
+    }
+
+    frame->grid->UpdateDimensions();
+
+    // Show the frame
+    frame->Show(TRUE);
+
+    SetTopWindow(frame);
+    return TRUE;
 }
 
+int MyApp::OnExit()
+{
+    if (cellBitmap1)
+    {
+        delete cellBitmap1;
+        cellBitmap1 = (wxBitmap *) NULL;
+    }
+
+    if (cellBitmap2)
+    {
+        delete cellBitmap2;
+        cellBitmap1 = (wxBitmap *) NULL;
+    }
+
+    // exit code is 0, everything is ok
+    return 0;
+}
+
+
 // My frame constructor
-MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
-  wxFrame(frame, -1, title, pos, size)
+MyFrame::MyFrame(wxFrame *frame, const wxString& title,
+                 const wxPoint& pos, const wxSize& size):
+    wxFrame(frame, -1, title, pos, size)
 {
-  grid = (wxGrid*) NULL;
+    grid = (wxGrid*) NULL;
 }
 
 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
-  EVT_MENU(GRID_TOGGLE_EDITABLE, MyFrame::ToggleEditable)
-  EVT_MENU(GRID_TOGGLE_EDITINPLACE, MyFrame::ToggleEditInPlace)
-  EVT_MENU(GRID_TOGGLE_ROW_LABEL, MyFrame::ToggleRowLabel)
-  EVT_MENU(GRID_TOGGLE_COL_LABEL, MyFrame::ToggleColLabel)
-  EVT_MENU(GRID_TOGGLE_DIVIDERS, MyFrame::ToggleDividers)
-  EVT_MENU(GRID_LEFT_CELL, MyFrame::LeftCell)
-  EVT_MENU(GRID_CENTRE_CELL, MyFrame::CentreCell)
-  EVT_MENU(GRID_RIGHT_CELL, MyFrame::RightCell)
-  EVT_MENU(GRID_COLOUR_LABEL_BACKGROUND, MyFrame::ColourLabelBackground)
-  EVT_MENU(GRID_COLOUR_LABEL_TEXT, MyFrame::ColourLabelText)
-  EVT_MENU(GRID_NORMAL_LABEL_COLOURING, MyFrame::NormalLabelColouring)
-  EVT_MENU(GRID_COLOUR_CELL_BACKGROUND, MyFrame::ColourCellBackground)
-  EVT_MENU(GRID_COLOUR_CELL_TEXT, MyFrame::ColourCellText)
-  EVT_MENU(GRID_NORMAL_CELL_COLOURING, MyFrame::NormalCellColouring)
-  EVT_MENU(GRID_QUIT, MyFrame::Quit)
+    EVT_MENU(GRID_TOGGLE_EDITABLE, MyFrame::ToggleEditable)
+    EVT_MENU(GRID_TOGGLE_EDITINPLACE, MyFrame::ToggleEditInPlace)
+    EVT_MENU(GRID_TOGGLE_ROW_LABEL, MyFrame::ToggleRowLabel)
+    EVT_MENU(GRID_TOGGLE_COL_LABEL, MyFrame::ToggleColLabel)
+    EVT_MENU(GRID_TOGGLE_DIVIDERS, MyFrame::ToggleDividers)
+    EVT_MENU(GRID_LEFT_CELL, MyFrame::LeftCell)
+    EVT_MENU(GRID_CENTRE_CELL, MyFrame::CentreCell)
+    EVT_MENU(GRID_RIGHT_CELL, MyFrame::RightCell)
+    EVT_MENU(GRID_COLOUR_LABEL_BACKGROUND, MyFrame::ColourLabelBackground)
+    EVT_MENU(GRID_COLOUR_LABEL_TEXT, MyFrame::ColourLabelText)
+    EVT_MENU(GRID_NORMAL_LABEL_COLOURING, MyFrame::NormalLabelColouring)
+    EVT_MENU(GRID_COLOUR_CELL_BACKGROUND, MyFrame::ColourCellBackground)
+    EVT_MENU(GRID_COLOUR_CELL_TEXT, MyFrame::ColourCellText)
+    EVT_MENU(GRID_NORMAL_CELL_COLOURING, MyFrame::NormalCellColouring)
+    EVT_MENU(GRID_QUIT, MyFrame::Quit)
 END_EVENT_TABLE()
 
 void MyFrame::ToggleEditable(wxCommandEvent& WXUNUSED(event))
 {
-      grid->SetEditable(!grid->GetEditable());
-      grid->Refresh();
+    grid->SetEditable(!grid->GetEditable());
+    grid->Refresh();
 }
 
 void MyFrame::ToggleEditInPlace(wxCommandEvent& WXUNUSED(event))
 {
-      grid->SetEditInPlace(!grid->GetEditInPlace());
-      grid->Refresh();
+    grid->SetEditInPlace(!grid->GetEditInPlace());
+    grid->Refresh();
 }
 
 void MyFrame::ToggleRowLabel(wxCommandEvent& WXUNUSED(event))
 {
-      if (grid->GetLabelSize(wxVERTICAL) > 0)
+    if (grid->GetLabelSize(wxVERTICAL) > 0)
         grid->SetLabelSize(wxVERTICAL, 0);
-      else
+    else
         grid->SetLabelSize(wxVERTICAL, 40);
-      grid->Refresh();
+
+    grid->Refresh();
 }
 
 void MyFrame::ToggleColLabel(wxCommandEvent& WXUNUSED(event))
 {
-      if (grid->GetLabelSize(wxHORIZONTAL) > 0)
+    if (grid->GetLabelSize(wxHORIZONTAL) > 0)
         grid->SetLabelSize(wxHORIZONTAL, 0);
-      else
+    else
         grid->SetLabelSize(wxHORIZONTAL, 20);
+
       grid->Refresh();
 }
 
 void MyFrame::ToggleDividers(wxCommandEvent& WXUNUSED(event))
 {
-      if (!grid->GetDividerPen().Ok())
-        grid->SetDividerPen(* wxThePenList->FindOrCreatePen("LIGHT GREY", 1, wxSOLID));
-      else
+    if (!grid->GetDividerPen().Ok())
+        grid->SetDividerPen(wxPen(wxT("LIGHT GREY"), 1, wxSOLID));
+    else
         grid->SetDividerPen(wxNullPen);
-      grid->Refresh();
+
+grid->Refresh();
 }
 
 void MyFrame::LeftCell(wxCommandEvent& WXUNUSED(event))
 {
-      grid->SetCellAlignment(wxLEFT);
-      grid->Refresh();
+    grid->SetCellAlignment(wxLEFT);
+    grid->Refresh();
 }
 
 void MyFrame::CentreCell(wxCommandEvent& WXUNUSED(event))
 {
-      grid->SetCellAlignment(wxCENTRE);
-      grid->Refresh();
+    grid->SetCellAlignment(wxCENTRE);
+    grid->Refresh();
 }
 
 void MyFrame::RightCell(wxCommandEvent& WXUNUSED(event))
 {
-      grid->SetCellAlignment(wxRIGHT);
-      grid->Refresh();
+    grid->SetCellAlignment(wxRIGHT);
+    grid->Refresh();
 }
 
 void MyFrame::ColourLabelBackground(wxCommandEvent& WXUNUSED(event))
 {
-      wxColourData data;
-      data.SetChooseFull(TRUE);
-      wxColourDialog dialog(this, &data);
-      if (dialog.ShowModal() != wxID_CANCEL)
-      {
+    wxColourData data;
+    data.SetChooseFull(TRUE);
+    wxColourDialog dialog(this, &data);
+    if (dialog.ShowModal() != wxID_CANCEL)
+    {
         wxColourData retData = dialog.GetColourData();
         wxColour col = retData.GetColour();
         grid->SetLabelBackgroundColour(col);
         grid->Refresh();
-      }
+    }
 }
 
 void MyFrame::ColourLabelText(wxCommandEvent& WXUNUSED(event))
 {
-      wxColourData data;
-      data.SetChooseFull(TRUE);
-      wxColourDialog dialog(this, &data);
-      if (dialog.ShowModal() != wxID_CANCEL)
-      {
+    wxColourData data;
+    data.SetChooseFull(TRUE);
+    wxColourDialog dialog(this, &data);
+    if (dialog.ShowModal() != wxID_CANCEL)
+    {
         wxColourData retData = dialog.GetColourData();
         wxColour col = retData.GetColour();
         grid->SetLabelTextColour(col);
         grid->Refresh();
-      }
+    }
 }
 
 void MyFrame::NormalLabelColouring(wxCommandEvent& WXUNUSED(event))
 {
-      grid->SetLabelBackgroundColour(*wxLIGHT_GREY);
-      grid->SetLabelTextColour(*wxBLACK);
-      grid->Refresh();
+    grid->SetLabelBackgroundColour(*wxLIGHT_GREY);
+    grid->SetLabelTextColour(*wxBLACK);
+    grid->Refresh();
 }
 
 void MyFrame::ColourCellBackground(wxCommandEvent& WXUNUSED(event))
 {
-      wxColourData data;
-      data.SetChooseFull(TRUE);
-      wxColourDialog dialog(this, &data);
-      if (dialog.ShowModal() != wxID_CANCEL)
-      {
+    wxColourData data;
+    data.SetChooseFull(TRUE);
+    wxColourDialog dialog(this, &data);
+    if (dialog.ShowModal() != wxID_CANCEL)
+    {
         wxColourData retData = dialog.GetColourData();
         wxColour col = retData.GetColour();
         grid->SetCellBackgroundColour(col);
         grid->Refresh();
-      }
+    }
 }
 
 void MyFrame::ColourCellText(wxCommandEvent& WXUNUSED(event))
 {
-      wxColourData data;
-      data.SetChooseFull(TRUE);
-      wxColourDialog dialog(this, &data);
-      if (dialog.ShowModal() != wxID_CANCEL)
-      {
+    wxColourData data;
+    data.SetChooseFull(TRUE);
+    wxColourDialog dialog(this, &data);
+    if (dialog.ShowModal() != wxID_CANCEL)
+    {
         wxColourData retData = dialog.GetColourData();
         wxColour col = retData.GetColour();
         grid->SetCellTextColour(col);
         grid->Refresh();
-      }
+    }
 }
 
 void MyFrame::NormalCellColouring(wxCommandEvent& WXUNUSED(event))
 {
-      grid->SetCellBackgroundColour(*wxWHITE);
-      grid->SetCellTextColour(*wxBLACK);
-      grid->Refresh();
+    grid->SetCellBackgroundColour(*wxWHITE);
+    grid->SetCellTextColour(*wxBLACK);
+    grid->Refresh();
 }
 
 void MyFrame::Quit(wxCommandEvent& WXUNUSED(event))
 {
-      this->Close(TRUE);
+    this->Close(TRUE);
 }
 
 // Ensure that the grid's edit control always has the focus.
 void MyFrame::OnActivate(wxActivateEvent& event)
 {
-  if (grid) grid->OnActivate(event.GetActive());
+    if (grid) grid->OnActivate(event.GetActive());
 }