]>
Commit | Line | Data |
---|---|---|
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 | 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 ); | |
140 | ||
141 | #if wxUSE_LOG | |
142 | m_log = new wxTextCtrl( this, wxID_ANY, _T("This is the log window.\n"), wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE ); | |
143 | wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) ); | |
144 | delete old_log; | |
145 | #endif // wxUSE_LOG | |
146 | ||
147 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); | |
148 | ||
149 | topsizer->Add( m_plot, 1, wxEXPAND ); | |
150 | #if wxUSE_LOG | |
151 | topsizer->Add( m_log, 0, wxEXPAND ); | |
152 | #endif // wxUSE_LOG | |
153 | ||
154 | SetAutoLayout( true ); | |
155 | SetSizer( topsizer ); | |
156 | topsizer->Fit(this); | |
157 | topsizer->SetSizeHints(this); | |
158 | } | |
159 | ||
160 | void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) | |
161 | { | |
162 | Close( true ); | |
163 | } | |
164 | ||
165 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) | |
166 | { | |
167 | (void)wxMessageBox( _T("wxPlotWindow Demo\n") | |
168 | _T("Robert Roebling (c) 1999,2000"), | |
169 | _T("About wxPlotWindow Demo"), wxICON_INFORMATION | wxOK ); | |
170 | } | |
171 | ||
172 | void MyFrame::OnPlotClick( wxPlotEvent &event ) | |
173 | { | |
174 | double x = event.GetPosition() * m_plot->GetUnitsPerValue(); | |
175 | double y = event.GetCurve()->GetY( event.GetPosition() ); | |
176 | wxLogMessage( _T("Clicked on curve at x coordinate: %f, value: %f"), x, y ); | |
177 | } | |
178 | ||
179 | void MyFrame::OnPlotDClick( wxPlotEvent &event ) | |
180 | { | |
181 | double x = event.GetPosition() * m_plot->GetUnitsPerValue(); | |
182 | double y = event.GetCurve()->GetY( event.GetPosition() ); | |
183 | wxLogMessage( _T("Double clicked on curve at x coordinate: %f, value: %f"), x, y ); | |
184 | } | |
185 | ||
186 | //----------------------------------------------------------------------------- | |
187 | // MyApp | |
188 | //----------------------------------------------------------------------------- | |
189 | ||
190 | bool MyApp::OnInit() | |
191 | { | |
192 | wxFrame *frame = new MyFrame(); | |
193 | frame->Show( true ); | |
194 | ||
195 | return true; | |
196 | } | |
197 |