@@ -47,7 +47,7 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
4747
4848#include " exec.h"
4949
50- MCControl * MCControl::focused;
50+ MCControlHandle MCControl::focused;
5151int2 MCControl::defaultmargin = 4 ;
5252int2 MCControl::xoffset;
5353int2 MCControl::yoffset;
@@ -126,8 +126,8 @@ MCControl::MCControl(const MCControl &cref) : MCObject(cref)
126126
127127MCControl::~MCControl ()
128128{
129- if (focused == this )
130- focused = NULL ;
129+ if (focused. IsBoundTo ( this ) )
130+ focused = nullptr ;
131131
132132 MCscreen->stopmove (this , False);
133133
@@ -155,9 +155,10 @@ void MCControl::open()
155155void MCControl::close ()
156156{
157157 // MW-2008-01-09: [[ Bug 5739 ]] Changing group layer cancels mousedown
158- if (opened == 1 )
159- if (focused == this )
160- focused = NULL ;
158+ if (opened == 1 && focused.IsBoundTo (this ))
159+ {
160+ focused = nullptr ;
161+ }
161162 MCObject::close ();
162163}
163164
@@ -263,7 +264,7 @@ Boolean MCControl::mfocus(int2 x, int2 y)
263264
264265 if (is || state & CS_MFOCUSED)
265266 {
266- if (focused == this || getstack () -> gettool (this ) == T_POINTER)
267+ if (focused. IsBoundTo ( this ) || getstack () -> gettool (this ) == T_POINTER)
267268 {
268269 if (MCdispatcher -> isdragtarget ())
269270 message_with_args (MCM_drag_move, x, y);
@@ -300,7 +301,7 @@ Boolean MCControl::mfocus(int2 x, int2 y)
300301
301302void MCControl::munfocus ()
302303{
303- if (focused == this )
304+ if (focused. IsBoundTo ( this ) )
304305 {
305306 if (state & CS_MFOCUSED)
306307 {
@@ -1358,8 +1359,11 @@ void MCControl::newmessage()
13581359
13591360void MCControl::enter ()
13601361{
1361- if (focused != NULL && focused != this )
1362- leave ();
1362+ if (focused.IsValid () && !focused.IsBoundTo (this ))
1363+ {
1364+ leave ();
1365+ }
1366+
13631367 if (MCdispatcher -> isdragtarget ())
13641368 {
13651369 MCdragaction = DRAG_ACTION_NONE;
@@ -1392,27 +1396,33 @@ void MCControl::enter()
13921396
13931397void MCControl::leave ()
13941398{
1395- MCControl * oldfocused = focused;
1399+ MCControlHandle oldfocused = focused;
13961400 if (MCdispatcher -> isdragtarget ())
13971401 {
13981402 // MW-2013-08-08: [[ Bug 10655 ]] If oldfocused is a field and has dragText set,
13991403 // then make sure we unset it (otherwise the caret will continue moving around
14001404 // on mouseMove).
1401- if (oldfocused->gettype () == CT_FIELD
1405+ if (oldfocused.IsValid ())
1406+ {
1407+ if (oldfocused->gettype () == CT_FIELD
14021408 && oldfocused -> getstate (CS_DRAG_TEXT))
1403- {
1404- MCField *fptr = (MCField *)oldfocused;
1405- fptr->removecursor ();
1406- getstack ()->clearibeam ();
1407- oldfocused->state &= ~(CS_DRAG_TEXT | CS_IBEAM);
1408- }
1409- oldfocused->message (MCM_drag_leave);
1409+ {
1410+ MCField *fptr = oldfocused.GetAs <MCField>();
1411+ fptr->removecursor ();
1412+ getstack ()->clearibeam ();
1413+ oldfocused->state &= ~(CS_DRAG_TEXT | CS_IBEAM);
1414+ }
1415+ oldfocused->message (MCM_drag_leave);
1416+ }
14101417 MCdragaction = DRAG_ACTION_NONE;
14111418 MCdragdest = nil;
14121419 }
1413- else
1420+ else if (oldfocused.IsValid ())
1421+ {
14141422 oldfocused->message (MCM_mouse_leave);
1415- focused = NULL ;
1423+ }
1424+
1425+ focused = nullptr ;
14161426}
14171427
14181428Boolean MCControl::sbfocus (int2 x, int2 y, MCScrollbar *hsb, MCScrollbar *vsb)
0 commit comments