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