New scrolling code.
[wxWidgets.git] / samples / scroll / scroll.cpp
1 /*
2 * Program: scroll
3 *
4 * Author: Robert Roebling
5 *
6 * Copyright: (C) 1998, 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 MyApp;
31
32 // MyCanvas
33
34 class MyCanvas: public wxScrolledWindow
35 {
36 public:
37 MyCanvas() {};
38 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
39 ~MyCanvas();
40 void OnPaint( wxPaintEvent &event );
41 void OnAddButton( wxCommandEvent &event );
42 void OnDeleteButton( wxCommandEvent &event );
43 void OnMoveButton( wxCommandEvent &event );
44 void OnScrollWin( wxCommandEvent &event );
45
46 wxButton *m_button;
47
48 DECLARE_DYNAMIC_CLASS(MyCanvas)
49 DECLARE_EVENT_TABLE()
50 };
51
52 // MyFrame
53
54 class MyFrame: public wxFrame
55 {
56 public:
57 MyFrame();
58
59 void OnAbout( wxCommandEvent &event );
60 void OnQuit( wxCommandEvent &event );
61
62 MyCanvas *m_canvas;
63 wxTextCtrl *m_log;
64
65 DECLARE_DYNAMIC_CLASS(MyFrame)
66 DECLARE_EVENT_TABLE()
67 };
68
69 // MyApp
70
71 class MyApp: public wxApp
72 {
73 public:
74 virtual bool OnInit();
75 };
76
77 // main program
78
79 IMPLEMENT_APP(MyApp)
80
81 // ids
82
83 #define ID_ADDBUTTON 1
84 #define ID_DELBUTTON 2
85 #define ID_MOVEBUTTON 3
86 #define ID_SCROLLWIN 4
87
88 #define ID_NEWBUTTON 10
89
90 // MyCanvas
91
92 IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
93
94 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
95 EVT_PAINT( MyCanvas::OnPaint)
96 EVT_BUTTON( ID_ADDBUTTON, MyCanvas::OnAddButton)
97 EVT_BUTTON( ID_DELBUTTON, MyCanvas::OnDeleteButton)
98 EVT_BUTTON( ID_MOVEBUTTON, MyCanvas::OnMoveButton)
99 EVT_BUTTON( ID_SCROLLWIN, MyCanvas::OnScrollWin)
100 END_EVENT_TABLE()
101
102 MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
103 const wxPoint &pos, const wxSize &size )
104 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER | wxTAB_TRAVERSAL, "test canvas" )
105 {
106 (void) new wxButton( this, ID_ADDBUTTON, "add button", wxPoint(10,10) );
107 (void) new wxButton( this, ID_DELBUTTON, "del button", wxPoint(10,40) );
108 (void) new wxButton( this, ID_MOVEBUTTON, "move button", wxPoint(150,10) );
109 (void) new wxButton( this, ID_SCROLLWIN, "scroll win", wxPoint(250,10) );
110
111 wxString choices[] =
112 {
113 "This",
114 "is one of my",
115 "really",
116 "wonderful",
117 "examples."
118 };
119
120 m_button = new wxButton( this, -1, "wxButton", wxPoint(10,110) );
121
122 (void) new wxTextCtrl( this, -1, "wxTextCtrl", wxPoint(10,150) );
123
124 (void) new wxRadioButton( this, -1, "Disable", wxPoint(10,190) );
125
126 (void) new wxComboBox( this, -1, "This", wxPoint(10,230), wxDefaultSize, 5, choices );
127
128 (void) new wxRadioBox( this, -1, "This", wxPoint(10,310), wxDefaultSize, 5, choices, 2, wxRA_SPECIFY_COLS );
129
130 (void) new wxRadioBox( this, -1, "This", wxPoint(10,440), wxDefaultSize, 5, choices, 2, wxRA_SPECIFY_ROWS );
131
132 wxListCtrl *m_listCtrl = new wxListCtrl(
133 this, -1, wxPoint(200, 110), wxSize(180, 120),
134 wxLC_REPORT | wxSIMPLE_BORDER | wxLC_SINGLE_SEL );
135
136 m_listCtrl->InsertColumn(0, "First", wxLIST_FORMAT_LEFT, 90);
137 m_listCtrl->InsertColumn(1, "Last", wxLIST_FORMAT_LEFT, 90);
138
139 for ( int i=0; i < 30; i++)
140 {
141 char buf[20];
142 sprintf(buf, "Item %d", i);
143 m_listCtrl->InsertItem(i, buf);
144 }
145 m_listCtrl->SetItemState( 3, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
146
147 (void) new wxListBox( this, -1, wxPoint(260,280), wxSize(120,120), 5, choices, wxLB_ALWAYS_SB );
148
149 wxWindow *test = new wxWindow( this, -1, wxPoint(10, 530), wxSize(130,120), wxSIMPLE_BORDER | wxTAB_TRAVERSAL );
150 test->SetBackgroundColour( "WHEAT" );
151 wxButton *test2 = new wxButton( test, -1, "Hallo", wxPoint(10,10) );
152
153 test = new wxWindow( this, -1, wxPoint(160, 530), wxSize(130,120), wxSUNKEN_BORDER | wxTAB_TRAVERSAL );
154 test->SetBackgroundColour( "WHEAT" );
155 test->SetCursor( wxCursor( wxCURSOR_NO_ENTRY ) );
156 test2 = new wxButton( test, -1, "Hallo", wxPoint(10,10) );
157 test2->SetCursor( wxCursor( wxCURSOR_PENCIL ) );
158
159 test = new wxWindow( this, -1, wxPoint(310, 530), wxSize(130,120), wxRAISED_BORDER | wxTAB_TRAVERSAL );
160 test->SetBackgroundColour( "WHEAT" );
161 test->SetCursor( wxCursor( wxCURSOR_PENCIL ) );
162 test2 = new wxButton( test, -1, "Hallo", wxPoint(10,10) );
163 test2->SetCursor( wxCursor( wxCURSOR_NO_ENTRY ) );
164
165 SetBackgroundColour( "WHEAT" );
166
167 SetCursor( wxCursor( wxCURSOR_IBEAM ) );
168 }
169
170 MyCanvas::~MyCanvas()
171 {
172 }
173
174 void MyCanvas::OnAddButton( wxCommandEvent &WXUNUSED(event) )
175 {
176 wxLogMessage( "Inserting button at position 50,50" );
177 (void) new wxButton( this, ID_NEWBUTTON, "new button", wxPoint(50,50) );
178 }
179
180 void MyCanvas::OnDeleteButton( wxCommandEvent &event )
181 {
182 wxLogMessage( "deleting button inserted with ""add button""" );
183 wxWindow *win = FindWindow( ID_NEWBUTTON );
184 if (win)
185 win->Destroy();
186 else
187 wxLogMessage( "-> No window with id = ID_NEWBUTTON found." );
188 }
189
190 void MyCanvas::OnMoveButton( wxCommandEvent &event )
191 {
192 wxLogMessage( "Moving button 10 pixels downward.." );
193 wxWindow *win = FindWindow( event.GetId() );
194 win->Move( -1, win->GetPosition().y + 10 );
195 }
196
197 void MyCanvas::OnScrollWin( wxCommandEvent &WXUNUSED(event) )
198 {
199 wxLogMessage( "Scrolling 2 units up.\nThe white square and the controls should move equally!" );
200 int x,y;
201 ViewStart( &x, &y );
202 Scroll( -1, y+2 );
203 }
204
205 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
206 {
207 wxPaintDC dc( this );
208 PrepareDC( dc );
209
210 dc.DrawText( "Some text", 140, 140 );
211
212 dc.DrawRectangle( 100, 160, 200, 200 );
213 }
214
215 // MyFrame
216
217 const int ID_QUIT = 108;
218 const int ID_ABOUT = 109;
219
220 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
221
222 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
223 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
224 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
225 END_EVENT_TABLE()
226
227 MyFrame::MyFrame()
228 : wxFrame( (wxFrame *)NULL, -1, "wxScrolledWindow sample",
229 wxPoint(20,20), wxSize(470,500) )
230 {
231 wxMenu *file_menu = new wxMenu();
232 file_menu->Append( ID_ABOUT, "&About..");
233 file_menu->Append( ID_QUIT, "E&xit\tAlt-X");
234
235 wxMenuBar *menu_bar = new wxMenuBar();
236 menu_bar->Append(file_menu, "&File");
237
238 SetMenuBar( menu_bar );
239
240 CreateStatusBar(2);
241 int widths[] = { -1, 100 };
242 SetStatusWidths( 2, widths );
243
244 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(100,100) );
245 m_canvas->SetScrollbars( 10, 10, 50, 100 );
246
247 m_log = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE );
248 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
249 delete old_log;
250
251 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
252
253 topsizer->Add( m_canvas, 1, wxEXPAND );
254 topsizer->Add( m_log, 0, wxEXPAND );
255
256 SetAutoLayout( TRUE );
257 SetSizer( topsizer );
258 }
259
260 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
261 {
262 Close( TRUE );
263 }
264
265 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
266 {
267 (void)wxMessageBox( "wxScroll demo\n"
268 "Robert Roebling (c) 1998",
269 "About wxScroll Demo", wxICON_INFORMATION | wxOK );
270 }
271
272 //-----------------------------------------------------------------------------
273 // MyApp
274 //-----------------------------------------------------------------------------
275
276 bool MyApp::OnInit()
277 {
278 wxFrame *frame = new MyFrame();
279 frame->Show( TRUE );
280
281 return TRUE;
282 }
283