]>
Commit | Line | Data |
---|---|---|
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: | |
39 | MyScrolledWindow() {} | |
40 | MyScrolledWindow( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size ); | |
41 | ~MyScrolledWindow(); | |
42 | void OnPaint( wxPaintEvent &event ); | |
43 | ||
44 | private: | |
45 | MyCanvas *m_canvas; | |
46 | ||
47 | DECLARE_DYNAMIC_CLASS(MyScrolledWindow) | |
48 | DECLARE_EVENT_TABLE() | |
49 | }; | |
50 | ||
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 ); | |
58 | ||
59 | void OnPaint( wxPaintEvent &event ); | |
60 | ||
61 | private: | |
62 | wxScrolledWindow *m_owner; | |
63 | ||
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 ); | |
75 | ||
76 | void OnPaint( wxPaintEvent &event ); | |
77 | ||
78 | private: | |
79 | wxScrolledWindow *m_owner; | |
80 | ||
81 | DECLARE_DYNAMIC_CLASS(MyRightLabels) | |
82 | DECLARE_EVENT_TABLE() | |
83 | }; | |
84 | ||
85 | // MyCanvas | |
86 | ||
87 | class MyCanvas: public wxPanel | |
88 | { | |
89 | public: | |
90 | MyCanvas() {} | |
91 | MyCanvas( wxScrolledWindow *parent, MyTopLabels *top, MyRightLabels *right, | |
92 | wxWindowID id, const wxPoint &pos, const wxSize &size ); | |
93 | ~MyCanvas(); | |
94 | void OnPaint( wxPaintEvent &event ); | |
95 | void ScrollWindow( int dx, int dy, const wxRect *rect ); | |
96 | ||
97 | private: | |
98 | wxScrolledWindow *m_owner; | |
99 | MyTopLabels *m_topLabels; | |
100 | MyRightLabels *m_rightLabels; | |
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 ); | |
115 | void OnFullScreen( wxCommandEvent &event ); | |
116 | ||
117 | wxScrolledWindow *m_scrolled; | |
118 | wxTextCtrl *m_log; | |
119 | ||
120 | private: | |
121 | DECLARE_DYNAMIC_CLASS(MyFrame) | |
122 | DECLARE_EVENT_TABLE() | |
123 | }; | |
124 | ||
125 | // MyApp | |
126 | ||
127 | class MyApp: public wxApp | |
128 | { | |
129 | public: | |
130 | virtual bool OnInit(); | |
131 | }; | |
132 | ||
133 | // main program | |
134 | ||
135 | IMPLEMENT_APP(MyApp) | |
136 | ||
137 | // MyScrolledWindow | |
138 | ||
139 | IMPLEMENT_DYNAMIC_CLASS(MyScrolledWindow, wxScrolledWindow) | |
140 | ||
141 | BEGIN_EVENT_TABLE(MyScrolledWindow, wxScrolledWindow) | |
142 | EVT_PAINT( MyScrolledWindow::OnPaint) | |
143 | END_EVENT_TABLE() | |
144 | ||
145 | MyScrolledWindow::MyScrolledWindow( wxWindow *parent, wxWindowID id, | |
146 | const wxPoint &pos, const wxSize &size ) | |
147 | : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER, "test canvas" ) | |
148 | { | |
149 | MyTopLabels *top = new MyTopLabels( this, -1, wxDefaultPosition, wxSize(-1,25) ); | |
150 | MyRightLabels *right = new MyRightLabels( this, -1, wxDefaultPosition, wxSize(60,-1) ); | |
151 | ||
152 | m_canvas = new MyCanvas( this, top, right, -1, wxDefaultPosition, wxDefaultSize ); | |
153 | ||
154 | SetTargetWindow( m_canvas ); | |
155 | ||
156 | SetBackgroundColour( wxT("WHEAT") ); | |
157 | ||
158 | SetCursor( wxCursor( wxCURSOR_HAND ) ); | |
159 | ||
160 | wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL ); | |
161 | ||
162 | wxBoxSizer *topsizer = new wxBoxSizer( wxHORIZONTAL ); | |
163 | topsizer->Add( 60,25 ); | |
164 | topsizer->Add( top, 1, wxEXPAND ); | |
165 | ||
166 | mainsizer->Add( topsizer, 0, wxEXPAND ); | |
167 | ||
168 | wxBoxSizer *middlesizer = new wxBoxSizer( wxHORIZONTAL ); | |
169 | middlesizer->Add( right, 0, wxEXPAND ); | |
170 | middlesizer->Add( m_canvas, 1, wxEXPAND ); | |
171 | ||
172 | mainsizer->Add( middlesizer, 1, wxEXPAND ); | |
173 | ||
174 | SetAutoLayout( TRUE ); | |
175 | SetSizer( mainsizer ); | |
176 | } | |
177 | ||
178 | MyScrolledWindow::~MyScrolledWindow() | |
179 | { | |
180 | } | |
181 | ||
182 | void MyScrolledWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
183 | { | |
184 | wxPaintDC dc( this ); | |
185 | ||
186 | /* | |
187 | wxSize size( GetClientSize() ); | |
188 | ||
189 | long w,h; | |
190 | dc.GetTextExtent( wxT("Headline"), &w, &h ); | |
191 | ||
192 | dc.DrawText( wxT("Headline"), long (size.x / 2 - w / 2), 10 ); | |
193 | */ | |
194 | } | |
195 | ||
196 | // MyTopLabels | |
197 | ||
198 | IMPLEMENT_DYNAMIC_CLASS(MyTopLabels,wxWindow) | |
199 | ||
200 | BEGIN_EVENT_TABLE(MyTopLabels, wxWindow) | |
201 | EVT_PAINT( MyTopLabels::OnPaint) | |
202 | END_EVENT_TABLE() | |
203 | ||
204 | MyTopLabels::MyTopLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size ) | |
205 | : wxWindow( parent, id, pos, size ) | |
206 | { | |
207 | m_owner = parent; | |
208 | } | |
209 | ||
210 | void MyTopLabels::OnPaint( wxPaintEvent &event ) | |
211 | { | |
212 | wxPaintDC dc(this); | |
213 | ||
214 | // This is wrong.. it will translate both x and y if the | |
215 | // window is scrolled, the label windows are active in one | |
216 | // direction only. Do the action below instead -- RL. | |
217 | //m_owner->PrepareDC( dc ); | |
218 | ||
219 | int xScrollUnits, xOrigin; | |
220 | ||
221 | m_owner->GetViewStart( &xOrigin, 0 ); | |
222 | m_owner->GetScrollPixelsPerUnit( &xScrollUnits, 0 ); | |
223 | dc.SetDeviceOrigin( -xOrigin * xScrollUnits, 0 ); | |
224 | ||
225 | dc.DrawText( "Column 1", 5, 5 ); | |
226 | dc.DrawText( "Column 2", 105, 5 ); | |
227 | dc.DrawText( "Column 3", 205, 5 ); | |
228 | } | |
229 | ||
230 | // MyRightLabels | |
231 | ||
232 | IMPLEMENT_DYNAMIC_CLASS(MyRightLabels,wxWindow) | |
233 | ||
234 | BEGIN_EVENT_TABLE(MyRightLabels, wxWindow) | |
235 | EVT_PAINT( MyRightLabels::OnPaint) | |
236 | END_EVENT_TABLE() | |
237 | ||
238 | MyRightLabels::MyRightLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size ) | |
239 | : wxWindow( parent, id, pos, size ) | |
240 | { | |
241 | m_owner = parent; | |
242 | } | |
243 | ||
244 | void MyRightLabels::OnPaint( wxPaintEvent &event ) | |
245 | { | |
246 | wxPaintDC dc(this); | |
247 | ||
248 | // This is wrong.. it will translate both x and y if the | |
249 | // window is scrolled, the label windows are active in one | |
250 | // direction only. Do the action below instead -- RL. | |
251 | //m_owner->PrepareDC( dc ); | |
252 | ||
253 | int yScrollUnits, yOrigin; | |
254 | ||
255 | m_owner->GetViewStart( 0, &yOrigin ); | |
256 | m_owner->GetScrollPixelsPerUnit( 0, &yScrollUnits ); | |
257 | dc.SetDeviceOrigin( 0, -yOrigin * yScrollUnits ); | |
258 | ||
259 | dc.DrawText( "Row 1", 5, 5 ); | |
260 | dc.DrawText( "Row 2", 5, 30 ); | |
261 | dc.DrawText( "Row 3", 5, 55 ); | |
262 | dc.DrawText( "Row 4", 5, 80 ); | |
263 | dc.DrawText( "Row 5", 5, 105 ); | |
264 | dc.DrawText( "Row 6", 5, 130 ); | |
265 | } | |
266 | ||
267 | // MyCanvas | |
268 | ||
269 | IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxPanel) | |
270 | ||
271 | BEGIN_EVENT_TABLE(MyCanvas, wxPanel) | |
272 | EVT_PAINT( MyCanvas::OnPaint) | |
273 | END_EVENT_TABLE() | |
274 | ||
275 | MyCanvas::MyCanvas( wxScrolledWindow *parent, MyTopLabels *top, MyRightLabels *right, | |
276 | wxWindowID id, const wxPoint &pos, const wxSize &size ) | |
277 | : wxPanel( parent, id, pos, size, wxSUNKEN_BORDER, "test canvas" ) | |
278 | { | |
279 | m_owner = parent; | |
280 | m_topLabels = top; | |
281 | m_rightLabels = right; | |
282 | ||
283 | (void)new wxButton( this, -1, "Hallo I", wxPoint(0,50), wxSize(100,25) ); | |
284 | (void)new wxButton( this, -1, "Hallo II", wxPoint(200,50), wxSize(100,25) ); | |
285 | ||
286 | (void)new wxTextCtrl( this, -1, "Text I", wxPoint(0,100), wxSize(100,25) ); | |
287 | (void)new wxTextCtrl( this, -1, "Text II", wxPoint(200,100), wxSize(100,25) ); | |
288 | ||
289 | SetBackgroundColour( wxT("WHEAT") ); | |
290 | ||
291 | SetCursor( wxCursor( wxCURSOR_IBEAM ) ); | |
292 | } | |
293 | ||
294 | MyCanvas::~MyCanvas() | |
295 | { | |
296 | } | |
297 | ||
298 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
299 | { | |
300 | wxPaintDC dc( this ); | |
301 | m_owner->PrepareDC( dc ); | |
302 | ||
303 | dc.SetPen( *wxBLACK_PEN ); | |
304 | ||
305 | // OK, let's assume we are a grid control and we have two | |
306 | // grid cells. Here in OnPaint we want to know which cell | |
307 | // to redraw so that we prevent redrawing cells that don't | |
308 | // need to get redrawn. We have one cell at (0,0) and one | |
309 | // more at (200,0), both having a size of (100,25). | |
310 | ||
311 | // We can query how much the window has been scrolled | |
312 | // by calling CalcUnscrolledPosition() | |
313 | ||
314 | int scroll_x = 0; | |
315 | int scroll_y = 0; | |
316 | m_owner->CalcUnscrolledPosition( scroll_x, scroll_y, &scroll_x, &scroll_y ); | |
317 | ||
318 | // We also need to know the size of the window to see which | |
319 | // cells are completely hidden and not get redrawn | |
320 | ||
321 | int size_x = 0; | |
322 | int size_y = 0; | |
323 | GetClientSize( &size_x, &size_y ); | |
324 | ||
325 | // First cell: (0,0)(100,25) | |
326 | // It it on screen? | |
327 | if ((0+100-scroll_x > 0) && (0+25-scroll_y > 0) && | |
328 | (0-scroll_x < size_x) && (0-scroll_y < size_y)) | |
329 | { | |
330 | // Has the region on screen been exposed? | |
331 | if (IsExposed(0,0,100,25)) | |
332 | { | |
333 | wxLogMessage( wxT("Redraw first cell") ); | |
334 | dc.DrawRectangle( 0, 0, 100, 25 ); | |
335 | dc.DrawText( "First Cell", 5, 5 ); | |
336 | } | |
337 | } | |
338 | ||
339 | ||
340 | // Second cell: (0,200)(100,25) | |
341 | // It it on screen? | |
342 | if ((200+100-scroll_x > 0) && (0+25-scroll_y > 0) && | |
343 | (200-scroll_x < size_x) && (0-scroll_y < size_y)) | |
344 | { | |
345 | // Has the region on screen been exposed? | |
346 | if (IsExposed(200,0,100,25)) | |
347 | { | |
348 | wxLogMessage( wxT("Redraw second cell") ); | |
349 | dc.DrawRectangle( 200, 0, 100, 25 ); | |
350 | dc.DrawText( "Second Cell", 205, 5 ); | |
351 | } | |
352 | } | |
353 | ||
354 | } | |
355 | ||
356 | void MyCanvas::ScrollWindow( int dx, int dy, const wxRect *rect ) | |
357 | { | |
358 | wxPanel::ScrollWindow( dx, dy, rect ); | |
359 | m_topLabels->ScrollWindow( dx, 0, rect ); | |
360 | m_rightLabels->ScrollWindow( 0, dy, rect ); | |
361 | } | |
362 | ||
363 | // MyFrame | |
364 | ||
365 | const int ID_QUIT = 108; | |
366 | const int ID_FULL = 109; | |
367 | const int ID_ABOUT = 110; | |
368 | ||
369 | IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) | |
370 | ||
371 | BEGIN_EVENT_TABLE(MyFrame,wxFrame) | |
372 | EVT_MENU (ID_ABOUT, MyFrame::OnAbout) | |
373 | EVT_MENU (ID_QUIT, MyFrame::OnQuit) | |
374 | EVT_MENU (ID_FULL, MyFrame::OnFullScreen) | |
375 | END_EVENT_TABLE() | |
376 | ||
377 | MyFrame::MyFrame() | |
378 | : wxFrame( (wxFrame *)NULL, -1, "wxScrolledWindow sample", | |
379 | wxPoint(20,20), wxSize(470,500) ) | |
380 | { | |
381 | wxMenu *file_menu = new wxMenu(); | |
382 | file_menu->Append( ID_ABOUT, "&About..."); | |
383 | file_menu->Append( ID_FULL, "&Full screen on/off"); | |
384 | file_menu->Append( ID_QUIT, "E&xit\tAlt-X"); | |
385 | ||
386 | wxMenuBar *menu_bar = new wxMenuBar(); | |
387 | menu_bar->Append(file_menu, "&File"); | |
388 | ||
389 | SetMenuBar( menu_bar ); | |
390 | ||
391 | CreateStatusBar(2); | |
392 | int widths[] = { -1, 100 }; | |
393 | SetStatusWidths( 2, widths ); | |
394 | ||
395 | m_scrolled = new MyScrolledWindow( this, -1, wxDefaultPosition, wxSize(100,100) ); | |
396 | m_scrolled->SetScrollbars( 10, 10, 50, 100 ); | |
397 | ||
398 | m_log = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE ); | |
399 | wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) ); | |
400 | delete old_log; | |
401 | ||
402 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); | |
403 | topsizer->Add( m_scrolled, 1, wxEXPAND ); | |
404 | topsizer->Add( m_log, 0, wxEXPAND ); | |
405 | ||
406 | SetAutoLayout( TRUE ); | |
407 | SetSizer( topsizer ); | |
408 | } | |
409 | ||
410 | void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) | |
411 | { | |
412 | Close( TRUE ); | |
413 | } | |
414 | ||
415 | void MyFrame::OnFullScreen( wxCommandEvent &WXUNUSED(event) ) | |
416 | { | |
417 | ShowFullScreen( !IsFullScreen(), wxFULLSCREEN_NOBORDER|wxFULLSCREEN_NOCAPTION ); | |
418 | } | |
419 | ||
420 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) | |
421 | { | |
422 | (void)wxMessageBox( "wxScroll demo II\n" | |
423 | "Robert Roebling (c) 1998", | |
424 | "About wxScroll II Demo", wxICON_INFORMATION | wxOK ); | |
425 | } | |
426 | ||
427 | //----------------------------------------------------------------------------- | |
428 | // MyApp | |
429 | //----------------------------------------------------------------------------- | |
430 | ||
431 | bool MyApp::OnInit() | |
432 | { | |
433 | wxFrame *frame = new MyFrame(); | |
434 | frame->Show( TRUE ); | |
435 | ||
436 | return TRUE; | |
437 | } | |
438 |