Select Git revision
listbox.cpp
listbox.cpp 3.03 KiB
#include "listbox.hpp"
#include "widget.hpp"
#include "graphics.hpp"
using namespace genv;
using namespace std;
ListBox::ListBox(Application* parent,int x, int y, int sizex, int sizey, vector<string> s) : Widget(parent,x,y,sizex,sizey), _s(s), _scroll(0), displaynum(0), _selected(-1)
{
}
void ListBox::draw()
{
gout.load_font("LiberationSans-Regular.ttf",20);
int height = gout.cascent()+gout.cdescent()+2;
int displaynum = _sizey/height;
if(displaynum > _s.size())
{
displaynum = _s.size();
}
_sizey = displaynum * height;
int ypos = 0;
gout << move_to(_x, _y) << color(200,200,200) << box (_sizex+2, displaynum*height);
for (int i = _scroll; i < _scroll + displaynum and i < _s.size(); ++i)
{
if(i != _selected){
{
gout << move_to(_x+1, _y+ypos) << color(0,0,0) << box (_sizex, height) << move_to(_x+2, _y+ypos) << color(255,255,255) << text(_s[i]) << move_to(_x, _y+ypos)
<< color(200,200,200) << line(_sizex, 0);
}
}
if(i == _selected){
{
gout << move_to(_x+1, _y+ypos) << color(0,0,0) << box (_sizex, height) << move_to(_x+2, _y+ypos) << color(255,0,255) << text(_s[i]) << move_to(_x, _y+ypos)
<< color(200,200,200) << line(_sizex, 0);
}
//ypos += height;
}
ypos += height;
}
gout << move_to(_x, _y + ypos) << color(200,200,200) << line(_sizex, 0) << refresh;
}
void ListBox::handle(event ev)
{
gout.load_font("LiberationSans-Regular.ttf",20);
int height = gout.cascent()+gout.cdescent()+2;
int displaynum = _sizey/height;
int max_scroll = _s.size()-displaynum;
if(ev.type == ev_mouse)
{
if(isover(ev.pos_x, ev.pos_y) and (ev.button == btn_wheeldown or ev.button == btn_middle))
{
if(_scroll < max_scroll)
{
_scroll++;
}
}
if(isover(ev.pos_x, ev.pos_y) and (ev.button == btn_wheelup or ev.button == btn_right))
{
if (_scroll > 0)
{
_scroll--;
}
}
for (int i = 0; i < displaynum; ++i)
{
if(i == 0)
{
if(ev.pos_x > _x and ev.pos_x < _x + _sizex and ev.pos_y < height+_y and ev.pos_y > _y and ev.button == btn_left )
{
_selected = i + _scroll;
}
}
if(i > 0){
if(ev.pos_x > _x and ev.pos_x < _x + _sizex and ev.pos_y < _y+i*2*height and ev.pos_y > _y+i*height and ev.button == btn_left )
{
_selected = i + _scroll;
}
}
}
if(!isover(ev.pos_x,ev.pos_y) and ev.button == btn_left)
{
_selected = -1;
}
}
}
bool ListBox::isover(int ms_x, int ms_y)
{
return (ms_x >= _x) and (ms_x <= _x + _sizex) and (ms_y >= _y) and (ms_y <= _y + _sizey);
}
string ListBox::getter()
{
return _s[_selected];
}