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