/** * Copyright Mikael Högdahl - triyana@users.sourceforge.com * * This source is distributed under the terms of the Q Public License version 1.0, * created by Trolltech (www.trolltech.com). */ #include "Fl_DC.h" #include /** * Create a chart canvas. * @param int - x pos * @param int - y pos * @param int - width * @param int - height * @param char* - label */ Fl_DC::Fl_DC (int x, int y, int w, int h, char *label, Fl_Slider* slider) : Fl_Box (x, y, w, h, label) { aSlider = slider; aChart = 0; aLeft.aVol = -1; aRight.aVol = -1; box (FL_DOWN_FRAME); if (aSlider) aSlider->callback ((Fl_Callback*) Fl_DC::slidercb, this); } /** * Destroy widget and also clear all data */ Fl_DC::~Fl_DC () { delete aChart; } /** * Add new chart object * @param MHDayChart* - Chart object */ void Fl_DC::AddChart (MHDayChart* chart) { delete aChart; if (chart) { aChart = chart; chart->Init (this); if (aSlider) { if (aChart->aDates.Size() > aChart->aTicks) { int size = aChart->aDates.Size() - aChart->aTicks; aSlider->bounds (0, int(size + 1)); aSlider->step (1.0); ((Fl_Valuator*)aSlider)->value (aChart->aDateStart); aSlider->activate(); } else { aSlider->deactivate(); } } } else aChart = 0; redraw (); } /** * */ void Fl_DC::slidercb_ () { if (aChart && aSlider) { aChart->aDateStart = int(aSlider->value()); aChart->Init (this); } redraw (); } /** * Call MHDayChart::Paint method to paint all charts */ void Fl_DC::draw () { if (aChart) { BeginClip (); aChart->Paint (this); EndClip (); } } /** * Draw a line between two points * @param int - Start x pos * @param int - Start y pos * @param int - End x pos * @param int - End y pos */ void Fl_DC::DrawLine (int x1, int y1, int x2, int y2) { fl_line (x1 + x(), y1 + y(), x2 + x(), y2 + y()); } /** * Draw a filled rectangle between end points. * @param int - Start x pos * @param int - Start y pos * @param int - End x pos * @param int - End y pos */ void Fl_DC::DrawRect (int x1, int y1, int x2, int y2) { fl_rectf (this->x() + x1, this->y() + y1, x2 - x1 + 1, y2 - y1); } /** * Draw text at specific point * @param const char* - String to draw * @param int - Start x pos * @param int - Start y pos */ void Fl_DC::DrawText (const char* p, int x, int y, bool center) { int adjust = center ? ((GetCharHeight() / 2) + (fl_descent() / 2)) : 0; fl_font (FL_COURIER, GetFontSize()); fl_draw (p, this->x() + x, this->y() + y - adjust + GetCharHeight()); } /** * All events goes here * @param int - Event * @return int - 1 for handled event 0 for not */ int Fl_DC::handle (int event) { if (event == FL_PUSH) Fl::focus(this); else if (event == FL_MOVE && aChart) aChart->Query (Fl::event_x() - this->x(), Fl::event_y() - this->y(), &aLeft,&aRight); else if (event == FL_MOUSEWHEEL && aChart) { double size = aChart->aDates.Size() - aChart->aTicks; double pos = aSlider->value(); double dy = -(Fl::event_dy() * 5); double adj = aChart->aTicks / dy; if (Fl::event_ctrl() > 0) { static bool foo = false; foo = !foo; if (adj > 0 && foo) aChart->aTickWidth += 1; else if (foo) aChart->aTickWidth -= 1; if (aChart->aTickWidth < 2) aChart->aTickWidth = 2; else if (aChart->aTickWidth > 20) aChart->aTickWidth = 20; resize (x(), y(), w(), h()); redraw (); } else { if (adj > 0) { if ((pos + adj) > size) aSlider->value (size); else aSlider->value (pos + adj); } else { if ((pos + adj) < 0.0) aSlider->value (0.0); else aSlider->value (pos + adj); } slidercb_(); Fl::event_clicks(0); return 1; } } return Fl_Box::handle (event); } /** * Resize only canvas but not the charts * @param int - x pos * @param int - y pos * @param int - width * @param int - height */ void Fl_DC::resize (int x, int y, int w, int h) { Fl_Box::resize(x, y, w, h); if (aChart) { aChart->Init (this); if (aSlider) { double size = 0; if (aChart->aDates.Size() > aChart->aTicks) { size = aChart->aDates.Size() - aChart->aTicks; if (aChart->aDateStart < 0) aChart->aDateStart = 0; //printf (" TICKS=%d DATESTART=%d DATES=%d SLIDER=%d\n", aChart->aTicks, aChart->aDateStart, aChart->aDates.Size(), int(aSlider->maximum())); } else if (aChart->aDates.Size() == aChart->aTicks) { aChart->aDateStart = 0; //printf ("_ TICKS=%d DATESTART=%d DATES=%d SLIDER=%d\n", aChart->aTicks, aChart->aDateStart, aChart->aDates.Size(), int(aSlider->maximum())); } ((Fl_Valuator*)aSlider)->value (aChart->aDateStart); aSlider->bounds (0.0, size); aSlider->step (1.0); if (aChart->aDates.Size() > aChart->aTicks) aSlider->activate(); else aSlider->deactivate(); } } } /** * Set color of background canvas * @param int - Color constant */ void Fl_DC::SetBrushColor (int color) { SetPenColor (color); } /** * Set pencil color to use for all drawings, both lines and text * @param int - Color constant */ void Fl_DC::SetPenColor (int Color) { switch (Color) { case MHDC::BLACK: fl_color (FL_BLACK); break; case MHDC::RED: fl_color (FL_RED); break; case MHDC::GREEN: fl_color (fl_darker(FL_GREEN)); break; case MHDC::BLUE: fl_color (FL_BLUE); break; case MHDC::MAGENTA: fl_color (FL_MAGENTA); break; case MHDC::CYAN: fl_color (FL_CYAN); break; case MHDC::GRAY: fl_color (FL_GRAY); break; case MHDC::WHITE: fl_color (FL_WHITE); break; case MHDC::YELLOW: fl_color (fl_darker(FL_YELLOW)); break; default: fl_color (Color); } }