rework keyframe hide popup, keyframe auto render, textbox set_selection wide text
[goodguy/history.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],
117                         0,
118                         0,
119                         w,
120                         h,
121                         0,
122                         0);
123         flash(flush);
124         return 0;
125 }
126
127 int BC_Tumbler::repeat_event(int64_t duration)
128 {
129 //printf("BC_Tumbler::repeat_event 1 %d\n", duration);
130         if(duration == top_level->get_resources()->tooltip_delay)
131         {
132                 if(tooltip_text && tooltip_text[0] != 0 &&
133                         status == TUMBLE_UPHI && !tooltip_done)
134                 {
135                         show_tooltip();
136                         tooltip_done = 1;
137                         return 1;
138                 }
139         }
140         else
141         if(duration == top_level->get_resources()->tumble_duration)
142         {
143 //printf("BC_Tumbler::repeat_event 2\n");
144                 repeat_count++;
145                 if(repeat_count == 2) return 0;
146                 if(status == TUMBLETOP_DN)
147                 {
148                         handle_up_event();
149                         return 1;
150                 }
151                 else
152                 if(status == TUMBLEBOTTOM_DN)
153                 {
154                         handle_down_event();
155                         return 1;
156                 }
157         }
158         return 0;
159 }
160
161 int BC_Tumbler::cursor_enter_event()
162 {
163         if(top_level->event_win == win)
164         {
165                 tooltip_done = 0;
166                 if(! top_level->button_down && status == TUMBLE_UP)
167                 {
168                         status = TUMBLE_UPHI;
169                         draw_face(1);
170                 }
171         }
172         return 0;
173 }
174
175 int BC_Tumbler::cursor_leave_event()
176 {
177         hide_tooltip();
178         if(status == TUMBLE_UPHI)
179         {
180                 status = TUMBLE_UP;
181                 draw_face(1);
182         }
183         return 0;
184 }
185
186 int BC_Tumbler::button_press_event()
187 {
188         hide_tooltip();
189         if(top_level->event_win == win)
190         {
191 //printf("BC_Tumbler::button_press_event 1 %d\n", get_buttonpress());
192                 if(get_buttonpress() == 4)
193                 {
194                         status = TUMBLETOP_DN;
195                         draw_face(1);
196                         handle_up_event();
197 //                      repeat_count = 0;
198 //                      repeat_event(top_level->get_resources()->tumble_duration);
199                 }
200                 else
201                 if(get_buttonpress() == 5)
202                 {
203                         status = TUMBLEBOTTOM_DN;
204                         draw_face(1);
205                         handle_down_event();
206 //                      repeat_count = 0;
207 //                      repeat_event(top_level->get_resources()->tumble_duration);
208                 }
209                 else
210                 {
211                         if(top_level->cursor_y < get_h() / 2)
212                         {
213                                 status = TUMBLETOP_DN;
214                         }
215                         else
216                         {
217                                 status = TUMBLEBOTTOM_DN;
218                         }
219
220                         draw_face(1);
221
222                         top_level->set_repeat(top_level->get_resources()->tumble_duration);
223                         repeat_count = 0;
224                         repeat_event(top_level->get_resources()->tumble_duration);
225 //printf("BC_Tumbler::button_press_event 2 %d\n", get_buttonpress());
226                 }
227                 return 1;
228         }
229         return 0;
230 }
231
232 int BC_Tumbler::button_release_event()
233 {
234         hide_tooltip();
235         if(top_level->event_win == win)
236         {
237                 if(status == TUMBLEBOTTOM_DN || status == TUMBLETOP_DN)
238                 {
239                         top_level->unset_repeat(top_level->get_resources()->tumble_duration);
240                         if(cursor_inside())
241                                 status = TUMBLE_UPHI;
242                         else
243                                 status = TUMBLE_UP;
244                 }
245                 draw_face(1);
246         }
247         return 0;
248 }
249
250 int BC_Tumbler::cursor_motion_event()
251 {
252         if(top_level->button_down && top_level->event_win == win &&
253                 !cursor_inside() &&
254                 !(status == TUMBLETOP_DN || status == TUMBLEBOTTOM_DN))
255         {
256                 status = TUMBLE_UP;
257                 draw_face(1);
258         }
259         return 0;
260 }
261
262
263
264
265 BC_ITumbler::BC_ITumbler(BC_TextBox *textbox, int64_t min, int64_t max, int x, int y)
266  : BC_Tumbler(x, y)
267 {
268         this->textbox = textbox;
269         this->min = min;
270         this->max = max;
271         this->increment = 1;
272 }
273
274 BC_ITumbler::~BC_ITumbler()
275 {
276 }
277
278 void BC_ITumbler::set_increment(float value)
279 {
280         this->increment = (int64_t)value;
281         if(increment < 1) increment = 1;
282 }
283
284 int BC_ITumbler::handle_up_event()
285 {
286         int64_t value = atol(textbox->get_text());
287         value += increment;
288         if(value > max) value = max;
289         textbox->update(value);
290         textbox->handle_event();
291         return 1;
292 }
293
294 int BC_ITumbler::handle_down_event()
295 {
296         int64_t value = atol(textbox->get_text());
297         value -= increment;
298         if(value < min) value = min;
299         textbox->update(value);
300         textbox->handle_event();
301         return 1;
302 }
303
304 void BC_ITumbler::set_boundaries(int64_t min, int64_t max)
305 {
306         this->min = min;
307         this->max = max;
308 }
309
310
311
312
313
314
315
316
317
318
319 BC_FTumbler::BC_FTumbler(BC_TextBox *textbox,
320         float min,
321         float max,
322         int x,
323         int y)
324  : BC_Tumbler(x, y)
325 {
326         this->textbox = textbox;
327         this->min = min;
328         this->max = max;
329         this->increment = 1.0;
330         this->log_floatincrement = 0;
331 }
332
333 BC_FTumbler::~BC_FTumbler()
334 {
335 }
336
337 int BC_FTumbler::handle_up_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) + 0.0001);
343                 value = floor((value/pow(10,cp))+ 0.0001)*pow(10,cp);
344                 value += pow(10,cp);
345         }
346         else
347         value += increment;
348         if(value > max) value = max;
349         textbox->update(value);
350         textbox->handle_event();
351         return 1;
352 }
353
354 int BC_FTumbler::handle_down_event()
355 {
356         float value = atof(textbox->get_text());
357         if (log_floatincrement) {
358                 // round off to to current precision (i.e. 250 -> 200)
359                 float cp = floor(log(value)/log(10));
360                 value = floor(value/pow(10,cp))*pow(10,cp);
361                 // Need to make it so that: [.001 .01 .1 1 10 100] => [.0001 .001 .01 .1 1 10]
362                 cp = floor(log(value)/log(10)-.01);
363                 value -= pow(10,cp);
364         }
365         else
366         value -= increment;
367         if(value < min) value = min;
368         textbox->update(value);
369         textbox->handle_event();
370         return 1;
371 }
372
373 void BC_FTumbler::set_boundaries(float min, float max)
374 {
375         this->min = min;
376         this->max = max;
377 }
378
379 void BC_FTumbler::set_increment(float value)
380 {
381         this->increment = value;
382 }
383
384 void BC_FTumbler::set_log_floatincrement(int value)
385 {
386         this->log_floatincrement = value;
387 }