]>
Commit | Line | Data |
---|---|---|
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 | #if wxUSE_PLOT | |
24 | ||
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" | |
37 | #include "wx/bmpbuttn.h" | |
38 | ||
39 | #include <math.h> | |
40 | ||
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 | ||
58 | static wxBitmap *GetEnlargeBitmap(); | |
59 | static wxBitmap *GetShrinkBitmap(); | |
60 | static wxBitmap *GetZoomInBitmap(); | |
61 | static wxBitmap *GetZoomOutBitmap(); | |
62 | static wxBitmap *GetUpBitmap(); | |
63 | static wxBitmap *GetDownBitmap(); | |
64 | ||
65 | //----------------------------------------------------------------------------- | |
66 | // consts | |
67 | //----------------------------------------------------------------------------- | |
68 | ||
69 | #define wxPLOT_SCROLL_STEP 30 | |
70 | ||
71 | //----------------------------------------------------------------------------- | |
72 | // wxPlotEvent | |
73 | //----------------------------------------------------------------------------- | |
74 | ||
75 | wxPlotEvent::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 | ||
83 | //----------------------------------------------------------------------------- | |
84 | // wxPlotCurve | |
85 | //----------------------------------------------------------------------------- | |
86 | ||
87 | IMPLEMENT_ABSTRACT_CLASS(wxPlotCurve, wxObject) | |
88 | ||
89 | wxPlotCurve::wxPlotCurve( int offsetY, double startY, double endY ) | |
90 | { | |
91 | m_offsetY = offsetY; | |
92 | m_startY = startY; | |
93 | m_endY = endY; | |
94 | } | |
95 | ||
96 | //----------------------------------------------------------------------------- | |
97 | // wxPlotOnOffCurve | |
98 | //----------------------------------------------------------------------------- | |
99 | ||
100 | IMPLEMENT_CLASS(wxPlotOnOffCurve, wxObject) | |
101 | ||
102 | #include "wx/arrimpl.cpp" | |
103 | WX_DEFINE_OBJARRAY(wxArrayPlotOnOff); | |
104 | ||
105 | wxPlotOnOffCurve::wxPlotOnOffCurve( int offsetY ) | |
106 | { | |
107 | m_offsetY = offsetY; | |
108 | m_minX = -1; | |
109 | m_maxX = -1; | |
110 | } | |
111 | ||
112 | void 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 | ||
129 | size_t wxPlotOnOffCurve::GetCount() | |
130 | { | |
131 | return m_marks.GetCount(); | |
132 | } | |
133 | ||
134 | wxInt32 wxPlotOnOffCurve::GetOn( size_t index ) | |
135 | { | |
136 | wxPlotOnOff *v = &m_marks.Item( index ); | |
137 | return v->m_on; | |
138 | } | |
139 | ||
140 | wxInt32 wxPlotOnOffCurve::GetOff( size_t index ) | |
141 | { | |
142 | wxPlotOnOff *v = &m_marks.Item( index ); | |
143 | return v->m_off; | |
144 | } | |
145 | ||
146 | void* wxPlotOnOffCurve::GetClientData( size_t index ) | |
147 | { | |
148 | wxPlotOnOff *v = &m_marks.Item( index ); | |
149 | return v->m_clientData; | |
150 | } | |
151 | ||
152 | wxPlotOnOff *wxPlotOnOffCurve::GetAt( size_t index ) | |
153 | { | |
154 | return &m_marks.Item( index ); | |
155 | } | |
156 | ||
157 | void 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 | ||
164 | void wxPlotOnOffCurve::DrawOffLine( wxDC &dc, wxCoord y, wxCoord start, wxCoord end ) | |
165 | { | |
166 | dc.DrawLine( start, y, end, y ); | |
167 | } | |
168 | ||
169 | //----------------------------------------------------------------------------- | |
170 | // wxPlotArea | |
171 | //----------------------------------------------------------------------------- | |
172 | ||
173 | IMPLEMENT_DYNAMIC_CLASS(wxPlotArea, wxWindow) | |
174 | ||
175 | BEGIN_EVENT_TABLE(wxPlotArea, wxWindow) | |
176 | EVT_PAINT( wxPlotArea::OnPaint) | |
177 | EVT_LEFT_DOWN( wxPlotArea::OnMouse) | |
178 | EVT_LEFT_DCLICK( wxPlotArea::OnMouse) | |
179 | END_EVENT_TABLE() | |
180 | ||
181 | wxPlotArea::wxPlotArea( wxPlotWindow *parent ) | |
182 | : wxWindow( parent, -1, wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER, "plotarea" ) | |
183 | { | |
184 | m_owner = parent; | |
185 | ||
186 | m_zooming = FALSE; | |
187 | ||
188 | SetBackgroundColour( *wxWHITE ); | |
189 | } | |
190 | ||
191 | void 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 ); | |
199 | view_x *= wxPLOT_SCROLL_STEP; | |
200 | view_y *= wxPLOT_SCROLL_STEP; | |
201 | ||
202 | wxCoord x = event.GetX(); | |
203 | wxCoord y = event.GetY(); | |
204 | x += view_x; | |
205 | y += view_y; | |
206 | ||
207 | wxNode *node = m_owner->m_curves.First(); | |
208 | while (node) | |
209 | { | |
210 | wxPlotCurve *curve = (wxPlotCurve*)node->Data(); | |
211 | ||
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(); | |
216 | ||
217 | double dy = (end - curve->GetY( (wxInt32)(x/m_owner->GetZoom()) )) / range; | |
218 | wxCoord curve_y = (wxCoord)(dy * double_client_height) - offset_y - 1; | |
219 | ||
220 | if ((y-curve_y < 4) && (y-curve_y > -4)) | |
221 | { | |
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 | ||
229 | if (curve != m_owner->GetCurrent()) | |
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 | } | |
240 | return; | |
241 | } | |
242 | ||
243 | node = node->Next(); | |
244 | } | |
245 | } | |
246 | ||
247 | void 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 | ||
255 | void wxPlotArea::DrawCurve( wxDC *dc, wxPlotCurve *curve, int from, int to ) | |
256 | { | |
257 | int view_x; | |
258 | int view_y; | |
259 | m_owner->GetViewStart( &view_x, &view_y ); | |
260 | view_x *= wxPLOT_SCROLL_STEP; | |
261 | ||
262 | if (from == -1) | |
263 | from = view_x; | |
264 | ||
265 | int client_width; | |
266 | int client_height; | |
267 | GetClientSize( &client_width, &client_height); | |
268 | ||
269 | if (to == -1) | |
270 | to = view_x + client_width; | |
271 | ||
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) ); | |
276 | ||
277 | start_x = wxMax( view_x, start_x ); | |
278 | end_x = wxMin( view_x + client_width, end_x ); | |
279 | ||
280 | end_x++; | |
281 | ||
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(); | |
286 | ||
287 | wxCoord y=0,last_y=0; | |
288 | for (int x = start_x; x < end_x; x++) | |
289 | { | |
290 | double dy = (end - curve->GetY( (wxInt32)(x/zoom) )) / range; | |
291 | y = (wxCoord)(dy * double_client_height) - offset_y - 1; | |
292 | ||
293 | if (x != start_x) | |
294 | dc->DrawLine( x-1, last_y, x, y ); | |
295 | ||
296 | last_y = y; | |
297 | } | |
298 | } | |
299 | ||
300 | void 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 | ||
360 | void wxPlotArea::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
361 | { | |
362 | int view_x; | |
363 | int view_y; | |
364 | m_owner->GetViewStart( &view_x, &view_y ); | |
365 | view_x *= wxPLOT_SCROLL_STEP; | |
366 | view_y *= wxPLOT_SCROLL_STEP; | |
367 | ||
368 | wxPaintDC dc( this ); | |
369 | m_owner->PrepareDC( dc ); | |
370 | ||
371 | wxRegionIterator upd( GetUpdateRegion() ); | |
372 | ||
373 | while (upd) | |
374 | { | |
375 | int update_x = upd.GetX(); | |
376 | int update_y = upd.GetY(); | |
377 | int update_width = upd.GetWidth(); | |
378 | ||
379 | update_x += view_x; | |
380 | update_y += view_y; | |
381 | ||
382 | /* | |
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 | } | |
389 | */ | |
390 | ||
391 | wxNode *node = m_owner->m_curves.First(); | |
392 | while (node) | |
393 | { | |
394 | wxPlotCurve *curve = (wxPlotCurve*) node->Data(); | |
395 | ||
396 | if (curve == m_owner->GetCurrent()) | |
397 | dc.SetPen( *wxBLACK_PEN ); | |
398 | else | |
399 | dc.SetPen( *wxGREY_PEN ); | |
400 | ||
401 | DrawCurve( &dc, curve, update_x-1, update_x+update_width+2 ); | |
402 | ||
403 | node = node->Next(); | |
404 | } | |
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 | ||
418 | upd ++; | |
419 | } | |
420 | } | |
421 | ||
422 | void 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 | ||
432 | IMPLEMENT_DYNAMIC_CLASS(wxPlotXAxisArea, wxWindow) | |
433 | ||
434 | BEGIN_EVENT_TABLE(wxPlotXAxisArea, wxWindow) | |
435 | EVT_PAINT( wxPlotXAxisArea::OnPaint) | |
436 | EVT_LEFT_DOWN( wxPlotXAxisArea::OnMouse) | |
437 | END_EVENT_TABLE() | |
438 | ||
439 | wxPlotXAxisArea::wxPlotXAxisArea( wxPlotWindow *parent ) | |
440 | : wxWindow( parent, -1, wxDefaultPosition, wxSize(-1,40), 0, "plotxaxisarea" ) | |
441 | { | |
442 | m_owner = parent; | |
443 | ||
444 | SetBackgroundColour( *wxWHITE ); | |
445 | SetFont( *wxSMALL_FONT ); | |
446 | } | |
447 | ||
448 | void 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 ); | |
456 | view_x *= wxPLOT_SCROLL_STEP; | |
457 | view_y *= wxPLOT_SCROLL_STEP; | |
458 | ||
459 | wxCoord x = event.GetX(); | |
460 | wxCoord y = event.GetY(); | |
461 | x += view_x; | |
462 | y += view_y; | |
463 | ||
464 | /* do something here */ | |
465 | } | |
466 | ||
467 | void wxPlotXAxisArea::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
468 | { | |
469 | int view_x; | |
470 | int view_y; | |
471 | m_owner->GetViewStart( &view_x, &view_y ); | |
472 | view_x *= wxPLOT_SCROLL_STEP; | |
473 | view_y *= wxPLOT_SCROLL_STEP; | |
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; | |
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 | } | |
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 | ||
559 | IMPLEMENT_DYNAMIC_CLASS(wxPlotYAxisArea, wxWindow) | |
560 | ||
561 | BEGIN_EVENT_TABLE(wxPlotYAxisArea, wxWindow) | |
562 | EVT_PAINT( wxPlotYAxisArea::OnPaint) | |
563 | EVT_LEFT_DOWN( wxPlotYAxisArea::OnMouse) | |
564 | END_EVENT_TABLE() | |
565 | ||
566 | wxPlotYAxisArea::wxPlotYAxisArea( wxPlotWindow *parent ) | |
567 | : wxWindow( parent, -1, wxDefaultPosition, wxSize(60,-1), 0, "plotyaxisarea" ) | |
568 | { | |
569 | m_owner = parent; | |
570 | ||
571 | SetBackgroundColour( *wxWHITE ); | |
572 | SetFont( *wxSMALL_FONT ); | |
573 | } | |
574 | ||
575 | void wxPlotYAxisArea::OnMouse( wxMouseEvent &WXUNUSED(event) ) | |
576 | { | |
577 | /* do something here */ | |
578 | } | |
579 | ||
580 | void 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; | |
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) ); | |
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 | ||
661 | //----------------------------------------------------------------------------- | |
662 | // wxPlotWindow | |
663 | //----------------------------------------------------------------------------- | |
664 | ||
665 | #define ID_ENLARGE 1000 | |
666 | #define ID_SHRINK 1002 | |
667 | ||
668 | #define ID_MOVE_UP 1006 | |
669 | #define ID_MOVE_DOWN 1007 | |
670 | ||
671 | #define ID_ZOOM_IN 1010 | |
672 | #define ID_ZOOM_OUT 1011 | |
673 | ||
674 | ||
675 | IMPLEMENT_DYNAMIC_CLASS(wxPlotWindow, wxScrolledWindow) | |
676 | ||
677 | BEGIN_EVENT_TABLE(wxPlotWindow, wxScrolledWindow) | |
678 | EVT_BUTTON( ID_MOVE_UP, wxPlotWindow::OnMoveUp) | |
679 | EVT_BUTTON( ID_MOVE_DOWN, wxPlotWindow::OnMoveDown) | |
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) | |
688 | END_EVENT_TABLE() | |
689 | ||
690 | wxPlotWindow::wxPlotWindow( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag ) | |
691 | : wxScrolledWindow( parent, id, pos, size, flag, "plotcanvas" ) | |
692 | { | |
693 | m_xUnitsPerValue = 1.0; | |
694 | m_xZoom = 1.0; | |
695 | ||
696 | m_enlargeAroundWindowCentre = FALSE; | |
697 | m_scrollOnThumbRelease = FALSE; | |
698 | ||
699 | m_area = new wxPlotArea( this ); | |
700 | wxBoxSizer *mainsizer = new wxBoxSizer( wxHORIZONTAL ); | |
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 | } | |
756 | ||
757 | mainsizer->Add( plotsizer, 1, wxEXPAND ); | |
758 | ||
759 | SetAutoLayout( TRUE ); | |
760 | SetSizer( mainsizer ); | |
761 | ||
762 | SetTargetWindow( m_area ); | |
763 | ||
764 | SetBackgroundColour( *wxWHITE ); | |
765 | ||
766 | m_current = (wxPlotCurve*) NULL; | |
767 | } | |
768 | ||
769 | wxPlotWindow::~wxPlotWindow() | |
770 | { | |
771 | } | |
772 | ||
773 | void wxPlotWindow::Add( wxPlotCurve *curve ) | |
774 | { | |
775 | m_curves.Append( curve ); | |
776 | if (!m_current) m_current = curve; | |
777 | ||
778 | ResetScrollbar(); | |
779 | } | |
780 | ||
781 | size_t wxPlotWindow::GetCount() | |
782 | { | |
783 | return m_curves.GetCount(); | |
784 | } | |
785 | ||
786 | wxPlotCurve *wxPlotWindow::GetAt( size_t n ) | |
787 | { | |
788 | wxNode *node = m_curves.Nth( n ); | |
789 | if (!node) | |
790 | return (wxPlotCurve*) NULL; | |
791 | ||
792 | return (wxPlotCurve*) node->Data(); | |
793 | } | |
794 | ||
795 | void wxPlotWindow::SetCurrent( wxPlotCurve* current ) | |
796 | { | |
797 | m_current = current; | |
798 | m_area->Refresh( FALSE ); | |
799 | ||
800 | RedrawYAxis(); | |
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 ); | |
807 | } | |
808 | ||
809 | void 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 | ||
820 | wxPlotCurve *wxPlotWindow::GetCurrent() | |
821 | { | |
822 | return m_current; | |
823 | } | |
824 | ||
825 | void wxPlotWindow::Add( wxPlotOnOffCurve *curve ) | |
826 | { | |
827 | m_onOffCurves.Append( curve ); | |
828 | } | |
829 | ||
830 | void wxPlotWindow::Delete( wxPlotOnOffCurve* curve ) | |
831 | { | |
832 | wxNode *node = m_onOffCurves.Find( curve ); | |
833 | if (!node) return; | |
834 | ||
835 | m_onOffCurves.DeleteObject( curve ); | |
836 | } | |
837 | ||
838 | size_t wxPlotWindow::GetOnOffCurveCount() | |
839 | { | |
840 | return m_onOffCurves.GetCount(); | |
841 | } | |
842 | ||
843 | wxPlotOnOffCurve *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 | ||
852 | void wxPlotWindow::Move( wxPlotCurve* curve, int pixels_up ) | |
853 | { | |
854 | m_area->DeleteCurve( curve ); | |
855 | ||
856 | curve->SetOffsetY( curve->GetOffsetY() + pixels_up ); | |
857 | ||
858 | m_area->Refresh( FALSE ); | |
859 | ||
860 | RedrawYAxis(); | |
861 | } | |
862 | ||
863 | void wxPlotWindow::OnMoveUp( wxCommandEvent& WXUNUSED(event) ) | |
864 | { | |
865 | if (!m_current) return; | |
866 | ||
867 | Move( m_current, 25 ); | |
868 | } | |
869 | ||
870 | void wxPlotWindow::OnMoveDown( wxCommandEvent& WXUNUSED(event) ) | |
871 | { | |
872 | if (!m_current) return; | |
873 | ||
874 | Move( m_current, -25 ); | |
875 | } | |
876 | ||
877 | void wxPlotWindow::Enlarge( wxPlotCurve *curve, double factor ) | |
878 | { | |
879 | m_area->DeleteCurve( curve ); | |
880 | ||
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 | ||
886 | double range = curve->GetEndY() - curve->GetStartY(); | |
887 | offset *= range; | |
888 | ||
889 | double new_range = range / factor; | |
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 | } | |
904 | ||
905 | m_area->Refresh( FALSE ); | |
906 | RedrawYAxis(); | |
907 | } | |
908 | ||
909 | void wxPlotWindow::SetUnitsPerValue( double upv ) | |
910 | { | |
911 | m_xUnitsPerValue = upv; | |
912 | ||
913 | RedrawXAxis(); | |
914 | } | |
915 | ||
916 | void wxPlotWindow::SetZoom( double zoom ) | |
917 | { | |
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 | } | |
934 | SetScrollbars( wxPLOT_SCROLL_STEP, wxPLOT_SCROLL_STEP, | |
935 | (int)((max*m_xZoom)/wxPLOT_SCROLL_STEP)+1, 0, | |
936 | (int)(view_x*zoom/old_zoom), 0, | |
937 | TRUE ); | |
938 | ||
939 | RedrawXAxis(); | |
940 | m_area->Refresh( TRUE ); | |
941 | } | |
942 | ||
943 | void wxPlotWindow::ResetScrollbar() | |
944 | { | |
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 | ||
955 | SetScrollbars( wxPLOT_SCROLL_STEP, wxPLOT_SCROLL_STEP, | |
956 | (int)(((max*m_xZoom)/wxPLOT_SCROLL_STEP)+1), 0 ); | |
957 | } | |
958 | ||
959 | void wxPlotWindow::RedrawXAxis() | |
960 | { | |
961 | if (m_xaxis) | |
962 | m_xaxis->Refresh( FALSE ); | |
963 | } | |
964 | ||
965 | void wxPlotWindow::RedrawYAxis() | |
966 | { | |
967 | if (m_yaxis) | |
968 | m_yaxis->Refresh( TRUE ); | |
969 | } | |
970 | ||
971 | void 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 | } | |
979 | ||
980 | void wxPlotWindow::OnZoomIn( wxCommandEvent& WXUNUSED(event) ) | |
981 | { | |
982 | SetZoom( m_xZoom * 1.5 ); | |
983 | } | |
984 | ||
985 | void wxPlotWindow::OnZoomOut( wxCommandEvent& WXUNUSED(event) ) | |
986 | { | |
987 | SetZoom( m_xZoom * 0.6666 ); | |
988 | } | |
989 | ||
990 | void wxPlotWindow::OnEnlarge( wxCommandEvent& WXUNUSED(event) ) | |
991 | { | |
992 | if (!m_current) return; | |
993 | ||
994 | Enlarge( m_current, 1.5 ); | |
995 | } | |
996 | ||
997 | void wxPlotWindow::OnShrink( wxCommandEvent& WXUNUSED(event) ) | |
998 | { | |
999 | if (!m_current) return; | |
1000 | ||
1001 | Enlarge( m_current, 0.6666666 ); | |
1002 | } | |
1003 | ||
1004 | void wxPlotWindow::OnScroll2( wxScrollWinEvent& event ) | |
1005 | { | |
1006 | if ((!m_scrollOnThumbRelease) || (event.GetEventType() != wxEVT_SCROLLWIN_THUMBTRACK)) | |
1007 | { | |
1008 | wxScrolledWindow::OnScroll( event ); | |
1009 | RedrawXAxis(); | |
1010 | } | |
1011 | } | |
1012 | ||
1013 | // ---------------------------------------------------------------------------- | |
1014 | // global functions | |
1015 | // ---------------------------------------------------------------------------- | |
1016 | ||
1017 | // FIXME MT-UNSAFE | |
1018 | static wxBitmap *GetEnlargeBitmap() | |
1019 | { | |
1020 | static wxBitmap* s_bitmap = (wxBitmap *) NULL; | |
1021 | static bool s_loaded = FALSE; | |
1022 | ||
1023 | if ( !s_loaded ) | |
1024 | { | |
1025 | s_loaded = TRUE; // set it to TRUE anyhow, we won't try again | |
1026 | ||
1027 | #if defined(__WXMSW__) || defined(__WXPM__) | |
1028 | s_bitmap = new wxBitmap("plot_enl_bmp", wxBITMAP_TYPE_RESOURCE); | |
1029 | #else | |
1030 | s_bitmap = new wxBitmap( plot_enl_xpm ); | |
1031 | #endif | |
1032 | } | |
1033 | ||
1034 | return s_bitmap; | |
1035 | } | |
1036 | ||
1037 | static wxBitmap *GetShrinkBitmap() | |
1038 | { | |
1039 | static wxBitmap* s_bitmap = (wxBitmap *) NULL; | |
1040 | static bool s_loaded = FALSE; | |
1041 | ||
1042 | if ( !s_loaded ) | |
1043 | { | |
1044 | s_loaded = TRUE; // set it to TRUE anyhow, we won't try again | |
1045 | ||
1046 | #if defined(__WXMSW__) || defined(__WXPM__) | |
1047 | s_bitmap = new wxBitmap("plot_shr_bmp", wxBITMAP_TYPE_RESOURCE); | |
1048 | #else | |
1049 | s_bitmap = new wxBitmap( plot_shr_xpm ); | |
1050 | #endif | |
1051 | } | |
1052 | ||
1053 | return s_bitmap; | |
1054 | } | |
1055 | ||
1056 | static wxBitmap *GetZoomInBitmap() | |
1057 | { | |
1058 | static wxBitmap* s_bitmap = (wxBitmap *) NULL; | |
1059 | static bool s_loaded = FALSE; | |
1060 | ||
1061 | if ( !s_loaded ) | |
1062 | { | |
1063 | s_loaded = TRUE; // set it to TRUE anyhow, we won't try again | |
1064 | ||
1065 | #if defined(__WXMSW__) || defined(__WXPM__) | |
1066 | s_bitmap = new wxBitmap("plot_zin_bmp", wxBITMAP_TYPE_RESOURCE); | |
1067 | #else | |
1068 | s_bitmap = new wxBitmap( plot_zin_xpm ); | |
1069 | #endif | |
1070 | } | |
1071 | ||
1072 | return s_bitmap; | |
1073 | } | |
1074 | ||
1075 | static wxBitmap *GetZoomOutBitmap() | |
1076 | { | |
1077 | static wxBitmap* s_bitmap = (wxBitmap *) NULL; | |
1078 | static bool s_loaded = FALSE; | |
1079 | ||
1080 | if ( !s_loaded ) | |
1081 | { | |
1082 | s_loaded = TRUE; // set it to TRUE anyhow, we won't try again | |
1083 | ||
1084 | #if defined(__WXMSW__) || defined(__WXPM__) | |
1085 | s_bitmap = new wxBitmap("plot_zot_bmp", wxBITMAP_TYPE_RESOURCE); | |
1086 | #else | |
1087 | s_bitmap = new wxBitmap( plot_zot_xpm ); | |
1088 | #endif | |
1089 | } | |
1090 | ||
1091 | return s_bitmap; | |
1092 | } | |
1093 | ||
1094 | static wxBitmap *GetUpBitmap() | |
1095 | { | |
1096 | static wxBitmap* s_bitmap = (wxBitmap *) NULL; | |
1097 | static bool s_loaded = FALSE; | |
1098 | ||
1099 | if ( !s_loaded ) | |
1100 | { | |
1101 | s_loaded = TRUE; // set it to TRUE anyhow, we won't try again | |
1102 | ||
1103 | #if defined(__WXMSW__) || defined(__WXPM__) | |
1104 | s_bitmap = new wxBitmap("plot_up_bmp", wxBITMAP_TYPE_RESOURCE); | |
1105 | #else | |
1106 | s_bitmap = new wxBitmap( plot_up_xpm ); | |
1107 | #endif | |
1108 | } | |
1109 | ||
1110 | return s_bitmap; | |
1111 | } | |
1112 | ||
1113 | static 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__) | |
1123 | s_bitmap = new wxBitmap("plot_dwn_bmp", wxBITMAP_TYPE_RESOURCE); | |
1124 | #else | |
1125 | s_bitmap = new wxBitmap( plot_dwn_xpm ); | |
1126 | #endif | |
1127 | } | |
1128 | ||
1129 | return s_bitmap; | |
1130 | } | |
1131 | ||
1132 | #endif // wxUSE_PLOT |