@@ -84,6 +84,8 @@ bool MCWidgetBase::Create(MCNameRef p_kind)
8484 if (!MCScriptCreateInstanceOfModule (t_module, m_instance))
8585 return false ;
8686
87+ MCScriptSetInstanceHostPtr (m_instance, AsWidget ());
88+
8789 if (!DispatchRestricted (MCNAME (" OnCreate" )))
8890 {
8991 MCScriptReleaseInstance (m_instance);
@@ -153,46 +155,12 @@ bool MCWidgetBase::HasHandler(MCNameRef p_handler)
153155
154156bool MCWidgetBase::SetProperty (MCNameRef p_property, MCValueRef p_value)
155157{
156- MCWidgetRef t_old_widget;
157- t_old_widget = MCcurrentwidget;
158- MCcurrentwidget = AsWidget ();
159-
160- MCWidget *t_host;
161- t_host = GetHost ();
162- if (t_host != nil)
163- t_host -> lockforexecution ();
164-
165- bool t_success;
166- t_success = MCScriptSetPropertyInInstance (m_instance, p_property, p_value);
167-
168- if (t_host != nil)
169- t_host -> unlockforexecution ();
170-
171- MCcurrentwidget = t_old_widget;
172-
173- return t_success;
158+ return MCScriptSetPropertyInInstance (m_instance, p_property, p_value);
174159}
175160
176161bool MCWidgetBase::GetProperty (MCNameRef p_property, MCValueRef& r_value)
177162{
178- MCWidgetRef t_old_widget;
179- t_old_widget = MCcurrentwidget;
180- MCcurrentwidget = AsWidget ();
181-
182- MCWidget *t_host;
183- t_host = GetHost ();
184- if (t_host != nil)
185- t_host -> lockforexecution ();
186-
187- bool t_success;
188- t_success = MCScriptGetPropertyInInstance (m_instance, p_property, r_value);
189-
190- if (t_host != nil)
191- t_host -> unlockforexecution ();
192-
193- MCcurrentwidget = t_old_widget;
194-
195- return t_success;
163+ return MCScriptGetPropertyInInstance (m_instance, p_property, r_value);
196164}
197165
198166static bool chunk_property_handler (MCNameRef p_property, MCNameRef p_chunk_name, bool p_is_get_operation, MCNameRef& r_handler)
@@ -848,34 +816,16 @@ MCGRectangle MCWidgetBase::MapRectFromGlobal(MCGRectangle rect)
848816
849817bool MCWidgetBase::DoDispatch (MCNameRef p_event, MCValueRef *x_args, uindex_t p_arg_count, MCValueRef *r_result)
850818{
851- MCWidgetRef t_old_widget_object;
852- t_old_widget_object = MCcurrentwidget;
853- MCcurrentwidget = AsWidget ();
854-
855- // Make sure the host object's 'scriptdepth' is incremented and
856- // decremented appropriately. This prevents script from deleting
857- // a widget which has its LCB code on the stack (just as it would if
858- // it had its LCS code on the stack).
859- MCWidget *t_host;
860- t_host = GetHost ();
861- if (t_host != nil)
862- t_host -> lockforexecution ();
863-
864819 bool t_success;
865820 MCAutoValueRef t_retval;
866821 t_success = MCScriptCallHandlerInInstanceIfFound (m_instance, p_event, x_args, p_arg_count, &t_retval);
867822
868- if (t_host != nil)
869- t_host -> unlockforexecution ();
870-
871823 if (t_success)
872824 {
873825 if (r_result != NULL )
874826 *r_result = t_retval . Take ();
875827 }
876828
877- MCcurrentwidget = t_old_widget_object;
878-
879829 return t_success;
880830
881831}
@@ -1501,3 +1451,37 @@ MCWidgetRoot *MCWidgetAsRoot(MCWidgetRef self)
15011451}
15021452
15031453// //////////////////////////////////////////////////////////////////////////////
1454+
1455+ void *MCWidgetEnter (MCScriptInstanceRef p_instance, void *p_host_ptr)
1456+ {
1457+ void *t_cookie = MCcurrentwidget;
1458+
1459+ auto t_widget = static_cast <MCWidgetRef>(p_host_ptr);
1460+
1461+ // Make sure the host object's 'scriptdepth' is incremented and
1462+ // decremented appropriately. This prevents script from deleting
1463+ // a widget which has its LCB code on the stack (just as it would if
1464+ // it had its LCS code on the stack).
1465+ MCWidget *t_host = MCWidgetAsBase (t_widget)->GetHost ();
1466+ if (t_host != nullptr )
1467+ t_host->lockforexecution ();
1468+
1469+ // Update the current widget context
1470+ MCcurrentwidget = t_widget;
1471+
1472+ return t_cookie;
1473+ }
1474+
1475+ void MCWidgetLeave (MCScriptInstanceRef p_instance, void *p_host_ptr, void *p_cookie)
1476+ {
1477+ auto t_old_widget = static_cast <MCWidgetRef>(p_cookie);
1478+ MCcurrentwidget = t_old_widget;
1479+
1480+ auto t_widget = static_cast <MCWidgetRef>(p_host_ptr);
1481+
1482+ MCWidget *t_host = MCWidgetAsBase (t_widget)->GetHost ();
1483+ if (t_host != nullptr )
1484+ t_host->unlockforexecution ();
1485+ }
1486+
1487+ // //////////////////////////////////////////////////////////////////////////////
0 commit comments