4 * Author: Robert Roebling
6 * Copyright: (C) 1998, Robert Roebling
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
22 #include "wx/listctrl.h"
34 class MyCanvas
: public wxScrolledWindow
38 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
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 void OnMouseDown( wxMouseEvent
&event
);
47 void OnScroll( wxScrollWinEvent
&event
);
51 DECLARE_DYNAMIC_CLASS(MyCanvas
)
57 class MyFrame
: public wxFrame
62 void OnAbout( wxCommandEvent
&event
);
63 void OnQuit( wxCommandEvent
&event
);
64 void OnDeleteAll( wxCommandEvent
&event
);
65 void OnInsertNew( wxCommandEvent
&event
);
70 DECLARE_DYNAMIC_CLASS(MyFrame
)
76 class MyApp
: public wxApp
79 virtual bool OnInit();
88 #define ID_ADDBUTTON 1
89 #define ID_DELBUTTON 2
90 #define ID_MOVEBUTTON 3
91 #define ID_SCROLLWIN 4
94 #define ID_NEWBUTTON 10
98 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
100 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
101 EVT_PAINT( MyCanvas::OnPaint
)
102 EVT_MOUSE_EVENTS( MyCanvas::OnMouseDown
)
103 EVT_BUTTON( ID_QUERYPOS
, MyCanvas::OnQueryPosition
)
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
)
108 EVT_SCROLLWIN( MyCanvas::OnScroll
)
111 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
112 const wxPoint
&pos
, const wxSize
&size
)
113 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
| wxTAB_TRAVERSAL
, "test canvas" )
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) );
129 m_button
= new wxButton( this, ID_QUERYPOS
, "Query position", wxPoint(10,110) );
131 (void) new wxTextCtrl( this, -1, "wxTextCtrl", wxPoint(10,150), wxSize(80,-1) );
133 (void) new wxRadioButton( this, -1, "Disable", wxPoint(10,190) );
135 (void) new wxComboBox( this, -1, "This", wxPoint(10,230), wxDefaultSize
, 5, choices
);
137 (void) new wxRadioBox( this, -1, "This", wxPoint(10,310), wxDefaultSize
, 5, choices
, 2, wxRA_SPECIFY_COLS
);
139 (void) new wxRadioBox( this, -1, "This", wxPoint(10,440), wxDefaultSize
, 5, choices
, 2, wxRA_SPECIFY_ROWS
);
141 wxListCtrl
*m_listCtrl
= new wxListCtrl(
142 this, -1, wxPoint(200, 110), wxSize(180, 120),
143 wxLC_REPORT
| wxSIMPLE_BORDER
| wxLC_SINGLE_SEL
);
145 m_listCtrl
->InsertColumn(0, "First", wxLIST_FORMAT_LEFT
, 90);
146 m_listCtrl
->InsertColumn(1, "Last", wxLIST_FORMAT_LEFT
, 90);
148 for ( int i
=0; i
< 30; i
++)
151 sprintf(buf
, "Item %d", i
);
152 m_listCtrl
->InsertItem(i
, buf
);
154 m_listCtrl
->SetItemState( 3, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
156 (void) new wxListBox( this, -1, wxPoint(260,280), wxSize(120,120), 5, choices
, wxLB_ALWAYS_SB
);
158 wxPanel
*test
= new wxPanel( this, -1, wxPoint(10, 530), wxSize(130,120), wxSIMPLE_BORDER
| wxTAB_TRAVERSAL
);
159 test
->SetBackgroundColour( "WHEAT" );
160 wxButton
*test2
= new wxButton( test
, -1, "Hallo", wxPoint(10,10) );
162 test
= new wxPanel( this, -1, wxPoint(160, 530), wxSize(130,120), wxSUNKEN_BORDER
| wxTAB_TRAVERSAL
);
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
) );
168 test
= new wxPanel( this, -1, wxPoint(310, 530), wxSize(130,120), wxRAISED_BORDER
| wxTAB_TRAVERSAL
);
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
) );
174 SetBackgroundColour( "WHEAT" );
176 SetCursor( wxCursor( wxCURSOR_IBEAM
) );
179 MyCanvas::~MyCanvas()
183 void MyCanvas::OnMouseDown( wxMouseEvent
&event
)
185 if (event
.LeftDown())
187 wxPoint
pt( event
.GetPosition() );
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
);
193 if (event
.LeftIsDown() &&
196 wxLogMessage( "Error: both LeftDown() and LeftIsDown() are TRUE!" );
200 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
202 wxPaintDC
dc( this );
205 dc
.DrawText( "Press mouse button to test calculations!", 160, 50 );
207 dc
.DrawText( "Some text", 140, 140 );
209 dc
.DrawRectangle( 100, 160, 200, 200 );
212 void MyCanvas::OnQueryPosition( wxCommandEvent
&WXUNUSED(event
) )
214 wxPoint
pt( m_button
->GetPosition() );
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
);
220 void MyCanvas::OnAddButton( wxCommandEvent
&WXUNUSED(event
) )
222 wxLogMessage( "Inserting button at position 10,70..." );
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
);
228 void MyCanvas::OnDeleteButton( wxCommandEvent
&event
)
230 wxLogMessage( "Deleting button inserted with ""Add button""..." );
231 wxWindow
*win
= FindWindow( ID_NEWBUTTON
);
235 wxLogMessage( "-> No window with id = ID_NEWBUTTON found." );
238 void MyCanvas::OnMoveButton( wxCommandEvent
&event
)
240 wxLogMessage( "Moving button 10 pixels downward.." );
241 wxWindow
*win
= FindWindow( event
.GetId() );
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
);
249 void MyCanvas::OnScrollWin( wxCommandEvent
&WXUNUSED(event
) )
251 wxLogMessage( "Scrolling 2 units up.\nThe white square and the controls should move equally!" );
257 void MyCanvas::OnScroll( wxScrollWinEvent
&event
)
259 if (( event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
261 wxLogMessage( "Thumb released; position: %u", event
.GetPosition() );
268 const int ID_QUIT
= 108;
269 const int ID_ABOUT
= 109;
270 const int ID_DELETE_ALL
= 110;
271 const int ID_INSERT_NEW
= 111;
273 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
275 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
276 EVT_MENU (ID_DELETE_ALL
, MyFrame::OnDeleteAll
)
277 EVT_MENU (ID_INSERT_NEW
, MyFrame::OnInsertNew
)
278 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
279 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
283 : wxFrame( (wxFrame
*)NULL
, -1, "wxScrolledWindow sample",
284 wxPoint(20,20), wxSize(470,500) )
286 wxMenu
*file_menu
= new wxMenu();
287 file_menu
->Append( ID_DELETE_ALL
, "Delete all");
288 file_menu
->Append( ID_INSERT_NEW
, "Insert new");
289 file_menu
->Append( ID_ABOUT
, "&About..");
290 file_menu
->Append( ID_QUIT
, "E&xit\tAlt-X");
292 wxMenuBar
*menu_bar
= new wxMenuBar();
293 menu_bar
->Append(file_menu
, "&File");
295 SetMenuBar( menu_bar
);
298 int widths
[] = { -1, 100 };
299 SetStatusWidths( 2, widths
);
301 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(100,100) );
302 m_canvas
->SetScrollbars( 10, 10, 50, 100 );
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
) );
308 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
310 topsizer
->Add( m_canvas
, 1, wxEXPAND
);
311 topsizer
->Add( m_log
, 0, wxEXPAND
);
313 SetAutoLayout( TRUE
);
314 SetSizer( topsizer
);
317 void MyFrame::OnDeleteAll( wxCommandEvent
&WXUNUSED(event
) )
319 m_canvas
->DestroyChildren();
322 void MyFrame::OnInsertNew( wxCommandEvent
&WXUNUSED(event
) )
324 (void)new wxButton( m_canvas
, -1, "Hello", wxPoint(100,100) );
327 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
332 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
334 (void)wxMessageBox( "wxScroll demo\n"
335 "Robert Roebling (c) 1998",
336 "About wxScroll Demo", wxICON_INFORMATION
| wxOK
);
339 //-----------------------------------------------------------------------------
341 //-----------------------------------------------------------------------------
345 wxFrame
*frame
= new MyFrame();