]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/plot/plot.cpp
glibc's vswprintf doesn't nul terminate on truncation.
[wxWidgets.git] / contrib / samples / plot / plot.cpp
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 /////////////////////////////////////////////////////////////////////////////
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
23 #include "wx/plot/plot.h"
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"
30 #include "wx/math.h"
31
32 // derived classes
33
34 class MyPlotCurve;
35 class MyFrame;
36 class MyApp;
37
38 // MyPlotCurve
39
40 class MyPlotCurve: public wxPlotCurve
41 {
42 public:
43 MyPlotCurve( int offsetY, double startY, double endY ) :
44 wxPlotCurve( offsetY, startY, endY ) {}
45
46 virtual wxInt32 GetStartX()
47 { return 0; }
48 virtual wxInt32 GetEndX()
49 { return 7000; }
50
51 virtual double GetY( wxInt32 x )
52 {
53 double dx = x;
54 dx /= 100;
55 return sin( dx );
56 }
57 };
58
59 // MyFrame
60
61 class MyFrame: public wxFrame
62 {
63 public:
64 MyFrame();
65
66 void OnAbout( wxCommandEvent &event );
67 void OnQuit( wxCommandEvent &event );
68
69 void OnPlotClick( wxPlotEvent &event );
70 void OnPlotDClick( wxPlotEvent &event );
71
72 private:
73
74 wxPlotWindow *m_plot;
75 #if wxUSE_LOG
76 wxTextCtrl *m_log;
77 #endif // wxUSE_LOG
78
79 DECLARE_DYNAMIC_CLASS(MyFrame)
80 DECLARE_EVENT_TABLE()
81 };
82
83 // MyApp
84
85 class MyApp: public wxApp
86 {
87 public:
88 virtual bool OnInit();
89 };
90
91 // main program
92
93 IMPLEMENT_APP(MyApp)
94
95 // MyFrame
96
97 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
98
99 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
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)
104 END_EVENT_TABLE()
105
106 MyFrame::MyFrame()
107 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxPlotWindow sample"),
108 wxPoint(20,20), wxSize(470,500) )
109 {
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"));
115
116 wxMenuBar *menuBar = new wxMenuBar();
117 menuBar->Append(fileMenu, _T("&File"));
118 menuBar->Append(helpMenu, _T("&Help"));
119
120 SetMenuBar( menuBar );
121
122 #if wxUSE_STATUSBAR
123 CreateStatusBar(2);
124 int widths[] = { -1, 100 };
125 SetStatusWidths( 2, widths );
126 #endif // wxUSE_STATUSBAR
127
128 m_plot = new wxPlotWindow( this, wxID_ANY, wxPoint(0,0), wxSize(100,100), wxSUNKEN_BORDER | wxPLOT_DEFAULT );
129 m_plot->SetUnitsPerValue( 0.01 );
130 // m_plot->SetScrollOnThumbRelease( true );
131
132 //Add a blue, 16pt chart title
133 wxString titleText( _T("The Chart Title") );
134 wxFont titleFont( *wxNORMAL_FONT );
135 titleFont.SetPointSize( 16 );
136 wxColour titleColour( *wxBLUE );
137 m_plot->AddChartTitle( titleText, titleFont, titleColour );
138
139 m_plot->Add( new MyPlotCurve( 0, -1.5, 1.5 ) );
140 m_plot->Add( new MyPlotCurve( 50, -1.5, 1.5 ) );
141 wxPlotOnOffCurve *oo = new wxPlotOnOffCurve( 10 );
142 oo->Add( 10, 20 );
143 oo->Add( 25, 30 );
144 oo->Add( 100, 400 );
145 oo->Add( 1000, 2000 );
146 m_plot->Add( oo );
147
148 #if wxUSE_LOG
149 m_log = new wxTextCtrl( this, wxID_ANY, _T("This is the log window.\n"), wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE );
150 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
151 delete old_log;
152 #endif // wxUSE_LOG
153
154 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
155
156 topsizer->Add( m_plot, 1, wxEXPAND );
157 #if wxUSE_LOG
158 topsizer->Add( m_log, 0, wxEXPAND );
159 #endif // wxUSE_LOG
160
161 SetAutoLayout( true );
162 SetSizer( topsizer );
163 topsizer->Fit(this);
164 topsizer->SetSizeHints(this);
165 }
166
167 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
168 {
169 Close( true );
170 }
171
172 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
173 {
174 (void)wxMessageBox( _T("wxPlotWindow Demo\n")
175 _T("Robert Roebling (c) 1999,2000"),
176 _T("About wxPlotWindow Demo"), wxICON_INFORMATION | wxOK );
177 }
178
179 void MyFrame::OnPlotClick( wxPlotEvent &event )
180 {
181 double x = event.GetPosition() * m_plot->GetUnitsPerValue();
182 double y = event.GetCurve()->GetY( event.GetPosition() );
183 wxLogMessage( _T("Clicked on curve at x coordinate: %f, value: %f"), x, y );
184 }
185
186 void MyFrame::OnPlotDClick( wxPlotEvent &event )
187 {
188 double x = event.GetPosition() * m_plot->GetUnitsPerValue();
189 double y = event.GetCurve()->GetY( event.GetPosition() );
190 wxLogMessage( _T("Double clicked on curve at x coordinate: %f, value: %f"), x, y );
191 }
192
193 //-----------------------------------------------------------------------------
194 // MyApp
195 //-----------------------------------------------------------------------------
196
197 bool MyApp::OnInit()
198 {
199 wxFrame *frame = new MyFrame();
200 frame->Show( true );
201
202 return true;
203 }
204