spek-ruler.cc (2566B)
1 #include <cmath> 2 3 #include "spek-ruler.h" 4 5 SpekRuler::SpekRuler( 6 int x, int y, Position pos, wxString sample_label, 7 int *factors, int min_units, int max_units, double spacing, 8 double scale, double offset, formatter_cb formatter) 9 : 10 x(x), y(y), pos(pos), sample_label(sample_label), 11 factors(factors), min_units(min_units), max_units(max_units), spacing(spacing), 12 scale(scale), offset(offset), formatter(formatter) 13 { 14 } 15 16 void SpekRuler::draw(wxDC& dc) 17 { 18 // Mesure the sample label. 19 wxSize size = dc.GetTextExtent(sample_label); 20 int len = this->pos == TOP || this->pos == BOTTOM ? size.GetWidth() : size.GetHeight(); 21 22 // Select the factor to use, we want some space between the labels. 23 int factor = 0; 24 for (int i = 0; factors[i]; ++i) { 25 if (fabs(this->scale * factors[i]) >= this->spacing * len) { 26 factor = factors[i]; 27 break; 28 } 29 } 30 31 // Draw the ticks. 32 this->draw_tick(dc, min_units); 33 this->draw_tick(dc, max_units); 34 35 if (factor > 0) { 36 for (int tick = min_units + factor; tick < max_units; tick += factor) { 37 if (fabs(this->scale * (max_units - tick)) < len * 1.2) { 38 break; 39 } 40 this->draw_tick(dc, tick); 41 } 42 } 43 } 44 45 void SpekRuler::draw_tick(wxDC& dc, int tick) 46 { 47 double GAP = 10; 48 double TICK_LEN = 4; 49 50 wxString label = this->formatter(tick); 51 int value = this->pos == TOP || this->pos == BOTTOM ? 52 tick : this->max_units + this->min_units - tick; 53 double p = this->offset + this->scale * (value - min_units); 54 wxSize size = dc.GetTextExtent(label); 55 int w = size.GetWidth(); 56 int h = size.GetHeight(); 57 58 if (this->pos == TOP) { 59 dc.DrawText(label, this->x + p - w / 2, this->y - GAP - h); 60 } else if (this->pos == RIGHT){ 61 dc.DrawText(label, this->x + GAP, this->y + p - h / 2); 62 } else if (this->pos == BOTTOM) { 63 dc.DrawText(label, this->x + p - w / 2, this->y + GAP); 64 } else if (this->pos == LEFT){ 65 dc.DrawText(label, this->x - w - GAP, this->y + p - h / 2); 66 } 67 68 if (this->pos == TOP) { 69 dc.DrawLine(this->x + p, this->y, this->x + p, this->y - TICK_LEN); 70 } else if (this->pos == RIGHT) { 71 dc.DrawLine(this->x, this->y + p, this->x + TICK_LEN, this->y + p); 72 } else if (this->pos == BOTTOM) { 73 dc.DrawLine(this->x + p, this->y, this->x + p, this->y + TICK_LEN); 74 } else if (this->pos == LEFT) { 75 dc.DrawLine(this->x, this->y + p, this->x - TICK_LEN, this->y + p); 76 } 77 }