]>
git.saurik.com Git - wxWidgets.git/blob - samples/scrollsub/scrollsub.cpp
4 * Author: Robert Roebling
6 * Copyright: (C) 1999, Robert Roebling
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
22 #include "wx/listctrl.h"
30 class MyScrolledWindow
;
36 class MyScrolledWindow
: public wxScrolledWindow
40 MyScrolledWindow( wxWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
42 void OnPaint( wxPaintEvent
&event
);
47 DECLARE_DYNAMIC_CLASS(MyScrolledWindow
)
53 class MyTopLabels
: public wxWindow
57 MyTopLabels( wxScrolledWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
59 void OnPaint( wxPaintEvent
&event
);
62 wxScrolledWindow
*m_owner
;
64 DECLARE_DYNAMIC_CLASS(MyTopLabels
)
70 class MyRightLabels
: public wxWindow
74 MyRightLabels( wxScrolledWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
76 void OnPaint( wxPaintEvent
&event
);
79 wxScrolledWindow
*m_owner
;
81 DECLARE_DYNAMIC_CLASS(MyRightLabels
)
87 class MyCanvas
: public wxPanel
91 MyCanvas( wxScrolledWindow
*parent
, MyTopLabels
*top
, MyRightLabels
*right
,
92 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
94 void OnPaint( wxPaintEvent
&event
);
95 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
98 wxScrolledWindow
*m_owner
;
99 MyTopLabels
*m_topLabels
;
100 MyRightLabels
*m_rightLabels
;
102 DECLARE_DYNAMIC_CLASS(MyCanvas
)
103 DECLARE_EVENT_TABLE()
108 class MyFrame
: public wxFrame
113 void OnAbout( wxCommandEvent
&event
);
114 void OnQuit( wxCommandEvent
&event
);
115 void OnFullScreen( wxCommandEvent
&event
);
117 wxScrolledWindow
*m_scrolled
;
123 DECLARE_DYNAMIC_CLASS(MyFrame
)
124 DECLARE_EVENT_TABLE()
129 class MyApp
: public wxApp
132 virtual bool OnInit();
141 IMPLEMENT_DYNAMIC_CLASS(MyScrolledWindow
, wxScrolledWindow
)
143 BEGIN_EVENT_TABLE(MyScrolledWindow
, wxScrolledWindow
)
144 EVT_PAINT( MyScrolledWindow::OnPaint
)
147 MyScrolledWindow::MyScrolledWindow( wxWindow
*parent
, wxWindowID id
,
148 const wxPoint
&pos
, const wxSize
&size
)
149 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
, _T("test canvas") )
151 MyTopLabels
*top
= new MyTopLabels( this, wxID_ANY
, wxDefaultPosition
, wxSize(wxDefaultCoord
,25) );
152 MyRightLabels
*right
= new MyRightLabels( this, wxID_ANY
, wxDefaultPosition
, wxSize(60,wxDefaultCoord
) );
154 m_canvas
= new MyCanvas( this, top
, right
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
156 SetTargetWindow( m_canvas
);
158 SetBackgroundColour( wxT("WHEAT") );
160 SetCursor( wxCursor( wxCURSOR_HAND
) );
162 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
164 wxBoxSizer
*topsizer
= new wxBoxSizer( wxHORIZONTAL
);
165 topsizer
->Add( 60,25 );
166 topsizer
->Add( top
, 1, wxEXPAND
);
168 mainsizer
->Add( topsizer
, 0, wxEXPAND
);
170 wxBoxSizer
*middlesizer
= new wxBoxSizer( wxHORIZONTAL
);
171 middlesizer
->Add( right
, 0, wxEXPAND
);
172 middlesizer
->Add( m_canvas
, 1, wxEXPAND
);
174 mainsizer
->Add( middlesizer
, 1, wxEXPAND
);
176 SetAutoLayout( true );
177 SetSizer( mainsizer
);
180 MyScrolledWindow::~MyScrolledWindow()
184 void MyScrolledWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
186 wxPaintDC
dc( this );
189 wxSize size( GetClientSize() );
192 dc.GetTextExtent( wxT("Headline"), &w, &h );
194 dc.DrawText( wxT("Headline"), long (size.x / 2 - w / 2), 10 );
200 IMPLEMENT_DYNAMIC_CLASS(MyTopLabels
,wxWindow
)
202 BEGIN_EVENT_TABLE(MyTopLabels
, wxWindow
)
203 EVT_PAINT( MyTopLabels::OnPaint
)
206 MyTopLabels::MyTopLabels( wxScrolledWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
207 : wxWindow( parent
, id
, pos
, size
)
212 void MyTopLabels::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
216 // This is wrong.. it will translate both x and y if the
217 // window is scrolled, the label windows are active in one
218 // direction only. Do the action below instead -- RL.
219 //m_owner->PrepareDC( dc );
221 int xScrollUnits
, xOrigin
;
223 m_owner
->GetViewStart( &xOrigin
, 0 );
224 m_owner
->GetScrollPixelsPerUnit( &xScrollUnits
, 0 );
225 dc
.SetDeviceOrigin( -xOrigin
* xScrollUnits
, 0 );
227 dc
.DrawText( _T("Column 1"), 5, 5 );
228 dc
.DrawText( _T("Column 2"), 105, 5 );
229 dc
.DrawText( _T("Column 3"), 205, 5 );
234 IMPLEMENT_DYNAMIC_CLASS(MyRightLabels
,wxWindow
)
236 BEGIN_EVENT_TABLE(MyRightLabels
, wxWindow
)
237 EVT_PAINT( MyRightLabels::OnPaint
)
240 MyRightLabels::MyRightLabels( wxScrolledWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
241 : wxWindow( parent
, id
, pos
, size
)
246 void MyRightLabels::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
250 // This is wrong.. it will translate both x and y if the
251 // window is scrolled, the label windows are active in one
252 // direction only. Do the action below instead -- RL.
253 //m_owner->PrepareDC( dc );
255 int yScrollUnits
, yOrigin
;
257 m_owner
->GetViewStart( 0, &yOrigin
);
258 m_owner
->GetScrollPixelsPerUnit( 0, &yScrollUnits
);
259 dc
.SetDeviceOrigin( 0, -yOrigin
* yScrollUnits
);
261 dc
.DrawText( _T("Row 1"), 5, 5 );
262 dc
.DrawText( _T("Row 2"), 5, 30 );
263 dc
.DrawText( _T("Row 3"), 5, 55 );
264 dc
.DrawText( _T("Row 4"), 5, 80 );
265 dc
.DrawText( _T("Row 5"), 5, 105 );
266 dc
.DrawText( _T("Row 6"), 5, 130 );
271 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxPanel
)
273 BEGIN_EVENT_TABLE(MyCanvas
, wxPanel
)
274 EVT_PAINT( MyCanvas::OnPaint
)
277 MyCanvas::MyCanvas( wxScrolledWindow
*parent
, MyTopLabels
*top
, MyRightLabels
*right
,
278 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
279 : wxPanel( parent
, id
, pos
, size
, wxSUNKEN_BORDER
, _T("test canvas") )
283 m_rightLabels
= right
;
285 (void)new wxButton( this, wxID_ANY
, _T("Hallo I"), wxPoint(0,50), wxSize(100,25) );
286 (void)new wxButton( this, wxID_ANY
, _T("Hallo II"), wxPoint(200,50), wxSize(100,25) );
288 (void)new wxTextCtrl( this, wxID_ANY
, _T("Text I"), wxPoint(0,100), wxSize(100,25) );
289 (void)new wxTextCtrl( this, wxID_ANY
, _T("Text II"), wxPoint(200,100), wxSize(100,25) );
291 (void)new wxComboBox( this, wxID_ANY
, _T("ComboBox I"), wxPoint(0,150), wxSize(100,25));
292 (void)new wxComboBox( this, wxID_ANY
, _T("ComboBox II"), wxPoint(200,150), wxSize(100,25));
294 SetBackgroundColour( wxT("WHEAT") );
296 SetCursor( wxCursor( wxCURSOR_IBEAM
) );
299 MyCanvas::~MyCanvas()
303 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
305 wxPaintDC
dc( this );
306 m_owner
->PrepareDC( dc
);
308 dc
.SetPen( *wxBLACK_PEN
);
310 // OK, let's assume we are a grid control and we have two
311 // grid cells. Here in OnPaint we want to know which cell
312 // to redraw so that we prevent redrawing cells that don't
313 // need to get redrawn. We have one cell at (0,0) and one
314 // more at (200,0), both having a size of (100,25).
316 // We can query how much the window has been scrolled
317 // by calling CalcUnscrolledPosition()
321 m_owner
->CalcUnscrolledPosition( scroll_x
, scroll_y
, &scroll_x
, &scroll_y
);
323 // We also need to know the size of the window to see which
324 // cells are completely hidden and not get redrawn
328 GetClientSize( &size_x
, &size_y
);
330 // First cell: (0,0)(100,25)
332 if ((0+100-scroll_x
> 0) && (0+25-scroll_y
> 0) &&
333 (0-scroll_x
< size_x
) && (0-scroll_y
< size_y
))
335 // Has the region on screen been exposed?
336 if (IsExposed(0,0,100,25))
338 wxLogMessage( wxT("Redraw first cell") );
339 dc
.DrawRectangle( 0, 0, 100, 25 );
340 dc
.DrawText( _T("First Cell"), 5, 5 );
345 // Second cell: (0,200)(100,25)
347 if ((200+100-scroll_x
> 0) && (0+25-scroll_y
> 0) &&
348 (200-scroll_x
< size_x
) && (0-scroll_y
< size_y
))
350 // Has the region on screen been exposed?
351 if (IsExposed(200,0,100,25))
353 wxLogMessage( wxT("Redraw second cell") );
354 dc
.DrawRectangle( 200, 0, 100, 25 );
355 dc
.DrawText( _T("Second Cell"), 205, 5 );
361 void MyCanvas::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
363 wxPanel::ScrollWindow( dx
, dy
, rect
);
364 m_topLabels
->ScrollWindow( dx
, 0, rect
);
365 m_rightLabels
->ScrollWindow( 0, dy
, rect
);
370 const int ID_QUIT
= 108;
371 const int ID_FULL
= 109;
372 const int ID_ABOUT
= 110;
374 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
376 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
377 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
378 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
379 EVT_MENU (ID_FULL
, MyFrame::OnFullScreen
)
383 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxScrolledWindow sample"),
384 wxPoint(20,20), wxSize(470,500) )
386 wxMenu
*file_menu
= new wxMenu();
387 file_menu
->Append( ID_ABOUT
, _T("&About..."));
388 file_menu
->Append( ID_FULL
, _T("&Full screen on/off"));
389 file_menu
->Append( ID_QUIT
, _T("E&xit\tAlt-X"));
391 wxMenuBar
*menu_bar
= new wxMenuBar();
392 menu_bar
->Append(file_menu
, _T("&File"));
394 SetMenuBar( menu_bar
);
398 int widths
[] = { -1, 100 };
399 SetStatusWidths( 2, widths
);
400 #endif // wxUSE_STATUSBAR
402 m_scrolled
= new MyScrolledWindow( this, wxID_ANY
, wxDefaultPosition
, wxSize(100,100) );
403 m_scrolled
->SetScrollbars( 10, 10, 50, 50 );
406 m_log
= new wxTextCtrl( this, wxID_ANY
, _T("This is the log window.\n"), wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE
);
407 wxLog
*old_log
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
411 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
412 topsizer
->Add( m_scrolled
, 1, wxEXPAND
);
414 topsizer
->Add( m_log
, 0, wxEXPAND
);
417 SetAutoLayout( true );
418 SetSizer( topsizer
);
421 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
426 void MyFrame::OnFullScreen( wxCommandEvent
&WXUNUSED(event
) )
428 ShowFullScreen( !IsFullScreen(), wxFULLSCREEN_NOBORDER
|wxFULLSCREEN_NOCAPTION
);
431 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
433 (void)wxMessageBox( _T("wxScroll demo II\n")
434 _T("Robert Roebling (c) 1998"),
435 _T("About wxScroll II Demo"), wxICON_INFORMATION
| wxOK
);
438 //-----------------------------------------------------------------------------
440 //-----------------------------------------------------------------------------
444 wxFrame
*frame
= new MyFrame();