add BC_SCALE env var for hi def monitors, cleanup theme data
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bctumble.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "bcpixmap.h"
23 #include "bcresources.h"
24 #include "bctextbox.h"
25 #include "bctumble.h"
26 #include "math.h"
27 #include "vframe.h"
28
29
30 #define TUMBLE_UP 0
31 #define TUMBLE_UPHI 1
32 #define TUMBLEBOTTOM_DN 2
33 #define TUMBLETOP_DN 3
34 #define TOTAL_STATES 4
35
36 BC_Tumbler::BC_Tumbler(int x, int y, VFrame **data)
37  : BC_SubWindow(x, y, 0, 0, -1)
38 {
39         for(int i = 0; i < TOTAL_STATES; i++)
40                 images[i] = 0;
41         status = TUMBLE_UP;
42         repeat_count = 0;
43         this->data = data;
44 }
45
46
47 BC_Tumbler::~BC_Tumbler()
48 {
49         for(int i = 0; i < TOTAL_STATES; i ++)
50                 delete images[i];
51 }
52
53
54 int BC_Tumbler::calculate_w()
55 {
56         return BC_WindowBase::get_resources()->tumble_data[0]->get_w();
57 }
58
59 int BC_Tumbler::calculate_h()
60 {
61         return BC_WindowBase::get_resources()->tumble_data[0]->get_h();
62 }
63
64
65 int BC_Tumbler::initialize()
66 {
67 // Get the image
68         if(data)
69                 set_images(data);
70         else
71                 set_images(get_resources()->tumble_data);
72         w = images[TUMBLE_UP]->get_w();
73         h = images[TUMBLE_UP]->get_h();
74
75 // Create the subwindow
76         BC_SubWindow::initialize();
77
78 // Display the bitmap
79         draw_face(0);
80         return 0;
81 }
82
83 int BC_Tumbler::reposition_window(int x, int y, int w, int h)
84 {
85         if (w > 0 || h > 0)
86                 printf("BC_Tumbler::reposition_window - w & h haven't been implemented yet!! (probably never will be)");
87
88         BC_WindowBase::reposition_window(x, y);
89         draw_face(0);
90         return 0;
91 }
92
93
94 int BC_Tumbler::update_bitmaps(VFrame **data)
95 {
96         set_images(data);
97         draw_top_background(parent_window, 0, 0, w, h);
98         draw_face(1);
99         return 0;
100 }
101
102 int BC_Tumbler::set_images(VFrame **data)
103 {
104         for(int i = 0; i < TOTAL_STATES; i++)
105         {
106                 if(images[i]) delete images[i];
107                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
108         }
109
110         return 0;
111 }
112
113 int BC_Tumbler::draw_face(int flush)
114 {
115         draw_top_background(parent_window, 0, 0, w, h);
116         pixmap->draw_pixmap(images[status], 0, 0, w, h, 0, 0);
117         flash(flush);
118         return 0;
119 }
120
121 int BC_Tumbler::repeat_event(int64_t duration)
122 {
123 //printf("BC_Tumbler::repeat_event 1 %d\n", duration);
124         if(duration == top_level->get_resources()->tooltip_delay)
125         {
126                 if(tooltip_text && tooltip_text[0] != 0 &&
127                         status == TUMBLE_UPHI)
128                 {
129                         show_tooltip();
130                         return 1;
131                 }
132         }
133         else
134         if(duration == top_level->get_resources()->tumble_duration)
135         {
136 //printf("BC_Tumbler::repeat_event 2\n");
137                 repeat_count++;
138 // delay the 1st repeat
139                 if(repeat_count > 1 && repeat_count < 5) return 0;
140                 if(status == TUMBLETOP_DN)
141                 {
142                         handle_up_event();
143                         return 1;
144                 }
145                 else
146                 if(status == TUMBLEBOTTOM_DN)
147                 {
148                         handle_down_event();
149                         return 1;
150                 }
151         }
152         return 0;
153 }
154
155 int BC_Tumbler::cursor_enter_event()
156 {
157         if(top_level->event_win == win)
158         {
159                 if(! top_level->button_down && status == TUMBLE_UP)
160                 {
161                         status = TUMBLE_UPHI;
162                         draw_face(1);
163                 }
164         }
165         return 0;
166 }
167
168 int BC_Tumbler::cursor_leave_event()
169 {
170         hide_tooltip();
171         if(status == TUMBLE_UPHI)
172         {
173                 status = TUMBLE_UP;
174                 draw_face(1);
175         }
176         return 0;
177 }
178
179 int BC_Tumbler::button_press_event()
180 {
181         hide_tooltip();
182         if(top_level->event_win == win)
183         {
184 //printf("BC_Tumbler::button_press_event 1 %d\n", get_buttonpress());
185                 if(get_buttonpress() == 4)
186                 {
187                         status = TUMBLETOP_DN;
188                         draw_face(1);
189                         handle_up_event();
190 //                      repeat_count = 0;
191 //                      repeat_event(top_level->get_resources()->tumble_duration);
192                 }
193                 else
194                 if(get_buttonpress() == 5)
195                 {
196                         status = TUMBLEBOTTOM_DN;
197                         draw_face(1);
198                         handle_down_event();
199 //                      repeat_count = 0;
200 //                      repeat_event(top_level->get_resources()->tumble_duration);
201                 }
202                 else
203                 {
204                         if(top_level->cursor_y < get_h() / 2)
205                         {
206                                 status = TUMBLETOP_DN;
207                         }
208                         else
209                         {
210                                 status = TUMBLEBOTTOM_DN;
211                         }
212
213                         draw_face(1);
214
215                         top_level->set_repeat(top_level->get_resources()->tumble_duration);
216                         repeat_count = 0;
217                         repeat_event(top_level->get_resources()->tumble_duration);
218 //printf("BC_Tumbler::button_press_event 2 %d\n", get_buttonpress());
219                 }
220                 return 1;
221         }
222         return 0;
223 }
224
225 int BC_Tumbler::button_release_event()
226 {
227         hide_tooltip();
228         if(top_level->event_win == win)
229         {
230                 if(status == TUMBLEBOTTOM_DN || status == TUMBLETOP_DN)
231                 {
232                         top_level->unset_repeat(top_level->get_resources()->tumble_duration);
233                         if(cursor_inside())
234                                 status = TUMBLE_UPHI;
235                         else
236                                 status = TUMBLE_UP;
237                 }
238                 draw_face(1);
239         }
240         return 0;
241 }
242
243 int BC_Tumbler::cursor_motion_event()
244 {
245         if(top_level->button_down && top_level->event_win == win &&
246                 !cursor_inside() &&
247                 !(status == TUMBLETOP_DN || status == TUMBLEBOTTOM_DN))
248         {
249                 status = TUMBLE_UP;
250                 draw_face(1);
251         }
252         return 0;
253 }
254
255
256 BC_ITumbler::BC_ITumbler(BC_TextBox *textbox, int64_t min, int64_t max, int x, int y)
257  : BC_Tumbler(x, y)
258 {
259         this->textbox = textbox;
260         this->min = min;
261         this->max = max;
262         this->increment = 1;
263 }
264
265 BC_ITumbler::~BC_ITumbler()
266 {
267 }
268
269 void BC_ITumbler::set_increment(float value)
270 {
271         this->increment = (int64_t)value;
272         if(increment < 1) increment = 1;
273 }
274
275 int BC_ITumbler::handle_up_event()
276 {
277         int64_t value = atol(textbox->get_text());
278         value += increment;
279         if(value > max) value = max;
280         textbox->update(value);
281         textbox->handle_event();
282         return 1;
283 }
284
285 int BC_ITumbler::handle_down_event()
286 {
287         int64_t value = atol(textbox->get_text());
288         value -= increment;
289         if(value < min) value = min;
290         textbox->update(value);
291         textbox->handle_event();
292         return 1;
293 }
294
295 void BC_ITumbler::set_boundaries(int64_t min, int64_t max)
296 {
297         this->min = min;
298         this->max = max;
299 }
300
301
302 BC_FTumbler::BC_FTumbler(BC_TextBox *textbox,
303         float min,
304         float max,
305         int x,
306         int y)
307  : BC_Tumbler(x, y)
308 {
309         this->textbox = textbox;
310         this->min = min;
311         this->max = max;
312         this->increment = 1.0;
313         this->log_floatincrement = 0;
314 }
315
316 BC_FTumbler::~BC_FTumbler()
317 {
318 }
319
320 int BC_FTumbler::handle_up_event()
321 {
322         float value = atof(textbox->get_text());
323         if (log_floatincrement) {
324                 // round off to to current precision (i.e. 250 -> 200)
325                 float cp = floor(log(value)/log(10) + 0.0001);
326                 value = floor((value/pow(10,cp))+ 0.0001)*pow(10,cp);
327                 value += pow(10,cp);
328         }
329         else
330         value += increment;
331         if(value > max) value = max;
332         textbox->update(value);
333         textbox->handle_event();
334         return 1;
335 }
336
337 int BC_FTumbler::handle_down_event()
338 {
339         float value = atof(textbox->get_text());
340         if (log_floatincrement) {
341                 // round off to to current precision (i.e. 250 -> 200)
342                 float cp = floor(log(value)/log(10));
343                 value = floor(value/pow(10,cp))*pow(10,cp);
344                 // Need to make it so that: [.001 .01 .1 1 10 100] => [.0001 .001 .01 .1 1 10]
345                 cp = floor(log(value)/log(10)-.01);
346                 value -= pow(10,cp);
347         }
348         else
349         value -= increment;
350         if(value < min) value = min;
351         textbox->update(value);
352         textbox->handle_event();
353         return 1;
354 }
355
356 void BC_FTumbler::set_boundaries(float min, float max)
357 {
358         this->min = min;
359         this->max = max;
360 }
361
362 void BC_FTumbler::set_increment(float value)
363 {
364         this->increment = value;
365 }
366
367 void BC_FTumbler::set_log_floatincrement(int value)
368 {
369         this->log_floatincrement = value;
370 }