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