Skip to content

Commit 37eb086

Browse files
Added alternative syntax to guiGridListAddRow and guiGridListInsertRowAfter to solve #9007
New syntax to add items simultaneously with rows: int guiGridListAddRow ( element gridList, int/string itemText1, int/string itemText2 ... ) int guiGridListInsertRowAfter ( element gridList, int rowIndex, int/string itemText1, int/string itemText2 ... ) Row headers cannot be added using this method. https://bugs.mtasa.com/view.php?id=9007
1 parent 7fb9436 commit 37eb086

File tree

7 files changed

+126
-16
lines changed

7 files changed

+126
-16
lines changed

MTA10/gui/CGUIGridList_Impl.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,42 @@ void CGUIGridList_Impl::ForceUpdate ( void )
182182
reinterpret_cast < CEGUI::MultiColumnList* > ( m_pWindow ) -> forceUpdate();
183183
}
184184

185-
int CGUIGridList_Impl::AddRow ( bool fast )
185+
186+
int CGUIGridList_Impl::AddRow(bool fast, std::vector < pair<SString, bool> > *m_items)
186187
{
187188
try
188189
{
189-
return reinterpret_cast < CEGUI::MultiColumnList* > ( m_pWindow ) -> addRow ( m_iIndex++, fast );
190+
int iRow = reinterpret_cast < CEGUI::MultiColumnList* > ( m_pWindow ) -> addRow ( m_iIndex++, fast );
191+
if (m_items)
192+
return SetRowItemsText(iRow, m_items);
193+
else
194+
return iRow;
190195
}
191196
catch ( CEGUI::Exception ) {
192197
return -1;
193198
}
194199
}
195200

201+
int CGUIGridList_Impl::SetRowItemsText(int iRow, std::vector < pair<SString, bool> > *m_items)
202+
{
203+
int iSortColumn = reinterpret_cast < CEGUI::MultiColumnList* > (m_pWindow)->getSortColumn() + 1; // MTA columns start at 1, CEGUI at 0
204+
205+
std::vector < pair<SString, bool> >::iterator it = m_items->begin();
206+
int col = 1;
207+
for (it; it != m_items->end(); it++)
208+
{
209+
// Loop through every item. We add each one unless it's the sorted by column, which we do last
210+
if (col != iSortColumn)
211+
SetItemText(iRow, col, it->first.c_str(), it->second, false, true);
212+
col++;
213+
}
214+
// Add our sort column text, if it was provided
215+
if ((uint)iSortColumn <= m_items->size())
216+
iRow = SetItemText(iRow, iSortColumn, m_items->at(iSortColumn - 1).first.c_str(), m_items->at(iSortColumn - 1).second, false, true);
217+
218+
return iRow;
219+
}
220+
196221

197222
void CGUIGridList_Impl::RemoveRow ( int iRow )
198223
{
@@ -204,18 +229,21 @@ void CGUIGridList_Impl::RemoveRow ( int iRow )
204229
}
205230

206231

207-
int CGUIGridList_Impl::InsertRowAfter ( int iRow )
232+
int CGUIGridList_Impl::InsertRowAfter(int iRow, std::vector < pair<SString, bool> > *m_items)
208233
{
209234
try
210235
{
211-
return reinterpret_cast < CEGUI::MultiColumnList* > ( m_pWindow ) -> insertRow ( iRow+1, m_iIndex++ );
236+
int iNewRow = reinterpret_cast < CEGUI::MultiColumnList* > (m_pWindow)->insertRow(iRow + 1, m_iIndex++);
237+
if (m_items)
238+
return SetRowItemsText(iNewRow, m_items);
239+
else
240+
return iNewRow;
212241
}
213242
catch ( CEGUI::Exception ) {
214243
return -1;
215244
}
216245
}
217246

218-
219247
void CGUIGridList_Impl::AutoSizeColumn ( unsigned int hColumn )
220248
{
221249
try

MTA10/gui/CGUIGridList_Impl.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ class CGUIGridList_Impl : public CGUIGridList, public CGUIElement_Impl
3636
const char* GetColumnTitle ( int hColumn );
3737

3838
void SetSelectionMode ( SelectionMode mode );
39+
int SetRowItemsText ( int iRow, std::vector < pair<SString,bool> > *m_items );
3940

4041
void ForceUpdate ( void );
41-
int AddRow ( bool bFast = false );
42+
int AddRow ( bool bFast = false, std::vector < pair<SString,bool> > *m_items = NULL );
4243
void RemoveRow ( int iRow );
43-
int InsertRowAfter ( int iRow );
44+
int InsertRowAfter ( int iRow, std::vector < pair<SString, bool> > *m_items = NULL );
4445
void Clear ( void );
4546
CGUIListItem* GetItem ( int iRow, int hColumn );
4647
const char* GetItemText ( int iRow, int hColumn );

MTA10/mods/deathmatch/logic/CStaticFunctionDefinitions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,8 @@ class CStaticFunctionDefinitions
450450
static inline void GUIGridListSetColumnTitle ( CClientGUIElement& GUIElement, unsigned int uiColumn, const char *szTitle ) { static_cast < CGUIGridList* > ( GUIElement.GetCGUIElement () ) -> SetColumnTitle ( uiColumn, szTitle ); };
451451
//static const char* GUIGridListGetColumnTitle ( CClientGUIElement& GUIElement, int iColumn );
452452
static void GUIGridListSetScrollBars ( CClientEntity& Element, bool bH, bool bV );
453-
static inline int GUIGridListAddRow ( CClientGUIElement& GUIElement, bool bFast ) { return static_cast < CGUIGridList* > ( GUIElement.GetCGUIElement () ) -> AddRow ( bFast ); };
454-
static inline int GUIGridListInsertRowAfter ( CClientGUIElement& GUIElement, int iRow ) { return static_cast < CGUIGridList* > ( GUIElement.GetCGUIElement () ) -> InsertRowAfter ( iRow ); };
453+
static inline int GUIGridListAddRow (CClientGUIElement& GUIElement, bool bFast, std::vector < pair<SString, bool> > *m_items = NULL) { return static_cast < CGUIGridList* > (GUIElement.GetCGUIElement())->AddRow(bFast,m_items); };
454+
static inline int GUIGridListInsertRowAfter ( CClientGUIElement& GUIElement, int iRow, std::vector < pair<SString, bool> > *m_items = NULL ) { return static_cast < CGUIGridList* > ( GUIElement.GetCGUIElement () ) -> InsertRowAfter ( iRow, m_items ); };
455455
static inline void GUIGridListRemoveRow ( CClientGUIElement& GUIElement, int iRow ) { static_cast < CGUIGridList* > ( GUIElement.GetCGUIElement () ) -> RemoveRow ( iRow ); };
456456
static inline void GUIGridListAutoSizeColumn ( CClientGUIElement& GUIElement, unsigned int uiColumn ) { static_cast < CGUIGridList* > ( GUIElement.GetCGUIElement () ) -> AutoSizeColumn ( uiColumn ); };
457457
static void GUIGridListClear ( CClientEntity& Element );

MTA10/mods/shared_logic/lua/CLuaArgument.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ using namespace std;
3939
CLuaArgument::CLuaArgument ( void )
4040
{
4141
m_iType = LUA_TNIL;
42+
m_iIndex = -1;
4243
m_pTableData = NULL;
4344
m_pUserData = NULL;
4445
}
@@ -63,6 +64,7 @@ CLuaArgument::CLuaArgument ( lua_State* luaVM, int iArgument, CFastHashMap < con
6364
{
6465
// Read the argument out of the lua VM
6566
m_pTableData = NULL;
67+
m_iIndex = iArgument;
6668
Read ( luaVM, iArgument, pKnownTables );
6769
}
6870

MTA10/mods/shared_logic/lua/CLuaArgument.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class CLuaArgument
5555
void Push ( lua_State* luaVM, CFastHashMap < CLuaArguments*, int > * pKnownTables = NULL ) const;
5656

5757
inline int GetType ( void ) const { return m_iType; };
58+
inline int GetIndex(void) const { return m_iIndex; };
5859

5960
inline bool GetBoolean ( void ) const { return m_bBoolean; };
6061
inline lua_Number GetNumber ( void ) const { return m_Number; };
@@ -72,6 +73,7 @@ class CLuaArgument
7273
void LogUnableToPacketize ( const char* szMessage ) const;
7374

7475
int m_iType;
76+
int m_iIndex;
7577
bool m_bBoolean;
7678
lua_Number m_Number;
7779
SString m_strString;

MTA10/mods/shared_logic/lua/CLuaFunctionDefs.GUI.cpp

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,18 +1696,56 @@ int CLuaFunctionDefs::GUIGridListGetColumnTitle ( lua_State* luaVM )
16961696
int CLuaFunctionDefs::GUIGridListAddRow ( lua_State* luaVM )
16971697
{
16981698
// int guiGridListAddRow ( element gridList )
1699+
// int guiGridListAddRow ( element gridList, int/string itemText1, int/string itemText2 ... )
16991700
CClientGUIElement* guiGridlist;
1701+
CLuaArguments Arguments;
17001702

17011703
CScriptArgReader argStream ( luaVM );
17021704
argStream.ReadUserData < CGUIGridList > ( guiGridlist );
1705+
argStream.ReadLuaArguments(Arguments);
17031706

17041707
if ( !argStream.HasErrors () )
17051708
{
1706-
int iRet = CStaticFunctionDefinitions::GUIGridListAddRow ( *guiGridlist, true );
1707-
if ( iRet >= 0 )
1709+
int iRet = 0;
1710+
if (Arguments.Count() == 0)
1711+
iRet = CStaticFunctionDefinitions::GUIGridListAddRow(*guiGridlist, true);
1712+
else
17081713
{
1709-
m_pGUIManager->QueueGridListUpdate ( guiGridlist );
1710-
lua_pushnumber ( luaVM, iRet );
1714+
// Vector containing our string arguments. We add a bool to store whether it was originally a number.
1715+
std::vector < pair<SString,bool> > m_items;
1716+
std::vector < CLuaArgument* > ::const_iterator it = Arguments.IterBegin();
1717+
for (it; it != Arguments.IterEnd(); it++)
1718+
{
1719+
CLuaArgument* pArgument = *it;
1720+
SString strItemText;
1721+
bool bNumber = false;
1722+
1723+
// Check the type of the argument and convert it to a string we can process
1724+
uint type = pArgument->GetType();
1725+
if (type == LUA_TNUMBER)
1726+
{
1727+
// Grab the lua string and its size
1728+
const char* szLuaString = lua_tostring(luaVM, pArgument->GetIndex());
1729+
size_t sizeLuaString = lua_strlen(luaVM, pArgument->GetIndex());
1730+
1731+
// Set our string
1732+
strItemText.assign(szLuaString, sizeLuaString);
1733+
bNumber = true;
1734+
}
1735+
else if ( type == LUA_TSTRING )
1736+
strItemText = pArgument->GetString();
1737+
else
1738+
continue;
1739+
1740+
m_items.push_back(std::make_pair(strItemText,bNumber));
1741+
}
1742+
iRet = CStaticFunctionDefinitions::GUIGridListAddRow(*guiGridlist, true, &m_items);
1743+
}
1744+
1745+
if (iRet >= 0)
1746+
{
1747+
m_pGUIManager->QueueGridListUpdate(guiGridlist);
1748+
lua_pushnumber(luaVM, iRet);
17111749
return 1;
17121750
}
17131751
}
@@ -1723,15 +1761,54 @@ int CLuaFunctionDefs::GUIGridListAddRow ( lua_State* luaVM )
17231761
int CLuaFunctionDefs::GUIGridListInsertRowAfter ( lua_State* luaVM )
17241762
{
17251763
// int guiGridListInsertRowAfter ( element gridList, int rowIndex )
1764+
// int guiGridListInsertRowAfter ( element gridList, int rowIndex, int/string itemText1, int/string itemText2 ... )
17261765
CClientGUIElement* guiGridlist; int rowIndex;
1766+
CLuaArguments Arguments;
17271767

17281768
CScriptArgReader argStream ( luaVM );
17291769
argStream.ReadUserData < CGUIGridList > ( guiGridlist );
17301770
argStream.ReadNumber ( rowIndex );
1771+
argStream.ReadLuaArguments(Arguments);
17311772

17321773
if ( !argStream.HasErrors () )
17331774
{
1734-
int iRet = CStaticFunctionDefinitions::GUIGridListInsertRowAfter ( *guiGridlist, rowIndex );
1775+
int iRet = 0;
1776+
if (Arguments.Count() == 0)
1777+
iRet = CStaticFunctionDefinitions::GUIGridListInsertRowAfter ( *guiGridlist, rowIndex );
1778+
else
1779+
{
1780+
// Vector containing our string arguments. We add a bool to store whether it was originally a number.
1781+
std::vector < pair<SString, bool> > m_items;
1782+
std::vector < CLuaArgument* > ::const_iterator it = Arguments.IterBegin();
1783+
for (it; it != Arguments.IterEnd(); it++)
1784+
{
1785+
CLuaArgument* pArgument = *it;
1786+
SString strItemText;
1787+
bool bNumber = false;
1788+
1789+
// Check the type of the argument and convert it to a string we can process
1790+
uint type = pArgument->GetType();
1791+
if (type == LUA_TNUMBER)
1792+
{
1793+
// Grab the lua string and its size
1794+
const char* szLuaString = lua_tostring(luaVM, pArgument->GetIndex());
1795+
size_t sizeLuaString = lua_strlen(luaVM, pArgument->GetIndex());
1796+
1797+
// Set our string
1798+
strItemText.assign(szLuaString, sizeLuaString);
1799+
bNumber = true;
1800+
}
1801+
else if (type == LUA_TSTRING)
1802+
strItemText = pArgument->GetString();
1803+
else
1804+
continue;
1805+
1806+
m_items.push_back(std::make_pair(strItemText, bNumber));
1807+
}
1808+
iRet = CStaticFunctionDefinitions::GUIGridListInsertRowAfter(*guiGridlist, rowIndex, &m_items);
1809+
}
1810+
1811+
17351812
if ( iRet >= 0 )
17361813
{
17371814
lua_pushnumber ( luaVM, iRet );

MTA10/sdk/gui/CGUIGridList.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ class CGUIGridList : public CGUIElement
6262
virtual void SetSelectionMode ( SelectionMode mode ) = 0;
6363

6464
virtual void ForceUpdate ( void ) = 0;
65-
virtual int AddRow ( bool fast = false ) = 0;
65+
virtual int AddRow ( bool bFast = false, std::vector < pair<SString,bool> > *m_items = NULL ) = 0;
6666
virtual void RemoveRow ( int iRow ) = 0;
67-
virtual int InsertRowAfter ( int iRow ) = 0;
67+
virtual int InsertRowAfter ( int iRow, std::vector < pair<SString,bool> > *m_items = NULL ) = 0;
6868
virtual void Clear ( void ) = 0;
6969
virtual CGUIListItem* GetItem ( int iRow, int hColumn ) = 0;
7070
virtual const char* GetItemText ( int iRow, int hColumn ) = 0;

0 commit comments

Comments
 (0)