1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Layout/foldpanelbar sample
4 // Author: Jorgen Bodde
7 // Copyright: (c) Jorgen Bodde based upon FoldPanelBarTest (c) Julian Smart
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
23 #include "wx/toolbar.h"
24 #include "wx/laywin.h"
25 #include <wx/spinctrl.h>
26 #include <wx/slider.h>
30 MyFrame
*frame
= NULL
;
35 // For drawing lines in a canvas
41 // Initialise this in OnInit, not statically
42 bool MyApp::OnInit(void)
44 // Create the main frame window
46 frame
= new MyFrame(NULL
, -1, _T("FoldPanelBar Extended Demo"), wxPoint(-1, -1), wxSize(500, 600),
47 wxDEFAULT_FRAME_STYLE
|
48 wxNO_FULL_REPAINT_ON_RESIZE
|
49 wxHSCROLL
| wxVSCROLL
);
51 // Give it an icon (this is ignored in MDI mode: uses resources)
54 frame
->SetIcon(wxIcon(_T("sashtest_icn")));
57 // Associate the menu bar with the frame
58 frame
->SetMenuBar(CreateMenuBar(false));
60 frame
->CreateStatusBar();
69 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
70 EVT_MENU(FPBTEST_ABOUT
, MyFrame::OnAbout
)
71 EVT_MENU(FPBTEST_NEW_WINDOW
, MyFrame::OnNewWindow
)
72 EVT_SIZE(MyFrame::OnSize
)
73 EVT_MENU(FPBTEST_QUIT
, MyFrame::OnQuit
)
74 EVT_MENU(FPBTEST_TOGGLE_WINDOW
, MyFrame::OnToggleWindow
)
75 EVT_SASH_DRAGGED_RANGE(ID_WINDOW_TOP
, ID_WINDOW_BOTTOM
, MyFrame::OnFoldPanelBarDrag
)
76 EVT_MENU(FPB_BOTTOM_STICK
, MyFrame::OnCreateBottomStyle
)
77 EVT_MENU(FPB_SINGLE_FOLD
, MyFrame::OnCreateNormalStyle
)
78 EVT_BUTTON(ID_COLLAPSEME
, MyFrame::OnCollapseMe
)
79 EVT_BUTTON(ID_APPLYTOALL
, MyFrame::OnExpandMe
)
80 EVT_SCROLL(MyFrame::OnSlideColour
)
81 EVT_RADIOBUTTON(ID_USE_HGRADIENT
, MyFrame::OnStyleChange
)
82 EVT_RADIOBUTTON(ID_USE_VGRADIENT
, MyFrame::OnStyleChange
)
83 EVT_RADIOBUTTON(ID_USE_SINGLE
, MyFrame::OnStyleChange
)
84 EVT_RADIOBUTTON(ID_USE_RECTANGLE
, MyFrame::OnStyleChange
)
85 EVT_RADIOBUTTON(ID_USE_FILLED_RECTANGLE
, MyFrame::OnStyleChange
)
89 // Define my frame constructor
90 MyFrame::MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
92 : wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
95 m_leftWindow1
= new wxSashLayoutWindow(this, ID_WINDOW_LEFT1
,
96 wxDefaultPosition
, wxSize(200, 30),
97 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
99 m_leftWindow1
->SetDefaultSize(wxSize(160, 1000));
100 m_leftWindow1
->SetOrientation(wxLAYOUT_VERTICAL
);
101 m_leftWindow1
->SetAlignment(wxLAYOUT_LEFT
);
102 m_leftWindow1
->SetSashVisible(wxSASH_RIGHT
, TRUE
);
103 m_leftWindow1
->SetExtraBorderSize(0);
106 ReCreateFoldPanel(0);
109 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
114 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
116 (void)wxMessageBox(_T("wxWidgets 2.0 FoldPanelBar Demo\nAuthor: Julian Smart (c) 1998"), _T("About FoldPanelBar Demo"));
119 void MyFrame::OnToggleWindow(wxCommandEvent
& WXUNUSED(event
))
121 m_leftWindow1
->Show(!m_leftWindow1
->IsShown());
122 wxLayoutAlgorithm layout
;
123 layout
.LayoutMDIFrame(this);
126 void MyFrame::OnFoldPanelBarDrag(wxSashEvent
& event
)
128 if (event
.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE
)
131 if(event
.GetId() == ID_WINDOW_LEFT1
)
132 m_leftWindow1
->SetDefaultSize(wxSize(event
.GetDragRect().width
, 1000));
134 wxLayoutAlgorithm layout
;
135 layout
.LayoutMDIFrame(this);
137 // Leaves bits of itself behind sometimes
138 GetClientWindow()->Refresh();
141 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
))
143 // Make another frame, containing a canvas
144 MyChild
*subframe
= new MyChild(frame
, _T("Canvas Frame"),
145 wxPoint(10, 10), wxSize(300, 300),
146 wxDEFAULT_FRAME_STYLE
|
147 wxNO_FULL_REPAINT_ON_RESIZE
);
149 subframe
->SetTitle(wxString::Format(_T("Canvas Frame %d"), winNumber
));
152 // Give it an icon (this is ignored in MDI mode: uses resources)
154 subframe
->SetIcon(wxIcon(_T("sashtest_icn")));
157 // Give it a status line
158 subframe
->CreateStatusBar();
160 // Associate the menu bar with the frame
161 subframe
->SetMenuBar(CreateMenuBar(true));
164 subframe
->GetClientSize(&width
, &height
);
165 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
166 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
167 subframe
->canvas
= canvas
;
169 // Give it scrollbars
170 canvas
->SetScrollbars(20, 20, 50, 50);
172 subframe
->Show(TRUE
);
175 void MyFrame::ReCreateFoldPanel(int fpb_flags
)
177 // delete earlier panel
178 m_leftWindow1
->DestroyChildren();
180 // recreate the foldpanelbar
182 _pnl
= new wxFoldPanelBar(m_leftWindow1
, -1, wxDefaultPosition
, wxSize(-1,-1), wxFPB_DEFAULT_STYLE
, fpb_flags
);
184 wxFoldPanel item
= _pnl
->AddFoldPanel("Caption colours", false);
186 _pnl
->AddFoldPanelWindow(item
, new wxStaticText(item
.GetParent(), -1, _T("Adjust the first colour")),
187 wxFPB_ALIGN_WIDTH
, 5, 20);
189 // RED color spin control
190 _rslider1
= new wxSlider(item
.GetParent(), -1, 0, 0, 255);
191 _pnl
->AddFoldPanelWindow(item
, _rslider1
, wxFPB_ALIGN_WIDTH
,
194 // GREEN color spin control
195 _gslider1
= new wxSlider(item
.GetParent(), -1, 0, 0, 255);
196 _pnl
->AddFoldPanelWindow(item
, _gslider1
, wxFPB_ALIGN_WIDTH
,
199 // BLUE color spin control
200 _bslider1
= new wxSlider(item
.GetParent(), -1, 0, 0, 255);
201 _pnl
->AddFoldPanelWindow(item
, _bslider1
, wxFPB_ALIGN_WIDTH
,
204 _pnl
->AddFoldPanelSeperator(item
);
206 _pnl
->AddFoldPanelWindow(item
, new wxStaticText(item
.GetParent(), -1, _T("Adjust the second colour")),
207 wxFPB_ALIGN_WIDTH
, 5, 20);
209 // RED color spin control
210 _rslider2
= new wxSlider(item
.GetParent(), -1, 0, 0, 255);
211 _pnl
->AddFoldPanelWindow(item
, _rslider2
, wxFPB_ALIGN_WIDTH
,
214 // GREEN color spin control
215 _gslider2
= new wxSlider(item
.GetParent(), -1, 0, 0, 255);
216 _pnl
->AddFoldPanelWindow(item
, _gslider2
, wxFPB_ALIGN_WIDTH
,
219 // BLUE color spin control
220 _bslider2
= new wxSlider(item
.GetParent(), -1, 0, 0, 255);
221 _pnl
->AddFoldPanelWindow(item
, _bslider2
, wxFPB_ALIGN_WIDTH
,
224 _pnl
->AddFoldPanelSeperator(item
);
226 _btn
= new wxButton(item
.GetParent(), ID_APPLYTOALL
, "Apply to all");
227 _pnl
->AddFoldPanelWindow(item
, _btn
);
229 // read back current gradients and set the sliders
230 // for the colour which is now taken as default
232 wxCaptionBarStyle style
= _pnl
->GetCaptionStyle(item
);
233 wxColour col
= style
.GetFirstColour();
234 _rslider1
->SetValue(col
.Red());
235 _gslider1
->SetValue(col
.Green());
236 _bslider1
->SetValue(col
.Blue());
238 col
= style
.GetSecondColour();
239 _rslider2
->SetValue(col
.Red());
240 _gslider2
->SetValue(col
.Green());
241 _bslider2
->SetValue(col
.Blue());
243 // put down some caption styles from which the user can
244 // select to show how the current or all caption bars will look like
246 item
= _pnl
->AddFoldPanel("Caption style", false);
248 wxRadioButton
*currStyle
= new wxRadioButton(item
.GetParent(), ID_USE_VGRADIENT
, "&Vertical gradient");
249 _pnl
->AddFoldPanelWindow(item
, currStyle
, wxFPB_ALIGN_WIDTH
, wxFPB_DEFAULT_YSPACING
, 10);
250 currStyle
->SetValue(true);
252 _pnl
->AddFoldPanelWindow(item
, new wxRadioButton(item
.GetParent(), ID_USE_HGRADIENT
, "&Horizontal gradient"),
253 wxFPB_ALIGN_WIDTH
, wxFPB_DEFAULT_YSPACING
, 10);
254 _pnl
->AddFoldPanelWindow(item
, new wxRadioButton(item
.GetParent(), ID_USE_SINGLE
, "&Single colour"),
255 wxFPB_ALIGN_WIDTH
, wxFPB_DEFAULT_YSPACING
, 10);
256 _pnl
->AddFoldPanelWindow(item
, new wxRadioButton(item
.GetParent(), ID_USE_RECTANGLE
, "&Rectangle box"),
257 wxFPB_ALIGN_WIDTH
, wxFPB_DEFAULT_YSPACING
, 10);
258 _pnl
->AddFoldPanelWindow(item
, new wxRadioButton(item
.GetParent(), ID_USE_FILLED_RECTANGLE
, "&Filled rectangle box"),
259 wxFPB_ALIGN_WIDTH
, wxFPB_DEFAULT_YSPACING
, 10);
261 _pnl
->AddFoldPanelSeperator(item
);
263 _single
= new wxCheckBox(item
.GetParent(), -1, "&Only this caption");
264 _pnl
->AddFoldPanelWindow(item
, _single
, wxFPB_ALIGN_WIDTH
, wxFPB_DEFAULT_YSPACING
, 10);
267 // one more panel to finish it
269 wxCaptionBarStyle cs
;
270 cs
.SetCaptionStyle(wxCAPTIONBAR_RECTANGLE
);
272 item
= _pnl
->AddFoldPanel("Misc stuff", true, cs
);
274 _pnl
->AddFoldPanelWindow(item
, new wxButton(item
.GetParent(), ID_COLLAPSEME
, "Collapse All"));
276 _pnl
->AddFoldPanelWindow(item
, new wxStaticText(item
.GetParent(), -1, _T("Enter some comments")),
277 wxFPB_ALIGN_WIDTH
, 5, 20);
279 _pnl
->AddFoldPanelWindow(item
, new wxTextCtrl(item
.GetParent(), -1, "Comments"),
280 wxFPB_ALIGN_WIDTH
, wxFPB_DEFAULT_YSPACING
, 10);
282 m_leftWindow1
->SizeWindows();
286 void MyFrame::OnCreateBottomStyle(wxCommandEvent
& event
)
288 // recreate with style collapse to bottom, which means
289 // all panels that are collapsed are placed at the bottom,
292 if(event
.IsChecked())
293 _flags
|= wxFPB_COLLAPSE_TO_BOTTOM
;
295 _flags
&= ~wxFPB_COLLAPSE_TO_BOTTOM
;
297 ReCreateFoldPanel(_flags
);
300 void MyFrame::OnCreateNormalStyle(wxCommandEvent
& event
)
302 // receate with style where only one panel at the time is
303 // allowed to be opened
305 // TODO: Not yet implemented!
307 if(event
.IsChecked())
308 _flags
|= wxFPB_SINGLE_FOLD
;
310 _flags
&= ~wxFPB_SINGLE_FOLD
;
312 ReCreateFoldPanel(_flags
);
315 void MyFrame::OnCollapseMe(wxCommandEvent
&event
)
318 for(size_t i
= 0; i
< _pnl
->GetCount(); i
++)
320 item
= _pnl
->Item(i
);
321 _pnl
->Collapse(item
);
325 void MyFrame::OnExpandMe(wxCommandEvent
&event
)
327 wxColour
col1(_rslider1
->GetValue(), _gslider1
->GetValue(), _bslider1
->GetValue()),
328 col2(_rslider2
->GetValue(), _gslider2
->GetValue(), _bslider2
->GetValue());
330 wxCaptionBarStyle style
;
332 style
.SetFirstColour(col1
);
333 style
.SetSecondColour(col2
);
335 _pnl
->ApplyCaptionStyleAll(style
);
338 wxMenuBar
*CreateMenuBar(bool with_window
)
341 wxMenu
*file_menu
= new wxMenu
;
343 file_menu
->Append(FPBTEST_NEW_WINDOW
, _T("&New window"));
345 file_menu
->Append(FPBTEST_CHILD_QUIT
, _T("&Close child"));
347 file_menu
->AppendSeparator();
348 file_menu
->Append(FPBTEST_QUIT
, _T("&Exit"));
350 wxMenu
*option_menu
= 0;
354 option_menu
= new wxMenu
;
355 option_menu
->Append(FPBTEST_REFRESH
, _T("&Refresh picture"));
358 // make fold panel menu
360 wxMenu
*fpb_menu
= new wxMenu
;
361 fpb_menu
->AppendCheckItem(FPB_BOTTOM_STICK
, _T("Create with &wxFPB_COLLAPSE_TO_BOTTOM"));
362 //fpb_menu->AppendCheckItem(FPB_SINGLE_FOLD, _T("Create with &wxFPB_SINGLE_FOLD"));
364 fpb_menu
->AppendSeparator();
365 fpb_menu
->Append(FPBTEST_TOGGLE_WINDOW
, _T("&Toggle FoldPanelBar"));
367 wxMenu
*help_menu
= new wxMenu
;
368 help_menu
->Append(FPBTEST_ABOUT
, _T("&About"));
370 wxMenuBar
*menu_bar
= new wxMenuBar
;
372 menu_bar
->Append(file_menu
, _T("&File"));
373 menu_bar
->Append(fpb_menu
, _T("&FoldPanel"));
375 menu_bar
->Append(option_menu
, _T("&Options"));
376 menu_bar
->Append(help_menu
, _T("&Help"));
381 void MyFrame::OnSlideColour(wxScrollEvent
&event
)
383 wxColour
col1(_rslider1
->GetValue(), _gslider1
->GetValue(), _bslider1
->GetValue()),
384 col2(_rslider2
->GetValue(), _gslider2
->GetValue(), _bslider2
->GetValue());
385 //_btn->SetBackgroundColour(col);
387 wxCaptionBarStyle style
;
389 style
.SetFirstColour(col1
);
390 style
.SetSecondColour(col2
);
392 wxFoldPanel item
= _pnl
->Item(0);
393 _pnl
->ApplyCaptionStyle(item
, style
);
396 void MyFrame::OnStyleChange(wxCommandEvent
&event
)
398 wxCaptionBarStyle style
;
399 switch(event
.GetId())
401 case ID_USE_HGRADIENT
:
402 style
.SetCaptionStyle(wxCAPTIONBAR_GRADIENT_H
);
405 case ID_USE_VGRADIENT
:
406 style
.SetCaptionStyle(wxCAPTIONBAR_GRADIENT_V
);
410 style
.SetCaptionStyle(wxCAPTIONBAR_SINGLE
);
413 case ID_USE_RECTANGLE
:
414 style
.SetCaptionStyle(wxCAPTIONBAR_RECTANGLE
);
417 case ID_USE_FILLED_RECTANGLE
:
418 style
.SetCaptionStyle(wxCAPTIONBAR_FILLED_RECTANGLE
);
425 if(_single
->GetValue())
427 wxFoldPanel item
= _pnl
->Item(1);
428 _pnl
->ApplyCaptionStyle(item
, style
);
431 _pnl
->ApplyCaptionStyleAll(style
);
435 /* ----------------------------------------------------------------------------------------------- */
437 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
438 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
441 // Define a constructor for my canvas
442 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
443 : wxScrolledWindow(parent
, -1, pos
, size
,
444 wxSUNKEN_BORDER
| wxNO_FULL_REPAINT_ON_RESIZE
)
446 SetBackgroundColour(* wxWHITE
);
449 // Define the repainting behaviour
450 void MyCanvas::OnDraw(wxDC
& dc
)
452 dc
.SetFont(*wxSWISS_FONT
);
453 dc
.SetPen(*wxGREEN_PEN
);
454 dc
.DrawLine(0, 0, 200, 200);
455 dc
.DrawLine(200, 0, 0, 200);
457 dc
.SetBrush(*wxCYAN_BRUSH
);
458 dc
.SetPen(*wxRED_PEN
);
459 dc
.DrawRectangle(100, 100, 100, 50);
460 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
462 dc
.DrawEllipse(250, 250, 100, 50);
464 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
465 #endif // wxUSE_SPLINES
466 dc
.DrawLine(50, 230, 200, 230);
467 dc
.DrawText(_T("This is a test string"), 50, 230);
470 points
[0].x
= 200; points
[0].y
= 300;
471 points
[1].x
= 100; points
[1].y
= 400;
472 points
[2].x
= 300; points
[2].y
= 400;
474 dc
.DrawPolygon(3, points
);
477 // This implements a tiny doodling program! Drag the mouse using
479 void MyCanvas::OnEvent(wxMouseEvent
& event
)
484 wxPoint
pt(event
.GetLogicalPosition(dc
));
486 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
488 dc
.SetPen(*wxBLACK_PEN
);
489 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
495 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
497 wxLayoutAlgorithm layout
;
498 layout
.LayoutMDIFrame(this);
501 // Note that FPBTEST_NEW_WINDOW and FPBTEST_ABOUT commands get passed
502 // to the parent window for processing, so no need to
503 // duplicate event handlers here.
505 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
506 EVT_MENU(FPBTEST_CHILD_QUIT
, MyChild::OnQuit
)
509 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
511 wxMDIChildFrame(parent
, -1, title
, pos
, size
, style
)
514 my_children
.Append(this);
517 MyChild::~MyChild(void)
519 my_children
.DeleteObject(this);
522 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
527 void MyChild::OnActivate(wxActivateEvent
& event
)
529 if (event
.GetActive() && canvas
)