]> git.saurik.com Git - wxWidgets.git/blame - samples/render/render.cpp
Allow more than one line in a wxStaticText on iOS.
[wxWidgets.git] / samples / render / render.cpp
CommitLineData
c59e5089
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: render.cpp
be5a51fb 3// Purpose: Render wxWidgets sample
c59e5089
VZ
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 04.08.03
be5a51fb 7// Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
c59e5089
VZ
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// For compilers that support precompilation, includes "wx/wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#ifndef WX_PRECOMP
27 #include "wx/app.h"
28 #include "wx/frame.h"
9c743a7c
VS
29 #include "wx/dc.h"
30 #include "wx/dcclient.h"
31 #include "wx/panel.h"
32 #include "wx/menu.h"
33 #include "wx/textdlg.h"
34 #include "wx/log.h"
35 #include "wx/msgdlg.h"
32b9d9ff 36 #include "wx/icon.h"
3427bc78 37 #include "wx/image.h"
c59e5089
VZ
38#endif
39
40#include "wx/apptrait.h"
945178c7 41#include "wx/artprov.h"
c59e5089
VZ
42#include "wx/renderer.h"
43
44// ----------------------------------------------------------------------------
45// resources
46// ----------------------------------------------------------------------------
47
48// the application icon (under Windows and OS/2 it is in resources)
e7092398 49#ifndef wxHAS_IMAGES_IN_RESOURCES
c59e5089
VZ
50 #include "../sample.xpm"
51#endif
52
53// ----------------------------------------------------------------------------
54// private classes
55// ----------------------------------------------------------------------------
56
57// A renderer class draws the header buttons in a "special" way
58class MyRenderer : public wxDelegateRendererNative
59{
60public:
61 MyRenderer() : wxDelegateRendererNative(wxRendererNative::GetDefault()) { }
62
adf07e34 63 virtual int DrawHeaderButton(wxWindow *WXUNUSED(win),
c59e5089
VZ
64 wxDC& dc,
65 const wxRect& rect,
1ca21e6d 66 int WXUNUSED(flags) = 0,
945178c7
VZ
67 wxHeaderSortIconType WXUNUSED(sortArrow)
68 = wxHDR_SORT_ICON_NONE,
69 wxHeaderButtonParams* params = NULL)
c59e5089 70 {
34456d33
VZ
71 wxDCBrushChanger setBrush(dc, *wxBLUE_BRUSH);
72 wxDCTextColourChanger setFgCol(dc, *wxWHITE);
c59e5089 73 dc.DrawRoundedRectangle(rect, 5);
945178c7
VZ
74
75 wxString label;
76 if ( params )
77 label = params->m_labelText;
78 dc.DrawLabel(label, wxNullBitmap, rect, wxALIGN_CENTER);
adf07e34 79 return rect.width;
c59e5089
VZ
80 }
81};
82
83// To use a different renderer from the very beginning we must override
84// wxAppTraits method creating the renderer (another, simpler, alternative is
85// to call wxRendererNative::Set() a.s.a.p. which should work in 99% of the
86// cases, but we show this here just for completeness)
87class MyTraits : public wxGUIAppTraits
88{
89 virtual wxRendererNative *CreateRenderer()
90 {
be5a51fb 91 // it will be deleted on program shutdown by wxWidgets itself
c59e5089
VZ
92 return new MyRenderer;
93 }
94};
95
96// Define a new application type, each program should derive a class from wxApp
97class MyApp : public wxApp
98{
99public:
100 virtual bool OnInit();
101
102 // if we want MyTraits to be used we must override CreateTraits()
103 virtual wxAppTraits *CreateTraits() { return new MyTraits; }
104};
105
106// Define a new frame type: this is going to be our main frame
107class MyFrame : public wxFrame
108{
109public:
110 // ctor(s)
111 MyFrame();
112 virtual ~MyFrame();
113
8c9d1533 114private:
c59e5089 115 // event handlers (these functions should _not_ be virtual)
8c9d1533
VZ
116 void OnDrawDisabled(wxCommandEvent& event)
117 { OnToggleDrawFlag(event, wxCONTROL_DISABLED); }
118 void OnDrawFocused(wxCommandEvent& event)
119 { OnToggleDrawFlag(event, wxCONTROL_FOCUSED); }
120 void OnDrawPressed(wxCommandEvent& event)
121 { OnToggleDrawFlag(event, wxCONTROL_PRESSED); }
122 void OnDrawChecked(wxCommandEvent& event)
123 { OnToggleDrawFlag(event, wxCONTROL_CHECKED); }
124 void OnDrawHot(wxCommandEvent& event)
125 { OnToggleDrawFlag(event, wxCONTROL_CURRENT); }
e78778c8
RR
126 void OnDrawUndetermined(wxCommandEvent &event)
127 { OnToggleDrawFlag(event, wxCONTROL_UNDETERMINED); }
8c9d1533 128
945178c7
VZ
129 void OnAlignLeft(wxCommandEvent& WXUNUSED(event))
130 { OnChangeAlign(wxALIGN_LEFT); }
131 void OnAlignCentre(wxCommandEvent& WXUNUSED(event))
132 { OnChangeAlign(wxALIGN_CENTRE); }
133 void OnAlignRight(wxCommandEvent& WXUNUSED(event))
134 { OnChangeAlign(wxALIGN_RIGHT); }
135
136 void OnUseIcon(wxCommandEvent& event);
137 void OnUseBitmap(wxCommandEvent& event);
138
ba46d9ea 139#if wxUSE_DYNLIB_CLASS
c59e5089
VZ
140 void OnLoad(wxCommandEvent& event);
141 void OnUnload(wxCommandEvent& event);
ba46d9ea 142#endif // wxUSE_DYNLIB_CLASS
c59e5089
VZ
143 void OnQuit(wxCommandEvent& event);
144 void OnAbout(wxCommandEvent& event);
145
8c9d1533 146 void OnToggleDrawFlag(wxCommandEvent& event, int flag);
945178c7 147 void OnChangeAlign(int align);
8c9d1533
VZ
148
149 class MyPanel *m_panel;
c59e5089 150
be5a51fb 151 // any class wishing to process wxWidgets events must use this macro
c59e5089
VZ
152 DECLARE_EVENT_TABLE()
153};
154
155// a very simple class just to have something to draw on
156class MyPanel : public wxPanel
157{
158public:
945178c7
VZ
159 MyPanel(wxWindow *parent) : wxPanel(parent)
160 {
161 m_flags = 0;
162 m_align = wxALIGN_LEFT;
163 m_useIcon =
164 m_useBitmap = false;
165 }
8c9d1533
VZ
166
167 int GetFlags() const { return m_flags; }
168 void SetFlags(int flags) { m_flags = flags; }
c59e5089 169
945178c7
VZ
170 void SetAlignment(int align) { m_align = align; }
171 void SetUseIcon(bool useIcon) { m_useIcon = useIcon; }
172 void SetUseBitmap(bool useBitmap) { m_useBitmap = useBitmap; }
173
8c9d1533 174private:
c59e5089
VZ
175 void OnPaint(wxPaintEvent&)
176 {
177 wxPaintDC dc(this);
178
51c42fc5 179 wxRendererNative& renderer = wxRendererNative::Get();
51c42fc5 180
737e5335 181 int x1 = 10, // text offset
945178c7 182 x2 = 300, // drawing offset
737e5335
VZ
183 y = 10;
184
185 const int lineHeight = dc.GetCharHeight();
186 dc.DrawText("Demonstration of various wxRenderer functions:", x1, y);
8c9d1533
VZ
187 y += lineHeight;
188 wxString flagsString;
189 if ( m_flags & wxCONTROL_DISABLED )
190 flagsString += "wxCONTROL_DISABLED ";
191 if ( m_flags & wxCONTROL_FOCUSED )
192 flagsString += "wxCONTROL_FOCUSED ";
193 if ( m_flags & wxCONTROL_PRESSED )
194 flagsString += "wxCONTROL_PRESSED ";
195 if ( m_flags & wxCONTROL_CURRENT )
196 flagsString += "wxCONTROL_CURRENT ";
197 if ( m_flags & wxCONTROL_CHECKED )
198 flagsString += "wxCONTROL_CHECKED ";
e78778c8
RR
199 if ( m_flags & wxCONTROL_UNDETERMINED )
200 flagsString += "wxCONTROL_UNDETERMINED ";
8c9d1533
VZ
201 if ( flagsString.empty() )
202 flagsString = "(none)";
203 dc.DrawText("Using flags: " + flagsString, x1, y);
737e5335
VZ
204 y += lineHeight*3;
205
737e5335 206 const wxCoord heightHdr = renderer.GetHeaderButtonHeight(this);
945178c7
VZ
207 const wxCoord widthHdr = 120;
208
209 const wxHeaderSortIconType
210 hdrSortIcon = m_useIcon ? wxHDR_SORT_ICON_UP
211 : wxHDR_SORT_ICON_NONE;
212
213 wxHeaderButtonParams hdrParams;
214 hdrParams.m_labelText = "Header";
215 hdrParams.m_labelAlignment = m_align;
216 if ( m_useBitmap )
217 {
218 hdrParams.m_labelBitmap = wxArtProvider::GetBitmap(wxART_WARNING,
219 wxART_LIST);
220 }
221
222 dc.DrawText("DrawHeaderButton() (default)", x1, y);
223 wxRendererNative::GetDefault().DrawHeaderButton(this, dc,
224 wxRect(x2, y, widthHdr, heightHdr), m_flags,
225 hdrSortIcon, &hdrParams);
226 y += lineHeight + heightHdr;
227
228 dc.DrawText("DrawHeaderButton() (overridden)", x1, y);
8c9d1533 229 renderer.DrawHeaderButton(this, dc,
945178c7
VZ
230 wxRect(x2, y, widthHdr, heightHdr), m_flags,
231 hdrSortIcon, &hdrParams);
737e5335
VZ
232 y += lineHeight + heightHdr;
233
234 dc.DrawText("DrawCheckBox()", x1, y);
235 const wxSize sizeCheck = renderer.GetCheckBoxSize(this);
8c9d1533
VZ
236 renderer.DrawCheckBox(this, dc,
237 wxRect(wxPoint(x2, y), sizeCheck), m_flags);
737e5335
VZ
238 y += lineHeight + sizeCheck.y;
239
240 dc.DrawText("DrawRadioBitmap()", x1, y);
8c9d1533
VZ
241 renderer.DrawRadioBitmap(this, dc,
242 wxRect(wxPoint(x2, y), sizeCheck), m_flags);
737e5335
VZ
243 y += lineHeight + sizeCheck.y;
244
245 dc.DrawText("DrawTreeItemButton()", x1, y);
8c9d1533
VZ
246 renderer.DrawTreeItemButton(this, dc,
247 wxRect(x2, y, 20, 20), m_flags);
737e5335 248 y += lineHeight + 20;
b50d93d1
VZ
249
250#ifdef wxHAS_DRAW_TITLE_BAR_BITMAP
251 dc.DrawText("DrawTitleBarBitmap()", x1, y);
252 wxRect rBtn(x2, y, 21, 21);
253 renderer.DrawTitleBarBitmap(this, dc, rBtn,
254 wxTITLEBAR_BUTTON_HELP, m_flags);
255 rBtn.x += 2*rBtn.width;
256 renderer.DrawTitleBarBitmap(this, dc, rBtn,
257 wxTITLEBAR_BUTTON_ICONIZE, m_flags);
258 rBtn.x += 2*rBtn.width;
259 renderer.DrawTitleBarBitmap(this, dc, rBtn,
260 wxTITLEBAR_BUTTON_RESTORE, m_flags);
261 rBtn.x += 2*rBtn.width;
262 renderer.DrawTitleBarBitmap(this, dc, rBtn,
263 wxTITLEBAR_BUTTON_MAXIMIZE, m_flags);
264 rBtn.x += 2*rBtn.width;
265 renderer.DrawTitleBarBitmap(this, dc, rBtn,
266 wxTITLEBAR_BUTTON_CLOSE, m_flags);
267
268 y += lineHeight + rBtn.height;
269#endif // wxHAS_DRAW_TITLE_BAR_BITMAP
c59e5089
VZ
270 }
271
8c9d1533 272 int m_flags;
945178c7
VZ
273 int m_align;
274 bool m_useIcon,
275 m_useBitmap;
8c9d1533 276
c59e5089
VZ
277 DECLARE_EVENT_TABLE()
278};
279
280BEGIN_EVENT_TABLE(MyPanel, wxPanel)
281 EVT_PAINT(MyPanel::OnPaint)
282END_EVENT_TABLE()
283
284// ----------------------------------------------------------------------------
285// constants
286// ----------------------------------------------------------------------------
287
288// IDs for the controls and the menu commands
289enum
290{
291 // our menu items
8c9d1533
VZ
292 Render_DrawDisabled = 100,
293 Render_DrawFocused,
294 Render_DrawPressed,
295 Render_DrawChecked,
296 Render_DrawHot,
e78778c8 297 Render_DrawUndetermined,
8c9d1533 298
945178c7
VZ
299 Render_AlignLeft,
300 Render_AlignCentre,
301 Render_AlignRight,
302
303 Render_UseIcon,
304 Render_UseBitmap,
305
ba46d9ea 306#if wxUSE_DYNLIB_CLASS
8c9d1533 307 Render_Load,
c59e5089 308 Render_Unload,
ba46d9ea 309#endif // wxUSE_DYNLIB_CLASS
c59e5089
VZ
310
311 // standard menu items
312 Render_Quit = wxID_EXIT,
313
314 // it is important for the id corresponding to the "About" command to have
315 // this standard value as otherwise it won't be handled properly under Mac
316 // (where it is special and put into the "Apple" menu)
317 Render_About = wxID_ABOUT
318};
319
320// ----------------------------------------------------------------------------
be5a51fb 321// event tables and other macros for wxWidgets
c59e5089
VZ
322// ----------------------------------------------------------------------------
323
be5a51fb 324// the event tables connect the wxWidgets events with the functions (event
c59e5089
VZ
325// handlers) which process them. It can be also done at run-time, but for the
326// simple menu events like this the static method is much simpler.
327BEGIN_EVENT_TABLE(MyFrame, wxFrame)
8c9d1533
VZ
328 EVT_MENU(Render_DrawDisabled, MyFrame::OnDrawDisabled)
329 EVT_MENU(Render_DrawFocused, MyFrame::OnDrawFocused)
330 EVT_MENU(Render_DrawPressed, MyFrame::OnDrawPressed)
331 EVT_MENU(Render_DrawChecked, MyFrame::OnDrawChecked)
332 EVT_MENU(Render_DrawHot, MyFrame::OnDrawHot)
e78778c8 333 EVT_MENU(Render_DrawUndetermined, MyFrame::OnDrawUndetermined)
945178c7
VZ
334 EVT_MENU(Render_AlignLeft, MyFrame::OnAlignLeft)
335 EVT_MENU(Render_AlignCentre, MyFrame::OnAlignCentre)
336 EVT_MENU(Render_AlignRight, MyFrame::OnAlignRight)
337
338 EVT_MENU(Render_UseIcon, MyFrame::OnUseIcon)
339 EVT_MENU(Render_UseBitmap, MyFrame::OnUseBitmap)
340
ba46d9ea 341#if wxUSE_DYNLIB_CLASS
c59e5089
VZ
342 EVT_MENU(Render_Load, MyFrame::OnLoad)
343 EVT_MENU(Render_Unload,MyFrame::OnUnload)
ba46d9ea 344#endif // wxUSE_DYNLIB_CLASS
c59e5089
VZ
345 EVT_MENU(Render_Quit, MyFrame::OnQuit)
346
347 EVT_MENU(Render_About, MyFrame::OnAbout)
348END_EVENT_TABLE()
349
be5a51fb 350// Create a new application object: this macro will allow wxWidgets to create
c59e5089
VZ
351// the application object during program execution (it's better than using a
352// static object for many reasons) and also implements the accessor function
353// wxGetApp() which will return the reference of the right type (i.e. MyApp and
354// not wxApp)
355IMPLEMENT_APP(MyApp)
356
357// ============================================================================
358// implementation
359// ============================================================================
360
361// ----------------------------------------------------------------------------
362// the application class
363// ----------------------------------------------------------------------------
364
365// 'Main program' equivalent: the program execution "starts" here
366bool MyApp::OnInit()
367{
45e6e6f8
VZ
368 if ( !wxApp::OnInit() )
369 return false;
370
3427bc78
VZ
371#ifdef __WXOSX__
372 // currently the images used by DrawTitleBarBitmap() are hard coded as PNG
373 // images inside the library itself so we need to enable PNG support to use
374 // this function
375 wxImage::AddHandler(new wxPNGHandler);
376#endif // OS X
377
c59e5089
VZ
378 // create the main application window
379 new MyFrame;
380
381 return true;
382}
383
384// ----------------------------------------------------------------------------
385// main frame
386// ----------------------------------------------------------------------------
387
388// frame constructor
389MyFrame::MyFrame()
390 : wxFrame(NULL,
1ca21e6d 391 wxID_ANY,
9a83f860 392 wxT("Render wxWidgets Sample"),
c59e5089
VZ
393 wxPoint(50, 50),
394 wxSize(450, 340))
395{
396 // set the frame icon
397 SetIcon(wxICON(sample));
398
399#if wxUSE_MENUS
400 // create a menu bar
401 wxMenu *menuFile = new wxMenu;
8c9d1533
VZ
402 menuFile->AppendCheckItem(Render_DrawDisabled,
403 "Draw in &disabled state\tCtrl-D");
404 menuFile->AppendCheckItem(Render_DrawFocused,
405 "Draw in &focused state\tCtrl-F");
406 menuFile->AppendCheckItem(Render_DrawPressed,
407 "Draw in &pressed state\tCtrl-P");
408 menuFile->AppendCheckItem(Render_DrawChecked,
409 "Draw in &checked state\tCtrl-C");
410 menuFile->AppendCheckItem(Render_DrawHot,
411 "Draw in &hot state\tCtrl-H");
e78778c8
RR
412 menuFile->AppendCheckItem(Render_DrawUndetermined,
413 "Draw in unde&termined state\tCtrl-T");
8c9d1533 414 menuFile->AppendSeparator();
945178c7
VZ
415
416 menuFile->AppendRadioItem(Render_AlignLeft, "&Left align\tCtrl-1");
417 menuFile->AppendRadioItem(Render_AlignCentre, "C&entre align\tCtrl-2");
418 menuFile->AppendRadioItem(Render_AlignRight, "&Right align\tCtrl-3");
419 menuFile->AppendSeparator();
420
421 menuFile->AppendCheckItem(Render_UseIcon, "Draw &icon\tCtrl-I");
422 menuFile->AppendCheckItem(Render_UseBitmap, "Draw &bitmap\tCtrl-B");
423 menuFile->AppendSeparator();
424
ba46d9ea 425#if wxUSE_DYNLIB_CLASS
9a83f860
VZ
426 menuFile->Append(Render_Load, wxT("&Load renderer...\tCtrl-L"));
427 menuFile->Append(Render_Unload, wxT("&Unload renderer\tCtrl-U"));
8c9d1533 428 menuFile->AppendSeparator();
ba46d9ea 429#endif // wxUSE_DYNLIB_CLASS
8c9d1533 430 menuFile->Append(Render_Quit);
c59e5089
VZ
431
432 // the "About" item should be in the help menu
433 wxMenu *helpMenu = new wxMenu;
8c9d1533 434 helpMenu->Append(Render_About);
c59e5089
VZ
435
436 // now append the freshly created menu to the menu bar...
437 wxMenuBar *menuBar = new wxMenuBar();
9a83f860
VZ
438 menuBar->Append(menuFile, wxT("&File"));
439 menuBar->Append(helpMenu, wxT("&Help"));
c59e5089
VZ
440
441 // ... and attach this menu bar to the frame
442 SetMenuBar(menuBar);
443#endif // wxUSE_MENUS
444
445 m_panel = new MyPanel(this);
446
447#if wxUSE_STATUSBAR
448 // create a status bar just for fun (by default with 1 pane only)
449 CreateStatusBar(2);
9a83f860 450 SetStatusText(wxT("Welcome to wxWidgets!"));
c59e5089
VZ
451#endif // wxUSE_STATUSBAR
452
453 Show();
454}
455
456MyFrame::~MyFrame()
457{
458 delete wxRendererNative::Set(NULL);
459}
460
461
462// event handlers
463
8c9d1533
VZ
464void MyFrame::OnToggleDrawFlag(wxCommandEvent& event, int flag)
465{
466 int flags = m_panel->GetFlags();
467 if ( event.IsChecked() )
468 flags |= flag;
469 else
470 flags &= ~flag;
471
472 m_panel->SetFlags(flags);
473 m_panel->Refresh();
474}
475
945178c7
VZ
476void MyFrame::OnChangeAlign(int align)
477{
478 m_panel->SetAlignment(align);
479 m_panel->Refresh();
480}
481
482void MyFrame::OnUseIcon(wxCommandEvent& event)
483{
484 m_panel->SetUseIcon(event.IsChecked());
485 m_panel->Refresh();
486}
487
488void MyFrame::OnUseBitmap(wxCommandEvent& event)
489{
490 m_panel->SetUseBitmap(event.IsChecked());
491 m_panel->Refresh();
492}
493
ba46d9ea
VZ
494#if wxUSE_DYNLIB_CLASS
495
c59e5089
VZ
496void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event))
497{
9a83f860 498 static wxString s_name = wxT("renddll");
c59e5089
VZ
499
500 wxString name = wxGetTextFromUser
501 (
9a83f860
VZ
502 wxT("Name of the renderer to load:"),
503 wxT("Render wxWidgets Sample"),
c59e5089
VZ
504 s_name,
505 this
506 );
507 if ( name.empty() )
508 {
509 // cancelled
510 return;
511 }
512
513 s_name = name;
514
515 wxRendererNative *renderer = wxRendererNative::Load(name);
516 if ( !renderer )
517 {
9a83f860 518 wxLogError(wxT("Failed to load renderer \"%s\"."), name.c_str());
c59e5089
VZ
519 }
520 else // loaded ok
521 {
522 delete wxRendererNative::Set(renderer);
523
524 m_panel->Refresh();
525
9a83f860 526 wxLogStatus(this, wxT("Successfully loaded the renderer \"%s\"."),
c59e5089
VZ
527 name.c_str());
528 }
529}
530
531void MyFrame::OnUnload(wxCommandEvent& WXUNUSED(event))
532{
533 wxRendererNative *renderer = wxRendererNative::Set(NULL);
534 if ( renderer )
535 {
536 delete renderer;
537
538 m_panel->Refresh();
539
9a83f860 540 wxLogStatus(this, wxT("Unloaded the previously loaded renderer."));
c59e5089
VZ
541 }
542 else
543 {
9a83f860 544 wxLogWarning(wxT("No renderer to unload."));
c59e5089
VZ
545 }
546}
547
ba46d9ea
VZ
548#endif // wxUSE_DYNLIB_CLASS
549
c59e5089
VZ
550void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
551{
552 // true is to force the frame to close
553 Close(true);
554}
555
556void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
557{
9a83f860
VZ
558 wxMessageBox(wxT("Render sample shows how to use custom renderers.\n")
559 wxT("\n")
560 wxT("(c) 2003 Vadim Zeitlin"),
561 wxT("About Render wxWidgets Sample"),
c59e5089
VZ
562 wxOK | wxICON_INFORMATION, this);
563}
564