1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSplitterWindow sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/splitter.h"
29 class MyApp
: public wxApp
35 class MySplitterWindow
: public wxSplitterWindow
38 MySplitterWindow(wxFrame
*parent
, wxWindowID id
)
39 : wxSplitterWindow(parent
, id
, wxDefaultPosition
, wxDefaultSize
,
40 wxSP_3D
| wxSP_LIVE_UPDATE
| wxCLIP_CHILDREN
)
45 virtual bool OnSashPositionChange(int newSashPosition
)
47 if ( !wxSplitterWindow::OnSashPositionChange(newSashPosition
) )
51 str
.Printf( _T("Sash position = %d"), newSashPosition
);
52 m_frame
->SetStatusText(str
);
61 class MyFrame
: public wxFrame
64 MyFrame(wxFrame
* frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
68 void SplitHorizontal(wxCommandEvent
& event
);
69 void SplitVertical(wxCommandEvent
& event
);
70 void Unsplit(wxCommandEvent
& event
);
71 void SetMinSize(wxCommandEvent
& event
);
72 void Quit(wxCommandEvent
& event
);
74 // Menu command update functions
75 void UpdateUIHorizontal(wxUpdateUIEvent
& event
);
76 void UpdateUIVertical(wxUpdateUIEvent
& event
);
77 void UpdateUIUnsplit(wxUpdateUIEvent
& event
);
80 void UpdatePosition();
85 MySplitterWindow
* m_splitter
;
90 class MyCanvas
: public wxScrolledWindow
93 MyCanvas(wxWindow
* parent
, wxWindowID id
= -1, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
, const wxString
& name
= "");
96 virtual void OnDraw(wxDC
& dc
);
101 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
104 // ID for the menu commands
115 #define SPLITTER_WINDOW 100
116 #define SPLITTER_FRAME 101
122 bool MyApp::OnInit(void)
124 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, "wxSplitterWindow Example",
125 wxPoint(50, 50), wxSize(420, 300));
135 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
136 EVT_MENU(SPLIT_VERTICAL
, MyFrame::SplitVertical
)
137 EVT_MENU(SPLIT_HORIZONTAL
, MyFrame::SplitHorizontal
)
138 EVT_MENU(SPLIT_UNSPLIT
, MyFrame::Unsplit
)
139 EVT_MENU(SPLIT_QUIT
, MyFrame::Quit
)
140 EVT_MENU(SPLIT_SETMINSIZE
, MyFrame::SetMinSize
)
142 EVT_UPDATE_UI(SPLIT_VERTICAL
, MyFrame::UpdateUIVertical
)
143 EVT_UPDATE_UI(SPLIT_HORIZONTAL
, MyFrame::UpdateUIHorizontal
)
144 EVT_UPDATE_UI(SPLIT_UNSPLIT
, MyFrame::UpdateUIUnsplit
)
147 // My frame constructor
148 MyFrame::MyFrame(wxFrame
* frame
, const wxString
& title
,
149 const wxPoint
& pos
, const wxSize
& size
)
150 : wxFrame(frame
, SPLITTER_FRAME
, title
, pos
, size
,
151 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
156 wxMenu
*fileMenu
= new wxMenu
;
157 fileMenu
->Append(SPLIT_VERTICAL
, "Split &Vertically\tCtrl-V", "Split vertically");
158 fileMenu
->Append(SPLIT_HORIZONTAL
, "Split &Horizontally\tCtrl-H", "Split horizontally");
159 fileMenu
->Append(SPLIT_UNSPLIT
, "&Unsplit\tCtrl-U", "Unsplit");
160 fileMenu
->AppendSeparator();
161 fileMenu
->Append(SPLIT_SETMINSIZE
, "Set &min size", "Set minimum pane size");
162 fileMenu
->AppendSeparator();
163 fileMenu
->Append(SPLIT_QUIT
, "E&xit\tAlt-X", "Exit");
165 wxMenuBar
*menuBar
= new wxMenuBar
;
166 menuBar
->Append(fileMenu
, "&File");
170 m_splitter
= new MySplitterWindow(this, SPLITTER_WINDOW
);
173 m_left
= new MyCanvas(m_splitter
, CANVAS1
, wxPoint(0, 0), wxSize(400, 400), "Test1" );
174 m_left
->SetBackgroundColour(*wxRED
);
175 m_left
->SetScrollbars(20, 20, 50, 50);
176 m_left
->SetCursor(wxCursor(wxCURSOR_MAGNIFIER
));
178 m_right
= new MyCanvas(m_splitter
, CANVAS2
, wxPoint(0, 0), wxSize(400, 400), "Test2" );
179 m_right
->SetBackgroundColour(*wxCYAN
);
180 m_right
->SetScrollbars(20, 20, 50, 50);
181 #else // for testing kbd navigation inside the splitter
182 m_left
= new wxTextCtrl(m_splitter
, -1, "first text");
183 m_right
= new wxTextCtrl(m_splitter
, -1, "second text");
186 // you can also do this to start with a single window
188 m_right
->Show(FALSE
);
189 m_splitter
->Initialize(m_left
);
191 m_splitter
->SplitVertically(m_left
, m_right
, 100);
194 SetStatusText("Min pane size = 0", 1);
201 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
206 void MyFrame::SplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
208 if ( m_splitter
->IsSplit() )
209 m_splitter
->Unsplit();
212 m_splitter
->SplitHorizontally( m_left
, m_right
);
216 void MyFrame::SplitVertical(wxCommandEvent
& WXUNUSED(event
) )
218 if ( m_splitter
->IsSplit() )
219 m_splitter
->Unsplit();
222 m_splitter
->SplitVertically( m_left
, m_right
);
226 void MyFrame::Unsplit(wxCommandEvent
& WXUNUSED(event
) )
228 if ( m_splitter
->IsSplit() )
229 m_splitter
->Unsplit();
230 SetStatusText("No splitter");
233 void MyFrame::SetMinSize(wxCommandEvent
& WXUNUSED(event
) )
236 str
.Printf( _T("%d"), m_splitter
->GetMinimumPaneSize());
237 str
= wxGetTextFromUser("Enter minimal size for panes:", "", str
, this);
241 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
242 m_splitter
->SetMinimumPaneSize(minsize
);
243 str
.Printf( _T("Min pane size = %d"), minsize
);
244 SetStatusText(str
, 1);
247 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent
& event
)
249 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) ) );
252 void MyFrame::UpdateUIVertical(wxUpdateUIEvent
& event
)
254 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
257 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent
& event
)
259 event
.Enable( m_splitter
->IsSplit() );
262 void MyFrame::UpdatePosition()
265 str
.Printf( _("Sash position = %d"), m_splitter
->GetSashPosition());
269 MyCanvas::MyCanvas(wxWindow
* parent
, wxWindowID id
, const wxPoint
& point
, const wxSize
& size
, const wxString
&name
) :
270 wxScrolledWindow(parent
, id
, point
, size
, 0, name
)
274 MyCanvas::~MyCanvas()
278 void MyCanvas::OnDraw(wxDC
& dc
)
280 dc
.SetPen(*wxBLACK_PEN
);
281 dc
.DrawLine(0, 0, 100, 100);
283 dc
.SetBackgroundMode(wxTRANSPARENT
);
284 dc
.DrawText("Testing", 50, 50);
286 dc
.SetPen(*wxRED_PEN
);
287 dc
.SetBrush(*wxGREEN_BRUSH
);
288 dc
.DrawRectangle(120, 120, 100, 80);