Skip to content

Commit 9bc7e9c

Browse files
committed
Added Lua script function: utils.filterpicker
1 parent 93994ad commit 9bc7e9c

File tree

6 files changed

+223
-100
lines changed

6 files changed

+223
-100
lines changed

OtherTypes.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -965,20 +965,3 @@ class CPane
965965

966966
//typedef map<string, CPane *>::iterator PaneMapIterator;
967967

968-
// ----------- here used for Lua in choosing from combo-box
969-
970-
class CKeyValuePair
971-
{
972-
973-
public:
974-
CKeyValuePair (const string sValue) :
975-
bNumber_ (false), iKey_ (0.0), sValue_ (sValue) { }; // constructor
976-
977-
bool bNumber_; // true if key a number, false if a string
978-
979-
string sKey_; // key if string
980-
double iKey_; // key if number?
981-
982-
string sValue_; // value
983-
984-
}; // end of class CStringValuePair

dialogs/FunctionListDlg.cpp

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
static char THIS_FILE[] = __FILE__;
1212
#endif
1313

14-
extern set<string> LuaFunctionsSet;
15-
1614
/////////////////////////////////////////////////////////////////////////////
1715
// CFunctionListDlg dialog
1816

@@ -23,7 +21,7 @@ CFunctionListDlg::CFunctionListDlg(CWnd* pParent /*=NULL*/)
2321
//{{AFX_DATA_INIT(CFunctionListDlg)
2422
m_strFilter = _T("");
2523
//}}AFX_DATA_INIT
26-
m_bLua = false;
24+
m_bFunctions = false;
2725
}
2826

2927

@@ -51,7 +49,13 @@ BEGIN_MESSAGE_MAP(CFunctionListDlg, CDialog)
5149

5250
END_MESSAGE_MAP()
5351

54-
extern tInternalFunctionsTable InternalFunctionsTable [1];
52+
53+
// see Josuttis p499/500
54+
bool nocase_compare (char c1, char c2)
55+
{
56+
return toupper (c1) == toupper (c2);
57+
}
58+
5559

5660
BOOL CFunctionListDlg::ReloadList ()
5761
{
@@ -62,25 +66,30 @@ BOOL CFunctionListDlg::ReloadList ()
6266
m_strFilter.TrimLeft ();
6367
m_strFilter.TrimRight ();
6468

69+
string sFilter (m_strFilter);
70+
6571
// filter based on a partial match on what is in the filter box
6672
// (eg. "chat" would find all chat functions)
6773

68-
CString strFunction;
69-
7074
int nItem = 0;
71-
72-
for (int i = 0; InternalFunctionsTable [i].sFunction [0]; i++)
75+
int nKeynum = 0;
76+
for (vector<CKeyValuePair>::const_iterator it = m_data.begin ();
77+
it != m_data.end ();
78+
it++, nKeynum++)
7379
{
74-
strFunction = InternalFunctionsTable [i].sFunction;
75-
strFunction.MakeLower ();
80+
string sValue = it->sValue_;
7681

77-
if (m_strFilter.IsEmpty () || strFunction.Find (m_strFilter) != -1)
82+
if (sFilter.empty () || search (sValue.begin (), sValue.end (),
83+
sFilter.begin (), sFilter.end (),
84+
nocase_compare) != sValue.end ())
7885
{
79-
m_ctlFunctions.InsertItem (nItem, InternalFunctionsTable [i].sFunction);
86+
int iPos = m_ctlFunctions.InsertItem (nItem, sValue.c_str ());
87+
if (iPos != -1)
88+
m_ctlFunctions.SetItemData (iPos, nKeynum);
8089

8190
// select the exact match, if any (so, if they highlight world.Note then it is selected)
8291

83-
if (strFunction == m_strFilter)
92+
if (sValue == sFilter)
8493
m_ctlFunctions.SetItemState (nItem,
8594
LVIS_FOCUSED | LVIS_SELECTED,
8695
LVIS_FOCUSED | LVIS_SELECTED);
@@ -89,34 +98,6 @@ BOOL CFunctionListDlg::ReloadList ()
8998
}
9099
}
91100

92-
// add Lua functions
93-
if (m_bLua)
94-
{
95-
for (set<string>::const_iterator it = LuaFunctionsSet.begin ();
96-
it != LuaFunctionsSet.end ();
97-
it++)
98-
99-
{
100-
strFunction = it->c_str ();
101-
strFunction.MakeLower ();
102-
103-
if (m_strFilter.IsEmpty () || strFunction.Find (m_strFilter) != -1)
104-
{
105-
m_ctlFunctions.InsertItem (nItem, it->c_str ());
106-
107-
// select the exact match, if any (so, if they highlight world.Note then it is selected)
108-
109-
if (strFunction == m_strFilter)
110-
m_ctlFunctions.SetItemState (nItem,
111-
LVIS_FOCUSED | LVIS_SELECTED,
112-
LVIS_FOCUSED | LVIS_SELECTED);
113-
nItem++;
114-
115-
}
116-
117-
} // end of doing each Lua function
118-
}
119-
120101

121102
// if the filtering results in a single item, select it
122103
if (nItem == 1)
@@ -144,6 +125,15 @@ BOOL CFunctionListDlg::OnInitDialog()
144125
{
145126
CDialog::OnInitDialog();
146127

128+
SetWindowText (m_strTitle);
129+
130+
// hide buttons if we are not showing functions
131+
if (!m_bFunctions)
132+
{
133+
GetDlgItem(IDC_LUA_FUNCTIONS)->ShowWindow (SW_HIDE);
134+
GetDlgItem(IDC_COPY_NAME)->ShowWindow (SW_HIDE);
135+
}
136+
147137
return ReloadList (); // return TRUE unless you set the focus to a control
148138
// EXCEPTION: OCX Property Pages should return FALSE
149139
}
@@ -156,14 +146,8 @@ void CFunctionListDlg::OnOK()
156146

157147
if (iWhich != -1)
158148
{
159-
string sFunctionName = (LPCTSTR) m_ctlFunctions.GetItemText (iWhich, 0);
160-
161-
// might be Lua function
162-
if (LuaFunctionsSet.find (sFunctionName) != LuaFunctionsSet.end ())
163-
m_strResult = CFormat ("LUA_%s", sFunctionName.c_str ());
164-
else
165-
m_strResult = CFormat ("FNC_%s", sFunctionName.c_str ());
166-
149+
int nKeynum = m_ctlFunctions.GetItemData (iWhich);
150+
m_result = m_data [nKeynum];
167151
CDialog::OnOK();
168152
}
169153

@@ -178,7 +162,7 @@ void CFunctionListDlg::OnDblclkFunctionsList(NMHDR* pNMHDR, LRESULT* pResult)
178162

179163
void CFunctionListDlg::OnLuaFunctions()
180164
{
181-
m_strResult = "DOC_lua";
165+
m_result.sValue_ = "DOC_lua";
182166
CDialog::OnOK();
183167
}
184168

dialogs/FunctionListDlg.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@ class CFunctionListDlg : public CDialog
2424
CString m_strFilter;
2525
//}}AFX_DATA
2626

27-
CString m_strResult;
2827

29-
bool m_bLua;
28+
bool m_bFunctions;
29+
30+
// dialog title
31+
CString m_strTitle;
32+
33+
// what to put in it
34+
vector<CKeyValuePair> m_data;
35+
36+
// chosen item
37+
CKeyValuePair m_result;
3038

3139
// Overrides
3240
// ClassWizard generated virtual function overrides

scripting/functionlist.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,13 +547,43 @@ CString strWord = GetSelectedFunction (strWindowContents, nStartChar, nEndChar);
547547

548548
CFunctionListDlg dlg;
549549

550-
dlg.m_bLua = bLua;
551550
dlg.m_strFilter = strWord; // selected word from dialog/text window
552551

553-
if (dlg.DoModal () == IDCANCEL || dlg.m_strResult.IsEmpty ())
552+
for (int i = 0; InternalFunctionsTable [i].sFunction [0]; i++)
553+
{
554+
CKeyValuePair kv (InternalFunctionsTable [i].sFunction);
555+
dlg.m_data.push_back (kv);
556+
}
557+
558+
if (bLua)
559+
{
560+
for (set<string>::const_iterator it = LuaFunctionsSet.begin ();
561+
it != LuaFunctionsSet.end ();
562+
it++)
563+
{
564+
CKeyValuePair kv (it->c_str ());
565+
dlg.m_data.push_back (kv);
566+
}
567+
}
568+
569+
dlg.m_strTitle = "Functions";
570+
dlg.m_bFunctions = true;
571+
572+
if (dlg.DoModal () == IDCANCEL || dlg.m_result.sValue_.empty ())
554573
return;
555574

556-
ShowHelp ("", dlg.m_strResult); // already has prefix
575+
CString strResult = dlg.m_result.sValue_.c_str ();
576+
577+
if (dlg.m_result.sValue_ != "DOC_lua")
578+
{
579+
// might be Lua function
580+
if (LuaFunctionsSet.find (dlg.m_result.sValue_) != LuaFunctionsSet.end ())
581+
strResult = CFormat ("LUA_%s", dlg.m_result.sValue_.c_str ());
582+
else
583+
strResult = CFormat ("FNC_%s", dlg.m_result.sValue_.c_str ());
584+
}
585+
586+
ShowHelp ("", strResult); // already has prefix
557587

558588
}
559589

scripting/lua_utils.cpp

Lines changed: 104 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "..\dialogs\LuaChooseBox.h"
99
#include "..\dialogs\LuaChooseList.h"
1010
#include "..\dialogs\LuaChooseListMulti.h"
11+
#include "..\dialogs\FunctionListDlg.h"
1112
#include "..\dmetaph.h"
1213
#include <direct.h>
1314

@@ -1563,39 +1564,114 @@ static int shell_execute (lua_State *L)
15631564
} // end of shell_execute
15641565

15651566

1567+
// arg1 is dialog title
1568+
// arg2 is table of key/value pairs - value is shown
1569+
// arg3 is initial filter
1570+
1571+
int filterpicker (lua_State *L)
1572+
{
1573+
const char * filtertitle = luaL_optstring (L, 1, "Filter");
1574+
const char * initialfilter = luaL_optstring (L, 3, "");
1575+
1576+
if (strlen (filtertitle) > 100)
1577+
luaL_error (L, "title too long (max 100 characters)");
1578+
1579+
const int table = 2;
1580+
1581+
if (!lua_istable (L, table))
1582+
luaL_error (L, "must have table of choices as 2nd argument");
1583+
1584+
CFunctionListDlg dlg;
1585+
1586+
dlg.m_strTitle = filtertitle;
1587+
dlg.m_strFilter = initialfilter;
1588+
1589+
// standard Lua table iteration
1590+
for (lua_pushnil (L); lua_next (L, table) != 0; lua_pop (L, 1))
1591+
{
1592+
if (!lua_isstring (L, -2))
1593+
luaL_error (L, "table must have string or number keys");
1594+
1595+
if (!lua_isstring (L, -1))
1596+
luaL_error (L, "table must have string or number values");
1597+
1598+
// value can simply be converted to a string
1599+
string sValue = lua_tostring (L, -1);
1600+
1601+
// get key
1602+
CKeyValuePair kv (sValue);
1603+
1604+
if (lua_type (L, -2) == LUA_TSTRING)
1605+
{
1606+
kv.sKey_ = lua_tostring (L, -2);
1607+
}
1608+
else
1609+
{ // not string, should be number :)
1610+
// cannot do lua_tostring because that confuses lua_next
1611+
kv.bNumber_ = true;
1612+
kv.iKey_ = lua_tonumber (L, -2);
1613+
} // end of key being a number
1614+
1615+
1616+
dlg.m_data.push_back (kv);
1617+
1618+
} // end of looping through table
1619+
1620+
1621+
if (dlg.DoModal () != IDOK)
1622+
lua_pushnil (L);
1623+
else
1624+
{ // not cancelled
1625+
if (dlg.m_result.sValue_ == "")
1626+
lua_pushnil (L); // no choice made
1627+
else
1628+
{ // get key of choice
1629+
if (dlg.m_result.bNumber_)
1630+
lua_pushnumber (L, dlg.m_result.iKey_);
1631+
else
1632+
lua_pushstring (L, dlg.m_result.sKey_.c_str ());
1633+
}
1634+
}
1635+
1636+
return 1; // 1 result
1637+
1638+
} // end of filterpicker
1639+
1640+
15661641

15671642
// table of operations
15681643
static const struct luaL_reg xmllib [] =
15691644
{
15701645

1571-
{"activatenotepad", activatenotepad},
1572-
{"appendtonotepad", appendtonotepad},
1573-
{"callbackslist", callbackslist},
1574-
{"choose", choose},
1575-
{"directorypicker", directorypicker},
1576-
{"edit_distance", edit_distance},
1577-
{"editbox", editbox},
1578-
{"filepicker", filepicker},
1579-
{"fontpicker", fontpicker},
1580-
{"functionargs", functionargs},
1581-
{"functionlist", functionlist},
1582-
{"getfontfamilies", getfontfamilies},
1583-
{"info", info},
1584-
{"inputbox", inputbox},
1585-
{"listbox", listbox},
1586-
{"metaphone", metaphone},
1587-
{"msgbox", msgbox}, // msgbox - not Unicode
1588-
{"multilistbox", multilistbox},
1589-
{"showdebugstatus", showdebugstatus},
1590-
{"sendtofront", send_to_front},
1591-
{"shellexecute", shell_execute},
1592-
{"spellcheckdialog", spellcheckdialog},
1593-
{"umsgbox", umsgbox}, // msgbox - UTF8
1594-
{"utf8decode", utf8decode},
1595-
{"utf8encode", utf8encode},
1596-
{"utf8sub", utf8sub},
1597-
{"utf8valid", utf8valid},
1598-
{"xmlread", xmlread},
1646+
{"activatenotepad", activatenotepad},
1647+
{"appendtonotepad", appendtonotepad},
1648+
{"callbackslist", callbackslist},
1649+
{"choose", choose},
1650+
{"directorypicker", directorypicker},
1651+
{"edit_distance", edit_distance},
1652+
{"editbox", editbox},
1653+
{"filepicker", filepicker},
1654+
{"filterpicker", filterpicker},
1655+
{"fontpicker", fontpicker},
1656+
{"functionargs", functionargs},
1657+
{"functionlist", functionlist},
1658+
{"getfontfamilies", getfontfamilies},
1659+
{"info", info},
1660+
{"inputbox", inputbox},
1661+
{"listbox", listbox},
1662+
{"metaphone", metaphone},
1663+
{"msgbox", msgbox}, // msgbox - not Unicode
1664+
{"multilistbox", multilistbox},
1665+
{"showdebugstatus", showdebugstatus},
1666+
{"sendtofront", send_to_front},
1667+
{"shellexecute", shell_execute},
1668+
{"spellcheckdialog", spellcheckdialog},
1669+
{"umsgbox", umsgbox}, // msgbox - UTF8
1670+
{"utf8decode", utf8decode},
1671+
{"utf8encode", utf8encode},
1672+
{"utf8sub", utf8sub},
1673+
{"utf8valid", utf8valid},
1674+
{"xmlread", xmlread},
15991675

16001676

16011677
{NULL, NULL}

0 commit comments

Comments
 (0)