]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/foldbar/extended/extended.cpp
4ab91dd1a50bec6d1ca6009116b007c0bc61493d
[wxWidgets.git] / contrib / samples / foldbar / extended / extended.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: expended.cpp
3 // Purpose: Layout/foldpanelbar sample
4 // Author: Jorgen Bodde
5 // Modified by:
6 // Created: 24/07/2004
7 // Copyright: (c) Jorgen Bodde based upon FoldPanelBarTest (c) Julian Smart
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 #include "wx/wx.h"
20 #include "wx/mdi.h"
21 #endif
22
23 #include "wx/toolbar.h"
24 #include "wx/laywin.h"
25 #include "wx/spinctrl.h"
26 #include "wx/slider.h"
27
28 #include "../../../../samples/sample.xpm"
29
30 #include "extended.h"
31
32 MyFrame *frame = NULL;
33 wxList my_children;
34
35 IMPLEMENT_APP(MyApp)
36
37 // For drawing lines in a canvas
38 long xpos = -1;
39 long ypos = -1;
40
41 int winNumber = 1;
42
43 // Initialise this in OnInit, not statically
44 bool MyApp::OnInit(void)
45 {
46 // Create the main frame window
47
48 frame = new MyFrame(NULL, -1, _T("FoldPanelBar Extended Demo"), wxPoint(-1, -1), wxSize(500, 600),
49 wxDEFAULT_FRAME_STYLE |
50 wxNO_FULL_REPAINT_ON_RESIZE |
51 wxHSCROLL | wxVSCROLL);
52
53 frame->SetIcon(wxIcon(sample_xpm));
54
55 // Associate the menu bar with the frame
56 frame->SetMenuBar(CreateMenuBar(false));
57
58 frame->CreateStatusBar();
59
60 frame->Show(TRUE);
61
62 SetTopWindow(frame);
63
64 return TRUE;
65 }
66
67 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
68 EVT_MENU(FPBTEST_ABOUT, MyFrame::OnAbout)
69 EVT_MENU(FPBTEST_NEW_WINDOW, MyFrame::OnNewWindow)
70 EVT_SIZE(MyFrame::OnSize)
71 EVT_MENU(FPBTEST_QUIT, MyFrame::OnQuit)
72 EVT_MENU(FPBTEST_TOGGLE_WINDOW, MyFrame::OnToggleWindow)
73 EVT_SASH_DRAGGED_RANGE(ID_WINDOW_TOP, ID_WINDOW_BOTTOM, MyFrame::OnFoldPanelBarDrag)
74 EVT_MENU(FPB_BOTTOM_STICK, MyFrame::OnCreateBottomStyle)
75 EVT_MENU(FPB_SINGLE_FOLD, MyFrame::OnCreateNormalStyle)
76 EVT_BUTTON(ID_COLLAPSEME, MyFrame::OnCollapseMe)
77 EVT_BUTTON(ID_APPLYTOALL, MyFrame::OnExpandMe)
78 EVT_SCROLL(MyFrame::OnSlideColour)
79 EVT_RADIOBUTTON(ID_USE_HGRADIENT, MyFrame::OnStyleChange)
80 EVT_RADIOBUTTON(ID_USE_VGRADIENT, MyFrame::OnStyleChange)
81 EVT_RADIOBUTTON(ID_USE_SINGLE, MyFrame::OnStyleChange)
82 EVT_RADIOBUTTON(ID_USE_RECTANGLE, MyFrame::OnStyleChange)
83 EVT_RADIOBUTTON(ID_USE_FILLED_RECTANGLE, MyFrame::OnStyleChange)
84 END_EVENT_TABLE()
85
86
87 // Define my frame constructor
88 MyFrame::MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size,
89 const long style)
90 : wxMDIParentFrame(parent, id, title, pos, size, style)
91 , _flags(0)
92 {
93 m_leftWindow1 = new wxSashLayoutWindow(this, ID_WINDOW_LEFT1,
94 wxDefaultPosition, wxSize(200, 30),
95 wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN);
96
97 m_leftWindow1->SetDefaultSize(wxSize(160, 1000));
98 m_leftWindow1->SetOrientation(wxLAYOUT_VERTICAL);
99 m_leftWindow1->SetAlignment(wxLAYOUT_LEFT);
100 m_leftWindow1->SetSashVisible(wxSASH_RIGHT, TRUE);
101 m_leftWindow1->SetExtraBorderSize(0);
102
103 _pnl = 0;
104 ReCreateFoldPanel(0);
105 }
106
107 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
108 {
109 Close(TRUE);
110 }
111
112 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
113 {
114 (void)wxMessageBox(_T("wxWidgets 2.0 FoldPanelBar Demo\nAuthor: Julian Smart (c) 1998"), _T("About FoldPanelBar Demo"));
115 }
116
117 void MyFrame::OnToggleWindow(wxCommandEvent& WXUNUSED(event))
118 {
119 m_leftWindow1->Show(!m_leftWindow1->IsShown());
120 wxLayoutAlgorithm layout;
121 layout.LayoutMDIFrame(this);
122 }
123
124 void MyFrame::OnFoldPanelBarDrag(wxSashEvent& event)
125 {
126 if (event.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE)
127 return;
128
129 if(event.GetId() == ID_WINDOW_LEFT1)
130 m_leftWindow1->SetDefaultSize(wxSize(event.GetDragRect().width, 1000));
131
132 wxLayoutAlgorithm layout;
133 layout.LayoutMDIFrame(this);
134
135 // Leaves bits of itself behind sometimes
136 GetClientWindow()->Refresh();
137 }
138
139 void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event))
140 {
141 // Make another frame, containing a canvas
142 MyChild *subframe = new MyChild(frame, _T("Canvas Frame"),
143 wxPoint(10, 10), wxSize(300, 300),
144 wxDEFAULT_FRAME_STYLE |
145 wxNO_FULL_REPAINT_ON_RESIZE);
146
147 subframe->SetTitle(wxString::Format(_T("Canvas Frame %d"), winNumber));
148 winNumber ++;
149
150 // Give it an icon (this is ignored in MDI mode: uses resources)
151 #ifdef __WXMSW__
152 subframe->SetIcon(wxIcon(_T("sashtest_icn")));
153 #endif
154
155 // Give it a status line
156 subframe->CreateStatusBar();
157
158 // Associate the menu bar with the frame
159 subframe->SetMenuBar(CreateMenuBar(true));
160
161 int width, height;
162 subframe->GetClientSize(&width, &height);
163 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
164 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
165 subframe->canvas = canvas;
166
167 // Give it scrollbars
168 canvas->SetScrollbars(20, 20, 50, 50);
169
170 subframe->Show(TRUE);
171 }
172
173 void MyFrame::ReCreateFoldPanel(int fpb_flags)
174 {
175 // delete earlier panel
176 m_leftWindow1->DestroyChildren();
177
178 // recreate the foldpanelbar
179
180 _pnl = new wxFoldPanelBar(m_leftWindow1, -1, wxDefaultPosition, wxSize(-1,-1), wxFPB_DEFAULT_STYLE, fpb_flags);
181
182 wxFoldPanel item = _pnl->AddFoldPanel("Caption colours", false);
183
184 _pnl->AddFoldPanelWindow(item, new wxStaticText(item.GetParent(), -1, _T("Adjust the first colour")),
185 wxFPB_ALIGN_WIDTH, 5, 20);
186
187 // RED color spin control
188 _rslider1 = new wxSlider(item.GetParent(), -1, 0, 0, 255);
189 _pnl->AddFoldPanelWindow(item, _rslider1, wxFPB_ALIGN_WIDTH,
190 2, 20);
191
192 // GREEN color spin control
193 _gslider1 = new wxSlider(item.GetParent(), -1, 0, 0, 255);
194 _pnl->AddFoldPanelWindow(item, _gslider1, wxFPB_ALIGN_WIDTH,
195 0, 20);
196
197 // BLUE color spin control
198 _bslider1 = new wxSlider(item.GetParent(), -1, 0, 0, 255);
199 _pnl->AddFoldPanelWindow(item, _bslider1, wxFPB_ALIGN_WIDTH,
200 0, 20);
201
202 _pnl->AddFoldPanelSeperator(item);
203
204 _pnl->AddFoldPanelWindow(item, new wxStaticText(item.GetParent(), -1, _T("Adjust the second colour")),
205 wxFPB_ALIGN_WIDTH, 5, 20);
206
207 // RED color spin control
208 _rslider2 = new wxSlider(item.GetParent(), -1, 0, 0, 255);
209 _pnl->AddFoldPanelWindow(item, _rslider2, wxFPB_ALIGN_WIDTH,
210 2, 20);
211
212 // GREEN color spin control
213 _gslider2 = new wxSlider(item.GetParent(), -1, 0, 0, 255);
214 _pnl->AddFoldPanelWindow(item, _gslider2, wxFPB_ALIGN_WIDTH,
215 0, 20);
216
217 // BLUE color spin control
218 _bslider2 = new wxSlider(item.GetParent(), -1, 0, 0, 255);
219 _pnl->AddFoldPanelWindow(item, _bslider2, wxFPB_ALIGN_WIDTH,
220 0, 20);
221
222 _pnl->AddFoldPanelSeperator(item);
223
224 _btn = new wxButton(item.GetParent(), ID_APPLYTOALL, "Apply to all");
225 _pnl->AddFoldPanelWindow(item, _btn);
226
227 // read back current gradients and set the sliders
228 // for the colour which is now taken as default
229
230 wxCaptionBarStyle style = _pnl->GetCaptionStyle(item);
231 wxColour col = style.GetFirstColour();
232 _rslider1->SetValue(col.Red());
233 _gslider1->SetValue(col.Green());
234 _bslider1->SetValue(col.Blue());
235
236 col = style.GetSecondColour();
237 _rslider2->SetValue(col.Red());
238 _gslider2->SetValue(col.Green());
239 _bslider2->SetValue(col.Blue());
240
241 // put down some caption styles from which the user can
242 // select to show how the current or all caption bars will look like
243
244 item = _pnl->AddFoldPanel("Caption style", false);
245
246 wxRadioButton *currStyle = new wxRadioButton(item.GetParent(), ID_USE_VGRADIENT, "&Vertical gradient");
247 _pnl->AddFoldPanelWindow(item, currStyle, wxFPB_ALIGN_WIDTH, wxFPB_DEFAULT_SPACING, 10);
248 currStyle->SetValue(true);
249
250 _pnl->AddFoldPanelWindow(item, new wxRadioButton(item.GetParent(), ID_USE_HGRADIENT, "&Horizontal gradient"),
251 wxFPB_ALIGN_WIDTH, wxFPB_DEFAULT_SPACING, 10);
252 _pnl->AddFoldPanelWindow(item, new wxRadioButton(item.GetParent(), ID_USE_SINGLE, "&Single colour"),
253 wxFPB_ALIGN_WIDTH, wxFPB_DEFAULT_SPACING, 10);
254 _pnl->AddFoldPanelWindow(item, new wxRadioButton(item.GetParent(), ID_USE_RECTANGLE, "&Rectangle box"),
255 wxFPB_ALIGN_WIDTH, wxFPB_DEFAULT_SPACING, 10);
256 _pnl->AddFoldPanelWindow(item, new wxRadioButton(item.GetParent(), ID_USE_FILLED_RECTANGLE, "&Filled rectangle box"),
257 wxFPB_ALIGN_WIDTH, wxFPB_DEFAULT_SPACING, 10);
258
259 _pnl->AddFoldPanelSeperator(item);
260
261 _single = new wxCheckBox(item.GetParent(), -1, "&Only this caption");
262 _pnl->AddFoldPanelWindow(item, _single, wxFPB_ALIGN_WIDTH, wxFPB_DEFAULT_SPACING, 10);
263
264
265 // one more panel to finish it
266
267 wxCaptionBarStyle cs;
268 cs.SetCaptionStyle(wxCAPTIONBAR_RECTANGLE);
269
270 item = _pnl->AddFoldPanel("Misc stuff", true, cs);
271
272 _pnl->AddFoldPanelWindow(item, new wxButton(item.GetParent(), ID_COLLAPSEME, "Collapse All"));
273
274 _pnl->AddFoldPanelWindow(item, new wxStaticText(item.GetParent(), -1, _T("Enter some comments")),
275 wxFPB_ALIGN_WIDTH, 5, 20);
276
277 _pnl->AddFoldPanelWindow(item, new wxTextCtrl(item.GetParent(), -1, "Comments"),
278 wxFPB_ALIGN_WIDTH, wxFPB_DEFAULT_SPACING, 10);
279
280 m_leftWindow1->SizeWindows();
281
282 }
283
284 void MyFrame::OnCreateBottomStyle(wxCommandEvent& event)
285 {
286 // recreate with style collapse to bottom, which means
287 // all panels that are collapsed are placed at the bottom,
288 // or normal
289
290 if(event.IsChecked())
291 _flags |= wxFPB_COLLAPSE_TO_BOTTOM;
292 else
293 _flags &= ~wxFPB_COLLAPSE_TO_BOTTOM;
294
295 ReCreateFoldPanel(_flags);
296 }
297
298 void MyFrame::OnCreateNormalStyle(wxCommandEvent& event)
299 {
300 // receate with style where only one panel at the time is
301 // allowed to be opened
302
303 // TODO: Not yet implemented!
304
305 if(event.IsChecked())
306 _flags |= wxFPB_SINGLE_FOLD;
307 else
308 _flags &= ~wxFPB_SINGLE_FOLD;
309
310 ReCreateFoldPanel(_flags);
311 }
312
313 void MyFrame::OnCollapseMe(wxCommandEvent &event)
314 {
315 wxFoldPanel item(0);
316 for(size_t i = 0; i < _pnl->GetCount(); i++)
317 {
318 item = _pnl->Item(i);
319 _pnl->Collapse(item);
320 }
321 }
322
323 void MyFrame::OnExpandMe(wxCommandEvent &event)
324 {
325 wxColour col1(_rslider1->GetValue(), _gslider1->GetValue(), _bslider1->GetValue()),
326 col2(_rslider2->GetValue(), _gslider2->GetValue(), _bslider2->GetValue());
327
328 wxCaptionBarStyle style;
329
330 style.SetFirstColour(col1);
331 style.SetSecondColour(col2);
332
333 _pnl->ApplyCaptionStyleAll(style);
334 }
335
336 wxMenuBar *CreateMenuBar(bool with_window)
337 {
338 // Make a menubar
339 wxMenu *file_menu = new wxMenu;
340
341 file_menu->Append(FPBTEST_NEW_WINDOW, _T("&New window"));
342 if(with_window)
343 file_menu->Append(FPBTEST_CHILD_QUIT, _T("&Close child"));
344
345 file_menu->AppendSeparator();
346 file_menu->Append(FPBTEST_QUIT, _T("&Exit"));
347
348 wxMenu *option_menu = 0;
349 if(with_window)
350 {
351 // Dummy option
352 option_menu = new wxMenu;
353 option_menu->Append(FPBTEST_REFRESH, _T("&Refresh picture"));
354 }
355
356 // make fold panel menu
357
358 wxMenu *fpb_menu = new wxMenu;
359 fpb_menu->AppendCheckItem(FPB_BOTTOM_STICK, _T("Create with &wxFPB_COLLAPSE_TO_BOTTOM"));
360 //fpb_menu->AppendCheckItem(FPB_SINGLE_FOLD, _T("Create with &wxFPB_SINGLE_FOLD"));
361
362 fpb_menu->AppendSeparator();
363 fpb_menu->Append(FPBTEST_TOGGLE_WINDOW, _T("&Toggle FoldPanelBar"));
364
365 wxMenu *help_menu = new wxMenu;
366 help_menu->Append(FPBTEST_ABOUT, _T("&About"));
367
368 wxMenuBar *menu_bar = new wxMenuBar;
369
370 menu_bar->Append(file_menu, _T("&File"));
371 menu_bar->Append(fpb_menu, _T("&FoldPanel"));
372 if(option_menu)
373 menu_bar->Append(option_menu, _T("&Options"));
374 menu_bar->Append(help_menu, _T("&Help"));
375
376 return menu_bar;
377 }
378
379 void MyFrame::OnSlideColour(wxScrollEvent &event)
380 {
381 wxColour col1(_rslider1->GetValue(), _gslider1->GetValue(), _bslider1->GetValue()),
382 col2(_rslider2->GetValue(), _gslider2->GetValue(), _bslider2->GetValue());
383 //_btn->SetBackgroundColour(col);
384
385 wxCaptionBarStyle style;
386
387 style.SetFirstColour(col1);
388 style.SetSecondColour(col2);
389
390 wxFoldPanel item = _pnl->Item(0);
391 _pnl->ApplyCaptionStyle(item, style);
392 }
393
394 void MyFrame::OnStyleChange(wxCommandEvent &event)
395 {
396 wxCaptionBarStyle style;
397 switch(event.GetId())
398 {
399 case ID_USE_HGRADIENT:
400 style.SetCaptionStyle(wxCAPTIONBAR_GRADIENT_H);
401 break;
402
403 case ID_USE_VGRADIENT:
404 style.SetCaptionStyle(wxCAPTIONBAR_GRADIENT_V);
405 break;
406
407 case ID_USE_SINGLE:
408 style.SetCaptionStyle(wxCAPTIONBAR_SINGLE);
409 break;
410
411 case ID_USE_RECTANGLE:
412 style.SetCaptionStyle(wxCAPTIONBAR_RECTANGLE);
413 break;
414
415 case ID_USE_FILLED_RECTANGLE:
416 style.SetCaptionStyle(wxCAPTIONBAR_FILLED_RECTANGLE);
417 break;
418
419 default:
420 break;
421 }
422
423 if(_single->GetValue())
424 {
425 wxFoldPanel item = _pnl->Item(1);
426 _pnl->ApplyCaptionStyle(item, style);
427 }
428 else
429 _pnl->ApplyCaptionStyleAll(style);
430
431 }
432
433 /* ----------------------------------------------------------------------------------------------- */
434
435 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
436 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
437 END_EVENT_TABLE()
438
439 // Define a constructor for my canvas
440 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
441 : wxScrolledWindow(parent, -1, pos, size,
442 wxSUNKEN_BORDER | wxNO_FULL_REPAINT_ON_RESIZE)
443 {
444 SetBackgroundColour(* wxWHITE);
445 }
446
447 // Define the repainting behaviour
448 void MyCanvas::OnDraw(wxDC& dc)
449 {
450 dc.SetFont(*wxSWISS_FONT);
451 dc.SetPen(*wxGREEN_PEN);
452 dc.DrawLine(0, 0, 200, 200);
453 dc.DrawLine(200, 0, 0, 200);
454
455 dc.SetBrush(*wxCYAN_BRUSH);
456 dc.SetPen(*wxRED_PEN);
457 dc.DrawRectangle(100, 100, 100, 50);
458 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
459
460 dc.DrawEllipse(250, 250, 100, 50);
461 #if wxUSE_SPLINES
462 dc.DrawSpline(50, 200, 50, 100, 200, 10);
463 #endif // wxUSE_SPLINES
464 dc.DrawLine(50, 230, 200, 230);
465 dc.DrawText(_T("This is a test string"), 50, 230);
466
467 wxPoint points[3];
468 points[0].x = 200; points[0].y = 300;
469 points[1].x = 100; points[1].y = 400;
470 points[2].x = 300; points[2].y = 400;
471
472 dc.DrawPolygon(3, points);
473 }
474
475 // This implements a tiny doodling program! Drag the mouse using
476 // the left button.
477 void MyCanvas::OnEvent(wxMouseEvent& event)
478 {
479 wxClientDC dc(this);
480 PrepareDC(dc);
481
482 wxPoint pt(event.GetLogicalPosition(dc));
483
484 if (xpos > -1 && ypos > -1 && event.Dragging())
485 {
486 dc.SetPen(*wxBLACK_PEN);
487 dc.DrawLine(xpos, ypos, pt.x, pt.y);
488 }
489 xpos = pt.x;
490 ypos = pt.y;
491 }
492
493 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
494 {
495 wxLayoutAlgorithm layout;
496 layout.LayoutMDIFrame(this);
497 }
498
499 // Note that FPBTEST_NEW_WINDOW and FPBTEST_ABOUT commands get passed
500 // to the parent window for processing, so no need to
501 // duplicate event handlers here.
502
503 BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
504 EVT_MENU(FPBTEST_CHILD_QUIT, MyChild::OnQuit)
505 END_EVENT_TABLE()
506
507 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
508 const long style):
509 wxMDIChildFrame(parent, -1, title, pos, size, style)
510 {
511 canvas = NULL;
512 my_children.Append(this);
513 }
514
515 MyChild::~MyChild(void)
516 {
517 my_children.DeleteObject(this);
518 }
519
520 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
521 {
522 Close(TRUE);
523 }
524
525 void MyChild::OnActivate(wxActivateEvent& event)
526 {
527 if (event.GetActive() && canvas)
528 canvas->SetFocus();
529 }
530