Skip to content

Commit

Permalink
[elistbox] Add method to calculate max list-item width for use in Mes…
Browse files Browse the repository at this point in the history
…sageBox

Author: @DimitarCC
  • Loading branch information
Huevos committed Oct 11, 2023
1 parent ac84823 commit f992732
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/gui/elistbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class iListboxContent: public iObject
virtual int getItemHeight()=0;
virtual int getItemWidth() { return -1; }
virtual int getOrientation() { return 1; }
virtual int getMaxItemTextWidth() { return 1; }

eListbox *m_listbox;
#endif
Expand Down Expand Up @@ -184,6 +185,7 @@ class eListbox: public eWidget

int getScrollbarWidth() { return m_scrollbar_width; }
int getScrollbarHeight() { return m_scrollbar_height; }
int getMaxItemTextWidth() { return m_content->getMaxItemTextWidth(); }

#ifndef SWIG
struct eListboxStyle *getLocalStyle(void);
Expand Down
42 changes: 39 additions & 3 deletions lib/gui/elistboxcontent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int iListboxContent::currentCursorSelectable()
DEFINE_REF(eListboxPythonStringContent);

eListboxPythonStringContent::eListboxPythonStringContent()
:m_cursor(0), m_saved_cursor(0), m_itemheight(25), m_itemwidth(25), m_orientation(1)
:m_cursor(0), m_saved_cursor(0), m_itemheight(25), m_itemwidth(25), m_max_text_width(0), m_orientation(1)
{
}

Expand Down Expand Up @@ -138,6 +138,43 @@ void eListboxPythonStringContent::setSize(const eSize &size)
m_itemsize = size;
}

int eListboxPythonStringContent::getMaxItemTextWidth()
{
ePtr<gFont> fnt;
eListboxStyle *local_style = 0;
int m_text_offset = 1;
if (m_listbox)
local_style = m_listbox->getLocalStyle();
if (local_style) {
fnt = local_style->m_font;
m_text_offset = local_style->m_text_offset.x();
}
if (!fnt) fnt = new gFont("Regular", 20);

for (int i = 0; i < size(); i++)
{
ePyObject item = PyList_GET_ITEM(m_list, i);
if (PyTuple_Check(item))
{
item = PyTuple_GET_ITEM(item, 0);
}
if (item != Py_None) {
const char *string = PyUnicode_Check(item) ? PyUnicode_AsUTF8(item) : "<not-a-string>";
eRect textRect = eRect(0,0, 8000, 100);

ePtr<eTextPara> para = new eTextPara(textRect);
para->setFont(fnt);
para->renderString(string);
int textWidth = para->getBoundBox().width();
if (textWidth > m_max_text_width) {
m_max_text_width = textWidth;
}
}
}

return m_max_text_width + (m_text_offset*2);
}

void eListboxPythonStringContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
{
ePtr<gFont> fnt;
Expand Down Expand Up @@ -275,8 +312,7 @@ void eListboxPythonStringContent::paint(gPainter &painter, eWindowStyle &style,
flags |= gPainter::RT_HALIGN_BLOCK;
}

painter.renderText(eRect(text_offset, m_itemsize),
string, flags, border_color, border_size);
painter.renderText(eRect(text_offset, m_itemsize), string, flags, border_color, border_size);
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/gui/elistboxcontent.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class eListboxPythonStringContent: public virtual iListboxContent
void invalidateEntry(int index);
void invalidate();
eSize getItemSize() { return m_itemsize; }
int getMaxItemTextWidth();
#ifndef SWIG
protected:
void cursorHome();
Expand Down Expand Up @@ -53,6 +54,7 @@ class eListboxPythonStringContent: public virtual iListboxContent
ePtr<gFont> m_font;
int m_itemheight;
int m_itemwidth;
int m_max_text_width;
int m_orientation;
#endif
};
Expand Down
3 changes: 3 additions & 0 deletions lib/python/Screens/MessageBox.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,6 @@ def move(self, direction):

def __repr__(self):
return "%s(%s)" % (str(type(self)), self.text)

def getListWidth(self):
return self["list"].instance.getMaxItemTextWidth()

0 comments on commit f992732

Please sign in to comment.