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