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