r199: This commit was manufactured by cvs2svn to create tag 'hv_1_1_9'.
[cinelerra_cv/ct.git] / hvirtual / guicast / bcbutton.C
blob4b418d06a0de0706abcc13c6ecf4c7b327190001
1 #include "bcbutton.h"
2 #include "bcresources.h"
3 #include "bcpixmap.h"
4 #include "colors.h"
5 #include "fonts.h"
6 #include "keys.h"
7 #include "vframe.h"
10 #include <string.h>
11 #include <unistd.h>
13 #define BUTTON_UP 0
14 #define BUTTON_UPHI 1
15 #define BUTTON_DOWNHI 2
17 BC_Button::BC_Button(int x, 
18         int y, 
19         VFrame **data)
20  : BC_SubWindow(x, y, 0, 0, -1)
22         this->data = data;
23         for(int i = 0; i < 3; i++) images[i] = 0;
24         if(!data) printf("BC_Button::BC_Button data == 0\n");
25         status = BUTTON_UP;
26         this->w_argument = 0;
27         underline_number = -1;
28         enabled = 1;
31 BC_Button::BC_Button(int x, 
32         int y, 
33         int w, 
34         VFrame **data)
35  : BC_SubWindow(x, y, 0, 0, -1)
37         this->data = data;
38         this->w_argument = w;
39         for(int i = 0; i < 3; i++) images[i] = 0;
40         if(!data) printf("BC_Button::BC_Button data == 0\n");
41         status = BUTTON_UP;
42         underline_number = -1;
43         enabled = 1;
47 BC_Button::~BC_Button()
49         for(int i = 0; i < 3; i++) if(images[i]) delete images[i];
54 int BC_Button::initialize()
56 // Get the image
57         set_images(data);
59 // Create the subwindow
60         BC_SubWindow::initialize();
62 // Display the bitmap
63         draw_face();
64         return 0;
67 int BC_Button::reposition_window(int x, int y)
69         BC_WindowBase::reposition_window(x, y);
70         draw_face();
71         return 0;
75 int BC_Button::update_bitmaps(VFrame **data)
77         this->data = data;
78         set_images(data);
79         draw_top_background(parent_window, 0, 0, w, h);
80         draw_face();
81         return 0;
84 void BC_Button::enable()
86         enabled = 1;
87         draw_face();
90 void BC_Button::disable()
92         enabled = 0;
93         draw_face();
97 void BC_Button::set_underline(int number)
99         this->underline_number = number;
102 int BC_Button::set_images(VFrame **data)
104         for(int i = 0; i < 3; i++)
105         {
106                 if(images[i]) delete images[i];
107                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
108         }
110         if(w_argument > 0)
111                 w = w_argument;
112         else
113                 w = images[BUTTON_UP]->get_w();
115         h = images[BUTTON_UP]->get_h();
116         return 0;
119 int BC_Button::draw_face()
121         draw_top_background(parent_window, 0, 0, w, h);
122         images[status]->write_drawable(pixmap, 
123                         0, 
124                         0,
125                         w,
126                         h,
127                         0,
128                         0);
129         flash();
130         flush();
131         return 0;
134 int BC_Button::repeat_event(int64_t duration)
136         if(duration == top_level->get_resources()->tooltip_delay &&
137                 tooltip_text[0] != 0 &&
138                 status == BUTTON_UPHI &&
139                 !tooltip_done)
140         {
141                 show_tooltip();
142                 tooltip_done = 1;
143                 return 1;
144         }
145         return 0;
148 int BC_Button::cursor_enter_event()
150         if(top_level->event_win == win && enabled)
151         {
152                 tooltip_done = 0;
153                 if(top_level->button_down)
154                 {
155                         status = BUTTON_DOWNHI;
156                 }
157                 else
158                 if(status == BUTTON_UP) status = BUTTON_UPHI;
159                 draw_face();
160         }
161         return 0;
164 int BC_Button::cursor_leave_event()
166         if(status == BUTTON_UPHI)
167         {
168                 status = BUTTON_UP;
169                 draw_face();
170                 hide_tooltip();
171         }
172         return 0;
175 int BC_Button::button_press_event()
177         if(top_level->event_win == win && get_buttonpress() == 1 && enabled)
178         {
179                 hide_tooltip();
180                 if(status == BUTTON_UPHI || status == BUTTON_UP) status = BUTTON_DOWNHI;
181                 draw_face();
182                 return 1;
183         }
184         return 0;
187 int BC_Button::button_release_event()
189 //printf("BC_Button::button_release_event 1\n");
190         if(top_level->event_win == win)
191         {
192                 hide_tooltip();
193 //printf("BC_Button::button_release_event 2\n");
194                 if(status == BUTTON_DOWNHI) 
195                 {
196 //printf("BC_Button::button_release_event 3\n");
197                         status = BUTTON_UPHI;
198                         draw_face();
200                         if(cursor_inside())
201                         {
202 //printf("BC_Button::button_release_event 4\n");
203                                 handle_event();
204                                 return 1;
205                         }
206                 }
207         }
208         return 0;
211 int BC_Button::cursor_motion_event()
213         if(top_level->button_down && top_level->event_win == win && 
214                 status == BUTTON_DOWNHI && !cursor_inside())
215         {
216                 status = BUTTON_UP;
217                 draw_face();
218         }
219         return 0;
223 BC_OKButton::BC_OKButton(int x, int y)
224  : BC_Button(x, y, 
225         BC_WindowBase::get_resources()->ok_images)
229 BC_OKButton::BC_OKButton(BC_WindowBase *parent_window)
230  : BC_Button(10, 
231         parent_window->get_h() - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10, 
232         BC_WindowBase::get_resources()->ok_images)
236 int BC_OKButton::handle_event()
238         get_top_level()->set_done(0);
239         return 0;
242 int BC_OKButton::resize_event(int w, int h)
244         reposition_window(10,
245                 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
246         return 1;
249 int BC_OKButton::keypress_event()
251         if(get_keypress() == RETURN) return handle_event();
252         return 0;
255 BC_CancelButton::BC_CancelButton(int x, int y)
256  : BC_Button(x, y, 
257         BC_WindowBase::get_resources()->cancel_images)
261 BC_CancelButton::BC_CancelButton(BC_WindowBase *parent_window)
262  : BC_Button(parent_window->get_w() - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10, 
263         parent_window->get_h() - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10, 
264         BC_WindowBase::get_resources()->cancel_images)
268 int BC_CancelButton::handle_event()
270         get_top_level()->set_done(1);
271         return 1;
274 int BC_CancelButton::resize_event(int w,int h)
276         reposition_window(w - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10, 
277                 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
278         return 1;
281 int BC_CancelButton::keypress_event()
283         if(get_keypress() == ESC) return handle_event();
284         return 0;
290 #define LEFT_DN  0
291 #define LEFT_HI  1
292 #define LEFT_UP  2
293 #define MID_DN   3
294 #define MID_HI   4
295 #define MID_UP   5
296 #define RIGHT_DN 6
297 #define RIGHT_HI 7
298 #define RIGHT_UP 8
300 BC_GenericButton::BC_GenericButton(int x, int y, char *text, VFrame **data)
301  : BC_Button(x, 
302         y, 
303         data ? data : BC_WindowBase::get_resources()->generic_button_images)
305         strcpy(this->text, text);
308 BC_GenericButton::BC_GenericButton(int x, int y, int w, char *text, VFrame **data)
309  : BC_Button(x, 
310         y, 
311         w,
312         data ? data : BC_WindowBase::get_resources()->generic_button_images)
314         strcpy(this->text, text);
317 int BC_GenericButton::set_images(VFrame **data)
319         for(int i = 0; i < 3; i++)
320         {
321                 if(images[i]) delete images[i];
322                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
323         }
325         if(w_argument)
326                 w = w_argument;
327         else
328                 w = get_text_width(MEDIUMFONT, text) + images[BUTTON_UP]->get_w() / 3;
331         h = images[BUTTON_UP]->get_h();
332         return 0;
335 int BC_GenericButton::draw_face()
337         int x, y, w;
338         draw_top_background(parent_window, 0, 0, get_w(), get_h());
339         draw_3segmenth(0, 0, get_w(), images[status]);
341         if(enabled)
342                 set_color(BLACK);
343         else
344                 set_color(MEGREY);
345         set_font(MEDIUMFONT);
346         y = (int)((float)get_h() / 2 + get_text_ascent(MEDIUMFONT) / 2 - 2);
347         w = get_text_width(current_font, text, strlen(text));
348         x = get_w() / 2 - w / 2;
349         draw_text(x, 
350                 y, 
351                 text);
353         if(underline_number >= 0)
354         {
355                 y++;
356                 int x1 = get_text_width(current_font, text, underline_number) + x;
357                 int x2 = get_text_width(current_font, text, underline_number + 1) + x;
358                 draw_line(x1, y, x2, y);
359                 draw_line(x1, y + 1, (x2 + x1) / 2, y + 1);
360         }
362         flash();
363         flush();
364         return 0;