+
+///
+/// Resource symbol validator
+///
+IMPLEMENT_DYNAMIC_CLASS(wxResourceSymbolValidator, wxPropertyListValidator)
+
+wxResourceSymbolValidator::wxResourceSymbolValidator(long flags):
+wxPropertyListValidator(flags)
+{
+}
+
+wxResourceSymbolValidator::~wxResourceSymbolValidator(void)
+{
+}
+
+bool wxResourceSymbolValidator::OnCheckValue(wxProperty *WXUNUSED(property), wxPropertyListView *WXUNUSED(view), wxWindow *WXUNUSED(parentWindow))
+{
+ return TRUE;
+}
+
+// Called when TICK is pressed or focus is lost or view wants to update
+// the property list.
+// Does the transferance from the property editing area to the property itself
+bool wxResourceSymbolValidator::OnRetrieveValue(wxProperty *property, wxPropertyListView *view, wxWindow *WXUNUSED(parentWindow))
+{
+ if (!view->GetValueText())
+ return FALSE;
+ wxString value(view->GetValueText()->GetValue());
+ property->GetValue() = value ;
+ return TRUE;
+}
+
+// Called when TICK is pressed or focus is lost or view wants to update
+// the property list.
+// Does the transferance from the property editing area to the property itself
+bool wxResourceSymbolValidator::OnDisplayValue(wxProperty *property, wxPropertyListView *view, wxWindow *WXUNUSED(parentWindow))
+{
+ if (!view->GetValueText())
+ return FALSE;
+ wxString str(property->GetValue().GetStringRepresentation());
+ view->GetValueText()->SetValue(str);
+ return TRUE;
+}
+
+// Called when the property is double clicked. Extra functionality can be provided,
+// cycling through possible values.
+bool wxResourceSymbolValidator::OnDoubleClick(wxProperty *property, wxPropertyListView *view, wxWindow *parentWindow)
+{
+ if (!view->GetValueText())
+ return FALSE;
+ OnEdit(property, view, parentWindow);
+ return TRUE;
+}
+
+bool wxResourceSymbolValidator::OnPrepareControls(wxProperty *WXUNUSED(property), wxPropertyListView *view, wxWindow *WXUNUSED(parentWindow))
+{
+ if (view->GetConfirmButton())
+ view->GetConfirmButton()->Enable(TRUE);
+ if (view->GetCancelButton())
+ view->GetCancelButton()->Enable(TRUE);
+ if (view->GetEditButton())
+ view->GetEditButton()->Enable(TRUE);
+ if (view->GetValueText())
+ view->GetValueText()->Enable((GetFlags() & wxPROP_ALLOW_TEXT_EDITING) == wxPROP_ALLOW_TEXT_EDITING);
+ return TRUE;
+}
+
+void wxResourceSymbolValidator::OnEdit(wxProperty *property, wxPropertyListView *view, wxWindow *parentWindow)
+{
+ if (!view->GetValueText())
+ return;
+
+ wxResourceSymbolDialog* dialog = new wxResourceSymbolDialog(parentWindow, -1, "Edit Symbol");
+
+ // Split name/id pair e.g. "IDC_TEXT=123"
+ wxString value(property->GetValue().StringValue());
+
+ wxString strName = value.Before('=');
+ wxString strId = value.After('=');
+
+ dialog->SetSymbol(strName);
+ dialog->SetId(atoi(strId));
+
+ dialog->Init();
+
+ if (dialog->ShowModal() == wxID_OK)
+ {
+ wxString symbolName(dialog->GetSymbol());
+ long id = dialog->GetId();
+
+ wxString str;
+ str.Printf("%d", id);
+ property->GetValue() = symbolName + wxString("=") + str;
+
+ view->DisplayProperty(property);
+ view->UpdatePropertyDisplayInList(property);
+ view->OnPropertyChanged(property);
+ }
+ // Moved from the 'if' branch on suggestion of Roman Pavlov
+ dialog->Destroy();
+}
+
+BEGIN_EVENT_TABLE(wxResourceSymbolDialog, wxDialog)
+EVT_BUTTON(wxID_OK, wxResourceSymbolDialog::OnOK)
+EVT_COMBOBOX(ID_SYMBOLNAME_COMBOBOX, wxResourceSymbolDialog::OnComboBoxSelect)
+EVT_TEXT(ID_SYMBOLNAME_COMBOBOX, wxResourceSymbolDialog::OnSymbolNameUpdate)
+END_EVENT_TABLE()
+
+wxResourceSymbolDialog::wxResourceSymbolDialog(wxWindow* parent, const wxWindowID id, const wxString& title, const wxPoint& pos,
+ const wxSize& size, long style):
+wxDialog(parent, id, title, pos, size, style)
+{
+ int x = 5;
+ int y = 5;
+
+ (void) new wxStaticText(this, -1, "Name: ", wxPoint(x, y));
+
+ x += 80;
+
+ m_nameCtrl = new wxComboBox(this, ID_SYMBOLNAME_COMBOBOX, "",
+ wxPoint(x, y), wxSize(200, -1), 0, NULL, wxCB_DROPDOWN|wxCB_SORT);
+
+ y += 30;
+ x = 5;
+
+ (void) new wxStaticText(this, -1, "Id: ", wxPoint(x, y));
+
+ x += 80;
+
+ m_idCtrl = new wxTextCtrl(this, ID_SYMBOLID_TEXTCTRL, "",
+ wxPoint(x, y), wxSize(200, -1));
+
+ y += 30;
+ x = 5;
+ (void) new wxButton(this, wxID_OK, "OK", wxPoint(x, y), wxSize(80, -1));
+
+ x += 100;
+ (void) new wxButton(this, wxID_CANCEL, "Cancel", wxPoint(x, y), wxSize(80, -1));
+
+ Fit();
+ Centre();
+}
+
+void wxResourceSymbolDialog::Init()
+{
+ wxString defaultId;
+ defaultId.Printf("%ld", m_symbolId);
+
+ m_nameCtrl->SetValue(m_symbolName);
+ m_idCtrl->SetValue(defaultId);
+
+ wxResourceManager::GetCurrentResourceManager()->GetSymbolTable().FillComboBox(m_nameCtrl);
+}
+
+void wxResourceSymbolDialog::OnOK(wxCommandEvent& event)
+{
+ if (CheckValues())
+ {
+ wxDialog::OnOK(event);
+ }
+}
+
+bool wxResourceSymbolDialog::CheckValues()
+{
+ wxString nameStr(m_nameCtrl->GetValue());
+ wxString idStr(m_idCtrl->GetValue());
+ int id = atoi(idStr);
+
+ if (id <= 0 )
+ {
+ wxMessageBox("Identifier cannot be missing or zero", "Dialog Editor", wxOK|wxICON_EXCLAMATION, this);
+ return FALSE;
+ }
+ if (nameStr == "")
+ {
+ wxMessageBox("Please enter a symbol name", "Dialog Editor", wxOK|wxICON_EXCLAMATION, this);
+ return FALSE;
+ }
+ if (nameStr.Contains(" "))
+ {
+ wxMessageBox("Symbol name cannot contain spaces.", "Dialog Editor", wxOK|wxICON_EXCLAMATION, this);
+ return FALSE;
+ }
+ if (nameStr.Contains("="))
+ {
+ wxMessageBox("Symbol name cannot contain =.", "Dialog Editor", wxOK|wxICON_EXCLAMATION, this);
+ return FALSE;
+ }
+ if (nameStr.IsNumber())
+ {
+ wxMessageBox("Symbol name cannot be a number.", "Dialog Editor", wxOK|wxICON_EXCLAMATION, this);
+ return FALSE;
+ }
+ // TODO: other checks on the name syntax.
+
+ if (!wxResourceManager::GetCurrentResourceManager()->GetSymbolTable().IsStandardSymbol(nameStr))
+ {
+ // If we change the id for an existing symbol, we need to:
+ // 1) Check if there are any other resources currently using the original id.
+ // If so, will need to change their id to the new id, in SetProperty.
+ // 2) Remove the old symbol, add the new symbol.
+ // In this check, we don't have to do this, but we need to do it in SetProperty.
+
+ if (nameStr == GetSymbol() && id != GetId())
+ {
+ // It's OK to change the id. But we'll need to change all matching ids in all resources,
+ // in SetProperty.
+ }
+
+ // If we change the name but not the id... we'll just need to remove and
+ // re-add the symbol/id pair, in SetProperty.
+ if (nameStr != GetSymbol() && id == GetId())
+ {
+ }
+
+ // What if we're changing both the name and the id?
+ // - if there's no symbol of that name, just remove the old, add the new (in SetProperty)
+ // - if there is a symbol of that name, if id matches, do nothing. If not, veto.
+
+ if (nameStr != GetSymbol() && id != GetId())
+ {
+ if (!wxResourceManager::GetCurrentResourceManager()->IsIdentifierOK(nameStr, id))
+ {
+ wxMessageBox("This integer id is already being used under a different name.\nPlease choose another.",
+ "Dialog Editor", wxOK|wxICON_EXCLAMATION, this);
+ return FALSE;
+ }
+ }
+
+ }
+
+ SetSymbol(nameStr);
+ SetId(id);
+
+ return TRUE;
+}
+
+void wxResourceSymbolDialog::OnComboBoxSelect(wxCommandEvent& WXUNUSED(event))
+{
+ wxString str(m_nameCtrl->GetStringSelection());
+ if (wxResourceManager::GetCurrentResourceManager()->GetSymbolTable().IsStandardSymbol(str))
+ {
+ int id = wxResourceManager::GetCurrentResourceManager()->GetSymbolTable().GetIdForSymbol(str);
+ wxString str2;
+ str2.Printf("%d", id);
+ m_idCtrl->SetValue(str2);
+ m_idCtrl->Enable(FALSE);
+ }
+ else
+ {
+ if (wxResourceManager::GetCurrentResourceManager()->GetSymbolTable().SymbolExists(str))
+ {
+ int id = wxResourceManager::GetCurrentResourceManager()->GetSymbolTable().GetIdForSymbol(str);
+ wxString str2;
+ str2.Printf("%d", id);
+ m_idCtrl->SetValue(str2);
+ }
+ m_idCtrl->Enable(TRUE);
+ }
+}
+
+void wxResourceSymbolDialog::OnSymbolNameUpdate(wxCommandEvent& WXUNUSED(event))
+{
+ wxString str(m_nameCtrl->GetValue());
+ if (wxResourceManager::GetCurrentResourceManager()->GetSymbolTable().IsStandardSymbol(str))
+ {
+ int id = wxResourceManager::GetCurrentResourceManager()->GetSymbolTable().GetIdForSymbol(str);
+ wxString str2;
+ str2.Printf("%d", id);
+ m_idCtrl->SetValue(str2);
+ m_idCtrl->Enable(FALSE);
+ }
+ else
+ {
+ if (wxResourceManager::GetCurrentResourceManager()->GetSymbolTable().SymbolExists(str))
+ {
+ int id = wxResourceManager::GetCurrentResourceManager()->GetSymbolTable().GetIdForSymbol(str);
+ wxString str2;
+ str2.Printf("%d", id);
+ m_idCtrl->SetValue(str2);
+ }
+ m_idCtrl->Enable(TRUE);
+ }
+}
+