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