1 #include <lib/gui/elistbox.h>
2 #include <lib/gui/elistboxcontent.h>
3 #include <lib/gdi/font.h>
7 The basic idea is to have an interface which gives all relevant list
8 processing functions, and can be used by the listbox to browse trough
11 The listbox directly uses the implemented cursor. It tries hard to avoid
12 iterating trough the (possibly very large) list, so it should be O(1),
13 i.e. the performance should not be influenced by the size of the list.
15 The list interface knows how to draw the current entry to a specified
16 offset. Different interfaces can be used to adapt different lists,
17 pre-filter lists on the fly etc.
19 cursorSave/Restore is used to avoid re-iterating the list on redraw.
20 The current selection is always selected as cursor position, the
21 cursor is then positioned to the start, and then iterated. This gives
22 at most 2x m_items_per_page cursor movements per redraw, indepenent
23 of the size of the list.
25 Although cursorSet is provided, it should be only used when there is no
26 other way, as it involves iterating trough the list.
29 iListboxContent::~iListboxContent()
33 iListboxContent::iListboxContent(): m_listbox(0)
37 void iListboxContent::setListbox(eListbox *lb)
42 DEFINE_REF(eListboxTestContent);
44 void eListboxTestContent::cursorHome()
49 void eListboxTestContent::cursorEnd()
54 int eListboxTestContent::cursorMove(int count)
60 else if (m_cursor > size())
65 int eListboxTestContent::cursorValid()
67 return m_cursor < size();
70 int eListboxTestContent::cursorSet(int n)
76 else if (m_cursor > size())
81 int eListboxTestContent::cursorGet()
86 void eListboxTestContent::cursorSave()
88 m_saved_cursor = m_cursor;
91 void eListboxTestContent::cursorRestore()
93 m_cursor = m_saved_cursor;
96 int eListboxTestContent::size()
101 RESULT eListboxTestContent::connectItemChanged(const Slot0<void> &itemChanged, ePtr<eConnection> &connection)
106 void eListboxTestContent::setSize(const eSize &size)
111 void eListboxTestContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
113 ePtr<gFont> fnt = new gFont("Arial", 14);
114 painter.clip(eRect(offset, m_size));
115 style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
120 painter.setFont(fnt);
122 sprintf(string, "%d.)", m_cursor);
124 ePoint text_offset = offset + (selected ? ePoint(2, 2) : ePoint(1, 1));
126 painter.renderText(eRect(text_offset, m_size), string);
129 style.drawFrame(painter, eRect(offset, m_size), eWindowStyle::frameListboxEntry);
135 //////////////////////////////////////
137 DEFINE_REF(eListboxStringContent);
139 eListboxStringContent::eListboxStringContent()
145 void eListboxStringContent::cursorHome()
147 m_cursor = m_list.begin();
151 void eListboxStringContent::cursorEnd()
153 m_cursor = m_list.end();
154 m_cursor_number = m_size;
157 int eListboxStringContent::cursorMove(int count)
161 while (count && (m_cursor != m_list.end()))
167 } else if (count < 0)
169 while (count && (m_cursor != m_list.begin()))
180 int eListboxStringContent::cursorValid()
182 return m_cursor != m_list.end();
185 int eListboxStringContent::cursorSet(int n)
193 int eListboxStringContent::cursorGet()
195 return m_cursor_number;
198 void eListboxStringContent::cursorSave()
200 m_saved_cursor = m_cursor;
201 m_saved_cursor_number = m_cursor_number;
204 void eListboxStringContent::cursorRestore()
206 m_cursor = m_saved_cursor;
207 m_cursor_number = m_saved_cursor_number;
210 int eListboxStringContent::size()
215 void eListboxStringContent::setSize(const eSize &size)
220 void eListboxStringContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
222 ePtr<gFont> fnt = new gFont("Arial", 14);
223 painter.clip(eRect(offset, m_itemsize));
224 style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
227 eDebug("item %d", m_cursor_number);
230 eDebug("is valid..");
231 painter.setFont(fnt);
233 ePoint text_offset = offset + (selected ? ePoint(2, 2) : ePoint(1, 1));
235 painter.renderText(eRect(text_offset, m_itemsize), *m_cursor);
238 style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
244 void eListboxStringContent::setList(std::list<std::string> &list)
247 m_size = list.size();
251 //////////////////////////////////////
253 DEFINE_REF(eListboxPythonStringContent);
255 eListboxPythonStringContent::eListboxPythonStringContent()
260 eListboxPythonStringContent::~eListboxPythonStringContent()
264 void eListboxPythonStringContent::cursorHome()
269 void eListboxPythonStringContent::cursorEnd()
274 int eListboxPythonStringContent::cursorMove(int count)
280 else if (m_cursor > size())
285 int eListboxPythonStringContent::cursorValid()
287 return m_cursor < size();
290 int eListboxPythonStringContent::cursorSet(int n)
296 else if (m_cursor > size())
301 int eListboxPythonStringContent::cursorGet()
306 void eListboxPythonStringContent::cursorSave()
308 m_saved_cursor = m_cursor;
311 void eListboxPythonStringContent::cursorRestore()
313 m_cursor = m_saved_cursor;
316 int eListboxPythonStringContent::size()
320 return PyList_Size(m_list);
323 void eListboxPythonStringContent::setSize(const eSize &size)
328 void eListboxPythonStringContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
330 ePtr<gFont> fnt = new gFont("Arial", 14);
331 painter.clip(eRect(offset, m_itemsize));
332 style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
335 if (m_list && cursorValid())
337 PyObject *item = PyList_GetItem(m_list, m_cursor); // borrowed reference!
338 painter.setFont(fnt);
340 /* the user can supply tuples, in this case the first one will be displayed. */
341 if (PyTuple_Check(item))
342 item = PyTuple_GetItem(item, 0);
344 const char *string = PyString_Check(item) ? PyString_AsString(item) : "<not-a-string>";
346 ePoint text_offset = offset + (selected ? ePoint(2, 2) : ePoint(1, 1));
348 painter.renderText(eRect(text_offset, m_itemsize), string);
351 style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
357 void eListboxPythonStringContent::setList(PyObject *list)
360 if (!PyList_Check(list))
370 PyObject *eListboxPythonStringContent::getCurrentSelection()
376 PyObject *r = PyList_GetItem(m_list, m_cursor);
381 void eListboxPythonStringContent::invalidateEntry(int index)
384 m_listbox->entryChanged(index);
387 //////////////////////////////////////
389 void eListboxPythonConfigContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
391 ePtr<gFont> fnt = new gFont("Arial", 14);
392 ePtr<gFont> fnt2 = new gFont("Arial", 16);
393 painter.clip(eRect(offset, m_itemsize));
394 style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
397 if (m_list && cursorValid())
399 /* get current list item */
400 PyObject *item = PyList_GetItem(m_list, m_cursor); // borrowed reference!
401 PyObject *text = 0, *value = 0;
402 painter.setFont(fnt);
404 /* the first tuple element is a string for the left side.
405 the second one will be called, and the result shall be an tuple.
408 the first one is the type (string).
409 the second one is the value. */
410 if (PyTuple_Check(item))
412 /* handle left part. get item from tuple, convert to string, display. */
414 text = PyTuple_GetItem(item, 0);
415 text = PyObject_Str(text);
416 const char *string = (text && PyString_Check(text)) ? PyString_AsString(text) : "<not-a-string>";
417 eSize item_left = eSize(m_seperation, m_itemsize.height());
418 eSize item_right = eSize(m_itemsize.width() - m_seperation, m_itemsize.height());
419 painter.renderText(eRect(offset, item_left), string, gPainter::RT_HALIGN_LEFT);
422 /* now, handle the value. get 2nd part from tuple*/
423 value = PyTuple_GetItem(item, 1);
426 PyObject *args = PyTuple_New(1);
427 PyTuple_SetItem(args, 0, PyInt_FromLong(selected));
429 /* CallObject will call __call__ which should return the value tuple */
430 value = PyObject_CallObject(value, args);
433 /* the PyInt was stolen. */
436 /* check if this is really a tuple */
437 if (PyTuple_Check(value))
439 /* convert type to string */
440 PyObject *type = PyTuple_GetItem(value, 0);
441 const char *atype = (type && PyString_Check(type)) ? PyString_AsString(type) : 0;
445 if (!strcmp(atype, "text"))
447 PyObject *pvalue = PyTuple_GetItem(value, 1);
448 const char *value = (pvalue && PyString_Check(pvalue)) ? PyString_AsString(pvalue) : "<not-a-string>";
449 painter.setFont(fnt2);
450 painter.renderText(eRect(offset + eSize(m_seperation, 0), item_right), value, gPainter::RT_HALIGN_RIGHT);
452 /* pvalue is borrowed */
453 } else if (!strcmp(atype, "slider"))
455 PyObject *pvalue = PyTuple_GetItem(value, 1);
457 /* convert value to Long. fallback to -1 on error. */
458 int value = (pvalue && PyInt_Check(pvalue)) ? PyInt_AsLong(pvalue) : -1;
460 /* calc. slider length */
461 int width = item_right.width() * value / 100;
462 int height = item_right.height();
466 //painter.fill(eRect(offset.x() + m_seperation, offset.y(), width, height));
467 //hack - make it customizable
468 painter.fill(eRect(offset.x() + m_seperation, offset.y() + 5, width, height-10));
470 /* pvalue is borrowed */
471 } else if (!strcmp(atype, "mtext"))
473 PyObject *pvalue = PyTuple_GetItem(value, 1);
474 const char *text = (pvalue && PyString_Check(pvalue)) ? PyString_AsString(pvalue) : "<not-a-string>";
476 ePtr<eTextPara> para = new eTextPara(eRect(offset + eSize(m_seperation, 0), item_right));
478 para->renderString(text, 0);
479 para->realign(eTextPara::dirRight);
480 int glyphs = para->size();
484 if (PyTuple_Size(value) >= 3)
485 plist = PyTuple_GetItem(value, 2);
489 if (plist && PyList_Check(plist))
490 entries = PyList_Size(plist);
492 for (int i = 0; i < entries; ++i)
494 PyObject *entry = PyList_GetItem(plist, i);
495 int num = PyInt_Check(entry) ? PyInt_AsLong(entry) : -1;
497 if ((num < 0) || (num >= glyphs))
498 eWarning("glyph index %d in PythonConfigList out of bounds!");
501 para->setGlyphFlag(num, GS_INVERT);
503 bbox = para->getGlyphBBox(num);
504 bbox = eRect(bbox.left(), offset.y(), bbox.width(), m_itemsize.height());
507 /* entry is borrowed */
510 painter.renderPara(para, ePoint(0, 0));
511 /* pvalue is borrowed */
512 /* plist is 0 or borrowed */
517 /* value is borrowed */
521 style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
527 //////////////////////////////////////
529 void eListboxPythonMultiContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
531 painter.clip(eRect(offset, m_itemsize));
532 style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
535 if (m_list && cursorValid())
537 PyObject *items = PyList_GetItem(m_list, m_cursor); // borrowed reference!
541 eDebug("eListboxPythonMultiContent: error getting item %d", m_cursor);
546 if (!PyList_Check(items))
548 eDebug("eListboxPythonMultiContent: list entry %d is not a list", m_cursor);
553 int size = PyList_Size(items);
554 for (int i = 1; i < size; ++i)
556 PyObject *item = PyList_GetItem(items, i); // borrowed reference!
560 eDebug("eListboxPythonMultiContent: ?");
566 PyObject *px, *py, *pwidth, *pheight, *pfnt, *pstring, *pflags;
569 we have a list of tuples:
571 (x, y, width, height, fnt, flags, "bla" ),
575 if (!PyTuple_Check(item))
577 eDebug("eListboxPythonMultiContent did not receive a tuple.");
582 px = PyTuple_GetItem(item, 0);
583 py = PyTuple_GetItem(item, 1);
584 pwidth = PyTuple_GetItem(item, 2);
585 pheight = PyTuple_GetItem(item, 3);
586 pfnt = PyTuple_GetItem(item, 4);
587 pflags = PyTuple_GetItem(item, 5);
588 pstring = PyTuple_GetItem(item, 6);
590 if (!(px && py && pwidth && pheight && pfnt && pstring))
592 eDebug("eListboxPythonMultiContent received too small tuple (must be (x, y, width, height, fnt, flags, string[, ...])");
597 pstring = PyObject_Str(pstring);
599 const char *string = (PyString_Check(pstring)) ? PyString_AsString(pstring) : "<not-a-string>";
601 int x = PyInt_AsLong(px);
602 int y = PyInt_AsLong(py);
603 int width = PyInt_AsLong(pwidth);
604 int height = PyInt_AsLong(pheight);
605 int flags = PyInt_AsLong(pflags);
607 int fnt = PyInt_AsLong(pfnt);
609 if (m_font.find(fnt) == m_font.end())
611 eDebug("eListboxPythonMultiContent: specified font %d was not found!", fnt);
617 eRect r = eRect(x, y, width, height);
620 painter.setFont(m_font[fnt]);
622 painter.renderText(r, string, flags);
627 style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
634 void eListboxPythonMultiContent::setFont(int fnt, gFont *font)