]> git.saurik.com Git - wxWidgets.git/blob - samples/propsize/propsize.cpp
glibc's vswprintf doesn't nul terminate on truncation.
[wxWidgets.git] / samples / propsize / propsize.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: propsize.cpp
3 // Purpose: wxWidgets propsize sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
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
28 // need because it includes almost all "standard" wxWidgets headers
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
39 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
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
48 class MyApp : public wxApp
49 {
50 public:
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
61 class MyFrame : public wxFrame
62 {
63 public:
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
71 private:
72 // any class wishing to process wxWidgets events must use this macro
73 DECLARE_EVENT_TABLE()
74 };
75
76 // ----------------------------------------------------------------------------
77 // constants
78 // ----------------------------------------------------------------------------
79
80 // IDs for the controls and the menu commands
81 enum
82 {
83 // menu items
84 Minimal_Quit = 1
85 };
86
87 // ----------------------------------------------------------------------------
88 // event tables and other macros for wxWidgets
89 // ----------------------------------------------------------------------------
90
91 // the event tables connect the wxWidgets events with the functions (event
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.
94 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
95 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
96 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
97 END_EVENT_TABLE()
98
99 // Create a new application object: this macro will allow wxWidgets to create
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)
104 IMPLEMENT_APP(MyApp)
105
106 // ============================================================================
107 // implementation
108 // ============================================================================
109
110 // ----------------------------------------------------------------------------
111 // the application class
112 // ----------------------------------------------------------------------------
113
114 // `Main program' equivalent: the program execution "starts" here
115 bool MyApp::OnInit()
116 {
117 // Create the main application window
118 MyFrame *frame = new MyFrame(_T("Proportional resize"),
119 wxPoint(50, 50), wxSize(450, 340));
120
121 // Show it and tell the application that it's our main window
122 // @@@ what does it do exactly, in fact? is it necessary here?
123 frame->Show(true);
124 SetTopWindow(frame);
125
126 // success: wxApp::OnRun() will be called which will enter the main message
127 // loop and the application will run. If we returned false here, the
128 // application would exit immediately.
129 return true;
130 }
131
132 // ----------------------------------------------------------------------------
133 // main frame
134 // ----------------------------------------------------------------------------
135
136 // frame constructor
137 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
138 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
139 {
140 // set the frame icon
141 SetIcon(wxICON(mondrian));
142
143 // create a menu bar
144 wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
145
146 menuFile->Append(wxID_ABOUT, _T("&About...\tCtrl-A"), _T("Show about dialog"));
147 menuFile->AppendSeparator();
148 menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
149
150 // now append the freshly created menu to the menu bar...
151 wxMenuBar *menuBar = new wxMenuBar();
152 menuBar->Append(menuFile, _T("&File"));
153
154 // ... and attach this menu bar to the frame
155 SetMenuBar(menuBar);
156
157 #if wxUSE_STATUSBAR
158 // create a status bar just for fun (by default with 1 pane only)
159 CreateStatusBar(1);
160 SetStatusText(_T("Resize the frame to see how controls react"));
161 #endif // wxUSE_STATUSBAR
162
163 #if wxUSE_STATLINE
164 #define AddLine(orient) \
165 Add( new wxStaticLine( this, wxID_ANY, wxDefaultPosition, wxSize(2,2), orient), \
166 0, wxEXPAND)
167 #else
168 #define AddLine(orient) \
169 Add( 2, 2)
170 #endif // wxUSE_STATLINE
171
172 #define AddButton(label,align) Add( \
173 new wxButton( this, wxID_ANY, label, wxDefaultPosition, wxSize(100,50)), \
174 1, wxSHAPED | align)
175
176 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
177 // top row -- top-aligned
178 wxBoxSizer *hsizer1 = new wxBoxSizer( wxHORIZONTAL );
179 hsizer1->AddButton( _T("one"), wxALIGN_LEFT | wxALIGN_TOP);
180 hsizer1->AddLine(wxVERTICAL);
181 hsizer1->AddButton( _T("two"), wxALIGN_CENTER_HORIZONTAL | wxALIGN_TOP);
182 hsizer1->AddLine(wxVERTICAL);
183 hsizer1->AddButton( _T("three"), wxALIGN_RIGHT | wxALIGN_TOP);
184
185 topsizer->Add(hsizer1, 1, wxEXPAND);
186 topsizer->AddLine(wxHORIZONTAL);
187
188 wxBoxSizer *hsizer2 = new wxBoxSizer( wxHORIZONTAL );
189 hsizer2->AddButton( _T("four"), wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);
190 hsizer2->AddLine(wxVERTICAL);
191 // sizer that preserves it's shape
192 wxBoxSizer *vsizer = new wxBoxSizer( wxVERTICAL );
193 vsizer->Add(
194 new wxButton( this, wxID_ANY, _T("up"), wxDefaultPosition, wxSize(100,25) ),
195 1, wxEXPAND);
196
197 vsizer->Add(
198 new wxButton( this, wxID_ANY, _T("down"), wxDefaultPosition, wxSize(100,25) ),
199 1, wxEXPAND);
200
201 hsizer2->Add(vsizer, 1, wxSHAPED | wxALIGN_CENTER);
202 hsizer2->AddLine(wxVERTICAL);
203 hsizer2->AddButton( _T("six"), wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
204
205 topsizer->Add(hsizer2, 1, wxEXPAND);
206 topsizer->AddLine(wxHORIZONTAL);
207
208 wxBoxSizer *hsizer3 = new wxBoxSizer( wxHORIZONTAL );
209 hsizer3->AddButton( _T("seven"), wxALIGN_LEFT | wxALIGN_BOTTOM);
210 hsizer3->AddLine(wxVERTICAL);
211 hsizer3->AddButton( _T("eight"), wxALIGN_CENTER_HORIZONTAL | wxALIGN_BOTTOM);
212 hsizer3->AddLine(wxVERTICAL);
213 // wxEXPAND should have no effect
214 hsizer3->AddButton( _T("nine"), wxEXPAND | wxALIGN_RIGHT | wxALIGN_BOTTOM);
215
216 topsizer->Add(hsizer3, 1, wxEXPAND);
217
218 // set frame to minimum size
219 topsizer->Fit( this );
220
221 // don't allow frame to get smaller than what the sizers tell ye
222 // topsizer->SetSizeHints( this );
223
224 SetSizer( topsizer );
225 }
226
227
228 // event handlers
229
230 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
231 {
232 // true is to force the frame to close
233 Close(true);
234 }
235
236 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
237 {
238 wxString msg;
239 msg.Printf( _T("This is the about dialog of proportional sizer test.\n")
240 _T("Welcome to %s")
241 #ifdef wxBETA_NUMBER
242 _T(" (beta %d)!")
243 #endif // wxBETA_NUMBER
244 , wxVERSION_STRING
245 #ifdef wxBETA_NUMBER
246 , wxBETA_NUMBER
247 #endif // wxBETA_NUMBER
248 );
249
250 wxMessageBox(msg, _T("About Shaped Sizer"), wxOK | wxICON_INFORMATION, this);
251 }