]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/plot/plot.cpp
wxString and wxChar* access for file functions. Source cleaning.
[wxWidgets.git] / contrib / samples / plot / plot.cpp
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
21 #include "wx/plot/plot.h"
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
33 class MyPlotCurve;
34 class MyFrame;
35 class MyApp;
36
37 // MyPlotCurve
38
39 class MyPlotCurve: public wxPlotCurve
40 {
41 public:
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
60 class MyFrame: public wxFrame
61 {
62 public:
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;
72 #if wxUSE_LOG
73 wxTextCtrl *m_log;
74 #endif // wxUSE_LOG
75
76 private:
77 DECLARE_DYNAMIC_CLASS(MyFrame)
78 DECLARE_EVENT_TABLE()
79 };
80
81 // MyApp
82
83 class MyApp: public wxApp
84 {
85 public:
86 virtual bool OnInit();
87 };
88
89 // main program
90
91 IMPLEMENT_APP(MyApp)
92
93 // MyFrame
94
95 const int ID_QUIT = 108;
96 const int ID_ABOUT = 109;
97
98 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
99
100 BEGIN_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)
105 END_EVENT_TABLE()
106
107 MyFrame::MyFrame()
108 : wxFrame( (wxFrame *)NULL, -1, _T("wxPlotWindow sample"),
109 wxPoint(20,20), wxSize(470,500) )
110 {
111 wxMenu *file_menu = new wxMenu();
112 file_menu->Append( ID_ABOUT, _T("&About.."));
113 file_menu->Append( ID_QUIT, _T("E&xit\tAlt-X"));
114
115 wxMenuBar *menu_bar = new wxMenuBar();
116 menu_bar->Append(file_menu, _T("&File"));
117
118 SetMenuBar( menu_bar );
119
120 #if wxUSE_STATUSBAR
121 CreateStatusBar(2);
122 int widths[] = { -1, 100 };
123 SetStatusWidths( 2, widths );
124 #endif // wxUSE_STATUSBAR
125
126 m_plot = new wxPlotWindow( this, -1, wxPoint(0,0), wxSize(100,100), wxSUNKEN_BORDER | wxPLOT_DEFAULT );
127 m_plot->SetUnitsPerValue( 0.01 );
128 // m_plot->SetScrollOnThumbRelease( TRUE );
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
139 #if wxUSE_LOG
140 m_log = new wxTextCtrl( this, -1, _T("This is the log window.\n"), wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE );
141 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
142 delete old_log;
143 #endif // wxUSE_LOG
144
145 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
146
147 topsizer->Add( m_plot, 1, wxEXPAND );
148 #if wxUSE_LOG
149 topsizer->Add( m_log, 0, wxEXPAND );
150 #endif // wxUSE_LOG
151
152 SetAutoLayout( true );
153 SetSizer( topsizer );
154 }
155
156 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
157 {
158 Close( true );
159 }
160
161 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
162 {
163 (void)wxMessageBox( _T("wxPlotWindow Demo\n")
164 _T("Robert Roebling (c) 1999,2000"),
165 _T("About wxPlotWindow Demo"), wxICON_INFORMATION | wxOK );
166 }
167
168 void MyFrame::OnPlotClick( wxPlotEvent &event )
169 {
170 double x = event.GetPosition() * m_plot->GetUnitsPerValue();
171 double y = event.GetCurve()->GetY( event.GetPosition() );
172 wxLogMessage( _T("Clicked on curve at x coordinate: %f, value: %f"), x, y );
173 }
174
175 void MyFrame::OnPlotDClick( wxPlotEvent &event )
176 {
177 double x = event.GetPosition() * m_plot->GetUnitsPerValue();
178 double y = event.GetCurve()->GetY( event.GetPosition() );
179 wxLogMessage( _T("Double clicked on curve at x coordinate: %f, value: %f"), x, y );
180 }
181
182 //-----------------------------------------------------------------------------
183 // MyApp
184 //-----------------------------------------------------------------------------
185
186 bool MyApp::OnInit()
187 {
188 wxFrame *frame = new MyFrame();
189 frame->Show( TRUE );
190
191 return TRUE;
192 }
193