]> git.saurik.com Git - wxWidgets.git/blame - contrib/samples/plot/plot.cpp
Avoid warnings from mgl headers if possible.
[wxWidgets.git] / contrib / samples / plot / plot.cpp
CommitLineData
298a3f2e
WS
1/////////////////////////////////////////////////////////////////////////////
2// Name: plot.cpp
3// Purpose: wxPlotWindow sample
4// Author: Robert Roebling
5// Modified by:
6// Created:
7// Copyright: (C) 1999, Robert Roebling
8// RCS-ID: $Id$
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
8556b795
RR
11
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
21#endif
22
adb85931 23#include "wx/plot/plot.h"
8556b795
RR
24
25#include "wx/image.h"
26#include "wx/listctrl.h"
27#include "wx/sizer.h"
28#include "wx/log.h"
29#include "wx/intl.h"
298a3f2e 30#include "wx/math.h"
8556b795
RR
31
32// derived classes
33
34class MyPlotCurve;
35class MyFrame;
36class MyApp;
37
38// MyPlotCurve
39
40class MyPlotCurve: public wxPlotCurve
41{
42public:
298a3f2e 43 MyPlotCurve( int offsetY, double startY, double endY ) :
8556b795 44 wxPlotCurve( offsetY, startY, endY ) {}
298a3f2e 45
8556b795
RR
46 virtual wxInt32 GetStartX()
47 { return 0; }
48 virtual wxInt32 GetEndX()
49 { return 7000; }
298a3f2e 50
8556b795 51 virtual double GetY( wxInt32 x )
298a3f2e 52 {
8556b795
RR
53 double dx = x;
54 dx /= 100;
55 return sin( dx );
56 }
57};
58
59// MyFrame
60
61class MyFrame: public wxFrame
62{
63public:
64 MyFrame();
65
66 void OnAbout( wxCommandEvent &event );
67 void OnQuit( wxCommandEvent &event );
298a3f2e 68
8556b795
RR
69 void OnPlotClick( wxPlotEvent &event );
70 void OnPlotDClick( wxPlotEvent &event );
71
298a3f2e
WS
72private:
73
8556b795 74 wxPlotWindow *m_plot;
fa0d3447 75#if wxUSE_LOG
8556b795 76 wxTextCtrl *m_log;
fa0d3447 77#endif // wxUSE_LOG
8556b795 78
8556b795
RR
79 DECLARE_DYNAMIC_CLASS(MyFrame)
80 DECLARE_EVENT_TABLE()
81};
82
83// MyApp
84
85class MyApp: public wxApp
86{
87public:
88 virtual bool OnInit();
89};
90
91// main program
92
93IMPLEMENT_APP(MyApp)
94
95// MyFrame
96
8556b795
RR
97IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
98
99BEGIN_EVENT_TABLE(MyFrame,wxFrame)
298a3f2e
WS
100 EVT_MENU (wxID_ABOUT, MyFrame::OnAbout)
101 EVT_MENU (wxID_EXIT, MyFrame::OnQuit)
102 EVT_PLOT_CLICKED (wxID_ANY, MyFrame::OnPlotClick)
103 EVT_PLOT_DOUBLECLICKED (wxID_ANY, MyFrame::OnPlotDClick)
8556b795
RR
104END_EVENT_TABLE()
105
106MyFrame::MyFrame()
298a3f2e 107 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxPlotWindow sample"),
8556b795
RR
108 wxPoint(20,20), wxSize(470,500) )
109{
298a3f2e
WS
110 wxMenu *fileMenu = new wxMenu();
111 fileMenu->Append( wxID_EXIT, _T("E&xit\tAlt-X"), _T("Quit this program"));
112
113 wxMenu *helpMenu = new wxMenu;
114 helpMenu->Append(wxID_ABOUT, _T("&About...\tF1"), _T("Show about dialog"));
8556b795 115
298a3f2e
WS
116 wxMenuBar *menuBar = new wxMenuBar();
117 menuBar->Append(fileMenu, _T("&File"));
118 menuBar->Append(helpMenu, _T("&Help"));
8556b795 119
298a3f2e 120 SetMenuBar( menuBar );
8556b795 121
d96cdd4a 122#if wxUSE_STATUSBAR
8556b795
RR
123 CreateStatusBar(2);
124 int widths[] = { -1, 100 };
125 SetStatusWidths( 2, widths );
d96cdd4a 126#endif // wxUSE_STATUSBAR
8556b795 127
298a3f2e 128 m_plot = new wxPlotWindow( this, wxID_ANY, wxPoint(0,0), wxSize(100,100), wxSUNKEN_BORDER | wxPLOT_DEFAULT );
8556b795 129 m_plot->SetUnitsPerValue( 0.01 );
298a3f2e 130// m_plot->SetScrollOnThumbRelease( true );
8556b795
RR
131
132 m_plot->Add( new MyPlotCurve( 0, -1.5, 1.5 ) );
133 m_plot->Add( new MyPlotCurve( 50, -1.5, 1.5 ) );
134 wxPlotOnOffCurve *oo = new wxPlotOnOffCurve( 10 );
135 oo->Add( 10, 20 );
136 oo->Add( 25, 30 );
137 oo->Add( 100, 400 );
138 oo->Add( 1000, 2000 );
139 m_plot->Add( oo );
298a3f2e 140
fa0d3447 141#if wxUSE_LOG
298a3f2e 142 m_log = new wxTextCtrl( this, wxID_ANY, _T("This is the log window.\n"), wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE );
8556b795
RR
143 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
144 delete old_log;
fa0d3447 145#endif // wxUSE_LOG
298a3f2e 146
8556b795 147 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
298a3f2e 148
8556b795 149 topsizer->Add( m_plot, 1, wxEXPAND );
fa0d3447 150#if wxUSE_LOG
8556b795 151 topsizer->Add( m_log, 0, wxEXPAND );
fa0d3447 152#endif // wxUSE_LOG
8556b795 153
fa0d3447 154 SetAutoLayout( true );
8556b795 155 SetSizer( topsizer );
298a3f2e
WS
156 topsizer->Fit(this);
157 topsizer->SetSizeHints(this);
8556b795
RR
158}
159
160void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
161{
298a3f2e 162 Close( true );
8556b795
RR
163}
164
165void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
166{
298a3f2e
WS
167 (void)wxMessageBox( _T("wxPlotWindow Demo\n")
168 _T("Robert Roebling (c) 1999,2000"),
169 _T("About wxPlotWindow Demo"), wxICON_INFORMATION | wxOK );
8556b795
RR
170}
171
172void MyFrame::OnPlotClick( wxPlotEvent &event )
173{
174 double x = event.GetPosition() * m_plot->GetUnitsPerValue();
175 double y = event.GetCurve()->GetY( event.GetPosition() );
f7a8c129 176 wxLogMessage( _T("Clicked on curve at x coordinate: %f, value: %f"), x, y );
8556b795
RR
177}
178
179void MyFrame::OnPlotDClick( wxPlotEvent &event )
180{
181 double x = event.GetPosition() * m_plot->GetUnitsPerValue();
182 double y = event.GetCurve()->GetY( event.GetPosition() );
f7a8c129 183 wxLogMessage( _T("Double clicked on curve at x coordinate: %f, value: %f"), x, y );
8556b795
RR
184}
185
186//-----------------------------------------------------------------------------
187// MyApp
188//-----------------------------------------------------------------------------
189
190bool MyApp::OnInit()
191{
192 wxFrame *frame = new MyFrame();
298a3f2e 193 frame->Show( true );
8556b795 194
298a3f2e 195 return true;
8556b795
RR
196}
197