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++)
49 int BC_Pot::calculate_h()
51 return BC_WindowBase::get_resources()->pot_images[0]->get_h();
54 int BC_Pot::calculate_w()
56 return BC_WindowBase::get_resources()->pot_images[0]->get_w();
59 int BC_Pot::initialize()
63 data = get_resources()->pot_images;
70 BC_SubWindow::initialize();
75 int BC_Pot::reposition_window(int x, int y)
77 BC_WindowBase::reposition_window(x, y);
82 int BC_Pot::set_data(VFrame **data)
84 for(int i = 0; i < POT_STATES; i++)
85 if(images[i]) delete images[i];
87 for(int i = 0; i < POT_STATES; i++)
88 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
93 void BC_Pot::set_use_caption(int value)
105 void BC_Pot::disable()
112 int BC_Pot::draw(int flush)
115 draw_top_background(parent_window, 0, 0, get_w(), get_h());
116 draw_pixmap(images[status]);
117 set_color(get_resources()->pot_needle_color);
119 angle_to_coords(x1, y1, x2, y2, percentage_to_angle(get_percentage()));
120 draw_line(x1, y1, x2, y2);
126 float BC_Pot::percentage_to_angle(float percentage)
128 return percentage * (MAX_ANGLE - MIN_ANGLE) + MIN_ANGLE;
131 float BC_Pot::angle_to_percentage(float angle)
133 return (angle - MIN_ANGLE) / (MAX_ANGLE - MIN_ANGLE);
137 int BC_Pot::angle_to_coords(int &x1, int &y1, int &x2, int &y2, float angle)
139 BC_Resources *resources = get_resources();
140 x1 = resources->pot_x1;
141 y1 = resources->pot_y1;
144 x1 += resources->pot_offset;
145 y1 += resources->pot_offset;
148 while(angle < 0) angle += 360;
150 x2 = (int)(cos(angle / 360 * (2 * M_PI)) * resources->pot_r + x1);
151 y2 = (int)(-sin(angle / 360 * (2 * M_PI)) * resources->pot_r + y1);
155 float BC_Pot::coords_to_angle(int x2, int y2)
160 x1 = get_resources()->pot_x1;
161 y1 = get_resources()->pot_y1;
173 angle = atan((float)-y / x) / (2 * M_PI) * 360;
178 angle = 180 - atan((float)-y / -x) / (2 * M_PI) * 360;
183 angle = 180 - atan((float)-y / -x) / (2 * M_PI) * 360;
188 angle = 360 + atan((float)-y / x) / (2 * M_PI) * 360;
212 void BC_Pot::show_value_tooltip()
216 set_tooltip(get_caption());
218 keypress_tooltip_timer = 2000;
222 int BC_Pot::repeat_event(int64_t duration)
224 if(duration == top_level->get_resources()->tooltip_delay)
228 if(keypress_tooltip_timer > 0)
230 keypress_tooltip_timer -= get_resources()->tooltip_delay;
233 if(status != POT_HIGH && status != POT_DN)
239 if(status == POT_HIGH)
241 if(use_caption && tooltip_text)
243 if(!tooltip_text[0] || isdigit(tooltip_text[0]))
245 set_tooltip(get_caption());
257 int BC_Pot::keypress_event()
260 switch(get_keypress())
282 show_value_tooltip();
289 int BC_Pot::cursor_enter_event()
291 if(top_level->event_win == win && enabled)
293 if(!top_level->button_down && status == POT_UP)
302 int BC_Pot::cursor_leave_event()
304 if(status == POT_HIGH)
313 int BC_Pot::button_press_event()
315 if(!tooltip_on) top_level->hide_tooltip();
316 if(top_level->event_win == win && enabled)
318 if(status == POT_HIGH || status == POT_UP)
320 if(get_buttonpress() == 4)
323 show_value_tooltip();
328 if(get_buttonpress() == 5)
331 show_value_tooltip();
338 start_cursor_angle = coords_to_angle(get_cursor_x(), get_cursor_y());
339 start_needle_angle = percentage_to_angle(get_percentage());
340 angle_offset = start_cursor_angle - start_needle_angle;
341 prev_angle = start_cursor_angle;
342 angle_correction = 0;
344 top_level->deactivate();
345 top_level->active_subwindow = this;
346 show_value_tooltip();
354 int BC_Pot::button_release_event()
356 if(top_level->event_win == win)
365 top_level->hide_tooltip();
374 int BC_Pot::cursor_motion_event()
376 if(top_level->button_down &&
377 top_level->event_win == win &&
380 float angle = coords_to_angle(get_cursor_x(), get_cursor_y());
382 if(prev_angle >= 0 && prev_angle < 90 &&
383 angle >= 270 && angle < 360)
385 angle_correction -= 360;
388 if(prev_angle >= 270 && prev_angle < 360 &&
389 angle >= 0 && angle < 90)
391 angle_correction += 360;
396 if(percentage_to_value(
397 angle_to_percentage(angle + angle_correction - angle_offset)))
399 set_tooltip(get_caption());
418 BC_FPot::BC_FPot(int x,
427 this->minvalue = minvalue;
428 this->maxvalue = maxvalue;
436 int BC_FPot::increase_value()
439 if(value > maxvalue) value = maxvalue;
443 int BC_FPot::decrease_value()
446 if(value < minvalue) value = minvalue;
450 void BC_FPot::set_precision(float value)
452 this->precision = value;
455 const char* BC_FPot::get_caption()
457 sprintf(caption, "%.2f", value);
461 float BC_FPot::get_percentage()
463 float range = maxvalue - minvalue;
464 return range > 0 ? (value - minvalue) / range : minvalue;
467 int BC_FPot::percentage_to_value(float percentage)
469 float old_value = value;
470 value = percentage * (maxvalue - minvalue) + minvalue;
471 value = Units::quantize(value, precision);
472 if(value < minvalue) value = minvalue;
473 if(value > maxvalue) value = maxvalue;
474 if(value != old_value) return 1;
478 float BC_FPot::get_value()
483 void BC_FPot::update(float value)
485 if(value < minvalue) value= minvalue;
486 if(value > maxvalue) value= maxvalue;
487 if(value != this->value)
494 void BC_FPot::update(float value, float minvalue, float maxvalue)
496 if(value != this->value ||
497 minvalue != this->minvalue ||
498 maxvalue != this->maxvalue)
501 this->minvalue = minvalue;
502 this->maxvalue = maxvalue;
514 BC_IPot::BC_IPot(int x,
523 this->minvalue = minvalue;
524 this->maxvalue = maxvalue;
531 int BC_IPot::increase_value()
534 if(value > maxvalue) value = maxvalue;
538 int BC_IPot::decrease_value()
541 if(value < minvalue) value = minvalue;
545 const char* BC_IPot::get_caption()
547 sprintf(caption, "%jd", value);
551 float BC_IPot::get_percentage()
553 float range = maxvalue - minvalue;
554 return range > 0 ? ((float)value - minvalue) / range : minvalue;
557 int BC_IPot::percentage_to_value(float percentage)
559 int64_t old_value = value;
560 value = (int64_t)(percentage * (maxvalue - minvalue) + minvalue);
561 if(value < minvalue) value = minvalue;
562 if(value > maxvalue) value = maxvalue;
563 if(value != old_value) return 1;
567 int64_t BC_IPot::get_value()
572 void BC_IPot::update(int64_t value)
574 if(value < minvalue) value= minvalue;
575 if(value > maxvalue) value= maxvalue;
576 if(this->value != value)
583 void BC_IPot::update(int64_t value, int64_t minvalue, int64_t maxvalue)
585 if(this->value != value ||
586 this->minvalue != minvalue ||
587 this->maxvalue != maxvalue)
590 this->minvalue = minvalue;
591 this->maxvalue = maxvalue;
601 BC_QPot::BC_QPot(int x,
607 this->value = Freq::fromfreq(value);
609 this->maxvalue = TOTALFREQS;
616 int BC_QPot::increase_value()
619 if(value > maxvalue) value = maxvalue;
623 int BC_QPot::decrease_value()
626 if(value < minvalue) value = minvalue;
630 const char* BC_QPot::get_caption()
632 sprintf(caption, "%d", Freq::tofreq(value));
636 float BC_QPot::get_percentage()
638 float range = maxvalue - minvalue;
639 return range > 0 ? ((float)value - minvalue) / range : 0;
642 int BC_QPot::percentage_to_value(float percentage)
644 int64_t old_value = value;
645 value = (int64_t)(percentage * (maxvalue - minvalue) + minvalue);
646 if(value < minvalue) value = minvalue;
647 if(value > maxvalue) value = maxvalue;
648 if(value != old_value) return 1;
652 int64_t BC_QPot::get_value()
654 return Freq::tofreq(value);
657 void BC_QPot::update(int64_t value)
659 value = Freq::fromfreq(value);
660 if(value < minvalue) value = minvalue;
661 if(value > maxvalue) value = maxvalue;
662 if(this->value != value)
676 BC_PercentagePot::BC_PercentagePot(int x,
685 this->minvalue = minvalue;
686 this->maxvalue = maxvalue;
689 BC_PercentagePot::~BC_PercentagePot()
693 int BC_PercentagePot::increase_value()
696 if(value > maxvalue) value = maxvalue;
700 int BC_PercentagePot::decrease_value()
703 if(value < minvalue) value = minvalue;
707 const char* BC_PercentagePot::get_caption()
709 sprintf(caption, "%d%%", (int)(get_percentage() * 100 + 0.5));
713 float BC_PercentagePot::get_percentage()
715 float range = maxvalue - minvalue;
716 return range > 0 ? (value - minvalue) / range : minvalue;
719 int BC_PercentagePot::percentage_to_value(float percentage)
721 float old_value = value;
722 value = percentage * (maxvalue - minvalue) + minvalue;
723 if(value < minvalue) value = minvalue;
724 if(value > maxvalue) value = maxvalue;
725 if(value != old_value) return 1;
729 float BC_PercentagePot::get_value()
734 void BC_PercentagePot::update(float value)
736 if(value < minvalue) value= minvalue;
737 if(value > maxvalue) value= maxvalue;
738 if(this->value != value)