]> git.saurik.com Git - wxWidgets.git/blame - samples/propsize/propsize.cpp
call OnInit() from all samples to allow using standard command line options with...
[wxWidgets.git] / samples / propsize / propsize.cpp
CommitLineData
be2577e4
RD
1/////////////////////////////////////////////////////////////////////////////
2// Name: propsize.cpp
be5a51fb 3// Purpose: wxWidgets propsize sample
be2577e4
RD
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
aec18ff7 9// License: wxWindows license
be2577e4
RD
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
be2577e4
RD
19
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27// for all others, include the necessary headers (this file is usually all you
be5a51fb 28// need because it includes almost all "standard" wxWidgets headers
be2577e4
RD
29#ifndef WX_PRECOMP
30 #include "wx/wx.h"
31#endif
32
33#include "wx/statline.h"
34
35// ----------------------------------------------------------------------------
36// ressources
37// ----------------------------------------------------------------------------
38// the application icon
618f2efa 39#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
be2577e4
RD
40 #include "mondrian.xpm"
41#endif
42
43// ----------------------------------------------------------------------------
44// private classes
45// ----------------------------------------------------------------------------
46
47// Define a new application type, each program should derive a class from wxApp
48class MyApp : public wxApp
49{
50public:
51 // override base class virtuals
52 // ----------------------------
53
54 // this one is called on application startup and is a good place for the app
55 // initialization (doing it here and not in the ctor allows to have an error
56 // return: if OnInit() returns false, the application terminates)
57 virtual bool OnInit();
58};
59
60// Define a new frame type: this is going to be our main frame
61class MyFrame : public wxFrame
62{
63public:
64 // ctor(s)
65 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
66
67 // event handlers (these functions should _not_ be virtual)
68 void OnQuit(wxCommandEvent& event);
69 void OnAbout(wxCommandEvent& event);
70
71private:
be5a51fb 72 // any class wishing to process wxWidgets events must use this macro
be2577e4
RD
73 DECLARE_EVENT_TABLE()
74};
75
76// ----------------------------------------------------------------------------
77// constants
78// ----------------------------------------------------------------------------
79
80// IDs for the controls and the menu commands
81enum
82{
83 // menu items
004f4002 84 Minimal_Quit = 1
be2577e4
RD
85};
86
87// ----------------------------------------------------------------------------
be5a51fb 88// event tables and other macros for wxWidgets
be2577e4
RD
89// ----------------------------------------------------------------------------
90
be5a51fb 91// the event tables connect the wxWidgets events with the functions (event
be2577e4
RD
92// handlers) which process them. It can be also done at run-time, but for the
93// simple menu events like this the static method is much simpler.
94BEGIN_EVENT_TABLE(MyFrame, wxFrame)
95 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
aec18ff7 96 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
be2577e4
RD
97END_EVENT_TABLE()
98
be5a51fb 99// Create a new application object: this macro will allow wxWidgets to create
be2577e4
RD
100// the application object during program execution (it's better than using a
101// static object for many reasons) and also declares the accessor function
102// wxGetApp() which will return the reference of the right type (i.e. MyApp and
103// not wxApp)
104IMPLEMENT_APP(MyApp)
105
106// ============================================================================
107// implementation
108// ============================================================================
109
110// ----------------------------------------------------------------------------
111// the application class
112// ----------------------------------------------------------------------------
113
114// `Main program' equivalent: the program execution "starts" here
115bool MyApp::OnInit()
116{
45e6e6f8
VZ
117 if ( !wxApp::OnInit() )
118 return false;
119
be2577e4 120 // Create the main application window
600683ca 121 MyFrame *frame = new MyFrame(_T("Proportional resize"),
be2577e4
RD
122 wxPoint(50, 50), wxSize(450, 340));
123
124 // Show it and tell the application that it's our main window
125 // @@@ what does it do exactly, in fact? is it necessary here?
b62ca03d 126 frame->Show(true);
be2577e4
RD
127 SetTopWindow(frame);
128
129 // success: wxApp::OnRun() will be called which will enter the main message
b62ca03d 130 // loop and the application will run. If we returned false here, the
be2577e4 131 // application would exit immediately.
b62ca03d 132 return true;
be2577e4
RD
133}
134
135// ----------------------------------------------------------------------------
136// main frame
137// ----------------------------------------------------------------------------
138
139// frame constructor
140MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
b62ca03d 141 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
be2577e4
RD
142{
143 // set the frame icon
144 SetIcon(wxICON(mondrian));
145
146 // create a menu bar
600683ca 147 wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
be2577e4 148
600683ca 149 menuFile->Append(wxID_ABOUT, _T("&About...\tCtrl-A"), _T("Show about dialog"));
be2577e4 150 menuFile->AppendSeparator();
600683ca 151 menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
be2577e4
RD
152
153 // now append the freshly created menu to the menu bar...
154 wxMenuBar *menuBar = new wxMenuBar();
600683ca 155 menuBar->Append(menuFile, _T("&File"));
be2577e4
RD
156
157 // ... and attach this menu bar to the frame
158 SetMenuBar(menuBar);
159
160#if wxUSE_STATUSBAR
161 // create a status bar just for fun (by default with 1 pane only)
162 CreateStatusBar(1);
600683ca 163 SetStatusText(_T("Resize the frame to see how controls react"));
be2577e4
RD
164#endif // wxUSE_STATUSBAR
165
cbf311dd
WS
166#if wxUSE_STATLINE
167 #define AddLine(orient) \
168 Add( new wxStaticLine( this, wxID_ANY, wxDefaultPosition, wxSize(2,2), orient), \
169 0, wxEXPAND)
170#else
171 #define AddLine(orient) \
172 Add( 2, 2)
173#endif // wxUSE_STATLINE
aec18ff7 174
be2577e4 175#define AddButton(label,align) Add( \
b62ca03d 176 new wxButton( this, wxID_ANY, label, wxDefaultPosition, wxSize(100,50)), \
aec18ff7
MB
177 1, wxSHAPED | align)
178
179 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
180 // top row -- top-aligned
181 wxBoxSizer *hsizer1 = new wxBoxSizer( wxHORIZONTAL );
600683ca 182 hsizer1->AddButton( _T("one"), wxALIGN_LEFT | wxALIGN_TOP);
aec18ff7 183 hsizer1->AddLine(wxVERTICAL);
600683ca 184 hsizer1->AddButton( _T("two"), wxALIGN_CENTER_HORIZONTAL | wxALIGN_TOP);
aec18ff7 185 hsizer1->AddLine(wxVERTICAL);
600683ca 186 hsizer1->AddButton( _T("three"), wxALIGN_RIGHT | wxALIGN_TOP);
aec18ff7
MB
187
188 topsizer->Add(hsizer1, 1, wxEXPAND);
189 topsizer->AddLine(wxHORIZONTAL);
190
191 wxBoxSizer *hsizer2 = new wxBoxSizer( wxHORIZONTAL );
600683ca 192 hsizer2->AddButton( _T("four"), wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);
aec18ff7
MB
193 hsizer2->AddLine(wxVERTICAL);
194 // sizer that preserves it's shape
195 wxBoxSizer *vsizer = new wxBoxSizer( wxVERTICAL );
196 vsizer->Add(
b62ca03d 197 new wxButton( this, wxID_ANY, _T("up"), wxDefaultPosition, wxSize(100,25) ),
aec18ff7
MB
198 1, wxEXPAND);
199
200 vsizer->Add(
b62ca03d 201 new wxButton( this, wxID_ANY, _T("down"), wxDefaultPosition, wxSize(100,25) ),
aec18ff7
MB
202 1, wxEXPAND);
203
204 hsizer2->Add(vsizer, 1, wxSHAPED | wxALIGN_CENTER);
205 hsizer2->AddLine(wxVERTICAL);
600683ca 206 hsizer2->AddButton( _T("six"), wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
aec18ff7
MB
207
208 topsizer->Add(hsizer2, 1, wxEXPAND);
209 topsizer->AddLine(wxHORIZONTAL);
210
211 wxBoxSizer *hsizer3 = new wxBoxSizer( wxHORIZONTAL );
600683ca 212 hsizer3->AddButton( _T("seven"), wxALIGN_LEFT | wxALIGN_BOTTOM);
aec18ff7 213 hsizer3->AddLine(wxVERTICAL);
600683ca 214 hsizer3->AddButton( _T("eight"), wxALIGN_CENTER_HORIZONTAL | wxALIGN_BOTTOM);
aec18ff7
MB
215 hsizer3->AddLine(wxVERTICAL);
216 // wxEXPAND should have no effect
600683ca 217 hsizer3->AddButton( _T("nine"), wxEXPAND | wxALIGN_RIGHT | wxALIGN_BOTTOM);
aec18ff7
MB
218
219 topsizer->Add(hsizer3, 1, wxEXPAND);
220
221 // set frame to minimum size
222 topsizer->Fit( this );
223
224 // don't allow frame to get smaller than what the sizers tell ye
225 // topsizer->SetSizeHints( this );
226
227 SetSizer( topsizer );
be2577e4
RD
228}
229
230
231// event handlers
232
233void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
234{
b62ca03d
WS
235 // true is to force the frame to close
236 Close(true);
be2577e4
RD
237}
238
239void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
240{
241 wxString msg;
242 msg.Printf( _T("This is the about dialog of proportional sizer test.\n")
243 _T("Welcome to %s")
244#ifdef wxBETA_NUMBER
245 _T(" (beta %d)!")
246#endif // wxBETA_NUMBER
247 , wxVERSION_STRING
248#ifdef wxBETA_NUMBER
249 , wxBETA_NUMBER
250#endif // wxBETA_NUMBER
251 );
252
600683ca 253 wxMessageBox(msg, _T("About Shaped Sizer"), wxOK | wxICON_INFORMATION, this);
be2577e4 254}