3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <IGUIScrollBar.h>
29 #include "client/renderingengine.h"
32 #include "client/tile.h"
34 #include "util/string.h"
35 #include "util/numeric.h"
36 #include "util/string.h" // for parseColorString()
37 #include "settings.h" // for settings
38 #include "porting.h" // for dpi
39 #include "guiscalingfilter.h"
45 GUITable::GUITable(gui::IGUIEnvironment *env,
46 gui::IGUIElement* parent, s32 id,
47 core::rect<s32> rectangle,
48 ISimpleTextureSource *tsrc
50 gui::IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rectangle),
55 gui::IGUISkin* skin = Environment->getSkin();
57 m_font = skin->getFont();
60 m_rowheight = m_font->getDimension(L"A").Height + 4;
61 m_rowheight = MYMAX(m_rowheight, 1);
64 const s32 s = skin->getSize(gui::EGDS_SCROLLBAR_SIZE);
65 m_scrollbar = Environment->addScrollBar(false,
66 core::rect<s32>(RelativeRect.getWidth() - s,
68 RelativeRect.getWidth(),
69 RelativeRect.getHeight()),
71 m_scrollbar->setSubElement(true);
72 m_scrollbar->setTabStop(false);
73 m_scrollbar->setAlignment(gui::EGUIA_LOWERRIGHT, gui::EGUIA_LOWERRIGHT,
74 gui::EGUIA_UPPERLEFT, gui::EGUIA_LOWERRIGHT);
75 m_scrollbar->setVisible(false);
76 m_scrollbar->setPos(0);
80 updateAbsolutePosition();
82 core::rect<s32> relative_rect = m_scrollbar->getRelativePosition();
83 s32 width = (relative_rect.getWidth()/(2.0/3.0)) *
84 RenderingEngine::getDisplayDensity() *
85 g_settings->getFloat("gui_scaling");
86 m_scrollbar->setRelativePosition(core::rect<s32>(
87 relative_rect.LowerRightCorner.X-width,relative_rect.UpperLeftCorner.Y,
88 relative_rect.LowerRightCorner.X,relative_rect.LowerRightCorner.Y
94 for (GUITable::Row &row : m_rows)
100 m_scrollbar->remove();
103 GUITable::Option GUITable::splitOption(const std::string &str)
105 size_t equal_pos = str.find('=');
106 if (equal_pos == std::string::npos)
107 return GUITable::Option(str, "");
109 return GUITable::Option(str.substr(0, equal_pos),
110 str.substr(equal_pos + 1));
113 void GUITable::setTextList(const std::vector<std::string> &content,
119 m_background.setAlpha(0);
123 m_is_textlist = true;
125 s32 empty_string_index = allocString("");
127 m_rows.resize(content.size());
128 for (s32 i = 0; i < (s32) content.size(); ++i) {
129 Row *row = &m_rows[i];
130 row->cells = new Cell[1];
133 row->visible_index = i;
134 m_visible_rows.push_back(i);
136 Cell *cell = row->cells;
138 cell->xmax = 0x7fff; // something large enough
140 cell->content_type = COLUMN_TYPE_TEXT;
141 cell->content_index = empty_string_index;
142 cell->tooltip_index = empty_string_index;
143 cell->color.set(255, 255, 255, 255);
144 cell->color_defined = false;
145 cell->reported_column = 1;
147 // parse row content (color)
148 const std::string &s = content[i];
149 if (s[0] == '#' && s[1] == '#') {
150 // double # to escape
151 cell->content_index = allocString(s.substr(2));
153 else if (s[0] == '#' && s.size() >= 7 &&
155 s.substr(0,7), cell->color, false)) {
156 // single # for color
157 cell->color_defined = true;
158 cell->content_index = allocString(s.substr(7));
162 cell->content_index = allocString(s);
167 allocationComplete();
169 // Clamp scroll bar position
173 void GUITable::setTable(const TableOptions &options,
174 const TableColumns &columns,
175 std::vector<std::string> &content)
179 // Naming conventions:
180 // i is always a row index, 0-based
181 // j is always a column index, 0-based
182 // k is another index, for example an option index
184 // Handle a stupid error case... (issue #1187)
185 if (columns.empty()) {
186 TableColumn text_column;
187 text_column.type = "text";
188 TableColumns new_columns;
189 new_columns.push_back(text_column);
190 setTable(options, new_columns, content);
194 // Handle table options
195 video::SColor default_color(255, 255, 255, 255);
197 for (const Option &option : options) {
198 const std::string &name = option.name;
199 const std::string &value = option.value;
201 parseColorString(value, m_color, false);
202 else if (name == "background")
203 parseColorString(value, m_background, false);
204 else if (name == "border")
205 m_border = is_yes(value);
206 else if (name == "highlight")
207 parseColorString(value, m_highlight, false);
208 else if (name == "highlight_text")
209 parseColorString(value, m_highlight_text, false);
210 else if (name == "opendepth")
211 opendepth = stoi(value);
213 errorstream<<"Invalid table option: \""<<name<<"\""
214 <<" (value=\""<<value<<"\")"<<std::endl;
217 // Get number of columns and rows
218 // note: error case columns.size() == 0 was handled above
219 s32 colcount = columns.size();
220 assert(colcount >= 1);
221 // rowcount = ceil(cellcount / colcount) but use integer arithmetic
222 s32 rowcount = (content.size() + colcount - 1) / colcount;
223 assert(rowcount >= 0);
224 // Append empty strings to content if there is an incomplete row
225 s32 cellcount = rowcount * colcount;
226 while (content.size() < (u32) cellcount)
227 content.emplace_back("");
229 // Create temporary rows (for processing columns)
231 // Current horizontal position (may different between rows due
232 // to indent/tree columns, or text/image columns with width<0)
234 // Tree indentation level
236 // Next cell: Index into m_strings or m_images
238 // Next cell: Width in pixels
240 // Vector of completed cells in this row
241 std::vector<Cell> cells;
242 // Stores colors and how long they last (maximum column index)
243 std::vector<std::pair<video::SColor, s32> > colors;
245 TempRow(): x(0), indent(0), content_index(0), content_width(0) {}
247 TempRow *rows = new TempRow[rowcount];
249 // Get em width. Pedantically speaking, the width of "M" is not
250 // necessarily the same as the em width, but whatever, close enough.
253 em = m_font->getDimension(L"M").Width;
255 s32 default_tooltip_index = allocString("");
257 std::map<s32, s32> active_image_indices;
259 // Process content in column-major order
260 for (s32 j = 0; j < colcount; ++j) {
262 ColumnType columntype = COLUMN_TYPE_TEXT;
263 if (columns[j].type == "text")
264 columntype = COLUMN_TYPE_TEXT;
265 else if (columns[j].type == "image")
266 columntype = COLUMN_TYPE_IMAGE;
267 else if (columns[j].type == "color")
268 columntype = COLUMN_TYPE_COLOR;
269 else if (columns[j].type == "indent")
270 columntype = COLUMN_TYPE_INDENT;
271 else if (columns[j].type == "tree")
272 columntype = COLUMN_TYPE_TREE;
274 errorstream<<"Invalid table column type: \""
275 <<columns[j].type<<"\""<<std::endl;
277 // Process column options
278 s32 padding = myround(0.5 * em);
279 s32 tooltip_index = default_tooltip_index;
284 if (columntype == COLUMN_TYPE_INDENT) {
285 padding = 0; // default indent padding
287 if (columntype == COLUMN_TYPE_INDENT ||
288 columntype == COLUMN_TYPE_TREE) {
289 width = myround(em * 1.5); // default indent width
292 for (const Option &option : columns[j].options) {
293 const std::string &name = option.name;
294 const std::string &value = option.value;
295 if (name == "padding")
296 padding = myround(stof(value) * em);
297 else if (name == "tooltip")
298 tooltip_index = allocString(value);
299 else if (name == "align" && value == "left")
301 else if (name == "align" && value == "center")
303 else if (name == "align" && value == "right")
305 else if (name == "align" && value == "inline")
307 else if (name == "width")
308 width = myround(stof(value) * em);
309 else if (name == "span" && columntype == COLUMN_TYPE_COLOR)
311 else if (columntype == COLUMN_TYPE_IMAGE &&
313 string_allowed(name, "0123456789")) {
314 s32 content_index = allocImage(value);
315 active_image_indices.insert(std::make_pair(
320 errorstream<<"Invalid table column option: \""<<name<<"\""
321 <<" (value=\""<<value<<"\")"<<std::endl;
325 // If current column type can use information from "color" columns,
326 // find out which of those is currently active
327 if (columntype == COLUMN_TYPE_TEXT) {
328 for (s32 i = 0; i < rowcount; ++i) {
329 TempRow *row = &rows[i];
330 while (!row->colors.empty() && row->colors.back().second < j)
331 row->colors.pop_back();
335 // Make template for new cells
337 memset(&newcell, 0, sizeof newcell);
338 newcell.content_type = columntype;
339 newcell.tooltip_index = tooltip_index;
340 newcell.reported_column = j+1;
342 if (columntype == COLUMN_TYPE_TEXT) {
343 // Find right edge of column
345 for (s32 i = 0; i < rowcount; ++i) {
346 TempRow *row = &rows[i];
347 row->content_index = allocString(content[i * colcount + j]);
348 const core::stringw &text = m_strings[row->content_index];
349 row->content_width = m_font ?
350 m_font->getDimension(text.c_str()).Width : 0;
351 row->content_width = MYMAX(row->content_width, width);
352 s32 row_xmax = row->x + padding + row->content_width;
353 xmax = MYMAX(xmax, row_xmax);
355 // Add a new cell (of text type) to each row
356 for (s32 i = 0; i < rowcount; ++i) {
357 newcell.xmin = rows[i].x + padding;
358 alignContent(&newcell, xmax, rows[i].content_width, align);
359 newcell.content_index = rows[i].content_index;
360 newcell.color_defined = !rows[i].colors.empty();
361 if (newcell.color_defined)
362 newcell.color = rows[i].colors.back().first;
363 rows[i].cells.push_back(newcell);
364 rows[i].x = newcell.xmax;
367 else if (columntype == COLUMN_TYPE_IMAGE) {
368 // Find right edge of column
370 for (s32 i = 0; i < rowcount; ++i) {
371 TempRow *row = &rows[i];
372 row->content_index = -1;
374 // Find content_index. Image indices are defined in
375 // column options so check active_image_indices.
376 s32 image_index = stoi(content[i * colcount + j]);
377 std::map<s32, s32>::iterator image_iter =
378 active_image_indices.find(image_index);
379 if (image_iter != active_image_indices.end())
380 row->content_index = image_iter->second;
382 // Get texture object (might be NULL)
383 video::ITexture *image = NULL;
384 if (row->content_index >= 0)
385 image = m_images[row->content_index];
387 // Get content width and update xmax
388 row->content_width = image ? image->getOriginalSize().Width : 0;
389 row->content_width = MYMAX(row->content_width, width);
390 s32 row_xmax = row->x + padding + row->content_width;
391 xmax = MYMAX(xmax, row_xmax);
393 // Add a new cell (of image type) to each row
394 for (s32 i = 0; i < rowcount; ++i) {
395 newcell.xmin = rows[i].x + padding;
396 alignContent(&newcell, xmax, rows[i].content_width, align);
397 newcell.content_index = rows[i].content_index;
398 rows[i].cells.push_back(newcell);
399 rows[i].x = newcell.xmax;
401 active_image_indices.clear();
403 else if (columntype == COLUMN_TYPE_COLOR) {
404 for (s32 i = 0; i < rowcount; ++i) {
405 video::SColor cellcolor(255, 255, 255, 255);
406 if (parseColorString(content[i * colcount + j], cellcolor, true))
407 rows[i].colors.emplace_back(cellcolor, j+span);
410 else if (columntype == COLUMN_TYPE_INDENT ||
411 columntype == COLUMN_TYPE_TREE) {
412 // For column type "tree", reserve additional space for +/-
413 // Also enable special processing for treeview-type tables
414 s32 content_width = 0;
415 if (columntype == COLUMN_TYPE_TREE) {
416 content_width = m_font ? m_font->getDimension(L"+").Width : 0;
417 m_has_tree_column = true;
419 // Add a new cell (of indent or tree type) to each row
420 for (s32 i = 0; i < rowcount; ++i) {
421 TempRow *row = &rows[i];
423 s32 indentlevel = stoi(content[i * colcount + j]);
424 indentlevel = MYMAX(indentlevel, 0);
425 if (columntype == COLUMN_TYPE_TREE)
426 row->indent = indentlevel;
428 newcell.xmin = row->x + padding;
429 newcell.xpos = newcell.xmin + indentlevel * width;
430 newcell.xmax = newcell.xpos + content_width;
431 newcell.content_index = 0;
432 newcell.color_defined = !rows[i].colors.empty();
433 if (newcell.color_defined)
434 newcell.color = rows[i].colors.back().first;
435 row->cells.push_back(newcell);
436 row->x = newcell.xmax;
441 // Copy temporary rows to not so temporary rows
443 m_rows.resize(rowcount);
444 for (s32 i = 0; i < rowcount; ++i) {
445 Row *row = &m_rows[i];
446 row->cellcount = rows[i].cells.size();
447 row->cells = new Cell[row->cellcount];
448 memcpy((void*) row->cells, (void*) &rows[i].cells[0],
449 row->cellcount * sizeof(Cell));
450 row->indent = rows[i].indent;
451 row->visible_index = i;
452 m_visible_rows.push_back(i);
456 if (m_has_tree_column) {
457 // Treeview: convert tree to indent cells on leaf rows
458 for (s32 i = 0; i < rowcount; ++i) {
459 if (i == rowcount-1 || m_rows[i].indent >= m_rows[i+1].indent)
460 for (s32 j = 0; j < m_rows[i].cellcount; ++j)
461 if (m_rows[i].cells[j].content_type == COLUMN_TYPE_TREE)
462 m_rows[i].cells[j].content_type = COLUMN_TYPE_INDENT;
465 // Treeview: close rows according to opendepth option
466 std::set<s32> opened_trees;
467 for (s32 i = 0; i < rowcount; ++i)
468 if (m_rows[i].indent < opendepth)
469 opened_trees.insert(i);
470 setOpenedTrees(opened_trees);
473 // Delete temporary information used only during setTable()
475 allocationComplete();
477 // Clamp scroll bar position
481 void GUITable::clear()
483 // Clean up cells and rows
484 for (GUITable::Row &row : m_rows)
487 m_visible_rows.clear();
489 // Get colors from skin
490 gui::IGUISkin *skin = Environment->getSkin();
491 m_color = skin->getColor(gui::EGDC_BUTTON_TEXT);
492 m_background = skin->getColor(gui::EGDC_3D_HIGH_LIGHT);
493 m_highlight = skin->getColor(gui::EGDC_HIGH_LIGHT);
494 m_highlight_text = skin->getColor(gui::EGDC_HIGH_LIGHT_TEXT);
497 m_is_textlist = false;
498 m_has_tree_column = false;
501 m_sel_doubleclick = false;
503 m_keynav_buffer = L"";
507 m_alloc_strings.clear();
508 m_alloc_images.clear();
511 std::string GUITable::checkEvent()
513 s32 sel = getSelected();
520 std::ostringstream os(std::ios::binary);
521 if (m_sel_doubleclick) {
523 m_sel_doubleclick = false;
529 if (!m_is_textlist) {
530 os<<":"<<m_sel_column;
535 s32 GUITable::getSelected() const
540 assert(m_selected >= 0 && m_selected < (s32) m_visible_rows.size());
541 return m_visible_rows[m_selected] + 1;
544 void GUITable::setSelected(s32 index)
546 s32 old_selected = m_selected;
550 m_sel_doubleclick = false;
552 --index; // Switch from 1-based indexing to 0-based indexing
554 s32 rowcount = m_rows.size();
555 if (rowcount == 0 || index < 0) {
559 if (index >= rowcount) {
560 index = rowcount - 1;
563 // If the selected row is not visible, open its ancestors to make it visible
564 bool selection_invisible = m_rows[index].visible_index < 0;
565 if (selection_invisible) {
566 std::set<s32> opened_trees;
567 getOpenedTrees(opened_trees);
568 s32 indent = m_rows[index].indent;
569 for (s32 j = index - 1; j >= 0; --j) {
570 if (m_rows[j].indent < indent) {
571 opened_trees.insert(j);
572 indent = m_rows[j].indent;
575 setOpenedTrees(opened_trees);
579 m_selected = m_rows[index].visible_index;
580 assert(m_selected >= 0 && m_selected < (s32) m_visible_rows.size());
583 if (m_selected != old_selected || selection_invisible) {
588 GUITable::DynamicData GUITable::getDynamicData() const
591 dyndata.selected = getSelected();
592 dyndata.scrollpos = m_scrollbar->getPos();
593 dyndata.keynav_time = m_keynav_time;
594 dyndata.keynav_buffer = m_keynav_buffer;
595 if (m_has_tree_column)
596 getOpenedTrees(dyndata.opened_trees);
600 void GUITable::setDynamicData(const DynamicData &dyndata)
602 if (m_has_tree_column)
603 setOpenedTrees(dyndata.opened_trees);
605 m_keynav_time = dyndata.keynav_time;
606 m_keynav_buffer = dyndata.keynav_buffer;
608 setSelected(dyndata.selected);
610 m_sel_doubleclick = false;
612 m_scrollbar->setPos(dyndata.scrollpos);
615 const c8* GUITable::getTypeName() const
620 void GUITable::updateAbsolutePosition()
622 IGUIElement::updateAbsolutePosition();
626 void GUITable::draw()
631 gui::IGUISkin *skin = Environment->getSkin();
635 bool draw_background = m_background.getAlpha() > 0;
637 skin->draw3DSunkenPane(this, m_background,
638 true, draw_background,
639 AbsoluteRect, &AbsoluteClippingRect);
640 else if (draw_background)
641 skin->draw2DRectangle(this, m_background,
642 AbsoluteRect, &AbsoluteClippingRect);
646 core::rect<s32> client_clip(AbsoluteRect);
647 client_clip.UpperLeftCorner.Y += 1;
648 client_clip.UpperLeftCorner.X += 1;
649 client_clip.LowerRightCorner.Y -= 1;
650 client_clip.LowerRightCorner.X -= 1;
651 if (m_scrollbar->isVisible()) {
652 client_clip.LowerRightCorner.X =
653 m_scrollbar->getAbsolutePosition().UpperLeftCorner.X;
655 client_clip.clipAgainst(AbsoluteClippingRect);
659 s32 scrollpos = m_scrollbar->getPos();
660 s32 row_min = scrollpos / m_rowheight;
661 s32 row_max = (scrollpos + AbsoluteRect.getHeight() - 1)
663 row_max = MYMIN(row_max, (s32) m_visible_rows.size());
665 core::rect<s32> row_rect(AbsoluteRect);
666 if (m_scrollbar->isVisible())
667 row_rect.LowerRightCorner.X -=
668 skin->getSize(gui::EGDS_SCROLLBAR_SIZE);
669 row_rect.UpperLeftCorner.Y += row_min * m_rowheight - scrollpos;
670 row_rect.LowerRightCorner.Y = row_rect.UpperLeftCorner.Y + m_rowheight;
672 for (s32 i = row_min; i < row_max; ++i) {
673 Row *row = &m_rows[m_visible_rows[i]];
674 bool is_sel = i == m_selected;
675 video::SColor color = m_color;
678 skin->draw2DRectangle(this, m_highlight, row_rect, &client_clip);
679 color = m_highlight_text;
682 for (s32 j = 0; j < row->cellcount; ++j)
683 drawCell(&row->cells[j], color, row_rect, client_clip);
685 row_rect.UpperLeftCorner.Y += m_rowheight;
686 row_rect.LowerRightCorner.Y += m_rowheight;
693 void GUITable::drawCell(const Cell *cell, video::SColor color,
694 const core::rect<s32> &row_rect,
695 const core::rect<s32> &client_clip)
697 if ((cell->content_type == COLUMN_TYPE_TEXT)
698 || (cell->content_type == COLUMN_TYPE_TREE)) {
700 core::rect<s32> text_rect = row_rect;
701 text_rect.UpperLeftCorner.X = row_rect.UpperLeftCorner.X
703 text_rect.LowerRightCorner.X = row_rect.UpperLeftCorner.X
706 if (cell->color_defined)
710 if (cell->content_type == COLUMN_TYPE_TEXT)
711 m_font->draw(m_strings[cell->content_index],
713 false, true, &client_clip);
715 m_font->draw(cell->content_index ? L"+" : L"-",
717 false, true, &client_clip);
720 else if (cell->content_type == COLUMN_TYPE_IMAGE) {
722 if (cell->content_index < 0)
725 video::IVideoDriver *driver = Environment->getVideoDriver();
726 video::ITexture *image = m_images[cell->content_index];
729 core::position2d<s32> dest_pos =
730 row_rect.UpperLeftCorner;
731 dest_pos.X += cell->xpos;
732 core::rect<s32> source_rect(
733 core::position2d<s32>(0, 0),
734 image->getOriginalSize());
735 s32 imgh = source_rect.LowerRightCorner.Y;
736 s32 rowh = row_rect.getHeight();
738 dest_pos.Y += (rowh - imgh) / 2;
740 source_rect.LowerRightCorner.Y = rowh;
742 video::SColor color(255, 255, 255, 255);
744 driver->draw2DImage(image, dest_pos, source_rect,
745 &client_clip, color, true);
750 bool GUITable::OnEvent(const SEvent &event)
753 return IGUIElement::OnEvent(event);
755 if (event.EventType == EET_KEY_INPUT_EVENT) {
756 if (event.KeyInput.PressedDown && (
757 event.KeyInput.Key == KEY_DOWN ||
758 event.KeyInput.Key == KEY_UP ||
759 event.KeyInput.Key == KEY_HOME ||
760 event.KeyInput.Key == KEY_END ||
761 event.KeyInput.Key == KEY_NEXT ||
762 event.KeyInput.Key == KEY_PRIOR)) {
764 switch (event.KeyInput.Key) {
772 offset = - (s32) m_visible_rows.size();
775 offset = m_visible_rows.size();
778 offset = AbsoluteRect.getHeight() / m_rowheight;
781 offset = - (s32) (AbsoluteRect.getHeight() / m_rowheight);
786 s32 old_selected = m_selected;
787 s32 rowcount = m_visible_rows.size();
789 m_selected = rangelim(m_selected + offset, 0, rowcount-1);
793 if (m_selected != old_selected)
794 sendTableEvent(0, false);
799 if (event.KeyInput.PressedDown && (
800 event.KeyInput.Key == KEY_LEFT ||
801 event.KeyInput.Key == KEY_RIGHT)) {
802 // Open/close subtree via keyboard
803 if (m_selected >= 0) {
804 int dir = event.KeyInput.Key == KEY_LEFT ? -1 : 1;
805 toggleVisibleTree(m_selected, dir, true);
809 else if (!event.KeyInput.PressedDown && (
810 event.KeyInput.Key == KEY_RETURN ||
811 event.KeyInput.Key == KEY_SPACE)) {
812 sendTableEvent(0, true);
815 else if (event.KeyInput.Key == KEY_ESCAPE ||
816 event.KeyInput.Key == KEY_SPACE) {
819 else if (event.KeyInput.PressedDown && event.KeyInput.Char) {
820 // change selection based on text as it is typed
821 u64 now = porting::getTimeMs();
822 if (now - m_keynav_time >= 500)
823 m_keynav_buffer = L"";
826 // add to key buffer if not a key repeat
827 if (!(m_keynav_buffer.size() == 1 &&
828 m_keynav_buffer[0] == event.KeyInput.Char)) {
829 m_keynav_buffer.append(event.KeyInput.Char);
832 // find the selected item, starting at the current selection
833 // don't change selection if the key buffer matches the current item
834 s32 old_selected = m_selected;
835 s32 start = MYMAX(m_selected, 0);
836 s32 rowcount = m_visible_rows.size();
837 for (s32 k = 1; k < rowcount; ++k) {
838 s32 current = start + k;
839 if (current >= rowcount)
841 if (doesRowStartWith(getRow(current), m_keynav_buffer)) {
842 m_selected = current;
847 if (m_selected != old_selected)
848 sendTableEvent(0, false);
853 if (event.EventType == EET_MOUSE_INPUT_EVENT) {
854 core::position2d<s32> p(event.MouseInput.X, event.MouseInput.Y);
856 if (event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
857 m_scrollbar->setPos(m_scrollbar->getPos() +
858 (event.MouseInput.Wheel < 0 ? -3 : 3) *
859 - (s32) m_rowheight / 2);
863 // Find hovered row and cell
864 bool really_hovering = false;
865 s32 row_i = getRowAt(p.Y, really_hovering);
866 const Cell *cell = NULL;
867 if (really_hovering) {
868 s32 cell_j = getCellAt(p.X, row_i);
870 cell = &(getRow(row_i)->cells[cell_j]);
874 setToolTipText(cell ? m_strings[cell->tooltip_index].c_str() : L"");
876 // Fix for #1567/#1806:
877 // IGUIScrollBar passes double click events to its parent,
878 // which we don't want. Detect this case and discard the event
879 if (event.MouseInput.Event != EMIE_MOUSE_MOVED &&
880 m_scrollbar->isVisible() &&
881 m_scrollbar->isPointInside(p))
884 if (event.MouseInput.isLeftPressed() &&
886 event.MouseInput.Event == EMIE_MOUSE_MOVED)) {
888 bool sel_doubleclick = (event.MouseInput.Event
889 == EMIE_LMOUSE_DOUBLE_CLICK);
890 bool plusminus_clicked = false;
892 // For certain events (left click), report column
893 // Also open/close subtrees when the +/- is clicked
895 event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN ||
896 event.MouseInput.Event == EMIE_LMOUSE_DOUBLE_CLICK ||
897 event.MouseInput.Event == EMIE_LMOUSE_TRIPLE_CLICK)) {
898 sel_column = cell->reported_column;
899 if (cell->content_type == COLUMN_TYPE_TREE)
900 plusminus_clicked = true;
903 if (plusminus_clicked) {
904 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
905 toggleVisibleTree(row_i, 0, false);
910 s32 old_selected = m_selected;
914 if (m_selected != old_selected ||
917 sendTableEvent(sel_column, sel_doubleclick);
920 // Treeview: double click opens/closes trees
921 if (m_has_tree_column && sel_doubleclick) {
922 toggleVisibleTree(m_selected, 0, false);
928 if (event.EventType == EET_GUI_EVENT &&
929 event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED &&
930 event.GUIEvent.Caller == m_scrollbar) {
931 // Don't pass events from our scrollbar to the parent
935 return IGUIElement::OnEvent(event);
938 /******************************************************************************/
939 /* GUITable helper functions */
940 /******************************************************************************/
942 s32 GUITable::allocString(const std::string &text)
944 std::map<std::string, s32>::iterator it = m_alloc_strings.find(text);
945 if (it == m_alloc_strings.end()) {
946 s32 id = m_strings.size();
947 std::wstring wtext = utf8_to_wide(text);
948 m_strings.emplace_back(wtext.c_str());
949 m_alloc_strings.insert(std::make_pair(text, id));
956 s32 GUITable::allocImage(const std::string &imagename)
958 std::map<std::string, s32>::iterator it = m_alloc_images.find(imagename);
959 if (it == m_alloc_images.end()) {
960 s32 id = m_images.size();
961 m_images.push_back(m_tsrc->getTexture(imagename));
962 m_alloc_images.insert(std::make_pair(imagename, id));
969 void GUITable::allocationComplete()
971 // Called when done with creating rows and cells from table data,
972 // i.e. when allocString and allocImage won't be called anymore
973 m_alloc_strings.clear();
974 m_alloc_images.clear();
977 const GUITable::Row* GUITable::getRow(s32 i) const
979 if (i >= 0 && i < (s32) m_visible_rows.size())
980 return &m_rows[m_visible_rows[i]];
985 bool GUITable::doesRowStartWith(const Row *row, const core::stringw &str) const
990 for (s32 j = 0; j < row->cellcount; ++j) {
991 Cell *cell = &row->cells[j];
992 if (cell->content_type == COLUMN_TYPE_TEXT) {
993 const core::stringw &cellstr = m_strings[cell->content_index];
994 if (cellstr.size() >= str.size() &&
995 str.equals_ignore_case(cellstr.subString(0, str.size())))
1002 s32 GUITable::getRowAt(s32 y, bool &really_hovering) const
1004 really_hovering = false;
1006 s32 rowcount = m_visible_rows.size();
1010 // Use arithmetic to find row
1011 s32 rel_y = y - AbsoluteRect.UpperLeftCorner.Y - 1;
1012 s32 i = (rel_y + m_scrollbar->getPos()) / m_rowheight;
1014 if (i >= 0 && i < rowcount) {
1015 really_hovering = true;
1021 return rowcount - 1;
1024 s32 GUITable::getCellAt(s32 x, s32 row_i) const
1026 const Row *row = getRow(row_i);
1030 // Use binary search to find cell in row
1031 s32 rel_x = x - AbsoluteRect.UpperLeftCorner.X - 1;
1033 s32 jmax = row->cellcount - 1;
1034 while (jmin < jmax) {
1035 s32 pivot = jmin + (jmax - jmin) / 2;
1036 assert(pivot >= 0 && pivot < row->cellcount);
1037 const Cell *cell = &row->cells[pivot];
1039 if (rel_x >= cell->xmin && rel_x <= cell->xmax)
1042 if (rel_x < cell->xmin)
1048 if (jmin >= 0 && jmin < row->cellcount &&
1049 rel_x >= row->cells[jmin].xmin &&
1050 rel_x <= row->cells[jmin].xmax)
1056 void GUITable::autoScroll()
1058 if (m_selected >= 0) {
1059 s32 pos = m_scrollbar->getPos();
1060 s32 maxpos = m_selected * m_rowheight;
1061 s32 minpos = maxpos - (AbsoluteRect.getHeight() - m_rowheight);
1063 m_scrollbar->setPos(maxpos);
1064 else if (pos < minpos)
1065 m_scrollbar->setPos(minpos);
1069 void GUITable::updateScrollBar()
1071 s32 totalheight = m_rowheight * m_visible_rows.size();
1072 s32 scrollmax = MYMAX(0, totalheight - AbsoluteRect.getHeight());
1073 m_scrollbar->setVisible(scrollmax > 0);
1074 m_scrollbar->setMax(scrollmax);
1075 m_scrollbar->setSmallStep(m_rowheight);
1076 m_scrollbar->setLargeStep(2 * m_rowheight);
1079 void GUITable::sendTableEvent(s32 column, bool doubleclick)
1081 m_sel_column = column;
1082 m_sel_doubleclick = doubleclick;
1085 memset(&e, 0, sizeof e);
1086 e.EventType = EET_GUI_EVENT;
1087 e.GUIEvent.Caller = this;
1088 e.GUIEvent.Element = 0;
1089 e.GUIEvent.EventType = gui::EGET_TABLE_CHANGED;
1094 void GUITable::getOpenedTrees(std::set<s32> &opened_trees) const
1096 opened_trees.clear();
1097 s32 rowcount = m_rows.size();
1098 for (s32 i = 0; i < rowcount - 1; ++i) {
1099 if (m_rows[i].indent < m_rows[i+1].indent &&
1100 m_rows[i+1].visible_index != -2)
1101 opened_trees.insert(i);
1105 void GUITable::setOpenedTrees(const std::set<s32> &opened_trees)
1107 s32 old_selected = -1;
1108 if (m_selected >= 0)
1109 old_selected = m_visible_rows[m_selected];
1111 std::vector<s32> parents;
1112 std::vector<s32> closed_parents;
1114 m_visible_rows.clear();
1116 for (size_t i = 0; i < m_rows.size(); ++i) {
1117 Row *row = &m_rows[i];
1119 // Update list of ancestors
1120 while (!parents.empty() && m_rows[parents.back()].indent >= row->indent)
1122 while (!closed_parents.empty() &&
1123 m_rows[closed_parents.back()].indent >= row->indent)
1124 closed_parents.pop_back();
1126 assert(closed_parents.size() <= parents.size());
1128 if (closed_parents.empty()) {
1130 row->visible_index = m_visible_rows.size();
1131 m_visible_rows.push_back(i);
1133 else if (parents.back() == closed_parents.back()) {
1134 // Invisible row, direct parent is closed
1135 row->visible_index = -2;
1138 // Invisible row, direct parent is open, some ancestor is closed
1139 row->visible_index = -1;
1142 // If not a leaf, add to parents list
1143 if (i < m_rows.size()-1 && row->indent < m_rows[i+1].indent) {
1144 parents.push_back(i);
1146 s32 content_index = 0; // "-", open
1147 if (opened_trees.count(i) == 0) {
1148 closed_parents.push_back(i);
1149 content_index = 1; // "+", closed
1152 // Update all cells of type "tree"
1153 for (s32 j = 0; j < row->cellcount; ++j)
1154 if (row->cells[j].content_type == COLUMN_TYPE_TREE)
1155 row->cells[j].content_index = content_index;
1161 // m_selected must be updated since it is a visible row index
1162 if (old_selected >= 0)
1163 m_selected = m_rows[old_selected].visible_index;
1166 void GUITable::openTree(s32 to_open)
1168 std::set<s32> opened_trees;
1169 getOpenedTrees(opened_trees);
1170 opened_trees.insert(to_open);
1171 setOpenedTrees(opened_trees);
1174 void GUITable::closeTree(s32 to_close)
1176 std::set<s32> opened_trees;
1177 getOpenedTrees(opened_trees);
1178 opened_trees.erase(to_close);
1179 setOpenedTrees(opened_trees);
1182 // The following function takes a visible row index (hidden rows skipped)
1183 // dir: -1 = left (close), 0 = auto (toggle), 1 = right (open)
1184 void GUITable::toggleVisibleTree(s32 row_i, int dir, bool move_selection)
1186 // Check if the chosen tree is currently open
1187 const Row *row = getRow(row_i);
1191 bool was_open = false;
1192 for (s32 j = 0; j < row->cellcount; ++j) {
1193 if (row->cells[j].content_type == COLUMN_TYPE_TREE) {
1194 was_open = row->cells[j].content_index == 0;
1199 // Check if the chosen tree should be opened
1200 bool do_open = !was_open;
1206 // Close or open the tree; the heavy lifting is done by setOpenedTrees
1207 if (was_open && !do_open)
1208 closeTree(m_visible_rows[row_i]);
1209 else if (!was_open && do_open)
1210 openTree(m_visible_rows[row_i]);
1212 // Change selected row if requested by caller,
1213 // this is useful for keyboard navigation
1214 if (move_selection) {
1216 if (was_open && do_open) {
1217 // Move selection to first child
1218 const Row *maybe_child = getRow(sel + 1);
1219 if (maybe_child && maybe_child->indent > row->indent)
1222 else if (!was_open && !do_open) {
1223 // Move selection to parent
1224 assert(getRow(sel) != NULL);
1225 while (sel > 0 && getRow(sel - 1)->indent >= row->indent)
1228 if (sel < 0) // was root already selected?
1231 if (sel != m_selected) {
1234 sendTableEvent(0, false);
1239 void GUITable::alignContent(Cell *cell, s32 xmax, s32 content_width, s32 align)
1241 // requires that cell.xmin, cell.xmax are properly set
1242 // align = 0: left aligned, 1: centered, 2: right aligned, 3: inline
1244 cell->xpos = cell->xmin;
1247 else if (align == 1) {
1248 cell->xpos = (cell->xmin + xmax - content_width) / 2;
1251 else if (align == 2) {
1252 cell->xpos = xmax - content_width;
1256 // inline alignment: the cells of the column don't have an aligned
1257 // right border, the right border of each cell depends on the content
1258 cell->xpos = cell->xmin;
1259 cell->xmax = cell->xmin + content_width;