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