]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/plot/plot.cpp
Removed redundant file
[wxWidgets.git] / contrib / src / plot / plot.cpp
CommitLineData
8556b795
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
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"
27b1df43 36#include "wx/module.h"
8556b795
RR
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
adb85931
RR
53//----------------------------------------------------------------------------
54// event types
55//----------------------------------------------------------------------------
56
97ee90ee
MB
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);
adb85931 78
8556b795
RR
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
8556b795
RR
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{
f7a8c129 139 wxASSERT_MSG( on > 0, _T("plot index < 0") );
8556b795
RR
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 )
f7a8c129 207 : wxWindow( parent, -1, wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER, _T("plotarea") )
8556b795
RR
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
9c592b61 232 wxList::compatibility_iterator node = m_owner->m_curves.GetFirst();
8556b795
RR
233 while (node)
234 {
d301b8a2 235 wxPlotCurve *curve = (wxPlotCurve*)node->GetData();
8556b795
RR
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
6c43b66e 254 if (curve != m_owner->GetCurrentCurve())
8556b795
RR
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 {
6c43b66e 262 m_owner->SetCurrentCurve( curve );
8556b795
RR
263 }
264 }
265 return;
266 }
267
d301b8a2 268 node = node->GetNext();
8556b795
RR
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
d301b8a2 312 wxCoord last_y=0;
8556b795
RR
313 for (int x = start_x; x < end_x; x++)
314 {
315 double dy = (end - curve->GetY( (wxInt32)(x/zoom) )) / range;
d301b8a2 316 wxCoord y = (wxCoord)(dy * double_client_height) - offset_y - 1;
8556b795
RR
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 {
d301b8a2
DS
400 int update_x = upd.GetX() + view_x;
401#if 0
402 // unused var
403 int update_y = upd.GetY() + view_y;
404#endif
8556b795
RR
405 int update_width = upd.GetWidth();
406
8556b795
RR
407/*
408 if (m_owner->m_current)
409 {
410 dc.SetPen( *wxLIGHT_GREY_PEN );
411 int base_line = client_height - m_owner->m_current->GetOffsetY();
412 dc.DrawLine( update_x-1, base_line-1, update_x+update_width+2, base_line-1 );
413 }
414*/
415
9c592b61 416 wxList::compatibility_iterator node = m_owner->m_curves.GetFirst();
8556b795
RR
417 while (node)
418 {
d301b8a2 419 wxPlotCurve *curve = (wxPlotCurve*) node->GetData();
8556b795 420
6c43b66e 421 if (curve == m_owner->GetCurrentCurve())
8556b795
RR
422 dc.SetPen( *wxBLACK_PEN );
423 else
424 dc.SetPen( *wxGREY_PEN );
425
426 DrawCurve( &dc, curve, update_x-1, update_x+update_width+2 );
427
d301b8a2 428 node = node->GetNext();
8556b795
RR
429 }
430
431 dc.SetPen( *wxRED_PEN );
432
d301b8a2 433 node = m_owner->m_onOffCurves.GetFirst();
8556b795
RR
434 while (node)
435 {
d301b8a2 436 wxPlotOnOffCurve *curve = (wxPlotOnOffCurve*) node->GetData();
8556b795
RR
437
438 DrawOnOffCurve( &dc, curve, update_x-1, update_x+update_width+2 );
439
d301b8a2 440 node = node->GetNext();
8556b795
RR
441 }
442
443 upd ++;
444 }
445}
446
447void wxPlotArea::ScrollWindow( int dx, int dy, const wxRect *rect )
448{
449 wxWindow::ScrollWindow( dx, dy, rect );
450// m_owner->m_xaxis->ScrollWindow( dx, 0 );
451}
452
453//-----------------------------------------------------------------------------
454// wxPlotXAxisArea
455//-----------------------------------------------------------------------------
456
457IMPLEMENT_DYNAMIC_CLASS(wxPlotXAxisArea, wxWindow)
458
459BEGIN_EVENT_TABLE(wxPlotXAxisArea, wxWindow)
460 EVT_PAINT( wxPlotXAxisArea::OnPaint)
461 EVT_LEFT_DOWN( wxPlotXAxisArea::OnMouse)
462END_EVENT_TABLE()
463
464wxPlotXAxisArea::wxPlotXAxisArea( wxPlotWindow *parent )
f7a8c129 465 : wxWindow( parent, -1, wxDefaultPosition, wxSize(-1,40), 0, _T("plotxaxisarea") )
8556b795
RR
466{
467 m_owner = parent;
468
469 SetBackgroundColour( *wxWHITE );
470 SetFont( *wxSMALL_FONT );
471}
472
473void wxPlotXAxisArea::OnMouse( wxMouseEvent &event )
474{
475 int client_width;
476 int client_height;
477 GetClientSize( &client_width, &client_height);
478 int view_x;
479 int view_y;
480 m_owner->GetViewStart( &view_x, &view_y );
481 view_x *= wxPLOT_SCROLL_STEP;
482 view_y *= wxPLOT_SCROLL_STEP;
483
d301b8a2
DS
484 wxCoord x = event.GetX() + view_x;
485 wxCoord y = event.GetY() + view_y;
8556b795 486
d301b8a2
DS
487 /* TO DO: do something here */
488 wxUnusedVar(x);
489 wxUnusedVar(y);
8556b795
RR
490}
491
492void wxPlotXAxisArea::OnPaint( wxPaintEvent &WXUNUSED(event) )
493{
494 int view_x;
495 int view_y;
496 m_owner->GetViewStart( &view_x, &view_y );
497 view_x *= wxPLOT_SCROLL_STEP;
498 view_y *= wxPLOT_SCROLL_STEP;
499
500 wxPaintDC dc( this );
501
502 int client_width;
503 int client_height;
504 GetClientSize( &client_width, &client_height);
505
506 double zoom = m_owner->GetZoom();
507
508 double ups = m_owner->GetUnitsPerValue() / zoom;
509
510 double start = view_x * ups;
511 double end = (view_x + client_width) * ups;
512 double range = end - start;
513
514 int int_log_range = (int)floor( log10( range ) );
515 double step = 1.0;
516 if (int_log_range > 0)
517 {
518 for (int i = 0; i < int_log_range; i++)
519 step *= 10;
520 }
521 if (int_log_range < 0)
522 {
523 for (int i = 0; i < -int_log_range; i++)
524 step /= 10;
525 }
526 double lower = ceil(start / step) * step;
527 double upper = floor(end / step) * step;
528
529 // if too few values, shrink size
530 if ((range/step) < 4)
531 {
532 step /= 2;
533 if (lower-step > start) lower -= step;
534 if (upper+step < end) upper += step;
535 }
536
537 // if still too few, again
538 if ((range/step) < 4)
539 {
540 step /= 2;
541 if (lower-step > start) lower -= step;
542 if (upper+step < end) upper += step;
543 }
544
545 dc.SetBrush( *wxWHITE_BRUSH );
d7313a61 546 dc.SetPen( *wxTRANSPARENT_PEN );
8556b795
RR
547 dc.DrawRectangle( 4, 5, client_width-14, 10 );
548 dc.DrawRectangle( 0, 20, client_width, 20 );
549 dc.SetPen( *wxBLACK_PEN );
550
551 double current = lower;
552 while (current < upper+(step/2))
553 {
554 int x = (int)ceil((current-start) / range * (double)client_width) - 1;
555 if ((x > 4) && (x < client_width-25))
556 {
557 dc.DrawLine( x, 5, x, 15 );
558 wxString label;
559 if (range < 50)
560 {
f7a8c129
JS
561 label.Printf( _T("%f"), current );
562 while (label.Last() == _T('0'))
8556b795 563 label.RemoveLast();
f7a8c129
JS
564 if ((label.Last() == _T('.')) || (label.Last() == _T(',')))
565 label.Append( _T('0') );
8556b795
RR
566 }
567 else
f7a8c129 568 label.Printf( _T("%d"), (int)floor(current) );
8556b795
RR
569 dc.DrawText( label, x-4, 20 );
570 }
571
572 current += step;
573 }
574
575 dc.DrawLine( 0, 15, client_width-8, 15 );
576 dc.DrawLine( client_width-4, 15, client_width-10, 10 );
577 dc.DrawLine( client_width-4, 15, client_width-10, 20 );
578}
579
580//-----------------------------------------------------------------------------
581// wxPlotYAxisArea
582//-----------------------------------------------------------------------------
583
584IMPLEMENT_DYNAMIC_CLASS(wxPlotYAxisArea, wxWindow)
585
586BEGIN_EVENT_TABLE(wxPlotYAxisArea, wxWindow)
587 EVT_PAINT( wxPlotYAxisArea::OnPaint)
588 EVT_LEFT_DOWN( wxPlotYAxisArea::OnMouse)
589END_EVENT_TABLE()
590
591wxPlotYAxisArea::wxPlotYAxisArea( wxPlotWindow *parent )
f7a8c129 592 : wxWindow( parent, -1, wxDefaultPosition, wxSize(60,-1), 0, _T("plotyaxisarea") )
8556b795
RR
593{
594 m_owner = parent;
595
596 SetBackgroundColour( *wxWHITE );
597 SetFont( *wxSMALL_FONT );
598}
599
600void wxPlotYAxisArea::OnMouse( wxMouseEvent &WXUNUSED(event) )
601{
602 /* do something here */
603}
604
605void wxPlotYAxisArea::OnPaint( wxPaintEvent &WXUNUSED(event) )
606{
607 wxPaintDC dc( this );
608
6c43b66e 609 wxPlotCurve *curve = m_owner->GetCurrentCurve();
8556b795
RR
610
611 if (!curve) return;
612
613 int client_width;
614 int client_height;
615 GetClientSize( &client_width, &client_height);
616
617
618 double range = curve->GetEndY() - curve->GetStartY();
619 double offset = ((double) curve->GetOffsetY() / (double)client_height ) * range;
620 double start = curve->GetStartY() - offset;
621 double end = curve->GetEndY() - offset;
622
623 int int_log_range = (int)floor( log10( range ) );
624 double step = 1.0;
625 if (int_log_range > 0)
626 {
627 for (int i = 0; i < int_log_range; i++)
628 step *= 10;
629 }
630 if (int_log_range < 0)
631 {
632 for (int i = 0; i < -int_log_range; i++)
633 step /= 10;
634 }
635 double lower = ceil(start / step) * step;
636 double upper = floor(end / step) * step;
637
638 // if too few values, shrink size
639 if ((range/step) < 4)
640 {
641 step /= 2;
642 if (lower-step > start) lower -= step;
643 if (upper+step < end) upper += step;
644 }
645
646 // if still too few, again
647 if ((range/step) < 4)
648 {
649 step /= 2;
650 if (lower-step > start) lower -= step;
651 if (upper+step < end) upper += step;
652 }
653
654 dc.SetPen( *wxBLACK_PEN );
655
656 double current = lower;
657 while (current < upper+(step/2))
658 {
659 int y = (int)((curve->GetEndY()-current) / range * (double)client_height) - 1;
660 y -= curve->GetOffsetY();
661 if ((y > 10) && (y < client_height-7))
662 {
663 dc.DrawLine( client_width-15, y, client_width-7, y );
664 wxString label;
665 if (range < 50)
666 {
f7a8c129
JS
667 label.Printf( _T("%f"), current );
668 while (label.Last() == _T('0'))
8556b795 669 label.RemoveLast();
f7a8c129
JS
670 if ((label.Last() == _T('.')) || (label.Last() == _T(',')))
671 label.Append( _T('0') );
8556b795
RR
672 }
673 else
f7a8c129 674 label.Printf( _T("%d"), (int)floor(current) );
8556b795
RR
675 dc.DrawText( label, 5, y-7 );
676 }
677
678 current += step;
679 }
680
681 dc.DrawLine( client_width-15, 6, client_width-15, client_height );
682 dc.DrawLine( client_width-15, 2, client_width-20, 8 );
683 dc.DrawLine( client_width-15, 2, client_width-10, 8 );
684}
685
686//-----------------------------------------------------------------------------
687// wxPlotWindow
688//-----------------------------------------------------------------------------
689
690#define ID_ENLARGE 1000
691#define ID_SHRINK 1002
692
693#define ID_MOVE_UP 1006
694#define ID_MOVE_DOWN 1007
695
696#define ID_ZOOM_IN 1010
697#define ID_ZOOM_OUT 1011
698
699
700IMPLEMENT_DYNAMIC_CLASS(wxPlotWindow, wxScrolledWindow)
701
702BEGIN_EVENT_TABLE(wxPlotWindow, wxScrolledWindow)
703 EVT_BUTTON( ID_MOVE_UP, wxPlotWindow::OnMoveUp)
704 EVT_BUTTON( ID_MOVE_DOWN, wxPlotWindow::OnMoveDown)
705
706 EVT_BUTTON( ID_ENLARGE, wxPlotWindow::OnEnlarge)
707 EVT_BUTTON( ID_SHRINK, wxPlotWindow::OnShrink)
708
709 EVT_BUTTON( ID_ZOOM_IN, wxPlotWindow::OnZoomIn)
710 EVT_BUTTON( ID_ZOOM_OUT, wxPlotWindow::OnZoomOut)
711
712 EVT_SCROLLWIN( wxPlotWindow::OnScroll2)
713END_EVENT_TABLE()
714
715wxPlotWindow::wxPlotWindow( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag )
f7a8c129 716 : wxScrolledWindow( parent, id, pos, size, flag, _T("plotcanvas") )
8556b795
RR
717{
718 m_xUnitsPerValue = 1.0;
719 m_xZoom = 1.0;
720
721 m_enlargeAroundWindowCentre = FALSE;
722 m_scrollOnThumbRelease = FALSE;
723
724 m_area = new wxPlotArea( this );
725 wxBoxSizer *mainsizer = new wxBoxSizer( wxHORIZONTAL );
726
727 if ((GetWindowStyleFlag() & wxPLOT_BUTTON_ALL) != 0)
728 {
729 wxBoxSizer *buttonlist = new wxBoxSizer( wxVERTICAL );
730 if ((GetWindowStyleFlag() & wxPLOT_BUTTON_ENLARGE) != 0)
731 {
732 buttonlist->Add( new wxBitmapButton( this, ID_ENLARGE, *GetEnlargeBitmap() ), 0, wxEXPAND|wxALL, 2 );
733 buttonlist->Add( new wxBitmapButton( this, ID_SHRINK, *GetShrinkBitmap() ), 0, wxEXPAND|wxALL, 2 );
734 buttonlist->Add( 20,10, 0 );
735 }
736 if ((GetWindowStyleFlag() & wxPLOT_BUTTON_MOVE) != 0)
737 {
738 buttonlist->Add( new wxBitmapButton( this, ID_MOVE_UP, *GetUpBitmap() ), 0, wxEXPAND|wxALL, 2 );
739 buttonlist->Add( new wxBitmapButton( this, ID_MOVE_DOWN, *GetDownBitmap() ), 0, wxEXPAND|wxALL, 2 );
740 buttonlist->Add( 20,10, 0 );
741 }
742 if ((GetWindowStyleFlag() & wxPLOT_BUTTON_ZOOM) != 0)
743 {
744 buttonlist->Add( new wxBitmapButton( this, ID_ZOOM_IN, *GetZoomInBitmap() ), 0, wxEXPAND|wxALL, 2 );
745 buttonlist->Add( new wxBitmapButton( this, ID_ZOOM_OUT, *GetZoomOutBitmap() ), 0, wxEXPAND|wxALL, 2 );
746 }
747 mainsizer->Add( buttonlist, 0, wxEXPAND|wxALL, 4 );
748 }
749
750 wxBoxSizer *plotsizer = new wxBoxSizer( wxHORIZONTAL );
751
752 if ((GetWindowStyleFlag() & wxPLOT_Y_AXIS) != 0)
753 {
754 m_yaxis = new wxPlotYAxisArea( this );
755
756 wxBoxSizer *vert1 = new wxBoxSizer( wxVERTICAL );
757 plotsizer->Add( vert1, 0, wxEXPAND );
758 vert1->Add( m_yaxis, 1 );
759 if ((GetWindowStyleFlag() & wxPLOT_X_AXIS) != 0)
760 vert1->Add( 60, 40 );
761 }
762 else
763 {
764 m_yaxis = (wxPlotYAxisArea*) NULL;
765 }
766
767 if ((GetWindowStyleFlag() & wxPLOT_X_AXIS) != 0)
768 {
769 m_xaxis = new wxPlotXAxisArea( this );
770
771 wxBoxSizer *vert2 = new wxBoxSizer( wxVERTICAL );
772 plotsizer->Add( vert2, 1, wxEXPAND );
773 vert2->Add( m_area, 1, wxEXPAND );
774 vert2->Add( m_xaxis, 0, wxEXPAND );
775 }
776 else
777 {
778 plotsizer->Add( m_area, 1, wxEXPAND );
779 m_xaxis = (wxPlotXAxisArea*) NULL;
780 }
781
782 mainsizer->Add( plotsizer, 1, wxEXPAND );
783
784 SetAutoLayout( TRUE );
785 SetSizer( mainsizer );
786
787 SetTargetWindow( m_area );
788
789 SetBackgroundColour( *wxWHITE );
790
791 m_current = (wxPlotCurve*) NULL;
792}
793
794wxPlotWindow::~wxPlotWindow()
795{
796}
797
798void wxPlotWindow::Add( wxPlotCurve *curve )
799{
800 m_curves.Append( curve );
801 if (!m_current) m_current = curve;
802
803 ResetScrollbar();
804}
805
806size_t wxPlotWindow::GetCount()
807{
808 return m_curves.GetCount();
809}
810
811wxPlotCurve *wxPlotWindow::GetAt( size_t n )
812{
9c592b61 813 wxList::compatibility_iterator node = m_curves.Item( n );
8556b795
RR
814 if (!node)
815 return (wxPlotCurve*) NULL;
816
d301b8a2 817 return (wxPlotCurve*) node->GetData();
8556b795
RR
818}
819
6c43b66e 820void wxPlotWindow::SetCurrentCurve( wxPlotCurve* current )
8556b795
RR
821{
822 m_current = current;
823 m_area->Refresh( FALSE );
824
825 RedrawYAxis();
826
827 wxPlotEvent event( wxEVT_PLOT_SEL_CHANGED, GetId() );
828 event.SetEventObject( this );
829 event.SetZoom( GetZoom() );
830 event.SetCurve( m_current );
831 GetEventHandler()->ProcessEvent( event );
832}
833
834void wxPlotWindow::Delete( wxPlotCurve* curve )
835{
9c592b61 836 wxList::compatibility_iterator node = m_curves.Find( curve );
8556b795
RR
837 if (!node) return;
838
839 m_curves.DeleteObject( curve );
840
841 m_area->DeleteCurve( curve );
842 m_area->Refresh( FALSE );
08a80932
JS
843
844 if (curve == m_current) m_current = (wxPlotCurve *) NULL;
8556b795
RR
845}
846
6c43b66e 847wxPlotCurve *wxPlotWindow::GetCurrentCurve()
8556b795
RR
848{
849 return m_current;
850}
851
852void wxPlotWindow::Add( wxPlotOnOffCurve *curve )
853{
854 m_onOffCurves.Append( curve );
855}
856
857void wxPlotWindow::Delete( wxPlotOnOffCurve* curve )
858{
9c592b61 859 wxList::compatibility_iterator node = m_onOffCurves.Find( curve );
8556b795
RR
860 if (!node) return;
861
862 m_onOffCurves.DeleteObject( curve );
863}
864
865size_t wxPlotWindow::GetOnOffCurveCount()
866{
867 return m_onOffCurves.GetCount();
868}
869
870wxPlotOnOffCurve *wxPlotWindow::GetOnOffCurveAt( size_t n )
871{
9c592b61 872 wxList::compatibility_iterator node = m_onOffCurves.Item( n );
8556b795
RR
873 if (!node)
874 return (wxPlotOnOffCurve*) NULL;
875
d301b8a2 876 return (wxPlotOnOffCurve*) node->GetData();
8556b795
RR
877}
878
879void wxPlotWindow::Move( wxPlotCurve* curve, int pixels_up )
880{
881 m_area->DeleteCurve( curve );
882
883 curve->SetOffsetY( curve->GetOffsetY() + pixels_up );
884
885 m_area->Refresh( FALSE );
886
887 RedrawYAxis();
888}
889
890void wxPlotWindow::OnMoveUp( wxCommandEvent& WXUNUSED(event) )
891{
892 if (!m_current) return;
893
894 Move( m_current, 25 );
895}
896
897void wxPlotWindow::OnMoveDown( wxCommandEvent& WXUNUSED(event) )
898{
899 if (!m_current) return;
900
901 Move( m_current, -25 );
902}
903
904void wxPlotWindow::Enlarge( wxPlotCurve *curve, double factor )
905{
906 m_area->DeleteCurve( curve );
907
908 int client_width;
909 int client_height;
910 m_area->GetClientSize( &client_width, &client_height);
911 double offset = (double)curve->GetOffsetY() / (double)client_height;
912
913 double range = curve->GetEndY() - curve->GetStartY();
914 offset *= range;
915
916 double new_range = range / factor;
917 double new_offset = offset / factor;
918
919 if (m_enlargeAroundWindowCentre)
920 {
921 double middle = curve->GetStartY() - offset + range/2;
922
923 curve->SetStartY( middle - new_range / 2 + new_offset );
924 curve->SetEndY( middle + new_range / 2 + new_offset );
925 }
926 else
927 {
928 curve->SetStartY( (curve->GetStartY() - offset)/factor + new_offset );
929 curve->SetEndY( (curve->GetEndY() - offset)/factor + new_offset );
930 }
931
932 m_area->Refresh( FALSE );
933 RedrawYAxis();
934}
935
936void wxPlotWindow::SetUnitsPerValue( double upv )
937{
938 m_xUnitsPerValue = upv;
939
940 RedrawXAxis();
941}
942
943void wxPlotWindow::SetZoom( double zoom )
944{
945 double old_zoom = m_xZoom;
946 m_xZoom = zoom;
947
948 int view_x = 0;
949 int view_y = 0;
950 GetViewStart( &view_x, &view_y );
951
952 wxInt32 max = 0;
9c592b61 953 wxList::compatibility_iterator node = m_curves.GetFirst();
8556b795
RR
954 while (node)
955 {
d301b8a2 956 wxPlotCurve *curve = (wxPlotCurve*) node->GetData();
8556b795
RR
957 if (curve->GetEndX() > max)
958 max = curve->GetEndX();
d301b8a2 959 node = node->GetNext();
8556b795
RR
960 }
961 SetScrollbars( wxPLOT_SCROLL_STEP, wxPLOT_SCROLL_STEP,
962 (int)((max*m_xZoom)/wxPLOT_SCROLL_STEP)+1, 0,
963 (int)(view_x*zoom/old_zoom), 0,
964 TRUE );
965
966 RedrawXAxis();
967 m_area->Refresh( TRUE );
968}
969
970void wxPlotWindow::ResetScrollbar()
971{
972 wxInt32 max = 0;
9c592b61 973 wxList::compatibility_iterator node = m_curves.GetFirst();
8556b795
RR
974 while (node)
975 {
d301b8a2 976 wxPlotCurve *curve = (wxPlotCurve*) node->GetData();
8556b795
RR
977 if (curve->GetEndX() > max)
978 max = curve->GetEndX();
d301b8a2 979 node = node->GetNext();
8556b795
RR
980 }
981
982 SetScrollbars( wxPLOT_SCROLL_STEP, wxPLOT_SCROLL_STEP,
983 (int)(((max*m_xZoom)/wxPLOT_SCROLL_STEP)+1), 0 );
984}
985
986void wxPlotWindow::RedrawXAxis()
987{
988 if (m_xaxis)
989 m_xaxis->Refresh( FALSE );
990}
991
992void wxPlotWindow::RedrawYAxis()
993{
994 if (m_yaxis)
995 m_yaxis->Refresh( TRUE );
996}
997
998void wxPlotWindow::RedrawEverything()
999{
1000 if (m_xaxis)
1001 m_xaxis->Refresh( TRUE );
1002 if (m_yaxis)
1003 m_yaxis->Refresh( TRUE );
1004 m_area->Refresh( TRUE );
1005}
1006
1007void wxPlotWindow::OnZoomIn( wxCommandEvent& WXUNUSED(event) )
1008{
1009 SetZoom( m_xZoom * 1.5 );
1010}
1011
1012void wxPlotWindow::OnZoomOut( wxCommandEvent& WXUNUSED(event) )
1013{
1014 SetZoom( m_xZoom * 0.6666 );
1015}
1016
1017void wxPlotWindow::OnEnlarge( wxCommandEvent& WXUNUSED(event) )
1018{
1019 if (!m_current) return;
1020
1021 Enlarge( m_current, 1.5 );
1022}
1023
1024void wxPlotWindow::OnShrink( wxCommandEvent& WXUNUSED(event) )
1025{
1026 if (!m_current) return;
1027
1028 Enlarge( m_current, 0.6666666 );
1029}
1030
1031void wxPlotWindow::OnScroll2( wxScrollWinEvent& event )
1032{
1033 if ((!m_scrollOnThumbRelease) || (event.GetEventType() != wxEVT_SCROLLWIN_THUMBTRACK))
1034 {
1035 wxScrolledWindow::OnScroll( event );
1036 RedrawXAxis();
1037 }
1038}
1039
1040// ----------------------------------------------------------------------------
1041// global functions
1042// ----------------------------------------------------------------------------
1043
1044// FIXME MT-UNSAFE
1045static wxBitmap *GetEnlargeBitmap()
1046{
1047 static wxBitmap* s_bitmap = (wxBitmap *) NULL;
1048 static bool s_loaded = FALSE;
1049
1050 if ( !s_loaded )
1051 {
1052 s_loaded = TRUE; // set it to TRUE anyhow, we won't try again
1053
1054 #if defined(__WXMSW__) || defined(__WXPM__)
f7a8c129 1055 s_bitmap = new wxBitmap(_T("plot_enl_bmp"), wxBITMAP_TYPE_RESOURCE);
8556b795
RR
1056 #else
1057 s_bitmap = new wxBitmap( plot_enl_xpm );
1058 #endif
1059 }
1060
1061 return s_bitmap;
1062}
1063
1064static wxBitmap *GetShrinkBitmap()
1065{
1066 static wxBitmap* s_bitmap = (wxBitmap *) NULL;
1067 static bool s_loaded = FALSE;
1068
1069 if ( !s_loaded )
1070 {
1071 s_loaded = TRUE; // set it to TRUE anyhow, we won't try again
1072
1073 #if defined(__WXMSW__) || defined(__WXPM__)
f7a8c129 1074 s_bitmap = new wxBitmap(_T("plot_shr_bmp"), wxBITMAP_TYPE_RESOURCE);
8556b795
RR
1075 #else
1076 s_bitmap = new wxBitmap( plot_shr_xpm );
1077 #endif
1078 }
1079
1080 return s_bitmap;
1081}
1082
1083static wxBitmap *GetZoomInBitmap()
1084{
1085 static wxBitmap* s_bitmap = (wxBitmap *) NULL;
1086 static bool s_loaded = FALSE;
1087
1088 if ( !s_loaded )
1089 {
1090 s_loaded = TRUE; // set it to TRUE anyhow, we won't try again
1091
1092 #if defined(__WXMSW__) || defined(__WXPM__)
f7a8c129 1093 s_bitmap = new wxBitmap(_T("plot_zin_bmp"), wxBITMAP_TYPE_RESOURCE);
8556b795
RR
1094 #else
1095 s_bitmap = new wxBitmap( plot_zin_xpm );
1096 #endif
1097 }
1098
1099 return s_bitmap;
1100}
1101
1102static wxBitmap *GetZoomOutBitmap()
1103{
1104 static wxBitmap* s_bitmap = (wxBitmap *) NULL;
1105 static bool s_loaded = FALSE;
1106
1107 if ( !s_loaded )
1108 {
1109 s_loaded = TRUE; // set it to TRUE anyhow, we won't try again
1110
1111 #if defined(__WXMSW__) || defined(__WXPM__)
f7a8c129 1112 s_bitmap = new wxBitmap(_T("plot_zot_bmp"), wxBITMAP_TYPE_RESOURCE);
8556b795
RR
1113 #else
1114 s_bitmap = new wxBitmap( plot_zot_xpm );
1115 #endif
1116 }
1117
1118 return s_bitmap;
1119}
1120
1121static wxBitmap *GetUpBitmap()
1122{
1123 static wxBitmap* s_bitmap = (wxBitmap *) NULL;
1124 static bool s_loaded = FALSE;
1125
1126 if ( !s_loaded )
1127 {
1128 s_loaded = TRUE; // set it to TRUE anyhow, we won't try again
1129
1130 #if defined(__WXMSW__) || defined(__WXPM__)
f7a8c129 1131 s_bitmap = new wxBitmap(_T("plot_up_bmp"), wxBITMAP_TYPE_RESOURCE);
8556b795
RR
1132 #else
1133 s_bitmap = new wxBitmap( plot_up_xpm );
1134 #endif
1135 }
1136
1137 return s_bitmap;
1138}
1139
1140static wxBitmap *GetDownBitmap()
1141{
1142 static wxBitmap* s_bitmap = (wxBitmap *) NULL;
1143 static bool s_loaded = FALSE;
1144
1145 if ( !s_loaded )
1146 {
1147 s_loaded = TRUE; // set it to TRUE anyhow, we won't try again
1148
1149 #if defined(__WXMSW__) || defined(__WXPM__)
f7a8c129 1150 s_bitmap = new wxBitmap(_T("plot_dwn_bmp"), wxBITMAP_TYPE_RESOURCE);
8556b795
RR
1151 #else
1152 s_bitmap = new wxBitmap( plot_dwn_xpm );
1153 #endif
1154 }
1155
1156 return s_bitmap;
1157}
1158