1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Data table lookup listbox code
4 // Author: George Tasker/Doug Card
8 // Copyright: (c) 1996 Remstar International, Inc.
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
15 Member functions for the classes defined in LISTDB.H
17 This class is used to present a generic ListBox lookup window for
18 use with any of the object creation/selection choice widgets. This
19 dialog window will present a (possibly) scrolling list of values
20 that come from a data table source. Based on the object type passed
21 in the constructor, a ListBox is built to present the user with a
22 single selection listbox.
24 The string selected from the list box is stored in the Global variable
25 "ListDB_Seclection", and will remain set until another interation of this
28 For each object (database) type that is to be used, an overridden
29 constructor should be written to appropriately link to the proper
30 data table/object for building the list.
32 The data table record access is all handled through the routines
33 in this module, interfacing with the methods defined in wxDbTable.
35 All objects which use data table access must be initialized and
36 have opened the table prior to passing them in the dialog
37 constructor, and the 'where' query should already have been set
38 and performed before creating this dialog instance.
44 #pragma implementation "listdb.h"
47 #include "wx/wxprec.h"
57 #include <wx/dbtable.h>
59 extern wxDbList WXDLLEXPORT
*PtrBegDbList
; /* from db.cpp, used in getting back error results from db connections */
63 // Global structure for holding ODBC connection information
64 extern wxDbConnectInf DbConnectInf
;
66 // Global database connection
67 extern wxDb
*READONLY_DB
;
70 // Used for passing the selected listbox selection back to the calling
71 // routine. This variable must be declared as 'extern' in the calling
73 char ListDB_Selection
[LOOKUP_COL_LEN
+1];
75 // If the listbox contains two columns of data, the second column is
76 // returned in this variable.
77 char ListDB_Selection2
[LOOKUP_COL_LEN
+1];
80 const int LISTDB_NO_SPACES_BETWEEN_COLS
= 3;
85 * This function will return the exact string(s) from the database engine
86 * indicating all error conditions which have just occured during the
87 * last call to the database engine.
89 * This demo uses the returned string by displaying it in a wxMessageBox. The
90 * formatting therefore is not the greatest, but this is just a demo, not a
91 * finished product. :-) gt
93 * NOTE: The value returned by this function is for temporary use only and
94 * should be copied for long term use
96 const char *GetExtendedDBErrorMsg2(char *ErrFile
, int ErrLine
)
103 if (ErrFile
|| ErrLine
)
108 tStr
.Printf("%d",ErrLine
);
113 msg
.Append ("\nODBC errors:\n");
116 /* Scan through each database connection displaying
117 * any ODBC errors that have occured. */
119 for (pDbList
= PtrBegDbList
; pDbList
; pDbList
= pDbList
->PtrNext
)
121 // Skip over any free connections
124 // Display errors for this connection
125 for (int i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
127 if (pDbList
->PtrDb
->errorList
[i
])
129 msg
.Append(pDbList
->PtrDb
->errorList
[i
]);
130 if (wxStrcmp(pDbList
->PtrDb
->errorList
[i
],"") != 0)
132 // Clear the errmsg buffer so the next error will not
133 // end up showing the previous error that have occurred
134 wxStrcpy(pDbList
->PtrDb
->errorList
[i
],"");
140 return /*(char*) (const char*) msg*/msg
.c_str();
141 } // GetExtendedDBErrorMsg
145 // Clookup constructor
146 Clookup::Clookup(char *tblName
, char *colName
) : wxDbTable(READONLY_DB
, tblName
, 1, NULL
, !wxDB_QUERY_ONLY
, DbConnectInf
.defaultDir
)
149 SetColDefs (0, colName
, DB_DATA_TYPE_VARCHAR
, lookupCol
, SQL_C_CHAR
, LOOKUP_COL_LEN
+1, FALSE
, FALSE
);
154 // Clookup2 constructor
155 Clookup2::Clookup2(char *tblName
, char *colName1
, char *colName2
, wxDb
*pDb
)
156 : wxDbTable(pDb
, tblName
, (1 + (wxStrlen(colName2
) > 0)), NULL
, !wxDB_QUERY_ONLY
, DbConnectInf
.defaultDir
)
160 SetColDefs (i
, colName1
, DB_DATA_TYPE_VARCHAR
, lookupCol1
, SQL_C_CHAR
, LOOKUP_COL_LEN
+1, FALSE
, FALSE
);
162 if (wxStrlen(colName2
) > 0)
163 SetColDefs (++i
, colName2
, DB_DATA_TYPE_VARCHAR
, lookupCol2
, SQL_C_CHAR
, LOOKUP_COL_LEN
+1, FALSE
, FALSE
);
168 BEGIN_EVENT_TABLE(ClookUpDlg
, wxDialog
)
169 // EVT_LISTBOX(LOOKUP_DIALOG_SELECT, ClookUpDlg::SelectCallback)
170 EVT_BUTTON(LOOKUP_DIALOG_OK
, ClookUpDlg::OnButton
)
171 EVT_BUTTON(LOOKUP_DIALOG_CANCEL
, ClookUpDlg::OnButton
)
172 EVT_CLOSE(ClookUpDlg::OnClose
)
175 // This is a generic lookup constructor that will work with any table and any column
176 ClookUpDlg::ClookUpDlg(wxWindow
*parent
, char *windowTitle
, char *tableName
, char *colName
,
177 char *where
, char *orderBy
) : wxDialog (parent
, LOOKUP_DIALOG
, "Select...", wxPoint(-1, -1), wxSize(400, 290))
181 wxStrcpy(ListDB_Selection
,"");
182 widgetPtrsSet
= FALSE
;
188 pLookUpSelectList
= new wxListBox(this, LOOKUP_DIALOG_SELECT
, wxPoint( 5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE
|wxLB_ALWAYS_SB
, wxDefaultValidator
, "LookUpSelectList");
189 pLookUpOkBtn
= new wxButton(this, LOOKUP_DIALOG_OK
, "&Ok", wxPoint(113, 222), wxSize( 70, 35), 0, wxDefaultValidator
, "LookUpOkBtn");
190 pLookUpCancelBtn
= new wxButton(this, LOOKUP_DIALOG_CANCEL
, "C&ancel", wxPoint(212, 222), wxSize( 70, 35), 0, wxDefaultValidator
, "LookUpCancelBtn");
192 widgetPtrsSet
= TRUE
;
194 // Query the lookup table and display the result set
195 if (!(lookup
= new Clookup(tableName
, colName
)))
197 wxMessageBox("Error allocating memory for 'Clookup'object.","Error...");
205 tStr
.Printf("Unable to open the table '%s'.",tableName
);
206 wxMessageBox(tStr
,"ODBC Error...");
211 lookup
->SetOrderByClause(orderBy
);
212 lookup
->SetWhereClause(where
);
213 if (!lookup
->Query())
215 wxMessageBox("ODBC error during Query()","ODBC Error...");
220 // Fill in the list box from the query result set
221 while (lookup
->GetNext())
222 pLookUpSelectList
->Append(lookup
->lookupCol
);
224 // Highlight the first list item
225 pLookUpSelectList
->SetSelection(0);
227 // Make the OK activate by pressing Enter
228 if (pLookUpSelectList
->Number())
229 pLookUpOkBtn
->SetDefault();
232 pLookUpCancelBtn
->SetDefault();
233 pLookUpOkBtn
->Enable(FALSE
);
236 // Display the dialog window
237 SetTitle(windowTitle
);
242 } // Generic lookup constructor
246 // This is a generic lookup constructor that will work with any table and any column.
247 // It extends the capabilites of the lookup dialog in the following ways:
249 // 1) 2 columns rather than one
250 // 2) The ability to select DISTINCT column values
252 // Only set distinctValues equal to true if necessary. In many cases, the constraints
253 // of the index(es) will enforce this uniqueness. Selecting DISTINCT does require
254 // overhead by the database to ensure that all values returned are distinct. Therefore,
255 // use this ONLY when you need it.
257 // For complicated queries, you can pass in the sql select statement. This would be
258 // necessary if joins are involved since by default both columns must come from the
261 // If you do query by sql statement, you must pass in the maximum lenght of column1,
262 // since it cannot be derived when you query using your own sql statement.
264 // The optional database connection can be used if you'd like the lookup class
265 // to use a database pointer other than the global READONLY_DB. This is necessary if
266 // records are being saved, but not committed to the db, yet should be included
267 // in the lookup window.
269 ClookUpDlg::ClookUpDlg(wxWindow
*parent
, char *windowTitle
, char *tableName
,
270 char *dispCol1
, char *dispCol2
, char *where
, char *orderBy
, bool distinctValues
,
271 char *selectStmt
, int maxLenCol1
, wxDb
*pDb
, bool allowOk
) : wxDialog (parent
, LOOKUP_DIALOG
, "Select...", wxPoint(-1, -1), wxSize(400, 290))
275 wxStrcpy(ListDB_Selection
,"");
276 wxStrcpy(ListDB_Selection2
,"");
277 widgetPtrsSet
= FALSE
;
280 noDisplayCols
= (wxStrlen(dispCol2
) ? 2 : 1);
283 wxFont
fixedFont(12,wxMODERN
,wxNORMAL
,wxNORMAL
);
285 // this is done with fixed font so that the second column (if any) will be left
286 // justified in the second column
287 pLookUpSelectList
= new wxListBox(this, LOOKUP_DIALOG_SELECT
, wxPoint(5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE
|wxLB_ALWAYS_SB
, wxDefaultValidator
, "LookUpSelectList");
289 pLookUpSelectList
->SetFont(fixedFont
);
291 pLookUpOkBtn
= new wxButton(this, LOOKUP_DIALOG_OK
, "&Ok", wxPoint(113, 222), wxSize(70, 35), 0, wxDefaultValidator
, "LookUpOkBtn");
292 pLookUpCancelBtn
= new wxButton(this, LOOKUP_DIALOG_CANCEL
, "C&ancel", wxPoint(212, 222), wxSize(70, 35), 0, wxDefaultValidator
, "LookUpCancelBtn");
294 widgetPtrsSet
= TRUE
;
296 // Query the lookup table and display the result set
297 if (!(lookup2
= new Clookup2(tableName
, dispCol1
, dispCol2
, pDb
)))
299 wxMessageBox("Error allocating memory for 'Clookup2'object.","Error...");
304 if (!lookup2
->Open())
307 tStr
.Printf("Unable to open the table '%s'.",tableName
);
308 tStr
+= GetExtendedDBErrorMsg2(__FILE__
,__LINE__
);
309 wxMessageBox(tStr
,"ODBC Error...");
314 // If displaying 2 columns, determine the maximum length of column1
317 maxColLen
= col1Len
= maxLenCol1
; // user passed in max col length for column 1
320 maxColLen
= LOOKUP_COL_LEN
;
321 if (wxStrlen(dispCol2
))
323 wxString q
= "SELECT MAX({fn LENGTH(";
333 if (!lookup2
->QueryBySqlStmt((char*) (const char*) q
))
335 wxMessageBox("ODBC error during QueryBySqlStmt()","ODBC Error...");
339 if (lookup2
->GetNext())
340 maxColLen
= col1Len
= atoi(lookup2
->lookupCol1
);
342 wxMessageBox("ODBC error during GetNext()","ODBC Error...");
346 // Query the actual record set
347 if (selectStmt
&& wxStrlen(selectStmt
)) // Query by sql stmt passed in
349 if (!lookup2
->QueryBySqlStmt(selectStmt
))
351 wxMessageBox("ODBC error during QueryBySqlStmt()","ODBC Error...");
356 else // Query using where and order by clauses
358 lookup2
->SetOrderByClause(orderBy
);
359 lookup2
->SetWhereClause(where
);
360 if (!lookup2
->Query(FALSE
, distinctValues
))
362 wxMessageBox("ODBC error during Query()","ODBC Error...");
368 // Fill in the list box from the query result set
370 while (lookup2
->GetNext())
372 s
= lookup2
->lookupCol1
;
373 if (wxStrlen(dispCol2
)) // Append the optional column 2
375 s
.Append(' ', (maxColLen
+ LISTDB_NO_SPACES_BETWEEN_COLS
- wxStrlen(lookup2
->lookupCol1
)));
376 s
.Append(lookup2
->lookupCol2
);
378 pLookUpSelectList
->Append(s
);
381 // Highlight the first list item
382 pLookUpSelectList
->SetSelection(0);
384 // Make the OK activate by pressing Enter
385 if (pLookUpSelectList
->Number())
386 pLookUpOkBtn
->SetDefault();
389 pLookUpCancelBtn
->SetDefault();
390 pLookUpOkBtn
->Enable(FALSE
);
393 pLookUpOkBtn
->Enable(allowOk
);
395 // Display the dialog window
396 SetTitle(windowTitle
);
401 } // Generic lookup constructor 2
404 void ClookUpDlg::OnClose(wxCloseEvent
& event
)
406 widgetPtrsSet
= FALSE
;
407 GetParent()->Enable(TRUE
);
416 while (wxIsBusy()) wxEndBusyCursor();
421 } // ClookUpDlg::OnClose
424 void ClookUpDlg::OnButton( wxCommandEvent
&event
)
426 wxWindow
*win
= (wxWindow
*) event
.GetEventObject();
427 OnCommand( *win
, event
);
431 void ClookUpDlg::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
433 wxString widgetName
= win
.GetName();
438 if (widgetName
== pLookUpOkBtn
->GetName())
440 if (pLookUpSelectList
->GetSelection() != -1)
442 if (noDisplayCols
== 1)
443 wxStrcpy (ListDB_Selection
, pLookUpSelectList
->GetStringSelection());
444 else // 2 display columns
446 wxString s
= pLookUpSelectList
->GetStringSelection();
448 s
= s
.SubString(0, col1Len
-1);
450 wxStrcpy(ListDB_Selection
, s
);
452 s
= pLookUpSelectList
->GetStringSelection();
453 s
= s
.Mid(col1Len
+ LISTDB_NO_SPACES_BETWEEN_COLS
);
455 wxStrcpy(ListDB_Selection2
, s
);
460 wxStrcpy(ListDB_Selection
,"");
461 wxStrcpy(ListDB_Selection2
,"");
467 if (widgetName
== pLookUpCancelBtn
->GetName())
469 wxStrcpy (ListDB_Selection
,"");
470 wxStrcpy (ListDB_Selection2
,"");
475 }; // ClookUpDlg::OnCommand
477 // *********************************** listdb.cpp **********************************