]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/db/listdb.cpp
Committing in .
[wxWidgets.git] / samples / db / listdb.cpp
index 330c79db2adbc29eb6efcff8743f58de94fd53af..675b9b97ba350ad1dbc221771cce23ecb97f1f3b 100644 (file)
@@ -56,6 +56,8 @@
 
 #include <wx/dbtable.h>
 
+extern DbList* WXDLLEXPORT PtrBegDbList;    /* from db.cpp, used in getting back error results from db connections */
+
 #include "listdb.h"
 
 // Global structure for holding ODBC connection information
@@ -75,11 +77,68 @@ char ListDB_Selection[LOOKUP_COL_LEN+1];
 char ListDB_Selection2[LOOKUP_COL_LEN+1];
 
 // Constants
-const LISTDB_NO_SPACES_BETWEEN_COLS = 3;
+const int LISTDB_NO_SPACES_BETWEEN_COLS = 3;
+
+
+
+/*
+ * This function will return the exact string(s) from the database engine
+ * indicating all error conditions which have just occured during the
+ * last call to the database engine.
+ *
+ * This demo uses the returned string by displaying it in a wxMessageBox.  The
+ * formatting therefore is not the greatest, but this is just a demo, not a
+ * finished product.  :-) gt
+ *
+ * NOTE: The value returned by this function is for temporary use only and
+ *       should be copied for long term use
+ */
+char *GetExtendedDBErrorMsg2(char *ErrFile, int ErrLine)
+{
+       static wxString msg;
+
+       wxString tStr;
+
+       if (ErrFile || ErrLine)
+       {
+               msg += "File: ";
+               msg += ErrFile;
+               msg += "   Line: ";
+               tStr.Printf("%d",ErrLine);
+               msg += tStr.GetData();
+               msg += "\n";
+       }
+
+       msg.Append ("\nODBC errors:\n");
+       msg += "\n";
+       
+       /* Scan through each database connection displaying 
+        * any ODBC errors that have occured. */
+       for (DbList *pDbList = PtrBegDbList; pDbList; pDbList = pDbList->PtrNext)
+       {
+               // Skip over any free connections
+               if (pDbList->Free)
+                       continue;
+               // Display errors for this connection
+               for (int i = 0; i < DB_MAX_ERROR_HISTORY; i++)
+               {
+                       if (pDbList->PtrDb->errorList[i])
+                       {
+                               msg.Append(pDbList->PtrDb->errorList[i]);
+                               if (strcmp(pDbList->PtrDb->errorList[i],"") != 0)
+                                       msg.Append("\n");
+                       }
+               }
+       }
+       msg += "\n";
+
+       return (char*) (const char*) msg;
+}  // GetExtendedDBErrorMsg
+
 
 
 // Clookup constructor
-Clookup::Clookup(char *tblName, char *colName) : wxTable(READONLY_DB, tblName, 1)
+Clookup::Clookup(char *tblName, char *colName) : wxTable(READONLY_DB, tblName, 1, NULL, !QUERY_ONLY, DbConnectInf.defaultDir)
 {
 
        SetColDefs (0, colName, DB_DATA_TYPE_VARCHAR, lookupCol,        SQL_C_CHAR, LOOKUP_COL_LEN+1, FALSE, FALSE);
@@ -89,7 +148,7 @@ Clookup::Clookup(char *tblName, char *colName) : wxTable(READONLY_DB, tblName, 1
 
 // Clookup2 constructor
 Clookup2::Clookup2(char *tblName, char *colName1, char *colName2, wxDB *pDb)
-   : wxTable(pDb, tblName, (1 + (strlen(colName2) > 0)))
+   : wxTable(pDb, tblName, (1 + (strlen(colName2) > 0)), NULL, !QUERY_ONLY, DbConnectInf.defaultDir)
 {
        int i = 0;
 
@@ -101,9 +160,16 @@ Clookup2::Clookup2(char *tblName, char *colName1, char *colName2, wxDB *pDb)
 }  // Clookup2()
 
 
+BEGIN_EVENT_TABLE(ClookUpDlg, wxDialog)
+//      EVT_LISTBOX(LOOKUP_DIALOG_SELECT, ClookUpDlg::SelectCallback)
+    EVT_BUTTON(LOOKUP_DIALOG_OK,  ClookUpDlg::OnButton)
+    EVT_BUTTON(LOOKUP_DIALOG_CANCEL,  ClookUpDlg::OnButton)
+    EVT_CLOSE(ClookUpDlg::OnClose)
+END_EVENT_TABLE()
+
 // This is a generic lookup constructor that will work with any table and any column
 ClookUpDlg::ClookUpDlg(wxWindow *parent, char *windowTitle, char *tableName, char *colName,
-       char *where, char *orderBy)  : wxDialogBox (parent, "Select...", 1, -1, -1, 400, 290)
+       char *where, char *orderBy)  : wxDialog (parent, LOOKUP_DIALOG, "Select...", wxPoint(-1, -1), wxSize(400, 290))
 {
        wxBeginBusyCursor();
        
@@ -114,19 +180,9 @@ ClookUpDlg::ClookUpDlg(wxWindow *parent, char *windowTitle, char *tableName, cha
        noDisplayCols = 1;
        col1Len = 0;
 
-       // Build the dialog
-       SetLabelPosition(wxVERTICAL);
-
-       wxFont *ButtonFont = new wxFont(12,wxSWISS,wxNORMAL,wxBOLD);
-       wxFont *TextFont   = new wxFont(12,wxSWISS,wxNORMAL,wxNORMAL);
-
-       SetButtonFont(ButtonFont);
-       SetLabelFont(TextFont);
-       SetLabelPosition(wxVERTICAL);
-
-       pLookUpSelectList               = new wxListBox(this, NULL, "", wxSINGLE|wxALWAYS_SB, 5, 15, 384, 195, 0, 0, 0, "LookUpSelectList");
-       pLookUpOkBtn                    = new wxButton(this, NULL, "&Ok",               113, 222, 70, 35, 0, "LookUpOkBtn");
-       pLookUpCancelBtn                = new wxButton(this, NULL, "C&ancel",   212, 222, 70, 35, 0, "LookUpCancelBtn");
+       pLookUpSelectList               = new wxListBox(this, LOOKUP_DIALOG_SELECT, wxPoint(5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE|wxLB_ALWAYS_SB, wxDefaultValidator, "LookUpSelectList");
+       pLookUpOkBtn                    = new wxButton(this, LOOKUP_DIALOG_OK, "&Ok",           wxPoint(113, 222), wxSize(70, 35), 0, wxDefaultValidator, "LookUpOkBtn");
+       pLookUpCancelBtn                = new wxButton(this, LOOKUP_DIALOG_CANCEL, "C&ancel",   wxPoint(212, 222), wxSize(70, 35), 0, wxDefaultValidator, "LookUpCancelBtn");
 
        widgetPtrsSet = TRUE;
 
@@ -141,8 +197,8 @@ ClookUpDlg::ClookUpDlg(wxWindow *parent, char *windowTitle, char *tableName, cha
        if (!lookup->Open())
        {
                wxString tStr;
-               tStr.sprintf("Unable to open the table '%s'.",tableName);
-               wxMessageBox(tStr.GetData(),"ODBC Error...");
+               tStr.Printf("Unable to open the table '%s'.",tableName);
+               wxMessageBox(tStr,"ODBC Error...");
                Close();
                return;
        }
@@ -176,7 +232,7 @@ ClookUpDlg::ClookUpDlg(wxWindow *parent, char *windowTitle, char *tableName, cha
        SetTitle(windowTitle);
        Centre(wxBOTH);
        wxEndBusyCursor();
-       Show(TRUE);
+       ShowModal();
 
 }  // Generic lookup constructor
 
@@ -207,7 +263,7 @@ ClookUpDlg::ClookUpDlg(wxWindow *parent, char *windowTitle, char *tableName, cha
 //
 ClookUpDlg::ClookUpDlg(wxWindow *parent, char *windowTitle, char *tableName,
        char *dispCol1, char *dispCol2, char *where, char *orderBy, bool distinctValues,
-       char *selectStmt, int maxLenCol1, wxDB *pDb, bool allowOk)  : wxDialogBox (parent, "Select...", 1, -1, -1, 400, 290)
+       char *selectStmt, int maxLenCol1, wxDB *pDb, bool allowOk)  : wxDialog (parent, LOOKUP_DIALOG, "Select...", wxPoint(-1, -1), wxSize(400, 290))
 {
        wxBeginBusyCursor();
        
@@ -219,24 +275,16 @@ ClookUpDlg::ClookUpDlg(wxWindow *parent, char *windowTitle, char *tableName,
        noDisplayCols = (strlen(dispCol2) ? 2 : 1);
        col1Len = 0;
 
-       // Build the dialog
-       SetLabelPosition(wxVERTICAL);
-
-       wxFont *ButtonFont = new wxFont(12,wxSWISS,wxNORMAL,wxBOLD);
-       wxFont *TextFont   = new wxFont(12,wxSWISS,wxNORMAL,wxNORMAL);
-       wxFont *FixedFont  = new wxFont(12,wxMODERN,wxNORMAL,wxNORMAL);
-
-       SetButtonFont(ButtonFont);
-       SetLabelFont(TextFont);
-       SetLabelPosition(wxVERTICAL);
+       wxFont fixedFont(12,wxMODERN,wxNORMAL,wxNORMAL);
 
        // this is done with fixed font so that the second column (if any) will be left
        // justified in the second column
-       SetButtonFont(FixedFont);
-       pLookUpSelectList               = new wxListBox(this, NULL, "", wxSINGLE|wxALWAYS_SB, 5, 15, 384, 195, 0, 0, 0, "LookUpSelectList");
-       SetButtonFont(ButtonFont);
-       pLookUpOkBtn                    = new wxButton(this, NULL, "&Ok",               113, 222, 70, 35, 0, "LookUpOkBtn");
-       pLookUpCancelBtn                = new wxButton(this, NULL, "C&ancel",   212, 222, 70, 35, 0, "LookUpCancelBtn");
+       pLookUpSelectList               = new wxListBox(this, LOOKUP_DIALOG_SELECT, wxPoint(5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE|wxLB_ALWAYS_SB, wxDefaultValidator, "LookUpSelectList");
+
+    pLookUpSelectList->SetFont(fixedFont);
+
+       pLookUpOkBtn                    = new wxButton(this, LOOKUP_DIALOG_OK, "&Ok",           wxPoint(113, 222), wxSize(70, 35), 0, wxDefaultValidator, "LookUpOkBtn");
+       pLookUpCancelBtn                = new wxButton(this, LOOKUP_DIALOG_CANCEL, "C&ancel",   wxPoint(212, 222), wxSize(70, 35), 0, wxDefaultValidator, "LookUpCancelBtn");
 
        widgetPtrsSet = TRUE;
 
@@ -251,8 +299,9 @@ ClookUpDlg::ClookUpDlg(wxWindow *parent, char *windowTitle, char *tableName,
        if (!lookup2->Open())
        {
                wxString tStr;
-               tStr.sprintf("Unable to open the table '%s'.",tableName);
-               wxMessageBox(tStr.GetData(),"ODBC Error...");
+               tStr.Printf("Unable to open the table '%s'.",tableName);
+               tStr += GetExtendedDBErrorMsg2(__FILE__,__LINE__);
+               wxMessageBox(tStr,"ODBC Error...");
                Close();
                return;
        }
@@ -276,7 +325,7 @@ ClookUpDlg::ClookUpDlg(wxWindow *parent, char *windowTitle, char *tableName,
                                q += " WHERE ";
                                q += where;
                        }
-                       if (!lookup2->QueryBySqlStmt(q.GetData()))
+                       if (!lookup2->QueryBySqlStmt((char*) (const char*) q))
                        {
                                wxMessageBox("ODBC error during QueryBySqlStmt()","ODBC Error...");
                                Close();
@@ -321,7 +370,7 @@ ClookUpDlg::ClookUpDlg(wxWindow *parent, char *windowTitle, char *tableName,
                        s.Append(' ', (maxColLen + LISTDB_NO_SPACES_BETWEEN_COLS - strlen(lookup2->lookupCol1)));
                        s.Append(lookup2->lookupCol2);
                }
-               pLookUpSelectList->Append(s.GetData());
+               pLookUpSelectList->Append(s);
        }
 
        // Highlight the first list item
@@ -342,12 +391,12 @@ ClookUpDlg::ClookUpDlg(wxWindow *parent, char *windowTitle, char *tableName,
        SetTitle(windowTitle);
        Centre(wxBOTH);
        wxEndBusyCursor();
-       Show(TRUE);
+       ShowModal();
 
 }  // Generic lookup constructor 2
 
 
-bool ClookUpDlg::OnClose(void)
+void ClookUpDlg::OnClose(wxCloseEvent& event)
 {
        widgetPtrsSet = FALSE;
        GetParent()->Enable(TRUE);
@@ -357,12 +406,21 @@ bool ClookUpDlg::OnClose(void)
        if (lookup2)
                delete lookup2;
 
-       wxEndBusyCursor();
-       return TRUE;
+       while (wxIsBusy()) wxEndBusyCursor();
+   event.Skip();
+
+//     return TRUE;
 
 }  // ClookUpDlg::OnClose
 
 
+void ClookUpDlg::OnButton( wxCommandEvent &event )
+{
+  wxWindow *win = (wxWindow*) event.GetEventObject();
+  OnCommand( *win, event );
+}
+
+
 void ClookUpDlg::OnCommand(wxWindow& win, wxCommandEvent& event)
 {
        wxString widgetName = win.GetName();
@@ -382,12 +440,12 @@ void ClookUpDlg::OnCommand(wxWindow& win, wxCommandEvent& event)
                                        // Column 1
                                        s = s.SubString(0, col1Len-1);
                                        s = s.Strip();
-                                       strcpy(ListDB_Selection, s.GetData());
+                                       strcpy(ListDB_Selection, s);
                                        // Column 2
                                        s = pLookUpSelectList->GetStringSelection();
-                                       s = s.From(col1Len + LISTDB_NO_SPACES_BETWEEN_COLS);
+                                       s = s.Mid(col1Len + LISTDB_NO_SPACES_BETWEEN_COLS);
                                        s = s.Strip();
-                                       strcpy(ListDB_Selection2, s.GetData());
+                                       strcpy(ListDB_Selection2, s);
                                }
                        }
                        else