LCOV - code coverage report
Current view: top level - source/gui/ObjectTypes - COList.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 244 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 7 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "COList.h"
      21             : 
      22             : #include "gui/CGUI.h"
      23             : #include "gui/CGUIText.h"
      24             : #include "gui/IGUIScrollBar.h"
      25             : #include "gui/ObjectBases/IGUIScrollBarOwner.h"
      26             : #include "gui/SGUIMessage.h"
      27             : #include "gui/SettingTypes/CGUIColor.h"
      28             : #include "gui/SettingTypes/CGUIList.h"
      29             : #include "gui/SettingTypes/CGUIString.h"
      30             : #include "i18n/L10n.h"
      31             : #include "lib/debug.h"
      32             : #include "maths/Rect.h"
      33             : #include "maths/Size2D.h"
      34             : #include "maths/Vector2D.h"
      35             : #include "ps/CLogger.h"
      36             : #include "ps/XMB/XMBData.h"
      37             : #include "ps/XML/Xeromyces.h"
      38             : 
      39             : #include <algorithm>
      40             : #include <cstddef>
      41             : #include <string_view>
      42             : #include <utility>
      43             : 
      44             : const float SORT_SPRITE_DIM = 16.0f;
      45             : const CVector2D COLUMN_SHIFT = CVector2D(0, 4);
      46             : 
      47             : const CStr COList::EventNameSelectionColumnChange = "SelectionColumnChange";
      48             : 
      49           0 : COList::COList(CGUI& pGUI)
      50             :     : CList(pGUI),
      51           0 :       m_SpriteHeading(this, "sprite_heading"),
      52           0 :       m_Sortable(this, "sortable"), // The actual sorting is done in JS for more versatility
      53           0 :       m_SelectedColumn(this, "selected_column"),
      54           0 :       m_SelectedColumnOrder(this, "selected_column_order"),
      55           0 :       m_SpriteAsc(this, "sprite_asc"), // Show the order of sorting
      56           0 :       m_SpriteDesc(this, "sprite_desc"),
      57           0 :       m_SpriteNotSorted(this, "sprite_not_sorted")
      58             : {
      59           0 : }
      60             : 
      61           0 : void COList::SetupText()
      62             : {
      63           0 :     m_ItemsYPositions.resize(m_List->m_Items.size() + 1);
      64             : 
      65             :     // Delete all generated texts. Some could probably be saved,
      66             :     //  but this is easier, and this function will never be called
      67             :     //  continuously, or even often, so it'll probably be okay.
      68           0 :     m_GeneratedTexts.clear();
      69             : 
      70           0 :     m_TotalAvailableColumnWidth = GetListRect().GetWidth();
      71             :     // remove scrollbar if applicable
      72           0 :     if (m_ScrollBar && GetScrollBar(0).GetStyle())
      73           0 :         m_TotalAvailableColumnWidth -= GetScrollBar(0).GetStyle()->m_Width;
      74             : 
      75           0 :     m_HeadingHeight = SORT_SPRITE_DIM; // At least the size of the sorting sprite
      76             : 
      77           0 :     for (const COListColumn& column : m_Columns)
      78             :     {
      79           0 :         float width = column.m_Width;
      80           0 :         if (column.m_Width > 0 && column.m_Width < 1)
      81           0 :             width *= m_TotalAvailableColumnWidth;
      82             : 
      83           0 :         CGUIString gui_string;
      84           0 :         gui_string.SetValue(column.m_Heading);
      85             : 
      86           0 :         const CGUIText& text = AddText(gui_string, m_Font, width, m_BufferZone);
      87           0 :         m_HeadingHeight = std::max(m_HeadingHeight, text.GetSize().Height + COLUMN_SHIFT.Y);
      88           0 :     }
      89             : 
      90             :     // Generate texts
      91             :     float buffered_y = 0.f;
      92             : 
      93           0 :     for (size_t i = 0; i < m_List->m_Items.size(); ++i)
      94             :     {
      95           0 :         m_ItemsYPositions[i] = buffered_y;
      96           0 :         float shift = 0.0f;
      97           0 :         for (const COListColumn& column : m_Columns)
      98             :         {
      99           0 :             float width = column.m_Width;
     100           0 :             if (column.m_Width > 0 && column.m_Width < 1)
     101           0 :                 width *= m_TotalAvailableColumnWidth;
     102             : 
     103           0 :             CGUIText* text;
     104           0 :             if (!column.m_List->m_Items[i].GetOriginalString().empty())
     105           0 :                 text = &AddText(column.m_List->m_Items[i], m_Font, width, m_BufferZone);
     106             :             else
     107             :             {
     108             :                 // Minimum height of a space character of the current font size
     109           0 :                 CGUIString align_string;
     110           0 :                 align_string.SetValue(L" ");
     111           0 :                 text = &AddText(align_string, m_Font, width, m_BufferZone);
     112           0 :             }
     113           0 :             shift = std::max(shift, text->GetSize().Height);
     114             :         }
     115           0 :         buffered_y += shift;
     116             :     }
     117             : 
     118           0 :     m_ItemsYPositions[m_List->m_Items.size()] = buffered_y;
     119             : 
     120           0 :     if (m_ScrollBar)
     121             :     {
     122           0 :         CRect rect = GetListRect();
     123           0 :         GetScrollBar(0).SetScrollRange(m_ItemsYPositions.back());
     124           0 :         GetScrollBar(0).SetScrollSpace(rect.GetHeight());
     125             : 
     126           0 :         GetScrollBar(0).SetX(rect.right);
     127           0 :         GetScrollBar(0).SetY(rect.top);
     128           0 :         GetScrollBar(0).SetZ(GetBufferedZ());
     129           0 :         GetScrollBar(0).SetLength(rect.bottom - rect.top);
     130             :     }
     131           0 : }
     132             : 
     133           0 : CRect COList::GetListRect() const
     134             : {
     135           0 :     return GetActualSize() + CRect(0, m_HeadingHeight, 0, 0);
     136             : }
     137             : 
     138           0 : void COList::HandleMessage(SGUIMessage& Message)
     139             : {
     140           0 :     CList::HandleMessage(Message);
     141             : 
     142           0 :     switch (Message.type)
     143             :     {
     144             : 
     145           0 :     case GUIM_SETTINGS_UPDATED:
     146           0 :     {
     147           0 :         if (Message.value.find("heading_") == 0)
     148           0 :             SetupText();
     149             :         break;
     150             :     }
     151             : 
     152             :     // If somebody clicks on the column heading
     153           0 :     case GUIM_MOUSE_PRESS_LEFT:
     154           0 :     {
     155           0 :         if (!m_Sortable)
     156             :             return;
     157             : 
     158           0 :         const CVector2D& mouse = m_pGUI.GetMousePos();
     159           0 :         if (!GetActualSize().PointInside(mouse))
     160             :             return;
     161             : 
     162           0 :         float xpos = 0;
     163           0 :         for (const COListColumn& column : m_Columns)
     164             :         {
     165           0 :             if (column.m_Hidden)
     166           0 :                 continue;
     167             : 
     168           0 :             float width = column.m_Width;
     169             :             // Check if it's a decimal value, and if so, assume relative positioning.
     170           0 :             if (column.m_Width < 1 && column.m_Width > 0)
     171           0 :                 width *= m_TotalAvailableColumnWidth;
     172           0 :             CVector2D leftTopCorner = GetActualSize().TopLeft() + CVector2D(xpos, 0);
     173           0 :             if (mouse.X >= leftTopCorner.X &&
     174           0 :                 mouse.X < leftTopCorner.X + width &&
     175           0 :                 mouse.Y < leftTopCorner.Y + m_HeadingHeight)
     176             :             {
     177           0 :                 if (column.m_Id != static_cast<CStr>(m_SelectedColumn))
     178             :                 {
     179           0 :                     m_SelectedColumnOrder.Set(column.m_SortOrder, true);
     180           0 :                     CStr selected_column = column.m_Id;
     181           0 :                     m_SelectedColumn.Set(selected_column, true);
     182           0 :                 }
     183             :                 else
     184           0 :                     m_SelectedColumnOrder.Set(-m_SelectedColumnOrder, true);
     185             : 
     186           0 :                 ScriptEvent(EventNameSelectionColumnChange);
     187           0 :                 PlaySound(m_SoundSelected);
     188           0 :                 return;
     189             :             }
     190           0 :             xpos += width;
     191             :         }
     192             :         return;
     193             :     }
     194             :     default:
     195             :         return;
     196             :     }
     197             : }
     198             : 
     199           0 : bool COList::HandleAdditionalChildren(const XMBData& xmb, const XMBElement& child)
     200             : {
     201             :     #define ELMT(x) int elmt_##x = xmb.GetElementID(#x)
     202             :     #define ATTR(x) int attr_##x = xmb.GetAttributeID(#x)
     203           0 :     ELMT(item);
     204           0 :     ELMT(column);
     205           0 :     ELMT(translatableAttribute);
     206           0 :     ATTR(id);
     207           0 :     ATTR(context);
     208             : 
     209           0 :     if (child.GetNodeName() == elmt_item)
     210             :     {
     211           0 :         CGUIString vlist;
     212           0 :         vlist.SetValue(child.GetText().FromUTF8());
     213           0 :         AddItem(vlist, vlist);
     214           0 :         return true;
     215           0 :     }
     216           0 :     else if (child.GetNodeName() == elmt_column)
     217             :     {
     218           0 :         CStr id;
     219           0 :         for (XMBAttribute attr : child.GetAttributes())
     220             :         {
     221           0 :             if (attr.Name == attr_id)
     222           0 :                 id = attr.Value;
     223           0 :         }
     224             : 
     225           0 :         COListColumn column(this, id);
     226             : 
     227           0 :         for (XMBAttribute attr : child.GetAttributes())
     228             :         {
     229           0 :             std::string_view attr_name(xmb.GetAttributeStringView(attr.Name));
     230           0 :             CStr attr_value(attr.Value);
     231             : 
     232           0 :             if (attr_name == "textcolor")
     233             :             {
     234           0 :                 if (!CGUI::ParseString<CGUIColor>(&m_pGUI, attr_value.FromUTF8(), column.m_TextColor))
     235             :                 {
     236           0 :                     LOGERROR("GUI: Error parsing '{}' (\"{}\")", attr_name.data(),
     237             :                         attr_value);
     238             :                 }
     239             :             }
     240           0 :             else if (attr_name == "textcolor_selected")
     241             :             {
     242           0 :                 if (!CGUI::ParseString<CGUIColor>(&m_pGUI, attr_value.FromUTF8(), column.m_TextColorSelected))
     243             :                 {
     244           0 :                     LOGERROR("GUI: Error parsing '{}' (\"{}\")", attr_name.data(),
     245             :                         attr_value);
     246             :                 }
     247             :             }
     248           0 :             else if (attr_name == "hidden")
     249             :             {
     250           0 :                 bool hidden = false;
     251           0 :                 if (!CGUI::ParseString<bool>(&m_pGUI, attr_value.FromUTF8(), hidden))
     252             :                 {
     253           0 :                     LOGERROR("GUI: Error parsing '{}' (\"{}\")", attr_name.data(),
     254             :                         attr_value);
     255             :                 }
     256             :                 else
     257           0 :                     column.m_Hidden.Set(hidden, false);
     258             :             }
     259           0 :             else if (attr_name == "width")
     260             :             {
     261           0 :                 float width;
     262           0 :                 if (!CGUI::ParseString<float>(&m_pGUI, attr_value.FromUTF8(), width))
     263             :                 {
     264           0 :                     LOGERROR("GUI: Error parsing '{}' (\"{}\")", attr_name.data(),
     265             :                         attr_value);
     266             :                 }
     267             :                 else
     268             :                 {
     269             :                     // Check if it's a relative value, and save as decimal if so.
     270           0 :                     if (attr_value.find("%") != std::string::npos)
     271           0 :                         width = width / 100.f;
     272           0 :                     column.m_Width = width;
     273             :                 }
     274             :             }
     275           0 :             else if (attr_name == "heading")
     276             :             {
     277           0 :                 column.m_Heading.Set(attr_value.FromUTF8(), false);
     278             :             }
     279           0 :             else if (attr_name == "sort_order")
     280             :             {
     281           0 :                 column.m_SortOrder.Set(attr_value == "desc" ? -1 : 1, false);
     282             :             }
     283           0 :         }
     284             : 
     285           0 :         for (XMBElement grandchild : child.GetChildNodes())
     286             :         {
     287           0 :             if (grandchild.GetNodeName() != elmt_translatableAttribute)
     288           0 :                 continue;
     289             : 
     290           0 :             CStr attributeName(grandchild.GetAttributes().GetNamedItem(attr_id));
     291             :             // only the heading is translatable for list column
     292           0 :             if (attributeName.empty() || attributeName != "heading")
     293             :             {
     294           0 :                 LOGERROR("GUI: translatable attribute in olist column that isn't a heading. "
     295             :                     "(object: {})", this->GetPresentableName());
     296           0 :                 continue;
     297           0 :             }
     298             : 
     299           0 :             CStr value(grandchild.GetText());
     300           0 :             if (value.empty())
     301           0 :                 continue;
     302             : 
     303           0 :             CStr context(grandchild.GetAttributes().GetNamedItem(attr_context)); // Read the context if any.
     304           0 :             if (!context.empty())
     305             :             {
     306           0 :                 CStr translatedValue(g_L10n.TranslateWithContext(context, value));
     307           0 :                 column.m_Heading.Set(translatedValue.FromUTF8(), false);
     308           0 :             }
     309             :             else
     310             :             {
     311           0 :                 CStr translatedValue(g_L10n.Translate(value));
     312           0 :                 column.m_Heading.Set(translatedValue.FromUTF8(), false);
     313           0 :             }
     314           0 :         }
     315             : 
     316           0 :         m_Columns.emplace_back(std::move(column));
     317           0 :         return true;
     318           0 :     }
     319             : 
     320             :     return false;
     321             : }
     322             : 
     323           0 : void COList::AdditionalChildrenHandled()
     324             : {
     325           0 :     SetupText();
     326           0 : }
     327             : 
     328           0 : void COList::DrawList(CCanvas2D& canvas, const int& selected, const CGUISpriteInstance& sprite, const CGUISpriteInstance& spriteOverlay,
     329             :                       const CGUISpriteInstance& spriteSelectArea, const CGUISpriteInstance& spriteSelectAreaOverlay, const CGUIColor& textColor)
     330             : {
     331           0 :     CRect rect = GetListRect();
     332             : 
     333           0 :     m_pGUI.DrawSprite(sprite, canvas, rect, m_VisibleArea);
     334             : 
     335           0 :     float scroll = 0.f;
     336           0 :     if (m_ScrollBar)
     337           0 :         scroll = GetScrollBar(0).GetPos();
     338             : 
     339           0 :     bool drawSelected = false;
     340           0 :     CRect rectSel;
     341             :     // Draw item selection
     342           0 :     if (selected != -1)
     343             :     {
     344           0 :         ENSURE(selected >= 0 && selected+1 < (int)m_ItemsYPositions.size());
     345             : 
     346             :         // Get rectangle of selection:
     347           0 :         rectSel = CRect(
     348           0 :             rect.left, rect.top + m_ItemsYPositions[selected] - scroll,
     349           0 :             rect.right, rect.top + m_ItemsYPositions[selected+1] - scroll);
     350             : 
     351           0 :         if (rectSel.top <= rect.bottom &&
     352           0 :             rectSel.bottom >= rect.top)
     353             :         {
     354           0 :             if (rectSel.bottom > rect.bottom)
     355           0 :                 rectSel.bottom = rect.bottom;
     356           0 :             if (rectSel.top < rect.top)
     357           0 :                 rectSel.top = rect.top;
     358             : 
     359           0 :             if (m_ScrollBar)
     360             :             {
     361             :                 // Remove any overlapping area of the scrollbar.
     362           0 :                 if (rectSel.right > GetScrollBar(0).GetOuterRect().left &&
     363           0 :                     rectSel.right <= GetScrollBar(0).GetOuterRect().right)
     364           0 :                     rectSel.right = GetScrollBar(0).GetOuterRect().left;
     365             : 
     366           0 :                 if (rectSel.left >= GetScrollBar(0).GetOuterRect().left &&
     367           0 :                     rectSel.left < GetScrollBar(0).GetOuterRect().right)
     368           0 :                     rectSel.left = GetScrollBar(0).GetOuterRect().right;
     369             :             }
     370             : 
     371             :             // Draw item selection
     372           0 :             m_pGUI.DrawSprite(spriteSelectArea, canvas, rectSel, m_VisibleArea);
     373             :             drawSelected = true;
     374             :         }
     375             :     }
     376             : 
     377             :     // Draw line above column header
     378           0 :     CRect rect_head(GetActualSize().left, GetActualSize().top, GetActualSize().right,
     379           0 :                                     GetActualSize().top + m_HeadingHeight);
     380           0 :     m_pGUI.DrawSprite(m_SpriteHeading, canvas, rect_head, m_VisibleArea);
     381             : 
     382             :     // Draw column headers
     383           0 :     float xpos = 0;
     384           0 :     size_t col = 0;
     385           0 :     for (const COListColumn& column : m_Columns)
     386             :     {
     387           0 :         if (column.m_Hidden)
     388             :         {
     389           0 :             ++col;
     390           0 :             continue;
     391             :         }
     392             : 
     393             :         // Check if it's a decimal value, and if so, assume relative positioning.
     394           0 :         float width = column.m_Width;
     395           0 :         if (column.m_Width < 1 && column.m_Width > 0)
     396           0 :             width *= m_TotalAvailableColumnWidth;
     397             : 
     398           0 :         CVector2D leftTopCorner = GetActualSize().TopLeft() + CVector2D(xpos, 0);
     399             : 
     400             :         // Draw sort arrows in colum header
     401           0 :         if (m_Sortable)
     402             :         {
     403           0 :             const CGUISpriteInstance* pSprite;
     404           0 :             if (*m_SelectedColumn == column.m_Id)
     405             :             {
     406           0 :                 if (m_SelectedColumnOrder == 0)
     407           0 :                     LOGERROR("selected_column_order must not be 0");
     408             : 
     409           0 :                 if (m_SelectedColumnOrder != -1)
     410           0 :                     pSprite = &*m_SpriteAsc;
     411             :                 else
     412           0 :                     pSprite = &*m_SpriteDesc;
     413             :             }
     414             :             else
     415           0 :                 pSprite = &*m_SpriteNotSorted;
     416             : 
     417           0 :             m_pGUI.DrawSprite(*pSprite, canvas, CRect(leftTopCorner + CVector2D(width - SORT_SPRITE_DIM, 0), leftTopCorner + CVector2D(width, SORT_SPRITE_DIM)), m_VisibleArea);
     418             :         }
     419             : 
     420             :         // Draw column header text
     421           0 :         DrawText(canvas, col, textColor, leftTopCorner + COLUMN_SHIFT, rect_head);
     422           0 :         xpos += width;
     423           0 :         ++col;
     424             :     }
     425             : 
     426             :     // Draw list items for each column
     427           0 :     const size_t objectsCount = m_Columns.size();
     428           0 :     for (size_t i = 0; i < m_List->m_Items.size(); ++i)
     429             :     {
     430           0 :         if (m_ItemsYPositions[i+1] - scroll < 0 ||
     431           0 :             m_ItemsYPositions[i] - scroll > rect.GetHeight())
     432           0 :             continue;
     433             : 
     434           0 :         const float rowHeight = m_ItemsYPositions[i+1] - m_ItemsYPositions[i];
     435             : 
     436             :         // Clipping area (we'll have to substract the scrollbar)
     437           0 :         CRect cliparea = GetListRect();
     438             : 
     439           0 :         if (m_ScrollBar)
     440             :         {
     441           0 :             if (cliparea.right > GetScrollBar(0).GetOuterRect().left &&
     442           0 :                 cliparea.right <= GetScrollBar(0).GetOuterRect().right)
     443           0 :                 cliparea.right = GetScrollBar(0).GetOuterRect().left;
     444             : 
     445           0 :             if (cliparea.left >= GetScrollBar(0).GetOuterRect().left &&
     446           0 :                 cliparea.left < GetScrollBar(0).GetOuterRect().right)
     447           0 :                 cliparea.left = GetScrollBar(0).GetOuterRect().right;
     448             :         }
     449             : 
     450             :         // Draw all items for that column
     451             :         xpos = 0;
     452           0 :         for (size_t colIdx = 0; colIdx < m_Columns.size(); ++colIdx)
     453             :         {
     454           0 :             const COListColumn& column = m_Columns[colIdx];
     455           0 :             if (column.m_Hidden)
     456           0 :                 continue;
     457             : 
     458             :             // Determine text position and width
     459           0 :             const CVector2D textPos = rect.TopLeft() + CVector2D(xpos, -scroll + m_ItemsYPositions[i]);
     460             : 
     461           0 :             float width = column.m_Width;
     462             :             // Check if it's a decimal value, and if so, assume relative positioning.
     463           0 :             if (column.m_Width < 1 && column.m_Width > 0)
     464           0 :                 width *= m_TotalAvailableColumnWidth;
     465             : 
     466             :             // Clip text to the column (to prevent drawing text into the neighboring column)
     467           0 :             CRect cliparea2 = cliparea;
     468           0 :             cliparea2.right = std::min(cliparea2.right, textPos.X + width);
     469           0 :             cliparea2.bottom = std::min(cliparea2.bottom, textPos.Y + rowHeight);
     470             : 
     471           0 :             const CGUIColor& finalTextColor = (drawSelected && static_cast<size_t>(selected) == i && column.m_TextColorSelected) ? column.m_TextColorSelected : column.m_TextColor;
     472             : 
     473             :             // Draw list item
     474           0 :             DrawText(canvas, objectsCount * (i +/*Heading*/1) + colIdx, finalTextColor, textPos, cliparea2);
     475           0 :             xpos += width;
     476             :         }
     477             :     }
     478             : 
     479             :     // Draw scrollbars on top of the content
     480           0 :     if (m_ScrollBar)
     481           0 :         IGUIScrollBarOwner::Draw(canvas);
     482             : 
     483             :     // Draw the overlays last
     484           0 :     m_pGUI.DrawSprite(spriteOverlay, canvas, rect, m_VisibleArea);
     485           0 :     if (drawSelected)
     486           0 :         m_pGUI.DrawSprite(spriteSelectAreaOverlay, canvas, rectSel, m_VisibleArea);
     487           0 : }

Generated by: LCOV version 1.16