]>
git.saurik.com Git - wxWidgets.git/blob - samples/drawing/drawing.cpp
b0d1b3d91ac77814d3036d138a8d4dfe74f9153d
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Minimal wxWindows sample
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
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 OnPaint(wxPaintEvent
&event
);
73 void OnOption(wxCommandEvent
&event
);
74 void OnMouseMove(wxMouseEvent
&event
);
83 // any class wishing to process wxWindows events must use this macro
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 // IDs for the controls and the menu commands
104 UserScale_StretchHoriz
,
105 UserScale_ShrinkHoriz
,
106 UserScale_StretchVertic
,
107 UserScale_ShrinkVertic
,
112 LogicalOrigin_MoveDown
,
113 LogicalOrigin_MoveUp
,
114 LogicalOrigin_MoveLeft
,
115 LogicalOrigin_MoveRight
,
119 // ----------------------------------------------------------------------------
120 // event tables and other macros for wxWindows
121 // ----------------------------------------------------------------------------
123 // the event tables connect the wxWindows events with the functions (event
124 // handlers) which process them. It can be also done at run-time, but for the
125 // simple menu events like this the static method is much simpler.
126 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
128 EVT_MOTION (MyFrame::OnMouseMove
)
129 EVT_PAINT (MyFrame::OnPaint
)
131 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
132 EVT_MENU(Minimal_About
, MyFrame::OnAbout
)
134 EVT_MENU(MapMode_Text
, MyFrame::OnOption
)
135 EVT_MENU(MapMode_Lometric
, MyFrame::OnOption
)
136 EVT_MENU(MapMode_Twips
, MyFrame::OnOption
)
137 EVT_MENU(MapMode_Points
, MyFrame::OnOption
)
138 EVT_MENU(MapMode_Metric
, MyFrame::OnOption
)
140 EVT_MENU(UserScale_StretchHoriz
, MyFrame::OnOption
)
141 EVT_MENU(UserScale_ShrinkHoriz
, MyFrame::OnOption
)
142 EVT_MENU(UserScale_StretchVertic
, MyFrame::OnOption
)
143 EVT_MENU(UserScale_ShrinkVertic
, MyFrame::OnOption
)
145 EVT_MENU(AxisMirror_Horiz
, MyFrame::OnOption
)
146 EVT_MENU(AxisMirror_Vertic
, MyFrame::OnOption
)
148 EVT_MENU(LogicalOrigin_MoveDown
, MyFrame::OnOption
)
149 EVT_MENU(LogicalOrigin_MoveUp
, MyFrame::OnOption
)
150 EVT_MENU(LogicalOrigin_MoveLeft
, MyFrame::OnOption
)
151 EVT_MENU(LogicalOrigin_MoveRight
, MyFrame::OnOption
)
154 // Create a new application object: this macro will allow wxWindows to create
155 // the application object during program execution (it's better than using a
156 // static object for many reasons) and also declares the accessor function
157 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
161 // ============================================================================
163 // ============================================================================
165 // ----------------------------------------------------------------------------
166 // the application class
167 // ----------------------------------------------------------------------------
169 // `Main program' equivalent: the program execution "starts" here
172 // Create the main application window
173 MyFrame
*frame
= new MyFrame("Drawing sample",
174 wxPoint(50, 50), wxSize(450, 340));
176 // Show it and tell the application that it's our main window
177 // @@@ what does it do exactly, in fact? is it necessary here?
181 // success: wxApp::OnRun() will be called which will enter the main message
182 // loop and the application will run. If we returned FALSE here, the
183 // application would exit immediately.
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
192 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
193 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
195 // set the frame icon
196 SetIcon(wxICON(mondrian
));
198 wxMenu
*menuFile
= new wxMenu
;
199 menuFile
->Append(Minimal_About
, "&About...\tCtrl-A", "Show about dialog");
200 menuFile
->AppendSeparator();
201 menuFile
->Append(Minimal_Quit
, "E&xit\tAlt-X", "Quit this program");
203 wxMenu
*menuMapMode
= new wxMenu
;
204 menuMapMode
->Append( MapMode_Text
, "&TEXT map mode" );
205 menuMapMode
->Append( MapMode_Lometric
, "&LOMETRIC map mode" );
206 menuMapMode
->Append( MapMode_Twips
, "T&WIPS map mode" );
207 menuMapMode
->Append( MapMode_Points
, "&POINTS map mode" );
208 menuMapMode
->Append( MapMode_Metric
, "&METRIC map mode" );
210 wxMenu
*menuUserScale
= new wxMenu
;
211 menuUserScale
->Append( UserScale_StretchHoriz
, "Stretch horizontally\tCtrl-H" );
212 menuUserScale
->Append( UserScale_ShrinkHoriz
, "Shrink horizontally\tCtrl-G" );
213 menuUserScale
->Append( UserScale_StretchVertic
, "Stretch vertically\tCtrl-V" );
214 menuUserScale
->Append( UserScale_ShrinkVertic
, "Shrink vertically\tCtrl-W" );
216 wxMenu
*menuAxis
= new wxMenu
;
217 menuAxis
->Append( AxisMirror_Horiz
, "Mirror horizontally" );
218 menuAxis
->Append( AxisMirror_Vertic
, "Mirror vertically" );
220 wxMenu
*menuLogical
= new wxMenu
;
221 menuLogical
->Append( LogicalOrigin_MoveDown
, "Move &down\tCtrl-D" );
222 menuLogical
->Append( LogicalOrigin_MoveUp
, "Move &up\tCtrl-U" );
223 menuLogical
->Append( LogicalOrigin_MoveLeft
, "Move &right\tCtrl-L" );
224 menuLogical
->Append( LogicalOrigin_MoveRight
, "Move &left\tCtrl-R" );
226 // now append the freshly created menu to the menu bar...
227 wxMenuBar
*menuBar
= new wxMenuBar
;
228 menuBar
->Append(menuFile
, "&File");
229 menuBar
->Append(menuMapMode
, "&MapMode");
230 menuBar
->Append(menuUserScale
, "&UserScale");
231 menuBar
->Append(menuAxis
, "&Axis");
232 menuBar
->Append(menuLogical
, "&LogicalOrigin");
234 // ... and attach this menu bar to the frame
237 // create a status bar just for fun (by default with 1 pane only)
239 SetStatusText("Welcome to wxWindows!");
241 m_mapMode
= wxMM_TEXT
;
244 m_xLogicalOrigin
= 0;
245 m_yLogicalOrigin
= 0;
251 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
253 // TRUE is to force the frame to close
257 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
260 msg
.Printf( _T("This is the about dialog of the drawing sample.\n")
261 _T("Copyright (c) Robert Roebling 1999")
264 wxMessageBox(msg
, "About Drawing", wxOK
| wxICON_INFORMATION
, this);
267 void MyFrame::OnOption(wxCommandEvent
&event
)
269 switch (event
.GetInt())
272 m_mapMode
= wxMM_LOMETRIC
;
274 case MapMode_Lometric
:
275 m_mapMode
= wxMM_LOMETRIC
;
278 m_mapMode
= wxMM_TWIPS
;
281 m_mapMode
= wxMM_POINTS
;
284 m_mapMode
= wxMM_METRIC
;
286 case LogicalOrigin_MoveDown
:
287 m_yLogicalOrigin
+= 10;
289 case LogicalOrigin_MoveUp
:
290 m_yLogicalOrigin
-= 10;
292 case LogicalOrigin_MoveLeft
:
293 m_xLogicalOrigin
+= 10;
295 case LogicalOrigin_MoveRight
:
296 m_xLogicalOrigin
-= 10;
298 case UserScale_StretchHoriz
:
299 m_xUserScale
*= 1.10;
301 case UserScale_ShrinkHoriz
:
302 m_xUserScale
/= 1.10;
304 case UserScale_StretchVertic
:
305 m_yUserScale
*= 1.10;
307 case UserScale_ShrinkVertic
:
308 m_yUserScale
/= 1.10;
315 void MyFrame::OnPaint(wxPaintEvent
&WXUNUSED(event
) )
318 dc
.SetMapMode( m_mapMode
);
319 dc
.SetUserScale( m_xUserScale
, m_yUserScale
);
320 dc
.SetLogicalOrigin( m_xLogicalOrigin
, m_yLogicalOrigin
);
322 dc
.DrawRectangle( 10, 10, 90, 90 );
323 dc
.DrawRoundedRectangle( 10, 110, 90, 90, 5 );
325 dc
.DrawText( "This is text.", 110, 10 );
327 dc
.DrawIcon( wxICON(mondrian
), 110, 40 );
330 void MyFrame::OnMouseMove(wxMouseEvent
&event
)
333 dc
.SetMapMode( m_mapMode
);
334 dc
.SetUserScale( m_xUserScale
, m_yUserScale
);
335 dc
.SetLogicalOrigin( m_xLogicalOrigin
, m_yLogicalOrigin
);
337 wxPoint pos
= event
.GetPosition();
338 long x
= dc
.DeviceToLogicalX( pos
.x
);
339 long y
= dc
.DeviceToLogicalY( pos
.y
);
341 str
.Printf( "Current mouse position: %d,%d", (int)x
, (int)y
);
342 SetStatusText( str
);