dxx-rebirth/common/ui/scroll.cpp

297 lines
8.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>
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"
#include "timer.h"
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_scrollbar( UI_DIALOG *dlg, UI_GADGET_SCROLLBAR * scrollbar )
2006-03-20 17:12:09 +00:00
{
#if 0 //ndef OGL
2006-03-20 17:12:09 +00:00
if (scrollbar->status==0)
return;
#endif
2006-03-20 17:12:09 +00:00
scrollbar->status = 0;
gr_set_current_canvas( scrollbar->canvas );
2017-02-11 21:42:37 +00:00
auto &canvas = *grd_curcanv;
2006-03-20 17:12:09 +00:00
2016-02-12 04:02:28 +00:00
uint8_t color = (dlg->keyboard_focus_gadget == scrollbar)
? CRED
: CGREY;
2006-03-20 17:12:09 +00:00
2017-02-11 21:42:37 +00:00
gr_rect(canvas, 0, 0, scrollbar->width - 1, scrollbar->fake_position - 1, color);
gr_rect(canvas, 0, scrollbar->fake_position + scrollbar->fake_size, scrollbar->width - 1, scrollbar->height - 1, color);
ui_draw_box_out(canvas, 0, scrollbar->fake_position, scrollbar->width - 1, scrollbar->fake_position + scrollbar->fake_size - 1);
2006-03-20 17:12:09 +00:00
}
std::unique_ptr<UI_GADGET_SCROLLBAR> ui_add_gadget_scrollbar(UI_DIALOG * dlg, short x, short y, short w, short h, int start, int stop, int position, int window_size)
2006-03-20 17:12:09 +00:00
{
2015-09-29 02:41:22 +00:00
int tw;
2006-03-20 17:12:09 +00:00
auto &up = "\x1e";
auto &down = "\x1f";
2006-03-20 17:12:09 +00:00
2017-01-08 22:31:59 +00:00
gr_get_string_size(*grd_curcanv->cv_font, up, &tw, nullptr, nullptr);
2006-03-20 17:12:09 +00:00
w = tw + 10;
if (stop < start ) stop = start;
std::unique_ptr<UI_GADGET_SCROLLBAR> scrollbar{ui_gadget_add<UI_GADGET_SCROLLBAR>(dlg, x, y+w, x+w-1, y+h-w-1)};
2006-03-20 17:12:09 +00:00
scrollbar->up_button = ui_add_gadget_button( dlg, x, y, w, w, up, NULL );
scrollbar->up_button->parent = scrollbar.get();
2006-03-20 17:12:09 +00:00
scrollbar->down_button =ui_add_gadget_button( dlg, x, y+h-w, w, w, down, NULL );
scrollbar->down_button->parent = scrollbar.get();
2006-03-20 17:12:09 +00:00
scrollbar->horz = 0;
scrollbar->width = scrollbar->x2-scrollbar->x1+1;
scrollbar->height = scrollbar->y2-scrollbar->y1+1;
scrollbar->start = start;
scrollbar->stop = stop;
scrollbar->position = position;
scrollbar->window_size = window_size;
scrollbar->fake_length = scrollbar->height;
scrollbar->fake_position = 0;
if (stop!=start)
scrollbar->fake_size = (window_size * scrollbar->height)/(stop-start+1+window_size);
else
scrollbar->fake_size = scrollbar->height;
if (scrollbar->fake_size < 7) scrollbar->fake_size = 7;
scrollbar->dragging = 0;
scrollbar->moved=0;
scrollbar->last_scrolled = 0;
return scrollbar;
}
2014-10-04 21:47:13 +00:00
window_event_result ui_scrollbar_do( UI_DIALOG *dlg, UI_GADGET_SCROLLBAR * scrollbar,const d_event &event )
2006-03-20 17:12:09 +00:00
{
int OnMe, OnSlider, keyfocus;
int oldpos, op;
int x, y, z;
2014-08-06 02:10:49 +00:00
window_event_result rval = window_event_result::ignored;
2014-10-04 21:47:13 +00:00
if (event.type == EVENT_WINDOW_DRAW)
{
ui_draw_scrollbar( dlg, scrollbar );
2014-08-06 02:10:49 +00:00
return window_event_result::ignored;
}
2006-03-20 17:12:09 +00:00
keyfocus = 0;
2014-10-04 22:13:47 +00:00
if (dlg->keyboard_focus_gadget==scrollbar)
2006-03-20 17:12:09 +00:00
keyfocus = 1;
if (scrollbar->start==scrollbar->stop)
{
scrollbar->position = 0;
scrollbar->fake_position = 0;
2012-03-03 12:19:15 +00:00
ui_draw_scrollbar( dlg, scrollbar );
2014-08-06 02:10:49 +00:00
return window_event_result::ignored;
2006-03-20 17:12:09 +00:00
}
op = scrollbar->position;
oldpos = scrollbar->fake_position;
scrollbar->moved = 0;
2014-10-04 21:47:13 +00:00
if (keyfocus && event.type == EVENT_KEY_COMMAND)
{
int key;
key = event_key_get(event);
if (key & KEY_UP)
{
scrollbar->up_button->position = 2;
2014-08-06 02:10:49 +00:00
rval = window_event_result::handled;
}
else if (key & KEY_DOWN)
{
scrollbar->down_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 (keyfocus && event.type == EVENT_KEY_RELEASE)
{
int key;
key = event_key_get(event);
if (key & KEY_UP)
{
scrollbar->up_button->position = 0;
2014-08-06 02:10:49 +00:00
rval = window_event_result::handled;
}
else if (key & KEY_DOWN)
{
scrollbar->down_button->position = 0;
2014-08-06 02:10:49 +00:00
rval = window_event_result::handled;
}
}
if (scrollbar->up_button->position!=0)
2006-03-20 17:12:09 +00:00
{
if (timer_query() > scrollbar->last_scrolled + 1)
2006-03-20 17:12:09 +00:00
{
scrollbar->last_scrolled = timer_query();
2006-03-20 17:12:09 +00:00
scrollbar->position--;
if (scrollbar->position < scrollbar->start )
scrollbar->position = scrollbar->start;
scrollbar->fake_position = scrollbar->position-scrollbar->start;
scrollbar->fake_position *= scrollbar->height-scrollbar->fake_size;
scrollbar->fake_position /= (scrollbar->stop-scrollbar->start);
}
}
if (scrollbar->down_button->position!=0)
2006-03-20 17:12:09 +00:00
{
if (timer_query() > scrollbar->last_scrolled + 1)
2006-03-20 17:12:09 +00:00
{
scrollbar->last_scrolled = timer_query();
2006-03-20 17:12:09 +00:00
scrollbar->position++;
if (scrollbar->position > scrollbar->stop )
scrollbar->position = scrollbar->stop;
scrollbar->fake_position = scrollbar->position-scrollbar->start;
scrollbar->fake_position *= scrollbar->height-scrollbar->fake_size;
scrollbar->fake_position /= (scrollbar->stop-scrollbar->start);
}
}
2014-10-04 22:13:47 +00:00
OnMe = ui_mouse_on_gadget( scrollbar );
2006-03-20 17:12:09 +00:00
//gr_ubox(0, scrollbar->fake_position, scrollbar->width-1, scrollbar->fake_position+scrollbar->fake_size-1 );
if (B1_JUST_RELEASED)
2006-03-20 17:12:09 +00:00
scrollbar->dragging = 0;
//if (B1_PRESSED && OnMe )
// listbox->dragging = 1;
mouse_get_pos(&x, &y, &z);
2006-03-20 17:12:09 +00:00
OnSlider = 0;
if ((y >= scrollbar->fake_position+scrollbar->y1) && \
(y < scrollbar->fake_position+scrollbar->y1+scrollbar->fake_size) && OnMe )
2006-03-20 17:12:09 +00:00
OnSlider = 1;
if (B1_JUST_PRESSED && OnSlider )
{
scrollbar->dragging = 1;
scrollbar->drag_x = x;
scrollbar->drag_y = y;
2006-03-20 17:12:09 +00:00
scrollbar->drag_starting = scrollbar->fake_position;
2014-08-06 02:10:49 +00:00
rval = window_event_result::handled;
}
else if (B1_JUST_PRESSED && OnMe)
{
scrollbar->dragging = 2; // outside the slider
2014-08-06 02:10:49 +00:00
rval = window_event_result::handled;
2006-03-20 17:12:09 +00:00
}
if ((scrollbar->dragging == 2) && OnMe && !OnSlider && (timer_query() > scrollbar->last_scrolled + 4))
2006-03-20 17:12:09 +00:00
{
scrollbar->last_scrolled = timer_query();
2006-03-20 17:12:09 +00:00
if ( y < scrollbar->fake_position+scrollbar->y1 )
2006-03-20 17:12:09 +00:00
{
// Page Up
scrollbar->position -= scrollbar->window_size;
if (scrollbar->position < scrollbar->start )
scrollbar->position = scrollbar->start;
} else {
// Page Down
scrollbar->position += scrollbar->window_size;
if (scrollbar->position > scrollbar->stop )
scrollbar->position = scrollbar->stop;
}
scrollbar->fake_position = scrollbar->position-scrollbar->start;
scrollbar->fake_position *= scrollbar->height-scrollbar->fake_size;
scrollbar->fake_position /= (scrollbar->stop-scrollbar->start);
}
2014-10-04 22:13:47 +00:00
if ((selected_gadget==scrollbar) && (scrollbar->dragging == 1))
2006-03-20 17:12:09 +00:00
{
//x = scrollbar->drag_x;
scrollbar->fake_position = scrollbar->drag_starting + (y - scrollbar->drag_y );
2006-03-20 17:12:09 +00:00
if (scrollbar->fake_position<0)
{
scrollbar->fake_position = 0;
//y = scrollbar->fake_position + scrollbar->drag_y - scrollbar->drag_starting;
2006-03-20 17:12:09 +00:00
}
if (scrollbar->fake_position > (scrollbar->height-scrollbar->fake_size))
{
scrollbar->fake_position = (scrollbar->height-scrollbar->fake_size);
//y = scrollbar->fake_position + scrollbar->drag_y - scrollbar->drag_starting;
2006-03-20 17:12:09 +00:00
}
//mouse_set_pos( x, y );
2006-03-20 17:12:09 +00:00
scrollbar->position = scrollbar->fake_position;
scrollbar->position *= (scrollbar->stop-scrollbar->start);
scrollbar->position /= ( scrollbar->height-scrollbar->fake_size ) ;
scrollbar->position += scrollbar->start;
if (scrollbar->position > scrollbar->stop )
scrollbar->position = scrollbar->stop;
if (scrollbar->position < scrollbar->start )
scrollbar->position = scrollbar->start;
//scrollbar->fake_position = scrollbar->position-scrollbar->start;
//scrollbar->fake_position *= scrollbar->height-scrollbar->fake_size;
//scrollbar->fake_position /= (scrollbar->stop-scrollbar->start);
}
if (op != scrollbar->position )
scrollbar->moved = 1;
2012-03-03 12:19:15 +00:00
if (scrollbar->moved)
{
rval = ui_gadget_send_event(dlg, EVENT_UI_GADGET_PRESSED, scrollbar);
if (rval == window_event_result::ignored)
rval = window_event_result::handled;
2012-03-03 12:19:15 +00:00
}
2006-03-20 17:12:09 +00:00
if (oldpos != scrollbar->fake_position)
scrollbar->status = 1;
return rval;
2006-03-20 17:12:09 +00:00
}
2015-12-05 22:57:24 +00:00
}