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.
|
|
|
|
*/
|
|
|
|
|
2014-09-06 23:27:57 +00:00
|
|
|
#include <stdexcept>
|
2006-03-20 17:12:09 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.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 "gr.h"
|
|
|
|
#include "ui.h"
|
2011-10-23 09:08:47 +00:00
|
|
|
#include "event.h"
|
2011-10-25 01:39:35 +00:00
|
|
|
#include "mouse.h"
|
2011-12-18 12:41:31 +00:00
|
|
|
#include "window.h"
|
2012-07-07 18:35:06 +00:00
|
|
|
#include "dxxerror.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
|
|
|
UI_GADGET * selected_gadget;
|
|
|
|
|
2017-06-25 20:46:03 +00:00
|
|
|
constexpr std::integral_constant<uint8_t, 1> UI_GADGET_BUTTON::s_kind;
|
|
|
|
constexpr std::integral_constant<uint8_t, 2> UI_GADGET_LISTBOX::s_kind;
|
|
|
|
constexpr std::integral_constant<uint8_t, 3> UI_GADGET_SCROLLBAR::s_kind;
|
|
|
|
constexpr std::integral_constant<uint8_t, 4> UI_GADGET_RADIO::s_kind;
|
|
|
|
constexpr std::integral_constant<uint8_t, 5> UI_GADGET_CHECKBOX::s_kind;
|
|
|
|
constexpr std::integral_constant<uint8_t, 6> UI_GADGET_INPUTBOX::s_kind;
|
|
|
|
constexpr std::integral_constant<uint8_t, 7> UI_GADGET_USERBOX::s_kind;
|
|
|
|
constexpr std::integral_constant<uint8_t, 9> UI_GADGET_ICON::s_kind;
|
2015-06-11 03:06:58 +00:00
|
|
|
|
2015-08-12 03:11:46 +00:00
|
|
|
namespace {
|
|
|
|
|
2014-10-04 18:05:26 +00:00
|
|
|
struct event_gadget : d_event
|
2012-03-03 12:19:15 +00:00
|
|
|
{
|
2020-10-12 03:28:25 +00:00
|
|
|
UI_GADGET &gadget;
|
|
|
|
constexpr event_gadget(const event_type t, UI_GADGET &g) :
|
2018-05-12 18:24:19 +00:00
|
|
|
d_event(t), gadget(g)
|
|
|
|
{
|
|
|
|
}
|
2013-12-22 22:03:07 +00:00
|
|
|
};
|
2012-03-03 12:19:15 +00:00
|
|
|
|
2020-10-12 03:28:26 +00:00
|
|
|
template <UI_GADGET *UI_GADGET::*step>
|
|
|
|
UI_GADGET &ui_gadget_get_step(UI_GADGET &gadget)
|
|
|
|
{
|
|
|
|
auto tmp = gadget.*step;
|
|
|
|
while (tmp != &gadget && tmp->parent)
|
|
|
|
tmp = tmp->*step;
|
|
|
|
return *tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
UI_GADGET &ui_gadget_get_prev(UI_GADGET &gadget)
|
|
|
|
{
|
|
|
|
return ui_gadget_get_step<&UI_GADGET::prev>(gadget);
|
|
|
|
}
|
|
|
|
|
2015-08-12 03:11:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 03:28:25 +00:00
|
|
|
void ui_gadget_add(UI_DIALOG &dlg, short x1, short y1, short x2, short y2, UI_GADGET &gadget)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2020-10-12 03:28:25 +00:00
|
|
|
if (dlg.gadget == NULL)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2020-10-12 03:28:25 +00:00
|
|
|
dlg.gadget = &gadget;
|
|
|
|
gadget.prev = &gadget;
|
|
|
|
gadget.next = &gadget;
|
2006-03-20 17:12:09 +00:00
|
|
|
} else {
|
2020-10-12 03:28:25 +00:00
|
|
|
dlg.gadget->prev->next = &gadget;
|
|
|
|
gadget.next = dlg.gadget;
|
|
|
|
gadget.prev = dlg.gadget->prev;
|
|
|
|
dlg.gadget->prev = &gadget;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 03:28:25 +00:00
|
|
|
gadget.when_tab = nullptr;
|
|
|
|
gadget.when_btab = nullptr;
|
|
|
|
gadget.when_up = nullptr;
|
|
|
|
gadget.when_down = nullptr;
|
|
|
|
gadget.when_left = nullptr;
|
|
|
|
gadget.when_right = nullptr;
|
|
|
|
gadget.status = 1;
|
|
|
|
gadget.oldstatus = 0;
|
|
|
|
if (x1 == 0 && x2 == 0 && y1 == 0 && y2 == 0)
|
|
|
|
gadget.canvas.reset();
|
2006-03-20 17:12:09 +00:00
|
|
|
else
|
2020-10-12 03:28:25 +00:00
|
|
|
gadget.canvas = gr_create_sub_canvas(dlg.w_canv, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
|
|
|
|
gadget.x1 = gadget.canvas->cv_bitmap.bm_x;
|
|
|
|
gadget.y1 = gadget.canvas->cv_bitmap.bm_y;
|
|
|
|
gadget.x2 = gadget.canvas->cv_bitmap.bm_x + x2 - x1 + 1;
|
|
|
|
gadget.y2 = gadget.canvas->cv_bitmap.bm_y + y2 - y1 + 1;
|
|
|
|
gadget.parent = nullptr;
|
|
|
|
gadget.hotkey = -1;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-18 12:41:31 +00:00
|
|
|
#if 0
|
2011-01-17 14:42:18 +00:00
|
|
|
int is_under_another_window( UI_DIALOG * dlg, UI_GADGET * gadget )
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2011-01-17 14:42:18 +00:00
|
|
|
UI_DIALOG * temp;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2011-01-17 14:42:18 +00:00
|
|
|
temp = dlg->next;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
while( temp != NULL ) {
|
|
|
|
if ( ( gadget->x1 > temp->x) &&
|
|
|
|
( gadget->x1 < (temp->x+temp->width) ) &&
|
|
|
|
( gadget->y1 > temp->y) &&
|
|
|
|
( gadget->y1 < (temp->y+temp->height) )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
//gadget->status =1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( ( gadget->x2 > temp->x) &&
|
|
|
|
( gadget->x2 < (temp->x+temp->width) ) &&
|
|
|
|
( gadget->y2 > temp->y) &&
|
|
|
|
( gadget->y2 < (temp->y+temp->height) )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
//gadget->status =1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
temp = temp->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2011-10-23 09:08:47 +00:00
|
|
|
#endif
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
int ui_mouse_on_gadget( UI_GADGET * gadget )
|
|
|
|
{
|
2011-10-23 09:08:47 +00:00
|
|
|
int x, y, z;
|
|
|
|
|
|
|
|
mouse_get_pos(&x, &y, &z);
|
|
|
|
if ((x >= gadget->x1) && (x <= gadget->x2-1) && (y >= gadget->y1) && (y <= gadget->y2-1) )
|
|
|
|
{
|
2011-12-18 12:41:31 +00:00
|
|
|
#if 0 // check is no longer required - if it is under another window, that dialog's handler would have returned 1
|
2012-03-03 12:19:15 +00:00
|
|
|
if (is_under_another_window(dlg, gadget))
|
2006-03-20 17:12:09 +00:00
|
|
|
return 0;
|
2011-10-23 09:08:47 +00:00
|
|
|
#endif
|
2006-03-20 17:12:09 +00:00
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-04 21:47:13 +00:00
|
|
|
static window_event_result ui_gadget_do(UI_DIALOG *dlg, UI_GADGET *g,const d_event &event)
|
2012-03-03 12:19:15 +00:00
|
|
|
{
|
|
|
|
switch( g->kind )
|
|
|
|
{
|
2014-09-06 23:27:57 +00:00
|
|
|
case UI_GADGET_BUTTON::s_kind:
|
2016-07-06 01:54:25 +00:00
|
|
|
return ui_button_do(dlg, static_cast<UI_GADGET_BUTTON *>(g), event);
|
2014-09-06 23:27:57 +00:00
|
|
|
case UI_GADGET_LISTBOX::s_kind:
|
2016-07-06 01:54:25 +00:00
|
|
|
return ui_listbox_do(dlg, static_cast<UI_GADGET_LISTBOX *>(g), event);
|
2014-09-06 23:27:57 +00:00
|
|
|
case UI_GADGET_SCROLLBAR::s_kind:
|
2016-07-06 01:54:25 +00:00
|
|
|
return ui_scrollbar_do(dlg, static_cast<UI_GADGET_SCROLLBAR *>(g), event);
|
2014-09-06 23:27:57 +00:00
|
|
|
case UI_GADGET_RADIO::s_kind:
|
2016-07-06 01:54:25 +00:00
|
|
|
return ui_radio_do(dlg, static_cast<UI_GADGET_RADIO *>(g), event);
|
2014-09-06 23:27:57 +00:00
|
|
|
case UI_GADGET_CHECKBOX::s_kind:
|
2016-07-06 01:54:25 +00:00
|
|
|
return ui_checkbox_do(dlg, static_cast<UI_GADGET_CHECKBOX *>(g), event);
|
2014-09-06 23:27:57 +00:00
|
|
|
case UI_GADGET_INPUTBOX::s_kind:
|
2016-07-06 01:54:25 +00:00
|
|
|
return ui_inputbox_do(dlg, static_cast<UI_GADGET_INPUTBOX *>(g), event);
|
2014-09-06 23:27:57 +00:00
|
|
|
case UI_GADGET_USERBOX::s_kind:
|
2016-07-06 01:54:25 +00:00
|
|
|
return ui_userbox_do(dlg, static_cast<UI_GADGET_USERBOX *>(g), event);
|
2014-09-06 23:27:57 +00:00
|
|
|
case UI_GADGET_ICON::s_kind:
|
2016-07-06 01:54:25 +00:00
|
|
|
return ui_icon_do(dlg, static_cast<UI_GADGET_ICON *>(g), event);
|
2012-03-03 12:19:15 +00:00
|
|
|
}
|
2014-08-06 02:10:49 +00:00
|
|
|
return window_event_result::ignored;
|
2012-03-03 12:19:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 03:28:25 +00:00
|
|
|
window_event_result ui_gadget_send_event(UI_DIALOG &dlg, const event_type type, UI_GADGET &gadget)
|
2012-03-03 12:19:15 +00:00
|
|
|
{
|
2018-05-12 18:24:19 +00:00
|
|
|
const event_gadget event{type, gadget};
|
|
|
|
|
2020-10-12 03:28:25 +00:00
|
|
|
if (const auto parent = gadget.parent)
|
|
|
|
return ui_gadget_do(&dlg, parent, event);
|
2012-03-03 12:19:15 +00:00
|
|
|
|
2020-10-12 03:28:25 +00:00
|
|
|
return window_send_event(dlg, event);
|
2012-03-03 12:19:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 03:28:25 +00:00
|
|
|
UI_GADGET &ui_event_get_gadget(const d_event &event)
|
2012-03-03 12:19:15 +00:00
|
|
|
{
|
2014-10-04 21:47:13 +00:00
|
|
|
auto &e = static_cast<const event_gadget &>(event);
|
|
|
|
Assert(e.type >= EVENT_UI_GADGET_PRESSED); // Any UI event
|
|
|
|
return e.gadget;
|
2012-03-03 12:19:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 03:28:25 +00:00
|
|
|
window_event_result ui_dialog_do_gadgets(UI_DIALOG &dlg, const d_event &event)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2011-10-23 09:08:47 +00:00
|
|
|
int keypress = 0;
|
2006-03-20 17:12:09 +00:00
|
|
|
UI_GADGET * tmp, * tmp1;
|
|
|
|
|
2014-10-04 21:47:13 +00:00
|
|
|
if (event.type == EVENT_KEY_COMMAND)
|
2011-10-23 09:08:47 +00:00
|
|
|
keypress = event_key_get(event);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2020-10-12 03:28:25 +00:00
|
|
|
tmp = dlg.gadget;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2014-08-06 02:10:49 +00:00
|
|
|
if (tmp == NULL) return window_event_result::ignored;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
if (selected_gadget==NULL)
|
|
|
|
selected_gadget = tmp;
|
|
|
|
|
2020-10-12 03:28:25 +00:00
|
|
|
tmp1 = dlg.keyboard_focus_gadget;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (ui_mouse_on_gadget(tmp) && B1_JUST_PRESSED )
|
|
|
|
{
|
|
|
|
selected_gadget = tmp;
|
|
|
|
if (tmp->parent!=NULL)
|
|
|
|
{
|
|
|
|
while (tmp->parent != NULL )
|
|
|
|
tmp = tmp->parent;
|
|
|
|
}
|
2020-10-12 03:28:25 +00:00
|
|
|
dlg.keyboard_focus_gadget = tmp;
|
|
|
|
break;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
if ( tmp->hotkey == keypress )
|
|
|
|
{
|
2020-10-12 03:28:25 +00:00
|
|
|
dlg.keyboard_focus_gadget = tmp;
|
2006-03-20 17:12:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmp = tmp->next;
|
2020-10-12 03:28:25 +00:00
|
|
|
} while(tmp != dlg.gadget);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2020-10-12 03:28:25 +00:00
|
|
|
if (dlg.keyboard_focus_gadget)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2020-10-12 03:28:25 +00:00
|
|
|
[&]{
|
2017-03-10 01:22:24 +00:00
|
|
|
UI_GADGET *UI_GADGET::*when;
|
2006-03-20 17:12:09 +00:00
|
|
|
switch (keypress )
|
|
|
|
{
|
|
|
|
case (KEY_TAB):
|
2017-03-10 01:22:24 +00:00
|
|
|
when = &UI_GADGET::when_tab;
|
2006-03-20 17:12:09 +00:00
|
|
|
break;
|
|
|
|
case (KEY_TAB+KEY_SHIFTED):
|
2017-03-10 01:22:24 +00:00
|
|
|
when = &UI_GADGET::when_btab;
|
2006-03-20 17:12:09 +00:00
|
|
|
break;
|
|
|
|
case (KEY_UP):
|
2017-03-10 01:22:24 +00:00
|
|
|
when = &UI_GADGET::when_up;
|
2006-03-20 17:12:09 +00:00
|
|
|
break;
|
|
|
|
case (KEY_DOWN):
|
2017-03-10 01:22:24 +00:00
|
|
|
when = &UI_GADGET::when_down;
|
2006-03-20 17:12:09 +00:00
|
|
|
break;
|
|
|
|
case (KEY_LEFT):
|
2017-03-10 01:22:24 +00:00
|
|
|
when = &UI_GADGET::when_left;
|
2006-03-20 17:12:09 +00:00
|
|
|
break;
|
|
|
|
case (KEY_RIGHT):
|
2017-03-10 01:22:24 +00:00
|
|
|
when = &UI_GADGET::when_right;
|
2006-03-20 17:12:09 +00:00
|
|
|
break;
|
2017-03-10 01:22:24 +00:00
|
|
|
default:
|
|
|
|
return;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
2020-10-12 03:28:25 +00:00
|
|
|
if (const auto p = dlg.keyboard_focus_gadget->*when)
|
|
|
|
dlg.keyboard_focus_gadget = p;
|
2017-03-10 01:22:24 +00:00
|
|
|
}();
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-06 02:10:49 +00:00
|
|
|
window_event_result rval = window_event_result::ignored;
|
2020-10-12 03:28:25 +00:00
|
|
|
if (dlg.keyboard_focus_gadget != tmp1)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2020-10-12 03:28:25 +00:00
|
|
|
if (dlg.keyboard_focus_gadget)
|
|
|
|
dlg.keyboard_focus_gadget->status = 1;
|
2006-03-20 17:12:09 +00:00
|
|
|
if (tmp1 != NULL )
|
|
|
|
tmp1->status = 1;
|
2014-08-06 02:10:49 +00:00
|
|
|
rval = window_event_result::handled;
|
2011-11-05 09:08:17 +00:00
|
|
|
|
|
|
|
if (keypress)
|
|
|
|
return rval;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 03:28:25 +00:00
|
|
|
tmp = dlg.gadget;
|
2006-03-20 17:12:09 +00:00
|
|
|
do
|
|
|
|
{
|
2011-12-18 12:41:31 +00:00
|
|
|
// If it is under another dialog, that dialog's handler would have returned 1 for mouse events.
|
|
|
|
// Key events are handled in a priority depending on the window ordering.
|
2012-03-03 12:19:15 +00:00
|
|
|
//if (!is_under_another_window( dlg, tmp ))
|
2020-10-12 03:28:25 +00:00
|
|
|
rval = ui_gadget_do(&dlg, tmp, event);
|
2012-03-17 09:39:24 +00:00
|
|
|
|
2016-10-28 00:40:50 +00:00
|
|
|
if (rval == window_event_result::deleted)
|
2012-03-17 09:39:24 +00:00
|
|
|
break;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
tmp = tmp->next;
|
2020-10-12 03:28:25 +00:00
|
|
|
} while(/*!rval &&*/ tmp != dlg.gadget); // have to look for pesky scrollbars in case an arrow button or arrow key are held down
|
2011-10-23 09:08:47 +00:00
|
|
|
|
|
|
|
return rval;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 03:28:26 +00:00
|
|
|
UI_GADGET &ui_gadget_get_next(UI_GADGET &gadget)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2020-10-12 03:28:26 +00:00
|
|
|
return ui_gadget_get_step<&UI_GADGET::next>(gadget);
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 03:28:26 +00:00
|
|
|
void ui_gadget_calc_keys(UI_DIALOG &dlg)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2020-10-12 03:28:26 +00:00
|
|
|
auto tmp = dlg.gadget;
|
2006-03-20 17:12:09 +00:00
|
|
|
if (tmp==NULL) return;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2020-10-12 03:28:26 +00:00
|
|
|
tmp->when_tab = &ui_gadget_get_next(*tmp);
|
|
|
|
tmp->when_btab = &ui_gadget_get_prev(*tmp);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
tmp = tmp->next;
|
2020-10-12 03:28:26 +00:00
|
|
|
} while(tmp != dlg.gadget);
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
2015-12-05 22:57:24 +00:00
|
|
|
|
|
|
|
}
|