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