]> git.saurik.com Git - wxWidgets.git/blame - samples/splitter/splitter.cpp
don't try to refresh the menu bar before showing it, fixes crash on startup with...
[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$
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:
4a0b46a7 38 MySplitterWindow(wxFrame *parent, wxWindowID id)
fe31f91c
VZ
39 : wxSplitterWindow(parent, id, wxDefaultPosition, wxDefaultSize,
40 wxSP_3D | wxSP_LIVE_UPDATE | wxCLIP_CHILDREN)
0d559d69
VZ
41 {
42 m_frame = parent;
43 }
44
45 virtual bool OnSashPositionChange(int newSashPosition)
46 {
47 if ( !wxSplitterWindow::OnSashPositionChange(newSashPosition) )
48 return FALSE;
4a0b46a7 49
0d559d69 50 wxString str;
1e133b7d 51 str.Printf( _T("Sash position = %d"), newSashPosition);
0d559d69
VZ
52 m_frame->SetStatusText(str);
53
54 return TRUE;
55 }
4a0b46a7 56
0d559d69
VZ
57private:
58 wxFrame *m_frame;
59};
c801d85f 60
0d559d69
VZ
61class MyFrame: public wxFrame
62{
63public:
64 MyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size);
65 virtual ~MyFrame();
c801d85f 66
0d559d69
VZ
67 // Menu commands
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);
c801d85f 73
0d559d69
VZ
74 // Menu command update functions
75 void UpdateUIHorizontal(wxUpdateUIEvent& event);
76 void UpdateUIVertical(wxUpdateUIEvent& event);
77 void UpdateUIUnsplit(wxUpdateUIEvent& event);
c801d85f
KB
78
79private:
0d559d69
VZ
80 void UpdatePosition();
81
6b55490a
VZ
82 wxWindow *m_left,
83 *m_right;
84
0d559d69 85 MySplitterWindow* m_splitter;
c801d85f 86
6b55490a 87 DECLARE_EVENT_TABLE()
c801d85f
KB
88};
89
90class MyCanvas: public wxScrolledWindow
91{
92public:
33611ebb
RR
93 MyCanvas(wxWindow* parent, wxWindowID id = -1, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, const wxString& name = "");
94 virtual ~MyCanvas();
c801d85f 95
0d559d69 96 virtual void OnDraw(wxDC& dc);
c801d85f
KB
97
98DECLARE_EVENT_TABLE()
99};
100
101BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
102END_EVENT_TABLE()
103
0d559d69
VZ
104// ID for the menu commands
105enum
106{
107 SPLIT_QUIT,
108 SPLIT_HORIZONTAL,
109 SPLIT_VERTICAL,
110 SPLIT_UNSPLIT,
111 SPLIT_SETMINSIZE
112};
c801d85f 113
0d57be45
JS
114// Window ids
115#define SPLITTER_WINDOW 100
116#define SPLITTER_FRAME 101
117#define CANVAS1 102
118#define CANVAS2 103
119
c801d85f
KB
120IMPLEMENT_APP(MyApp)
121
122bool MyApp::OnInit(void)
123{
0d559d69
VZ
124 MyFrame* frame = new MyFrame((wxFrame *) NULL, "wxSplitterWindow Example",
125 wxPoint(50, 50), wxSize(420, 300));
c801d85f 126
0d559d69
VZ
127 // Show the frame
128 frame->Show(TRUE);
4a0b46a7 129
0d559d69 130 SetTopWindow(frame);
c801d85f 131
0d559d69 132 return TRUE;
c801d85f
KB
133}
134
135BEGIN_EVENT_TABLE(MyFrame, wxFrame)
0d559d69
VZ
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)
c801d85f
KB
145END_EVENT_TABLE()
146
147// My frame constructor
fe31f91c
VZ
148MyFrame::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)
c801d85f 152{
0d559d69 153 CreateStatusBar(2);
b7346a70 154
0d559d69 155 // Make a menubar
6b55490a 156 wxMenu *fileMenu = new wxMenu;
8b8bff20
VZ
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");
0d559d69
VZ
160 fileMenu->AppendSeparator();
161 fileMenu->Append(SPLIT_SETMINSIZE, "Set &min size", "Set minimum pane size");
162 fileMenu->AppendSeparator();
8b8bff20 163 fileMenu->Append(SPLIT_QUIT, "E&xit\tAlt-X", "Exit");
c801d85f 164
6b55490a 165 wxMenuBar *menuBar = new wxMenuBar;
0d559d69 166 menuBar->Append(fileMenu, "&File");
c801d85f 167
0d559d69 168 SetMenuBar(menuBar);
c801d85f 169
0d559d69 170 m_splitter = new MySplitterWindow(this, SPLITTER_WINDOW);
4a0b46a7 171
6b55490a
VZ
172#if 1
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));
177
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");
184#endif
c801d85f 185
4a0b46a7
VZ
186 // you can also do this to start with a single window
187#if 0
6b55490a
VZ
188 m_right->Show(FALSE);
189 m_splitter->Initialize(m_left);
4a0b46a7 190#else
6b55490a 191 m_splitter->SplitVertically(m_left, m_right, 100);
4a0b46a7
VZ
192#endif
193
0d559d69 194 SetStatusText("Min pane size = 0", 1);
c801d85f
KB
195}
196
197MyFrame::~MyFrame()
198{
199}
200
e3e65dac 201void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
c801d85f 202{
0d559d69 203 Close(TRUE);
c801d85f
KB
204}
205
e3e65dac 206void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) )
c801d85f 207{
0d559d69
VZ
208 if ( m_splitter->IsSplit() )
209 m_splitter->Unsplit();
6b55490a
VZ
210 m_left->Show(TRUE);
211 m_right->Show(TRUE);
212 m_splitter->SplitHorizontally( m_left, m_right );
0d559d69 213 UpdatePosition();
c801d85f
KB
214}
215
e3e65dac 216void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
c801d85f 217{
0d559d69
VZ
218 if ( m_splitter->IsSplit() )
219 m_splitter->Unsplit();
6b55490a
VZ
220 m_left->Show(TRUE);
221 m_right->Show(TRUE);
222 m_splitter->SplitVertically( m_left, m_right );
0d559d69 223 UpdatePosition();
c801d85f
KB
224}
225
e3e65dac 226void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
c801d85f 227{
0d559d69
VZ
228 if ( m_splitter->IsSplit() )
229 m_splitter->Unsplit();
230 SetStatusText("No splitter");
231}
232
233void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
234{
235 wxString str;
1e133b7d 236 str.Printf( _T("%d"), m_splitter->GetMinimumPaneSize());
0d559d69
VZ
237 str = wxGetTextFromUser("Enter minimal size for panes:", "", str, this);
238 if ( str.IsEmpty() )
239 return;
240
1b3667ab 241 int minsize = wxStrtol( str, (wxChar**)NULL, 10 );
0d559d69 242 m_splitter->SetMinimumPaneSize(minsize);
1e133b7d 243 str.Printf( _T("Min pane size = %d"), minsize);
0d559d69 244 SetStatusText(str, 1);
c801d85f
KB
245}
246
247void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
248{
0d559d69 249 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) ) );
c801d85f
KB
250}
251
252void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
253{
0d559d69 254 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
c801d85f
KB
255}
256
257void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
258{
0d559d69 259 event.Enable( m_splitter->IsSplit() );
c801d85f
KB
260}
261
0d559d69 262void MyFrame::UpdatePosition()
c801d85f 263{
0d559d69 264 wxString str;
1e133b7d 265 str.Printf( _("Sash position = %d"), m_splitter->GetSashPosition());
0d559d69 266 SetStatusText(str);
c801d85f
KB
267}
268
f6bcfd97 269MyCanvas::MyCanvas(wxWindow* parent, wxWindowID id, const wxPoint& point, const wxSize& size, const wxString &name ) :
33611ebb 270 wxScrolledWindow(parent, id, point, size, 0, name )
c801d85f
KB
271{
272}
273
274MyCanvas::~MyCanvas()
275{
276}
277
278void MyCanvas::OnDraw(wxDC& dc)
279{
0d559d69
VZ
280 dc.SetPen(*wxBLACK_PEN);
281 dc.DrawLine(0, 0, 100, 100);
c801d85f 282
0d559d69
VZ
283 dc.SetBackgroundMode(wxTRANSPARENT);
284 dc.DrawText("Testing", 50, 50);
b7346a70 285
0d559d69
VZ
286 dc.SetPen(*wxRED_PEN);
287 dc.SetBrush(*wxGREEN_BRUSH);
288 dc.DrawRectangle(120, 120, 100, 80);
c801d85f 289}