allow disabling scroll bars in ListWidget

This commit is contained in:
thrust26 2020-10-15 09:40:52 +02:00
parent 8219e607e4
commit 6dbc6676be
7 changed files with 71 additions and 34 deletions

View File

@ -69,11 +69,16 @@ int CartDebugWidget::addBaseInformation(size_t bytes, const string& manufacturer
const StringList& sl = bs.stringList();
uInt32 lines = uInt32(sl.size());
if(lines < 3) lines = 3;
if(lines > maxlines) lines = maxlines;
bool useScrollbar = false;
if(lines > maxlines)
{
lines = maxlines;
useScrollbar = true;
}
new StaticTextWidget(_boss, _font, x, y + 1, "Description ");
myDesc = new StringListWidget(_boss, _nfont, x+lwidth, y - 1,
fwidth, lines * myLineHeight, false);
fwidth, lines * myLineHeight, false, useScrollbar);
myDesc->setEditable(false);
myDesc->setEnabled(false);
myDesc->setList(sl);

View File

@ -65,12 +65,18 @@ CartRamWidget::CartRamWidget(
StringParser bs(desc, (fwidth - ScrollBarWidget::scrollBarWidth(_font)) / myFontWidth);
const StringList& sl = bs.stringList();
uInt32 lines = uInt32(sl.size());
bool useScrollbar = false;
if(lines < 2) lines = 2;
if(lines > maxlines) lines = maxlines;
if(lines > maxlines)
{
lines = maxlines;
useScrollbar = true;
}
new StaticTextWidget(_boss, _font, xpos, ypos + 1, "Description ");
myDesc = new StringListWidget(boss, nfont, xpos+lwidth, ypos - 1,
fwidth, lines * myLineHeight, false);
fwidth, lines * myLineHeight, false, useScrollbar);
myDesc->setEditable(false);
myDesc->setEnabled(false);
myDesc->setList(sl);

View File

@ -141,8 +141,10 @@ void CheckListWidget::drawWidget(bool hilite)
}
// Only draw the caret while editing, and if it's in the current viewport
if(_editMode && (_selectedItem >= _scrollBar->_currentPos) &&
(_selectedItem < _scrollBar->_currentPos + _rows))
if(_editMode &&
(!(_useScrollbar) ||
(_selectedItem >= _scrollBar->_currentPos) &&
(_selectedItem < _scrollBar->_currentPos + _rows)))
drawCaret();
}

View File

@ -26,8 +26,9 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ListWidget::ListWidget(GuiObject* boss, const GUI::Font& font,
int x, int y, int w, int h)
: EditableWidget(boss, font, x, y, 16, 16)
int x, int y, int w, int h, bool useScrollbar)
: EditableWidget(boss, font, x, y, 16, 16),
_useScrollbar(useScrollbar)
{
_flags = Widget::FLAG_ENABLED | Widget::FLAG_CLEARBG | Widget::FLAG_RETAIN_FOCUS;
_bgcolor = kWidColor;
@ -39,13 +40,18 @@ ListWidget::ListWidget(GuiObject* boss, const GUI::Font& font,
_rows = h / _lineHeight;
// Set real dimensions
_w = w - ScrollBarWidget::scrollBarWidth(_font);
_h = h + 2;
// Create scrollbar and attach to the list
_scrollBar = new ScrollBarWidget(boss, font, _x + _w, _y,
ScrollBarWidget::scrollBarWidth(_font), _h);
_scrollBar->setTarget(this);
if(_useScrollbar)
{
_w = w - ScrollBarWidget::scrollBarWidth(_font);
_scrollBar = new ScrollBarWidget(boss, font, _x + _w, _y,
ScrollBarWidget::scrollBarWidth(_font), _h);
_scrollBar->setTarget(this);
}
else
_w = w - 1;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -168,10 +174,13 @@ void ListWidget::recalc()
_editMode = false;
_scrollBar->_numEntries = int(_list.size());
_scrollBar->_entriesPerPage = _rows;
// disable scrollbar if no longer necessary
scrollBarRecalc();
if(_useScrollbar)
{
_scrollBar->_numEntries = int(_list.size());
_scrollBar->_entriesPerPage = _rows;
// disable scrollbar if no longer necessary
scrollBarRecalc();
}
// Reset to normal data entry
abortEditMode();
@ -182,9 +191,12 @@ void ListWidget::recalc()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ListWidget::scrollBarRecalc()
{
_scrollBar->_currentPos = _currentPos;
_scrollBar->recalc();
sendCommand(ListWidget::kScrolledCmd, _currentPos, _id);
if(_useScrollbar)
{
_scrollBar->_currentPos = _currentPos;
_scrollBar->recalc();
sendCommand(ListWidget::kScrolledCmd, _currentPos, _id);
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -230,7 +242,8 @@ void ListWidget::handleMouseUp(int x, int y, MouseButton b, int clickCount)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ListWidget::handleMouseWheel(int x, int y, int direction)
{
_scrollBar->handleMouseWheel(x, y, direction);
if(_useScrollbar)
_scrollBar->handleMouseWheel(x, y, direction);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -354,8 +367,11 @@ bool ListWidget::handleEvent(Event::Type e)
if (_selectedItem != oldSelectedItem)
{
_scrollBar->draw();
scrollToSelected();
if(_useScrollbar)
{
_scrollBar->draw();
scrollToSelected();
}
sendCommand(ListWidget::kSelectionChangedCmd, _selectedItem, _id);
}
@ -408,14 +424,17 @@ void ListWidget::scrollToCurrent(int item)
else if (_currentPos + _rows > int(_list.size()))
_currentPos = int(_list.size()) - _rows;
int oldScrollPos = _scrollBar->_currentPos;
_scrollBar->_currentPos = _currentPos;
_scrollBar->recalc();
if(_useScrollbar)
{
int oldScrollPos = _scrollBar->_currentPos;
_scrollBar->_currentPos = _currentPos;
_scrollBar->recalc();
setDirty();
setDirty();
if(oldScrollPos != _currentPos)
sendCommand(ListWidget::kScrolledCmd, _currentPos, _id);
if(oldScrollPos != _currentPos)
sendCommand(ListWidget::kScrolledCmd, _currentPos, _id);
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -42,7 +42,7 @@ class ListWidget : public EditableWidget
public:
ListWidget(GuiObject* boss, const GUI::Font& font,
int x, int y, int w, int h);
int x, int y, int w, int h, bool useScrollbar = true);
~ListWidget() override = default;
int rows() const { return _rows; }
@ -100,6 +100,7 @@ class ListWidget : public EditableWidget
int _selectedItem{-1};
int _highlightedItem{-1};
bool _editMode{false};
bool _useScrollbar{true};
ScrollBarWidget* _scrollBar{nullptr};

View File

@ -24,8 +24,9 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
StringListWidget::StringListWidget(GuiObject* boss, const GUI::Font& font,
int x, int y, int w, int h, bool hilite)
: ListWidget(boss, font, x, y, w, h),
int x, int y, int w, int h, bool hilite,
bool useScrollbar)
: ListWidget(boss, font, x, y, w, h, useScrollbar),
_hilite(hilite)
{
_bgcolorlo = kDlgColor;
@ -107,8 +108,10 @@ void StringListWidget::drawWidget(bool hilite)
}
// Only draw the caret while editing, and if it's in the current viewport
if(_editMode && (_selectedItem >= _scrollBar->_currentPos) &&
(_selectedItem < _scrollBar->_currentPos + _rows))
if(_editMode &&
(!(_useScrollbar) ||
(_selectedItem >= _scrollBar->_currentPos) &&
(_selectedItem < _scrollBar->_currentPos + _rows)))
drawCaret();
}

View File

@ -25,7 +25,8 @@ class StringListWidget : public ListWidget
{
public:
StringListWidget(GuiObject* boss, const GUI::Font& font,
int x, int y, int w, int h, bool hilite = true);
int x, int y, int w, int h, bool hilite = true,
bool useScrollbar = true);
~StringListWidget() override = default;
void setList(const StringList& list);