]> git.saurik.com Git - wxWidgets.git/blob - samples/splitter/test.cpp
Added dynamic events sample.
[wxWidgets.git] / samples / splitter / test.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 MyFrame: public wxFrame
36 {
37 public:
38 MyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size);
39 virtual ~MyFrame();
40
41 bool OnClose();
42
43 // Menu commands
44 void SplitHorizontal(wxCommandEvent& event);
45 void SplitVertical(wxCommandEvent& event);
46 void Unsplit(wxCommandEvent& event);
47 void Quit(wxCommandEvent& event);
48
49 // Menu command update functions
50 void UpdateUIHorizontal(wxUpdateUIEvent& event);
51 void UpdateUIVertical(wxUpdateUIEvent& event);
52 void UpdateUIUnsplit(wxUpdateUIEvent& event);
53
54 void OnIdle(wxIdleEvent& event);
55
56 private:
57 wxMenu* fileMenu;
58 wxMenuBar* menuBar;
59 MyCanvas* leftCanvas;
60 MyCanvas* rightCanvas;
61 wxSplitterWindow* splitter;
62
63 DECLARE_EVENT_TABLE()
64 };
65
66 class MyCanvas: public wxScrolledWindow
67 {
68 public:
69 MyCanvas(wxWindow* parent, int x, int y, int w, int h);
70 virtual ~MyCanvas();
71
72 virtual void OnDraw(wxDC& dc);
73
74 DECLARE_EVENT_TABLE()
75 };
76
77 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
78 END_EVENT_TABLE()
79
80 // ID for the menu quit command
81 #define SPLIT_QUIT 1
82 #define SPLIT_HORIZONTAL 2
83 #define SPLIT_VERTICAL 3
84 #define SPLIT_UNSPLIT 4
85
86 IMPLEMENT_APP(MyApp)
87
88 bool MyApp::OnInit(void)
89 {
90 MyFrame* frame = new MyFrame(NULL, "wxSplitterWindow Example", wxPoint(50, 50), wxSize(400, 300));
91
92 // Show the frame
93 frame->Show(TRUE);
94
95 SetTopWindow(frame);
96
97 return TRUE;
98 }
99
100 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
101 EVT_MENU(SPLIT_VERTICAL, MyFrame::SplitVertical)
102 EVT_MENU(SPLIT_HORIZONTAL, MyFrame::SplitHorizontal)
103 EVT_MENU(SPLIT_UNSPLIT, MyFrame::Unsplit)
104 EVT_MENU(SPLIT_QUIT, MyFrame::Quit)
105 EVT_UPDATE_UI(SPLIT_VERTICAL, MyFrame::UpdateUIVertical)
106 EVT_UPDATE_UI(SPLIT_HORIZONTAL, MyFrame::UpdateUIHorizontal)
107 EVT_UPDATE_UI(SPLIT_UNSPLIT, MyFrame::UpdateUIUnsplit)
108 EVT_IDLE(MyFrame::OnIdle)
109 END_EVENT_TABLE()
110
111 // My frame constructor
112 MyFrame::MyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size):
113 wxFrame(frame, -1, title, pos, size)
114 {
115 // set the icon
116 #ifdef __WINDOWS__
117 SetIcon(wxIcon("mondrian"));
118 #endif
119 #ifdef __X__
120 SetIcon(wxIcon("aiai.xbm"));
121 #endif
122
123 // Make a menubar
124 fileMenu = new wxMenu;
125 fileMenu->Append(SPLIT_VERTICAL, "Split &Vertically", "Split vertically");
126 fileMenu->Append(SPLIT_HORIZONTAL, "Split &Horizontally", "Split horizontally");
127 fileMenu->Append(SPLIT_UNSPLIT, "&Unsplit", "Unsplit");
128 fileMenu->Append(SPLIT_QUIT, "E&xit", "Exit");
129
130 menuBar = new wxMenuBar;
131 menuBar->Append(fileMenu, "&File");
132
133 SetMenuBar(menuBar);
134
135 splitter = new wxSplitterWindow(this, -1, wxPoint(0, 0), wxSize(400, 400),
136 // wxSP_BORDER);
137 wxSP_3D);
138 // wxSP_NOBORDER);
139
140 leftCanvas = new MyCanvas(splitter, 0, 0, 400, 400);
141 leftCanvas->SetBackgroundColour(*wxRED);
142 leftCanvas->SetScrollbars(20, 20, 50, 50);
143
144 rightCanvas = new MyCanvas(splitter, 0, 0, 400, 400);
145 rightCanvas->SetBackgroundColour(*wxCYAN);
146 rightCanvas->SetScrollbars(20, 20, 50, 50);
147 rightCanvas->Show(FALSE);
148
149 splitter->Initialize(leftCanvas);
150
151 // Set this to prevent unsplitting
152 // splitter->SetMinimumPaneSize(20);
153 CreateStatusBar();
154 }
155
156 MyFrame::~MyFrame()
157 {
158 }
159
160 bool MyFrame::OnClose()
161 {
162 return TRUE;
163 }
164
165 void MyFrame::Quit(wxCommandEvent& event)
166 {
167 Close(TRUE);
168 }
169
170 void MyFrame::SplitHorizontal(wxCommandEvent& event)
171 {
172 if ( splitter->IsSplit() )
173 splitter->Unsplit();
174 leftCanvas->Show(TRUE);
175 rightCanvas->Show(TRUE);
176 splitter->SplitHorizontally( leftCanvas, rightCanvas );
177 }
178
179 void MyFrame::SplitVertical(wxCommandEvent& event)
180 {
181 if ( splitter->IsSplit() )
182 splitter->Unsplit();
183 leftCanvas->Show(TRUE);
184 rightCanvas->Show(TRUE);
185 splitter->SplitVertically( leftCanvas, rightCanvas );
186 }
187
188 void MyFrame::Unsplit(wxCommandEvent& event)
189 {
190 if ( splitter->IsSplit() )
191 splitter->Unsplit();
192 }
193
194 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
195 {
196 event.Enable( ( (!splitter->IsSplit()) || (splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) ) );
197 }
198
199 void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
200 {
201 event.Enable( ( (!splitter->IsSplit()) || (splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
202 }
203
204 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
205 {
206 event.Enable( splitter->IsSplit() );
207 }
208
209 void MyFrame::OnIdle(wxIdleEvent& event)
210 {
211 if ( GetStatusBar()->GetStatusText(0) != "Ready" )
212 SetStatusText("Ready");
213 }
214
215 MyCanvas::MyCanvas(wxWindow* parent, int x, int y, int w, int h) :
216 wxScrolledWindow(parent, -1, wxPoint(x, y), wxSize(w, h))
217 {
218 }
219
220 MyCanvas::~MyCanvas()
221 {
222 }
223
224 void MyCanvas::OnDraw(wxDC& dc)
225 {
226 dc.DrawLine(0, 0, 100, 100);
227
228 dc.SetBackgroundMode(wxTRANSPARENT);
229 dc.DrawText("Testing", 50, 50);
230 }