Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua plugin: Add selection API to text input elements #434

Merged
merged 1 commit into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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