]>
git.saurik.com Git - wxWidgets.git/blob - samples/splitter/splitter.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSplitterWindow sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/splitter.h"
29 class MyApp
: public wxApp
35 class MySplitterWindow
: public wxSplitterWindow
38 MySplitterWindow(wxFrame
*parent
, wxWindowID id
)
39 : wxSplitterWindow(parent
, id
, wxDefaultPosition
, wxDefaultSize
, wxSP_3D
| wxSP_LIVE_UPDATE
)
44 virtual bool OnSashPositionChange(int newSashPosition
)
46 if ( !wxSplitterWindow::OnSashPositionChange(newSashPosition
) )
50 str
.Printf( _T("Sash position = %d"), newSashPosition
);
51 m_frame
->SetStatusText(str
);
60 class MyFrame
: public wxFrame
63 MyFrame(wxFrame
* frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
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
);
73 // Menu command update functions
74 void UpdateUIHorizontal(wxUpdateUIEvent
& event
);
75 void UpdateUIVertical(wxUpdateUIEvent
& event
);
76 void UpdateUIUnsplit(wxUpdateUIEvent
& event
);
79 void UpdatePosition();
83 MyCanvas
* m_leftCanvas
;
84 MyCanvas
* m_rightCanvas
;
85 MySplitterWindow
* m_splitter
;
90 class MyCanvas
: public wxScrolledWindow
93 MyCanvas(wxWindow
* parent
, wxWindowID id
= -1, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
, const wxString
& name
= "");
96 virtual void OnDraw(wxDC
& dc
);
101 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
104 // ID for the menu commands
115 #define SPLITTER_WINDOW 100
116 #define SPLITTER_FRAME 101
122 bool MyApp::OnInit(void)
124 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, "wxSplitterWindow Example",
125 wxPoint(50, 50), wxSize(420, 300));
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
)
142 EVT_UPDATE_UI(SPLIT_VERTICAL
, MyFrame::UpdateUIVertical
)
143 EVT_UPDATE_UI(SPLIT_HORIZONTAL
, MyFrame::UpdateUIHorizontal
)
144 EVT_UPDATE_UI(SPLIT_UNSPLIT
, MyFrame::UpdateUIUnsplit
)
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
)
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");
163 menuBar
= new wxMenuBar
;
164 menuBar
->Append(fileMenu
, "&File");
168 m_splitter
= new MySplitterWindow(this, SPLITTER_WINDOW
);
170 wxSize
sz( m_splitter
->GetSize() );
171 // wxLogMessage( "Initial splitter size: %d %d\n", (int)sz.x, (int)sz.y );
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
));
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);
183 m_splitter
->SplitVertically(m_leftCanvas
,m_rightCanvas
, 40 );
184 // m_splitter->Initialize(m_leftCanvas);
185 SetStatusText("Min pane size = 0", 1);
192 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
197 void MyFrame::SplitHorizontal(wxCommandEvent
& WXUNUSED(event
) )
199 if ( m_splitter
->IsSplit() )
200 m_splitter
->Unsplit();
201 m_leftCanvas
->Show(TRUE
);
202 m_rightCanvas
->Show(TRUE
);
203 m_splitter
->SplitHorizontally( m_leftCanvas
, m_rightCanvas
);
207 void MyFrame::SplitVertical(wxCommandEvent
& WXUNUSED(event
) )
209 if ( m_splitter
->IsSplit() )
210 m_splitter
->Unsplit();
211 m_leftCanvas
->Show(TRUE
);
212 m_rightCanvas
->Show(TRUE
);
213 m_splitter
->SplitVertically( m_leftCanvas
, m_rightCanvas
);
217 void MyFrame::Unsplit(wxCommandEvent
& WXUNUSED(event
) )
219 if ( m_splitter
->IsSplit() )
220 m_splitter
->Unsplit();
221 SetStatusText("No splitter");
224 void MyFrame::SetMinSize(wxCommandEvent
& WXUNUSED(event
) )
227 str
.Printf( _T("%d"), m_splitter
->GetMinimumPaneSize());
228 str
= wxGetTextFromUser("Enter minimal size for panes:", "", str
, this);
232 int minsize
= wxStrtol( str
, (wxChar
**)NULL
, 10 );
233 m_splitter
->SetMinimumPaneSize(minsize
);
234 str
.Printf( _T("Min pane size = %d"), minsize
);
235 SetStatusText(str
, 1);
238 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent
& event
)
240 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_HORIZONTAL
) ) );
243 void MyFrame::UpdateUIVertical(wxUpdateUIEvent
& event
)
245 event
.Enable( ( (!m_splitter
->IsSplit()) || (m_splitter
->GetSplitMode() != wxSPLIT_VERTICAL
) ) );
248 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent
& event
)
250 event
.Enable( m_splitter
->IsSplit() );
253 void MyFrame::UpdatePosition()
256 str
.Printf( _("Sash position = %d"), m_splitter
->GetSashPosition());
260 MyCanvas::MyCanvas(wxWindow
* parent
, wxWindowID id
, const wxPoint
& point
, const wxSize
& size
, const wxString
&name
) :
261 wxScrolledWindow(parent
, id
, point
, size
, 0, name
)
265 MyCanvas::~MyCanvas()
269 void MyCanvas::OnDraw(wxDC
& dc
)
271 dc
.SetPen(*wxBLACK_PEN
);
272 dc
.DrawLine(0, 0, 100, 100);
274 dc
.SetBackgroundMode(wxTRANSPARENT
);
275 dc
.DrawText("Testing", 50, 50);
277 dc
.SetPen(*wxRED_PEN
);
278 dc
.SetBrush(*wxGREEN_BRUSH
);
279 dc
.DrawRectangle(120, 120, 100, 80);