1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxPlotWindow
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
20 #include "wx/object.h"
22 #include "wx/colour.h"
23 #include "wx/settings.h"
27 #include "wx/dcclient.h"
28 #include "wx/stattext.h"
31 #include "wx/plot/plot.h"
32 #include "wx/bmpbuttn.h"
33 #include "wx/module.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 #if !defined(__WXMSW__) && !defined(__WXPM__)
42 #include "wx/plot/plot_enl.xpm"
43 #include "wx/plot/plot_shr.xpm"
44 #include "wx/plot/plot_zin.xpm"
45 #include "wx/plot/plot_zot.xpm"
46 #include "wx/plot/plot_up.xpm"
47 #include "wx/plot/plot_dwn.xpm"
50 //----------------------------------------------------------------------------
52 //----------------------------------------------------------------------------
54 DEFINE_EVENT_TYPE(wxEVT_PLOT_SEL_CHANGING
);
55 DEFINE_EVENT_TYPE(wxEVT_PLOT_SEL_CHANGED
);
56 DEFINE_EVENT_TYPE(wxEVT_PLOT_CLICKED
);
57 DEFINE_EVENT_TYPE(wxEVT_PLOT_DOUBLECLICKED
);
58 DEFINE_EVENT_TYPE(wxEVT_PLOT_ZOOM_IN
);
59 DEFINE_EVENT_TYPE(wxEVT_PLOT_ZOOM_OUT
);
60 DEFINE_EVENT_TYPE(wxEVT_PLOT_VALUE_SEL_CREATING
);
61 DEFINE_EVENT_TYPE(wxEVT_PLOT_VALUE_SEL_CREATED
);
62 DEFINE_EVENT_TYPE(wxEVT_PLOT_VALUE_SEL_CHANGING
);
63 DEFINE_EVENT_TYPE(wxEVT_PLOT_VALUE_SEL_CHANGED
);
64 DEFINE_EVENT_TYPE(wxEVT_PLOT_AREA_SEL_CREATING
);
65 DEFINE_EVENT_TYPE(wxEVT_PLOT_AREA_SEL_CREATED
);
66 DEFINE_EVENT_TYPE(wxEVT_PLOT_AREA_SEL_CHANGING
);
67 DEFINE_EVENT_TYPE(wxEVT_PLOT_AREA_SEL_CHANGED
);
68 DEFINE_EVENT_TYPE(wxEVT_PLOT_BEGIN_X_LABEL_EDIT
);
69 DEFINE_EVENT_TYPE(wxEVT_PLOT_END_X_LABEL_EDIT
);
70 DEFINE_EVENT_TYPE(wxEVT_PLOT_BEGIN_Y_LABEL_EDIT
);
71 DEFINE_EVENT_TYPE(wxEVT_PLOT_END_Y_LABEL_EDIT
);
72 DEFINE_EVENT_TYPE(wxEVT_PLOT_BEGIN_TITLE_EDIT
);
73 DEFINE_EVENT_TYPE(wxEVT_PLOT_END_TITLE_EDIT
);
74 DEFINE_EVENT_TYPE(wxEVT_PLOT_AREA_CREATE
);
76 //----------------------------------------------------------------------------
77 // accessor functions for the bitmaps (may return NULL, check for it!)
78 //----------------------------------------------------------------------------
80 static wxBitmap
*GetEnlargeBitmap();
81 static wxBitmap
*GetShrinkBitmap();
82 static wxBitmap
*GetZoomInBitmap();
83 static wxBitmap
*GetZoomOutBitmap();
84 static wxBitmap
*GetUpBitmap();
85 static wxBitmap
*GetDownBitmap();
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
91 #define wxPLOT_SCROLL_STEP 30
93 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
97 wxPlotEvent::wxPlotEvent( wxEventType commandType
, int id
)
98 : wxNotifyEvent( commandType
, id
)
100 m_curve
= (wxPlotCurve
*) NULL
;
105 //-----------------------------------------------------------------------------
107 //-----------------------------------------------------------------------------
109 IMPLEMENT_ABSTRACT_CLASS(wxPlotCurve
, wxObject
)
111 wxPlotCurve::wxPlotCurve( int offsetY
, double startY
, double endY
)
112 : m_penNormal(*wxGREY_PEN
), m_penSelected(*wxBLACK_PEN
)
119 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
123 IMPLEMENT_CLASS(wxPlotOnOffCurve
, wxObject
)
125 #include "wx/arrimpl.cpp"
126 WX_DEFINE_OBJARRAY(wxArrayPlotOnOff
);
128 wxPlotOnOffCurve::wxPlotOnOffCurve( int offsetY
)
135 void wxPlotOnOffCurve::Add( wxInt32 on
, wxInt32 off
, void *clientData
)
137 wxASSERT_MSG( on
> 0, _T("plot index < 0") );
138 wxASSERT( on
<= off
);
145 wxPlotOnOff
*v
= new wxPlotOnOff
;
148 v
->m_clientData
= clientData
;
152 size_t wxPlotOnOffCurve::GetCount()
154 return m_marks
.GetCount();
157 wxInt32
wxPlotOnOffCurve::GetOn( size_t index
)
159 wxPlotOnOff
*v
= &m_marks
.Item( index
);
163 wxInt32
wxPlotOnOffCurve::GetOff( size_t index
)
165 wxPlotOnOff
*v
= &m_marks
.Item( index
);
169 void* wxPlotOnOffCurve::GetClientData( size_t index
)
171 wxPlotOnOff
*v
= &m_marks
.Item( index
);
172 return v
->m_clientData
;
175 wxPlotOnOff
*wxPlotOnOffCurve::GetAt( size_t index
)
177 return &m_marks
.Item( index
);
180 void wxPlotOnOffCurve::DrawOnLine( wxDC
&dc
, wxCoord y
, wxCoord start
, wxCoord end
, void *WXUNUSED(clientData
) )
182 dc
.DrawLine( start
, y
, start
, y
-30 );
183 dc
.DrawLine( start
, y
-30, end
, y
-30 );
184 dc
.DrawLine( end
, y
-30, end
, y
);
187 void wxPlotOnOffCurve::DrawOffLine( wxDC
&dc
, wxCoord y
, wxCoord start
, wxCoord end
)
189 dc
.DrawLine( start
, y
, end
, y
);
192 //-----------------------------------------------------------------------------
194 //-----------------------------------------------------------------------------
196 IMPLEMENT_DYNAMIC_CLASS(wxPlotArea
, wxWindow
)
198 BEGIN_EVENT_TABLE(wxPlotArea
, wxWindow
)
199 EVT_PAINT( wxPlotArea::OnPaint
)
200 EVT_LEFT_DOWN( wxPlotArea::OnMouse
)
201 EVT_LEFT_DCLICK( wxPlotArea::OnMouse
)
204 wxPlotArea::wxPlotArea( wxPlotWindow
*parent
)
205 : wxWindow( parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxSIMPLE_BORDER
, _T("plotarea") )
211 SetBackgroundColour( *wxWHITE
);
214 void wxPlotArea::OnMouse( wxMouseEvent
&event
)
218 GetClientSize( &client_width
, &client_height
);
221 m_owner
->GetViewStart( &view_x
, &view_y
);
222 view_x
*= wxPLOT_SCROLL_STEP
;
223 view_y
*= wxPLOT_SCROLL_STEP
;
225 wxCoord x
= event
.GetX();
226 wxCoord y
= event
.GetY();
230 wxList::compatibility_iterator node
= m_owner
->m_curves
.GetFirst();
233 wxPlotCurve
*curve
= (wxPlotCurve
*)node
->GetData();
235 double double_client_height
= (double)client_height
;
236 double range
= curve
->GetEndY() - curve
->GetStartY();
237 double end
= curve
->GetEndY();
238 wxCoord offset_y
= curve
->GetOffsetY();
240 double dy
= (end
- curve
->GetY( (wxInt32
)(x
/m_owner
->GetZoom()) )) / range
;
241 wxCoord curve_y
= (wxCoord
)(dy
* double_client_height
) - offset_y
- 1;
243 if ((y
-curve_y
< 4) && (y
-curve_y
> -4))
245 wxPlotEvent
event1( event
.ButtonDClick() ? wxEVT_PLOT_DOUBLECLICKED
: wxEVT_PLOT_CLICKED
, m_owner
->GetId() );
246 event1
.SetEventObject( m_owner
);
247 event1
.SetZoom( m_owner
->GetZoom() );
248 event1
.SetCurve( curve
);
249 event1
.SetPosition( (int)floor(x
/m_owner
->GetZoom()) );
250 m_owner
->GetEventHandler()->ProcessEvent( event1
);
252 if (curve
!= m_owner
->GetCurrentCurve())
254 wxPlotEvent
event2( wxEVT_PLOT_SEL_CHANGING
, m_owner
->GetId() );
255 event2
.SetEventObject( m_owner
);
256 event2
.SetZoom( m_owner
->GetZoom() );
257 event2
.SetCurve( curve
);
258 if (!m_owner
->GetEventHandler()->ProcessEvent( event2
) || event2
.IsAllowed())
260 m_owner
->SetCurrentCurve( curve
);
266 node
= node
->GetNext();
270 void wxPlotArea::DeleteCurve( wxPlotCurve
*curve
, int from
, int to
)
273 m_owner
->PrepareDC( dc
);
274 dc
.SetPen( *wxWHITE_PEN
);
275 DrawCurve( &dc
, curve
, from
, to
);
278 void wxPlotArea::DrawCurve( wxDC
*dc
, wxPlotCurve
*curve
, int from
, int to
)
282 m_owner
->GetViewStart( &view_x
, &view_y
);
283 view_x
*= wxPLOT_SCROLL_STEP
;
290 GetClientSize( &client_width
, &client_height
);
293 to
= view_x
+ client_width
;
295 double zoom
= m_owner
->GetZoom();
297 int start_x
= wxMax( from
, (int)floor(curve
->GetStartX()*zoom
) );
298 int end_x
= wxMin( to
, (int)floor(curve
->GetEndX()*zoom
) );
300 start_x
= wxMax( view_x
, start_x
);
301 end_x
= wxMin( view_x
+ client_width
, end_x
);
305 double double_client_height
= (double)client_height
;
306 double range
= curve
->GetEndY() - curve
->GetStartY();
307 double end
= curve
->GetEndY();
308 wxCoord offset_y
= curve
->GetOffsetY();
311 for (int x
= start_x
; x
< end_x
; x
++)
313 double dy
= (end
- curve
->GetY( (wxInt32
)(x
/zoom
) )) / range
;
314 wxCoord y
= (wxCoord
)(dy
* double_client_height
) - offset_y
- 1;
317 dc
->DrawLine( x
-1, last_y
, x
, y
);
323 void wxPlotArea::DrawOnOffCurve( wxDC
*dc
, wxPlotOnOffCurve
*curve
, int from
, int to
)
327 m_owner
->GetViewStart( &view_x
, &view_y
);
328 view_x
*= wxPLOT_SCROLL_STEP
;
335 GetClientSize( &client_width
, &client_height
);
338 to
= view_x
+ client_width
;
340 double zoom
= m_owner
->GetZoom();
342 int start_x
= wxMax( from
, (int)floor(curve
->GetStartX()*zoom
) );
343 int end_x
= wxMin( to
, (int)floor(curve
->GetEndX()*zoom
) );
345 start_x
= wxMax( view_x
, start_x
);
346 end_x
= wxMin( view_x
+ client_width
, end_x
);
350 wxCoord offset_y
= curve
->GetOffsetY();
351 wxCoord last_off
= -5;
353 if (curve
->GetCount() == 0)
356 for (size_t index
= 0; index
< curve
->GetCount(); index
++)
358 wxPlotOnOff
*p
= curve
->GetAt( index
);
360 wxCoord on
= (wxCoord
)(p
->m_on
*zoom
);
361 wxCoord off
= (wxCoord
)(p
->m_off
*zoom
);
365 curve
->DrawOffLine( *dc
, client_height
-offset_y
, last_off
, on
);
371 curve
->DrawOffLine( *dc
, client_height
-offset_y
, last_off
, on
);
372 curve
->DrawOnLine( *dc
, client_height
-offset_y
, on
, off
, p
->m_clientData
);
377 wxPlotOnOff
*p
= curve
->GetAt( curve
->GetCount()-1 );
378 wxCoord off
= (wxCoord
)(p
->m_off
*zoom
);
380 curve
->DrawOffLine( *dc
, client_height
-offset_y
, off
, to
);
383 void wxPlotArea::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
387 m_owner
->GetViewStart( &view_x
, &view_y
);
388 view_x
*= wxPLOT_SCROLL_STEP
;
389 view_y
*= wxPLOT_SCROLL_STEP
;
391 wxPaintDC
dc( this );
392 m_owner
->PrepareDC( dc
);
394 wxRegionIterator
upd( GetUpdateRegion() );
398 int update_x
= upd
.GetX() + view_x
;
399 int update_width
= upd
.GetWidth();
402 if (m_owner->m_current)
404 dc.SetPen( *wxLIGHT_GREY_PEN );
405 int base_line = client_height - m_owner->m_current->GetOffsetY();
406 dc.DrawLine( update_x-1, base_line-1, update_x+update_width+2, base_line-1 );
410 wxList::compatibility_iterator node
= m_owner
->m_curves
.GetFirst();
413 wxPlotCurve
*curve
= (wxPlotCurve
*) node
->GetData();
415 if (curve
== m_owner
->GetCurrentCurve())
416 dc
.SetPen( curve
->GetPenSelected() );
418 dc
.SetPen( curve
->GetPenNormal() );
420 DrawCurve( &dc
, curve
, update_x
-1, update_x
+update_width
+2 );
422 node
= node
->GetNext();
425 dc
.SetPen( *wxRED_PEN
);
427 node
= m_owner
->m_onOffCurves
.GetFirst();
430 wxPlotOnOffCurve
*curve
= (wxPlotOnOffCurve
*) node
->GetData();
432 DrawOnOffCurve( &dc
, curve
, update_x
-1, update_x
+update_width
+2 );
434 node
= node
->GetNext();
441 void wxPlotArea::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
443 wxWindow::ScrollWindow( dx
, dy
, rect
);
444 // m_owner->m_xaxis->ScrollWindow( dx, 0 );
447 //-----------------------------------------------------------------------------
449 //-----------------------------------------------------------------------------
451 IMPLEMENT_DYNAMIC_CLASS(wxPlotXAxisArea
, wxWindow
)
453 BEGIN_EVENT_TABLE(wxPlotXAxisArea
, wxWindow
)
454 EVT_PAINT( wxPlotXAxisArea::OnPaint
)
455 EVT_LEFT_DOWN( wxPlotXAxisArea::OnMouse
)
458 wxPlotXAxisArea::wxPlotXAxisArea( wxPlotWindow
*parent
)
459 : wxWindow( parent
, wxID_ANY
, wxDefaultPosition
, wxSize(wxDefaultCoord
,40), 0, _T("plotxaxisarea") )
463 SetBackgroundColour( *wxWHITE
);
464 SetFont( *wxSMALL_FONT
);
467 void wxPlotXAxisArea::OnMouse( wxMouseEvent
&event
)
471 GetClientSize( &client_width
, &client_height
);
474 m_owner
->GetViewStart( &view_x
, &view_y
);
475 view_x
*= wxPLOT_SCROLL_STEP
;
476 view_y
*= wxPLOT_SCROLL_STEP
;
478 wxCoord x
= event
.GetX() + view_x
;
479 wxCoord y
= event
.GetY() + view_y
;
481 /* TO DO: do something here */
486 void wxPlotXAxisArea::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
490 m_owner
->GetViewStart( &view_x
, &view_y
);
491 view_x
*= wxPLOT_SCROLL_STEP
;
492 view_y
*= wxPLOT_SCROLL_STEP
;
494 wxPaintDC
dc( this );
498 GetClientSize( &client_width
, &client_height
);
500 double zoom
= m_owner
->GetZoom();
502 double ups
= m_owner
->GetUnitsPerValue() / zoom
;
504 double start
= view_x
* ups
;
505 double end
= (view_x
+ client_width
) * ups
;
506 double range
= end
- start
;
508 int int_log_range
= (int)floor( log10( range
) );
510 if (int_log_range
> 0)
512 for (int i
= 0; i
< int_log_range
; i
++)
515 if (int_log_range
< 0)
517 for (int i
= 0; i
< -int_log_range
; i
++)
520 double lower
= ceil(start
/ step
) * step
;
521 double upper
= floor(end
/ step
) * step
;
523 // if too few values, shrink size
524 if ((range
/step
) < 4)
527 if (lower
-step
> start
) lower
-= step
;
528 if (upper
+step
< end
) upper
+= step
;
531 // if still too few, again
532 if ((range
/step
) < 4)
535 if (lower
-step
> start
) lower
-= step
;
536 if (upper
+step
< end
) upper
+= step
;
539 dc
.SetBrush( *wxWHITE_BRUSH
);
540 dc
.SetPen( *wxTRANSPARENT_PEN
);
541 dc
.DrawRectangle( 4, 5, client_width
-14, 10 );
542 dc
.DrawRectangle( 0, 20, client_width
, 20 );
543 dc
.SetPen( *wxBLACK_PEN
);
545 double current
= lower
;
546 while (current
< upper
+(step
/2))
548 int x
= (int)ceil((current
-start
) / range
* (double)client_width
) - 1;
549 if ((x
> 4) && (x
< client_width
-25))
551 dc
.DrawLine( x
, 5, x
, 15 );
555 label
.Printf( _T("%f"), current
);
556 while (label
.Last() == _T('0'))
558 if ((label
.Last() == _T('.')) || (label
.Last() == _T(',')))
559 label
.Append( _T('0') );
562 label
.Printf( _T("%d"), (int)floor(current
) );
563 dc
.DrawText( label
, x
-4, 20 );
569 dc
.DrawLine( 0, 15, client_width
-8, 15 );
570 dc
.DrawLine( client_width
-4, 15, client_width
-10, 10 );
571 dc
.DrawLine( client_width
-4, 15, client_width
-10, 20 );
574 //-----------------------------------------------------------------------------
576 //-----------------------------------------------------------------------------
578 IMPLEMENT_DYNAMIC_CLASS(wxPlotYAxisArea
, wxWindow
)
580 BEGIN_EVENT_TABLE(wxPlotYAxisArea
, wxWindow
)
581 EVT_PAINT( wxPlotYAxisArea::OnPaint
)
582 EVT_LEFT_DOWN( wxPlotYAxisArea::OnMouse
)
585 wxPlotYAxisArea::wxPlotYAxisArea( wxPlotWindow
*parent
)
586 : wxWindow( parent
, wxID_ANY
, wxDefaultPosition
, wxSize(60,wxDefaultCoord
), 0, _T("plotyaxisarea") )
590 SetBackgroundColour( *wxWHITE
);
591 SetFont( *wxSMALL_FONT
);
594 void wxPlotYAxisArea::OnMouse( wxMouseEvent
&WXUNUSED(event
) )
596 /* do something here */
599 void wxPlotYAxisArea::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
601 wxPaintDC
dc( this );
603 wxPlotCurve
*curve
= m_owner
->GetCurrentCurve();
609 GetClientSize( &client_width
, &client_height
);
612 double range
= curve
->GetEndY() - curve
->GetStartY();
613 double offset
= ((double) curve
->GetOffsetY() / (double)client_height
) * range
;
614 double start
= curve
->GetStartY() - offset
;
615 double end
= curve
->GetEndY() - offset
;
617 int int_log_range
= (int)floor( log10( range
) );
619 if (int_log_range
> 0)
621 for (int i
= 0; i
< int_log_range
; i
++)
624 if (int_log_range
< 0)
626 for (int i
= 0; i
< -int_log_range
; i
++)
629 double lower
= ceil(start
/ step
) * step
;
630 double upper
= floor(end
/ step
) * step
;
632 // if too few values, shrink size
633 if ((range
/step
) < 4)
636 if (lower
-step
> start
) lower
-= step
;
637 if (upper
+step
< end
) upper
+= step
;
640 // if still too few, again
641 if ((range
/step
) < 4)
644 if (lower
-step
> start
) lower
-= step
;
645 if (upper
+step
< end
) upper
+= step
;
648 dc
.SetPen( *wxBLACK_PEN
);
650 double current
= lower
;
651 while (current
< upper
+(step
/2))
653 int y
= (int)((curve
->GetEndY()-current
) / range
* (double)client_height
) - 1;
654 y
-= curve
->GetOffsetY();
655 if ((y
> 10) && (y
< client_height
-7))
657 dc
.DrawLine( client_width
-15, y
, client_width
-7, y
);
661 label
.Printf( _T("%f"), current
);
662 while (label
.Last() == _T('0'))
664 if ((label
.Last() == _T('.')) || (label
.Last() == _T(',')))
665 label
.Append( _T('0') );
668 label
.Printf( _T("%d"), (int)floor(current
) );
669 dc
.DrawText( label
, 5, y
-7 );
675 dc
.DrawLine( client_width
-15, 6, client_width
-15, client_height
);
676 dc
.DrawLine( client_width
-15, 2, client_width
-20, 8 );
677 dc
.DrawLine( client_width
-15, 2, client_width
-10, 8 );
680 //-----------------------------------------------------------------------------
682 //-----------------------------------------------------------------------------
684 #define ID_ENLARGE 1000
685 #define ID_SHRINK 1002
687 #define ID_MOVE_UP 1006
688 #define ID_MOVE_DOWN 1007
690 #define ID_ZOOM_IN 1010
691 #define ID_ZOOM_OUT 1011
694 IMPLEMENT_DYNAMIC_CLASS(wxPlotWindow
, wxScrolledWindow
)
696 BEGIN_EVENT_TABLE(wxPlotWindow
, wxScrolledWindow
)
697 EVT_BUTTON( ID_MOVE_UP
, wxPlotWindow::OnMoveUp
)
698 EVT_BUTTON( ID_MOVE_DOWN
, wxPlotWindow::OnMoveDown
)
700 EVT_BUTTON( ID_ENLARGE
, wxPlotWindow::OnEnlarge
)
701 EVT_BUTTON( ID_SHRINK
, wxPlotWindow::OnShrink
)
703 EVT_BUTTON( ID_ZOOM_IN
, wxPlotWindow::OnZoomIn
)
704 EVT_BUTTON( ID_ZOOM_OUT
, wxPlotWindow::OnZoomOut
)
706 EVT_SCROLLWIN( wxPlotWindow::OnScroll2
)
709 wxPlotWindow::wxPlotWindow( wxWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
, int flag
)
710 : wxScrolledWindow( parent
, id
, pos
, size
, flag
, _T("plotcanvas") ),
711 m_titleStaticText( NULL
)
713 m_xUnitsPerValue
= 1.0;
716 m_enlargeAroundWindowCentre
= false;
717 m_scrollOnThumbRelease
= false;
719 m_area
= new wxPlotArea( this );
720 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxHORIZONTAL
);
722 if ((GetWindowStyleFlag() & wxPLOT_BUTTON_ALL
) != 0)
724 wxBoxSizer
*buttonlist
= new wxBoxSizer( wxVERTICAL
);
725 if ((GetWindowStyleFlag() & wxPLOT_BUTTON_ENLARGE
) != 0)
727 buttonlist
->Add( new wxBitmapButton( this, ID_ENLARGE
, *GetEnlargeBitmap() ), 0, wxEXPAND
|wxALL
, 2 );
728 buttonlist
->Add( new wxBitmapButton( this, ID_SHRINK
, *GetShrinkBitmap() ), 0, wxEXPAND
|wxALL
, 2 );
729 buttonlist
->Add( 20,10, 0 );
731 if ((GetWindowStyleFlag() & wxPLOT_BUTTON_MOVE
) != 0)
733 buttonlist
->Add( new wxBitmapButton( this, ID_MOVE_UP
, *GetUpBitmap() ), 0, wxEXPAND
|wxALL
, 2 );
734 buttonlist
->Add( new wxBitmapButton( this, ID_MOVE_DOWN
, *GetDownBitmap() ), 0, wxEXPAND
|wxALL
, 2 );
735 buttonlist
->Add( 20,10, 0 );
737 if ((GetWindowStyleFlag() & wxPLOT_BUTTON_ZOOM
) != 0)
739 buttonlist
->Add( new wxBitmapButton( this, ID_ZOOM_IN
, *GetZoomInBitmap() ), 0, wxEXPAND
|wxALL
, 2 );
740 buttonlist
->Add( new wxBitmapButton( this, ID_ZOOM_OUT
, *GetZoomOutBitmap() ), 0, wxEXPAND
|wxALL
, 2 );
742 mainsizer
->Add( buttonlist
, 0, wxEXPAND
|wxALL
, 4 );
745 wxBoxSizer
*plotsizer
= new wxBoxSizer( wxHORIZONTAL
);
747 //Add sizer to hold the title and plot.
748 //Title to be added later.
749 m_plotAndTitleSizer
= new wxBoxSizer( wxVERTICAL
);
750 m_plotAndTitleSizer
->Add( plotsizer
, 1, wxEXPAND
| wxTOP
, 10 );
752 if ((GetWindowStyleFlag() & wxPLOT_Y_AXIS
) != 0)
754 m_yaxis
= new wxPlotYAxisArea( this );
756 wxBoxSizer
*vert1
= new wxBoxSizer( wxVERTICAL
);
757 plotsizer
->Add( vert1
, 1, wxEXPAND
|wxTOP
,10 );
758 vert1
->Add( m_yaxis
, 1 );
759 if ((GetWindowStyleFlag() & wxPLOT_X_AXIS
) != 0)
760 vert1
->Add( 60, 40 );
764 m_yaxis
= (wxPlotYAxisArea
*) NULL
;
767 if ((GetWindowStyleFlag() & wxPLOT_X_AXIS
) != 0)
769 m_xaxis
= new wxPlotXAxisArea( this );
771 wxBoxSizer
*vert2
= new wxBoxSizer( wxVERTICAL
);
772 plotsizer
->Add( vert2
, 5, wxEXPAND
);
773 vert2
->Add( m_area
, 1, wxEXPAND
|wxTOP
,10 );
774 vert2
->Add( m_xaxis
, 0, wxEXPAND
);
778 plotsizer
->Add( m_area
, 1, wxEXPAND
);
779 m_xaxis
= (wxPlotXAxisArea
*) NULL
;
782 mainsizer
->Add( m_plotAndTitleSizer
, 1, wxEXPAND
);
784 SetAutoLayout( true );
785 SetSizer( mainsizer
);
786 mainsizer
->Fit(this);
787 mainsizer
->SetSizeHints(this);
789 SetTargetWindow( m_area
);
791 SetBackgroundColour( *wxWHITE
);
793 m_current
= (wxPlotCurve
*) NULL
;
796 wxPlotWindow::~wxPlotWindow()
800 void wxPlotWindow::Add( wxPlotCurve
*curve
)
802 m_curves
.Append( curve
);
803 if (!m_current
) m_current
= curve
;
808 size_t wxPlotWindow::GetCount()
810 return m_curves
.GetCount();
813 wxPlotCurve
*wxPlotWindow::GetAt( size_t n
)
815 wxList::compatibility_iterator node
= m_curves
.Item( n
);
817 return (wxPlotCurve
*) NULL
;
819 return (wxPlotCurve
*) node
->GetData();
822 void wxPlotWindow::SetCurrentCurve( wxPlotCurve
* current
)
825 m_area
->Refresh( false );
829 wxPlotEvent
event( wxEVT_PLOT_SEL_CHANGED
, GetId() );
830 event
.SetEventObject( this );
831 event
.SetZoom( GetZoom() );
832 event
.SetCurve( m_current
);
833 GetEventHandler()->ProcessEvent( event
);
836 void wxPlotWindow::Delete( wxPlotCurve
* curve
)
838 wxList::compatibility_iterator node
= m_curves
.Find( curve
);
841 m_curves
.DeleteObject( curve
);
843 m_area
->DeleteCurve( curve
);
844 m_area
->Refresh( false );
846 if (curve
== m_current
) m_current
= (wxPlotCurve
*) NULL
;
849 wxPlotCurve
*wxPlotWindow::GetCurrentCurve()
854 void wxPlotWindow::Add( wxPlotOnOffCurve
*curve
)
856 m_onOffCurves
.Append( curve
);
859 void wxPlotWindow::Delete( wxPlotOnOffCurve
* curve
)
861 wxList::compatibility_iterator node
= m_onOffCurves
.Find( curve
);
864 m_onOffCurves
.DeleteObject( curve
);
867 size_t wxPlotWindow::GetOnOffCurveCount()
869 return m_onOffCurves
.GetCount();
872 wxPlotOnOffCurve
*wxPlotWindow::GetOnOffCurveAt( size_t n
)
874 wxList::compatibility_iterator node
= m_onOffCurves
.Item( n
);
876 return (wxPlotOnOffCurve
*) NULL
;
878 return (wxPlotOnOffCurve
*) node
->GetData();
881 void wxPlotWindow::Move( wxPlotCurve
* curve
, int pixels_up
)
883 m_area
->DeleteCurve( curve
);
885 curve
->SetOffsetY( curve
->GetOffsetY() + pixels_up
);
887 m_area
->Refresh( false );
892 void wxPlotWindow::OnMoveUp( wxCommandEvent
& WXUNUSED(event
) )
894 if (!m_current
) return;
896 Move( m_current
, 25 );
899 void wxPlotWindow::OnMoveDown( wxCommandEvent
& WXUNUSED(event
) )
901 if (!m_current
) return;
903 Move( m_current
, -25 );
906 void wxPlotWindow::Enlarge( wxPlotCurve
*curve
, double factor
)
908 m_area
->DeleteCurve( curve
);
912 m_area
->GetClientSize( &client_width
, &client_height
);
913 double offset
= (double)curve
->GetOffsetY() / (double)client_height
;
915 double range
= curve
->GetEndY() - curve
->GetStartY();
918 double new_range
= range
/ factor
;
919 double new_offset
= offset
/ factor
;
921 if (m_enlargeAroundWindowCentre
)
923 double middle
= curve
->GetStartY() - offset
+ range
/2;
925 curve
->SetStartY( middle
- new_range
/ 2 + new_offset
);
926 curve
->SetEndY( middle
+ new_range
/ 2 + new_offset
);
930 curve
->SetStartY( (curve
->GetStartY() - offset
)/factor
+ new_offset
);
931 curve
->SetEndY( (curve
->GetEndY() - offset
)/factor
+ new_offset
);
934 m_area
->Refresh( false );
938 void wxPlotWindow::SetUnitsPerValue( double upv
)
940 m_xUnitsPerValue
= upv
;
945 void wxPlotWindow::SetZoom( double zoom
)
947 double old_zoom
= m_xZoom
;
952 GetViewStart( &view_x
, &view_y
);
955 wxList::compatibility_iterator node
= m_curves
.GetFirst();
958 wxPlotCurve
*curve
= (wxPlotCurve
*) node
->GetData();
959 if (curve
->GetEndX() > max
)
960 max
= curve
->GetEndX();
961 node
= node
->GetNext();
963 SetScrollbars( wxPLOT_SCROLL_STEP
, wxPLOT_SCROLL_STEP
,
964 (int)((max
*m_xZoom
)/wxPLOT_SCROLL_STEP
)+1, 0,
965 (int)(view_x
*zoom
/old_zoom
), 0,
969 m_area
->Refresh( true );
972 void wxPlotWindow::ResetScrollbar()
975 wxList::compatibility_iterator node
= m_curves
.GetFirst();
978 wxPlotCurve
*curve
= (wxPlotCurve
*) node
->GetData();
979 if (curve
->GetEndX() > max
)
980 max
= curve
->GetEndX();
981 node
= node
->GetNext();
984 SetScrollbars( wxPLOT_SCROLL_STEP
, wxPLOT_SCROLL_STEP
,
985 (int)(((max
*m_xZoom
)/wxPLOT_SCROLL_STEP
)+1), 0 );
988 void wxPlotWindow::AddChartTitle(const wxString
& title
, wxFont font
,
993 m_titleColour
= colour
;
997 void wxPlotWindow::DrawChartTitle()
999 if(m_title
.size() != 0)
1001 //If it is already added, remove child and delete
1002 if(m_titleStaticText
)
1004 RemoveChild( m_titleStaticText
);
1005 m_titleStaticText
->Destroy();
1008 //Create the text control and set the font, colour
1009 m_titleStaticText
= new wxStaticText( this, -1, m_title
);
1010 m_titleStaticText
->SetFont( m_titleFont
);
1011 m_titleStaticText
->SetForegroundColour( m_titleColour
);
1013 //Create a sizer for the title. Prepend it to the Plot + Title sizer.
1014 wxBoxSizer
* titleSizer
= new wxBoxSizer( wxHORIZONTAL
);
1015 titleSizer
->Add( m_titleStaticText
, 0, wxALIGN_CENTER
| wxALL
, 10 );
1016 m_plotAndTitleSizer
->Prepend( titleSizer
, 0, wxALIGN_CENTER_HORIZONTAL
);
1018 //Finally, force layout
1019 m_plotAndTitleSizer
->Layout();
1023 void wxPlotWindow::RedrawXAxis()
1026 m_xaxis
->Refresh( true );
1029 void wxPlotWindow::RedrawYAxis()
1032 m_yaxis
->Refresh( true );
1035 void wxPlotWindow::RedrawEverything()
1038 m_xaxis
->Refresh( true );
1040 m_yaxis
->Refresh( true );
1041 m_area
->Refresh( true );
1046 void wxPlotWindow::OnZoomIn( wxCommandEvent
& WXUNUSED(event
) )
1048 SetZoom( m_xZoom
* 1.5 );
1051 void wxPlotWindow::OnZoomOut( wxCommandEvent
& WXUNUSED(event
) )
1053 SetZoom( m_xZoom
* 0.6666 );
1056 void wxPlotWindow::OnEnlarge( wxCommandEvent
& WXUNUSED(event
) )
1058 if (!m_current
) return;
1060 Enlarge( m_current
, 1.5 );
1063 void wxPlotWindow::OnShrink( wxCommandEvent
& WXUNUSED(event
) )
1065 if (!m_current
) return;
1067 Enlarge( m_current
, 0.6666666 );
1070 void wxPlotWindow::OnScroll2( wxScrollWinEvent
& event
)
1072 if ((!m_scrollOnThumbRelease
) || (event
.GetEventType() != wxEVT_SCROLLWIN_THUMBTRACK
))
1074 wxScrolledWindow::OnScroll( event
);
1079 // ----------------------------------------------------------------------------
1081 // ----------------------------------------------------------------------------
1084 static wxBitmap
*GetEnlargeBitmap()
1086 static wxBitmap
* s_bitmap
= (wxBitmap
*) NULL
;
1087 static bool s_loaded
= false;
1091 s_loaded
= true; // set it to true anyhow, we won't try again
1093 #if defined(__WXMSW__) || defined(__WXPM__)
1094 s_bitmap
= new wxBitmap(_T("plot_enl_bmp"), wxBITMAP_TYPE_RESOURCE
);
1096 s_bitmap
= new wxBitmap( plot_enl_xpm
);
1103 static wxBitmap
*GetShrinkBitmap()
1105 static wxBitmap
* s_bitmap
= (wxBitmap
*) NULL
;
1106 static bool s_loaded
= false;
1110 s_loaded
= true; // set it to true anyhow, we won't try again
1112 #if defined(__WXMSW__) || defined(__WXPM__)
1113 s_bitmap
= new wxBitmap(_T("plot_shr_bmp"), wxBITMAP_TYPE_RESOURCE
);
1115 s_bitmap
= new wxBitmap( plot_shr_xpm
);
1122 static wxBitmap
*GetZoomInBitmap()
1124 static wxBitmap
* s_bitmap
= (wxBitmap
*) NULL
;
1125 static bool s_loaded
= false;
1129 s_loaded
= true; // set it to true anyhow, we won't try again
1131 #if defined(__WXMSW__) || defined(__WXPM__)
1132 s_bitmap
= new wxBitmap(_T("plot_zin_bmp"), wxBITMAP_TYPE_RESOURCE
);
1134 s_bitmap
= new wxBitmap( plot_zin_xpm
);
1141 static wxBitmap
*GetZoomOutBitmap()
1143 static wxBitmap
* s_bitmap
= (wxBitmap
*) NULL
;
1144 static bool s_loaded
= false;
1148 s_loaded
= true; // set it to true anyhow, we won't try again
1150 #if defined(__WXMSW__) || defined(__WXPM__)
1151 s_bitmap
= new wxBitmap(_T("plot_zot_bmp"), wxBITMAP_TYPE_RESOURCE
);
1153 s_bitmap
= new wxBitmap( plot_zot_xpm
);
1160 static wxBitmap
*GetUpBitmap()
1162 static wxBitmap
* s_bitmap
= (wxBitmap
*) NULL
;
1163 static bool s_loaded
= false;
1167 s_loaded
= true; // set it to true anyhow, we won't try again
1169 #if defined(__WXMSW__) || defined(__WXPM__)
1170 s_bitmap
= new wxBitmap(_T("plot_up_bmp"), wxBITMAP_TYPE_RESOURCE
);
1172 s_bitmap
= new wxBitmap( plot_up_xpm
);
1179 static wxBitmap
*GetDownBitmap()
1181 static wxBitmap
* s_bitmap
= (wxBitmap
*) NULL
;
1182 static bool s_loaded
= false;
1186 s_loaded
= true; // set it to true anyhow, we won't try again
1188 #if defined(__WXMSW__) || defined(__WXPM__)
1189 s_bitmap
= new wxBitmap(_T("plot_dwn_bmp"), wxBITMAP_TYPE_RESOURCE
);
1191 s_bitmap
= new wxBitmap( plot_dwn_xpm
);