@@ -55,17 +55,22 @@ CLanguageSelector::CLanguageSelector(CGUIElement* pMainMenuCanvas)
5555// /////////////////////////////////////////////////////////////
5656CLanguageSelector::~CLanguageSelector ()
5757{
58- SAFE_DELETE (m_pButtonWindow);
59- SAFE_DELETE (m_pListWindow);
60- SAFE_DELETE (m_ButtonItem.pContainerPane );
61- SAFE_DELETE (m_ButtonItem.pIcon );
62- SAFE_DELETE (m_ButtonItem.pLabel );
63- for (CLangListItem& item : m_ListItems)
64- {
65- SAFE_DELETE (item.pContainerPane );
66- SAFE_DELETE (item.pIcon );
67- SAFE_DELETE (item.pLabel );
68- }
58+ CGUI* pGUI = g_pCore ? g_pCore->GetGUI () : nullptr ;
59+
60+ auto destroyElement = [pGUI](auto *& element) {
61+ if (!element)
62+ return ;
63+
64+ if (pGUI)
65+ pGUI->DestroyElementRecursive (element);
66+ element = nullptr ;
67+ };
68+
69+ destroyElement (m_pListWindow);
70+ destroyElement (m_pButtonWindow);
71+
72+ m_ButtonItem = CLangListItem ();
73+ m_ListItems.clear ();
6974}
7075
7176// /////////////////////////////////////////////////////////////
@@ -118,18 +123,17 @@ void CLanguageSelector::CreateGUI(CGUIElement* pMainMenuCanvas)
118123 pLabel->SetZOrderingEnabled (false );
119124 pLabel->SetText (g_pLocalization->GetLanguageNativeName ());
120125
121- CLangListItem m_ButtonItem;
122- m_ButtonItem.strLocale = " " ;
123- m_ButtonItem.pContainerPane = pContainerPane;
124- m_ButtonItem.pIcon = pIcon;
125- m_ButtonItem.vecIconInitialPos = pIcon->GetPosition ();
126- m_ButtonItem.vecIconInitialSize = pIcon->GetSize ();
127- m_ButtonItem.pLabel = pLabel;
128- m_ButtonItem.vecLabelInitialPos = pLabel->GetPosition ();
129- m_ButtonItem.vecLabelInitialSize = pLabel->GetSize ();
130- m_ButtonItem.pContainerPane ->SetMouseButtonDownHandler (GUI_CALLBACK (&CLanguageSelector::OnButtonClick, this ));
131- m_ButtonItem.pContainerPane ->SetMouseEnterHandler (GUI_CALLBACK (&CLanguageSelector::OnButtonEnter, this ));
132- m_ButtonItem.pContainerPane ->SetMouseLeaveHandler (GUI_CALLBACK (&CLanguageSelector::OnButtonLeave, this ));
126+ m_ButtonItem.strLocale = " " ;
127+ m_ButtonItem.pContainerPane = pContainerPane;
128+ m_ButtonItem.pIcon = pIcon;
129+ m_ButtonItem.vecIconInitialPos = pIcon->GetPosition ();
130+ m_ButtonItem.vecIconInitialSize = pIcon->GetSize ();
131+ m_ButtonItem.pLabel = pLabel;
132+ m_ButtonItem.vecLabelInitialPos = pLabel->GetPosition ();
133+ m_ButtonItem.vecLabelInitialSize = pLabel->GetSize ();
134+ m_ButtonItem.pContainerPane ->SetMouseButtonDownHandler (GUI_CALLBACK (&CLanguageSelector::OnButtonClick, this ));
135+ m_ButtonItem.pContainerPane ->SetMouseEnterHandler (GUI_CALLBACK (&CLanguageSelector::OnButtonEnter, this ));
136+ m_ButtonItem.pContainerPane ->SetMouseLeaveHandler (GUI_CALLBACK (&CLanguageSelector::OnButtonLeave, this ));
133137 }
134138
135139 //
@@ -263,6 +267,16 @@ void CLanguageSelector::DoPulse()
263267 }
264268}
265269
270+ bool CLanguageSelector::ConsumePendingLocale (SString& outLocale)
271+ {
272+ if (m_strPendingLocale.empty ())
273+ return false ;
274+
275+ outLocale = m_strPendingLocale;
276+ m_strPendingLocale.clear ();
277+ return true ;
278+ }
279+
266280// /////////////////////////////////////////////////////////////
267281//
268282// CLanguageSelector::SetLanguageListVisible
@@ -383,9 +397,13 @@ bool CLanguageSelector::OnListItemClick(CGUIElement* pElement)
383397 SString strNewLocale = pItem->strLocale ;
384398 if (strNewLocale != strPrevLocale)
385399 {
386- // Locale change
387- CVARS_SET (" locale" , strNewLocale);
388- SetApplicationSetting (" locale" , strNewLocale);
400+ // Defer locale change to avoid destroying GUI during callback stack
401+ m_strPendingLocale = strNewLocale;
402+
403+ // Update button label immediately so user sees their selection
404+ m_ButtonItem.strLocale = strNewLocale;
405+ if (m_ButtonItem.pLabel )
406+ m_ButtonItem.pLabel ->SetText (g_pLocalization->GetLanguageNativeName (strNewLocale));
389407 }
390408 }
391409 return true ;
0 commit comments