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());
258 int BC_Pot::keypress_event()
261 switch(get_keypress())
283 show_value_tooltip();
290 int BC_Pot::cursor_enter_event()
292 if(top_level->event_win == win && enabled)
295 if(!top_level->button_down && status == POT_UP)
304 int BC_Pot::cursor_leave_event()
306 if(status == POT_HIGH)
315 int BC_Pot::button_press_event()
317 if(!tooltip_on) top_level->hide_tooltip();
318 if(top_level->event_win == win && enabled)
320 if(status == POT_HIGH || status == POT_UP)
322 if(get_buttonpress() == 4)
325 show_value_tooltip();
330 if(get_buttonpress() == 5)
333 show_value_tooltip();
340 start_cursor_angle = coords_to_angle(get_cursor_x(), get_cursor_y());
341 start_needle_angle = percentage_to_angle(get_percentage());
342 angle_offset = start_cursor_angle - start_needle_angle;
343 prev_angle = start_cursor_angle;
344 angle_correction = 0;
346 top_level->deactivate();
347 top_level->active_subwindow = this;
348 show_value_tooltip();
356 int BC_Pot::button_release_event()
358 if(top_level->event_win == win)
367 top_level->hide_tooltip();
376 int BC_Pot::cursor_motion_event()
378 if(top_level->button_down &&
379 top_level->event_win == win &&
382 float angle = coords_to_angle(get_cursor_x(), get_cursor_y());
384 if(prev_angle >= 0 && prev_angle < 90 &&
385 angle >= 270 && angle < 360)
387 angle_correction -= 360;
390 if(prev_angle >= 270 && prev_angle < 360 &&
391 angle >= 0 && angle < 90)
393 angle_correction += 360;
398 if(percentage_to_value(
399 angle_to_percentage(angle + angle_correction - angle_offset)))
401 set_tooltip(get_caption());
420 BC_FPot::BC_FPot(int x,
429 this->minvalue = minvalue;
430 this->maxvalue = maxvalue;
438 int BC_FPot::increase_value()
441 if(value > maxvalue) value = maxvalue;
445 int BC_FPot::decrease_value()
448 if(value < minvalue) value = minvalue;
452 void BC_FPot::set_precision(float value)
454 this->precision = value;
457 const char* BC_FPot::get_caption()
459 sprintf(caption, "%.2f", value);
463 float BC_FPot::get_percentage()
465 return (value - minvalue) / (maxvalue - minvalue);
468 int BC_FPot::percentage_to_value(float percentage)
470 float old_value = value;
471 value = percentage * (maxvalue - minvalue) + minvalue;
472 value = Units::quantize(value, precision);
473 if(value < minvalue) value = minvalue;
474 if(value > maxvalue) value = maxvalue;
475 if(value != old_value) return 1;
479 float BC_FPot::get_value()
484 void BC_FPot::update(float value)
486 if(value < minvalue) value= minvalue;
487 if(value > maxvalue) value= maxvalue;
488 if(value != this->value)
495 void BC_FPot::update(float value, float minvalue, float maxvalue)
497 if(value != this->value ||
498 minvalue != this->minvalue ||
499 maxvalue != this->maxvalue)
502 this->minvalue = minvalue;
503 this->maxvalue = maxvalue;
515 BC_IPot::BC_IPot(int x,
524 this->minvalue = minvalue;
525 this->maxvalue = maxvalue;
532 int BC_IPot::increase_value()
535 if(value > maxvalue) value = maxvalue;
539 int BC_IPot::decrease_value()
542 if(value < minvalue) value = minvalue;
546 const char* BC_IPot::get_caption()
548 sprintf(caption, "%jd", value);
552 float BC_IPot::get_percentage()
554 return ((float)value - minvalue) / (maxvalue - 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 return ((float)value - minvalue) / (maxvalue - minvalue);
641 int BC_QPot::percentage_to_value(float percentage)
643 int64_t old_value = value;
644 value = (int64_t)(percentage * (maxvalue - minvalue) + minvalue);
645 if(value < minvalue) value = minvalue;
646 if(value > maxvalue) value = maxvalue;
647 if(value != old_value) return 1;
651 int64_t BC_QPot::get_value()
653 return Freq::tofreq(value);
656 void BC_QPot::update(int64_t value)
658 value = Freq::fromfreq(value);
659 if(value < minvalue) value = minvalue;
660 if(value > maxvalue) value = maxvalue;
661 if(this->value != value)
675 BC_PercentagePot::BC_PercentagePot(int x,
684 this->minvalue = minvalue;
685 this->maxvalue = maxvalue;
688 BC_PercentagePot::~BC_PercentagePot()
692 int BC_PercentagePot::increase_value()
695 if(value > maxvalue) value = maxvalue;
699 int BC_PercentagePot::decrease_value()
702 if(value < minvalue) value = minvalue;
706 const char* BC_PercentagePot::get_caption()
708 sprintf(caption, "%d%%", (int)(get_percentage() * 100 + 0.5));
712 float BC_PercentagePot::get_percentage()
714 return (value - minvalue) / (maxvalue - minvalue);
717 int BC_PercentagePot::percentage_to_value(float percentage)
719 float old_value = value;
720 value = percentage * (maxvalue - minvalue) + minvalue;
721 if(value < minvalue) value = minvalue;
722 if(value > maxvalue) value = maxvalue;
723 if(value != old_value) return 1;
727 float BC_PercentagePot::get_value()
732 void BC_PercentagePot::update(float value)
734 if(value < minvalue) value= minvalue;
735 if(value > maxvalue) value= maxvalue;
736 if(this->value != value)