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
);
68 DECLARE_DYNAMIC_CLASS(MyFrame
)
74 class MyApp
: public wxApp
77 virtual bool OnInit();
86 #define ID_ADDBUTTON 1
87 #define ID_DELBUTTON 2
88 #define ID_MOVEBUTTON 3
89 #define ID_SCROLLWIN 4
92 #define ID_NEWBUTTON 10
96 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
98 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
99 EVT_PAINT( MyCanvas::OnPaint
)
100 EVT_MOUSE_EVENTS( MyCanvas::OnMouseDown
)
101 EVT_BUTTON( ID_QUERYPOS
, MyCanvas::OnQueryPosition
)
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
)
106 EVT_SCROLLWIN( MyCanvas::OnScroll
)
109 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
110 const wxPoint
&pos
, const wxSize
&size
)
111 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
| wxTAB_TRAVERSAL
, "test canvas" )
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) );
127 m_button
= new wxButton( this, ID_QUERYPOS
, "Query position", wxPoint(10,110) );
129 (void) new wxTextCtrl( this, -1, "wxTextCtrl", wxPoint(10,150), wxSize(80,-1) );
131 (void) new wxRadioButton( this, -1, "Disable", wxPoint(10,190) );
133 (void) new wxComboBox( this, -1, "This", wxPoint(10,230), wxDefaultSize
, 5, choices
);
135 (void) new wxRadioBox( this, -1, "This", wxPoint(10,310), wxDefaultSize
, 5, choices
, 2, wxRA_SPECIFY_COLS
);
137 (void) new wxRadioBox( this, -1, "This", wxPoint(10,440), wxDefaultSize
, 5, choices
, 2, wxRA_SPECIFY_ROWS
);
139 wxListCtrl
*m_listCtrl
= new wxListCtrl(
140 this, -1, wxPoint(200, 110), wxSize(180, 120),
141 wxLC_REPORT
| wxSIMPLE_BORDER
| wxLC_SINGLE_SEL
);
143 m_listCtrl
->InsertColumn(0, "First", wxLIST_FORMAT_LEFT
, 90);
144 m_listCtrl
->InsertColumn(1, "Last", wxLIST_FORMAT_LEFT
, 90);
146 for ( int i
=0; i
< 30; i
++)
149 sprintf(buf
, "Item %d", i
);
150 m_listCtrl
->InsertItem(i
, buf
);
152 m_listCtrl
->SetItemState( 3, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
154 (void) new wxListBox( this, -1, wxPoint(260,280), wxSize(120,120), 5, choices
, wxLB_ALWAYS_SB
);
156 wxPanel
*test
= new wxPanel( this, -1, wxPoint(10, 530), wxSize(130,120), wxSIMPLE_BORDER
| wxTAB_TRAVERSAL
);
157 test
->SetBackgroundColour( "WHEAT" );
158 wxButton
*test2
= new wxButton( test
, -1, "Hallo", wxPoint(10,10) );
160 test
= new wxPanel( this, -1, wxPoint(160, 530), wxSize(130,120), wxSUNKEN_BORDER
| wxTAB_TRAVERSAL
);
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
) );
166 test
= new wxPanel( this, -1, wxPoint(310, 530), wxSize(130,120), wxRAISED_BORDER
| wxTAB_TRAVERSAL
);
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
) );
172 SetBackgroundColour( "WHEAT" );
174 SetCursor( wxCursor( wxCURSOR_IBEAM
) );
177 MyCanvas::~MyCanvas()
181 void MyCanvas::OnMouseDown( wxMouseEvent
&event
)
183 if (event
.LeftDown())
185 wxPoint
pt( event
.GetPosition() );
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
);
191 if (event
.LeftIsDown() &&
194 wxLogMessage( "Error: both LeftDown() and LeftIsDown() are TRUE!" );
198 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
200 wxPaintDC
dc( this );
203 dc
.DrawText( "Press mouse button to test calculations!", 160, 50 );
205 dc
.DrawText( "Some text", 140, 140 );
207 dc
.DrawRectangle( 100, 160, 200, 200 );
210 void MyCanvas::OnQueryPosition( wxCommandEvent
&WXUNUSED(event
) )
212 wxPoint
pt( m_button
->GetPosition() );
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
);
218 void MyCanvas::OnAddButton( wxCommandEvent
&WXUNUSED(event
) )
220 wxLogMessage( "Inserting button at position 10,70..." );
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
);
226 void MyCanvas::OnDeleteButton( wxCommandEvent
&event
)
228 wxLogMessage( "Deleting button inserted with ""Add button""..." );
229 wxWindow
*win
= FindWindow( ID_NEWBUTTON
);
233 wxLogMessage( "-> No window with id = ID_NEWBUTTON found." );
236 void MyCanvas::OnMoveButton( wxCommandEvent
&event
)
238 wxLogMessage( "Moving button 10 pixels downward.." );
239 wxWindow
*win
= FindWindow( event
.GetId() );
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
);
247 void MyCanvas::OnScrollWin( wxCommandEvent
&WXUNUSED(event
) )
249 wxLogMessage( "Scrolling 2 units up.\nThe white square and the controls should move equally!" );
255 void MyCanvas::OnScroll( wxScrollWinEvent
&event
)
257 if (( event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) &&
258 ( !event
.IsScrolling() ))
260 wxLogMessage( "Thumb released; position: %u", event
.GetPosition() );
267 const int ID_QUIT
= 108;
268 const int ID_ABOUT
= 109;
270 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
272 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
273 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
274 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
278 : wxFrame( (wxFrame
*)NULL
, -1, "wxScrolledWindow sample",
279 wxPoint(20,20), wxSize(470,500) )
281 wxMenu
*file_menu
= new wxMenu();
282 file_menu
->Append( ID_ABOUT
, "&About..");
283 file_menu
->Append( ID_QUIT
, "E&xit\tAlt-X");
285 wxMenuBar
*menu_bar
= new wxMenuBar();
286 menu_bar
->Append(file_menu
, "&File");
288 SetMenuBar( menu_bar
);
291 int widths
[] = { -1, 100 };
292 SetStatusWidths( 2, widths
);
294 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(100,100) );
295 m_canvas
->SetScrollbars( 10, 10, 50, 100 );
297 m_log
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE
);
298 wxLog
*old_log
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
301 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
303 topsizer
->Add( m_canvas
, 1, wxEXPAND
);
304 topsizer
->Add( m_log
, 0, wxEXPAND
);
306 SetAutoLayout( TRUE
);
307 SetSizer( topsizer
);
310 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
315 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
317 (void)wxMessageBox( "wxScroll demo\n"
318 "Robert Roebling (c) 1998",
319 "About wxScroll Demo", wxICON_INFORMATION
| wxOK
);
322 //-----------------------------------------------------------------------------
324 //-----------------------------------------------------------------------------
328 wxFrame
*frame
= new MyFrame();