]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/plot/plot.cpp
wxX11:
[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 wxTextCtrl *m_log;
73
74 private:
75 DECLARE_DYNAMIC_CLASS(MyFrame)
76 DECLARE_EVENT_TABLE()
77 };
78
79 // MyApp
80
81 class MyApp: public wxApp
82 {
83 public:
84 virtual bool OnInit();
85 };
86
87 // main program
88
89 IMPLEMENT_APP(MyApp)
90
91 // MyFrame
92
93 const int ID_QUIT = 108;
94 const int ID_ABOUT = 109;
95
96 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
97
98 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
99 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
100 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
101 EVT_PLOT_CLICKED ( -1, MyFrame::OnPlotClick)
102 EVT_PLOT_DOUBLECLICKED ( -1, MyFrame::OnPlotDClick)
103 END_EVENT_TABLE()
104
105 MyFrame::MyFrame()
106 : wxFrame( (wxFrame *)NULL, -1, "wxPlotWindow sample",
107 wxPoint(20,20), wxSize(470,500) )
108 {
109 wxMenu *file_menu = new wxMenu();
110 file_menu->Append( ID_ABOUT, "&About..");
111 file_menu->Append( ID_QUIT, "E&xit\tAlt-X");
112
113 wxMenuBar *menu_bar = new wxMenuBar();
114 menu_bar->Append(file_menu, "&File");
115
116 SetMenuBar( menu_bar );
117
118 CreateStatusBar(2);
119 int widths[] = { -1, 100 };
120 SetStatusWidths( 2, widths );
121
122 m_plot = new wxPlotWindow( this, -1, wxPoint(0,0), wxSize(100,100), wxSUNKEN_BORDER | wxPLOT_DEFAULT );
123 m_plot->SetUnitsPerValue( 0.01 );
124 // m_plot->SetScrollOnThumbRelease( TRUE );
125
126 m_plot->Add( new MyPlotCurve( 0, -1.5, 1.5 ) );
127 m_plot->Add( new MyPlotCurve( 50, -1.5, 1.5 ) );
128 wxPlotOnOffCurve *oo = new wxPlotOnOffCurve( 10 );
129 oo->Add( 10, 20 );
130 oo->Add( 25, 30 );
131 oo->Add( 100, 400 );
132 oo->Add( 1000, 2000 );
133 m_plot->Add( oo );
134
135 m_log = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE );
136 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
137 delete old_log;
138
139 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
140
141 topsizer->Add( m_plot, 1, wxEXPAND );
142 topsizer->Add( m_log, 0, wxEXPAND );
143
144 SetAutoLayout( TRUE );
145 SetSizer( topsizer );
146 }
147
148 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
149 {
150 Close( TRUE );
151 }
152
153 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
154 {
155 (void)wxMessageBox( "wxPlotWindow Demo\n"
156 "Robert Roebling (c) 1999,2000",
157 "About wxPlotWindow Demo", wxICON_INFORMATION | wxOK );
158 }
159
160 void MyFrame::OnPlotClick( wxPlotEvent &event )
161 {
162 double x = event.GetPosition() * m_plot->GetUnitsPerValue();
163 double y = event.GetCurve()->GetY( event.GetPosition() );
164 wxLogMessage( "Clicked on curve at x coordinate: %f, value: %f", x, y );
165 }
166
167 void MyFrame::OnPlotDClick( wxPlotEvent &event )
168 {
169 double x = event.GetPosition() * m_plot->GetUnitsPerValue();
170 double y = event.GetCurve()->GetY( event.GetPosition() );
171 wxLogMessage( "Double clicked on curve at x coordinate: %f, value: %f", x, y );
172 }
173
174 //-----------------------------------------------------------------------------
175 // MyApp
176 //-----------------------------------------------------------------------------
177
178 bool MyApp::OnInit()
179 {
180 wxFrame *frame = new MyFrame();
181 frame->Show( TRUE );
182
183 return TRUE;
184 }
185