allow disabling scroll bars in ListWidget

This commit is contained in:
thrust26 2020-10-15 09:40:52 +02:00
parent 51416dd883
commit 388e3504fb
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(); const StringList& sl = bs.stringList();
uInt32 lines = uInt32(sl.size()); uInt32 lines = uInt32(sl.size());
if(lines < 3) lines = 3; 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 "); new StaticTextWidget(_boss, _font, x, y + 1, "Description ");
myDesc = new StringListWidget(_boss, _nfont, x+lwidth, y - 1, myDesc = new StringListWidget(_boss, _nfont, x+lwidth, y - 1,
fwidth, lines * myLineHeight, false); fwidth, lines * myLineHeight, false, useScrollbar);
myDesc->setEditable(false); myDesc->setEditable(false);
myDesc->setEnabled(false); myDesc->setEnabled(false);
myDesc->setList(sl); myDesc->setList(sl);

View File

@ -65,12 +65,18 @@ CartRamWidget::CartRamWidget(
StringParser bs(desc, (fwidth - ScrollBarWidget::scrollBarWidth(_font)) / myFontWidth); StringParser bs(desc, (fwidth - ScrollBarWidget::scrollBarWidth(_font)) / myFontWidth);
const StringList& sl = bs.stringList(); const StringList& sl = bs.stringList();
uInt32 lines = uInt32(sl.size()); uInt32 lines = uInt32(sl.size());
bool useScrollbar = false;
if(lines < 2) lines = 2; 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 "); new StaticTextWidget(_boss, _font, xpos, ypos + 1, "Description ");
myDesc = new StringListWidget(boss, nfont, xpos+lwidth, ypos - 1, myDesc = new StringListWidget(boss, nfont, xpos+lwidth, ypos - 1,
fwidth, lines * myLineHeight, false); fwidth, lines * myLineHeight, false, useScrollbar);
myDesc->setEditable(false); myDesc->setEditable(false);
myDesc->setEnabled(false); myDesc->setEnabled(false);
myDesc->setList(sl); 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 // Only draw the caret while editing, and if it's in the current viewport
if(_editMode && (_selectedItem >= _scrollBar->_currentPos) && if(_editMode &&
(_selectedItem < _scrollBar->_currentPos + _rows)) (!(_useScrollbar) ||
(_selectedItem >= _scrollBar->_currentPos) &&
(_selectedItem < _scrollBar->_currentPos + _rows)))
drawCaret(); drawCaret();
} }

View File

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

View File

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

View File

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

View File

@ -25,7 +25,8 @@ class StringListWidget : public ListWidget
{ {
public: public:
StringListWidget(GuiObject* boss, const GUI::Font& font, 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; ~StringListWidget() override = default;
void setList(const StringList& list); void setList(const StringList& list);