]> git.saurik.com Git - wxWidgets.git/blame - samples/plot/plot.cpp
Added SetSelectionMode
[wxWidgets.git] / samples / plot / plot.cpp
CommitLineData
981b2508
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
21#include "wx/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
33class MyPlotCurve;
34class MyFrame;
35class MyApp;
36
37// MyPlotCurve
38
39class MyPlotCurve: public wxPlotCurve
40{
41public:
846e1424
RR
42 MyPlotCurve( int offsetY, double startY, double endY ) :
43 wxPlotCurve( offsetY, startY, endY ) {}
981b2508
RR
44
45 virtual wxInt32 GetStartX()
46 { return 0; }
47 virtual wxInt32 GetEndX()
279ababf 48 { return 7000; }
981b2508
RR
49
50 virtual double GetY( wxInt32 x )
51 {
52 double dx = x;
53 dx /= 100;
846e1424 54 return sin( dx );
981b2508
RR
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 );
279ababf
RR
67
68 void OnPlotClick( wxPlotEvent &event );
69 void OnPlotDClick( wxPlotEvent &event );
981b2508
RR
70
71 wxPlotWindow *m_plot;
72 wxTextCtrl *m_log;
73
74private:
75 DECLARE_DYNAMIC_CLASS(MyFrame)
76 DECLARE_EVENT_TABLE()
77};
78
79// MyApp
80
81class MyApp: public wxApp
82{
83public:
84 virtual bool OnInit();
85};
86
87// main program
88
89IMPLEMENT_APP(MyApp)
90
91// MyFrame
92
93const int ID_QUIT = 108;
94const int ID_ABOUT = 109;
95
96IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
97
98BEGIN_EVENT_TABLE(MyFrame,wxFrame)
279ababf
RR
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)
981b2508
RR
103END_EVENT_TABLE()
104
105MyFrame::MyFrame()
846e1424 106 : wxFrame( (wxFrame *)NULL, -1, "wxPlotWindow sample",
981b2508
RR
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
279ababf
RR
122 m_plot = new wxPlotWindow( this, -1, wxPoint(0,0), wxSize(100,100), wxSUNKEN_BORDER | wxPLOT_DEFAULT );
123 m_plot->SetUnitsPerValue( 0.01 );
846e1424
RR
124
125 m_plot->Add( new MyPlotCurve( 0, -1.5, 1.5 ) );
126 m_plot->Add( new MyPlotCurve( 50, -1.5, 1.5 ) );
b480710b
RR
127 wxPlotOnOffCurve *oo = new wxPlotOnOffCurve( 10 );
128 oo->Add( 10, 20 );
129 oo->Add( 25, 30 );
130 oo->Add( 100, 400 );
131 oo->Add( 1000, 2000 );
132 m_plot->Add( oo );
981b2508
RR
133
134 m_log = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE );
135 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
136 delete old_log;
137
138 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
139
140 topsizer->Add( m_plot, 1, wxEXPAND );
141 topsizer->Add( m_log, 0, wxEXPAND );
142
143 SetAutoLayout( TRUE );
144 SetSizer( topsizer );
145}
146
147void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
148{
149 Close( TRUE );
150}
151
152void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
153{
846e1424
RR
154 (void)wxMessageBox( "wxPlotWindow Demo\n"
155 "Robert Roebling (c) 1999,2000",
156 "About wxPlotWindow Demo", wxICON_INFORMATION | wxOK );
981b2508
RR
157}
158
279ababf
RR
159void MyFrame::OnPlotClick( wxPlotEvent &event )
160{
161 double x = event.GetPosition() * m_plot->GetUnitsPerValue();
162 double y = event.GetCurve()->GetY( event.GetPosition() );
163 wxLogMessage( "Clicked on curve at x coordinate: %f, value: %f", x, y );
164}
165
166void MyFrame::OnPlotDClick( wxPlotEvent &event )
167{
168 double x = event.GetPosition() * m_plot->GetUnitsPerValue();
169 double y = event.GetCurve()->GetY( event.GetPosition() );
170 wxLogMessage( "Double clicked on curve at x coordinate: %f, value: %f", x, y );
171}
172
981b2508
RR
173//-----------------------------------------------------------------------------
174// MyApp
175//-----------------------------------------------------------------------------
176
177bool MyApp::OnInit()
178{
179 wxFrame *frame = new MyFrame();
180 frame->Show( TRUE );
181
182 return TRUE;
183}
184