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 char *GetExtendedDBErrorMsg2(char *ErrFile
, int ErrLine
)
102 if (ErrFile
|| ErrLine
)
107 tStr
.Printf("%d",ErrLine
);
112 msg
.Append ("\nODBC errors:\n");
115 /* Scan through each database connection displaying
116 * any ODBC errors that have occured. */
118 for (pDbList
= PtrBegDbList
; pDbList
; pDbList
= pDbList
->PtrNext
)
120 // Skip over any free connections
123 // Display errors for this connection
124 for (int i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
126 if (pDbList
->PtrDb
->errorList
[i
])
128 msg
.Append(pDbList
->PtrDb
->errorList
[i
]);
129 if (wxStrcmp(pDbList
->PtrDb
->errorList
[i
],"") != 0)
136 return (char*) (const char*) msg
;
137 } // GetExtendedDBErrorMsg
141 // Clookup constructor
142 Clookup::Clookup(char *tblName
, char *colName
) : wxDbTable(READONLY_DB
, tblName
, 1, NULL
, !wxDB_QUERY_ONLY
, DbConnectInf
.defaultDir
)
145 SetColDefs (0, colName
, DB_DATA_TYPE_VARCHAR
, lookupCol
, SQL_C_CHAR
, LOOKUP_COL_LEN
+1, FALSE
, FALSE
);
150 // Clookup2 constructor
151 Clookup2::Clookup2(char *tblName
, char *colName1
, char *colName2
, wxDb
*pDb
)
152 : wxDbTable(pDb
, tblName
, (1 + (wxStrlen(colName2
) > 0)), NULL
, !wxDB_QUERY_ONLY
, DbConnectInf
.defaultDir
)
156 SetColDefs (i
, colName1
, DB_DATA_TYPE_VARCHAR
, lookupCol1
, SQL_C_CHAR
, LOOKUP_COL_LEN
+1, FALSE
, FALSE
);
158 if (wxStrlen(colName2
) > 0)
159 SetColDefs (++i
, colName2
, DB_DATA_TYPE_VARCHAR
, lookupCol2
, SQL_C_CHAR
, LOOKUP_COL_LEN
+1, FALSE
, FALSE
);
164 BEGIN_EVENT_TABLE(ClookUpDlg
, wxDialog
)
165 // EVT_LISTBOX(LOOKUP_DIALOG_SELECT, ClookUpDlg::SelectCallback)
166 EVT_BUTTON(LOOKUP_DIALOG_OK
, ClookUpDlg::OnButton
)
167 EVT_BUTTON(LOOKUP_DIALOG_CANCEL
, ClookUpDlg::OnButton
)
168 EVT_CLOSE(ClookUpDlg::OnClose
)
171 // This is a generic lookup constructor that will work with any table and any column
172 ClookUpDlg::ClookUpDlg(wxWindow
*parent
, char *windowTitle
, char *tableName
, char *colName
,
173 char *where
, char *orderBy
) : wxDialog (parent
, LOOKUP_DIALOG
, "Select...", wxPoint(-1, -1), wxSize(400, 290))
177 wxStrcpy(ListDB_Selection
,"");
178 widgetPtrsSet
= FALSE
;
184 pLookUpSelectList
= new wxListBox(this, LOOKUP_DIALOG_SELECT
, wxPoint( 5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE
|wxLB_ALWAYS_SB
, wxDefaultValidator
, "LookUpSelectList");
185 pLookUpOkBtn
= new wxButton(this, LOOKUP_DIALOG_OK
, "&Ok", wxPoint(113, 222), wxSize( 70, 35), 0, wxDefaultValidator
, "LookUpOkBtn");
186 pLookUpCancelBtn
= new wxButton(this, LOOKUP_DIALOG_CANCEL
, "C&ancel", wxPoint(212, 222), wxSize( 70, 35), 0, wxDefaultValidator
, "LookUpCancelBtn");
188 widgetPtrsSet
= TRUE
;
190 // Query the lookup table and display the result set
191 if (!(lookup
= new Clookup(tableName
, colName
)))
193 wxMessageBox("Error allocating memory for 'Clookup'object.","Error...");
201 tStr
.Printf("Unable to open the table '%s'.",tableName
);
202 wxMessageBox(tStr
,"ODBC Error...");
207 lookup
->SetOrderByClause(orderBy
);
208 lookup
->SetWhereClause(where
);
209 if (!lookup
->Query())
211 wxMessageBox("ODBC error during Query()","ODBC Error...");
216 // Fill in the list box from the query result set
217 while (lookup
->GetNext())
218 pLookUpSelectList
->Append(lookup
->lookupCol
);
220 // Highlight the first list item
221 pLookUpSelectList
->SetSelection(0);
223 // Make the OK activate by pressing Enter
224 if (pLookUpSelectList
->Number())
225 pLookUpOkBtn
->SetDefault();
228 pLookUpCancelBtn
->SetDefault();
229 pLookUpOkBtn
->Enable(FALSE
);
232 // Display the dialog window
233 SetTitle(windowTitle
);
238 } // Generic lookup constructor
242 // This is a generic lookup constructor that will work with any table and any column.
243 // It extends the capabilites of the lookup dialog in the following ways:
245 // 1) 2 columns rather than one
246 // 2) The ability to select DISTINCT column values
248 // Only set distinctValues equal to true if necessary. In many cases, the constraints
249 // of the index(es) will enforce this uniqueness. Selecting DISTINCT does require
250 // overhead by the database to ensure that all values returned are distinct. Therefore,
251 // use this ONLY when you need it.
253 // For complicated queries, you can pass in the sql select statement. This would be
254 // necessary if joins are involved since by default both columns must come from the
257 // If you do query by sql statement, you must pass in the maximum lenght of column1,
258 // since it cannot be derived when you query using your own sql statement.
260 // The optional database connection can be used if you'd like the lookup class
261 // to use a database pointer other than the global READONLY_DB. This is necessary if
262 // records are being saved, but not committed to the db, yet should be included
263 // in the lookup window.
265 ClookUpDlg::ClookUpDlg(wxWindow
*parent
, char *windowTitle
, char *tableName
,
266 char *dispCol1
, char *dispCol2
, char *where
, char *orderBy
, bool distinctValues
,
267 char *selectStmt
, int maxLenCol1
, wxDb
*pDb
, bool allowOk
) : wxDialog (parent
, LOOKUP_DIALOG
, "Select...", wxPoint(-1, -1), wxSize(400, 290))
271 wxStrcpy(ListDB_Selection
,"");
272 wxStrcpy(ListDB_Selection2
,"");
273 widgetPtrsSet
= FALSE
;
276 noDisplayCols
= (wxStrlen(dispCol2
) ? 2 : 1);
279 wxFont
fixedFont(12,wxMODERN
,wxNORMAL
,wxNORMAL
);
281 // this is done with fixed font so that the second column (if any) will be left
282 // justified in the second column
283 pLookUpSelectList
= new wxListBox(this, LOOKUP_DIALOG_SELECT
, wxPoint(5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE
|wxLB_ALWAYS_SB
, wxDefaultValidator
, "LookUpSelectList");
285 pLookUpSelectList
->SetFont(fixedFont
);
287 pLookUpOkBtn
= new wxButton(this, LOOKUP_DIALOG_OK
, "&Ok", wxPoint(113, 222), wxSize(70, 35), 0, wxDefaultValidator
, "LookUpOkBtn");
288 pLookUpCancelBtn
= new wxButton(this, LOOKUP_DIALOG_CANCEL
, "C&ancel", wxPoint(212, 222), wxSize(70, 35), 0, wxDefaultValidator
, "LookUpCancelBtn");
290 widgetPtrsSet
= TRUE
;
292 // Query the lookup table and display the result set
293 if (!(lookup2
= new Clookup2(tableName
, dispCol1
, dispCol2
, pDb
)))
295 wxMessageBox("Error allocating memory for 'Clookup2'object.","Error...");
300 if (!lookup2
->Open())
303 tStr
.Printf("Unable to open the table '%s'.",tableName
);
304 tStr
+= GetExtendedDBErrorMsg2(__FILE__
,__LINE__
);
305 wxMessageBox(tStr
,"ODBC Error...");
310 // If displaying 2 columns, determine the maximum length of column1
313 maxColLen
= col1Len
= maxLenCol1
; // user passed in max col length for column 1
316 maxColLen
= LOOKUP_COL_LEN
;
317 if (wxStrlen(dispCol2
))
319 wxString q
= "SELECT MAX({fn LENGTH(";
329 if (!lookup2
->QueryBySqlStmt((char*) (const char*) q
))
331 wxMessageBox("ODBC error during QueryBySqlStmt()","ODBC Error...");
335 if (lookup2
->GetNext())
336 maxColLen
= col1Len
= atoi(lookup2
->lookupCol1
);
338 wxMessageBox("ODBC error during GetNext()","ODBC Error...");
342 // Query the actual record set
343 if (selectStmt
&& wxStrlen(selectStmt
)) // Query by sql stmt passed in
345 if (!lookup2
->QueryBySqlStmt(selectStmt
))
347 wxMessageBox("ODBC error during QueryBySqlStmt()","ODBC Error...");
352 else // Query using where and order by clauses
354 lookup2
->SetOrderByClause(orderBy
);
355 lookup2
->SetWhereClause(where
);
356 if (!lookup2
->Query(FALSE
, distinctValues
))
358 wxMessageBox("ODBC error during Query()","ODBC Error...");
364 // Fill in the list box from the query result set
366 while (lookup2
->GetNext())
368 s
= lookup2
->lookupCol1
;
369 if (wxStrlen(dispCol2
)) // Append the optional column 2
371 s
.Append(' ', (maxColLen
+ LISTDB_NO_SPACES_BETWEEN_COLS
- wxStrlen(lookup2
->lookupCol1
)));
372 s
.Append(lookup2
->lookupCol2
);
374 pLookUpSelectList
->Append(s
);
377 // Highlight the first list item
378 pLookUpSelectList
->SetSelection(0);
380 // Make the OK activate by pressing Enter
381 if (pLookUpSelectList
->Number())
382 pLookUpOkBtn
->SetDefault();
385 pLookUpCancelBtn
->SetDefault();
386 pLookUpOkBtn
->Enable(FALSE
);
389 pLookUpOkBtn
->Enable(allowOk
);
391 // Display the dialog window
392 SetTitle(windowTitle
);
397 } // Generic lookup constructor 2
400 void ClookUpDlg::OnClose(wxCloseEvent
& event
)
402 widgetPtrsSet
= FALSE
;
403 GetParent()->Enable(TRUE
);
410 while (wxIsBusy()) wxEndBusyCursor();
415 } // ClookUpDlg::OnClose
418 void ClookUpDlg::OnButton( wxCommandEvent
&event
)
420 wxWindow
*win
= (wxWindow
*) event
.GetEventObject();
421 OnCommand( *win
, event
);
425 void ClookUpDlg::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
427 wxString widgetName
= win
.GetName();
432 if (widgetName
== pLookUpOkBtn
->GetName())
434 if (pLookUpSelectList
->GetSelection() != -1)
436 if (noDisplayCols
== 1)
437 wxStrcpy (ListDB_Selection
, pLookUpSelectList
->GetStringSelection());
438 else // 2 display columns
440 wxString s
= pLookUpSelectList
->GetStringSelection();
442 s
= s
.SubString(0, col1Len
-1);
444 wxStrcpy(ListDB_Selection
, s
);
446 s
= pLookUpSelectList
->GetStringSelection();
447 s
= s
.Mid(col1Len
+ LISTDB_NO_SPACES_BETWEEN_COLS
);
449 wxStrcpy(ListDB_Selection2
, s
);
454 wxStrcpy(ListDB_Selection
,"");
455 wxStrcpy(ListDB_Selection2
,"");
461 if (widgetName
== pLookUpCancelBtn
->GetName())
463 wxStrcpy (ListDB_Selection
,"");
464 wxStrcpy (ListDB_Selection2
,"");
469 }; // ClookUpDlg::OnCommand
471 // *********************************** listdb.cpp **********************************