]>
git.saurik.com Git - wxWidgets.git/blob - samples/minimal/minimal.cpp
b65cb80df0f8d3571afb8b5ae2bd579c2e415fe0
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Minimal wxWindows sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "minimal.cpp"
21 #pragma interface "minimal.cpp"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
31 // for all others, include the necessary headers (this file is usually all you
32 // need because it includes almost all "standard" wxWindows headers
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
40 // the application icon
41 #if defined(__WXGTK__) || defined(__WXMOTIF__)
42 #include "mondrian.xpm"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // Define a new application type, each program should derive a class from wxApp
50 class MyApp
: public wxApp
53 // override base class virtuals
54 // ----------------------------
56 // this one is called on application startup and is a good place for the app
57 // initialization (doing it here and not in the ctor allows to have an error
58 // return: if OnInit() returns false, the application terminates)
59 virtual bool OnInit();
62 // Define a new frame type: this is going to be our main frame
63 class MyFrame
: public wxFrame
67 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
69 // event handlers (these functions should _not_ be virtual)
70 void OnQuit(wxCommandEvent
& event
);
71 void OnAbout(wxCommandEvent
& event
);
72 void OnTest(wxCommandEvent
& event
);
74 void OnPaint(wxPaintEvent
& event
);
77 // any class wishing to process wxWindows events must use this macro
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 // IDs for the controls and the menu commands
93 // controls start here (the numbers are, of course, arbitrary)
97 // ----------------------------------------------------------------------------
98 // event tables and other macros for wxWindows
99 // ----------------------------------------------------------------------------
101 // the event tables connect the wxWindows events with the functions (event
102 // handlers) which process them. It can be also done at run-time, but for the
103 // simple menu events like this the static method is much simpler.
104 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
105 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
106 EVT_MENU(Minimal_About
, MyFrame::OnAbout
)
108 EVT_MENU(Minimal_Test
, MyFrame::OnTest
)
110 EVT_PAINT(MyFrame::OnPaint
)
113 // Create a new application object: this macro will allow wxWindows to create
114 // the application object during program execution (it's better than using a
115 // static object for many reasons) and also declares the accessor function
116 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
120 // ============================================================================
122 // ============================================================================
124 // ----------------------------------------------------------------------------
125 // the application class
126 // ----------------------------------------------------------------------------
128 // `Main program' equivalent: the program execution "starts" here
131 // Create the main application window
132 MyFrame
*frame
= new MyFrame("Minimal wxWindows App",
133 wxPoint(50, 50), wxSize(450, 340));
135 // Show it and tell the application that it's our main window
136 // @@@ what does it do exactly, in fact? is it necessary here?
140 // success: wxApp::OnRun() will be called which will enter the main message
141 // loop and the application will run. If we returned FALSE here, the
142 // application would exit immediately.
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
151 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
152 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
154 // set the frame icon
155 SetIcon(wxICON(mondrian
));
158 // wxMenu *menuFile = new wxMenu;
159 wxMenu
*menuFile
= new wxMenu(wxMENU_TEAROFF
);
161 menuFile
->Append(Minimal_About
, "&About...\tCtrl-A", "Show about dialog");
162 menuFile
->Append(Minimal_Test
, "&Test...\tCtrl-T", "Test");
163 menuFile
->AppendSeparator();
164 menuFile
->Append(Minimal_Quit
, "E&xit\tAlt-X", "Quit this program");
166 // now append the freshly created menu to the menu bar...
167 wxMenuBar
*menuBar
= new wxMenuBar();
168 menuBar
->Append(menuFile
, "&File");
170 // ... and attach this menu bar to the frame
174 // create a status bar just for fun (by default with 1 pane only)
176 SetStatusText("Welcome to wxWindows!");
177 #endif // wxUSE_STATUSBAR
183 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
185 // TRUE is to force the frame to close
189 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
192 msg
.Printf( _T("This is the about dialog of minimal sample.\n")
196 #endif // wxBETA_NUMBER
200 #endif // wxBETA_NUMBER
203 wxMessageBox(msg
, "About Minimal", wxOK
| wxICON_INFORMATION
, this);
208 Foo(int n_
) { n
= n_
; }
213 WX_DECLARE_LIST(Foo
, FooList
);
215 #include <wx/listimpl.cpp>
217 WX_DEFINE_LIST(FooList
);
219 int FooSort(const Foo
**item1
, const Foo
**item2
)
221 return (*item2
)->n
- (*item1
)->n
;
224 void ShowList(const FooList
& list
)
227 msg
= "The list elements: (";
228 for ( FooList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
232 str
.Printf("%d", node
->GetData()->n
);
238 wxMessageBox(msg
, "List contents", wxOK
| wxICON_INFORMATION
);
241 void MyFrame::OnTest(wxCommandEvent
& event
)
244 list
.Append(new Foo(12));
245 list
.Append(new Foo(3));
246 list
.Append(new Foo(1));
247 list
.Append(new Foo(7));
248 list
.Append(new Foo(4));
254 void MyFrame::OnPaint(wxPaintEvent
& event
)
259 wxSize
size(GetClientSize());
260 dcMem
.SelectObject(wxBitmap(size
.x
, size
.y
, -1));
262 dcMem
.SetBackground(wxBrush(wxColour(0, 0, 255), wxSOLID
));
263 dcMem
.SetTextForeground(wxColour(0, 255, 0));
264 dcMem
.SetTextBackground(wxColour(0, 0, 0));
265 dcMem
.SetBackgroundMode(wxSOLID
);
267 dcMem
.DrawText("Hello, wxWindows!", 10, 10);
269 wxPoint
ptOrig(0, 0);
270 dc
.Blit(ptOrig
, size
, &dcMem
, ptOrig
);
272 dcMem
.SelectObject(wxNullBitmap
);