4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "bcresources.h"
35 BC_Pot::BC_Pot(int x, int y, VFrame **data)
36 : BC_SubWindow(x, y, -1, -1, -1)
39 for(int i = 0; i < POT_STATES; i++)
47 for(int i = 0; i < POT_STATES; i++)
48 if(images[i]) delete images[i];
51 int BC_Pot::calculate_h()
53 return BC_WindowBase::get_resources()->pot_images[0]->get_h();
56 int BC_Pot::calculate_w()
58 return BC_WindowBase::get_resources()->pot_images[0]->get_w();
61 int BC_Pot::initialize()
65 data = get_resources()->pot_images;
72 BC_SubWindow::initialize();
77 int BC_Pot::reposition_window(int x, int y)
79 BC_WindowBase::reposition_window(x, y);
84 int BC_Pot::set_data(VFrame **data)
86 for(int i = 0; i < POT_STATES; i++)
87 if(images[i]) delete images[i];
89 for(int i = 0; i < POT_STATES; i++)
90 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
95 void BC_Pot::set_use_caption(int value)
101 void BC_Pot::enable()
107 void BC_Pot::disable()
114 int BC_Pot::draw(int flush)
117 draw_top_background(parent_window, 0, 0, get_w(), get_h());
118 draw_pixmap(images[status]);
119 set_color(get_resources()->pot_needle_color);
121 angle_to_coords(x1, y1, x2, y2, percentage_to_angle(get_percentage()));
122 draw_line(x1, y1, x2, y2);
128 float BC_Pot::percentage_to_angle(float percentage)
130 return percentage * (MAX_ANGLE - MIN_ANGLE) + MIN_ANGLE;
133 float BC_Pot::angle_to_percentage(float angle)
135 return (angle - MIN_ANGLE) / (MAX_ANGLE - MIN_ANGLE);
139 int BC_Pot::angle_to_coords(int &x1, int &y1, int &x2, int &y2, float angle)
141 BC_Resources *resources = get_resources();
142 x1 = resources->pot_x1;
143 y1 = resources->pot_y1;
146 x1 += xS(resources->pot_offset);
147 y1 += yS(resources->pot_offset);
150 while(angle < 0) angle += 360;
152 x2 = (int)(cos(angle / 360 * (2 * M_PI)) * resources->pot_r + x1);
153 y2 = (int)(-sin(angle / 360 * (2 * M_PI)) * resources->pot_r + y1);
157 float BC_Pot::coords_to_angle(int x2, int y2)
162 x1 = get_resources()->pot_x1;
163 y1 = get_resources()->pot_y1;
175 angle = atan((float)-y / x) / (2 * M_PI) * 360;
180 angle = 180 - atan((float)-y / -x) / (2 * M_PI) * 360;
185 angle = 180 - atan((float)-y / -x) / (2 * M_PI) * 360;
190 angle = 360 + atan((float)-y / x) / (2 * M_PI) * 360;
214 void BC_Pot::show_value_tooltip()
218 set_tooltip(get_caption());
220 keypress_tooltip_timer = 2000;
224 int BC_Pot::repeat_event(int64_t duration)
226 if(duration == top_level->get_resources()->tooltip_delay)
230 if(keypress_tooltip_timer > 0)
232 keypress_tooltip_timer -= get_resources()->tooltip_delay;
235 if(status != POT_HIGH && status != POT_DN)
241 if(status == POT_HIGH)
243 if(use_caption && tooltip_text)
245 if(!tooltip_text[0] || isdigit(tooltip_text[0]))
247 set_tooltip(get_caption());
259 int BC_Pot::keypress_event()
262 switch(get_keypress())
284 show_value_tooltip();
290 return context_help_check_and_show();
293 int BC_Pot::cursor_enter_event()
295 if(top_level->event_win == win && enabled)
297 if(!top_level->button_down && status == POT_UP)
306 int BC_Pot::cursor_leave_event()
308 if(status == POT_HIGH)
317 int BC_Pot::button_press_event()
319 if(!tooltip_on) top_level->hide_tooltip();
320 if(top_level->event_win == win && enabled)
322 if(status == POT_HIGH || status == POT_UP)
324 if(get_buttonpress() == 4)
327 show_value_tooltip();
332 if(get_buttonpress() == 5)
335 show_value_tooltip();
342 start_cursor_angle = coords_to_angle(get_cursor_x(), get_cursor_y());
343 start_needle_angle = percentage_to_angle(get_percentage());
344 angle_offset = start_cursor_angle - start_needle_angle;
345 prev_angle = start_cursor_angle;
346 angle_correction = 0;
348 top_level->deactivate();
349 top_level->active_subwindow = this;
350 show_value_tooltip();
358 int BC_Pot::button_release_event()
360 if(top_level->event_win == win)
369 top_level->hide_tooltip();
378 int BC_Pot::cursor_motion_event()
380 if(top_level->button_down &&
381 top_level->event_win == win &&
384 float angle = coords_to_angle(get_cursor_x(), get_cursor_y());
386 if(prev_angle >= 0 && prev_angle < 90 &&
387 angle >= 270 && angle < 360)
389 angle_correction -= 360;
392 if(prev_angle >= 270 && prev_angle < 360 &&
393 angle >= 0 && angle < 90)
395 angle_correction += 360;
400 if(percentage_to_value(
401 angle_to_percentage(angle + angle_correction - angle_offset)))
403 set_tooltip(get_caption());
413 BC_FPot::BC_FPot(int x, int y,
414 float value, float minvalue, float maxvalue, VFrame **data)
418 this->minvalue = minvalue;
419 this->maxvalue = maxvalue;
427 int BC_FPot::increase_value()
430 if(value > maxvalue) value = maxvalue;
434 int BC_FPot::decrease_value()
437 if(value < minvalue) value = minvalue;
441 void BC_FPot::set_precision(float value)
443 this->precision = value;
446 const char* BC_FPot::get_caption()
448 sprintf(caption, "%.2f", value);
452 float BC_FPot::get_percentage()
454 float range = maxvalue - minvalue;
455 return range > 0 ? (value - minvalue) / range : minvalue;
458 int BC_FPot::percentage_to_value(float percentage)
460 float old_value = value;
461 value = percentage * (maxvalue - minvalue) + minvalue;
462 value = Units::quantize(value, precision);
463 if(value < minvalue) value = minvalue;
464 if(value > maxvalue) value = maxvalue;
465 if(value != old_value) return 1;
469 float BC_FPot::get_value()
474 void BC_FPot::update(float value)
476 if(value < minvalue) value= minvalue;
477 if(value > maxvalue) value= maxvalue;
478 if(value != this->value)
485 void BC_FPot::update(float value, float minvalue, float maxvalue)
487 if(value != this->value ||
488 minvalue != this->minvalue ||
489 maxvalue != this->maxvalue)
492 this->minvalue = minvalue;
493 this->maxvalue = maxvalue;
499 BC_IPot::BC_IPot(int x, int y,
500 int64_t value, int64_t minvalue, int64_t maxvalue, VFrame **data)
504 this->minvalue = minvalue;
505 this->maxvalue = maxvalue;
512 int BC_IPot::increase_value()
515 if(value > maxvalue) value = maxvalue;
519 int BC_IPot::decrease_value()
522 if(value < minvalue) value = minvalue;
526 const char* BC_IPot::get_caption()
528 sprintf(caption, "%jd", value);
532 float BC_IPot::get_percentage()
534 float range = maxvalue - minvalue;
535 return range > 0 ? ((float)value - minvalue) / range : minvalue;
538 int BC_IPot::percentage_to_value(float percentage)
540 int64_t old_value = value;
541 value = (int64_t)(percentage * (maxvalue - minvalue) + minvalue);
542 if(value < minvalue) value = minvalue;
543 if(value > maxvalue) value = maxvalue;
544 if(value != old_value) return 1;
548 int64_t BC_IPot::get_value()
553 void BC_IPot::update(int64_t value)
555 if(value < minvalue) value= minvalue;
556 if(value > maxvalue) value= maxvalue;
557 if(this->value != value)
564 void BC_IPot::update(int64_t value, int64_t minvalue, int64_t maxvalue)
566 if(this->value != value ||
567 this->minvalue != minvalue ||
568 this->maxvalue != maxvalue)
571 this->minvalue = minvalue;
572 this->maxvalue = maxvalue;
578 BC_QPot::BC_QPot(int x, int y, int64_t value, VFrame **data)
581 this->value = Freq::fromfreq(value);
583 this->maxvalue = TOTALFREQS;
590 int BC_QPot::increase_value()
593 if(value > maxvalue) value = maxvalue;
597 int BC_QPot::decrease_value()
600 if(value < minvalue) value = minvalue;
604 const char* BC_QPot::get_caption()
606 sprintf(caption, "%d", Freq::tofreq(value));
610 float BC_QPot::get_percentage()
612 float range = maxvalue - minvalue;
613 return range > 0 ? ((float)value - minvalue) / range : 0;
616 int BC_QPot::percentage_to_value(float percentage)
618 int64_t old_value = value;
619 value = (int64_t)(percentage * (maxvalue - minvalue) + minvalue);
620 if(value < minvalue) value = minvalue;
621 if(value > maxvalue) value = maxvalue;
622 if(value != old_value) return 1;
626 int64_t BC_QPot::get_value()
628 return Freq::tofreq(value);
631 void BC_QPot::update(int64_t value)
633 value = Freq::fromfreq(value);
634 if(value < minvalue) value = minvalue;
635 if(value > maxvalue) value = maxvalue;
636 if(this->value != value)
644 BC_PercentagePot::BC_PercentagePot(int x, int y,
645 float value, float minvalue, float maxvalue, VFrame **data)
649 this->minvalue = minvalue;
650 this->maxvalue = maxvalue;
653 BC_PercentagePot::~BC_PercentagePot()
657 int BC_PercentagePot::increase_value()
660 if(value > maxvalue) value = maxvalue;
664 int BC_PercentagePot::decrease_value()
667 if(value < minvalue) value = minvalue;
671 const char* BC_PercentagePot::get_caption()
673 sprintf(caption, "%d%%", (int)(get_percentage() * 100 + 0.5));
677 float BC_PercentagePot::get_percentage()
679 float range = maxvalue - minvalue;
680 return range > 0 ? (value - minvalue) / range : minvalue;
683 int BC_PercentagePot::percentage_to_value(float percentage)
685 float old_value = value;
686 value = percentage * (maxvalue - minvalue) + minvalue;
687 if(value < minvalue) value = minvalue;
688 if(value > maxvalue) value = maxvalue;
689 if(value != old_value) return 1;
693 float BC_PercentagePot::get_value()
698 void BC_PercentagePot::update(float value)
700 if(value < minvalue) value= minvalue;
701 if(value > maxvalue) value= maxvalue;
702 if(this->value != value)