dxx-rebirth/common/ui/inputbox.cpp

170 lines
4.8 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 "key.h"
#include "mouse.h"
2006-03-20 17:12:09 +00:00
2015-12-13 18:00:49 +00:00
namespace dcx {
2015-12-05 22:57:24 +00:00
2012-03-03 12:19:15 +00:00
void ui_draw_inputbox( UI_DIALOG *dlg, UI_GADGET_INPUTBOX * inputbox )
2006-03-20 17:12:09 +00:00
{
#if 0 //ndef OGL
2006-03-20 17:12:09 +00:00
if ((inputbox->status==1) || (inputbox->position != inputbox->oldposition))
#endif
2006-03-20 17:12:09 +00:00
{
gr_set_current_canvas( inputbox->canvas );
2017-03-10 01:22:24 +00:00
auto &canvas = *grd_curcanv;
2006-03-20 17:12:09 +00:00
2017-03-10 01:22:24 +00:00
gr_rect(canvas, 0, 0, inputbox->width-1, inputbox->height-1, CBLACK);
2015-09-29 02:41:22 +00:00
int w, h;
2017-03-10 01:22:24 +00:00
gr_get_string_size(*canvas.cv_font, inputbox->text.get(), &w, &h, nullptr);
2014-10-04 22:13:47 +00:00
if (dlg->keyboard_focus_gadget == inputbox)
2006-03-20 17:12:09 +00:00
{
if (inputbox->first_time)
{
2017-03-10 01:22:24 +00:00
gr_set_fontcolor(canvas, CBLACK, -1);
gr_rect(canvas, 2, 2, 2 + w, 2 + h, CRED);
}
2006-03-20 17:12:09 +00:00
else
2017-03-10 01:22:24 +00:00
gr_set_fontcolor(canvas, CRED, -1);
2006-03-20 17:12:09 +00:00
}
else
2017-03-10 01:22:24 +00:00
gr_set_fontcolor(canvas, CWHITE, -1);
2006-03-20 17:12:09 +00:00
inputbox->status = 0;
2018-05-19 23:21:42 +00:00
gr_string(canvas, *canvas.cv_font, 2, 2, inputbox->text.get(), w, h);
2006-03-20 17:12:09 +00:00
2014-10-04 22:13:47 +00:00
if (dlg->keyboard_focus_gadget == inputbox && !inputbox->first_time )
2006-03-20 17:12:09 +00:00
{
2016-02-12 04:02:28 +00:00
const uint8_t cred = CRED;
2017-03-10 01:22:24 +00:00
Vline(canvas, 2, inputbox->height - 3, 3 + w, cred);
Vline(canvas, 2, inputbox->height - 3, 4 + w, cred);
2006-03-20 17:12:09 +00:00
}
}
}
std::unique_ptr<UI_GADGET_INPUTBOX> ui_add_gadget_inputbox(UI_DIALOG * dlg, short x, short y, short length, short slength, const char * text)
2006-03-20 17:12:09 +00:00
{
2015-09-29 02:41:22 +00:00
int h, aw;
2017-01-08 22:31:59 +00:00
gr_get_string_size(*grd_curcanv->cv_font, nullptr, nullptr, &h, &aw);
std::unique_ptr<UI_GADGET_INPUTBOX> inputbox{ui_gadget_add<UI_GADGET_INPUTBOX>(dlg, x, y, x+aw*slength-1, y+h-1+4)};
MALLOC(inputbox->text, char[], length + 1);
auto ltext = strlen(text) + 1;
memcpy(inputbox->text.get(), text, ltext);
inputbox->position = ltext - 1;
2006-03-20 17:12:09 +00:00
inputbox->oldposition = inputbox->position;
inputbox->width = aw*slength;
inputbox->height = h+4;
inputbox->length = length;
inputbox->slength = slength;
inputbox->pressed = 0;
inputbox->first_time = 1;
return inputbox;
}
2014-10-04 21:47:13 +00:00
window_event_result ui_inputbox_do( UI_DIALOG *dlg, UI_GADGET_INPUTBOX * inputbox,const d_event &event )
2006-03-20 17:12:09 +00:00
{
unsigned char ascii;
int keypress = 0;
2014-10-04 21:47:13 +00:00
if (event.type == EVENT_KEY_COMMAND)
keypress = event_key_get(event);
2006-03-20 17:12:09 +00:00
inputbox->oldposition = inputbox->position;
2006-03-20 17:12:09 +00:00
inputbox->pressed=0;
2014-08-06 02:10:49 +00:00
window_event_result rval = window_event_result::ignored;
2014-10-04 22:13:47 +00:00
if (dlg->keyboard_focus_gadget==inputbox)
2006-03-20 17:12:09 +00:00
{
switch( keypress )
{
case 0:
break;
case (KEY_LEFT):
case (KEY_BACKSP):
if (inputbox->position > 0)
inputbox->position--;
inputbox->text[inputbox->position] = 0;
inputbox->status = 1;
if (inputbox->first_time) inputbox->first_time = 0;
2014-08-06 02:10:49 +00:00
rval = window_event_result::handled;
2006-03-20 17:12:09 +00:00
break;
case (KEY_ENTER):
inputbox->pressed=1;
inputbox->status = 1;
if (inputbox->first_time) inputbox->first_time = 0;
2014-08-06 02:10:49 +00:00
rval = window_event_result::handled;
2006-03-20 17:12:09 +00:00
break;
default:
ascii = key_ascii();
2006-03-20 17:12:09 +00:00
if ((ascii < 255 ) && (inputbox->position < inputbox->length-2))
{
if (inputbox->first_time) {
inputbox->first_time = 0;
inputbox->position = 0;
}
inputbox->text[inputbox->position++] = ascii;
inputbox->text[inputbox->position] = 0;
2014-08-06 02:10:49 +00:00
rval = window_event_result::handled;
2006-03-20 17:12:09 +00:00
}
inputbox->status = 1;
break;
}
} else {
inputbox->first_time = 1;
}
2012-03-03 12:19:15 +00:00
if (inputbox->pressed)
{
rval = ui_gadget_send_event(dlg, EVENT_UI_GADGET_PRESSED, inputbox);
if (rval == window_event_result::ignored)
rval = window_event_result::handled;
2012-03-03 12:19:15 +00:00
}
2014-10-04 21:47:13 +00:00
if (event.type == EVENT_WINDOW_DRAW)
ui_draw_inputbox( dlg, inputbox );
2006-03-20 17:12:09 +00:00
return rval;
2006-03-20 17:12:09 +00:00
}
void ui_inputbox_set_text(UI_GADGET_INPUTBOX *inputbox, const char *text)
2006-03-20 17:12:09 +00:00
{
auto ltext = std::min(static_cast<std::size_t>(inputbox->length), strlen(text));
memcpy(inputbox->text.get(), text, ltext);
inputbox->text[ltext] = 0;
inputbox->position = ltext;
2006-03-20 17:12:09 +00:00
inputbox->oldposition = inputbox->position;
inputbox->status = 1; // redraw
inputbox->first_time = 1; // select all
}
2015-12-05 22:57:24 +00:00
}