]>
git.saurik.com Git - wxWidgets.git/blob - samples/minimal/minimal.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Minimal wxWindows sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 // #pragma implementation "minimal.cpp"
22 // #pragma interface "minimal.cpp"
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
32 // for all others, include the necessary headers (this file is usually all you
33 // need because it includes almost all "standard" wxWindows headers)
38 #include "wx/dynarray.h"
40 WX_DEFINE_ARRAY(wxWindow
*, wxArrayLboxes
);
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
45 // the application icon
46 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
47 #include "mondrian.xpm"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // Define a new application type, each program should derive a class from wxApp
55 class MyApp
: public wxApp
58 // override base class virtuals
59 // ----------------------------
61 // this one is called on application startup and is a good place for the app
62 // initialization (doing it here and not in the ctor allows to have an error
63 // return: if OnInit() returns false, the application terminates)
64 virtual bool OnInit();
67 // Define a new frame type: this is going to be our main frame
68 class MyFrame
: public wxFrame
72 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
74 // event handlers (these functions should _not_ be virtual)
75 void OnQuit(wxCommandEvent
& event
);
76 void OnAbout(wxCommandEvent
& event
);
77 void OnCreateLbox(wxCommandEvent
& event
);
78 void OnDeleteLbox(wxCommandEvent
& event
);
81 wxArrayLboxes m_lboxes
;
84 // any class wishing to process wxWindows events must use this macro
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 // IDs for the controls and the menu commands
102 static const size_t NUM_LBOXES
= 10;
103 static const wxCoord POS_STEP
= 5;
105 // ----------------------------------------------------------------------------
106 // event tables and other macros for wxWindows
107 // ----------------------------------------------------------------------------
109 // the event tables connect the wxWindows events with the functions (event
110 // handlers) which process them. It can be also done at run-time, but for the
111 // simple menu events like this the static method is much simpler.
112 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
113 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
114 EVT_MENU(Minimal_About
, MyFrame::OnAbout
)
115 EVT_MENU(Minimal_CreateLbox
, MyFrame::OnCreateLbox
)
116 EVT_MENU(Minimal_DeleteLbox
, MyFrame::OnDeleteLbox
)
119 // Create a new application object: this macro will allow wxWindows to create
120 // the application object during program execution (it's better than using a
121 // static object for many reasons) and also declares the accessor function
122 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
126 // ============================================================================
128 // ============================================================================
130 // ----------------------------------------------------------------------------
131 // the application class
132 // ----------------------------------------------------------------------------
134 // 'Main program' equivalent: the program execution "starts" here
137 // create the main application window
138 MyFrame
*frame
= new MyFrame("Minimal wxWindows App",
139 wxPoint(50, 50), wxSize(450, 340));
141 // and show it (the frames, unlike simple controls, are not shown when
142 // created initially)
145 // success: wxApp::OnRun() will be called which will enter the main message
146 // loop and the application will run. If we returned FALSE here, the
147 // application would exit immediately.
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
156 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
157 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
162 // we need this in order to allow the about menu relocation, since ABOUT is
163 // not the default id of the about menu
164 wxApp::s_macAboutMenuItemId
= Minimal_About
;
167 // set the frame icon
168 SetIcon(wxICON(mondrian
));
172 wxMenu
*menuFile
= new wxMenu("", wxMENU_TEAROFF
);
174 // the "About" item should be in the help menu
175 wxMenu
*helpMenu
= new wxMenu
;
176 helpMenu
->Append(Minimal_About
, "&About...\tCtrl-A", "Show about dialog");
177 helpMenu
->Append(Minimal_CreateLbox
, "&Create 10 listboxes\tCtrl-C");
178 helpMenu
->Append(Minimal_DeleteLbox
, "&Delete 10 listboxes\tCtrl-D");
180 menuFile
->Append(Minimal_Quit
, "E&xit\tAlt-X", "Quit this program");
182 // now append the freshly created menu to the menu bar...
183 wxMenuBar
*menuBar
= new wxMenuBar();
184 menuBar
->Append(menuFile
, "&File");
185 menuBar
->Append(helpMenu
, "&Help");
187 // ... and attach this menu bar to the frame
189 #endif // wxUSE_MENUS
192 // create a status bar just for fun (by default with 1 pane only)
194 SetStatusText("Welcome to wxWindows!");
195 #endif // wxUSE_STATUSBAR
201 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
203 // TRUE is to force the frame to close
207 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
210 msg
.Printf( _T("This is the about dialog of minimal sample.\n")
211 _T("Welcome to %s"), wxVERSION_STRING
);
213 wxMessageBox(msg
, "About Minimal", wxOK
| wxICON_INFORMATION
, this);
216 void MyFrame::OnCreateLbox(wxCommandEvent
& WXUNUSED(event
))
218 for ( size_t n
= 0; n
< NUM_LBOXES
; n
++ )
220 m_lboxes
.Add(new wxListBox(this, -1, wxPoint(m_pos
, m_pos
)));
225 void MyFrame::OnDeleteLbox(wxCommandEvent
& WXUNUSED(event
))
227 size_t count
= m_lboxes
.GetCount();
228 if ( count
< NUM_LBOXES
)
231 for ( size_t n
= 0; n
< NUM_LBOXES
; n
++ )
233 delete m_lboxes
[--count
];
234 m_lboxes
.RemoveAt(count
);