This was how I fixed it :
original code :
void EditorConfigurationDlg::OnChangeDefCodeFileType(wxCommandEvent& event)
{
int sel = XRCCTRL(*this, "cmbDefCodeFileType", wxComboBox)->GetSelection();
if (sel != m_DefCodeFileType)
{
wxString key;
key.Printf(_T("/default_code/set%d"), IdxToFileType[m_DefCodeFileType]);
Manager::Get()->GetConfigManager(_T("editor"))->Write(key, XRCCTRL(*this, "txtDefCode", wxTextCtrl)->GetValue());
}
m_DefCodeFileType = sel;
key.Printf(_T("/default_code/set%d"), IdxToFileType[m_DefCodeFileType]);
XRCCTRL(*this, "txtDefCode", wxTextCtrl)->SetValue(Manager::Get()->GetConfigManager(_T("editor"))->Read(key, wxEmptyString));
}
MY code :
void EditorConfigurationDlg::OnChangeDefCodeFileType(wxCommandEvent& event)
{
int sel = XRCCTRL(*this, "cmbDefCodeFileType", wxComboBox)->GetSelection();
if (sel != m_DefCodeFileType)
{
wxString key;
key.Printf(_T("/default_code/set%d"), IdxToFileType[m_DefCodeFileType]);
Manager::Get()->GetConfigManager(_T("editor"))->Write(key, XRCCTRL(*this, "txtDefCode", wxTextCtrl)->GetValue());
m_DefCodeFileType = sel;
key.Printf(_T("/default_code/set%d"), IdxToFileType[m_DefCodeFileType]);
XRCCTRL(*this, "txtDefCode", wxTextCtrl)->SetValue(Manager::Get()->GetConfigManager(_T("editor"))->Read(key, wxEmptyString));
}
}
During my debugging sessions I saw yesterday that, the first we changed the selection in the combo box from 'source' to 'header', we ended up TWICE in this method, the very first time, sel will be 0 (!!!! so extra event with the old value), and the member m_DefCodeFileTYpe is initially 0 (constructor), so the if test failes, it thinks the selection has not changed, then after the if test it updates the member (in this case, no change), then reads out the 'default code' that's in the configuration file, and sets/shows that in the dialog. That means that in this case it will show the old default 'source' code (which in my test was , just nothing, nada, empty).
Then the second call arrives, and now sel is 1, correct that's the 'header' we switched to, if test succeeds and we SAVE (note : next thing I will fix, we should not save yet, nobody pressed OK !!!) the current default code to the configuration file. Well we just have reloaded the old stuff, so our new stuff is gone. That's why we loose it. Then we set the member to the new selection, and we set/show what we had for this new selection in the configuration file.
Every other switch from this moment on, for example 'header' -> 'source', or again 'source'->'header' works ok (when dialog is closed, and you go back to this settings dialog, the story restarts off course), because there's only 1 call to the method, and the sel will always be the new selection.
So the ultimate solution : not that errounous first call, Yiannis and Thomas told me it's a wx event problem.
Thomas his fix, does not have this problem, since he now always save, even if the selection did not change (eg : in the combo, mouse from 'source' to 'header' and back to 'source' and then left click with you mouse).
My fix does it a little bit different : if the selection did not change, why would we read out the the 'default' code , which was set to the ctrl, back from the configuration file, to set again to the ctrl. The ctrl still has it (I think/hope this is correct and there are no other reasons to come here). Therefor those 3 lines (setting the member with the new sel, and readout fron configuration file and setting ctrl) can be moved inside the if test.
So when that erronous first call comes, the sel == m_DefCodeFileType, if test fails, and nothing else get's done (so no overwriting the new stuff with the old stuff), when the correct second call (and all other correct 'first and only' calls) come and the selection has changed, save to file (ahem ahem, we did not click on OK ;-) ) and the default code for the new selection is loaded from the configuration file and set/shown to the ctrl.
So as said, next I will fix the issue that stuff gets save before clicking on ok, so we can bail/cancel our way out.
Lieven