00001 #include "bcbutton.h"
00002 #include "bcpixmap.h"
00003 #include "bcresources.h"
00004 #include "bcsignals.h"
00005 #include "colors.h"
00006 #include "fonts.h"
00007 #include "keys.h"
00008 #include "language.h"
00009 #include "vframe.h"
00010
00011
00012 #include <string.h>
00013 #include <unistd.h>
00014
00015 #define BUTTON_UP 0
00016 #define BUTTON_UPHI 1
00017 #define BUTTON_DOWNHI 2
00018
00019 BC_Button::BC_Button(int x,
00020 int y,
00021 VFrame **data)
00022 : BC_SubWindow(x, y, 0, 0, -1)
00023 {
00024 this->data = data;
00025 for(int i = 0; i < 3; i++) images[i] = 0;
00026 if(!data) printf("BC_Button::BC_Button data == 0\n");
00027 status = BUTTON_UP;
00028 this->w_argument = 0;
00029 underline_number = -1;
00030 enabled = 1;
00031 }
00032
00033 BC_Button::BC_Button(int x,
00034 int y,
00035 int w,
00036 VFrame **data)
00037 : BC_SubWindow(x, y, 0, 0, -1)
00038 {
00039 this->data = data;
00040 this->w_argument = w;
00041 for(int i = 0; i < 3; i++) images[i] = 0;
00042 if(!data) printf("BC_Button::BC_Button data == 0\n");
00043 status = BUTTON_UP;
00044 underline_number = -1;
00045 enabled = 1;
00046 }
00047
00048
00049 BC_Button::~BC_Button()
00050 {
00051 for(int i = 0; i < 3; i++) if(images[i]) delete images[i];
00052 }
00053
00054
00055
00056 int BC_Button::initialize()
00057 {
00058
00059 set_images(data);
00060
00061
00062 BC_SubWindow::initialize();
00063
00064
00065 draw_face();
00066 return 0;
00067 }
00068
00069 int BC_Button::reposition_window(int x, int y)
00070 {
00071 BC_WindowBase::reposition_window(x, y);
00072 draw_face();
00073 return 0;
00074 }
00075
00076
00077 int BC_Button::update_bitmaps(VFrame **data)
00078 {
00079 this->data = data;
00080 set_images(data);
00081 draw_top_background(parent_window, 0, 0, w, h);
00082 draw_face();
00083 return 0;
00084 }
00085
00086 void BC_Button::enable()
00087 {
00088 enabled = 1;
00089 draw_face();
00090 }
00091
00092 void BC_Button::disable()
00093 {
00094 enabled = 0;
00095 draw_face();
00096 }
00097
00098
00099 void BC_Button::set_underline(int number)
00100 {
00101 this->underline_number = number;
00102 }
00103
00104 int BC_Button::set_images(VFrame **data)
00105 {
00106 for(int i = 0; i < 3; i++)
00107 {
00108 if(images[i]) delete images[i];
00109 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
00110 }
00111
00112 if(w_argument > 0)
00113 w = w_argument;
00114 else
00115 w = images[BUTTON_UP]->get_w();
00116
00117 h = images[BUTTON_UP]->get_h();
00118 return 0;
00119 }
00120
00121 int BC_Button::draw_face()
00122 {
00123 draw_top_background(parent_window, 0, 0, w, h);
00124 pixmap->draw_pixmap(images[status],
00125 0,
00126 0,
00127 w,
00128 h,
00129 0,
00130 0);
00131 flash();
00132 return 0;
00133 }
00134
00135 int BC_Button::repeat_event(int64_t duration)
00136 {
00137 if(duration == top_level->get_resources()->tooltip_delay &&
00138 tooltip_text[0] != 0 &&
00139 status == BUTTON_UPHI &&
00140 !tooltip_done)
00141 {
00142 show_tooltip();
00143 tooltip_done = 1;
00144 return 1;
00145 }
00146 return 0;
00147 }
00148
00149 int BC_Button::cursor_enter_event()
00150 {
00151 if(top_level->event_win == win && enabled)
00152 {
00153 tooltip_done = 0;
00154 if(top_level->button_down)
00155 {
00156 status = BUTTON_DOWNHI;
00157 }
00158 else
00159 if(status == BUTTON_UP) status = BUTTON_UPHI;
00160 draw_face();
00161 }
00162 return 0;
00163 }
00164
00165 int BC_Button::cursor_leave_event()
00166 {
00167 if(status == BUTTON_UPHI)
00168 {
00169 status = BUTTON_UP;
00170
00171 draw_face();
00172
00173 hide_tooltip();
00174
00175 }
00176 return 0;
00177 }
00178
00179 int BC_Button::button_press_event()
00180 {
00181 if(top_level->event_win == win && get_buttonpress() == 1 && enabled)
00182 {
00183 hide_tooltip();
00184 if(status == BUTTON_UPHI || status == BUTTON_UP) status = BUTTON_DOWNHI;
00185 draw_face();
00186 return 1;
00187 }
00188 return 0;
00189 }
00190
00191 int BC_Button::button_release_event()
00192 {
00193 if(top_level->event_win == win)
00194 {
00195 hide_tooltip();
00196 if(status == BUTTON_DOWNHI)
00197 {
00198 status = BUTTON_UPHI;
00199 draw_face();
00200
00201 if(cursor_inside())
00202 {
00203 handle_event();
00204 return 1;
00205 }
00206 }
00207 }
00208 return 0;
00209 }
00210
00211 int BC_Button::cursor_motion_event()
00212 {
00213 if(top_level->button_down && top_level->event_win == win &&
00214 status == BUTTON_DOWNHI && !cursor_inside())
00215 {
00216 status = BUTTON_UP;
00217 draw_face();
00218 }
00219 return 0;
00220 }
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 BC_OKButton::BC_OKButton(int x, int y)
00233 : BC_Button(x, y,
00234 BC_WindowBase::get_resources()->ok_images)
00235 {
00236 }
00237
00238 BC_OKButton::BC_OKButton(BC_WindowBase *parent_window, VFrame **images)
00239 : BC_Button(10,
00240 parent_window->get_h() - images[0]->get_h() - 10,
00241 images)
00242 {
00243 set_tooltip("OK");
00244 }
00245
00246 BC_OKButton::BC_OKButton(BC_WindowBase *parent_window)
00247 : BC_Button(10,
00248 parent_window->get_h() - BC_WindowBase::get_resources()->ok_images[0]->get_h() - 10,
00249 BC_WindowBase::get_resources()->ok_images)
00250 {
00251 set_tooltip("OK");
00252 }
00253
00254 int BC_OKButton::handle_event()
00255 {
00256 get_top_level()->set_done(0);
00257 return 0;
00258 }
00259
00260 int BC_OKButton::resize_event(int w, int h)
00261 {
00262 reposition_window(10,
00263 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
00264 return 1;
00265 }
00266
00267 int BC_OKButton::keypress_event()
00268 {
00269 if(get_keypress() == RETURN) return handle_event();
00270 return 0;
00271 }
00272
00273 int BC_OKButton::calculate_h()
00274 {
00275 return BC_WindowBase::get_resources()->ok_images[0]->get_h();
00276 }
00277
00278 int BC_OKButton::calculate_w()
00279 {
00280 return BC_WindowBase::get_resources()->ok_images[0]->get_w();
00281 }
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 BC_CancelButton::BC_CancelButton(int x, int y)
00296 : BC_Button(x, y,
00297 BC_WindowBase::get_resources()->cancel_images)
00298 {
00299 set_tooltip("Cancel");
00300 }
00301
00302 BC_CancelButton::BC_CancelButton(BC_WindowBase *parent_window)
00303 : BC_Button(parent_window->get_w() - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10,
00304 parent_window->get_h() - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10,
00305 BC_WindowBase::get_resources()->cancel_images)
00306 {
00307 set_tooltip("Cancel");
00308 }
00309
00310 BC_CancelButton::BC_CancelButton(BC_WindowBase *parent_window, VFrame **images)
00311 : BC_Button(parent_window->get_w() - images[0]->get_w() - 10,
00312 parent_window->get_h() - images[0]->get_h() - 10,
00313 images)
00314 {
00315 set_tooltip("Cancel");
00316 }
00317
00318 int BC_CancelButton::handle_event()
00319 {
00320 get_top_level()->set_done(1);
00321 return 1;
00322 }
00323
00324 int BC_CancelButton::resize_event(int w,int h)
00325 {
00326 reposition_window(w - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10,
00327 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
00328 return 1;
00329 }
00330
00331 int BC_CancelButton::keypress_event()
00332 {
00333 if(get_keypress() == ESC) return handle_event();
00334 return 0;
00335 }
00336
00337 int BC_CancelButton::calculate_h()
00338 {
00339 return BC_WindowBase::get_resources()->cancel_images[0]->get_h();
00340 }
00341
00342 int BC_CancelButton::calculate_w()
00343 {
00344 return BC_WindowBase::get_resources()->cancel_images[0]->get_w();
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 #define LEFT_DN 0
00357 #define LEFT_HI 1
00358 #define LEFT_UP 2
00359 #define MID_DN 3
00360 #define MID_HI 4
00361 #define MID_UP 5
00362 #define RIGHT_DN 6
00363 #define RIGHT_HI 7
00364 #define RIGHT_UP 8
00365
00366 BC_GenericButton::BC_GenericButton(int x, int y, char *text, VFrame **data)
00367 : BC_Button(x,
00368 y,
00369 data ? data : BC_WindowBase::get_resources()->generic_button_images)
00370 {
00371 strcpy(this->text, text);
00372 }
00373
00374 BC_GenericButton::BC_GenericButton(int x, int y, int w, char *text, VFrame **data)
00375 : BC_Button(x,
00376 y,
00377 w,
00378 data ? data : BC_WindowBase::get_resources()->generic_button_images)
00379 {
00380 strcpy(this->text, text);
00381 }
00382
00383 int BC_GenericButton::set_images(VFrame **data)
00384 {
00385 BC_Resources *resources = get_resources();
00386 for(int i = 0; i < 3; i++)
00387 {
00388 if(images[i]) delete images[i];
00389 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
00390 }
00391
00392 if(w_argument)
00393 w = w_argument;
00394 else
00395 w = get_text_width(MEDIUMFONT, text) +
00396 resources->generic_button_margin * 2;
00397
00398
00399 h = images[BUTTON_UP]->get_h();
00400 return 0;
00401 }
00402
00403 int BC_GenericButton::calculate_w(BC_WindowBase *gui, char *text)
00404 {
00405 BC_Resources *resources = gui->get_resources();
00406 return gui->get_text_width(MEDIUMFONT, text) +
00407 resources->generic_button_margin * 2;
00408 }
00409
00410 int BC_GenericButton::calculate_h()
00411 {
00412 BC_Resources *resources = BC_WindowBase::get_resources();
00413 return resources->generic_button_images[0]->get_h();
00414 }
00415
00416 int BC_GenericButton::draw_face()
00417 {
00418 draw_top_background(parent_window, 0, 0, get_w(), get_h());
00419 draw_3segmenth(0, 0, get_w(), images[status]);
00420
00421 if(enabled)
00422 set_color(get_resources()->default_text_color);
00423 else
00424 set_color(get_resources()->disabled_text_color);
00425 set_font(MEDIUMFONT);
00426
00427 int x, y, w;
00428 BC_Resources *resources = get_resources();
00429 y = (int)((float)get_h() / 2 + get_text_ascent(MEDIUMFONT) / 2 - 2);
00430 w = get_text_width(current_font, text, strlen(text)) +
00431 resources->generic_button_margin * 2;
00432 x = get_w() / 2 - w / 2 + resources->generic_button_margin;
00433 if(status == BUTTON_DOWNHI)
00434 {
00435 x++;
00436 y++;
00437 }
00438 draw_text(x,
00439 y,
00440 text);
00441
00442 if(underline_number >= 0)
00443 {
00444 y++;
00445 int x1 = get_text_width(current_font, text, underline_number) +
00446 x +
00447 resources->toggle_text_margin;
00448 int x2 = get_text_width(current_font, text, underline_number + 1) +
00449 x +
00450 resources->toggle_text_margin;
00451 draw_line(x1, y, x2, y);
00452 draw_line(x1, y + 1, (x2 + x1) / 2, y + 1);
00453 }
00454
00455 flash();
00456 return 0;
00457 }
00458
00459
00460
00461
00462
00463 BC_OKTextButton::BC_OKTextButton(BC_WindowBase *parent_window)
00464 : BC_GenericButton(10,
00465 parent_window->get_h() - BC_GenericButton::calculate_h() - 10,
00466 _("OK"))
00467 {
00468 this->parent_window = parent_window;
00469 }
00470
00471 int BC_OKTextButton::resize_event(int w, int h)
00472 {
00473 reposition_window(10,
00474 parent_window->get_h() - BC_GenericButton::calculate_h() - 10);
00475 return 1;
00476 }
00477
00478 int BC_OKTextButton::handle_event()
00479 {
00480 get_top_level()->set_done(0);
00481 return 0;
00482 }
00483
00484 int BC_OKTextButton::keypress_event()
00485 {
00486 if(get_keypress() == RETURN) return handle_event();
00487 return 0;
00488 }
00489
00490
00491
00492 BC_CancelTextButton::BC_CancelTextButton(BC_WindowBase *parent_window)
00493 : BC_GenericButton(parent_window->get_w() - BC_GenericButton::calculate_w(parent_window, _("Cancel")) - 10,
00494 parent_window->get_h() - BC_GenericButton::calculate_h() - 10,
00495 _("Cancel"))
00496 {
00497 this->parent_window = parent_window;
00498 }
00499
00500 int BC_CancelTextButton::resize_event(int w, int h)
00501 {
00502 reposition_window(parent_window->get_w() - BC_GenericButton::calculate_w(parent_window, _("Cancel")) - 10,
00503 parent_window->get_h() - BC_GenericButton::calculate_h() - 10);
00504 return 1;
00505 }
00506
00507 int BC_CancelTextButton::handle_event()
00508 {
00509 get_top_level()->set_done(1);
00510 return 1;
00511 }
00512
00513 int BC_CancelTextButton::keypress_event()
00514 {
00515 if(get_keypress() == ESC) return handle_event();
00516 return 0;
00517 }
00518
00519
00520
00521
00522
00523