Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / guicast / bctumble.C
blob4ef4e8009e6a035acbd47fd3ac79bafc1e9041a5
1 #include "bcpixmap.h"
2 #include "bcresources.h"
3 #include "bctextbox.h"
4 #include "bctumble.h"
5 #include "math.h"
8 #define TUMBLE_UP 0
9 #define TUMBLE_UPHI 1
10 #define TUMBLEBOTTOM_DN 2
11 #define TUMBLETOP_DN 3
12 #define TOTAL_STATES 4
14 BC_Tumbler::BC_Tumbler(int x, int y, VFrame **data)
15  : BC_SubWindow(x, y, 0, 0, -1)
17         for(int i = 0; i < TOTAL_STATES; i++)
18                 images[i] = 0;
19         status = TUMBLE_UP;
20         repeat_count = 0;
21         this->data = data;
25 BC_Tumbler::~BC_Tumbler()
27         for(int i = 0; i < TOTAL_STATES; i ++)
28                 delete images[i];
33 int BC_Tumbler::initialize()
35 // Get the image
36         if(data)
37                 set_images(data);
38         else
39                 set_images(get_resources()->tumble_data);
40         w = images[TUMBLE_UP]->get_w();
41         h = images[TUMBLE_UP]->get_h();
43 // Create the subwindow
44         BC_SubWindow::initialize();
46 // Display the bitmap
47         draw_face();
48         return 0;
51 int BC_Tumbler::reposition_window(int x, int y, int w, int h)
53         if (w > 0 || h > 0) 
54                 printf("BC_Tumbler::reposition_window - w & h haven't been implemented yet!! (probably never will be)");
56         BC_WindowBase::reposition_window(x, y);
57         draw_face();
58         return 0;
62 int BC_Tumbler::update_bitmaps(VFrame **data)
64         set_images(data);
65         draw_top_background(parent_window, 0, 0, w, h);
66         draw_face();
67         return 0;
70 int BC_Tumbler::set_images(VFrame **data)
72         for(int i = 0; i < TOTAL_STATES; i++)
73         {
74                 if(images[i]) delete images[i];
75                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
76         }
78         return 0;
81 int BC_Tumbler::draw_face()
83         draw_top_background(parent_window, 0, 0, w, h);
84         pixmap->draw_pixmap(images[status], 
85                         0, 
86                         0,
87                         w,
88                         h,
89                         0,
90                         0);
91         flash();
92         return 0;
95 int BC_Tumbler::repeat_event(int64_t duration)
97 //printf("BC_Tumbler::repeat_event 1 %d\n", duration);
98         if(duration == top_level->get_resources()->tooltip_delay)
99         {
100                 if(tooltip_text[0] != 0 &&
101                         status == TUMBLE_UPHI &&
102                         !tooltip_done)
103                 {
104                         show_tooltip();
105                         tooltip_done = 1;
106                         return 1;
107                 }
108         }
109         else
110         if(duration == top_level->get_resources()->tumble_duration)
111         {
112 //printf("BC_Tumbler::repeat_event 2\n");
113                 repeat_count++;
114                 if(repeat_count == 2) return 0;
115                 if(status == TUMBLETOP_DN)
116                 {
117                         handle_up_event();
118                         return 1;
119                 }
120                 else
121                 if(status == TUMBLEBOTTOM_DN)
122                 {
123                         handle_down_event();
124                         return 1;
125                 }
126         }
127         return 0;
130 int BC_Tumbler::cursor_enter_event()
132         if(top_level->event_win == win)
133         {
134                 tooltip_done = 0;
135                 if(! top_level->button_down && status == TUMBLE_UP) 
136                 {
137                         status = TUMBLE_UPHI;
138                         draw_face();
139                 }
140         }
141         return 0;
144 int BC_Tumbler::cursor_leave_event()
146         hide_tooltip();
147         if(status == TUMBLE_UPHI)
148         {
149                 status = TUMBLE_UP;
150                 draw_face();
151         }
152         return 0;
155 int BC_Tumbler::button_press_event()
157         hide_tooltip();
158         if(top_level->event_win == win)
159         {
160 //printf("BC_Tumbler::button_press_event 1 %d\n", get_buttonpress());
161                 if(get_buttonpress() == 4)
162                 {
163                         status = TUMBLETOP_DN;
164                         draw_face();
165                         flush();
166                         handle_up_event();
167 //                      repeat_count = 0;
168 //                      repeat_event(top_level->get_resources()->tumble_duration);
169                 }
170                 else
171                 if(get_buttonpress() == 5)
172                 {
173                         status = TUMBLEBOTTOM_DN;
174                         draw_face();
175                         flush();
176                         handle_down_event();
177 //                      repeat_count = 0;
178 //                      repeat_event(top_level->get_resources()->tumble_duration);
179                 }
180                 else
181                 {
182                         if(top_level->cursor_y < get_h() / 2)
183                         {
184                                 status = TUMBLETOP_DN;
185                         }
186                         else
187                         {
188                                 status = TUMBLEBOTTOM_DN;
189                         }
191                         draw_face();
192                         flush();
194                         top_level->set_repeat(top_level->get_resources()->tumble_duration);
195                         repeat_count = 0;
196                         repeat_event(top_level->get_resources()->tumble_duration);
197 //printf("BC_Tumbler::button_press_event 2 %d\n", get_buttonpress());
198                 }
199                 return 1;
200         }
201         return 0;
204 int BC_Tumbler::button_release_event()
206         hide_tooltip();
207         if(top_level->event_win == win)
208         {
209                 if(status == TUMBLEBOTTOM_DN || status == TUMBLETOP_DN)
210                 {
211                         top_level->unset_repeat(top_level->get_resources()->tumble_duration);
212                         if(cursor_inside())
213                                 status = TUMBLE_UPHI;
214                         else
215                                 status = TUMBLE_UP;
216                 }
217                 draw_face();
218         }
219         return 0;
222 int BC_Tumbler::cursor_motion_event()
224         if(top_level->button_down && top_level->event_win == win && 
225                 !cursor_inside() &&
226                 !(status == TUMBLETOP_DN || status == TUMBLEBOTTOM_DN))
227         {
228                 status = TUMBLE_UP;
229                 draw_face();
230         }
231         return 0;
237 BC_ITumbler::BC_ITumbler(BC_TextBox *textbox, int64_t min, int64_t max, int x, int y)
238  : BC_Tumbler(x, y)
240         this->textbox = textbox;
241         this->min = min;
242         this->max = max;
243         this->increment = 1;
246 BC_ITumbler::~BC_ITumbler()
250 void BC_ITumbler::set_increment(float value)
252         this->increment = (int64_t)value;
253         if(increment < 1) increment = 1;
256 int BC_ITumbler::handle_up_event()
258         int64_t value = atol(textbox->get_text());
259         value += increment;
260         if(value > max) value = max;
261         textbox->update(value);
262         textbox->handle_event();
263         return 1;
266 int BC_ITumbler::handle_down_event()
268         int64_t value = atol(textbox->get_text());
269         value -= increment;
270         if(value < min) value = min;
271         textbox->update(value);
272         textbox->handle_event();
273         return 1;
276 void BC_ITumbler::set_boundaries(int64_t min, int64_t max)
278         this->min = min;
279         this->max = max;
291 BC_FTumbler::BC_FTumbler(BC_TextBox *textbox, 
292         float min, 
293         float max, 
294         int x, 
295         int y)
296  : BC_Tumbler(x, y)
298         this->textbox = textbox;
299         this->min = min;
300         this->max = max;
301         this->increment = 1.0;
302         this->log_floatincrement = 0;
305 BC_FTumbler::~BC_FTumbler()
309 int BC_FTumbler::handle_up_event()
311         float value = atof(textbox->get_text());
312         if (log_floatincrement) {
313                 // round off to to current precision (i.e. 250 -> 200)
314                 float cp = floor(log(value)/log(10) + 0.0001);
315                 value = floor((value/pow(10,cp))+ 0.0001)*pow(10,cp);
316                 value += pow(10,cp);
317         }
318         else
319                 value += increment;
320         if(value > max) value = max;
321         textbox->update(value);
322         textbox->handle_event();
323         return 1;
326 int BC_FTumbler::handle_down_event()
328         float value = atof(textbox->get_text());
329         if (log_floatincrement) {
330                 // round off to to current precision (i.e. 250 -> 200)
331                 float cp = floor(log(value)/log(10));
332                 value = floor(value/pow(10,cp))*pow(10,cp);
333                 // Need to make it so that: [.001 .01 .1 1 10 100] => [.0001 .001 .01 .1 1 10]
334                 cp = floor(log(value)/log(10)-.01);
335                 value -= pow(10,cp);
336         }
337         else
338                 value -= increment;
339         if(value < min) value = min;
340         textbox->update(value);
341         textbox->handle_event();
342         return 1;
345 void BC_FTumbler::set_boundaries(float min, float max)
347         this->min = min;
348         this->max = max;
351 void BC_FTumbler::set_increment(float value)
353         this->increment = value;
356 void BC_FTumbler::set_log_floatincrement(int value)
358         this->log_floatincrement = value;
361 //      Local Variables:
362 //      mode: C++
363 //      c-file-style: "linux"
364 //      End: