]> git.saurik.com Git - wxWidgets.git/blob - samples/splitter/splitter.cpp
Misc release-related mods
[wxWidgets.git] / samples / splitter / splitter.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: splitter.cpp
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
9 // Licence: wxWindows license
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
25 class MyApp;
26 class MyFrame;
27 class MyCanvas;
28
29 class MyApp: public wxApp
30 {
31 public:
32 bool OnInit();
33 };
34
35 class MySplitterWindow : public wxSplitterWindow
36 {
37 public:
38 MySplitterWindow(wxFrame *parent, wxWindowID id)
39 : wxSplitterWindow(parent, id, wxDefaultPosition, wxDefaultSize, wxSP_3D | wxSP_LIVE_UPDATE)
40 {
41 m_frame = parent;
42 }
43
44 virtual bool OnSashPositionChange(int newSashPosition)
45 {
46 if ( !wxSplitterWindow::OnSashPositionChange(newSashPosition) )
47 return FALSE;
48
49 wxString str;
50 str.Printf( _T("Sash position = %d"), newSashPosition);
51 m_frame->SetStatusText(str);
52
53 return TRUE;
54 }
55
56 private:
57 wxFrame *m_frame;
58 };
59
60 class MyFrame: public wxFrame
61 {
62 public:
63 MyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size);
64 virtual ~MyFrame();
65
66 // Menu commands
67 void SplitHorizontal(wxCommandEvent& event);
68 void SplitVertical(wxCommandEvent& event);
69 void Unsplit(wxCommandEvent& event);
70 void SetMinSize(wxCommandEvent& event);
71 void Quit(wxCommandEvent& event);
72
73 // Menu command update functions
74 void UpdateUIHorizontal(wxUpdateUIEvent& event);
75 void UpdateUIVertical(wxUpdateUIEvent& event);
76 void UpdateUIUnsplit(wxUpdateUIEvent& event);
77
78 private:
79 void UpdatePosition();
80
81 wxMenu* fileMenu;
82 wxMenuBar* menuBar;
83 MyCanvas* m_leftCanvas;
84 MyCanvas* m_rightCanvas;
85 MySplitterWindow* m_splitter;
86
87 DECLARE_EVENT_TABLE()
88 };
89
90 class MyCanvas: public wxScrolledWindow
91 {
92 public:
93 MyCanvas(wxWindow* parent, wxWindowID id = -1, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, const wxString& name = "");
94 virtual ~MyCanvas();
95
96 virtual void OnDraw(wxDC& dc);
97
98 DECLARE_EVENT_TABLE()
99 };
100
101 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
102 END_EVENT_TABLE()
103
104 // ID for the menu commands
105 enum
106 {
107 SPLIT_QUIT,
108 SPLIT_HORIZONTAL,
109 SPLIT_VERTICAL,
110 SPLIT_UNSPLIT,
111 SPLIT_SETMINSIZE
112 };
113
114 // Window ids
115 #define SPLITTER_WINDOW 100
116 #define SPLITTER_FRAME 101
117 #define CANVAS1 102
118 #define CANVAS2 103
119
120 IMPLEMENT_APP(MyApp)
121
122 bool MyApp::OnInit(void)
123 {
124 MyFrame* frame = new MyFrame((wxFrame *) NULL, "wxSplitterWindow Example",
125 wxPoint(50, 50), wxSize(420, 300));
126
127 // Show the frame
128 frame->Show(TRUE);
129
130 SetTopWindow(frame);
131
132 return TRUE;
133 }
134
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)
141
142 EVT_UPDATE_UI(SPLIT_VERTICAL, MyFrame::UpdateUIVertical)
143 EVT_UPDATE_UI(SPLIT_HORIZONTAL, MyFrame::UpdateUIHorizontal)
144 EVT_UPDATE_UI(SPLIT_UNSPLIT, MyFrame::UpdateUIUnsplit)
145 END_EVENT_TABLE()
146
147 // My frame constructor
148 MyFrame::MyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size):
149 wxFrame(frame, SPLITTER_FRAME, title, pos, size)
150 {
151 CreateStatusBar(2);
152
153 // Make a menubar
154 fileMenu = new wxMenu;
155 fileMenu->Append(SPLIT_VERTICAL, "Split &Vertically\tCtrl-V", "Split vertically");
156 fileMenu->Append(SPLIT_HORIZONTAL, "Split &Horizontally\tCtrl-H", "Split horizontally");
157 fileMenu->Append(SPLIT_UNSPLIT, "&Unsplit\tCtrl-U", "Unsplit");
158 fileMenu->AppendSeparator();
159 fileMenu->Append(SPLIT_SETMINSIZE, "Set &min size", "Set minimum pane size");
160 fileMenu->AppendSeparator();
161 fileMenu->Append(SPLIT_QUIT, "E&xit\tAlt-X", "Exit");
162
163 menuBar = new wxMenuBar;
164 menuBar->Append(fileMenu, "&File");
165
166 SetMenuBar(menuBar);
167
168 m_splitter = new MySplitterWindow(this, SPLITTER_WINDOW);
169
170 wxSize sz( m_splitter->GetSize() );
171 wxLogMessage( "Initial splitter size: %d %d\n", (int)sz.x, (int)sz.y );
172
173 m_leftCanvas = new MyCanvas(m_splitter, CANVAS1, wxPoint(0, 0), wxSize(400, 400), "Test1" );
174 m_leftCanvas->SetBackgroundColour(*wxRED);
175 m_leftCanvas->SetScrollbars(20, 20, 50, 50);
176 m_leftCanvas->SetCursor(wxCursor(wxCURSOR_MAGNIFIER));
177
178 m_rightCanvas = new MyCanvas(m_splitter, CANVAS2, wxPoint(0, 0), wxSize(400, 400), "Test2" );
179 m_rightCanvas->SetBackgroundColour(*wxCYAN);
180 m_rightCanvas->SetScrollbars(20, 20, 50, 50);
181 m_rightCanvas->Show(FALSE);
182
183 m_splitter->Initialize(m_leftCanvas);
184 SetStatusText("Min pane size = 0", 1);
185 }
186
187 MyFrame::~MyFrame()
188 {
189 }
190
191 void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
192 {
193 Close(TRUE);
194 }
195
196 void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) )
197 {
198 if ( m_splitter->IsSplit() )
199 m_splitter->Unsplit();
200 m_leftCanvas->Show(TRUE);
201 m_rightCanvas->Show(TRUE);
202 m_splitter->SplitHorizontally( m_leftCanvas, m_rightCanvas );
203 UpdatePosition();
204 }
205
206 void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
207 {
208 if ( m_splitter->IsSplit() )
209 m_splitter->Unsplit();
210 m_leftCanvas->Show(TRUE);
211 m_rightCanvas->Show(TRUE);
212 m_splitter->SplitVertically( m_leftCanvas, m_rightCanvas );
213 UpdatePosition();
214 }
215
216 void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
217 {
218 if ( m_splitter->IsSplit() )
219 m_splitter->Unsplit();
220 SetStatusText("No splitter");
221 }
222
223 void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
224 {
225 wxString str;
226 str.Printf( _T("%d"), m_splitter->GetMinimumPaneSize());
227 str = wxGetTextFromUser("Enter minimal size for panes:", "", str, this);
228 if ( str.IsEmpty() )
229 return;
230
231 int minsize = wxStrtol( str, (wxChar**)NULL, 10 );
232 m_splitter->SetMinimumPaneSize(minsize);
233 str.Printf( _T("Min pane size = %d"), minsize);
234 SetStatusText(str, 1);
235 }
236
237 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
238 {
239 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) ) );
240 }
241
242 void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
243 {
244 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
245 }
246
247 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
248 {
249 event.Enable( m_splitter->IsSplit() );
250 }
251
252 void MyFrame::UpdatePosition()
253 {
254 wxString str;
255 str.Printf( _("Sash position = %d"), m_splitter->GetSashPosition());
256 SetStatusText(str);
257 }
258
259 MyCanvas::MyCanvas(wxWindow* parent, wxWindowID id, const wxPoint& point, const wxSize& size, const wxString &name ) :
260 wxScrolledWindow(parent, id, point, size, 0, name )
261 {
262 }
263
264 MyCanvas::~MyCanvas()
265 {
266 }
267
268 void MyCanvas::OnDraw(wxDC& dc)
269 {
270 dc.SetPen(*wxBLACK_PEN);
271 dc.DrawLine(0, 0, 100, 100);
272
273 dc.SetBackgroundMode(wxTRANSPARENT);
274 dc.DrawText("Testing", 50, 50);
275
276 dc.SetPen(*wxRED_PEN);
277 dc.SetBrush(*wxGREEN_BRUSH);
278 dc.DrawRectangle(120, 120, 100, 80);
279 }