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