r602: Fix baver's code... don't insert timecode when show_tc is not set
[cinelerra_cv/mob.git] / cinelerra / zoompanel.C
blobc45ba3ddca1d0c7486d1955b212a49daaa056b6b
1 #include "clip.h"
2 #include "edl.h"
3 #include "edlsession.h"
4 #include "mwindow.h"
5 #include "mwindowgui.h"
6 #include "theme.h"
7 #include "trackcanvas.h"
8 #include "units.h"
9 #include "vframe.h"
10 #include "zoompanel.h"
13 ZoomHash::ZoomHash(double value, char *text)
15         this->value = value;
16         this->text = new char[strlen(text) + 1];
17         strcpy(this->text, text);
20 ZoomHash::~ZoomHash()
22         delete [] text;
26 ZoomPanel::ZoomPanel(MWindow *mwindow, 
27         BC_WindowBase *subwindow, 
28         double value, 
29         int x, 
30         int y,
31         int w, 
32         double min,
33         double max,
34         int zoom_type)
36         this->mwindow = mwindow;
37         this->subwindow = subwindow;
38         this->x = x;
39         this->y = y;
40         this->w = w;
41         this->value = value;
42         this->min = min;
43         this->max = max;
44         this->zoom_type = zoom_type;
47 ZoomPanel::~ZoomPanel()
49         delete zoom_text;
50         delete zoom_tumbler;
51         zoom_table.remove_all_objects();
54 void ZoomPanel::calculate_menu()
56         for(double zoom = min; zoom <= max; zoom *= 2)
57         {
58                 zoom_text->add_item(new BC_MenuItem(value_to_text(zoom, 0)));
59 //printf("ZoomPanel::calculate_menu 1 %s\n", value_to_text(zoom, 0));
60                 zoom_table.append(new ZoomHash(zoom, value_to_text(zoom, 0)));
61         }
64 void ZoomPanel::update_menu()
66         while(zoom_text->total_items())
67         {
68                 zoom_text->remove_item(0);
69         }
71         zoom_table.remove_all_objects();
72         calculate_menu();
76 int ZoomPanel::create_objects()
78         subwindow->add_subwindow(zoom_text = new ZoomPopup(mwindow, this, x, y));
79         x += zoom_text->get_w();
80         subwindow->add_subwindow(zoom_tumbler = new ZoomTumbler(mwindow, this, x, y));
81         calculate_menu();
82         return 0;
85 void ZoomPanel::reposition_window(int x, int y)
87         zoom_text->reposition_window(x, y);
88         x += zoom_text->get_w();
89         zoom_tumbler->reposition_window(x, y);
93 int ZoomPanel::get_w()
95         return zoom_text->get_w() + zoom_tumbler->get_w();
98 double ZoomPanel::get_value()
100         return value;
103 char* ZoomPanel::get_text()
105         return zoom_text->get_text();
108 void ZoomPanel::set_text(char *text)
110         zoom_text->set_text(text);
113 void ZoomPanel::update(double value)
115         this->value = value;
116         zoom_text->set_text(value_to_text(value));
119 void ZoomPanel::update(char *value)
121         zoom_text->set_text(value);
125 char* ZoomPanel::value_to_text(double value, int use_table)
127         if(use_table)
128         {
129                 for(int i = 0; i < zoom_table.total; i++)
130                 {
131 //printf("ZoomPanel::value_to_text %p\n", zoom_table.values[i]);
132                         if(EQUIV(zoom_table.values[i]->value, value))
133                                 return zoom_table.values[i]->text;
134                 }
135 //printf("ZoomPanel::value_to_text: should never get here\n");
136                 return zoom_table.values[0]->text;
137         }
139         switch(zoom_type)
140         {
141                 case ZOOM_PERCENTAGE:
142                         sprintf(string, "%d%%", (int)(value * 100));
143                         break;
145                 case ZOOM_FLOAT:
146                         sprintf(string, "%.1f", value);
147                         break;
149                 case ZOOM_LONG:
150                         sprintf(string, "%ld", (long)value);
151                         break;
153                 case ZOOM_TIME:
154                 {
155                         double total_seconds = (double)mwindow->gui->canvas->get_w() * 
156                                 value / 
157                                 mwindow->edl->session->sample_rate;
158                         Units::totext(string, 
159                                 total_seconds, 
160                                 mwindow->edl->session->time_format, 
161                                 mwindow->edl->session->sample_rate, 
162                                 mwindow->edl->session->frame_rate, 
163                                 mwindow->edl->session->frames_per_foot);
164                         break;
165                 }
166         }
167         return string;
170 double ZoomPanel::text_to_zoom(char *text, int use_table)
172         if(use_table)
173         {
174                 for(int i = 0; i < zoom_table.total; i++)
175                 {
176                         if(!strcasecmp(text, zoom_table.values[i]->text))
177                                 return zoom_table.values[i]->value;
178                 }
179                 return zoom_table.values[0]->value;
180         }
182         switch(zoom_type)
183         {
184                 case ZOOM_PERCENTAGE:
185                         return atof(text) / 100;
186                         break;
187                 case ZOOM_FLOAT:
188                 case ZOOM_LONG:
189                         return atof(text);
190                         break;
191                 case ZOOM_TIME:
192                 {
193                         double result = 1;
194                         double total_samples = Units::fromtext(text, 
195                                 mwindow->edl->session->sample_rate, 
196                                 mwindow->edl->session->time_format, 
197                                 mwindow->edl->session->frame_rate,
198                                 mwindow->edl->session->frames_per_foot);
199                         total_samples /= mwindow->gui->canvas->get_w();
200                         double difference = fabs(total_samples - result);
201                         while(fabs(result - total_samples) <= difference)
202                         {
203                                 difference = fabs(result - total_samples);
204                                 result *= 2;
205                         }
206                         return result;
207                         break;
208                 }
209         }
218 ZoomPopup::ZoomPopup(MWindow *mwindow, ZoomPanel *panel, int x, int y)
219  : BC_PopupMenu(x, 
220                 y, 
221                 panel->w, 
222                 panel->value_to_text(panel->value, 0), 
223                 1)
225         this->mwindow = mwindow;
226         this->panel = panel;
229 ZoomPopup::~ZoomPopup()
233 int ZoomPopup::handle_event()
235         panel->value = panel->text_to_zoom(get_text());
236         panel->handle_event();
237         return 1;
242 ZoomTumbler::ZoomTumbler(MWindow *mwindow, ZoomPanel *panel, int x, int y)
243  : BC_Tumbler(x, y)
245         this->mwindow = mwindow;
246         this->panel = panel;
249 ZoomTumbler::~ZoomTumbler()
253 int ZoomTumbler::handle_up_event()
255         panel->value *= 2;
256         RECLIP(panel->value, panel->min, panel->max);
257         panel->zoom_text->set_text(panel->value_to_text(panel->value));
258         panel->handle_event();
259         return 1;
262 int ZoomTumbler::handle_down_event()
264         panel->value /= 2;
265         RECLIP(panel->value, panel->min, panel->max);
266         panel->zoom_text->set_text(panel->value_to_text(panel->value));
267         panel->handle_event();
268         return 1;