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 wxTable.
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 DbList
* WXDLLEXPORT PtrBegDbList
; /* from db.cpp, used in getting back error results from db connections */
63 // Global structure for holding ODBC connection information
64 extern DbStuff 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
);
108 msg
+= tStr
.GetData();
112 msg
.Append ("\nODBC errors:\n");
115 /* Scan through each database connection displaying
116 * any ODBC errors that have occured. */
117 for (DbList
*pDbList
= PtrBegDbList
; pDbList
; pDbList
= pDbList
->PtrNext
)
119 // Skip over any free connections
122 // Display errors for this connection
123 for (int i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
125 if (pDbList
->PtrDb
->errorList
[i
])
127 msg
.Append(pDbList
->PtrDb
->errorList
[i
]);
128 if (strcmp(pDbList
->PtrDb
->errorList
[i
],"") != 0)
135 return (char*) (const char*) msg
;
136 } // GetExtendedDBErrorMsg
140 // Clookup constructor
141 Clookup::Clookup(char *tblName
, char *colName
) : wxTable(READONLY_DB
, tblName
, 1, NULL
, !QUERY_ONLY
, DbConnectInf
.defaultDir
)
144 SetColDefs (0, colName
, DB_DATA_TYPE_VARCHAR
, lookupCol
, SQL_C_CHAR
, LOOKUP_COL_LEN
+1, FALSE
, FALSE
);
149 // Clookup2 constructor
150 Clookup2::Clookup2(char *tblName
, char *colName1
, char *colName2
, wxDB
*pDb
)
151 : wxTable(pDb
, tblName
, (1 + (strlen(colName2
) > 0)), NULL
, !QUERY_ONLY
, DbConnectInf
.defaultDir
)
155 SetColDefs (i
, colName1
, DB_DATA_TYPE_VARCHAR
, lookupCol1
, SQL_C_CHAR
, LOOKUP_COL_LEN
+1, FALSE
, FALSE
);
157 if (strlen(colName2
) > 0)
158 SetColDefs (++i
, colName2
, DB_DATA_TYPE_VARCHAR
, lookupCol2
, SQL_C_CHAR
, LOOKUP_COL_LEN
+1, FALSE
, FALSE
);
163 BEGIN_EVENT_TABLE(ClookUpDlg
, wxDialog
)
164 // EVT_LISTBOX(LOOKUP_DIALOG_SELECT, ClookUpDlg::SelectCallback)
165 EVT_BUTTON(LOOKUP_DIALOG_OK
, ClookUpDlg::OnButton
)
166 EVT_BUTTON(LOOKUP_DIALOG_CANCEL
, ClookUpDlg::OnButton
)
167 EVT_CLOSE(ClookUpDlg::OnClose
)
170 // This is a generic lookup constructor that will work with any table and any column
171 ClookUpDlg::ClookUpDlg(wxWindow
*parent
, char *windowTitle
, char *tableName
, char *colName
,
172 char *where
, char *orderBy
) : wxDialog (parent
, LOOKUP_DIALOG
, "Select...", wxPoint(-1, -1), wxSize(400, 290))
176 strcpy(ListDB_Selection
,"");
177 widgetPtrsSet
= FALSE
;
183 pLookUpSelectList
= new wxListBox(this, LOOKUP_DIALOG_SELECT
, wxPoint(5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE
|wxLB_ALWAYS_SB
, wxDefaultValidator
, "LookUpSelectList");
184 pLookUpOkBtn
= new wxButton(this, LOOKUP_DIALOG_OK
, "&Ok", wxPoint(113, 222), wxSize(70, 35), 0, wxDefaultValidator
, "LookUpOkBtn");
185 pLookUpCancelBtn
= new wxButton(this, LOOKUP_DIALOG_CANCEL
, "C&ancel", wxPoint(212, 222), wxSize(70, 35), 0, wxDefaultValidator
, "LookUpCancelBtn");
187 widgetPtrsSet
= TRUE
;
189 // Query the lookup table and display the result set
190 if (!(lookup
= new Clookup(tableName
, colName
)))
192 wxMessageBox("Error allocating memory for 'Clookup'object.","Error...");
200 tStr
.Printf("Unable to open the table '%s'.",tableName
);
201 wxMessageBox(tStr
,"ODBC Error...");
206 lookup
->orderBy
= orderBy
;
207 lookup
->where
= where
;
208 if (!lookup
->Query())
210 wxMessageBox("ODBC error during Query()","ODBC Error...");
215 // Fill in the list box from the query result set
216 while (lookup
->GetNext())
217 pLookUpSelectList
->Append(lookup
->lookupCol
);
219 // Highlight the first list item
220 pLookUpSelectList
->SetSelection(0);
222 // Make the OK activate by pressing Enter
223 if (pLookUpSelectList
->Number())
224 pLookUpOkBtn
->SetDefault();
227 pLookUpCancelBtn
->SetDefault();
228 pLookUpOkBtn
->Enable(FALSE
);
231 // Display the dialog window
232 SetTitle(windowTitle
);
237 } // Generic lookup constructor
241 // This is a generic lookup constructor that will work with any table and any column.
242 // It extends the capabilites of the lookup dialog in the following ways:
244 // 1) 2 columns rather than one
245 // 2) The ability to select DISTINCT column values
247 // Only set distinctValues equal to true if necessary. In many cases, the constraints
248 // of the index(es) will enforce this uniqueness. Selecting DISTINCT does require
249 // overhead by the database to ensure that all values returned are distinct. Therefore,
250 // use this ONLY when you need it.
252 // For complicated queries, you can pass in the sql select statement. This would be
253 // necessary if joins are involved since by default both columns must come from the
256 // If you do query by sql statement, you must pass in the maximum lenght of column1,
257 // since it cannot be derived when you query using your own sql statement.
259 // The optional database connection can be used if you'd like the lookup class
260 // to use a database pointer other than the global READONLY_DB. This is necessary if
261 // records are being saved, but not committed to the db, yet should be included
262 // in the lookup window.
264 ClookUpDlg::ClookUpDlg(wxWindow
*parent
, char *windowTitle
, char *tableName
,
265 char *dispCol1
, char *dispCol2
, char *where
, char *orderBy
, bool distinctValues
,
266 char *selectStmt
, int maxLenCol1
, wxDB
*pDb
, bool allowOk
) : wxDialog (parent
, LOOKUP_DIALOG
, "Select...", wxPoint(-1, -1), wxSize(400, 290))
270 strcpy(ListDB_Selection
,"");
271 strcpy(ListDB_Selection2
,"");
272 widgetPtrsSet
= FALSE
;
275 noDisplayCols
= (strlen(dispCol2
) ? 2 : 1);
278 wxFont
fixedFont(12,wxMODERN
,wxNORMAL
,wxNORMAL
);
280 // this is done with fixed font so that the second column (if any) will be left
281 // justified in the second column
282 pLookUpSelectList
= new wxListBox(this, LOOKUP_DIALOG_SELECT
, wxPoint(5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE
|wxLB_ALWAYS_SB
, wxDefaultValidator
, "LookUpSelectList");
284 pLookUpSelectList
->SetFont(fixedFont
);
286 pLookUpOkBtn
= new wxButton(this, LOOKUP_DIALOG_OK
, "&Ok", wxPoint(113, 222), wxSize(70, 35), 0, wxDefaultValidator
, "LookUpOkBtn");
287 pLookUpCancelBtn
= new wxButton(this, LOOKUP_DIALOG_CANCEL
, "C&ancel", wxPoint(212, 222), wxSize(70, 35), 0, wxDefaultValidator
, "LookUpCancelBtn");
289 widgetPtrsSet
= TRUE
;
291 // Query the lookup table and display the result set
292 if (!(lookup2
= new Clookup2(tableName
, dispCol1
, dispCol2
, pDb
)))
294 wxMessageBox("Error allocating memory for 'Clookup2'object.","Error...");
299 if (!lookup2
->Open())
302 tStr
.Printf("Unable to open the table '%s'.",tableName
);
303 tStr
+= GetExtendedDBErrorMsg2(__FILE__
,__LINE__
);
304 wxMessageBox(tStr
,"ODBC Error...");
309 // If displaying 2 columns, determine the maximum length of column1
312 maxColLen
= col1Len
= maxLenCol1
; // user passed in max col length for column 1
315 maxColLen
= LOOKUP_COL_LEN
;
316 if (strlen(dispCol2
))
318 wxString q
= "SELECT MAX({fn LENGTH(";
328 if (!lookup2
->QueryBySqlStmt((char*) (const char*) q
))
330 wxMessageBox("ODBC error during QueryBySqlStmt()","ODBC Error...");
334 if (lookup2
->GetNext())
335 maxColLen
= col1Len
= atoi(lookup2
->lookupCol1
);
337 wxMessageBox("ODBC error during GetNext()","ODBC Error...");
341 // Query the actual record set
342 if (selectStmt
&& strlen(selectStmt
)) // Query by sql stmt passed in
344 if (!lookup2
->QueryBySqlStmt(selectStmt
))
346 wxMessageBox("ODBC error during QueryBySqlStmt()","ODBC Error...");
351 else // Query using where and order by clauses
353 lookup2
->orderBy
= orderBy
;
354 lookup2
->where
= where
;
355 if (!lookup2
->Query(FALSE
, distinctValues
))
357 wxMessageBox("ODBC error during Query()","ODBC Error...");
363 // Fill in the list box from the query result set
365 while (lookup2
->GetNext())
367 s
= lookup2
->lookupCol1
;
368 if (strlen(dispCol2
)) // Append the optional column 2
370 s
.Append(' ', (maxColLen
+ LISTDB_NO_SPACES_BETWEEN_COLS
- strlen(lookup2
->lookupCol1
)));
371 s
.Append(lookup2
->lookupCol2
);
373 pLookUpSelectList
->Append(s
);
376 // Highlight the first list item
377 pLookUpSelectList
->SetSelection(0);
379 // Make the OK activate by pressing Enter
380 if (pLookUpSelectList
->Number())
381 pLookUpOkBtn
->SetDefault();
384 pLookUpCancelBtn
->SetDefault();
385 pLookUpOkBtn
->Enable(FALSE
);
388 pLookUpOkBtn
->Enable(allowOk
);
390 // Display the dialog window
391 SetTitle(windowTitle
);
396 } // Generic lookup constructor 2
399 void ClookUpDlg::OnClose(wxCloseEvent
& event
)
401 widgetPtrsSet
= FALSE
;
402 GetParent()->Enable(TRUE
);
414 } // ClookUpDlg::OnClose
417 void ClookUpDlg::OnButton( wxCommandEvent
&event
)
419 wxWindow
*win
= (wxWindow
*) event
.GetEventObject();
420 OnCommand( *win
, event
);
424 void ClookUpDlg::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
426 wxString widgetName
= win
.GetName();
431 if (widgetName
== pLookUpOkBtn
->GetName())
433 if (pLookUpSelectList
->GetSelection() != -1)
435 if (noDisplayCols
== 1)
436 strcpy (ListDB_Selection
, pLookUpSelectList
->GetStringSelection());
437 else // 2 display columns
439 wxString s
= pLookUpSelectList
->GetStringSelection();
441 s
= s
.SubString(0, col1Len
-1);
443 strcpy(ListDB_Selection
, s
);
445 s
= pLookUpSelectList
->GetStringSelection();
446 s
= s
.Mid(col1Len
+ LISTDB_NO_SPACES_BETWEEN_COLS
);
448 strcpy(ListDB_Selection2
, s
);
453 strcpy(ListDB_Selection
,"");
454 strcpy(ListDB_Selection2
,"");
460 if (widgetName
== pLookUpCancelBtn
->GetName())
462 strcpy (ListDB_Selection
,"");
463 strcpy (ListDB_Selection2
,"");
468 }; // ClookUpDlg::OnCommand
470 // *********************************** listdb.cpp **********************************