Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit a644520

Browse files
Merge pull request #7048 from livecodeian/feature-widget_reference
[[ Widget ]] Add `this widget` and `trigger all in <widget>` syntax
2 parents f00d7e3 + 2a76aaf commit a644520

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# LiveCode Builder Host Library
2+
## Widget Library
3+
New syntax has been added to obtain a reference to the widget, and to use that
4+
reference when notifying the engine of property changes via the `trigger all`
5+
command.
6+
7+
This is most useful when LCB handlers within a widget module are used as
8+
asynchronous callback functions passed to foreign functions, as these may be
9+
called at a time when the widget is not the currently active widget. Using the
10+
reference prevents updates being seen as coming from the wrong widget.

engine/src/widget-syntax.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ extern "C" MC_DLLEXPORT_DEF void MCWidgetExecTriggerAll(void)
143143
MCWidgetTriggerAll(MCcurrentwidget);
144144
}
145145

146+
extern "C" MC_DLLEXPORT_DEF void MCWidgetExecTriggerAllInWidget(MCWidgetRef p_widget)
147+
{
148+
MCWidgetTriggerAll(p_widget);
149+
}
150+
146151
////////////////////////////////////////////////////////////////////////////////
147152

148153
extern "C" MC_DLLEXPORT_DEF void MCWidgetGetMyScriptObject(MCScriptObjectRef& r_script_object)
@@ -475,6 +480,14 @@ extern "C" MC_DLLEXPORT_DEF void MCWidgetGetMouseButtonState(uinteger_t p_index,
475480

476481
////////////////////////////////////////////////////////////////////////////////
477482

483+
extern "C" MC_DLLEXPORT_DEF void MCWidgetEvalThisWidget(MCWidgetRef& r_widget)
484+
{
485+
if (!MCWidgetEnsureCurrentWidget())
486+
return;
487+
488+
r_widget = MCValueRetain(MCcurrentwidget);
489+
}
490+
478491
extern "C" MC_DLLEXPORT_DEF void MCWidgetEvalTheTarget(MCWidgetRef& r_widget)
479492
{
480493
if (!MCWidgetEnsureCurrentWidget())

engine/src/widget.lcb

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,18 @@ use com.livecode.foreign
394394
use com.livecode.canvas
395395
use com.livecode.engine
396396

397+
// ---------- Widget type definition ---------- //
398+
399+
public foreign type Widget binds to "MCWidgetTypeInfo"
400+
397401
// ---------- Widget commands ---------- //
398402

399403
public foreign handler MCWidgetExecRedrawAll() returns nothing binds to "<builtin>"
400404
public foreign handler MCWidgetExecScheduleTimerIn(in pTime as CDouble) returns nothing binds to "<builtin>"
401405
public foreign handler MCWidgetExecCancelTimer() returns nothing binds to "<builtin>"
402406
public foreign handler MCWidgetEvalInEditMode(out rInEditMode as CBool) returns nothing binds to "<builtin>"
403407
public foreign handler MCWidgetExecTriggerAll() returns nothing binds to "<builtin>"
408+
public foreign handler MCWidgetExecTriggerAllInWidget(in pWidget as Widget) returns nothing binds to "<builtin>"
404409

405410
/**
406411
Summary: Redraws the widget.
@@ -488,23 +493,32 @@ end syntax
488493

489494
/**
490495
Summary: Causes all of a widget's property triggers to be fired.
496+
mWidget: An expression that evaluates to a widget.
491497

492498
Example:
493499
handler TextChangedCallback()
494500
UpdateTextProperty()
495501
trigger all
496502
end handler
497503

504+
Example:
505+
private variable mSelf as Widget
506+
handler TextChangedCallback()
507+
UpdateTextProperty()
508+
trigger all in mSelf
509+
end handler
510+
498511
Description:
499512
Use trigger all to cause all triggers for all a widget's properties to
500513
be fired, for example when user action causes a native widget's
501514
properties to change, to signal the property change to the IDE.
502515
*/
503516

504517
syntax TriggerAll is statement
505-
"trigger" "all"
518+
"trigger" "all" [ "in" <mWidget: Expression> ]
506519
begin
507520
MCWidgetExecTriggerAll()
521+
MCWidgetExecTriggerAllInWidget(mWidget)
508522
end syntax
509523

510524

@@ -1164,8 +1178,7 @@ end syntax
11641178

11651179
// annotation <name> of <widget>
11661180

1167-
public foreign type Widget binds to "MCWidgetTypeInfo"
1168-
1181+
public foreign handler MCWidgetEvalThisWidget(out rMe as optional Widget) returns nothing binds to "<builtin>"
11691182
public foreign handler MCWidgetEvalTheTarget(out rTarget as optional Widget) returns nothing binds to "<builtin>"
11701183

11711184
public foreign handler MCWidgetEvalMyChildren(out rChildWidgets as List) returns nothing binds to "<builtin>"
@@ -1260,6 +1273,49 @@ end syntax
12601273

12611274
//
12621275

1276+
/**
1277+
Summary: Returns the current widget.
1278+
Returns: A widget object.
1279+
1280+
Example:
1281+
1282+
-- In a widget
1283+
private variable mSelf as Widget
1284+
public handler OnCreate() returns nothing
1285+
-- Keep a reference to this widget
1286+
put this widget into mSelf
1287+
1288+
-- defined in separate module library
1289+
SetEventCallback(EventCallback)
1290+
end handler
1291+
1292+
-- may be called from another module library
1293+
private handler EventCallback() returns nothing
1294+
-- update internal variables
1295+
1296+
-- notify ide of changes
1297+
trigger all in mSelf
1298+
end handler
1299+
1300+
Description:
1301+
This widget evaluates to the current widget. This can be used to retain a
1302+
reference for occasions where widget handlers may be called from another module,
1303+
where the current widget may not be valid.
1304+
1305+
This is useful when LCB handlers within a widget module are used as
1306+
asynchronous callback functions passed to foreign functions, as these may be
1307+
called at a time when the widget is not the currently active widget. Using the
1308+
reference prevents updates being seen as coming from the wrong widget.
1309+
*/
1310+
1311+
syntax ThisWidget is expression
1312+
"this" "widget"
1313+
begin
1314+
MCWidgetEvalThisWidget(output)
1315+
end syntax
1316+
1317+
//
1318+
12631319
/**
12641320
Summary: The currently placed child widgets of this widget.
12651321
Returns: A list of the child widgets of this widget.

0 commit comments

Comments
 (0)