]> git.saurik.com Git - wxWidgets.git/blob - samples/scroll/scroll.cpp
Removed fstream include which seemed unnecessary, and gave errors for BC++ 5.5 anyway.
[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 OnQueryPosition( wxCommandEvent &event );
42 void OnAddButton( wxCommandEvent &event );
43 void OnDeleteButton( wxCommandEvent &event );
44 void OnMoveButton( wxCommandEvent &event );
45 void OnScrollWin( wxCommandEvent &event );
46 void OnMouseDown( wxMouseEvent &event );
47 void OnScroll( wxScrollWinEvent &event );
48
49 wxButton *m_button;
50
51 DECLARE_DYNAMIC_CLASS(MyCanvas)
52 DECLARE_EVENT_TABLE()
53 };
54
55 // MyFrame
56
57 class MyFrame: public wxFrame
58 {
59 public:
60 MyFrame();
61
62 void OnAbout( wxCommandEvent &event );
63 void OnQuit( wxCommandEvent &event );
64 void OnDeleteAll( wxCommandEvent &event );
65 void OnInsertNew( wxCommandEvent &event );
66
67 MyCanvas *m_canvas;
68 wxTextCtrl *m_log;
69
70 DECLARE_DYNAMIC_CLASS(MyFrame)
71 DECLARE_EVENT_TABLE()
72 };
73
74 // MyApp
75
76 class MyApp: public wxApp
77 {
78 public:
79 virtual bool OnInit();
80 };
81
82 // main program
83
84 IMPLEMENT_APP(MyApp)
85
86 // ids
87
88 #define ID_ADDBUTTON 1
89 #define ID_DELBUTTON 2
90 #define ID_MOVEBUTTON 3
91 #define ID_SCROLLWIN 4
92 #define ID_QUERYPOS 5
93
94 #define ID_NEWBUTTON 10
95
96 // MyCanvas
97
98 IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
99
100 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
101 EVT_PAINT( MyCanvas::OnPaint)
102 EVT_MOUSE_EVENTS( MyCanvas::OnMouseDown)
103 EVT_BUTTON( ID_QUERYPOS, MyCanvas::OnQueryPosition)
104 EVT_BUTTON( ID_ADDBUTTON, MyCanvas::OnAddButton)
105 EVT_BUTTON( ID_DELBUTTON, MyCanvas::OnDeleteButton)
106 EVT_BUTTON( ID_MOVEBUTTON, MyCanvas::OnMoveButton)
107 EVT_BUTTON( ID_SCROLLWIN, MyCanvas::OnScrollWin)
108 EVT_SCROLLWIN( MyCanvas::OnScroll)
109 END_EVENT_TABLE()
110
111 MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
112 const wxPoint &pos, const wxSize &size )
113 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER | wxTAB_TRAVERSAL, "test canvas" )
114 {
115 (void) new wxButton( this, ID_ADDBUTTON, "add button", wxPoint(10,10) );
116 (void) new wxButton( this, ID_DELBUTTON, "del button", wxPoint(10,40) );
117 (void) new wxButton( this, ID_MOVEBUTTON, "move button", wxPoint(150,10) );
118 (void) new wxButton( this, ID_SCROLLWIN, "scroll win", wxPoint(250,10) );
119
120 #if 0
121
122 wxString choices[] =
123 {
124 "This",
125 "is one of my",
126 "really",
127 "wonderful",
128 "examples."
129 };
130
131 m_button = new wxButton( this, ID_QUERYPOS, "Query position", wxPoint(10,110) );
132
133 (void) new wxTextCtrl( this, -1, "wxTextCtrl", wxPoint(10,150), wxSize(80,-1) );
134
135 (void) new wxRadioButton( this, -1, "Disable", wxPoint(10,190) );
136
137 (void) new wxComboBox( this, -1, "This", wxPoint(10,230), wxDefaultSize, 5, choices );
138
139 (void) new wxRadioBox( this, -1, "This", wxPoint(10,310), wxDefaultSize, 5, choices, 2, wxRA_SPECIFY_COLS );
140
141 (void) new wxRadioBox( this, -1, "This", wxPoint(10,440), wxDefaultSize, 5, choices, 2, wxRA_SPECIFY_ROWS );
142
143 wxListCtrl *m_listCtrl = new wxListCtrl(
144 this, -1, wxPoint(200, 110), wxSize(180, 120),
145 wxLC_REPORT | wxSIMPLE_BORDER | wxLC_SINGLE_SEL );
146
147 m_listCtrl->InsertColumn(0, "First", wxLIST_FORMAT_LEFT, 90);
148 m_listCtrl->InsertColumn(1, "Last", wxLIST_FORMAT_LEFT, 90);
149
150 for ( int i=0; i < 30; i++)
151 {
152 char buf[20];
153 sprintf(buf, "Item %d", i);
154 m_listCtrl->InsertItem(i, buf);
155 }
156 m_listCtrl->SetItemState( 3, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
157
158 (void) new wxListBox( this, -1, wxPoint(260,280), wxSize(120,120), 5, choices, wxLB_ALWAYS_SB );
159
160 #endif
161
162 wxPanel *test = new wxPanel( this, -1, wxPoint(10, 10), wxSize(130,50), wxSIMPLE_BORDER | wxTAB_TRAVERSAL );
163 test->SetBackgroundColour( "WHEAT" );
164
165 #if 0
166
167 wxButton *test2 = new wxButton( test, -1, "Hallo", wxPoint(10,10) );
168
169 test = new wxPanel( this, -1, wxPoint(160, 530), wxSize(130,120), wxSUNKEN_BORDER | wxTAB_TRAVERSAL );
170 test->SetBackgroundColour( "WHEAT" );
171 test->SetCursor( wxCursor( wxCURSOR_NO_ENTRY ) );
172 test2 = new wxButton( test, -1, "Hallo", wxPoint(10,10) );
173 test2->SetCursor( wxCursor( wxCURSOR_PENCIL ) );
174
175 test = new wxPanel( this, -1, wxPoint(310, 530), wxSize(130,120), wxRAISED_BORDER | wxTAB_TRAVERSAL );
176 test->SetBackgroundColour( "WHEAT" );
177 test->SetCursor( wxCursor( wxCURSOR_PENCIL ) );
178 test2 = new wxButton( test, -1, "Hallo", wxPoint(10,10) );
179 test2->SetCursor( wxCursor( wxCURSOR_NO_ENTRY ) );
180
181 #endif
182
183 SetBackgroundColour( "BLUE" );
184
185 SetCursor( wxCursor( wxCURSOR_IBEAM ) );
186 }
187
188 MyCanvas::~MyCanvas()
189 {
190 }
191
192 void MyCanvas::OnMouseDown( wxMouseEvent &event )
193 {
194 if (event.LeftDown())
195 {
196 wxPoint pt( event.GetPosition() );
197 int x,y;
198 CalcUnscrolledPosition( pt.x, pt.y, &x, &y );
199 wxLogMessage( "Mouse down event at: %d %d, scrolled: %d %d", pt.x, pt.y, x, y );
200
201 if ( !event.LeftIsDown() )
202 wxLogMessage( "Error: LeftIsDown() should be TRUE if for LeftDown()" );
203 }
204 }
205
206 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
207 {
208 wxPaintDC dc( this );
209 PrepareDC( dc );
210
211 dc.DrawText( "Press mouse button to test calculations!", 160, 50 );
212
213 dc.DrawText( "Some text", 140, 140 );
214
215 dc.DrawRectangle( 100, 160, 200, 200 );
216 }
217
218 void MyCanvas::OnQueryPosition( wxCommandEvent &WXUNUSED(event) )
219 {
220 wxPoint pt( m_button->GetPosition() );
221 wxLogMessage( "Position of ""Query position"" is %d %d", pt.x, pt.y );
222 pt = ClientToScreen( pt );
223 wxLogMessage( "Position of ""Query position"" on screen is %d %d", pt.x, pt.y );
224 }
225
226 void MyCanvas::OnAddButton( wxCommandEvent &WXUNUSED(event) )
227 {
228 wxLogMessage( "Inserting button at position 10,70..." );
229 wxButton *button = new wxButton( this, ID_NEWBUTTON, "new button", wxPoint(10,70), wxSize(80,25) );
230 wxPoint pt( button->GetPosition() );
231 wxLogMessage( "-> Position after inserting %d %d", pt.x, pt.y );
232 }
233
234 void MyCanvas::OnDeleteButton( wxCommandEvent &event )
235 {
236 wxLogMessage( "Deleting button inserted with ""Add button""..." );
237 wxWindow *win = FindWindow( ID_NEWBUTTON );
238 if (win)
239 win->Destroy();
240 else
241 wxLogMessage( "-> No window with id = ID_NEWBUTTON found." );
242 }
243
244 void MyCanvas::OnMoveButton( wxCommandEvent &event )
245 {
246 wxLogMessage( "Moving button 10 pixels downward.." );
247 wxWindow *win = FindWindow( event.GetId() );
248 wxPoint pt( win->GetPosition() );
249 wxLogMessage( "-> Position before move is %d %d", pt.x, pt.y );
250 win->Move( -1, pt.y + 10 );
251 pt = win->GetPosition();
252 wxLogMessage( "-> Position after move is %d %d", pt.x, pt.y );
253 }
254
255 void MyCanvas::OnScrollWin( wxCommandEvent &WXUNUSED(event) )
256 {
257 wxLogMessage( "Scrolling 2 units up.\nThe white square and the controls should move equally!" );
258 int x,y;
259 ViewStart( &x, &y );
260 Scroll( -1, y+2 );
261 }
262
263 void MyCanvas::OnScroll( wxScrollWinEvent &event )
264 {
265 if (( event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE ))
266 {
267 wxLogMessage( "Thumb released; position: %u", event.GetPosition() );
268 }
269 event.Skip();
270 }
271
272 // MyFrame
273
274 const int ID_QUIT = 108;
275 const int ID_ABOUT = 109;
276 const int ID_DELETE_ALL = 110;
277 const int ID_INSERT_NEW = 111;
278
279 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
280
281 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
282 EVT_MENU (ID_DELETE_ALL, MyFrame::OnDeleteAll)
283 EVT_MENU (ID_INSERT_NEW, MyFrame::OnInsertNew)
284 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
285 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
286 END_EVENT_TABLE()
287
288 MyFrame::MyFrame()
289 : wxFrame( (wxFrame *)NULL, -1, "wxScrolledWindow sample",
290 wxPoint(20,20), wxSize(470,500) )
291 {
292 wxMenu *file_menu = new wxMenu();
293 file_menu->Append( ID_DELETE_ALL, "Delete all");
294 file_menu->Append( ID_INSERT_NEW, "Insert new");
295 file_menu->Append( ID_ABOUT, "&About..");
296 file_menu->Append( ID_QUIT, "E&xit\tAlt-X");
297
298 wxMenuBar *menu_bar = new wxMenuBar();
299 menu_bar->Append(file_menu, "&File");
300
301 SetMenuBar( menu_bar );
302
303 CreateStatusBar(2);
304 int widths[] = { -1, 100 };
305 SetStatusWidths( 2, widths );
306
307 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(100,100) );
308 m_canvas->SetScrollbars( 10, 10, 50, 100 );
309 #if 0
310 m_log = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE );
311 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
312 delete old_log;
313 #endif
314 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
315
316 topsizer->Add( m_canvas, 1, wxEXPAND );
317 // topsizer->Add( m_log, 0, wxEXPAND );
318
319 SetAutoLayout( TRUE );
320 SetSizer( topsizer );
321 }
322
323 void MyFrame::OnDeleteAll( wxCommandEvent &WXUNUSED(event) )
324 {
325 m_canvas->DestroyChildren();
326 }
327
328 void MyFrame::OnInsertNew( wxCommandEvent &WXUNUSED(event) )
329 {
330 (void)new wxButton( m_canvas, -1, "Hello", wxPoint(100,100) );
331 }
332
333 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
334 {
335 Close( TRUE );
336 }
337
338 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
339 {
340 (void)wxMessageBox( "wxScroll demo\n"
341 "Robert Roebling (c) 1998",
342 "About wxScroll Demo", wxICON_INFORMATION | wxOK );
343 }
344
345 //-----------------------------------------------------------------------------
346 // MyApp
347 //-----------------------------------------------------------------------------
348
349 bool MyApp::OnInit()
350 {
351 wxFrame *frame = new MyFrame();
352 frame->Show( TRUE );
353
354 return TRUE;
355 }
356