]>
Commit | Line | Data |
---|---|---|
ecab4dba RR |
1 | /* |
2 | * Program: scrollsub | |
3 | * | |
4 | * Author: Robert Roebling | |
5 | * | |
6 | * Copyright: (C) 1999, Robert Roebling | |
7 | * | |
8 | */ | |
9 | ||
10 | // For compilers that support precompilation, includes "wx/wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #ifdef __BORLANDC__ | |
14 | #pragma hdrstop | |
15 | #endif | |
16 | ||
17 | #ifndef WX_PRECOMP | |
18 | #include "wx/wx.h" | |
19 | #endif | |
20 | ||
21 | #include "wx/image.h" | |
22 | #include "wx/listctrl.h" | |
23 | #include "wx/sizer.h" | |
24 | #include "wx/log.h" | |
25 | ||
26 | ||
27 | // derived classes | |
28 | ||
29 | class MyFrame; | |
30 | class MyScrolledWindow; | |
31 | class MyCanvas; | |
32 | class MyApp; | |
33 | ||
34 | // MyScrolledWindow | |
35 | ||
36 | class MyScrolledWindow: public wxScrolledWindow | |
37 | { | |
38 | public: | |
925e9792 | 39 | MyScrolledWindow(){}; |
ecab4dba | 40 | MyScrolledWindow( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size ); |
925e9792 | 41 | ~MyScrolledWindow(){}; |
ecab4dba | 42 | void OnPaint( wxPaintEvent &event ); |
c376d80f | 43 | void OnSize( wxSizeEvent &event ); |
ecab4dba RR |
44 | private: |
45 | MyCanvas *m_canvas; | |
46 | ||
47 | DECLARE_DYNAMIC_CLASS(MyScrolledWindow) | |
48 | DECLARE_EVENT_TABLE() | |
49 | }; | |
50 | ||
dfd6b52f RR |
51 | // MyTopLabels |
52 | ||
53 | class MyTopLabels: public wxWindow | |
54 | { | |
55 | public: | |
56 | MyTopLabels() {} | |
57 | MyTopLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size ); | |
5f3286d1 | 58 | |
dfd6b52f | 59 | void OnPaint( wxPaintEvent &event ); |
5f3286d1 | 60 | |
dfd6b52f RR |
61 | private: |
62 | wxScrolledWindow *m_owner; | |
5f3286d1 | 63 | |
dfd6b52f RR |
64 | DECLARE_DYNAMIC_CLASS(MyTopLabels) |
65 | DECLARE_EVENT_TABLE() | |
66 | }; | |
67 | ||
68 | // MyRightLabels | |
69 | ||
70 | class MyRightLabels: public wxWindow | |
71 | { | |
72 | public: | |
73 | MyRightLabels() {} | |
74 | MyRightLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size ); | |
5f3286d1 | 75 | |
dfd6b52f | 76 | void OnPaint( wxPaintEvent &event ); |
5f3286d1 | 77 | |
dfd6b52f RR |
78 | private: |
79 | wxScrolledWindow *m_owner; | |
5f3286d1 | 80 | |
dfd6b52f RR |
81 | DECLARE_DYNAMIC_CLASS(MyRightLabels) |
82 | DECLARE_EVENT_TABLE() | |
83 | }; | |
84 | ||
ecab4dba RR |
85 | // MyCanvas |
86 | ||
87 | class MyCanvas: public wxPanel | |
88 | { | |
89 | public: | |
925e9792 | 90 | MyCanvas(){}; |
dfd6b52f RR |
91 | MyCanvas( wxScrolledWindow *parent, MyTopLabels *top, MyRightLabels *right, |
92 | wxWindowID id, const wxPoint &pos, const wxSize &size ); | |
925e9792 | 93 | ~MyCanvas(){}; |
ecab4dba | 94 | void OnPaint( wxPaintEvent &event ); |
dfd6b52f | 95 | void ScrollWindow( int dx, int dy, const wxRect *rect ); |
ecab4dba RR |
96 | |
97 | private: | |
98 | wxScrolledWindow *m_owner; | |
dfd6b52f RR |
99 | MyTopLabels *m_topLabels; |
100 | MyRightLabels *m_rightLabels; | |
ecab4dba RR |
101 | |
102 | DECLARE_DYNAMIC_CLASS(MyCanvas) | |
103 | DECLARE_EVENT_TABLE() | |
104 | }; | |
105 | ||
106 | // MyFrame | |
107 | ||
108 | class MyFrame: public wxFrame | |
109 | { | |
110 | public: | |
111 | MyFrame(); | |
112 | ||
113 | void OnAbout( wxCommandEvent &event ); | |
114 | void OnQuit( wxCommandEvent &event ); | |
3d0c4d2e | 115 | void OnFullScreen( wxCommandEvent &event ); |
ecab4dba RR |
116 | |
117 | wxScrolledWindow *m_scrolled; | |
b29903d4 | 118 | #if wxUSE_LOG |
ecab4dba | 119 | wxTextCtrl *m_log; |
b29903d4 | 120 | #endif // wxUSE_LOG |
ecab4dba RR |
121 | |
122 | private: | |
123 | DECLARE_DYNAMIC_CLASS(MyFrame) | |
124 | DECLARE_EVENT_TABLE() | |
125 | }; | |
126 | ||
127 | // MyApp | |
128 | ||
129 | class MyApp: public wxApp | |
130 | { | |
131 | public: | |
132 | virtual bool OnInit(); | |
133 | }; | |
134 | ||
135 | // main program | |
136 | ||
137 | IMPLEMENT_APP(MyApp) | |
138 | ||
139 | // MyScrolledWindow | |
140 | ||
141 | IMPLEMENT_DYNAMIC_CLASS(MyScrolledWindow, wxScrolledWindow) | |
142 | ||
143 | BEGIN_EVENT_TABLE(MyScrolledWindow, wxScrolledWindow) | |
c376d80f RR |
144 | EVT_PAINT( MyScrolledWindow::OnPaint) |
145 | EVT_SIZE( MyScrolledWindow::OnSize) | |
ecab4dba RR |
146 | END_EVENT_TABLE() |
147 | ||
148 | MyScrolledWindow::MyScrolledWindow( wxWindow *parent, wxWindowID id, | |
149 | const wxPoint &pos, const wxSize &size ) | |
600683ca | 150 | : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER, _T("test canvas") ) |
ecab4dba | 151 | { |
422d0ff0 WS |
152 | MyTopLabels *top = new MyTopLabels( this, wxID_ANY, wxDefaultPosition, wxSize(wxDefaultCoord,25) ); |
153 | MyRightLabels *right = new MyRightLabels( this, wxID_ANY, wxDefaultPosition, wxSize(60,wxDefaultCoord) ); | |
dfd6b52f | 154 | |
b62ca03d | 155 | m_canvas = new MyCanvas( this, top, right, wxID_ANY, wxDefaultPosition, wxDefaultSize ); |
5f3286d1 | 156 | |
ecab4dba RR |
157 | SetTargetWindow( m_canvas ); |
158 | ||
a60b1f5d | 159 | SetBackgroundColour( wxT("WHEAT") ); |
5f3286d1 | 160 | |
ecab4dba | 161 | SetCursor( wxCursor( wxCURSOR_HAND ) ); |
5f3286d1 | 162 | |
dfd6b52f | 163 | wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL ); |
5f3286d1 | 164 | |
dfd6b52f RR |
165 | wxBoxSizer *topsizer = new wxBoxSizer( wxHORIZONTAL ); |
166 | topsizer->Add( 60,25 ); | |
5f3286d1 VZ |
167 | topsizer->Add( top, 1, wxEXPAND ); |
168 | ||
dfd6b52f | 169 | mainsizer->Add( topsizer, 0, wxEXPAND ); |
5f3286d1 | 170 | |
dfd6b52f RR |
171 | wxBoxSizer *middlesizer = new wxBoxSizer( wxHORIZONTAL ); |
172 | middlesizer->Add( right, 0, wxEXPAND ); | |
173 | middlesizer->Add( m_canvas, 1, wxEXPAND ); | |
5f3286d1 | 174 | |
dfd6b52f | 175 | mainsizer->Add( middlesizer, 1, wxEXPAND ); |
ecab4dba | 176 | |
dfd6b52f | 177 | SetSizer( mainsizer ); |
ecab4dba RR |
178 | } |
179 | ||
c376d80f RR |
180 | void MyScrolledWindow::OnSize( wxSizeEvent &WXUNUSED(event) ) |
181 | { | |
182 | // We need to override OnSize so that our scrolled | |
183 | // window a) does call Layout() to use sizers for | |
184 | // positioning the controls but b) does not query | |
185 | // the sizer for their size and use that for setting | |
186 | // the scrollable area as set that ourselves by | |
187 | // calling SetScrollbar() further down. | |
188 | ||
189 | Layout(); | |
190 | ||
191 | AdjustScrollbars(); | |
192 | } | |
193 | ||
ecab4dba RR |
194 | void MyScrolledWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
195 | { | |
196 | wxPaintDC dc( this ); | |
197 | ||
dfd6b52f | 198 | /* |
ecab4dba | 199 | wxSize size( GetClientSize() ); |
5f3286d1 | 200 | |
ecab4dba RR |
201 | long w,h; |
202 | dc.GetTextExtent( wxT("Headline"), &w, &h ); | |
203 | ||
204 | dc.DrawText( wxT("Headline"), long (size.x / 2 - w / 2), 10 ); | |
dfd6b52f RR |
205 | */ |
206 | } | |
207 | ||
208 | // MyTopLabels | |
209 | ||
210 | IMPLEMENT_DYNAMIC_CLASS(MyTopLabels,wxWindow) | |
211 | ||
212 | BEGIN_EVENT_TABLE(MyTopLabels, wxWindow) | |
213 | EVT_PAINT( MyTopLabels::OnPaint) | |
214 | END_EVENT_TABLE() | |
215 | ||
216 | MyTopLabels::MyTopLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size ) | |
217 | : wxWindow( parent, id, pos, size ) | |
218 | { | |
219 | m_owner = parent; | |
220 | } | |
221 | ||
4547faae | 222 | void MyTopLabels::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
dfd6b52f RR |
223 | { |
224 | wxPaintDC dc(this); | |
566d84a7 RL |
225 | |
226 | // This is wrong.. it will translate both x and y if the | |
227 | // window is scrolled, the label windows are active in one | |
228 | // direction only. Do the action below instead -- RL. | |
229 | //m_owner->PrepareDC( dc ); | |
230 | ||
231 | int xScrollUnits, xOrigin; | |
232 | ||
233 | m_owner->GetViewStart( &xOrigin, 0 ); | |
234 | m_owner->GetScrollPixelsPerUnit( &xScrollUnits, 0 ); | |
235 | dc.SetDeviceOrigin( -xOrigin * xScrollUnits, 0 ); | |
236 | ||
600683ca MB |
237 | dc.DrawText( _T("Column 1"), 5, 5 ); |
238 | dc.DrawText( _T("Column 2"), 105, 5 ); | |
239 | dc.DrawText( _T("Column 3"), 205, 5 ); | |
dfd6b52f RR |
240 | } |
241 | ||
242 | // MyRightLabels | |
243 | ||
244 | IMPLEMENT_DYNAMIC_CLASS(MyRightLabels,wxWindow) | |
245 | ||
246 | BEGIN_EVENT_TABLE(MyRightLabels, wxWindow) | |
247 | EVT_PAINT( MyRightLabels::OnPaint) | |
248 | END_EVENT_TABLE() | |
249 | ||
250 | MyRightLabels::MyRightLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size ) | |
251 | : wxWindow( parent, id, pos, size ) | |
252 | { | |
253 | m_owner = parent; | |
254 | } | |
255 | ||
4547faae | 256 | void MyRightLabels::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
dfd6b52f RR |
257 | { |
258 | wxPaintDC dc(this); | |
566d84a7 RL |
259 | |
260 | // This is wrong.. it will translate both x and y if the | |
261 | // window is scrolled, the label windows are active in one | |
262 | // direction only. Do the action below instead -- RL. | |
263 | //m_owner->PrepareDC( dc ); | |
264 | ||
265 | int yScrollUnits, yOrigin; | |
266 | ||
267 | m_owner->GetViewStart( 0, &yOrigin ); | |
268 | m_owner->GetScrollPixelsPerUnit( 0, &yScrollUnits ); | |
269 | dc.SetDeviceOrigin( 0, -yOrigin * yScrollUnits ); | |
270 | ||
600683ca MB |
271 | dc.DrawText( _T("Row 1"), 5, 5 ); |
272 | dc.DrawText( _T("Row 2"), 5, 30 ); | |
273 | dc.DrawText( _T("Row 3"), 5, 55 ); | |
274 | dc.DrawText( _T("Row 4"), 5, 80 ); | |
275 | dc.DrawText( _T("Row 5"), 5, 105 ); | |
276 | dc.DrawText( _T("Row 6"), 5, 130 ); | |
ecab4dba RR |
277 | } |
278 | ||
279 | // MyCanvas | |
280 | ||
281 | IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxPanel) | |
282 | ||
283 | BEGIN_EVENT_TABLE(MyCanvas, wxPanel) | |
284 | EVT_PAINT( MyCanvas::OnPaint) | |
285 | END_EVENT_TABLE() | |
286 | ||
dfd6b52f RR |
287 | MyCanvas::MyCanvas( wxScrolledWindow *parent, MyTopLabels *top, MyRightLabels *right, |
288 | wxWindowID id, const wxPoint &pos, const wxSize &size ) | |
600683ca | 289 | : wxPanel( parent, id, pos, size, wxSUNKEN_BORDER, _T("test canvas") ) |
ecab4dba RR |
290 | { |
291 | m_owner = parent; | |
dfd6b52f RR |
292 | m_topLabels = top; |
293 | m_rightLabels = right; | |
5f3286d1 | 294 | |
b62ca03d WS |
295 | (void)new wxButton( this, wxID_ANY, _T("Hallo I"), wxPoint(0,50), wxSize(100,25) ); |
296 | (void)new wxButton( this, wxID_ANY, _T("Hallo II"), wxPoint(200,50), wxSize(100,25) ); | |
5f3286d1 | 297 | |
b62ca03d WS |
298 | (void)new wxTextCtrl( this, wxID_ANY, _T("Text I"), wxPoint(0,100), wxSize(100,25) ); |
299 | (void)new wxTextCtrl( this, wxID_ANY, _T("Text II"), wxPoint(200,100), wxSize(100,25) ); | |
ecab4dba | 300 | |
b62ca03d WS |
301 | (void)new wxComboBox( this, wxID_ANY, _T("ComboBox I"), wxPoint(0,150), wxSize(100,25)); |
302 | (void)new wxComboBox( this, wxID_ANY, _T("ComboBox II"), wxPoint(200,150), wxSize(100,25)); | |
2a0e49a4 | 303 | |
a60b1f5d | 304 | SetBackgroundColour( wxT("WHEAT") ); |
5f3286d1 | 305 | |
ecab4dba RR |
306 | SetCursor( wxCursor( wxCURSOR_IBEAM ) ); |
307 | } | |
308 | ||
ecab4dba RR |
309 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
310 | { | |
311 | wxPaintDC dc( this ); | |
312 | m_owner->PrepareDC( dc ); | |
313 | ||
76db86e7 | 314 | dc.SetPen( *wxBLACK_PEN ); |
5f3286d1 | 315 | |
17800993 RR |
316 | // OK, let's assume we are a grid control and we have two |
317 | // grid cells. Here in OnPaint we want to know which cell | |
318 | // to redraw so that we prevent redrawing cells that don't | |
319 | // need to get redrawn. We have one cell at (0,0) and one | |
320 | // more at (200,0), both having a size of (100,25). | |
5f3286d1 VZ |
321 | |
322 | // We can query how much the window has been scrolled | |
17800993 | 323 | // by calling CalcUnscrolledPosition() |
5f3286d1 | 324 | |
17800993 RR |
325 | int scroll_x = 0; |
326 | int scroll_y = 0; | |
327 | m_owner->CalcUnscrolledPosition( scroll_x, scroll_y, &scroll_x, &scroll_y ); | |
5f3286d1 | 328 | |
17800993 RR |
329 | // We also need to know the size of the window to see which |
330 | // cells are completely hidden and not get redrawn | |
5f3286d1 | 331 | |
17800993 RR |
332 | int size_x = 0; |
333 | int size_y = 0; | |
334 | GetClientSize( &size_x, &size_y ); | |
5f3286d1 | 335 | |
17800993 RR |
336 | // First cell: (0,0)(100,25) |
337 | // It it on screen? | |
338 | if ((0+100-scroll_x > 0) && (0+25-scroll_y > 0) && | |
339 | (0-scroll_x < size_x) && (0-scroll_y < size_y)) | |
340 | { | |
dfd6b52f | 341 | // Has the region on screen been exposed? |
2f6c54eb VZ |
342 | if (IsExposed(0,0,100,25)) |
343 | { | |
344 | wxLogMessage( wxT("Redraw first cell") ); | |
17800993 | 345 | dc.DrawRectangle( 0, 0, 100, 25 ); |
600683ca | 346 | dc.DrawText( _T("First Cell"), 5, 5 ); |
2f6c54eb | 347 | } |
17800993 | 348 | } |
5f3286d1 VZ |
349 | |
350 | ||
17800993 RR |
351 | // Second cell: (0,200)(100,25) |
352 | // It it on screen? | |
353 | if ((200+100-scroll_x > 0) && (0+25-scroll_y > 0) && | |
354 | (200-scroll_x < size_x) && (0-scroll_y < size_y)) | |
355 | { | |
dfd6b52f | 356 | // Has the region on screen been exposed? |
2f6c54eb VZ |
357 | if (IsExposed(200,0,100,25)) |
358 | { | |
359 | wxLogMessage( wxT("Redraw second cell") ); | |
17800993 | 360 | dc.DrawRectangle( 200, 0, 100, 25 ); |
600683ca | 361 | dc.DrawText( _T("Second Cell"), 205, 5 ); |
2f6c54eb | 362 | } |
17800993 | 363 | } |
5f3286d1 | 364 | |
ecab4dba RR |
365 | } |
366 | ||
dfd6b52f RR |
367 | void MyCanvas::ScrollWindow( int dx, int dy, const wxRect *rect ) |
368 | { | |
369 | wxPanel::ScrollWindow( dx, dy, rect ); | |
370 | m_topLabels->ScrollWindow( dx, 0, rect ); | |
371 | m_rightLabels->ScrollWindow( 0, dy, rect ); | |
372 | } | |
ecab4dba RR |
373 | |
374 | // MyFrame | |
375 | ||
91b07357 | 376 | const int ID_QUIT = wxID_EXIT; |
3d0c4d2e | 377 | const int ID_FULL = 109; |
91b07357 | 378 | const int ID_ABOUT = wxID_ABOUT; |
ecab4dba RR |
379 | |
380 | IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) | |
381 | ||
382 | BEGIN_EVENT_TABLE(MyFrame,wxFrame) | |
383 | EVT_MENU (ID_ABOUT, MyFrame::OnAbout) | |
384 | EVT_MENU (ID_QUIT, MyFrame::OnQuit) | |
3d0c4d2e | 385 | EVT_MENU (ID_FULL, MyFrame::OnFullScreen) |
ecab4dba RR |
386 | END_EVENT_TABLE() |
387 | ||
388 | MyFrame::MyFrame() | |
b62ca03d | 389 | : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxScrolledWindow sample"), |
ecab4dba RR |
390 | wxPoint(20,20), wxSize(470,500) ) |
391 | { | |
392 | wxMenu *file_menu = new wxMenu(); | |
600683ca MB |
393 | file_menu->Append( ID_ABOUT, _T("&About...")); |
394 | file_menu->Append( ID_FULL, _T("&Full screen on/off")); | |
395 | file_menu->Append( ID_QUIT, _T("E&xit\tAlt-X")); | |
ecab4dba RR |
396 | |
397 | wxMenuBar *menu_bar = new wxMenuBar(); | |
600683ca | 398 | menu_bar->Append(file_menu, _T("&File")); |
ecab4dba RR |
399 | |
400 | SetMenuBar( menu_bar ); | |
401 | ||
8520f137 | 402 | #if wxUSE_STATUSBAR |
ecab4dba RR |
403 | CreateStatusBar(2); |
404 | int widths[] = { -1, 100 }; | |
405 | SetStatusWidths( 2, widths ); | |
8520f137 | 406 | #endif // wxUSE_STATUSBAR |
ecab4dba | 407 | |
b62ca03d | 408 | m_scrolled = new MyScrolledWindow( this, wxID_ANY, wxDefaultPosition, wxSize(100,100) ); |
4547faae | 409 | m_scrolled->SetScrollbars( 10, 10, 50, 50 ); |
5f3286d1 | 410 | |
b29903d4 | 411 | #if wxUSE_LOG |
b62ca03d | 412 | m_log = new wxTextCtrl( this, wxID_ANY, _T("This is the log window.\n"), wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE ); |
ecab4dba RR |
413 | wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) ); |
414 | delete old_log; | |
b29903d4 | 415 | #endif // wxUSE_LOG |
5f3286d1 | 416 | |
ecab4dba | 417 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); |
ecab4dba | 418 | topsizer->Add( m_scrolled, 1, wxEXPAND ); |
b29903d4 | 419 | #if wxUSE_LOG |
ecab4dba | 420 | topsizer->Add( m_log, 0, wxEXPAND ); |
b29903d4 | 421 | #endif // wxUSE_LOG |
ecab4dba | 422 | |
ecab4dba RR |
423 | SetSizer( topsizer ); |
424 | } | |
425 | ||
426 | void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) | |
427 | { | |
b62ca03d | 428 | Close( true ); |
ecab4dba RR |
429 | } |
430 | ||
3d0c4d2e RR |
431 | void MyFrame::OnFullScreen( wxCommandEvent &WXUNUSED(event) ) |
432 | { | |
433 | ShowFullScreen( !IsFullScreen(), wxFULLSCREEN_NOBORDER|wxFULLSCREEN_NOCAPTION ); | |
434 | } | |
435 | ||
ecab4dba RR |
436 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) |
437 | { | |
600683ca MB |
438 | (void)wxMessageBox( _T("wxScroll demo II\n") |
439 | _T("Robert Roebling (c) 1998"), | |
440 | _T("About wxScroll II Demo"), wxICON_INFORMATION | wxOK ); | |
ecab4dba RR |
441 | } |
442 | ||
443 | //----------------------------------------------------------------------------- | |
444 | // MyApp | |
445 | //----------------------------------------------------------------------------- | |
446 | ||
447 | bool MyApp::OnInit() | |
448 | { | |
45e6e6f8 VZ |
449 | if ( !wxApp::OnInit() ) |
450 | return false; | |
451 | ||
ecab4dba | 452 | wxFrame *frame = new MyFrame(); |
b62ca03d | 453 | frame->Show( true ); |
ecab4dba | 454 | |
b62ca03d | 455 | return true; |
ecab4dba RR |
456 | } |
457 |