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