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();
291 int BC_Pot::cursor_enter_event()
293 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());
411 BC_FPot::BC_FPot(int x, int y,
412 float value, float minvalue, float maxvalue, VFrame **data)
416 this->minvalue = minvalue;
417 this->maxvalue = maxvalue;
425 int BC_FPot::increase_value()
428 if(value > maxvalue) value = maxvalue;
432 int BC_FPot::decrease_value()
435 if(value < minvalue) value = minvalue;
439 void BC_FPot::set_precision(float value)
441 this->precision = value;
444 const char* BC_FPot::get_caption()
446 sprintf(caption, "%.2f", value);
450 float BC_FPot::get_percentage()
452 float range = maxvalue - minvalue;
453 return range > 0 ? (value - minvalue) / range : minvalue;
456 int BC_FPot::percentage_to_value(float percentage)
458 float old_value = value;
459 value = percentage * (maxvalue - minvalue) + minvalue;
460 value = Units::quantize(value, precision);
461 if(value < minvalue) value = minvalue;
462 if(value > maxvalue) value = maxvalue;
463 if(value != old_value) return 1;
467 float BC_FPot::get_value()
472 void BC_FPot::update(float value)
474 if(value < minvalue) value= minvalue;
475 if(value > maxvalue) value= maxvalue;
476 if(value != this->value)
483 void BC_FPot::update(float value, float minvalue, float maxvalue)
485 if(value != this->value ||
486 minvalue != this->minvalue ||
487 maxvalue != this->maxvalue)
490 this->minvalue = minvalue;
491 this->maxvalue = maxvalue;
497 BC_IPot::BC_IPot(int x, int y,
498 int64_t value, int64_t minvalue, int64_t maxvalue, VFrame **data)
502 this->minvalue = minvalue;
503 this->maxvalue = maxvalue;
510 int BC_IPot::increase_value()
513 if(value > maxvalue) value = maxvalue;
517 int BC_IPot::decrease_value()
520 if(value < minvalue) value = minvalue;
524 const char* BC_IPot::get_caption()
526 sprintf(caption, "%jd", value);
530 float BC_IPot::get_percentage()
532 float range = maxvalue - minvalue;
533 return range > 0 ? ((float)value - minvalue) / range : minvalue;
536 int BC_IPot::percentage_to_value(float percentage)
538 int64_t old_value = value;
539 value = (int64_t)(percentage * (maxvalue - minvalue) + minvalue);
540 if(value < minvalue) value = minvalue;
541 if(value > maxvalue) value = maxvalue;
542 if(value != old_value) return 1;
546 int64_t BC_IPot::get_value()
551 void BC_IPot::update(int64_t value)
553 if(value < minvalue) value= minvalue;
554 if(value > maxvalue) value= maxvalue;
555 if(this->value != value)
562 void BC_IPot::update(int64_t value, int64_t minvalue, int64_t maxvalue)
564 if(this->value != value ||
565 this->minvalue != minvalue ||
566 this->maxvalue != maxvalue)
569 this->minvalue = minvalue;
570 this->maxvalue = maxvalue;
576 BC_QPot::BC_QPot(int x, int y, int64_t value, VFrame **data)
579 this->value = Freq::fromfreq(value);
581 this->maxvalue = TOTALFREQS;
588 int BC_QPot::increase_value()
591 if(value > maxvalue) value = maxvalue;
595 int BC_QPot::decrease_value()
598 if(value < minvalue) value = minvalue;
602 const char* BC_QPot::get_caption()
604 sprintf(caption, "%d", Freq::tofreq(value));
608 float BC_QPot::get_percentage()
610 float range = maxvalue - minvalue;
611 return range > 0 ? ((float)value - minvalue) / range : 0;
614 int BC_QPot::percentage_to_value(float percentage)
616 int64_t old_value = value;
617 value = (int64_t)(percentage * (maxvalue - minvalue) + minvalue);
618 if(value < minvalue) value = minvalue;
619 if(value > maxvalue) value = maxvalue;
620 if(value != old_value) return 1;
624 int64_t BC_QPot::get_value()
626 return Freq::tofreq(value);
629 void BC_QPot::update(int64_t value)
631 value = Freq::fromfreq(value);
632 if(value < minvalue) value = minvalue;
633 if(value > maxvalue) value = maxvalue;
634 if(this->value != value)
642 BC_PercentagePot::BC_PercentagePot(int x, int y,
643 float value, float minvalue, float maxvalue, VFrame **data)
647 this->minvalue = minvalue;
648 this->maxvalue = maxvalue;
651 BC_PercentagePot::~BC_PercentagePot()
655 int BC_PercentagePot::increase_value()
658 if(value > maxvalue) value = maxvalue;
662 int BC_PercentagePot::decrease_value()
665 if(value < minvalue) value = minvalue;
669 const char* BC_PercentagePot::get_caption()
671 sprintf(caption, "%d%%", (int)(get_percentage() * 100 + 0.5));
675 float BC_PercentagePot::get_percentage()
677 float range = maxvalue - minvalue;
678 return range > 0 ? (value - minvalue) / range : minvalue;
681 int BC_PercentagePot::percentage_to_value(float percentage)
683 float old_value = value;
684 value = percentage * (maxvalue - minvalue) + minvalue;
685 if(value < minvalue) value = minvalue;
686 if(value > maxvalue) value = maxvalue;
687 if(value != old_value) return 1;
691 float BC_PercentagePot::get_value()
696 void BC_PercentagePot::update(float value)
698 if(value < minvalue) value= minvalue;
699 if(value > maxvalue) value= maxvalue;
700 if(this->value != value)