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