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"
36 BC_Pot::BC_Pot(int x, int y, VFrame **data)
37 : BC_SubWindow(x, y, -1, -1, -1)
40 for(int i = 0; i < POT_STATES; i++)
50 int BC_Pot::calculate_h()
52 return BC_WindowBase::get_resources()->pot_images[0]->get_h();
55 int BC_Pot::calculate_w()
57 return BC_WindowBase::get_resources()->pot_images[0]->get_w();
60 int BC_Pot::initialize()
64 data = get_resources()->pot_images;
71 BC_SubWindow::initialize();
76 int BC_Pot::reposition_window(int x, int y)
78 BC_WindowBase::reposition_window(x, y);
83 int BC_Pot::set_data(VFrame **data)
85 for(int i = 0; i < POT_STATES; i++)
86 if(images[i]) delete images[i];
88 for(int i = 0; i < POT_STATES; i++)
89 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
94 void BC_Pot::set_use_caption(int value)
100 void BC_Pot::enable()
106 void BC_Pot::disable()
113 int BC_Pot::draw(int flush)
116 draw_top_background(parent_window, 0, 0, get_w(), get_h());
117 draw_pixmap(images[status]);
118 set_color(get_resources()->pot_needle_color);
120 angle_to_coords(x1, y1, x2, y2, percentage_to_angle(get_percentage()));
121 draw_line(x1, y1, x2, y2);
127 float BC_Pot::percentage_to_angle(float percentage)
129 return percentage * (MAX_ANGLE - MIN_ANGLE) + MIN_ANGLE;
132 float BC_Pot::angle_to_percentage(float angle)
134 return (angle - MIN_ANGLE) / (MAX_ANGLE - MIN_ANGLE);
138 int BC_Pot::angle_to_coords(int &x1, int &y1, int &x2, int &y2, float angle)
140 BC_Resources *resources = get_resources();
141 x1 = resources->pot_x1;
142 y1 = resources->pot_y1;
145 x1 += resources->pot_offset;
146 y1 += resources->pot_offset;
149 while(angle < 0) angle += 360;
151 x2 = (int)(cos(angle / 360 * (2 * M_PI)) * resources->pot_r + x1);
152 y2 = (int)(-sin(angle / 360 * (2 * M_PI)) * resources->pot_r + y1);
156 float BC_Pot::coords_to_angle(int x2, int y2)
161 x1 = get_resources()->pot_x1;
162 y1 = get_resources()->pot_y1;
174 angle = atan((float)-y / x) / (2 * M_PI) * 360;
179 angle = 180 - atan((float)-y / -x) / (2 * M_PI) * 360;
184 angle = 180 - atan((float)-y / -x) / (2 * M_PI) * 360;
189 angle = 360 + atan((float)-y / x) / (2 * M_PI) * 360;
213 void BC_Pot::show_value_tooltip()
217 set_tooltip(get_caption());
219 keypress_tooltip_timer = 2000;
223 int BC_Pot::repeat_event(int64_t duration)
225 if(duration == top_level->get_resources()->tooltip_delay)
229 if(keypress_tooltip_timer > 0)
231 keypress_tooltip_timer -= get_resources()->tooltip_delay;
234 if(status != POT_HIGH && status != POT_DN)
240 if(status == POT_HIGH)
242 if(use_caption && tooltip_text)
244 if(!tooltip_text[0] || isdigit(tooltip_text[0]))
246 set_tooltip(get_caption());
259 int BC_Pot::keypress_event()
262 switch(get_keypress())
284 show_value_tooltip();
291 int BC_Pot::cursor_enter_event()
293 if(top_level->event_win == win && enabled)
296 if(!top_level->button_down && status == POT_UP)
305 int BC_Pot::cursor_leave_event()
307 if(status == POT_HIGH)
316 int BC_Pot::button_press_event()
318 if(!tooltip_on) top_level->hide_tooltip();
319 if(top_level->event_win == win && enabled)
321 if(status == POT_HIGH || status == POT_UP)
323 if(get_buttonpress() == 4)
326 show_value_tooltip();
331 if(get_buttonpress() == 5)
334 show_value_tooltip();
341 start_cursor_angle = coords_to_angle(get_cursor_x(), get_cursor_y());
342 start_needle_angle = percentage_to_angle(get_percentage());
343 angle_offset = start_cursor_angle - start_needle_angle;
344 prev_angle = start_cursor_angle;
345 angle_correction = 0;
347 top_level->deactivate();
348 top_level->active_subwindow = this;
349 show_value_tooltip();
357 int BC_Pot::button_release_event()
359 if(top_level->event_win == win)
368 top_level->hide_tooltip();
377 int BC_Pot::cursor_motion_event()
379 if(top_level->button_down &&
380 top_level->event_win == win &&
383 float angle = coords_to_angle(get_cursor_x(), get_cursor_y());
385 if(prev_angle >= 0 && prev_angle < 90 &&
386 angle >= 270 && angle < 360)
388 angle_correction -= 360;
391 if(prev_angle >= 270 && prev_angle < 360 &&
392 angle >= 0 && angle < 90)
394 angle_correction += 360;
399 if(percentage_to_value(
400 angle_to_percentage(angle + angle_correction - angle_offset)))
402 set_tooltip(get_caption());
421 BC_FPot::BC_FPot(int x,
430 this->minvalue = minvalue;
431 this->maxvalue = maxvalue;
439 int BC_FPot::increase_value()
442 if(value > maxvalue) value = maxvalue;
446 int BC_FPot::decrease_value()
449 if(value < minvalue) value = minvalue;
453 void BC_FPot::set_precision(float value)
455 this->precision = value;
458 const char* BC_FPot::get_caption()
460 sprintf(caption, "%.2f", value);
464 float BC_FPot::get_percentage()
466 return (value - minvalue) / (maxvalue - minvalue);
469 int BC_FPot::percentage_to_value(float percentage)
471 float old_value = value;
472 value = percentage * (maxvalue - minvalue) + minvalue;
473 value = Units::quantize(value, precision);
474 if(value < minvalue) value = minvalue;
475 if(value > maxvalue) value = maxvalue;
476 if(value != old_value) return 1;
480 float BC_FPot::get_value()
485 void BC_FPot::update(float value)
487 if(value < minvalue) value= minvalue;
488 if(value > maxvalue) value= maxvalue;
489 if(value != this->value)
496 void BC_FPot::update(float value, float minvalue, float maxvalue)
498 if(value != this->value ||
499 minvalue != this->minvalue ||
500 maxvalue != this->maxvalue)
503 this->minvalue = minvalue;
504 this->maxvalue = maxvalue;
516 BC_IPot::BC_IPot(int x,
525 this->minvalue = minvalue;
526 this->maxvalue = maxvalue;
533 int BC_IPot::increase_value()
536 if(value > maxvalue) value = maxvalue;
540 int BC_IPot::decrease_value()
543 if(value < minvalue) value = minvalue;
547 const char* BC_IPot::get_caption()
549 sprintf(caption, "%jd", value);
553 float BC_IPot::get_percentage()
555 return ((float)value - minvalue) / (maxvalue - minvalue);
558 int BC_IPot::percentage_to_value(float percentage)
560 int64_t old_value = value;
561 value = (int64_t)(percentage * (maxvalue - minvalue) + minvalue);
562 if(value < minvalue) value = minvalue;
563 if(value > maxvalue) value = maxvalue;
564 if(value != old_value) return 1;
568 int64_t BC_IPot::get_value()
573 void BC_IPot::update(int64_t value)
575 if(value < minvalue) value= minvalue;
576 if(value > maxvalue) value= maxvalue;
577 if(this->value != value)
584 void BC_IPot::update(int64_t value, int64_t minvalue, int64_t maxvalue)
586 if(this->value != value ||
587 this->minvalue != minvalue ||
588 this->maxvalue != maxvalue)
591 this->minvalue = minvalue;
592 this->maxvalue = maxvalue;
602 BC_QPot::BC_QPot(int x,
608 this->value = Freq::fromfreq(value);
610 this->maxvalue = TOTALFREQS;
617 int BC_QPot::increase_value()
620 if(value > maxvalue) value = maxvalue;
624 int BC_QPot::decrease_value()
627 if(value < minvalue) value = minvalue;
631 const char* BC_QPot::get_caption()
633 sprintf(caption, "%d", Freq::tofreq(value));
637 float BC_QPot::get_percentage()
639 return ((float)value - minvalue) / (maxvalue - minvalue);
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 return (value - minvalue) / (maxvalue - minvalue);
718 int BC_PercentagePot::percentage_to_value(float percentage)
720 float old_value = value;
721 value = percentage * (maxvalue - minvalue) + minvalue;
722 if(value < minvalue) value = minvalue;
723 if(value > maxvalue) value = maxvalue;
724 if(value != old_value) return 1;
728 float BC_PercentagePot::get_value()
733 void BC_PercentagePot::update(float value)
735 if(value < minvalue) value= minvalue;
736 if(value > maxvalue) value= maxvalue;
737 if(this->value != value)