Skip to content

Commit

Permalink
Added Lua script function: utils.filterpicker
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgammon committed Sep 23, 2010
1 parent 93994ad commit 9bc7e9c
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 100 deletions.
17 changes: 0 additions & 17 deletions OtherTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -965,20 +965,3 @@ class CPane

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

// ----------- here used for Lua in choosing from combo-box

class CKeyValuePair
{

public:
CKeyValuePair (const string sValue) :
bNumber_ (false), iKey_ (0.0), sValue_ (sValue) { }; // constructor

bool bNumber_; // true if key a number, false if a string

string sKey_; // key if string
double iKey_; // key if number?

string sValue_; // value

}; // end of class CStringValuePair
84 changes: 34 additions & 50 deletions dialogs/FunctionListDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
static char THIS_FILE[] = __FILE__;
#endif

extern set<string> LuaFunctionsSet;

/////////////////////////////////////////////////////////////////////////////
// CFunctionListDlg dialog

Expand All @@ -23,7 +21,7 @@ CFunctionListDlg::CFunctionListDlg(CWnd* pParent /*=NULL*/)
//{{AFX_DATA_INIT(CFunctionListDlg)
m_strFilter = _T("");
//}}AFX_DATA_INIT
m_bLua = false;
m_bFunctions = false;
}


Expand Down Expand Up @@ -51,7 +49,13 @@ BEGIN_MESSAGE_MAP(CFunctionListDlg, CDialog)

END_MESSAGE_MAP()

extern tInternalFunctionsTable InternalFunctionsTable [1];

// see Josuttis p499/500
bool nocase_compare (char c1, char c2)
{
return toupper (c1) == toupper (c2);
}


BOOL CFunctionListDlg::ReloadList ()
{
Expand All @@ -62,25 +66,30 @@ BOOL CFunctionListDlg::ReloadList ()
m_strFilter.TrimLeft ();
m_strFilter.TrimRight ();

string sFilter (m_strFilter);

// filter based on a partial match on what is in the filter box
// (eg. "chat" would find all chat functions)

CString strFunction;

int nItem = 0;

for (int i = 0; InternalFunctionsTable [i].sFunction [0]; i++)
int nKeynum = 0;
for (vector<CKeyValuePair>::const_iterator it = m_data.begin ();
it != m_data.end ();
it++, nKeynum++)
{
strFunction = InternalFunctionsTable [i].sFunction;
strFunction.MakeLower ();
string sValue = it->sValue_;

if (m_strFilter.IsEmpty () || strFunction.Find (m_strFilter) != -1)
if (sFilter.empty () || search (sValue.begin (), sValue.end (),
sFilter.begin (), sFilter.end (),
nocase_compare) != sValue.end ())
{
m_ctlFunctions.InsertItem (nItem, InternalFunctionsTable [i].sFunction);
int iPos = m_ctlFunctions.InsertItem (nItem, sValue.c_str ());
if (iPos != -1)
m_ctlFunctions.SetItemData (iPos, nKeynum);

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

if (strFunction == m_strFilter)
if (sValue == sFilter)
m_ctlFunctions.SetItemState (nItem,
LVIS_FOCUSED | LVIS_SELECTED,
LVIS_FOCUSED | LVIS_SELECTED);
Expand All @@ -89,34 +98,6 @@ BOOL CFunctionListDlg::ReloadList ()
}
}

// add Lua functions
if (m_bLua)
{
for (set<string>::const_iterator it = LuaFunctionsSet.begin ();
it != LuaFunctionsSet.end ();
it++)

{
strFunction = it->c_str ();
strFunction.MakeLower ();

if (m_strFilter.IsEmpty () || strFunction.Find (m_strFilter) != -1)
{
m_ctlFunctions.InsertItem (nItem, it->c_str ());

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

if (strFunction == m_strFilter)
m_ctlFunctions.SetItemState (nItem,
LVIS_FOCUSED | LVIS_SELECTED,
LVIS_FOCUSED | LVIS_SELECTED);
nItem++;

}

} // end of doing each Lua function
}


// if the filtering results in a single item, select it
if (nItem == 1)
Expand Down Expand Up @@ -144,6 +125,15 @@ BOOL CFunctionListDlg::OnInitDialog()
{
CDialog::OnInitDialog();

SetWindowText (m_strTitle);

// hide buttons if we are not showing functions
if (!m_bFunctions)
{
GetDlgItem(IDC_LUA_FUNCTIONS)->ShowWindow (SW_HIDE);
GetDlgItem(IDC_COPY_NAME)->ShowWindow (SW_HIDE);
}

return ReloadList (); // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
Expand All @@ -156,14 +146,8 @@ void CFunctionListDlg::OnOK()

if (iWhich != -1)
{
string sFunctionName = (LPCTSTR) m_ctlFunctions.GetItemText (iWhich, 0);

// might be Lua function
if (LuaFunctionsSet.find (sFunctionName) != LuaFunctionsSet.end ())
m_strResult = CFormat ("LUA_%s", sFunctionName.c_str ());
else
m_strResult = CFormat ("FNC_%s", sFunctionName.c_str ());

int nKeynum = m_ctlFunctions.GetItemData (iWhich);
m_result = m_data [nKeynum];
CDialog::OnOK();
}

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

void CFunctionListDlg::OnLuaFunctions()
{
m_strResult = "DOC_lua";
m_result.sValue_ = "DOC_lua";
CDialog::OnOK();
}

Expand Down
12 changes: 10 additions & 2 deletions dialogs/FunctionListDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ class CFunctionListDlg : public CDialog
CString m_strFilter;
//}}AFX_DATA

CString m_strResult;

bool m_bLua;
bool m_bFunctions;

// dialog title
CString m_strTitle;

// what to put in it
vector<CKeyValuePair> m_data;

// chosen item
CKeyValuePair m_result;

// Overrides
// ClassWizard generated virtual function overrides
Expand Down
36 changes: 33 additions & 3 deletions scripting/functionlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,13 +547,43 @@ CString strWord = GetSelectedFunction (strWindowContents, nStartChar, nEndChar);

CFunctionListDlg dlg;

dlg.m_bLua = bLua;
dlg.m_strFilter = strWord; // selected word from dialog/text window

if (dlg.DoModal () == IDCANCEL || dlg.m_strResult.IsEmpty ())
for (int i = 0; InternalFunctionsTable [i].sFunction [0]; i++)
{
CKeyValuePair kv (InternalFunctionsTable [i].sFunction);
dlg.m_data.push_back (kv);
}

if (bLua)
{
for (set<string>::const_iterator it = LuaFunctionsSet.begin ();
it != LuaFunctionsSet.end ();
it++)
{
CKeyValuePair kv (it->c_str ());
dlg.m_data.push_back (kv);
}
}

dlg.m_strTitle = "Functions";
dlg.m_bFunctions = true;

if (dlg.DoModal () == IDCANCEL || dlg.m_result.sValue_.empty ())
return;

ShowHelp ("", dlg.m_strResult); // already has prefix
CString strResult = dlg.m_result.sValue_.c_str ();

if (dlg.m_result.sValue_ != "DOC_lua")
{
// might be Lua function
if (LuaFunctionsSet.find (dlg.m_result.sValue_) != LuaFunctionsSet.end ())
strResult = CFormat ("LUA_%s", dlg.m_result.sValue_.c_str ());
else
strResult = CFormat ("FNC_%s", dlg.m_result.sValue_.c_str ());
}

ShowHelp ("", strResult); // already has prefix

}

Expand Down
132 changes: 104 additions & 28 deletions scripting/lua_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "..\dialogs\LuaChooseBox.h"
#include "..\dialogs\LuaChooseList.h"
#include "..\dialogs\LuaChooseListMulti.h"
#include "..\dialogs\FunctionListDlg.h"
#include "..\dmetaph.h"
#include <direct.h>

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


// arg1 is dialog title
// arg2 is table of key/value pairs - value is shown
// arg3 is initial filter

int filterpicker (lua_State *L)
{
const char * filtertitle = luaL_optstring (L, 1, "Filter");
const char * initialfilter = luaL_optstring (L, 3, "");

if (strlen (filtertitle) > 100)
luaL_error (L, "title too long (max 100 characters)");

const int table = 2;

if (!lua_istable (L, table))
luaL_error (L, "must have table of choices as 2nd argument");

CFunctionListDlg dlg;

dlg.m_strTitle = filtertitle;
dlg.m_strFilter = initialfilter;

// standard Lua table iteration
for (lua_pushnil (L); lua_next (L, table) != 0; lua_pop (L, 1))
{
if (!lua_isstring (L, -2))
luaL_error (L, "table must have string or number keys");

if (!lua_isstring (L, -1))
luaL_error (L, "table must have string or number values");

// value can simply be converted to a string
string sValue = lua_tostring (L, -1);

// get key
CKeyValuePair kv (sValue);

if (lua_type (L, -2) == LUA_TSTRING)
{
kv.sKey_ = lua_tostring (L, -2);
}
else
{ // not string, should be number :)
// cannot do lua_tostring because that confuses lua_next
kv.bNumber_ = true;
kv.iKey_ = lua_tonumber (L, -2);
} // end of key being a number


dlg.m_data.push_back (kv);

} // end of looping through table


if (dlg.DoModal () != IDOK)
lua_pushnil (L);
else
{ // not cancelled
if (dlg.m_result.sValue_ == "")
lua_pushnil (L); // no choice made
else
{ // get key of choice
if (dlg.m_result.bNumber_)
lua_pushnumber (L, dlg.m_result.iKey_);
else
lua_pushstring (L, dlg.m_result.sKey_.c_str ());
}
}

return 1; // 1 result

} // end of filterpicker



// table of operations
static const struct luaL_reg xmllib [] =
{

{"activatenotepad", activatenotepad},
{"appendtonotepad", appendtonotepad},
{"callbackslist", callbackslist},
{"choose", choose},
{"directorypicker", directorypicker},
{"edit_distance", edit_distance},
{"editbox", editbox},
{"filepicker", filepicker},
{"fontpicker", fontpicker},
{"functionargs", functionargs},
{"functionlist", functionlist},
{"getfontfamilies", getfontfamilies},
{"info", info},
{"inputbox", inputbox},
{"listbox", listbox},
{"metaphone", metaphone},
{"msgbox", msgbox}, // msgbox - not Unicode
{"multilistbox", multilistbox},
{"showdebugstatus", showdebugstatus},
{"sendtofront", send_to_front},
{"shellexecute", shell_execute},
{"spellcheckdialog", spellcheckdialog},
{"umsgbox", umsgbox}, // msgbox - UTF8
{"utf8decode", utf8decode},
{"utf8encode", utf8encode},
{"utf8sub", utf8sub},
{"utf8valid", utf8valid},
{"xmlread", xmlread},
{"activatenotepad", activatenotepad},
{"appendtonotepad", appendtonotepad},
{"callbackslist", callbackslist},
{"choose", choose},
{"directorypicker", directorypicker},
{"edit_distance", edit_distance},
{"editbox", editbox},
{"filepicker", filepicker},
{"filterpicker", filterpicker},
{"fontpicker", fontpicker},
{"functionargs", functionargs},
{"functionlist", functionlist},
{"getfontfamilies", getfontfamilies},
{"info", info},
{"inputbox", inputbox},
{"listbox", listbox},
{"metaphone", metaphone},
{"msgbox", msgbox}, // msgbox - not Unicode
{"multilistbox", multilistbox},
{"showdebugstatus", showdebugstatus},
{"sendtofront", send_to_front},
{"shellexecute", shell_execute},
{"spellcheckdialog", spellcheckdialog},
{"umsgbox", umsgbox}, // msgbox - UTF8
{"utf8decode", utf8decode},
{"utf8encode", utf8encode},
{"utf8sub", utf8sub},
{"utf8valid", utf8valid},
{"xmlread", xmlread},


{NULL, NULL}
Expand Down
Loading

0 comments on commit 9bc7e9c

Please sign in to comment.