]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/plot.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxPlotWindow
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "plot.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #include "wx/object.h"
26 #include "wx/colour.h"
27 #include "wx/settings.h"
31 #include "wx/dcclient.h"
34 #include "wx/generic/plot.h"
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 IMPLEMENT_ABSTRACT_CLASS(wxPlotCurve
, wxObject
)
42 wxPlotCurve::wxPlotCurve( int offsetY
)
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 IMPLEMENT_DYNAMIC_CLASS(wxPlotArea
, wxWindow
)
53 BEGIN_EVENT_TABLE(wxPlotArea
, wxWindow
)
54 EVT_PAINT( wxPlotArea::OnPaint
)
55 EVT_LEFT_DOWN( wxPlotArea::OnMouse
)
58 wxPlotArea::wxPlotArea( wxPlotWindow
*parent
)
59 : wxWindow( parent
, -1, wxDefaultPosition
, wxDefaultSize
, wxSIMPLE_BORDER
, "plotarea" )
63 SetBackgroundColour( *wxWHITE
);
66 void wxPlotArea::OnMouse( wxMouseEvent
&event
)
70 GetClientSize( &client_width
, &client_height
);
73 m_owner
->GetViewStart( &view_x
, &view_y
);
82 wxNode
*node
= m_owner
->m_curves
.First();
85 wxPlotCurve
*curve
= (wxPlotCurve
*)node
->Data();
87 wxCoord offset_y
= client_height
- curve
->GetOffsetY();
89 double dy
= curve
->GetY( x
);
90 int curve_y
= (wxCoord
)(-dy
* 100) + offset_y
- 1;
91 if ((y
-curve_y
< 4) && (y
-curve_y
> -4))
93 m_owner
->SetCurrent( curve
);
101 void wxPlotArea::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
105 GetClientSize( &client_width
, &client_height
);
108 m_owner
->GetViewStart( &view_x
, &view_y
);
112 wxPaintDC
dc( this );
113 m_owner
->PrepareDC( dc
);
115 wxRegionIterator
upd( GetUpdateRegion() );
119 int update_x
= upd
.GetX();
120 int update_y
= upd
.GetY();
121 int update_width
= upd
.GetWidth();
126 if (m_owner
->m_current
)
128 dc
.SetPen( *wxLIGHT_GREY_PEN
);
129 int base_line
= client_height
- m_owner
->m_current
->GetOffsetY();
130 dc
.DrawLine( update_x
-1, base_line
-1, update_x
+update_width
+2, base_line
-1 );
133 wxNode
*node
= m_owner
->m_curves
.First();
136 wxPlotCurve
*curve
= (wxPlotCurve
*)node
->Data();
138 if (curve
== m_owner
->GetCurrent())
139 dc
.SetPen( *wxBLACK_PEN
);
141 dc
.SetPen( *wxLIGHT_GREY_PEN
);
142 wxCoord offset_y
= client_height
- curve
->GetOffsetY();
144 int start_x
= wxMax( update_x
-1, curve
->GetStartX() );
145 int end_x
= wxMin( update_x
+update_width
+2, curve
->GetEndX() );
147 wxCoord y
=0,last_y
=0;
148 for (int x
= start_x
; x
< end_x
; x
++)
150 double dy
= curve
->GetY( x
);
151 y
= (wxCoord
)(-dy
* 100) + offset_y
- 1;
154 dc
.DrawLine( x
-1, last_y
, x
, y
);
164 //-----------------------------------------------------------------------------
166 //-----------------------------------------------------------------------------
168 #define ID_ENLARGE_100 1000
169 #define ID_ENLARGE_50 1001
170 #define ID_SHRINK_33 1002
171 #define ID_SHRINK_50 1003
173 #define ID_MOVE_UP 1006
174 #define ID_MOVE_DOWN 1007
177 IMPLEMENT_DYNAMIC_CLASS(wxPlotWindow
, wxScrolledWindow
)
179 BEGIN_EVENT_TABLE(wxPlotWindow
, wxScrolledWindow
)
180 EVT_PAINT( wxPlotWindow::OnPaint
)
183 wxPlotWindow::wxPlotWindow( wxWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
, int flag
)
184 : wxScrolledWindow( parent
, id
, pos
, size
, flag
, "plotcanvas" )
186 m_area
= new wxPlotArea( this );
188 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxHORIZONTAL
);
190 wxBoxSizer
*buttonlist
= new wxBoxSizer( wxVERTICAL
);
191 buttonlist
->Add( new wxButton( this, ID_ENLARGE_100
, _("+ 100%") ), 0, wxEXPAND
|wxALL
, 5 );
192 buttonlist
->Add( new wxButton( this, ID_ENLARGE_50
, _("+ 50%") ), 0, wxEXPAND
|wxALL
, 5 );
193 buttonlist
->Add( new wxButton( this, ID_SHRINK_33
, _("- 33%") ), 0, wxEXPAND
|wxALL
, 5 );
194 buttonlist
->Add( new wxButton( this, ID_SHRINK_50
, _("- 50%") ), 0, wxEXPAND
|wxALL
, 5 );
195 buttonlist
->Add( 20,20, 0 );
196 buttonlist
->Add( new wxButton( this, ID_MOVE_UP
, _("Up") ), 0, wxEXPAND
|wxALL
, 5 );
197 buttonlist
->Add( new wxButton( this, ID_MOVE_DOWN
, _("Down") ), 0, wxEXPAND
|wxALL
, 5 );
198 buttonlist
->Add( 20,20, 1 );
200 mainsizer
->Add( buttonlist
, 0, wxEXPAND
);
202 mainsizer
->Add( m_area
, 1, wxEXPAND
|wxLEFT
, 50 );
204 SetAutoLayout( TRUE
);
205 SetSizer( mainsizer
);
207 SetTargetWindow( m_area
);
209 SetBackgroundColour( *wxWHITE
);
211 m_current
= (wxPlotCurve
*) NULL
;
214 wxPlotWindow::~wxPlotWindow()
218 void wxPlotWindow::Add( wxPlotCurve
*curve
)
220 m_curves
.Append( curve
);
221 if (!m_current
) m_current
= curve
;
224 size_t wxPlotWindow::GetCount()
226 return m_curves
.GetCount();
229 wxPlotCurve
*wxPlotWindow::GetAt( size_t n
)
231 wxNode
*node
= m_curves
.Nth( n
);
233 return (wxPlotCurve
*) NULL
;
235 return (wxPlotCurve
*) node
->Data();
238 void wxPlotWindow::SetCurrent( wxPlotCurve
* current
)
241 m_area
->Refresh( TRUE
);
243 wxPoint
pos( m_area
->GetPosition() );
247 GetClientSize( &client_width
, &client_height
);
248 wxRect
rect(pos
.x
-40,0,40,client_height
);
252 wxPlotCurve
*wxPlotWindow::GetCurrent()
257 void wxPlotWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
259 wxPaintDC
dc( this );
261 if (!m_current
) return;
265 GetClientSize( &client_width
, &client_height
);
267 dc
.SetPen( *wxBLACK_PEN
);
269 wxPoint
pos( m_area
->GetPosition() );
271 dc
.DrawLine( pos
.x
-15, 5, pos
.x
-15, client_height
-5 );
272 dc
.DrawLine( pos
.x
-19, 9, pos
.x
-15, 5 );
273 dc
.DrawLine( pos
.x
-10, 10, pos
.x
-15, 5 );
275 int y
= client_height
- m_current
->GetOffsetY() - 1;
276 dc
.DrawLine( pos
.x
-15, y
, pos
.x
-7, y
);