]> git.saurik.com Git - wxWidgets.git/blame - samples/splitter/splitter.cpp
Implemented simplistic ConvertToImage()
[wxWidgets.git] / samples / splitter / splitter.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
6718d773 2// Name: splitter.cpp
c801d85f
KB
3// Purpose: wxSplitterWindow sample
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
0d559d69 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
2f294a9c
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c801d85f
KB
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
2f294a9c 24 #pragma hdrstop
c801d85f
KB
25#endif
26
27#ifndef WX_PRECOMP
6faed57d
VZ
28 #include "wx/log.h"
29
30 #include "wx/app.h"
31 #include "wx/frame.h"
32
33 #include "wx/scrolwin.h"
34 #include "wx/menu.h"
35
36 #include "wx/textdlg.h" // for wxGetTextFromUser
c801d85f
KB
37#endif
38
39#include "wx/splitter.h"
a63e17c1 40#include "wx/dcmirror.h"
c801d85f 41
2f294a9c
VZ
42// ----------------------------------------------------------------------------
43// constants
44// ----------------------------------------------------------------------------
45
46// ID for the menu commands
47enum
48{
49 SPLIT_QUIT,
50 SPLIT_HORIZONTAL,
51 SPLIT_VERTICAL,
52 SPLIT_UNSPLIT,
b795f9ca 53 SPLIT_LIVE,
6faed57d 54 SPLIT_SETPOSITION,
14b4c0ff
VZ
55 SPLIT_SETMINSIZE,
56 SPLIT_SETGRAVITY
2f294a9c
VZ
57};
58
59// ----------------------------------------------------------------------------
60// our classes
61// ----------------------------------------------------------------------------
c801d85f
KB
62
63class MyApp: public wxApp
64{
65public:
a63e17c1
VZ
66 MyApp() { }
67
68 virtual bool OnInit();
69
70 DECLARE_NO_COPY_CLASS(MyApp)
c801d85f
KB
71};
72
2f294a9c 73class MyFrame: public wxFrame
c801d85f
KB
74{
75public:
2f294a9c 76 MyFrame();
925e9792 77 virtual ~MyFrame(){};
0d559d69 78
2f294a9c
VZ
79 // Menu commands
80 void SplitHorizontal(wxCommandEvent& event);
81 void SplitVertical(wxCommandEvent& event);
82 void Unsplit(wxCommandEvent& event);
b795f9ca 83 void ToggleLive(wxCommandEvent& event);
6faed57d 84 void SetPosition(wxCommandEvent& event);
2f294a9c 85 void SetMinSize(wxCommandEvent& event);
14b4c0ff 86 void SetGravity(wxCommandEvent& event);
b795f9ca 87
2f294a9c 88 void Quit(wxCommandEvent& event);
4a0b46a7 89
2f294a9c
VZ
90 // Menu command update functions
91 void UpdateUIHorizontal(wxUpdateUIEvent& event);
92 void UpdateUIVertical(wxUpdateUIEvent& event);
93 void UpdateUIUnsplit(wxUpdateUIEvent& event);
4a0b46a7 94
0d559d69 95private:
2f294a9c
VZ
96 wxScrolledWindow *m_left, *m_right;
97
98 wxSplitterWindow* m_splitter;
99
100 DECLARE_EVENT_TABLE()
a63e17c1 101 DECLARE_NO_COPY_CLASS(MyFrame)
0d559d69 102};
c801d85f 103
2f294a9c 104class MySplitterWindow : public wxSplitterWindow
0d559d69
VZ
105{
106public:
2f294a9c 107 MySplitterWindow(wxFrame *parent);
c801d85f 108
2f294a9c
VZ
109 // event handlers
110 void OnPositionChanged(wxSplitterEvent& event);
111 void OnPositionChanging(wxSplitterEvent& event);
112 void OnDClick(wxSplitterEvent& event);
8ad18dc3 113 void OnUnsplitEvent(wxSplitterEvent& event);
c801d85f
KB
114
115private:
2f294a9c 116 wxFrame *m_frame;
0d559d69 117
2f294a9c 118 DECLARE_EVENT_TABLE()
a63e17c1 119 DECLARE_NO_COPY_CLASS(MySplitterWindow)
c801d85f
KB
120};
121
122class MyCanvas: public wxScrolledWindow
123{
124public:
a63e17c1 125 MyCanvas(wxWindow* parent, bool mirror);
925e9792 126 virtual ~MyCanvas(){};
c801d85f 127
2f294a9c 128 virtual void OnDraw(wxDC& dc);
a63e17c1
VZ
129
130private:
131 bool m_mirror;
132
133 DECLARE_NO_COPY_CLASS(MyCanvas)
c801d85f
KB
134};
135
2f294a9c
VZ
136// ============================================================================
137// implementation
138// ============================================================================
c801d85f 139
2f294a9c
VZ
140// ----------------------------------------------------------------------------
141// MyApp
142// ----------------------------------------------------------------------------
0d57be45 143
c801d85f
KB
144IMPLEMENT_APP(MyApp)
145
2f294a9c 146bool MyApp::OnInit()
c801d85f 147{
2f294a9c
VZ
148 // create and show the main frame
149 MyFrame* frame = new MyFrame;
4a0b46a7 150
8ad18dc3 151 frame->Show(true);
c801d85f 152
8ad18dc3 153 return true;
c801d85f
KB
154}
155
2f294a9c
VZ
156// ----------------------------------------------------------------------------
157// MyFrame
158// ----------------------------------------------------------------------------
159
c801d85f 160BEGIN_EVENT_TABLE(MyFrame, wxFrame)
2f294a9c
VZ
161 EVT_MENU(SPLIT_VERTICAL, MyFrame::SplitVertical)
162 EVT_MENU(SPLIT_HORIZONTAL, MyFrame::SplitHorizontal)
163 EVT_MENU(SPLIT_UNSPLIT, MyFrame::Unsplit)
b795f9ca 164 EVT_MENU(SPLIT_LIVE, MyFrame::ToggleLive)
6faed57d 165 EVT_MENU(SPLIT_SETPOSITION, MyFrame::SetPosition)
2f294a9c 166 EVT_MENU(SPLIT_SETMINSIZE, MyFrame::SetMinSize)
14b4c0ff 167 EVT_MENU(SPLIT_SETGRAVITY, MyFrame::SetGravity)
2f294a9c 168
b795f9ca
VZ
169 EVT_MENU(SPLIT_QUIT, MyFrame::Quit)
170
2f294a9c
VZ
171 EVT_UPDATE_UI(SPLIT_VERTICAL, MyFrame::UpdateUIVertical)
172 EVT_UPDATE_UI(SPLIT_HORIZONTAL, MyFrame::UpdateUIHorizontal)
173 EVT_UPDATE_UI(SPLIT_UNSPLIT, MyFrame::UpdateUIUnsplit)
c801d85f
KB
174END_EVENT_TABLE()
175
176// My frame constructor
2f294a9c 177MyFrame::MyFrame()
8ad18dc3 178 : wxFrame(NULL, wxID_ANY, _T("wxSplitterWindow sample"),
2f294a9c 179 wxDefaultPosition, wxSize(420, 300),
fe31f91c 180 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 181{
8520f137 182#if wxUSE_STATUSBAR
2f294a9c 183 CreateStatusBar(2);
8520f137 184#endif // wxUSE_STATUSBAR
b7346a70 185
2f294a9c 186 // Make a menubar
6faed57d
VZ
187 wxMenu *splitMenu = new wxMenu;
188 splitMenu->Append(SPLIT_VERTICAL,
189 _T("Split &Vertically\tCtrl-V"),
190 _T("Split vertically"));
191 splitMenu->Append(SPLIT_HORIZONTAL,
192 _T("Split &Horizontally\tCtrl-H"),
193 _T("Split horizontally"));
194 splitMenu->Append(SPLIT_UNSPLIT,
195 _T("&Unsplit\tCtrl-U"),
196 _T("Unsplit"));
197 splitMenu->AppendSeparator();
198
199 splitMenu->AppendCheckItem(SPLIT_LIVE,
200 _T("&Live update\tCtrl-L"),
201 _T("Toggle live update mode"));
202 splitMenu->Append(SPLIT_SETPOSITION,
203 _T("Set splitter &position\tCtrl-P"),
204 _T("Set the splitter position"));
205 splitMenu->Append(SPLIT_SETMINSIZE,
206 _T("Set &min size\tCtrl-M"),
207 _T("Set minimum pane size"));
14b4c0ff
VZ
208 splitMenu->Append(SPLIT_SETGRAVITY,
209 _T("Set &gravity\tCtrl-G"),
210 _T("Set gravity of sash"));
6faed57d
VZ
211 splitMenu->AppendSeparator();
212
213 splitMenu->Append(SPLIT_QUIT, _T("E&xit\tAlt-X"), _T("Exit"));
c801d85f 214
2f294a9c 215 wxMenuBar *menuBar = new wxMenuBar;
6faed57d 216 menuBar->Append(splitMenu, _T("&Splitter"));
c801d85f 217
2f294a9c 218 SetMenuBar(menuBar);
c801d85f 219
8ad18dc3 220 menuBar->Check(SPLIT_LIVE, true);
2f294a9c 221 m_splitter = new MySplitterWindow(this);
14b4c0ff
VZ
222
223 m_splitter->SetSashGravity(1.0);
4a0b46a7 224
6b55490a 225#if 1
a63e17c1 226 m_left = new MyCanvas(m_splitter, true);
2f294a9c 227 m_left->SetBackgroundColour(*wxRED);
a63e17c1 228 m_left->SetScrollbars(20, 20, 5, 5);
2f294a9c
VZ
229 m_left->SetCursor(wxCursor(wxCURSOR_MAGNIFIER));
230
a63e17c1 231 m_right = new MyCanvas(m_splitter, false);
2f294a9c 232 m_right->SetBackgroundColour(*wxCYAN);
a63e17c1 233 m_right->SetScrollbars(20, 20, 5, 5);
6b55490a 234#else // for testing kbd navigation inside the splitter
8ad18dc3
JS
235 m_left = new wxTextCtrl(m_splitter, wxID_ANY, _T("first text"));
236 m_right = new wxTextCtrl(m_splitter, wxID_ANY, _T("second text"));
6b55490a 237#endif
c801d85f 238
2f294a9c 239 // you can also do this to start with a single window
4a0b46a7 240#if 0
8ad18dc3 241 m_right->Show(false);
2f294a9c 242 m_splitter->Initialize(m_left);
4a0b46a7 243#else
74c57d1f 244 // you can also try -100
2f294a9c 245 m_splitter->SplitVertically(m_left, m_right, 100);
4a0b46a7
VZ
246#endif
247
8520f137 248#if wxUSE_STATUSBAR
2f294a9c 249 SetStatusText(_T("Min pane size = 0"), 1);
8520f137 250#endif // wxUSE_STATUSBAR
c801d85f
KB
251}
252
2f294a9c
VZ
253// menu command handlers
254
e3e65dac 255void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
c801d85f 256{
8ad18dc3 257 Close(true);
c801d85f
KB
258}
259
e3e65dac 260void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) )
c801d85f 261{
2f294a9c
VZ
262 if ( m_splitter->IsSplit() )
263 m_splitter->Unsplit();
8ad18dc3
JS
264 m_left->Show(true);
265 m_right->Show(true);
2f294a9c 266 m_splitter->SplitHorizontally( m_left, m_right );
74c57d1f 267
8520f137 268#if wxUSE_STATUSBAR
74c57d1f 269 SetStatusText(_T("Splitter split horizontally"), 1);
8520f137 270#endif // wxUSE_STATUSBAR
c801d85f
KB
271}
272
e3e65dac 273void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
c801d85f 274{
2f294a9c
VZ
275 if ( m_splitter->IsSplit() )
276 m_splitter->Unsplit();
8ad18dc3
JS
277 m_left->Show(true);
278 m_right->Show(true);
2f294a9c 279 m_splitter->SplitVertically( m_left, m_right );
74c57d1f 280
8520f137 281#if wxUSE_STATUSBAR
74c57d1f 282 SetStatusText(_T("Splitter split vertically"), 1);
8520f137 283#endif // wxUSE_STATUSBAR
c801d85f
KB
284}
285
e3e65dac 286void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
c801d85f 287{
2f294a9c
VZ
288 if ( m_splitter->IsSplit() )
289 m_splitter->Unsplit();
8520f137 290#if wxUSE_STATUSBAR
2f294a9c 291 SetStatusText(_T("No splitter"));
8520f137 292#endif // wxUSE_STATUSBAR
0d559d69
VZ
293}
294
b795f9ca
VZ
295void MyFrame::ToggleLive(wxCommandEvent& event )
296{
297 long style = m_splitter->GetWindowStyleFlag();
298 if ( event.IsChecked() )
299 style |= wxSP_LIVE_UPDATE;
300 else
301 style &= ~wxSP_LIVE_UPDATE;
302
303 m_splitter->SetWindowStyleFlag(style);
304}
305
6faed57d
VZ
306void MyFrame::SetPosition(wxCommandEvent& WXUNUSED(event) )
307{
308 wxString str;
309 str.Printf( wxT("%d"), m_splitter->GetSashPosition());
310 str = wxGetTextFromUser(_T("Enter splitter position:"), _T(""), str, this);
311 if ( str.empty() )
312 return;
313
314 long pos;
315 if ( !str.ToLong(&pos) )
316 {
317 wxLogError(_T("The splitter position should be an integer."));
318 return;
319 }
320
321 m_splitter->SetSashPosition(pos);
322
323 wxLogStatus(this, _T("Splitter position set to %ld"), pos);
324}
325
0d559d69
VZ
326void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
327{
2f294a9c 328 wxString str;
f565a6c2 329 str.Printf( wxT("%d"), m_splitter->GetMinimumPaneSize());
2f294a9c 330 str = wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str, this);
6faed57d 331 if ( str.empty() )
2f294a9c
VZ
332 return;
333
334 int minsize = wxStrtol( str, (wxChar**)NULL, 10 );
335 m_splitter->SetMinimumPaneSize(minsize);
8520f137 336#if wxUSE_STATUSBAR
f565a6c2 337 str.Printf( wxT("Min pane size = %d"), minsize);
2f294a9c 338 SetStatusText(str, 1);
8520f137 339#endif // wxUSE_STATUSBAR
c801d85f 340}
14b4c0ff
VZ
341void MyFrame::SetGravity(wxCommandEvent& WXUNUSED(event) )
342{
343 wxString str;
344 str.Printf( wxT("%g"), m_splitter->GetSashGravity());
345 str = wxGetTextFromUser(_T("Enter sash gravity (0,1):"), _T(""), str, this);
346 if ( str.empty() )
347 return;
348
349 double gravity = wxStrtod( str, (wxChar**)NULL);
350 m_splitter->SetSashGravity(gravity);
351#if wxUSE_STATUSBAR
352 str.Printf( wxT("Gravity = %g"), gravity);
353 SetStatusText(str, 1);
354#endif // wxUSE_STATUSBAR
355}
c801d85f 356
2f294a9c
VZ
357// Update UI handlers
358
c801d85f
KB
359void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
360{
2f294a9c 361 event.Enable( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) );
c801d85f
KB
362}
363
364void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
365{
2f294a9c 366 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
c801d85f
KB
367}
368
369void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
370{
2f294a9c 371 event.Enable( m_splitter->IsSplit() );
c801d85f
KB
372}
373
2f294a9c
VZ
374// ----------------------------------------------------------------------------
375// MySplitterWindow
376// ----------------------------------------------------------------------------
377
378BEGIN_EVENT_TABLE(MySplitterWindow, wxSplitterWindow)
8ad18dc3
JS
379 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY, MySplitterWindow::OnPositionChanged)
380 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY, MySplitterWindow::OnPositionChanging)
2f294a9c 381
8ad18dc3 382 EVT_SPLITTER_DCLICK(wxID_ANY, MySplitterWindow::OnDClick)
2f294a9c 383
8ad18dc3 384 EVT_SPLITTER_UNSPLIT(wxID_ANY, MySplitterWindow::OnUnsplitEvent)
2f294a9c
VZ
385END_EVENT_TABLE()
386
387MySplitterWindow::MySplitterWindow(wxFrame *parent)
8ad18dc3 388 : wxSplitterWindow(parent, wxID_ANY,
2f294a9c 389 wxDefaultPosition, wxDefaultSize,
3c2544bb
JS
390 wxSP_3D | wxSP_LIVE_UPDATE |
391 wxCLIP_CHILDREN /* | wxSP_NO_XP_THEME */ )
c801d85f 392{
2f294a9c 393 m_frame = parent;
c801d85f
KB
394}
395
2f294a9c
VZ
396void MySplitterWindow::OnPositionChanged(wxSplitterEvent& event)
397{
398 wxLogStatus(m_frame, _T("Position has changed, now = %d (or %d)"),
399 event.GetSashPosition(), GetSashPosition());
400
401 event.Skip();
402}
403
404void MySplitterWindow::OnPositionChanging(wxSplitterEvent& event)
405{
406 wxLogStatus(m_frame, _T("Position is changing, now = %d (or %d)"),
407 event.GetSashPosition(), GetSashPosition());
408
409 event.Skip();
410}
411
412void MySplitterWindow::OnDClick(wxSplitterEvent& event)
413{
8520f137 414#if wxUSE_STATUSBAR
2f294a9c 415 m_frame->SetStatusText(_T("Splitter double clicked"), 1);
8520f137 416#endif // wxUSE_STATUSBAR
2f294a9c
VZ
417
418 event.Skip();
419}
420
8ad18dc3 421void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent& event)
2f294a9c 422{
8520f137 423#if wxUSE_STATUSBAR
2f294a9c 424 m_frame->SetStatusText(_T("Splitter unsplit"), 1);
8520f137 425#endif // wxUSE_STATUSBAR
2f294a9c
VZ
426
427 event.Skip();
428}
429
430// ----------------------------------------------------------------------------
431// MyCanvas
432// ----------------------------------------------------------------------------
433
a63e17c1 434MyCanvas::MyCanvas(wxWindow* parent, bool mirror)
8ad18dc3 435 : wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
a63e17c1 436 wxHSCROLL | wxVSCROLL | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 437{
a63e17c1 438 m_mirror = mirror;
c801d85f
KB
439}
440
a63e17c1 441void MyCanvas::OnDraw(wxDC& dcOrig)
c801d85f 442{
a63e17c1
VZ
443 wxMirrorDC dc(dcOrig, m_mirror);
444
2f294a9c 445 dc.SetPen(*wxBLACK_PEN);
a63e17c1 446 dc.DrawLine(0, 0, 100, 200);
c801d85f 447
2f294a9c
VZ
448 dc.SetBackgroundMode(wxTRANSPARENT);
449 dc.DrawText(_T("Testing"), 50, 50);
b7346a70 450
2f294a9c
VZ
451 dc.SetPen(*wxRED_PEN);
452 dc.SetBrush(*wxGREEN_BRUSH);
453 dc.DrawRectangle(120, 120, 100, 80);
c801d85f 454}
2f294a9c 455