]> git.saurik.com Git - wxWidgets.git/blame - samples/splitter/test.cpp
latest changes: added word-wise movement (buggy, esp. backwards), more minor
[wxWidgets.git] / samples / splitter / test.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
0d559d69 2// Name: m_splitter.cpp
c801d85f
KB
3// Purpose: wxSplitterWindow sample
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
0d559d69 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
21#endif
22
23#include "wx/splitter.h"
24
25class MyApp;
26class MyFrame;
27class MyCanvas;
28
29class MyApp: public wxApp
30{
31public:
0d559d69 32 bool OnInit();
c801d85f
KB
33};
34
0d559d69 35class MySplitterWindow : public wxSplitterWindow
c801d85f
KB
36{
37public:
0d559d69
VZ
38 MySplitterWindow(wxFrame *parent, wxWindowID id) : wxSplitterWindow(parent, id)
39 {
40 m_frame = parent;
41 }
42
43 virtual bool OnSashPositionChange(int newSashPosition)
44 {
45 if ( !wxSplitterWindow::OnSashPositionChange(newSashPosition) )
46 return FALSE;
47
48 wxString str;
1e133b7d 49 str.Printf( _T("Sash position = %d"), newSashPosition);
0d559d69
VZ
50 m_frame->SetStatusText(str);
51
52 return TRUE;
53 }
54
55private:
56 wxFrame *m_frame;
57};
c801d85f 58
0d559d69
VZ
59class MyFrame: public wxFrame
60{
61public:
62 MyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size);
63 virtual ~MyFrame();
c801d85f 64
0d559d69
VZ
65 // Menu commands
66 void SplitHorizontal(wxCommandEvent& event);
67 void SplitVertical(wxCommandEvent& event);
68 void Unsplit(wxCommandEvent& event);
69 void SetMinSize(wxCommandEvent& event);
70 void Quit(wxCommandEvent& event);
c801d85f 71
0d559d69
VZ
72 // Menu command update functions
73 void UpdateUIHorizontal(wxUpdateUIEvent& event);
74 void UpdateUIVertical(wxUpdateUIEvent& event);
75 void UpdateUIUnsplit(wxUpdateUIEvent& event);
c801d85f
KB
76
77private:
0d559d69
VZ
78 void UpdatePosition();
79
80 wxMenu* fileMenu;
81 wxMenuBar* menuBar;
82 MyCanvas* m_leftCanvas;
83 MyCanvas* m_rightCanvas;
84 MySplitterWindow* m_splitter;
c801d85f
KB
85
86DECLARE_EVENT_TABLE()
87};
88
89class MyCanvas: public wxScrolledWindow
90{
91public:
90e58684 92 MyCanvas(wxWindow* parent, wxWindowID id, int x, int y, int w, int h, const wxString &name);
c801d85f
KB
93 virtual ~MyCanvas();
94
0d559d69 95 virtual void OnDraw(wxDC& dc);
c801d85f
KB
96
97DECLARE_EVENT_TABLE()
98};
99
100BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
101END_EVENT_TABLE()
102
0d559d69
VZ
103// ID for the menu commands
104enum
105{
106 SPLIT_QUIT,
107 SPLIT_HORIZONTAL,
108 SPLIT_VERTICAL,
109 SPLIT_UNSPLIT,
110 SPLIT_SETMINSIZE
111};
c801d85f 112
0d57be45
JS
113// Window ids
114#define SPLITTER_WINDOW 100
115#define SPLITTER_FRAME 101
116#define CANVAS1 102
117#define CANVAS2 103
118
c801d85f
KB
119IMPLEMENT_APP(MyApp)
120
121bool MyApp::OnInit(void)
122{
0d559d69
VZ
123 MyFrame* frame = new MyFrame((wxFrame *) NULL, "wxSplitterWindow Example",
124 wxPoint(50, 50), wxSize(420, 300));
c801d85f 125
0d559d69
VZ
126 // Show the frame
127 frame->Show(TRUE);
c801d85f 128
0d559d69 129 SetTopWindow(frame);
c801d85f 130
0d559d69 131 return TRUE;
c801d85f
KB
132}
133
134BEGIN_EVENT_TABLE(MyFrame, wxFrame)
0d559d69
VZ
135 EVT_MENU(SPLIT_VERTICAL, MyFrame::SplitVertical)
136 EVT_MENU(SPLIT_HORIZONTAL, MyFrame::SplitHorizontal)
137 EVT_MENU(SPLIT_UNSPLIT, MyFrame::Unsplit)
138 EVT_MENU(SPLIT_QUIT, MyFrame::Quit)
139 EVT_MENU(SPLIT_SETMINSIZE, MyFrame::SetMinSize)
140
141 EVT_UPDATE_UI(SPLIT_VERTICAL, MyFrame::UpdateUIVertical)
142 EVT_UPDATE_UI(SPLIT_HORIZONTAL, MyFrame::UpdateUIHorizontal)
143 EVT_UPDATE_UI(SPLIT_UNSPLIT, MyFrame::UpdateUIUnsplit)
c801d85f
KB
144END_EVENT_TABLE()
145
146// My frame constructor
147MyFrame::MyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size):
0d57be45 148 wxFrame(frame, SPLITTER_FRAME, title, pos, size)
c801d85f 149{
0d559d69 150 CreateStatusBar(2);
b7346a70 151
0d559d69
VZ
152 // Make a menubar
153 fileMenu = new wxMenu;
154 fileMenu->Append(SPLIT_VERTICAL, "Split &Vertically", "Split vertically");
155 fileMenu->Append(SPLIT_HORIZONTAL, "Split &Horizontally", "Split horizontally");
156 fileMenu->Append(SPLIT_UNSPLIT, "&Unsplit", "Unsplit");
157 fileMenu->AppendSeparator();
158 fileMenu->Append(SPLIT_SETMINSIZE, "Set &min size", "Set minimum pane size");
159 fileMenu->AppendSeparator();
160 fileMenu->Append(SPLIT_QUIT, "E&xit", "Exit");
c801d85f 161
0d559d69
VZ
162 menuBar = new wxMenuBar;
163 menuBar->Append(fileMenu, "&File");
c801d85f 164
0d559d69 165 SetMenuBar(menuBar);
c801d85f 166
0d559d69 167 m_splitter = new MySplitterWindow(this, SPLITTER_WINDOW);
c801d85f 168
90e58684 169 m_leftCanvas = new MyCanvas(m_splitter, CANVAS1, 0, 0, 400, 400, "Test1" );
0d559d69
VZ
170 m_leftCanvas->SetBackgroundColour(*wxRED);
171 m_leftCanvas->SetScrollbars(20, 20, 50, 50);
f4621a09 172 m_leftCanvas->SetCursor(wxCursor(wxCURSOR_MAGNIFIER));
c801d85f 173
90e58684 174 m_rightCanvas = new MyCanvas(m_splitter, CANVAS2, 0, 0, 400, 400, "Test2" );
0d559d69
VZ
175 m_rightCanvas->SetBackgroundColour(*wxCYAN);
176 m_rightCanvas->SetScrollbars(20, 20, 50, 50);
177 m_rightCanvas->Show(FALSE);
c801d85f 178
0d559d69
VZ
179 m_splitter->Initialize(m_leftCanvas);
180 SetStatusText("Min pane size = 0", 1);
c801d85f
KB
181}
182
183MyFrame::~MyFrame()
184{
185}
186
e3e65dac 187void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
c801d85f 188{
0d559d69 189 Close(TRUE);
c801d85f
KB
190}
191
e3e65dac 192void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) )
c801d85f 193{
0d559d69
VZ
194 if ( m_splitter->IsSplit() )
195 m_splitter->Unsplit();
196 m_leftCanvas->Show(TRUE);
197 m_rightCanvas->Show(TRUE);
198 m_splitter->SplitHorizontally( m_leftCanvas, m_rightCanvas );
199 UpdatePosition();
c801d85f
KB
200}
201
e3e65dac 202void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
c801d85f 203{
0d559d69
VZ
204 if ( m_splitter->IsSplit() )
205 m_splitter->Unsplit();
206 m_leftCanvas->Show(TRUE);
207 m_rightCanvas->Show(TRUE);
208 m_splitter->SplitVertically( m_leftCanvas, m_rightCanvas );
209 UpdatePosition();
c801d85f
KB
210}
211
e3e65dac 212void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
c801d85f 213{
0d559d69
VZ
214 if ( m_splitter->IsSplit() )
215 m_splitter->Unsplit();
216 SetStatusText("No splitter");
217}
218
219void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
220{
221 wxString str;
1e133b7d 222 str.Printf( _T("%d"), m_splitter->GetMinimumPaneSize());
0d559d69
VZ
223 str = wxGetTextFromUser("Enter minimal size for panes:", "", str, this);
224 if ( str.IsEmpty() )
225 return;
226
1b3667ab 227 int minsize = wxStrtol( str, (wxChar**)NULL, 10 );
0d559d69 228 m_splitter->SetMinimumPaneSize(minsize);
1e133b7d 229 str.Printf( _T("Min pane size = %d"), minsize);
0d559d69 230 SetStatusText(str, 1);
c801d85f
KB
231}
232
233void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
234{
0d559d69 235 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) ) );
c801d85f
KB
236}
237
238void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
239{
0d559d69 240 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
c801d85f
KB
241}
242
243void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
244{
0d559d69 245 event.Enable( m_splitter->IsSplit() );
c801d85f
KB
246}
247
0d559d69 248void MyFrame::UpdatePosition()
c801d85f 249{
0d559d69 250 wxString str;
1e133b7d 251 str.Printf( _("Sash position = %d"), m_splitter->GetSashPosition());
0d559d69 252 SetStatusText(str);
c801d85f
KB
253}
254
90e58684
RR
255MyCanvas::MyCanvas(wxWindow* parent, wxWindowID id, int x, int y, int w, int h, const wxString &name ) :
256 wxScrolledWindow(parent, id, wxPoint(x, y), wxSize(w, h), 0, name )
c801d85f
KB
257{
258}
259
260MyCanvas::~MyCanvas()
261{
262}
263
264void MyCanvas::OnDraw(wxDC& dc)
265{
0d559d69
VZ
266 dc.SetPen(*wxBLACK_PEN);
267 dc.DrawLine(0, 0, 100, 100);
c801d85f 268
0d559d69
VZ
269 dc.SetBackgroundMode(wxTRANSPARENT);
270 dc.DrawText("Testing", 50, 50);
b7346a70 271
0d559d69
VZ
272 dc.SetPen(*wxRED_PEN);
273 dc.SetBrush(*wxGREEN_BRUSH);
274 dc.DrawRectangle(120, 120, 100, 80);
c801d85f 275}