2003-05-30 17:08:31 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2005-10-27 12:01:45 -04:00
|
|
|
// Name: samples/vscroll/vstest.cpp
|
2004-05-25 07:20:37 -04:00
|
|
|
// Purpose: VScroll wxWidgets sample
|
2003-05-30 17:08:31 -04:00
|
|
|
// Author: Vadim Zeitlin
|
2006-04-14 18:03:07 -04:00
|
|
|
// Modified by:
|
2003-05-30 17:08:31 -04:00
|
|
|
// Created: 04/01/98
|
|
|
|
// RCS-ID: $Id$
|
2004-05-25 07:20:37 -04:00
|
|
|
// Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
|
2003-05-30 17:08:31 -04:00
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// for all others, include the necessary headers (this file is usually all you
|
2004-05-25 07:20:37 -04:00
|
|
|
// need because it includes almost all "standard" wxWidgets headers)
|
2003-05-30 17:08:31 -04:00
|
|
|
#ifndef WX_PRECOMP
|
2003-12-11 06:25:46 -05:00
|
|
|
#include "wx/wx.h"
|
2003-05-30 17:08:31 -04:00
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/frame.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// we need to include the headers not included from wx/wx.h explicitly anyhow
|
|
|
|
#include "wx/vscroll.h"
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// resources
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// the application icon (under Windows and OS/2 it is in resources)
|
2005-10-27 12:01:45 -04:00
|
|
|
#if !defined(__WXMSW__) && !defined(__WXPM__)
|
2005-03-01 12:11:15 -05:00
|
|
|
#include "../sample.xpm"
|
2003-05-30 17:08:31 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// private classes
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Define a new application type, each program should derive a class from wxApp
|
2006-04-14 18:03:07 -04:00
|
|
|
class VScrollApp : public wxApp
|
2003-05-30 17:08:31 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// create our main window
|
|
|
|
virtual bool OnInit();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Define a new frame type: this is going to be our main frame
|
2006-04-14 18:03:07 -04:00
|
|
|
class VScrollFrame : public wxFrame
|
2003-05-30 17:08:31 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// ctor
|
2006-04-14 18:03:07 -04:00
|
|
|
VScrollFrame();
|
2003-05-30 17:08:31 -04:00
|
|
|
|
|
|
|
// event handlers (these functions should _not_ be virtual)
|
|
|
|
void OnQuit(wxCommandEvent& event);
|
|
|
|
void OnAbout(wxCommandEvent& event);
|
|
|
|
|
|
|
|
void OnSize(wxSizeEvent& event)
|
|
|
|
{
|
|
|
|
// show current size in the status bar
|
|
|
|
#if wxUSE_STATUSBAR
|
|
|
|
if ( m_frameStatusBar )
|
|
|
|
{
|
|
|
|
wxSize sz = GetClientSize();
|
2003-09-09 16:24:34 -04:00
|
|
|
SetStatusText(wxString::Format(_T("%dx%d"), sz.x, sz.y), 1);
|
2003-05-30 17:08:31 -04:00
|
|
|
}
|
|
|
|
#endif // wxUSE_STATUSBAR
|
|
|
|
|
|
|
|
event.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2004-05-25 07:20:37 -04:00
|
|
|
// any class wishing to process wxWidgets events must use this macro
|
2003-05-30 17:08:31 -04:00
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
};
|
|
|
|
|
|
|
|
class VScrollWindow : public wxVScrolledWindow
|
|
|
|
{
|
|
|
|
public:
|
2004-05-31 06:43:22 -04:00
|
|
|
VScrollWindow(wxFrame *frame) : wxVScrolledWindow(frame, wxID_ANY)
|
2003-05-30 17:08:31 -04:00
|
|
|
{
|
|
|
|
m_frame = frame;
|
|
|
|
|
2006-04-14 18:03:07 -04:00
|
|
|
SetLineCount(200);
|
2003-05-30 17:08:31 -04:00
|
|
|
|
|
|
|
m_changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnIdle(wxIdleEvent&)
|
|
|
|
{
|
2004-07-19 11:36:01 -04:00
|
|
|
#if wxUSE_STATUSBAR
|
2003-05-30 17:08:31 -04:00
|
|
|
m_frame->SetStatusText(wxString::Format
|
|
|
|
(
|
2003-09-09 16:24:34 -04:00
|
|
|
_T("Page size = %d, pos = %d, max = %d"),
|
2003-05-30 17:08:31 -04:00
|
|
|
GetScrollThumb(wxVERTICAL),
|
|
|
|
GetScrollPos(wxVERTICAL),
|
|
|
|
GetScrollRange(wxVERTICAL)
|
|
|
|
));
|
2004-07-19 11:36:01 -04:00
|
|
|
#endif // wxUSE_STATUSBAR
|
2003-05-30 17:08:31 -04:00
|
|
|
m_changed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnPaint(wxPaintEvent&)
|
|
|
|
{
|
|
|
|
wxPaintDC dc(this);
|
|
|
|
|
2006-04-14 18:03:07 -04:00
|
|
|
dc.SetPen(*wxBLACK_DASHED_PEN);
|
2003-05-30 17:08:31 -04:00
|
|
|
|
2006-04-14 18:03:07 -04:00
|
|
|
const size_t lineFirst = GetFirstVisibleLine(),
|
|
|
|
lineLast = GetLastVisibleLine();
|
2003-05-30 17:08:31 -04:00
|
|
|
|
|
|
|
const wxCoord hText = dc.GetCharHeight();
|
|
|
|
|
|
|
|
wxCoord y = 0;
|
2006-04-14 18:03:07 -04:00
|
|
|
for ( size_t line = lineFirst; line <= lineLast; line++ )
|
2003-05-30 17:08:31 -04:00
|
|
|
{
|
2006-04-14 18:03:07 -04:00
|
|
|
dc.DrawLine(0, y, 1000, y);
|
2003-05-30 17:08:31 -04:00
|
|
|
|
|
|
|
wxCoord hLine = OnGetLineHeight(line);
|
|
|
|
dc.DrawText(wxString::Format(_T("Line %lu"), (unsigned long)line),
|
2006-04-14 18:03:07 -04:00
|
|
|
0, y + (hLine - hText) / 2);
|
2003-05-30 17:08:31 -04:00
|
|
|
|
|
|
|
y += hLine;
|
|
|
|
dc.DrawLine(0, y, 1000, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnScroll(wxScrollWinEvent& event)
|
|
|
|
{
|
|
|
|
m_changed = true;
|
|
|
|
|
|
|
|
event.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual wxCoord OnGetLineHeight(size_t n) const
|
|
|
|
{
|
|
|
|
wxASSERT( n < GetLineCount() );
|
|
|
|
|
2006-04-14 18:03:07 -04:00
|
|
|
return n % 2 ? 15 : 30; // 15 + 2*n
|
2003-05-30 17:08:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxFrame *m_frame;
|
|
|
|
|
|
|
|
bool m_changed;
|
|
|
|
|
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
};
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(VScrollWindow, wxVScrolledWindow)
|
|
|
|
EVT_IDLE(VScrollWindow::OnIdle)
|
|
|
|
EVT_PAINT(VScrollWindow::OnPaint)
|
|
|
|
EVT_SCROLLWIN(VScrollWindow::OnScroll)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// constants
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// IDs for the controls and the menu commands
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
// menu items
|
2005-06-02 08:04:48 -04:00
|
|
|
VScroll_Quit = wxID_EXIT,
|
2003-05-30 17:08:31 -04:00
|
|
|
|
|
|
|
// it is important for the id corresponding to the "About" command to have
|
|
|
|
// this standard value as otherwise it won't be handled properly under Mac
|
|
|
|
// (where it is special and put into the "Apple" menu)
|
2006-04-14 18:03:07 -04:00
|
|
|
VScroll_About = wxID_ABOUT
|
2003-05-30 17:08:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2004-05-25 07:20:37 -04:00
|
|
|
// event tables and other macros for wxWidgets
|
2003-05-30 17:08:31 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2004-05-25 07:20:37 -04:00
|
|
|
// the event tables connect the wxWidgets events with the functions (event
|
2003-05-30 17:08:31 -04:00
|
|
|
// handlers) which process them. It can be also done at run-time, but for the
|
|
|
|
// simple menu events like this the static method is much simpler.
|
2006-04-14 18:03:07 -04:00
|
|
|
BEGIN_EVENT_TABLE(VScrollFrame, wxFrame)
|
|
|
|
EVT_MENU(VScroll_Quit, VScrollFrame::OnQuit)
|
|
|
|
EVT_MENU(VScroll_About, VScrollFrame::OnAbout)
|
|
|
|
EVT_SIZE(VScrollFrame::OnSize)
|
2003-05-30 17:08:31 -04:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
2004-05-25 07:20:37 -04:00
|
|
|
// Create a new application object: this macro will allow wxWidgets to create
|
2003-05-30 17:08:31 -04:00
|
|
|
// the application object during program execution (it's better than using a
|
|
|
|
// static object for many reasons) and also declares the accessor function
|
2006-04-14 18:03:07 -04:00
|
|
|
// wxGetApp() which will return the reference of the right type (i.e. VScrollApp and
|
2003-05-30 17:08:31 -04:00
|
|
|
// not wxApp)
|
2006-04-14 18:03:07 -04:00
|
|
|
IMPLEMENT_APP(VScrollApp)
|
2003-05-30 17:08:31 -04:00
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// the application class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// 'Main program' equivalent: the program execution "starts" here
|
2006-04-14 18:03:07 -04:00
|
|
|
bool VScrollApp::OnInit()
|
2003-05-30 17:08:31 -04:00
|
|
|
{
|
|
|
|
// create the main application window
|
2006-04-14 18:03:07 -04:00
|
|
|
VScrollFrame *frame = new VScrollFrame;
|
2003-05-30 17:08:31 -04:00
|
|
|
|
|
|
|
// and show it (the frames, unlike simple controls, are not shown when
|
|
|
|
// created initially)
|
2004-05-31 06:43:22 -04:00
|
|
|
frame->Show(true);
|
2003-05-30 17:08:31 -04:00
|
|
|
|
|
|
|
// ok
|
2004-05-31 06:43:22 -04:00
|
|
|
return true;
|
2003-05-30 17:08:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// main frame
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// frame constructor
|
2006-04-14 18:03:07 -04:00
|
|
|
VScrollFrame::VScrollFrame()
|
|
|
|
: wxFrame(NULL,
|
|
|
|
wxID_ANY,
|
|
|
|
_T("VScroll wxWidgets Sample"),
|
|
|
|
wxDefaultPosition,
|
|
|
|
wxSize(400, 350))
|
2003-05-30 17:08:31 -04:00
|
|
|
{
|
|
|
|
// set the frame icon
|
2005-03-01 12:11:15 -05:00
|
|
|
SetIcon(wxICON(sample));
|
2003-05-30 17:08:31 -04:00
|
|
|
|
|
|
|
#if wxUSE_MENUS
|
|
|
|
// create a menu bar
|
|
|
|
wxMenu *menuFile = new wxMenu;
|
|
|
|
|
|
|
|
// the "About" item should be in the help menu
|
|
|
|
wxMenu *menuHelp = new wxMenu;
|
|
|
|
menuHelp->Append(VScroll_About, _T("&About...\tF1"), _T("Show about dialog"));
|
|
|
|
|
|
|
|
menuFile->Append(VScroll_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
|
|
|
|
|
|
|
|
// now append the freshly created menu to the menu bar...
|
|
|
|
wxMenuBar *menuBar = new wxMenuBar;
|
|
|
|
menuBar->Append(menuFile, _T("&File"));
|
|
|
|
menuBar->Append(menuHelp, _T("&Help"));
|
|
|
|
|
|
|
|
// ... and attach this menu bar to the frame
|
|
|
|
SetMenuBar(menuBar);
|
|
|
|
#endif // wxUSE_MENUS
|
|
|
|
|
|
|
|
#if wxUSE_STATUSBAR
|
|
|
|
// create a status bar just for fun (by default with 1 pane only)
|
|
|
|
CreateStatusBar(2);
|
2004-05-25 07:20:37 -04:00
|
|
|
SetStatusText(_T("Welcome to wxWidgets!"));
|
2003-05-30 17:08:31 -04:00
|
|
|
#endif // wxUSE_STATUSBAR
|
|
|
|
|
|
|
|
// create our one and only child -- it will take our entire client area
|
2006-04-14 18:03:07 -04:00
|
|
|
new VScrollWindow(this);
|
2003-05-30 17:08:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// event handlers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2006-04-14 18:03:07 -04:00
|
|
|
void VScrollFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
|
2003-05-30 17:08:31 -04:00
|
|
|
{
|
2004-05-31 06:43:22 -04:00
|
|
|
// true is to force the frame to close
|
|
|
|
Close(true);
|
2003-05-30 17:08:31 -04:00
|
|
|
}
|
|
|
|
|
2006-04-14 18:03:07 -04:00
|
|
|
void VScrollFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
|
2003-05-30 17:08:31 -04:00
|
|
|
{
|
|
|
|
wxMessageBox(_T("VScroll shows how to implement scrolling with\n")
|
2006-04-14 18:03:07 -04:00
|
|
|
_T("variable line heights.\n")
|
2004-10-02 08:36:02 -04:00
|
|
|
_T("(c) 2003 Vadim Zeitlin"),
|
2003-05-30 17:08:31 -04:00
|
|
|
_T("About VScroll"),
|
|
|
|
wxOK | wxICON_INFORMATION,
|
|
|
|
this);
|
|
|
|
}
|