4 * Copyright (C) 1997-2014 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
22 #include "bcsignals.h"
25 #include "edlsession.h"
27 #include "mwindowgui.h"
29 #include "trackcanvas.h"
32 #include "zoompanel.h"
35 ZoomHash::ZoomHash(double value, const char *text)
38 this->text = new char[strlen(text) + 1];
39 strcpy(this->text, text);
63 ZoomPanel::ZoomPanel(MWindow *mwindow,
64 BC_WindowBase *subwindow,
73 this->mwindow = mwindow;
74 this->subwindow = subwindow;
81 this->zoom_type = zoom_type;
86 ZoomPanel::ZoomPanel(MWindow *mwindow,
87 BC_WindowBase *subwindow,
96 this->mwindow = mwindow;
97 this->subwindow = subwindow;
104 this->zoom_type = zoom_type;
105 this->user_table = user_table;
106 this->user_size = user_size;
109 ZoomPanel::~ZoomPanel()
113 zoom_table.remove_all_objects();
116 void ZoomPanel::calculate_menu()
120 for(int i = 0; i < user_size; i++)
122 zoom_text->add_item(new BC_MenuItem(value_to_text(user_table[i], 0)));
123 zoom_table.append(new ZoomHash(user_table[i], value_to_text(user_table[i], 0)));
128 for(double zoom = min; zoom <= max; zoom *= 2)
130 zoom_text->add_item(new BC_MenuItem(value_to_text(zoom, 0)));
131 zoom_table.append(new ZoomHash(zoom, value_to_text(zoom, 0)));
136 int ZoomPanel::calculate_w(int menu_w)
138 return BC_PopupMenu::calculate_w(menu_w) + BC_Tumbler::calculate_w();
141 void ZoomPanel::update_menu()
143 while(zoom_text->total_items()) zoom_text->del_item(0);
145 zoom_table.remove_all_objects();
149 void ZoomPanel::create_objects()
151 subwindow->add_subwindow(zoom_text = new ZoomPopup(mwindow, this, x, y));
152 x += zoom_text->get_w();
153 subwindow->add_subwindow(zoom_tumbler = new ZoomTumbler(mwindow, this, x, y));
157 void ZoomPanel::reposition_window(int x, int y)
159 zoom_text->reposition_window(x, y);
160 x += zoom_text->get_w();
161 zoom_tumbler->reposition_window(x, y);
165 int ZoomPanel::get_w()
167 return zoom_text->get_w() + zoom_tumbler->get_w();
170 double ZoomPanel::get_value()
175 char* ZoomPanel::get_text()
177 return zoom_text->get_text();
180 void ZoomPanel::set_text(const char *text)
182 zoom_text->set_text(text);
185 void ZoomPanel::set_tooltip(const char *text)
187 zoom_text->set_tooltip(text);
188 zoom_tumbler->set_tooltip(text);
191 void ZoomPanel::update(double value)
194 zoom_text->set_text(value_to_text(value));
197 void ZoomPanel::update(const char *value)
199 zoom_text->set_text(value);
203 char* ZoomPanel::value_to_text(double value, int use_table)
207 for(int i = 0; i < zoom_table.total; i++)
209 //printf("ZoomPanel::value_to_text %p\n", zoom_table.values[i]);
210 if(EQUIV(zoom_table.values[i]->value, value))
211 return zoom_table.values[i]->text;
213 //printf("ZoomPanel::value_to_text: should never get here\n");
214 return zoom_table.values[0]->text;
219 case ZOOM_PERCENTAGE:
220 sprintf(string, "%d%%", (int)(value * 100));
224 sprintf(string, "%.1f", value);
228 sprintf(string, "%ld", (long)value);
233 // sprintf(string, "%ld", (long)value);
234 double total_seconds = (double)(mwindow->theme->mcanvas_w -
235 mwindow->theme->patchbay_w -
236 BC_ScrollBar::get_span(SCROLL_VERT)) *
238 mwindow->edl->session->sample_rate;
239 Units::totext(string,
241 mwindow->edl->session->time_format,
242 mwindow->edl->session->sample_rate,
243 mwindow->edl->session->frame_rate,
244 mwindow->edl->session->frames_per_foot);
251 double ZoomPanel::text_to_zoom(char *text, int use_table)
254 for(int i = 0; i < zoom_table.total; i++) {
255 if(!strcasecmp(text, zoom_table.values[i]->text))
256 return zoom_table.values[i]->value;
258 return zoom_table.values[0]->value;
263 case ZOOM_PERCENTAGE:
264 result = atof(text) / 100;
272 double total_samples = Units::fromtext(text,
273 mwindow->edl->session->sample_rate,
274 mwindow->edl->session->time_format,
275 mwindow->edl->session->frame_rate,
276 mwindow->edl->session->frames_per_foot);
277 total_samples /= mwindow->theme->mcanvas_w -
278 mwindow->theme->patchbay_w -
279 BC_ScrollBar::get_span(SCROLL_VERT);
280 double difference = fabs(total_samples - result);
281 while(fabs(result - total_samples) <= difference) {
282 difference = fabs(result - total_samples);
297 ZoomPopup::ZoomPopup(MWindow *mwindow, ZoomPanel *panel, int x, int y)
298 : BC_PopupMenu(x, y, panel->w, panel->value_to_text(panel->value, 0),
299 1, mwindow->theme->get_image_set("zoombar_menu", 0))
301 this->mwindow = mwindow;
305 ZoomPopup::~ZoomPopup()
309 int ZoomPopup::handle_event()
311 panel->value = panel->text_to_zoom(get_text());
312 panel->handle_event();
318 ZoomTumbler::ZoomTumbler(MWindow *mwindow, ZoomPanel *panel, int x, int y)
319 : BC_Tumbler(x, y, mwindow->theme->get_image_set("zoombar_tumbler", 0))
321 this->mwindow = mwindow;
325 ZoomTumbler::~ZoomTumbler()
329 int ZoomTumbler::handle_up_event()
331 if(panel->user_table)
333 int current_index = 0;
334 for(current_index = 0; current_index < panel->user_size; current_index++)
335 if(EQUIV(panel->user_table[current_index], panel->value)) break;
337 CLAMP(current_index, 0, panel->user_size - 1);
338 panel->value = panel->user_table[current_index];
343 RECLIP(panel->value, panel->min, panel->max);
346 panel->zoom_text->set_text(panel->value_to_text(panel->value));
347 panel->handle_event();
351 int ZoomTumbler::handle_down_event()
353 if(panel->user_table)
355 int current_index = 0;
356 for(current_index = 0; current_index < panel->user_size; current_index++)
357 if(EQUIV(panel->user_table[current_index], panel->value)) break;
359 CLAMP(current_index, 0, panel->user_size - 1);
360 panel->value = panel->user_table[current_index];
365 RECLIP(panel->value, panel->min, panel->max);
367 panel->zoom_text->set_text(panel->value_to_text(panel->value));
368 panel->handle_event();