Skip to content

Commit e901b4e

Browse files
committed
Added filter-prepare function to utils.filterpicker
1 parent 863e926 commit e901b4e

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

dialogs/FunctionListDlg.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ CFunctionListDlg::CFunctionListDlg(CWnd* pParent /*=NULL*/)
2727
m_bNoSort = false;
2828
m_L = NULL;
2929
m_iFilterIndex = LUA_NOREF;
30+
m_iFilterPrepIndex = LUA_NOREF;
3031
}
3132

3233

@@ -72,6 +73,28 @@ BOOL CFunctionListDlg::ReloadList ()
7273

7374
string sFilter (m_strFilter);
7475

76+
// call filter "prep" function (eg. do FTS3 database lookup based on the wanted filter)
77+
78+
if (m_L && m_iFilterPrepIndex != LUA_NOREF)
79+
{
80+
81+
lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_iFilterPrepIndex);
82+
83+
// Lua filter: function f (filter) ... end
84+
85+
// what they have currently typed
86+
lua_pushlstring (m_L, sFilter.c_str (), sFilter.size ());
87+
88+
// call the function: arg1: filter field, arg2: key, arg3: value
89+
if (lua_pcall (m_L, 1, 0, 0)) // call with 3 args and 0 results
90+
{
91+
LuaError (m_L); // note that this clears the stack, so we won't call it again
92+
lua_settop (m_L, 0); // clear stack, just in case LuaError changes behaviour
93+
} // end of error
94+
95+
} // end of Lua filter prep function available
96+
97+
7598
// filter based on a partial match on what is in the filter box
7699
// (eg. "chat" would find all chat functions)
77100

dialogs/FunctionListDlg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class CFunctionListDlg : public CDialog
4040
// Lua state for filter function
4141
lua_State * m_L;
4242
int m_iFilterIndex;
43+
int m_iFilterPrepIndex;
4344

4445
// Overrides
4546
// ClassWizard generated virtual function overrides

scripting/lua_utils.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,6 +1787,7 @@ CFunctionListDlg dlg;
17871787
} // end of looping through table
17881788

17891789

1790+
// filter function
17901791
if (lua_gettop (L) > 4)
17911792
{
17921793
if (!lua_isnil (L, 5))
@@ -1804,6 +1805,24 @@ CFunctionListDlg dlg;
18041805
}
18051806
}
18061807

1808+
// filter prep function
1809+
if (lua_gettop (L) > 5)
1810+
{
1811+
if (!lua_isnil (L, 6))
1812+
{
1813+
luaL_checktype (L, 6, LUA_TFUNCTION);
1814+
lua_pushvalue (L, 6); // function is now on top of stack
1815+
1816+
dlg.m_L = L;
1817+
1818+
// we can't leave the function on the stack, that gets cleared from time to time
1819+
// while the dialog box is running - so we store it in the registry and get the
1820+
// unique index back
1821+
dlg.m_iFilterPrepIndex = luaL_ref (L, LUA_REGISTRYINDEX);
1822+
1823+
}
1824+
}
1825+
18071826
if (dlg.DoModal () != IDOK)
18081827
lua_pushnil (L);
18091828
else
@@ -1821,6 +1840,7 @@ CFunctionListDlg dlg;
18211840

18221841
// free up registry entry (is OK with LUA_NOREF)
18231842
luaL_unref(L, LUA_REGISTRYINDEX, dlg.m_iFilterIndex);
1843+
luaL_unref(L, LUA_REGISTRYINDEX, dlg.m_iFilterPrepIndex);
18241844

18251845
return 1; // 1 result
18261846

0 commit comments

Comments
 (0)