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

Connecting Frontend to Backend #260

Closed
MichealShinoda opened this issue Sep 17, 2021 · 2 comments
Closed

Connecting Frontend to Backend #260

MichealShinoda opened this issue Sep 17, 2021 · 2 comments

Comments

@MichealShinoda
Copy link

I couldn't find any information on how to connecting frontend (button clicks) to backend. Let's say I have a button like this:

<w type="button" class="btn ml-3">Start Application</w>

How can I add click event to the xml file and program it in C?
Thanks

@lc-soft
Copy link
Owner

lc-soft commented Sep 17, 2021

Here are the steps:

  1. Add id attribute to xml element
  2. Call LCUIWidget_GetById() to get the widget
  3. Call Widget_BindEvent() to bind the click event of the widget

Examples for reference:

LCUI/test/helloworld.c

Lines 1 to 33 in ea409f1

#include <LCUI_Build.h>
#include <LCUI/LCUI.h>
#include <LCUI/gui/widget.h>
#include <LCUI/gui/builder.h>
#include <LCUI/gui/widget/textview.h>
#include <LCUI/gui/widget/textedit.h>
static void OnBtnClick(LCUI_Widget self, LCUI_WidgetEvent e, void *arg)
{
wchar_t str[256];
LCUI_Widget edit = LCUIWidget_GetById("edit");
LCUI_Widget txt = LCUIWidget_GetById("text-hello");
TextEdit_GetTextW(edit, 0, 255, str);
TextView_SetTextW(txt, str);
}
int main(int argc, char **argv)
{
LCUI_Widget root, pack, btn;
LCUI_Init();
root = LCUIWidget_GetRoot();
pack = LCUIBuilder_LoadFile("helloworld.xml");
if (!pack) {
return -1;
}
Widget_Append(root, pack);
Widget_Unwrap(pack);
btn = LCUIWidget_GetById("btn");
Widget_BindEvent(btn, "click", OnBtnClick, NULL, NULL);
return LCUI_Main();
}

<?xml version="1.0" encoding="UTF-8" ?>
<lcui-app>
<resource type="text/css" src="helloworld.css"/>
<ui>
<textview id="text-hello" type="textview" class="text-hello">
[i][color=#f00]Hello[/color][/i], [b][color=#fff][bgcolor=#f00]World![/bgcolor][/color][/b]
</textview>
<textedit id="edit">[i][color=#f00]Hello[/color][/i], [b][color=#fff][bgcolor=#f00]World![/bgcolor][/color][/b]</textedit>
<button id="btn">Change</button>
</ui>
</lcui-app>

@MichealShinoda
Copy link
Author

Worked like charm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants