dxx-rebirth/common/ui/scroll.cpp

290 lines
6.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>
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
const auto color = (dlg->keyboard_focus_gadget == scrollbar)
2016-02-12 04:02:28 +00:00
? 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;
auto 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, nullptr);
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, nullptr);
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;
}
window_event_result UI_GADGET_SCROLLBAR::event_handler(UI_DIALOG &dlg, const d_event &event)
2006-03-20 17:12:09 +00:00
{
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, this);
2014-08-06 02:10:49 +00:00
return window_event_result::ignored;
}
const auto keyfocus = (dlg.keyboard_focus_gadget == this);
2006-03-20 17:12:09 +00:00
if (start==stop)
2006-03-20 17:12:09 +00:00
{
position = 0;
fake_position = 0;
ui_draw_scrollbar(&dlg, this);
2014-08-06 02:10:49 +00:00
return window_event_result::ignored;
2006-03-20 17:12:09 +00:00
}
const auto op = position;
const auto oldpos = fake_position;
2006-03-20 17:12:09 +00:00
moved = 0;
2006-03-20 17:12:09 +00:00
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)
{
up_button->position = 2;
2014-08-06 02:10:49 +00:00
rval = window_event_result::handled;
}
else if (key & KEY_DOWN)
{
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)
{
up_button->position = 0;
2014-08-06 02:10:49 +00:00
rval = window_event_result::handled;
}
else if (key & KEY_DOWN)
{
down_button->position = 0;
2014-08-06 02:10:49 +00:00
rval = window_event_result::handled;
}
}
if (up_button->position!=0)
2006-03-20 17:12:09 +00:00
{
if (timer_query() > last_scrolled + 1)
2006-03-20 17:12:09 +00:00
{
last_scrolled = timer_query();
position--;
if (position < start )
position = start;
fake_position = position-start;
fake_position *= height-fake_size;
fake_position /= (stop-start);
2006-03-20 17:12:09 +00:00
}
}
if (down_button->position!=0)
2006-03-20 17:12:09 +00:00
{
if (timer_query() > last_scrolled + 1)
2006-03-20 17:12:09 +00:00
{
last_scrolled = timer_query();
position++;
if (position > stop )
position = stop;
fake_position = position-start;
fake_position *= height-fake_size;
fake_position /= (stop-start);
2006-03-20 17:12:09 +00:00
}
}
const auto OnMe = ui_mouse_on_gadget(*this);
2006-03-20 17:12:09 +00:00
//gr_ubox(0, fake_position, width-1, fake_position+fake_size-1 );
2006-03-20 17:12:09 +00:00
if (B1_JUST_RELEASED)
dragging = 0;
2006-03-20 17:12:09 +00:00
//if (B1_PRESSED && OnMe )
// listbox->dragging = 1;
mouse_get_pos(&x, &y, &z);
const auto OnSlider = y >= fake_position + y1 &&
y < fake_position + y1 + fake_size &&
OnMe;
2006-03-20 17:12:09 +00:00
if (B1_JUST_PRESSED && OnSlider )
{
dragging = 1;
drag_x = x;
drag_y = y;
drag_starting = fake_position;
2014-08-06 02:10:49 +00:00
rval = window_event_result::handled;
}
else if (B1_JUST_PRESSED && OnMe)
{
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 ((dragging == 2) && OnMe && !OnSlider && (timer_query() > last_scrolled + 4))
2006-03-20 17:12:09 +00:00
{
last_scrolled = timer_query();
2006-03-20 17:12:09 +00:00
if ( y < fake_position+y1 )
2006-03-20 17:12:09 +00:00
{
// Page Up
position -= window_size;
if (position < start )
position = start;
2006-03-20 17:12:09 +00:00
} else {
// Page Down
position += window_size;
if (position > stop )
position = stop;
2006-03-20 17:12:09 +00:00
}
fake_position = position-start;
fake_position *= height-fake_size;
fake_position /= (stop-start);
2006-03-20 17:12:09 +00:00
}
if (selected_gadget == this && dragging == 1)
2006-03-20 17:12:09 +00:00
{
//x = drag_x;
fake_position = drag_starting + (y - drag_y );
if (fake_position<0)
2006-03-20 17:12:09 +00:00
{
fake_position = 0;
//y = fake_position + drag_y - drag_starting;
2006-03-20 17:12:09 +00:00
}
if (fake_position > (height-fake_size))
2006-03-20 17:12:09 +00:00
{
fake_position = (height-fake_size);
//y = fake_position + drag_y - drag_starting;
2006-03-20 17:12:09 +00:00
}
//mouse_set_pos( x, y );
2006-03-20 17:12:09 +00:00
position = fake_position;
position *= (stop-start);
position /= ( height-fake_size ) ;
position += start;
2006-03-20 17:12:09 +00:00
if (position > stop )
position = stop;
2006-03-20 17:12:09 +00:00
if (position < start )
position = start;
2006-03-20 17:12:09 +00:00
//fake_position = position-start;
//fake_position *= height-fake_size;
//fake_position /= (stop-start);
2006-03-20 17:12:09 +00:00
}
if (op != position )
moved = 1;
if (moved)
2012-03-03 12:19:15 +00:00
{
rval = ui_gadget_send_event(dlg, EVENT_UI_GADGET_PRESSED, *this);
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 != fake_position)
status = 1;
2006-03-20 17:12:09 +00:00
return rval;
2006-03-20 17:12:09 +00:00
}
2015-12-05 22:57:24 +00:00
}