dxx-rebirth/common/ui/checkbox.cpp

152 lines
4 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.
*/
#include <stdlib.h>
#include <string.h>
#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)
2020-10-12 03:28:26 +00:00
namespace {
void ui_draw_checkbox(UI_DIALOG &dlg, UI_GADGET_CHECKBOX &checkbox)
2006-03-20 17:12:09 +00:00
{
#if 0 //ndef OGL
2020-10-12 03:28:26 +00:00
if ((checkbox.status==1) || (checkbox.position != checkbox.oldposition))
#endif
2006-03-20 17:12:09 +00:00
{
2020-10-12 03:28:26 +00:00
checkbox.status = 0;
2006-03-20 17:12:09 +00:00
2020-10-12 03:28:26 +00:00
gr_set_current_canvas(checkbox.canvas);
2017-02-11 21:42:46 +00:00
auto &canvas = *grd_curcanv;
2020-10-12 03:28:26 +00:00
gr_set_fontcolor(canvas, dlg.keyboard_focus_gadget == &checkbox
2017-02-11 21:42:36 +00:00
? CRED
: CBLACK, -1);
2006-03-20 17:12:09 +00:00
2017-02-11 21:42:36 +00:00
unsigned offset;
2020-10-12 03:28:26 +00:00
if (checkbox.position == 0)
2006-03-20 17:12:09 +00:00
{
2020-10-12 03:28:26 +00:00
ui_draw_box_out(canvas, 0, 0, checkbox.width - 1, checkbox.height - 1);
2017-02-11 21:42:36 +00:00
offset = 0;
2006-03-20 17:12:09 +00:00
} else {
2020-10-12 03:28:26 +00:00
ui_draw_box_in(canvas, 0, 0, checkbox.width - 1, checkbox.height - 1);
2017-02-11 21:42:36 +00:00
offset = 1;
2006-03-20 17:12:09 +00:00
}
2020-10-12 03:28:26 +00:00
ui_string_centered(canvas, Middle(checkbox.width) + offset, Middle(checkbox.height) + offset, checkbox.flag ? "X" : " ");
gr_ustring(canvas, *canvas.cv_font, checkbox.width + 4, 2, checkbox.text.get());
2006-03-20 17:12:09 +00:00
}
}
2020-10-12 03:28:26 +00:00
}
2006-03-20 17:12:09 +00:00
std::unique_ptr<UI_GADGET_CHECKBOX> ui_add_gadget_checkbox(UI_DIALOG &dlg, short x, short y, short w, short h, short group, const char *const text)
2006-03-20 17:12:09 +00:00
{
auto checkbox = ui_gadget_add<UI_GADGET_CHECKBOX>(dlg, x, y, x + w - 1, y + h - 1);
auto ltext = strlen(text) + 1;
MALLOC(checkbox->text, char[], ltext + 4);
memcpy(checkbox->text.get(), text, ltext);
2006-03-20 17:12:09 +00:00
checkbox->width = w;
checkbox->height = h;
checkbox->position = 0;
checkbox->oldposition = 0;
checkbox->pressed = 0;
checkbox->flag = 0;
checkbox->group = group;
return checkbox;
}
window_event_result UI_GADGET_CHECKBOX::event_handler(UI_DIALOG &dlg, const d_event &event)
2006-03-20 17:12:09 +00:00
{
oldposition = position;
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
{
const auto OnMe = ui_mouse_on_gadget(*this);
if (B1_JUST_PRESSED && OnMe)
{
position = 1;
2014-08-06 02:10:49 +00:00
return window_event_result::handled;
}
else if (B1_JUST_RELEASED)
{
if ((position==1) && OnMe)
pressed = 1;
2006-03-20 17:12:09 +00:00
position = 0;
}
}
2006-03-20 17:12:09 +00:00
2014-10-04 21:47:13 +00:00
if (event.type == EVENT_KEY_COMMAND)
{
const auto key = event_key_get(event);
if (dlg.keyboard_focus_gadget == this && (key == KEY_SPACEBAR || key == KEY_ENTER))
{
position = 2;
2014-08-06 02:10:49 +00:00
return window_event_result::handled;
}
}
2014-10-04 21:47:13 +00:00
else if (event.type == EVENT_KEY_RELEASE)
{
const auto key = event_key_get(event);
position = 0;
if (dlg.keyboard_focus_gadget == this && (key == KEY_SPACEBAR || key == KEY_ENTER))
pressed = 1;
}
if (pressed == 1)
{
flag ^= 1;
auto rval = ui_gadget_send_event(dlg, EVENT_UI_GADGET_PRESSED, *this);
if (rval == window_event_result::ignored)
rval = window_event_result::handled;
return rval;
}
2006-03-20 17:12:09 +00:00
2014-10-04 21:47:13 +00:00
if (event.type == EVENT_WINDOW_DRAW)
2020-10-12 03:28:26 +00:00
ui_draw_checkbox(dlg, *this);
2006-03-20 17:12:09 +00:00
2014-08-06 02:10:49 +00:00
return window_event_result::ignored;
2006-03-20 17:12:09 +00:00
}
void ui_checkbox_check(UI_GADGET_CHECKBOX * checkbox, int check)
2006-03-20 17:12:09 +00:00
{
check = check != 0;
if (checkbox->flag == check)
return;
checkbox->flag = check;
checkbox->status = 1; // redraw
}
2015-12-05 22:57:24 +00:00
}