dxx-rebirth/common/ui/button.cpp

211 lines
5.2 KiB
C++
Raw Normal View History

2006-03-20 17:12:09 +00:00
/*
2014-06-01 17:55:23 +00:00
* Portions of this file are copyright Rebirth contributors and licensed as
* described in COPYING.txt.
* Portions of this file are copyright Parallax Software and licensed
* according to the Parallax license below.
* See COPYING.txt for license details.
2006-03-20 17:12:09 +00:00
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
*/
/*
*
* Routines for manipulating the button gadgets.
*
*/
2006-03-20 17:12:09 +00:00
#include <stdlib.h>
#include <string.h>
2013-12-05 00:10:35 +00:00
#include "strutil.h"
2006-03-20 17:12:09 +00:00
#include "u_mem.h"
2012-07-01 02:54:33 +00:00
#include "maths.h"
2006-03-20 17:12:09 +00:00
#include "pstypes.h"
#include "event.h"
2006-03-20 17:12:09 +00:00
#include "gr.h"
#include "ui.h"
#include "mouse.h"
2006-03-20 17:12:09 +00:00
#include "key.h"
2015-12-13 18:00:49 +00:00
namespace dcx {
2015-12-05 22:57:24 +00:00
2006-03-20 17:12:09 +00:00
#define Middle(x) ((2*(x)+1)/4)
#define BUTTON_EXTRA_WIDTH 15
#define BUTTON_EXTRA_HEIGHT 2
int ui_button_any_drawn = 0;
2017-02-11 21:42:34 +00:00
void ui_get_button_size(const grs_font &cv_font, const char *text, int &width, int &height)
2006-03-20 17:12:09 +00:00
{
2017-02-11 21:42:34 +00:00
gr_get_string_size(cv_font, text, &width, &height, nullptr);
width += BUTTON_EXTRA_WIDTH * 2 + 6;
height += BUTTON_EXTRA_HEIGHT * 2 + 6;
2006-03-20 17:12:09 +00:00
}
2012-03-03 12:19:15 +00:00
void ui_draw_button(UI_DIALOG *dlg, UI_GADGET_BUTTON * button)
2006-03-20 17:12:09 +00:00
{
#if 0 //ndef OGL
2006-03-20 17:12:09 +00:00
if ((button->status==1) || (button->position != button->oldposition))
#endif
2006-03-20 17:12:09 +00:00
{
ui_button_any_drawn = 1;
gr_set_current_canvas( button->canvas );
color_t color = 0;
2006-03-20 17:12:09 +00:00
gr_set_fontcolor(*grd_curcanv, dlg->keyboard_focus_gadget == button
? CRED
: (!button->user_function && button->dim_if_no_function
? CGREY
: CBLACK
), -1);
2006-03-20 17:12:09 +00:00
button->status = 0;
if (!button->text.empty())
2006-03-20 17:12:09 +00:00
{
2017-02-11 21:42:34 +00:00
unsigned offset;
if (button->position == 0)
{
2006-03-20 17:12:09 +00:00
ui_draw_box_out( 0, 0, button->width-1, button->height-1 );
2017-02-11 21:42:34 +00:00
offset = 0;
}
else
{
2006-03-20 17:12:09 +00:00
ui_draw_box_in( 0, 0, button->width-1, button->height-1 );
2017-02-11 21:42:34 +00:00
offset = 1;
}
2017-02-11 21:42:34 +00:00
ui_string_centered(Middle(button->width) + offset, Middle(button->height) + offset, button->text.c_str());
} else {
2017-02-11 21:42:34 +00:00
unsigned left, top, right, bottom;
if (button->position == 0)
{
2017-02-11 21:42:34 +00:00
left = top = 1;
right = button->width - 1;
bottom = button->height - 1;
}
else
{
2017-02-11 21:42:34 +00:00
left = top = 2;
right = button->width;
bottom = button->height;
}
2017-02-11 21:42:34 +00:00
gr_rect(*grd_curcanv, 0, 0, button->width, button->height, CBLACK);
gr_rect(*grd_curcanv, left, top, right, bottom, color);
2006-03-20 17:12:09 +00:00
}
}
}
std::unique_ptr<UI_GADGET_BUTTON> ui_add_gadget_button(UI_DIALOG * dlg, short x, short y, short w, short h, const char * text, int (*function_to_call)())
2006-03-20 17:12:09 +00:00
{
std::unique_ptr<UI_GADGET_BUTTON> button{ui_gadget_add<UI_GADGET_BUTTON>( dlg, x, y, x+w-1, y+h-1)};
2006-03-20 17:12:09 +00:00
if ( text )
{
button->text = text;
2006-03-20 17:12:09 +00:00
} else {
button->text.clear();
2006-03-20 17:12:09 +00:00
}
button->width = w;
button->height = h;
button->position = 0;
button->oldposition = 0;
button->pressed = 0;
button->user_function = function_to_call;
button->user_function1 = NULL;
button->hotkey1= -1;
button->dim_if_no_function = 0;
return button;
}
2014-10-04 21:47:13 +00:00
window_event_result ui_button_do(UI_DIALOG *dlg, UI_GADGET_BUTTON * button,const d_event &event)
2006-03-20 17:12:09 +00:00
{
2014-08-06 02:10:49 +00:00
window_event_result rval = window_event_result::ignored;
2006-03-20 17:12:09 +00:00
button->oldposition = button->position;
button->pressed = 0;
2006-03-20 17:12:09 +00:00
2014-10-04 21:47:13 +00:00
if (event.type == EVENT_MOUSE_BUTTON_DOWN || event.type == EVENT_MOUSE_BUTTON_UP)
2006-03-20 17:12:09 +00:00
{
int OnMe;
2006-03-20 17:12:09 +00:00
2014-10-04 22:13:47 +00:00
OnMe = ui_mouse_on_gadget( button );
2006-03-20 17:12:09 +00:00
if (B1_JUST_PRESSED && OnMe)
{
button->position = 1;
2014-08-06 02:10:49 +00:00
rval = window_event_result::handled;
}
else if (B1_JUST_RELEASED)
{
if ((button->position == 1) && OnMe)
button->pressed = 1;
2006-03-20 17:12:09 +00:00
button->position = 0;
}
2006-03-20 17:12:09 +00:00
}
2014-10-04 21:47:13 +00:00
if (event.type == EVENT_KEY_COMMAND)
{
int keypress;
keypress = event_key_get(event);
2006-03-20 17:12:09 +00:00
if ((keypress == button->hotkey) ||
((keypress == button->hotkey1) && button->user_function1) ||
2014-10-04 22:13:47 +00:00
((dlg->keyboard_focus_gadget==button) && ((keypress==KEY_SPACEBAR) || (keypress==KEY_ENTER)) ))
{
2006-03-20 17:12:09 +00:00
button->position = 2;
2014-08-06 02:10:49 +00:00
rval = window_event_result::handled;
}
}
2014-10-04 21:47:13 +00:00
else if (event.type == EVENT_KEY_RELEASE)
{
int keypress;
keypress = event_key_get(event);
button->position = 0;
2006-03-20 17:12:09 +00:00
if ((keypress == button->hotkey) ||
2014-10-04 22:13:47 +00:00
((dlg->keyboard_focus_gadget==button) && ((keypress==KEY_SPACEBAR) || (keypress==KEY_ENTER)) ))
2006-03-20 17:12:09 +00:00
button->pressed = 1;
if ((keypress == button->hotkey1) && button->user_function1)
{
button->user_function1();
2014-08-06 02:10:49 +00:00
rval = window_event_result::handled;
}
2006-03-20 17:12:09 +00:00
}
2014-10-04 21:47:13 +00:00
if (event.type == EVENT_WINDOW_DRAW)
ui_draw_button( dlg, button );
2006-03-20 17:12:09 +00:00
if (button->pressed && button->user_function )
{
2011-09-26 23:31:19 +00:00
button->user_function();
2014-08-06 02:10:49 +00:00
return window_event_result::handled;
2006-03-20 17:12:09 +00:00
}
2012-03-03 12:19:15 +00:00
else if (button->pressed)
{
rval = ui_gadget_send_event(dlg, EVENT_UI_GADGET_PRESSED, button);
if (rval == window_event_result::ignored)
rval = window_event_result::handled;
2012-03-03 12:19:15 +00:00
}
return rval;
2006-03-20 17:12:09 +00:00
}
2015-12-05 22:57:24 +00:00
}