+bool CeditorDlg::Initialize()
+{
+ // Create the data structure and a new database connection.
+ // (As there is not a pDb being passed in the constructor, a new database
+ // connection is created)
+ Contact = new Ccontact();
+
+ if (!Contact)
+ {
+ wxMessageBox(wxT("Unable to instantiate an instance of Ccontact"),wxT("Error..."),wxOK | wxICON_EXCLAMATION);
+ return FALSE;
+ }
+
+ // Check if the table exists or not. If it doesn't, ask the user if they want to
+ // create the table. Continue trying to create the table until it exists, or user aborts
+ while (!Contact->GetDb()->TableExists((wxChar *)CONTACT_TABLE_NAME,
+ wxGetApp().DbConnectInf->GetUserID(),
+ wxGetApp().DbConnectInf->GetDefaultDir()))
+ {
+ wxString tStr;
+ tStr.Printf(wxT("Unable to open the table '%s'.\n\nTable may need to be created...?\n\n"),CONTACT_TABLE_NAME);
+ tStr += GetExtendedDBErrorMsg(Contact->GetDb(),__FILE__,__LINE__);
+ wxMessageBox(tStr,wxT("ODBC Error..."),wxOK | wxICON_EXCLAMATION);
+
+ bool createTable = (wxMessageBox(wxT("Do you wish to try to create/clear the CONTACTS table?"),wxT("Confirm"),wxYES_NO|wxICON_QUESTION) == wxYES);
+
+ if (!createTable)
+ {
+// Close();
+ return FALSE;
+ }
+ else
+ wxGetApp().CreateDataTable(TRUE);
+ }
+
+ // Tables must be "opened" before anything other than creating/deleting table can be done
+ if (!Contact->Open())
+ {
+ // Table does exist, or there was some problem opening it. Currently this should
+ // never fail, except in the case of the table not exisiting or the current
+ // user has insufficent privileges to access the table
+#if 0
+// This code is experimenting with a new function that will hopefully be available
+// in the 2.4 release. This check will determine whether the open failing was due
+// to the table not existing, or the users privileges being insufficient to
+// open the table.
+ if (!Contact->GetDb()->TablePrivileges(CONTACT_TABLE_NAME,wxT("SELECT"),Contact->GetDb()->GetUsername(),Contact->GetDb()->GetUsername(),DbConnectInf->GetDefaultDir()))
+ {
+ wxString tStr;
+ tStr.Printf(wxT("Unable to open the table '%s'.\n\n"),CONTACT_TABLE_NAME);
+ tStr += GetExtendedDBErrorMsg(Contact->GetDb(),__FILE__,__LINE__);
+ wxMessageBox(tStr,wxT("ODBC Error..."),wxOK | wxICON_EXCLAMATION);
+ }
+ else
+#endif
+ if (Contact->GetDb()->TableExists(CONTACT_TABLE_NAME, Contact->GetDb()->GetUsername(),
+ wxGetApp().DbConnectInf->GetDefaultDir()))
+ {
+ wxString tStr;
+ tStr.Printf(wxT("Unable to open the table '%s'.\n\n"),CONTACT_TABLE_NAME);
+ tStr += GetExtendedDBErrorMsg(Contact->GetDb(),__FILE__,__LINE__);
+ wxMessageBox(tStr,wxT("ODBC Error..."),wxOK | wxICON_EXCLAMATION);
+ }
+
+ return FALSE;
+ }
+
+ // Build the dialog
+
+ (void)new wxStaticBox(this, EDITOR_DIALOG_FN_GROUP, wxT(""), wxPoint(15, 1), wxSize(497, 69), 0, wxT("FunctionGrp"));
+ (void)new wxStaticBox(this, EDITOR_DIALOG_SEARCH_GROUP, wxT(""), wxPoint(417, 1), wxSize(95, 242), 0, wxT("SearchGrp"));
+
+ pCreateBtn = new wxButton(this, EDITOR_DIALOG_CREATE, wxT("&Create"), wxPoint( 25, 21), wxSize( 70, 35), 0, wxDefaultValidator, wxT("CreateBtn"));
+ pEditBtn = new wxButton(this, EDITOR_DIALOG_EDIT, wxT("&Edit"), wxPoint(102, 21), wxSize( 70, 35), 0, wxDefaultValidator, wxT("EditBtn"));
+ pDeleteBtn = new wxButton(this, EDITOR_DIALOG_DELETE, wxT("&Delete"), wxPoint(179, 21), wxSize( 70, 35), 0, wxDefaultValidator, wxT("DeleteBtn"));
+ pCopyBtn = new wxButton(this, EDITOR_DIALOG_COPY, wxT("Cop&y"), wxPoint(256, 21), wxSize( 70, 35), 0, wxDefaultValidator, wxT("CopyBtn"));
+ pSaveBtn = new wxButton(this, EDITOR_DIALOG_SAVE, wxT("&Save"), wxPoint(333, 21), wxSize( 70, 35), 0, wxDefaultValidator, wxT("SaveBtn"));
+ pCancelBtn = new wxButton(this, EDITOR_DIALOG_CANCEL, wxT("C&ancel"), wxPoint(430, 21), wxSize( 70, 35), 0, wxDefaultValidator, wxT("CancelBtn"));
+ pPrevBtn = new wxButton(this, EDITOR_DIALOG_PREV, wxT("<< &Prev"), wxPoint(430, 81), wxSize( 70, 35), 0, wxDefaultValidator, wxT("PrevBtn"));
+ pNextBtn = new wxButton(this, EDITOR_DIALOG_NEXT, wxT("&Next >>"), wxPoint(430, 121), wxSize( 70, 35), 0, wxDefaultValidator, wxT("NextBtn"));
+ pQueryBtn = new wxButton(this, EDITOR_DIALOG_QUERY, wxT("&Query"), wxPoint(430, 161), wxSize( 70, 35), 0, wxDefaultValidator, wxT("QueryBtn"));
+ pResetBtn = new wxButton(this, EDITOR_DIALOG_RESET, wxT("&Reset"), wxPoint(430, 200), wxSize( 70, 35), 0, wxDefaultValidator, wxT("ResetBtn"));
+ pNameMsg = new wxStaticText(this, EDITOR_DIALOG_NAME_MSG, wxT("Name:"), wxPoint( 17, 80), wxSize( -1, -1), 0, wxT("NameMsg"));
+ pNameTxt = new wxTextCtrl(this, EDITOR_DIALOG_NAME_TEXT, wxT(""), wxPoint( 17, 97), wxSize(308, 25), 0, wxDefaultValidator, wxT("NameTxt"));
+ pNameListBtn = new wxButton(this, EDITOR_DIALOG_LOOKUP, wxT("&Lookup"), wxPoint(333, 97), wxSize( 70, 24), 0, wxDefaultValidator, wxT("LookupBtn"));
+ pAddress1Msg = new wxStaticText(this, EDITOR_DIALOG_ADDRESS1_MSG, wxT("Address:"), wxPoint( 17, 130), wxSize( -1, -1), 0, wxT("Address1Msg"));
+ pAddress1Txt = new wxTextCtrl(this, EDITOR_DIALOG_ADDRESS2_TEXT, wxT(""), wxPoint( 17, 147), wxSize(308, 25), 0, wxDefaultValidator, wxT("Address1Txt"));
+ pAddress2Msg = new wxStaticText(this, EDITOR_DIALOG_ADDRESS2_MSG, wxT("Address:"), wxPoint( 17, 180), wxSize( -1, -1), 0, wxT("Address2Msg"));
+ pAddress2Txt = new wxTextCtrl(this, EDITOR_DIALOG_ADDRESS2_TEXT, wxT(""), wxPoint( 17, 197), wxSize(308, 25), 0, wxDefaultValidator, wxT("Address2Txt"));
+ pCityMsg = new wxStaticText(this, EDITOR_DIALOG_CITY_MSG, wxT("City:"), wxPoint( 17, 230), wxSize( -1, -1), 0, wxT("CityMsg"));
+ pCityTxt = new wxTextCtrl(this, EDITOR_DIALOG_CITY_TEXT, wxT(""), wxPoint( 17, 247), wxSize(225, 25), 0, wxDefaultValidator, wxT("CityTxt"));
+ pStateMsg = new wxStaticText(this, EDITOR_DIALOG_STATE_MSG, wxT("State:"), wxPoint(250, 230), wxSize( -1, -1), 0, wxT("StateMsg"));
+ pStateTxt = new wxTextCtrl(this, EDITOR_DIALOG_STATE_TEXT, wxT(""), wxPoint(250, 247), wxSize(153, 25), 0, wxDefaultValidator, wxT("StateTxt"));
+ pCountryMsg = new wxStaticText(this, EDITOR_DIALOG_COUNTRY_MSG, wxT("Country:"), wxPoint( 17, 280), wxSize( -1, -1), 0, wxT("CountryMsg"));
+ pCountryTxt = new wxTextCtrl(this, EDITOR_DIALOG_COUNTRY_TEXT, wxT(""), wxPoint( 17, 297), wxSize(225, 25), 0, wxDefaultValidator, wxT("CountryTxt"));
+ pPostalCodeMsg = new wxStaticText(this, EDITOR_DIALOG_POSTAL_MSG, wxT("Postal Code:"),wxPoint(250, 280), wxSize( -1, -1), 0, wxT("PostalCodeMsg"));
+ pPostalCodeTxt = new wxTextCtrl(this, EDITOR_DIALOG_POSTAL_TEXT, wxT(""), wxPoint(250, 297), wxSize(153, 25), 0, wxDefaultValidator, wxT("PostalCodeTxt"));
+
+ wxString choice_strings[5];
+ choice_strings[0] = wxT("English");
+ choice_strings[1] = wxT("French");
+ choice_strings[2] = wxT("German");
+ choice_strings[3] = wxT("Spanish");
+ choice_strings[4] = wxT("Other");
+
+ pNativeLangChoice = new wxChoice(this, EDITOR_DIALOG_LANG_CHOICE, wxPoint( 17, 346), wxSize(277, -1), 5, choice_strings);
+ pNativeLangMsg = new wxStaticText(this, EDITOR_DIALOG_LANG_MSG, wxT("Native language:"), wxPoint( 17, 330), wxSize( -1, -1), 0, wxT("NativeLangMsg"));
+
+ wxString radio_strings[2];
+ radio_strings[0] = wxT("No");
+ radio_strings[1] = wxT("Yes");
+ pDeveloperRadio = new wxRadioBox(this,EDITOR_DIALOG_DEVELOPER, wxT("Developer:"), wxPoint(303, 330), wxSize( -1, -1), 2, radio_strings, 2, wxHORIZONTAL);
+ pJoinDateMsg = new wxStaticText(this, EDITOR_DIALOG_JOIN_MSG, wxT("Date joined:"), wxPoint( 17, 380), wxSize( -1, -1), 0, wxT("JoinDateMsg"));
+ pJoinDateTxt = new wxTextCtrl(this, EDITOR_DIALOG_JOIN_TEXT, wxT(""), wxPoint( 17, 397), wxSize(150, 25), 0, wxDefaultValidator, wxT("JoinDateTxt"));
+ pContribMsg = new wxStaticText(this, EDITOR_DIALOG_CONTRIB_MSG,wxT("Contributions:"), wxPoint(175, 380), wxSize( -1, -1), 0, wxT("ContribMsg"));
+ pContribTxt = new wxTextCtrl(this, EDITOR_DIALOG_CONTRIB_TEXT, wxT(""), wxPoint(175, 397), wxSize(120, 25), 0, wxDefaultValidator, wxT("ContribTxt"));
+ pLinesMsg = new wxStaticText(this, EDITOR_DIALOG_LINES_MSG, wxT("Lines of code:"), wxPoint(303, 380), wxSize( -1, -1), 0, wxT("LinesMsg"));
+ pLinesTxt = new wxTextCtrl(this, EDITOR_DIALOG_LINES_TEXT, wxT(""), wxPoint(303, 397), wxSize(100, 25), 0, wxDefaultValidator, wxT("LinesTxt"));
+
+ // Now that all the widgets on the panel are created, its safe to allow ::OnCommand() to
+ // handle all widget processing
+ widgetPtrsSet = TRUE;
+
+ // Setup the orderBy and where clauses to return back a single record as the result set,
+ // as there will only be one record being shown on the dialog at a time, this optimizes
+ // network traffic by only returning a one row result
+
+ Contact->SetOrderByClause(wxT("NAME")); // field name to sort by
+
+ // The wxString "whereStr" is not a member of the wxDbTable object, it is a member variable
+ // specifically in the Ccontact class. It is used here for simpler construction of a varying
+ // length string, and then after the string is built, the wxDbTable member variable "where" is
+ // assigned the pointer to the constructed string.
+ //
+ // The constructed where clause below has a sub-query within it "SELECT MIN(NAME) FROM %s"
+ // to achieve a single row (in this case the first name in alphabetical order).
+
+ if (Contact->GetDb()->Dbms() != dbmsPOSTGRES && Contact->GetDb()->Dbms() != dbmsMY_SQL)
+ {
+ Contact->whereStr.Printf(wxT("NAME = (SELECT MIN(NAME) FROM %s)"),Contact->GetTableName().c_str());
+ // NOTE: (const wxChar*) returns a pointer which may not be valid later, so this is short term use only
+ Contact->SetWhereClause(Contact->whereStr);
+ }
+ else
+ Contact->SetWhereClause(wxT(""));
+
+ // Perform the Query to get the result set.
+ // NOTE: If there are no rows returned, that is a valid result, so Query() would return TRUE.
+ // Only if there is a database error will Query() come back as FALSE
+ if (!Contact->Query())
+ {
+ wxString tStr;
+ tStr = wxT("ODBC error during Query()\n\n");
+ tStr += GetExtendedDBErrorMsg(Contact->GetDb(),__FILE__,__LINE__);
+ wxMessageBox(tStr,wxT("ODBC Error..."),wxOK | wxICON_EXCLAMATION);
+// GetParent()->Close();
+ return FALSE;
+ }
+
+ // Since Query succeeded, now get the row that was returned
+ if (!Contact->GetNext())
+ // If the GetNext() failed at this point, then there are no rows to retrieve,
+ // so clear the values in the members of "Contact" so that PutData() blanks the
+ // widgets on the dialog
+ Contact->Initialize();
+
+ SetMode(mView);
+ PutData();
+
+ Show(TRUE);
+
+ initialized = TRUE;
+ return TRUE;
+} // CeditorDlg::Initialize()
+
+