Skip to content

Commit

Permalink
Lua plugin: Add selection API to text input elements (#434)
Browse files Browse the repository at this point in the history
See #419
  • Loading branch information
ShawnCZek committed Mar 25, 2023
1 parent 75ab61f commit 41a783d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Source/Lua/Elements/ElementFormControlInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@
namespace Rml {
namespace Lua {

//methods
int ElementFormControlInputSelect(lua_State* /*L*/, ElementFormControlInput* obj)
{
obj->Select();
return 0;
}

int ElementFormControlInputSetSelection(lua_State* L, ElementFormControlInput* obj)
{
int start = (int)luaL_checkinteger(L, 1);
int end = (int)luaL_checkinteger(L, 2);
obj->SetSelectionRange(start, end);
return 0;
}

int ElementFormControlInputGetSelection(lua_State* L, ElementFormControlInput* obj)
{
int selection_start = 0, selection_end = 0;
String selected_text;
obj->GetSelection(&selection_start, &selection_end, &selected_text);
lua_pushinteger(L, selection_start);
lua_pushinteger(L, selection_end);
lua_pushstring(L, selected_text.c_str());
return 3;
}

//getters
int ElementFormControlInputGetAttrchecked(lua_State* L)
Expand Down Expand Up @@ -146,6 +171,9 @@ int ElementFormControlInputSetAttrstep(lua_State* L)

RegType<ElementFormControlInput> ElementFormControlInputMethods[] =
{
RMLUI_LUAMETHOD(ElementFormControlInput,Select)
RMLUI_LUAMETHOD(ElementFormControlInput,SetSelection)
RMLUI_LUAMETHOD(ElementFormControlInput,GetSelection)
{nullptr,nullptr},
};

Expand Down
5 changes: 5 additions & 0 deletions Source/Lua/Elements/ElementFormControlInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
namespace Rml {
namespace Lua {

//methods
int ElementFormControlInputSelect(lua_State* L, ElementFormControlInput* obj);
int ElementFormControlInputSetSelection(lua_State* L, ElementFormControlInput* obj);
int ElementFormControlInputGetSelection(lua_State* L, ElementFormControlInput* obj);

//getters
int ElementFormControlInputGetAttrchecked(lua_State* L);
int ElementFormControlInputGetAttrmaxlength(lua_State* L);
Expand Down
29 changes: 29 additions & 0 deletions Source/Lua/Elements/ElementFormControlTextArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@
namespace Rml {
namespace Lua {

//methods
int ElementFormControlTextAreaSelect(lua_State* /*L*/, ElementFormControlTextArea* obj)
{
obj->Select();
return 0;
}

int ElementFormControlTextAreaSetSelection(lua_State* L, ElementFormControlTextArea* obj)
{
int start = (int)luaL_checkinteger(L, 1);
int end = (int)luaL_checkinteger(L, 2);
obj->SetSelectionRange(start, end);
return 0;
}

int ElementFormControlTextAreaGetSelection(lua_State* L, ElementFormControlTextArea* obj)
{
int selection_start = 0, selection_end = 0;
String selected_text;
obj->GetSelection(&selection_start, &selection_end, &selected_text);
lua_pushinteger(L, selection_start);
lua_pushinteger(L, selection_end);
lua_pushstring(L, selected_text.c_str());
return 3;
}

//getters
int ElementFormControlTextAreaGetAttrcols(lua_State* L)
{
Expand Down Expand Up @@ -108,6 +134,9 @@ int ElementFormControlTextAreaSetAttrwordwrap(lua_State* L)

RegType<ElementFormControlTextArea> ElementFormControlTextAreaMethods[] =
{
RMLUI_LUAMETHOD(ElementFormControlTextArea,Select)
RMLUI_LUAMETHOD(ElementFormControlTextArea,SetSelection)
RMLUI_LUAMETHOD(ElementFormControlTextArea,GetSelection)
{ nullptr, nullptr },
};

Expand Down
5 changes: 5 additions & 0 deletions Source/Lua/Elements/ElementFormControlTextArea.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
namespace Rml {
namespace Lua {

//methods
int ElementFormControlTextAreaSelect(lua_State* L, ElementFormControlTextArea* obj);
int ElementFormControlTextAreaSetSelection(lua_State* L, ElementFormControlTextArea* obj);
int ElementFormControlTextAreaGetSelection(lua_State* L, ElementFormControlTextArea* obj);

//getters
int ElementFormControlTextAreaGetAttrcols(lua_State* L);
int ElementFormControlTextAreaGetAttrmaxlength(lua_State* L);
Expand Down

0 comments on commit 41a783d

Please sign in to comment.