]> git.saurik.com Git - wxWidgets.git/blame_incremental - contrib/src/plot/plot.cpp
Baked files for 2.6.1
[wxWidgets.git] / contrib / src / plot / plot.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: plot.cpp
3// Purpose: wxPlotWindow
4// Author: Robert Roebling
5// Modified by:
6// Created: 12/01/2000
7// RCS-ID: $Id$
8// Copyright: (c) Robert Roebling
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "plot.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include "wx/object.h"
25#include "wx/font.h"
26#include "wx/colour.h"
27#include "wx/settings.h"
28#include "wx/sizer.h"
29#include "wx/log.h"
30#include "wx/intl.h"
31#include "wx/dcclient.h"
32#endif
33
34#include "wx/plot/plot.h"
35#include "wx/bmpbuttn.h"
36#include "wx/module.h"
37
38#include <math.h>
39
40// ----------------------------------------------------------------------------
41// XPMs
42// ----------------------------------------------------------------------------
43
44#if !defined(__WXMSW__) && !defined(__WXPM__)
45 #include "wx/plot/plot_enl.xpm"
46 #include "wx/plot/plot_shr.xpm"
47 #include "wx/plot/plot_zin.xpm"
48 #include "wx/plot/plot_zot.xpm"
49 #include "wx/plot/plot_up.xpm"
50 #include "wx/plot/plot_dwn.xpm"
51#endif
52
53//----------------------------------------------------------------------------
54// event types
55//----------------------------------------------------------------------------
56
57DEFINE_EVENT_TYPE(wxEVT_PLOT_SEL_CHANGING);
58DEFINE_EVENT_TYPE(wxEVT_PLOT_SEL_CHANGED);
59DEFINE_EVENT_TYPE(wxEVT_PLOT_CLICKED);
60DEFINE_EVENT_TYPE(wxEVT_PLOT_DOUBLECLICKED);
61DEFINE_EVENT_TYPE(wxEVT_PLOT_ZOOM_IN);
62DEFINE_EVENT_TYPE(wxEVT_PLOT_ZOOM_OUT);
63DEFINE_EVENT_TYPE(wxEVT_PLOT_VALUE_SEL_CREATING);
64DEFINE_EVENT_TYPE(wxEVT_PLOT_VALUE_SEL_CREATED);
65DEFINE_EVENT_TYPE(wxEVT_PLOT_VALUE_SEL_CHANGING);
66DEFINE_EVENT_TYPE(wxEVT_PLOT_VALUE_SEL_CHANGED);
67DEFINE_EVENT_TYPE(wxEVT_PLOT_AREA_SEL_CREATING);
68DEFINE_EVENT_TYPE(wxEVT_PLOT_AREA_SEL_CREATED);
69DEFINE_EVENT_TYPE(wxEVT_PLOT_AREA_SEL_CHANGING);
70DEFINE_EVENT_TYPE(wxEVT_PLOT_AREA_SEL_CHANGED);
71DEFINE_EVENT_TYPE(wxEVT_PLOT_BEGIN_X_LABEL_EDIT);
72DEFINE_EVENT_TYPE(wxEVT_PLOT_END_X_LABEL_EDIT);
73DEFINE_EVENT_TYPE(wxEVT_PLOT_BEGIN_Y_LABEL_EDIT);
74DEFINE_EVENT_TYPE(wxEVT_PLOT_END_Y_LABEL_EDIT);
75DEFINE_EVENT_TYPE(wxEVT_PLOT_BEGIN_TITLE_EDIT);
76DEFINE_EVENT_TYPE(wxEVT_PLOT_END_TITLE_EDIT);
77DEFINE_EVENT_TYPE(wxEVT_PLOT_AREA_CREATE);
78
79//----------------------------------------------------------------------------
80// accessor functions for the bitmaps (may return NULL, check for it!)
81//----------------------------------------------------------------------------
82
83static wxBitmap *GetEnlargeBitmap();
84static wxBitmap *GetShrinkBitmap();
85static wxBitmap *GetZoomInBitmap();
86static wxBitmap *GetZoomOutBitmap();
87static wxBitmap *GetUpBitmap();
88static wxBitmap *GetDownBitmap();
89
90//-----------------------------------------------------------------------------
91// consts
92//-----------------------------------------------------------------------------
93
94#define wxPLOT_SCROLL_STEP 30
95
96//-----------------------------------------------------------------------------
97// wxPlotEvent
98//-----------------------------------------------------------------------------
99
100wxPlotEvent::wxPlotEvent( wxEventType commandType, int id )
101 : wxNotifyEvent( commandType, id )
102{
103 m_curve = (wxPlotCurve*) NULL;
104 m_zoom = 1.0;
105 m_position = 0;
106}
107
108//-----------------------------------------------------------------------------
109// wxPlotCurve
110//-----------------------------------------------------------------------------
111
112IMPLEMENT_ABSTRACT_CLASS(wxPlotCurve, wxObject)
113
114wxPlotCurve::wxPlotCurve( int offsetY, double startY, double endY )
115{
116 m_offsetY = offsetY;
117 m_startY = startY;
118 m_endY = endY;
119}
120
121//-----------------------------------------------------------------------------
122// wxPlotOnOffCurve
123//-----------------------------------------------------------------------------
124
125IMPLEMENT_CLASS(wxPlotOnOffCurve, wxObject)
126
127#include "wx/arrimpl.cpp"
128WX_DEFINE_OBJARRAY(wxArrayPlotOnOff);
129
130wxPlotOnOffCurve::wxPlotOnOffCurve( int offsetY )
131{
132 m_offsetY = offsetY;
133 m_minX = -1;
134 m_maxX = -1;
135}
136
137void wxPlotOnOffCurve::Add( wxInt32 on, wxInt32 off, void *clientData )
138{
139 wxASSERT_MSG( on > 0, _T("plot index < 0") );
140 wxASSERT( on <= off );
141
142 if (m_minX == -1)
143 m_minX = on;
144 if (off > m_maxX)
145 m_maxX = off;
146
147 wxPlotOnOff *v = new wxPlotOnOff;
148 v->m_on = on;
149 v->m_off = off;
150 v->m_clientData = clientData;
151 m_marks.Add( v );
152}
153
154size_t wxPlotOnOffCurve::GetCount()
155{
156 return m_marks.GetCount();
157}
158
159wxInt32 wxPlotOnOffCurve::GetOn( size_t index )
160{
161 wxPlotOnOff *v = &m_marks.Item( index );
162 return v->m_on;
163}
164
165wxInt32 wxPlotOnOffCurve::GetOff( size_t index )
166{
167 wxPlotOnOff *v = &m_marks.Item( index );
168 return v->m_off;
169}
170
171void* wxPlotOnOffCurve::GetClientData( size_t index )
172{
173 wxPlotOnOff *v = &m_marks.Item( index );
174 return v->m_clientData;
175}
176
177wxPlotOnOff *wxPlotOnOffCurve::GetAt( size_t index )
178{
179 return &m_marks.Item( index );
180}
181
182void wxPlotOnOffCurve::DrawOnLine( wxDC &dc, wxCoord y, wxCoord start, wxCoord end, void *WXUNUSED(clientData) )
183{
184 dc.DrawLine( start, y, start, y-30 );
185 dc.DrawLine( start, y-30, end, y-30 );
186 dc.DrawLine( end, y-30, end, y );
187}
188
189void wxPlotOnOffCurve::DrawOffLine( wxDC &dc, wxCoord y, wxCoord start, wxCoord end )
190{
191 dc.DrawLine( start, y, end, y );
192}
193
194//-----------------------------------------------------------------------------
195// wxPlotArea
196//-----------------------------------------------------------------------------
197
198IMPLEMENT_DYNAMIC_CLASS(wxPlotArea, wxWindow)
199
200BEGIN_EVENT_TABLE(wxPlotArea, wxWindow)
201 EVT_PAINT( wxPlotArea::OnPaint)
202 EVT_LEFT_DOWN( wxPlotArea::OnMouse)
203 EVT_LEFT_DCLICK( wxPlotArea::OnMouse)
204END_EVENT_TABLE()
205
206wxPlotArea::wxPlotArea( wxPlotWindow *parent )
207 : wxWindow( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER, _T("plotarea") )
208{
209 m_owner = parent;
210
211 m_zooming = false;
212
213 SetBackgroundColour( *wxWHITE );
214}
215
216void wxPlotArea::OnMouse( wxMouseEvent &event )
217{
218 int client_width;
219 int client_height;
220 GetClientSize( &client_width, &client_height);
221 int view_x;
222 int view_y;
223 m_owner->GetViewStart( &view_x, &view_y );
224 view_x *= wxPLOT_SCROLL_STEP;
225 view_y *= wxPLOT_SCROLL_STEP;
226
227 wxCoord x = event.GetX();
228 wxCoord y = event.GetY();
229 x += view_x;
230 y += view_y;
231
232 wxList::compatibility_iterator node = m_owner->m_curves.GetFirst();
233 while (node)
234 {
235 wxPlotCurve *curve = (wxPlotCurve*)node->GetData();
236
237 double double_client_height = (double)client_height;
238 double range = curve->GetEndY() - curve->GetStartY();
239 double end = curve->GetEndY();
240 wxCoord offset_y = curve->GetOffsetY();
241
242 double dy = (end - curve->GetY( (wxInt32)(x/m_owner->GetZoom()) )) / range;
243 wxCoord curve_y = (wxCoord)(dy * double_client_height) - offset_y - 1;
244
245 if ((y-curve_y < 4) && (y-curve_y > -4))
246 {
247 wxPlotEvent event1( event.ButtonDClick() ? wxEVT_PLOT_DOUBLECLICKED : wxEVT_PLOT_CLICKED, m_owner->GetId() );
248 event1.SetEventObject( m_owner );
249 event1.SetZoom( m_owner->GetZoom() );
250 event1.SetCurve( curve );
251 event1.SetPosition( (int)floor(x/m_owner->GetZoom()) );
252 m_owner->GetEventHandler()->ProcessEvent( event1 );
253
254 if (curve != m_owner->GetCurrentCurve())
255 {
256 wxPlotEvent event2( wxEVT_PLOT_SEL_CHANGING, m_owner->GetId() );
257 event2.SetEventObject( m_owner );
258 event2.SetZoom( m_owner->GetZoom() );
259 event2.SetCurve( curve );
260 if (!m_owner->GetEventHandler()->ProcessEvent( event2 ) || event2.IsAllowed())
261 {
262 m_owner->SetCurrentCurve( curve );
263 }
264 }
265 return;
266 }
267
268 node = node->GetNext();
269 }
270}
271
272void wxPlotArea::DeleteCurve( wxPlotCurve *curve, int from, int to )
273{
274 wxClientDC dc(this);
275 m_owner->PrepareDC( dc );
276 dc.SetPen( *wxWHITE_PEN );
277 DrawCurve( &dc, curve, from, to );
278}
279
280void wxPlotArea::DrawCurve( wxDC *dc, wxPlotCurve *curve, int from, int to )
281{
282 int view_x;
283 int view_y;
284 m_owner->GetViewStart( &view_x, &view_y );
285 view_x *= wxPLOT_SCROLL_STEP;
286
287 if (from == -1)
288 from = view_x;
289
290 int client_width;
291 int client_height;
292 GetClientSize( &client_width, &client_height);
293
294 if (to == -1)
295 to = view_x + client_width;
296
297 double zoom = m_owner->GetZoom();
298
299 int start_x = wxMax( from, (int)floor(curve->GetStartX()*zoom) );
300 int end_x = wxMin( to, (int)floor(curve->GetEndX()*zoom) );
301
302 start_x = wxMax( view_x, start_x );
303 end_x = wxMin( view_x + client_width, end_x );
304
305 end_x++;
306
307 double double_client_height = (double)client_height;
308 double range = curve->GetEndY() - curve->GetStartY();
309 double end = curve->GetEndY();
310 wxCoord offset_y = curve->GetOffsetY();
311
312 wxCoord last_y=0;
313 for (int x = start_x; x < end_x; x++)
314 {
315 double dy = (end - curve->GetY( (wxInt32)(x/zoom) )) / range;
316 wxCoord y = (wxCoord)(dy * double_client_height) - offset_y - 1;
317
318 if (x != start_x)
319 dc->DrawLine( x-1, last_y, x, y );
320
321 last_y = y;
322 }
323}
324
325void wxPlotArea::DrawOnOffCurve( wxDC *dc, wxPlotOnOffCurve *curve, int from, int to )
326{
327 int view_x;
328 int view_y;
329 m_owner->GetViewStart( &view_x, &view_y );
330 view_x *= wxPLOT_SCROLL_STEP;
331
332 if (from == -1)
333 from = view_x;
334
335 int client_width;
336 int client_height;
337 GetClientSize( &client_width, &client_height);
338
339 if (to == -1)
340 to = view_x + client_width;
341
342 double zoom = m_owner->GetZoom();
343
344 int start_x = wxMax( from, (int)floor(curve->GetStartX()*zoom) );
345 int end_x = wxMin( to, (int)floor(curve->GetEndX()*zoom) );
346
347 start_x = wxMax( view_x, start_x );
348 end_x = wxMin( view_x + client_width, end_x );
349
350 end_x++;
351
352 wxCoord offset_y = curve->GetOffsetY();
353 wxCoord last_off = -5;
354
355 if (curve->GetCount() == 0)
356 return;
357
358 for (size_t index = 0; index < curve->GetCount(); index++)
359 {
360 wxPlotOnOff *p = curve->GetAt( index );
361
362 wxCoord on = (wxCoord)(p->m_on*zoom);
363 wxCoord off = (wxCoord)(p->m_off*zoom);
364
365 if (end_x < on)
366 {
367 curve->DrawOffLine( *dc, client_height-offset_y, last_off, on );
368 break;
369 }
370
371 if (off >= start_x)
372 {
373 curve->DrawOffLine( *dc, client_height-offset_y, last_off, on );
374 curve->DrawOnLine( *dc, client_height-offset_y, on, off, p->m_clientData );
375 }
376 last_off = off;
377 }
378
379 wxPlotOnOff *p = curve->GetAt( curve->GetCount()-1 );
380 wxCoord off = (wxCoord)(p->m_off*zoom);
381 if (off < end_x)
382 curve->DrawOffLine( *dc, client_height-offset_y, off, to );
383}
384
385void wxPlotArea::OnPaint( wxPaintEvent &WXUNUSED(event) )
386{
387 int view_x;
388 int view_y;
389 m_owner->GetViewStart( &view_x, &view_y );
390 view_x *= wxPLOT_SCROLL_STEP;
391 view_y *= wxPLOT_SCROLL_STEP;
392
393 wxPaintDC dc( this );
394 m_owner->PrepareDC( dc );
395
396 wxRegionIterator upd( GetUpdateRegion() );
397
398 while (upd)
399 {
400 int update_x = upd.GetX() + view_x;
401 int update_width = upd.GetWidth();
402
403/*
404 if (m_owner->m_current)
405 {
406 dc.SetPen( *wxLIGHT_GREY_PEN );
407 int base_line = client_height - m_owner->m_current->GetOffsetY();
408 dc.DrawLine( update_x-1, base_line-1, update_x+update_width+2, base_line-1 );
409 }
410*/
411
412 wxList::compatibility_iterator node = m_owner->m_curves.GetFirst();
413 while (node)
414 {
415 wxPlotCurve *curve = (wxPlotCurve*) node->GetData();
416
417 if (curve == m_owner->GetCurrentCurve())
418 dc.SetPen( *wxBLACK_PEN );
419 else
420 dc.SetPen( *wxGREY_PEN );
421
422 DrawCurve( &dc, curve, update_x-1, update_x+update_width+2 );
423
424 node = node->GetNext();
425 }
426
427 dc.SetPen( *wxRED_PEN );
428
429 node = m_owner->m_onOffCurves.GetFirst();
430 while (node)
431 {
432 wxPlotOnOffCurve *curve = (wxPlotOnOffCurve*) node->GetData();
433
434 DrawOnOffCurve( &dc, curve, update_x-1, update_x+update_width+2 );
435
436 node = node->GetNext();
437 }
438
439 upd ++;
440 }
441}
442
443void wxPlotArea::ScrollWindow( int dx, int dy, const wxRect *rect )
444{
445 wxWindow::ScrollWindow( dx, dy, rect );
446// m_owner->m_xaxis->ScrollWindow( dx, 0 );
447}
448
449//-----------------------------------------------------------------------------
450// wxPlotXAxisArea
451//-----------------------------------------------------------------------------
452
453IMPLEMENT_DYNAMIC_CLASS(wxPlotXAxisArea, wxWindow)
454
455BEGIN_EVENT_TABLE(wxPlotXAxisArea, wxWindow)
456 EVT_PAINT( wxPlotXAxisArea::OnPaint)
457 EVT_LEFT_DOWN( wxPlotXAxisArea::OnMouse)
458END_EVENT_TABLE()
459
460wxPlotXAxisArea::wxPlotXAxisArea( wxPlotWindow *parent )
461 : wxWindow( parent, wxID_ANY, wxDefaultPosition, wxSize(wxDefaultCoord,40), 0, _T("plotxaxisarea") )
462{
463 m_owner = parent;
464
465 SetBackgroundColour( *wxWHITE );
466 SetFont( *wxSMALL_FONT );
467}
468
469void wxPlotXAxisArea::OnMouse( wxMouseEvent &event )
470{
471 int client_width;
472 int client_height;
473 GetClientSize( &client_width, &client_height);
474 int view_x;
475 int view_y;
476 m_owner->GetViewStart( &view_x, &view_y );
477 view_x *= wxPLOT_SCROLL_STEP;
478 view_y *= wxPLOT_SCROLL_STEP;
479
480 wxCoord x = event.GetX() + view_x;
481 wxCoord y = event.GetY() + view_y;
482
483 /* TO DO: do something here */
484 wxUnusedVar(x);
485 wxUnusedVar(y);
486}
487
488void wxPlotXAxisArea::OnPaint( wxPaintEvent &WXUNUSED(event) )
489{
490 int view_x;
491 int view_y;
492 m_owner->GetViewStart( &view_x, &view_y );
493 view_x *= wxPLOT_SCROLL_STEP;
494 view_y *= wxPLOT_SCROLL_STEP;
495
496 wxPaintDC dc( this );
497
498 int client_width;
499 int client_height;
500 GetClientSize( &client_width, &client_height);
501
502 double zoom = m_owner->GetZoom();
503
504 double ups = m_owner->GetUnitsPerValue() / zoom;
505
506 double start = view_x * ups;
507 double end = (view_x + client_width) * ups;
508 double range = end - start;
509
510 int int_log_range = (int)floor( log10( range ) );
511 double step = 1.0;
512 if (int_log_range > 0)
513 {
514 for (int i = 0; i < int_log_range; i++)
515 step *= 10;
516 }
517 if (int_log_range < 0)
518 {
519 for (int i = 0; i < -int_log_range; i++)
520 step /= 10;
521 }
522 double lower = ceil(start / step) * step;
523 double upper = floor(end / step) * step;
524
525 // if too few values, shrink size
526 if ((range/step) < 4)
527 {
528 step /= 2;
529 if (lower-step > start) lower -= step;
530 if (upper+step < end) upper += step;
531 }
532
533 // if still too few, again
534 if ((range/step) < 4)
535 {
536 step /= 2;
537 if (lower-step > start) lower -= step;
538 if (upper+step < end) upper += step;
539 }
540
541 dc.SetBrush( *wxWHITE_BRUSH );
542 dc.SetPen( *wxTRANSPARENT_PEN );
543 dc.DrawRectangle( 4, 5, client_width-14, 10 );
544 dc.DrawRectangle( 0, 20, client_width, 20 );
545 dc.SetPen( *wxBLACK_PEN );
546
547 double current = lower;
548 while (current < upper+(step/2))
549 {
550 int x = (int)ceil((current-start) / range * (double)client_width) - 1;
551 if ((x > 4) && (x < client_width-25))
552 {
553 dc.DrawLine( x, 5, x, 15 );
554 wxString label;
555 if (range < 50)
556 {
557 label.Printf( _T("%f"), current );
558 while (label.Last() == _T('0'))
559 label.RemoveLast();
560 if ((label.Last() == _T('.')) || (label.Last() == _T(',')))
561 label.Append( _T('0') );
562 }
563 else
564 label.Printf( _T("%d"), (int)floor(current) );
565 dc.DrawText( label, x-4, 20 );
566 }
567
568 current += step;
569 }
570
571 dc.DrawLine( 0, 15, client_width-8, 15 );
572 dc.DrawLine( client_width-4, 15, client_width-10, 10 );
573 dc.DrawLine( client_width-4, 15, client_width-10, 20 );
574}
575
576//-----------------------------------------------------------------------------
577// wxPlotYAxisArea
578//-----------------------------------------------------------------------------
579
580IMPLEMENT_DYNAMIC_CLASS(wxPlotYAxisArea, wxWindow)
581
582BEGIN_EVENT_TABLE(wxPlotYAxisArea, wxWindow)
583 EVT_PAINT( wxPlotYAxisArea::OnPaint)
584 EVT_LEFT_DOWN( wxPlotYAxisArea::OnMouse)
585END_EVENT_TABLE()
586
587wxPlotYAxisArea::wxPlotYAxisArea( wxPlotWindow *parent )
588 : wxWindow( parent, wxID_ANY, wxDefaultPosition, wxSize(60,wxDefaultCoord), 0, _T("plotyaxisarea") )
589{
590 m_owner = parent;
591
592 SetBackgroundColour( *wxWHITE );
593 SetFont( *wxSMALL_FONT );
594}
595
596void wxPlotYAxisArea::OnMouse( wxMouseEvent &WXUNUSED(event) )
597{
598 /* do something here */
599}
600
601void wxPlotYAxisArea::OnPaint( wxPaintEvent &WXUNUSED(event) )
602{
603 wxPaintDC dc( this );
604
605 wxPlotCurve *curve = m_owner->GetCurrentCurve();
606
607 if (!curve) return;
608
609 int client_width;
610 int client_height;
611 GetClientSize( &client_width, &client_height);
612
613
614 double range = curve->GetEndY() - curve->GetStartY();
615 double offset = ((double) curve->GetOffsetY() / (double)client_height ) * range;
616 double start = curve->GetStartY() - offset;
617 double end = curve->GetEndY() - offset;
618
619 int int_log_range = (int)floor( log10( range ) );
620 double step = 1.0;
621 if (int_log_range > 0)
622 {
623 for (int i = 0; i < int_log_range; i++)
624 step *= 10;
625 }
626 if (int_log_range < 0)
627 {
628 for (int i = 0; i < -int_log_range; i++)
629 step /= 10;
630 }
631 double lower = ceil(start / step) * step;
632 double upper = floor(end / step) * step;
633
634 // if too few values, shrink size
635 if ((range/step) < 4)
636 {
637 step /= 2;
638 if (lower-step > start) lower -= step;
639 if (upper+step < end) upper += step;
640 }
641
642 // if still too few, again
643 if ((range/step) < 4)
644 {
645 step /= 2;
646 if (lower-step > start) lower -= step;
647 if (upper+step < end) upper += step;
648 }
649
650 dc.SetPen( *wxBLACK_PEN );
651
652 double current = lower;
653 while (current < upper+(step/2))
654 {
655 int y = (int)((curve->GetEndY()-current) / range * (double)client_height) - 1;
656 y -= curve->GetOffsetY();
657 if ((y > 10) && (y < client_height-7))
658 {
659 dc.DrawLine( client_width-15, y, client_width-7, y );
660 wxString label;
661 if (range < 50)
662 {
663 label.Printf( _T("%f"), current );
664 while (label.Last() == _T('0'))
665 label.RemoveLast();
666 if ((label.Last() == _T('.')) || (label.Last() == _T(',')))
667 label.Append( _T('0') );
668 }
669 else
670 label.Printf( _T("%d"), (int)floor(current) );
671 dc.DrawText( label, 5, y-7 );
672 }
673
674 current += step;
675 }
676
677 dc.DrawLine( client_width-15, 6, client_width-15, client_height );
678 dc.DrawLine( client_width-15, 2, client_width-20, 8 );
679 dc.DrawLine( client_width-15, 2, client_width-10, 8 );
680}
681
682//-----------------------------------------------------------------------------
683// wxPlotWindow
684//-----------------------------------------------------------------------------
685
686#define ID_ENLARGE 1000
687#define ID_SHRINK 1002
688
689#define ID_MOVE_UP 1006
690#define ID_MOVE_DOWN 1007
691
692#define ID_ZOOM_IN 1010
693#define ID_ZOOM_OUT 1011
694
695
696IMPLEMENT_DYNAMIC_CLASS(wxPlotWindow, wxScrolledWindow)
697
698BEGIN_EVENT_TABLE(wxPlotWindow, wxScrolledWindow)
699 EVT_BUTTON( ID_MOVE_UP, wxPlotWindow::OnMoveUp)
700 EVT_BUTTON( ID_MOVE_DOWN, wxPlotWindow::OnMoveDown)
701
702 EVT_BUTTON( ID_ENLARGE, wxPlotWindow::OnEnlarge)
703 EVT_BUTTON( ID_SHRINK, wxPlotWindow::OnShrink)
704
705 EVT_BUTTON( ID_ZOOM_IN, wxPlotWindow::OnZoomIn)
706 EVT_BUTTON( ID_ZOOM_OUT, wxPlotWindow::OnZoomOut)
707
708 EVT_SCROLLWIN( wxPlotWindow::OnScroll2)
709END_EVENT_TABLE()
710
711wxPlotWindow::wxPlotWindow( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag )
712 : wxScrolledWindow( parent, id, pos, size, flag, _T("plotcanvas") )
713{
714 m_xUnitsPerValue = 1.0;
715 m_xZoom = 1.0;
716
717 m_enlargeAroundWindowCentre = false;
718 m_scrollOnThumbRelease = false;
719
720 m_area = new wxPlotArea( this );
721 wxBoxSizer *mainsizer = new wxBoxSizer( wxHORIZONTAL );
722
723 if ((GetWindowStyleFlag() & wxPLOT_BUTTON_ALL) != 0)
724 {
725 wxBoxSizer *buttonlist = new wxBoxSizer( wxVERTICAL );
726 if ((GetWindowStyleFlag() & wxPLOT_BUTTON_ENLARGE) != 0)
727 {
728 buttonlist->Add( new wxBitmapButton( this, ID_ENLARGE, *GetEnlargeBitmap() ), 0, wxEXPAND|wxALL, 2 );
729 buttonlist->Add( new wxBitmapButton( this, ID_SHRINK, *GetShrinkBitmap() ), 0, wxEXPAND|wxALL, 2 );
730 buttonlist->Add( 20,10, 0 );
731 }
732 if ((GetWindowStyleFlag() & wxPLOT_BUTTON_MOVE) != 0)
733 {
734 buttonlist->Add( new wxBitmapButton( this, ID_MOVE_UP, *GetUpBitmap() ), 0, wxEXPAND|wxALL, 2 );
735 buttonlist->Add( new wxBitmapButton( this, ID_MOVE_DOWN, *GetDownBitmap() ), 0, wxEXPAND|wxALL, 2 );
736 buttonlist->Add( 20,10, 0 );
737 }
738 if ((GetWindowStyleFlag() & wxPLOT_BUTTON_ZOOM) != 0)
739 {
740 buttonlist->Add( new wxBitmapButton( this, ID_ZOOM_IN, *GetZoomInBitmap() ), 0, wxEXPAND|wxALL, 2 );
741 buttonlist->Add( new wxBitmapButton( this, ID_ZOOM_OUT, *GetZoomOutBitmap() ), 0, wxEXPAND|wxALL, 2 );
742 }
743 mainsizer->Add( buttonlist, 0, wxEXPAND|wxALL, 4 );
744 }
745
746 wxBoxSizer *plotsizer = new wxBoxSizer( wxHORIZONTAL );
747
748 if ((GetWindowStyleFlag() & wxPLOT_Y_AXIS) != 0)
749 {
750 m_yaxis = new wxPlotYAxisArea( this );
751
752 wxBoxSizer *vert1 = new wxBoxSizer( wxVERTICAL );
753 plotsizer->Add( vert1, 1, wxEXPAND|wxTOP,10 );
754 vert1->Add( m_yaxis, 1 );
755 if ((GetWindowStyleFlag() & wxPLOT_X_AXIS) != 0)
756 vert1->Add( 60, 40 );
757 }
758 else
759 {
760 m_yaxis = (wxPlotYAxisArea*) NULL;
761 }
762
763 if ((GetWindowStyleFlag() & wxPLOT_X_AXIS) != 0)
764 {
765 m_xaxis = new wxPlotXAxisArea( this );
766
767 wxBoxSizer *vert2 = new wxBoxSizer( wxVERTICAL );
768 plotsizer->Add( vert2, 5, wxEXPAND);
769 vert2->Add( m_area, 1, wxEXPAND|wxTOP,10 );
770 vert2->Add( m_xaxis, 0, wxEXPAND );
771 }
772 else
773 {
774 plotsizer->Add( m_area, 1, wxEXPAND );
775 m_xaxis = (wxPlotXAxisArea*) NULL;
776 }
777
778 mainsizer->Add( plotsizer, 1, wxEXPAND );
779
780 SetAutoLayout( true );
781 SetSizer( mainsizer );
782 mainsizer->Fit(this);
783 mainsizer->SetSizeHints(this);
784
785 SetTargetWindow( m_area );
786
787 SetBackgroundColour( *wxWHITE );
788
789 m_current = (wxPlotCurve*) NULL;
790}
791
792wxPlotWindow::~wxPlotWindow()
793{
794}
795
796void wxPlotWindow::Add( wxPlotCurve *curve )
797{
798 m_curves.Append( curve );
799 if (!m_current) m_current = curve;
800
801 ResetScrollbar();
802}
803
804size_t wxPlotWindow::GetCount()
805{
806 return m_curves.GetCount();
807}
808
809wxPlotCurve *wxPlotWindow::GetAt( size_t n )
810{
811 wxList::compatibility_iterator node = m_curves.Item( n );
812 if (!node)
813 return (wxPlotCurve*) NULL;
814
815 return (wxPlotCurve*) node->GetData();
816}
817
818void wxPlotWindow::SetCurrentCurve( wxPlotCurve* current )
819{
820 m_current = current;
821 m_area->Refresh( false );
822
823 RedrawYAxis();
824
825 wxPlotEvent event( wxEVT_PLOT_SEL_CHANGED, GetId() );
826 event.SetEventObject( this );
827 event.SetZoom( GetZoom() );
828 event.SetCurve( m_current );
829 GetEventHandler()->ProcessEvent( event );
830}
831
832void wxPlotWindow::Delete( wxPlotCurve* curve )
833{
834 wxList::compatibility_iterator node = m_curves.Find( curve );
835 if (!node) return;
836
837 m_curves.DeleteObject( curve );
838
839 m_area->DeleteCurve( curve );
840 m_area->Refresh( false );
841
842 if (curve == m_current) m_current = (wxPlotCurve *) NULL;
843}
844
845wxPlotCurve *wxPlotWindow::GetCurrentCurve()
846{
847 return m_current;
848}
849
850void wxPlotWindow::Add( wxPlotOnOffCurve *curve )
851{
852 m_onOffCurves.Append( curve );
853}
854
855void wxPlotWindow::Delete( wxPlotOnOffCurve* curve )
856{
857 wxList::compatibility_iterator node = m_onOffCurves.Find( curve );
858 if (!node) return;
859
860 m_onOffCurves.DeleteObject( curve );
861}
862
863size_t wxPlotWindow::GetOnOffCurveCount()
864{
865 return m_onOffCurves.GetCount();
866}
867
868wxPlotOnOffCurve *wxPlotWindow::GetOnOffCurveAt( size_t n )
869{
870 wxList::compatibility_iterator node = m_onOffCurves.Item( n );
871 if (!node)
872 return (wxPlotOnOffCurve*) NULL;
873
874 return (wxPlotOnOffCurve*) node->GetData();
875}
876
877void wxPlotWindow::Move( wxPlotCurve* curve, int pixels_up )
878{
879 m_area->DeleteCurve( curve );
880
881 curve->SetOffsetY( curve->GetOffsetY() + pixels_up );
882
883 m_area->Refresh( false );
884
885 RedrawYAxis();
886}
887
888void wxPlotWindow::OnMoveUp( wxCommandEvent& WXUNUSED(event) )
889{
890 if (!m_current) return;
891
892 Move( m_current, 25 );
893}
894
895void wxPlotWindow::OnMoveDown( wxCommandEvent& WXUNUSED(event) )
896{
897 if (!m_current) return;
898
899 Move( m_current, -25 );
900}
901
902void wxPlotWindow::Enlarge( wxPlotCurve *curve, double factor )
903{
904 m_area->DeleteCurve( curve );
905
906 int client_width;
907 int client_height;
908 m_area->GetClientSize( &client_width, &client_height);
909 double offset = (double)curve->GetOffsetY() / (double)client_height;
910
911 double range = curve->GetEndY() - curve->GetStartY();
912 offset *= range;
913
914 double new_range = range / factor;
915 double new_offset = offset / factor;
916
917 if (m_enlargeAroundWindowCentre)
918 {
919 double middle = curve->GetStartY() - offset + range/2;
920
921 curve->SetStartY( middle - new_range / 2 + new_offset );
922 curve->SetEndY( middle + new_range / 2 + new_offset );
923 }
924 else
925 {
926 curve->SetStartY( (curve->GetStartY() - offset)/factor + new_offset );
927 curve->SetEndY( (curve->GetEndY() - offset)/factor + new_offset );
928 }
929
930 m_area->Refresh( false );
931 RedrawYAxis();
932}
933
934void wxPlotWindow::SetUnitsPerValue( double upv )
935{
936 m_xUnitsPerValue = upv;
937
938 RedrawXAxis();
939}
940
941void wxPlotWindow::SetZoom( double zoom )
942{
943 double old_zoom = m_xZoom;
944 m_xZoom = zoom;
945
946 int view_x = 0;
947 int view_y = 0;
948 GetViewStart( &view_x, &view_y );
949
950 wxInt32 max = 0;
951 wxList::compatibility_iterator node = m_curves.GetFirst();
952 while (node)
953 {
954 wxPlotCurve *curve = (wxPlotCurve*) node->GetData();
955 if (curve->GetEndX() > max)
956 max = curve->GetEndX();
957 node = node->GetNext();
958 }
959 SetScrollbars( wxPLOT_SCROLL_STEP, wxPLOT_SCROLL_STEP,
960 (int)((max*m_xZoom)/wxPLOT_SCROLL_STEP)+1, 0,
961 (int)(view_x*zoom/old_zoom), 0,
962 true );
963
964 RedrawXAxis();
965 m_area->Refresh( true );
966}
967
968void wxPlotWindow::ResetScrollbar()
969{
970 wxInt32 max = 0;
971 wxList::compatibility_iterator node = m_curves.GetFirst();
972 while (node)
973 {
974 wxPlotCurve *curve = (wxPlotCurve*) node->GetData();
975 if (curve->GetEndX() > max)
976 max = curve->GetEndX();
977 node = node->GetNext();
978 }
979
980 SetScrollbars( wxPLOT_SCROLL_STEP, wxPLOT_SCROLL_STEP,
981 (int)(((max*m_xZoom)/wxPLOT_SCROLL_STEP)+1), 0 );
982}
983
984void wxPlotWindow::RedrawXAxis()
985{
986 if (m_xaxis)
987 m_xaxis->Refresh( false );
988}
989
990void wxPlotWindow::RedrawYAxis()
991{
992 if (m_yaxis)
993 m_yaxis->Refresh( true );
994}
995
996void wxPlotWindow::RedrawEverything()
997{
998 if (m_xaxis)
999 m_xaxis->Refresh( true );
1000 if (m_yaxis)
1001 m_yaxis->Refresh( true );
1002 m_area->Refresh( true );
1003}
1004
1005void wxPlotWindow::OnZoomIn( wxCommandEvent& WXUNUSED(event) )
1006{
1007 SetZoom( m_xZoom * 1.5 );
1008}
1009
1010void wxPlotWindow::OnZoomOut( wxCommandEvent& WXUNUSED(event) )
1011{
1012 SetZoom( m_xZoom * 0.6666 );
1013}
1014
1015void wxPlotWindow::OnEnlarge( wxCommandEvent& WXUNUSED(event) )
1016{
1017 if (!m_current) return;
1018
1019 Enlarge( m_current, 1.5 );
1020}
1021
1022void wxPlotWindow::OnShrink( wxCommandEvent& WXUNUSED(event) )
1023{
1024 if (!m_current) return;
1025
1026 Enlarge( m_current, 0.6666666 );
1027}
1028
1029void wxPlotWindow::OnScroll2( wxScrollWinEvent& event )
1030{
1031 if ((!m_scrollOnThumbRelease) || (event.GetEventType() != wxEVT_SCROLLWIN_THUMBTRACK))
1032 {
1033 wxScrolledWindow::OnScroll( event );
1034 RedrawXAxis();
1035 }
1036}
1037
1038// ----------------------------------------------------------------------------
1039// global functions
1040// ----------------------------------------------------------------------------
1041
1042// FIXME MT-UNSAFE
1043static wxBitmap *GetEnlargeBitmap()
1044{
1045 static wxBitmap* s_bitmap = (wxBitmap *) NULL;
1046 static bool s_loaded = false;
1047
1048 if ( !s_loaded )
1049 {
1050 s_loaded = true; // set it to true anyhow, we won't try again
1051
1052 #if defined(__WXMSW__) || defined(__WXPM__)
1053 s_bitmap = new wxBitmap(_T("plot_enl_bmp"), wxBITMAP_TYPE_RESOURCE);
1054 #else
1055 s_bitmap = new wxBitmap( plot_enl_xpm );
1056 #endif
1057 }
1058
1059 return s_bitmap;
1060}
1061
1062static wxBitmap *GetShrinkBitmap()
1063{
1064 static wxBitmap* s_bitmap = (wxBitmap *) NULL;
1065 static bool s_loaded = false;
1066
1067 if ( !s_loaded )
1068 {
1069 s_loaded = true; // set it to true anyhow, we won't try again
1070
1071 #if defined(__WXMSW__) || defined(__WXPM__)
1072 s_bitmap = new wxBitmap(_T("plot_shr_bmp"), wxBITMAP_TYPE_RESOURCE);
1073 #else
1074 s_bitmap = new wxBitmap( plot_shr_xpm );
1075 #endif
1076 }
1077
1078 return s_bitmap;
1079}
1080
1081static wxBitmap *GetZoomInBitmap()
1082{
1083 static wxBitmap* s_bitmap = (wxBitmap *) NULL;
1084 static bool s_loaded = false;
1085
1086 if ( !s_loaded )
1087 {
1088 s_loaded = true; // set it to true anyhow, we won't try again
1089
1090 #if defined(__WXMSW__) || defined(__WXPM__)
1091 s_bitmap = new wxBitmap(_T("plot_zin_bmp"), wxBITMAP_TYPE_RESOURCE);
1092 #else
1093 s_bitmap = new wxBitmap( plot_zin_xpm );
1094 #endif
1095 }
1096
1097 return s_bitmap;
1098}
1099
1100static wxBitmap *GetZoomOutBitmap()
1101{
1102 static wxBitmap* s_bitmap = (wxBitmap *) NULL;
1103 static bool s_loaded = false;
1104
1105 if ( !s_loaded )
1106 {
1107 s_loaded = true; // set it to true anyhow, we won't try again
1108
1109 #if defined(__WXMSW__) || defined(__WXPM__)
1110 s_bitmap = new wxBitmap(_T("plot_zot_bmp"), wxBITMAP_TYPE_RESOURCE);
1111 #else
1112 s_bitmap = new wxBitmap( plot_zot_xpm );
1113 #endif
1114 }
1115
1116 return s_bitmap;
1117}
1118
1119static wxBitmap *GetUpBitmap()
1120{
1121 static wxBitmap* s_bitmap = (wxBitmap *) NULL;
1122 static bool s_loaded = false;
1123
1124 if ( !s_loaded )
1125 {
1126 s_loaded = true; // set it to true anyhow, we won't try again
1127
1128 #if defined(__WXMSW__) || defined(__WXPM__)
1129 s_bitmap = new wxBitmap(_T("plot_up_bmp"), wxBITMAP_TYPE_RESOURCE);
1130 #else
1131 s_bitmap = new wxBitmap( plot_up_xpm );
1132 #endif
1133 }
1134
1135 return s_bitmap;
1136}
1137
1138static wxBitmap *GetDownBitmap()
1139{
1140 static wxBitmap* s_bitmap = (wxBitmap *) NULL;
1141 static bool s_loaded = false;
1142
1143 if ( !s_loaded )
1144 {
1145 s_loaded = true; // set it to true anyhow, we won't try again
1146
1147 #if defined(__WXMSW__) || defined(__WXPM__)
1148 s_bitmap = new wxBitmap(_T("plot_dwn_bmp"), wxBITMAP_TYPE_RESOURCE);
1149 #else
1150 s_bitmap = new wxBitmap( plot_dwn_xpm );
1151 #endif
1152 }
1153
1154 return s_bitmap;
1155}
1156