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>
61 // Global structure for holding ODBC connection information
62 extern DbStuff DbConnectInf
;
64 // Global database connection
65 extern wxDB
*READONLY_DB
;
68 // Used for passing the selected listbox selection back to the calling
69 // routine. This variable must be declared as 'extern' in the calling
71 char ListDB_Selection
[LOOKUP_COL_LEN
+1];
73 // If the listbox contains two columns of data, the second column is
74 // returned in this variable.
75 char ListDB_Selection2
[LOOKUP_COL_LEN
+1];
78 const int LISTDB_NO_SPACES_BETWEEN_COLS
= 3;
81 // Clookup constructor
82 Clookup::Clookup(char *tblName
, char *colName
) : wxTable(READONLY_DB
, tblName
, 1)
85 SetColDefs (0, colName
, DB_DATA_TYPE_VARCHAR
, lookupCol
, SQL_C_CHAR
, LOOKUP_COL_LEN
+1, FALSE
, FALSE
);
90 // Clookup2 constructor
91 Clookup2::Clookup2(char *tblName
, char *colName1
, char *colName2
, wxDB
*pDb
)
92 : wxTable(pDb
, tblName
, (1 + (strlen(colName2
) > 0)))
96 SetColDefs (i
, colName1
, DB_DATA_TYPE_VARCHAR
, lookupCol1
, SQL_C_CHAR
, LOOKUP_COL_LEN
+1, FALSE
, FALSE
);
98 if (strlen(colName2
) > 0)
99 SetColDefs (++i
, colName2
, DB_DATA_TYPE_VARCHAR
, lookupCol2
, SQL_C_CHAR
, LOOKUP_COL_LEN
+1, FALSE
, FALSE
);
104 // This is a generic lookup constructor that will work with any table and any column
105 ClookUpDlg::ClookUpDlg(wxWindow
*parent
, char *windowTitle
, char *tableName
, char *colName
,
106 char *where
, char *orderBy
) : wxDialog (parent
, LOOKUP_DIALOG
, "Select...", wxPoint(-1, -1), wxSize(400, 290))
110 strcpy(ListDB_Selection
,"");
111 widgetPtrsSet
= FALSE
;
117 pLookUpSelectList
= new wxListBox(this, LOOKUP_DIALOG_SELECT
, wxPoint(5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE
|wxLB_ALWAYS_SB
, wxDefaultValidator
, "LookUpSelectList");
118 pLookUpOkBtn
= new wxButton(this, LOOKUP_DIALOG_OK
, "&Ok", wxPoint(113, 222), wxSize(70, 35), 0, wxDefaultValidator
, "LookUpOkBtn");
119 pLookUpCancelBtn
= new wxButton(this, LOOKUP_DIALOG_CANCEL
, "C&ancel", wxPoint(212, 222), wxSize(70, 35), 0, wxDefaultValidator
, "LookUpCancelBtn");
121 widgetPtrsSet
= TRUE
;
123 // Query the lookup table and display the result set
124 if (!(lookup
= new Clookup(tableName
, colName
)))
126 wxMessageBox("Error allocating memory for 'Clookup'object.","Error...");
134 tStr
.Printf("Unable to open the table '%s'.",tableName
);
135 wxMessageBox(tStr
,"ODBC Error...");
140 lookup
->orderBy
= orderBy
;
141 lookup
->where
= where
;
142 if (!lookup
->Query())
144 wxMessageBox("ODBC error during Query()","ODBC Error...");
149 // Fill in the list box from the query result set
150 while (lookup
->GetNext())
151 pLookUpSelectList
->Append(lookup
->lookupCol
);
153 // Highlight the first list item
154 pLookUpSelectList
->SetSelection(0);
156 // Make the OK activate by pressing Enter
157 if (pLookUpSelectList
->Number())
158 pLookUpOkBtn
->SetDefault();
161 pLookUpCancelBtn
->SetDefault();
162 pLookUpOkBtn
->Enable(FALSE
);
165 // Display the dialog window
166 SetTitle(windowTitle
);
171 } // Generic lookup constructor
175 // This is a generic lookup constructor that will work with any table and any column.
176 // It extends the capabilites of the lookup dialog in the following ways:
178 // 1) 2 columns rather than one
179 // 2) The ability to select DISTINCT column values
181 // Only set distinctValues equal to true if necessary. In many cases, the constraints
182 // of the index(es) will enforce this uniqueness. Selecting DISTINCT does require
183 // overhead by the database to ensure that all values returned are distinct. Therefore,
184 // use this ONLY when you need it.
186 // For complicated queries, you can pass in the sql select statement. This would be
187 // necessary if joins are involved since by default both columns must come from the
190 // If you do query by sql statement, you must pass in the maximum lenght of column1,
191 // since it cannot be derived when you query using your own sql statement.
193 // The optional database connection can be used if you'd like the lookup class
194 // to use a database pointer other than the global READONLY_DB. This is necessary if
195 // records are being saved, but not committed to the db, yet should be included
196 // in the lookup window.
198 ClookUpDlg::ClookUpDlg(wxWindow
*parent
, char *windowTitle
, char *tableName
,
199 char *dispCol1
, char *dispCol2
, char *where
, char *orderBy
, bool distinctValues
,
200 char *selectStmt
, int maxLenCol1
, wxDB
*pDb
, bool allowOk
) : wxDialog (parent
, LOOKUP_DIALOG
, "Select...", wxPoint(-1, -1), wxSize(400, 290))
204 strcpy(ListDB_Selection
,"");
205 strcpy(ListDB_Selection2
,"");
206 widgetPtrsSet
= FALSE
;
209 noDisplayCols
= (strlen(dispCol2
) ? 2 : 1);
212 wxFont
fixedFont(12,wxMODERN
,wxNORMAL
,wxNORMAL
);
214 // this is done with fixed font so that the second column (if any) will be left
215 // justified in the second column
216 pLookUpSelectList
= new wxListBox(this, LOOKUP_DIALOG_SELECT
, wxPoint(5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE
|wxLB_ALWAYS_SB
, wxDefaultValidator
, "LookUpSelectList");
218 pLookUpSelectList
->SetFont(fixedFont
);
220 pLookUpOkBtn
= new wxButton(this, LOOKUP_DIALOG_OK
, "&Ok", wxPoint(113, 222), wxSize(70, 35), 0, wxDefaultValidator
, "LookUpOkBtn");
221 pLookUpCancelBtn
= new wxButton(this, LOOKUP_DIALOG_CANCEL
, "C&ancel", wxPoint(212, 222), wxSize(70, 35), 0, wxDefaultValidator
, "LookUpCancelBtn");
223 widgetPtrsSet
= TRUE
;
225 // Query the lookup table and display the result set
226 if (!(lookup2
= new Clookup2(tableName
, dispCol1
, dispCol2
, pDb
)))
228 wxMessageBox("Error allocating memory for 'Clookup2'object.","Error...");
233 if (!lookup2
->Open())
236 tStr
.Printf("Unable to open the table '%s'.",tableName
);
237 wxMessageBox(tStr
,"ODBC Error...");
242 // If displaying 2 columns, determine the maximum length of column1
245 maxColLen
= col1Len
= maxLenCol1
; // user passed in max col length for column 1
248 maxColLen
= LOOKUP_COL_LEN
;
249 if (strlen(dispCol2
))
251 wxString q
= "SELECT MAX({fn LENGTH(";
261 if (!lookup2
->QueryBySqlStmt((char*) (const char*) q
))
263 wxMessageBox("ODBC error during QueryBySqlStmt()","ODBC Error...");
267 if (lookup2
->GetNext())
268 maxColLen
= col1Len
= atoi(lookup2
->lookupCol1
);
270 wxMessageBox("ODBC error during GetNext()","ODBC Error...");
274 // Query the actual record set
275 if (selectStmt
&& strlen(selectStmt
)) // Query by sql stmt passed in
277 if (!lookup2
->QueryBySqlStmt(selectStmt
))
279 wxMessageBox("ODBC error during QueryBySqlStmt()","ODBC Error...");
284 else // Query using where and order by clauses
286 lookup2
->orderBy
= orderBy
;
287 lookup2
->where
= where
;
288 if (!lookup2
->Query(FALSE
, distinctValues
))
290 wxMessageBox("ODBC error during Query()","ODBC Error...");
296 // Fill in the list box from the query result set
298 while (lookup2
->GetNext())
300 s
= lookup2
->lookupCol1
;
301 if (strlen(dispCol2
)) // Append the optional column 2
303 s
.Append(' ', (maxColLen
+ LISTDB_NO_SPACES_BETWEEN_COLS
- strlen(lookup2
->lookupCol1
)));
304 s
.Append(lookup2
->lookupCol2
);
306 pLookUpSelectList
->Append(s
);
309 // Highlight the first list item
310 pLookUpSelectList
->SetSelection(0);
312 // Make the OK activate by pressing Enter
313 if (pLookUpSelectList
->Number())
314 pLookUpOkBtn
->SetDefault();
317 pLookUpCancelBtn
->SetDefault();
318 pLookUpOkBtn
->Enable(FALSE
);
321 pLookUpOkBtn
->Enable(allowOk
);
323 // Display the dialog window
324 SetTitle(windowTitle
);
329 } // Generic lookup constructor 2
332 bool ClookUpDlg::OnClose(void)
334 widgetPtrsSet
= FALSE
;
335 GetParent()->Enable(TRUE
);
345 } // ClookUpDlg::OnClose
348 void ClookUpDlg::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
350 wxString widgetName
= win
.GetName();
355 if (widgetName
== pLookUpOkBtn
->GetName())
357 if (pLookUpSelectList
->GetSelection() != -1)
359 if (noDisplayCols
== 1)
360 strcpy (ListDB_Selection
, pLookUpSelectList
->GetStringSelection());
361 else // 2 display columns
363 wxString s
= pLookUpSelectList
->GetStringSelection();
365 s
= s
.SubString(0, col1Len
-1);
367 strcpy(ListDB_Selection
, s
);
369 s
= pLookUpSelectList
->GetStringSelection();
370 s
= s
.Mid(col1Len
+ LISTDB_NO_SPACES_BETWEEN_COLS
);
372 strcpy(ListDB_Selection2
, s
);
377 strcpy(ListDB_Selection
,"");
378 strcpy(ListDB_Selection2
,"");
384 if (widgetName
== pLookUpCancelBtn
->GetName())
386 strcpy (ListDB_Selection
,"");
387 strcpy (ListDB_Selection2
,"");
392 }; // ClookUpDlg::OnCommand
394 // *********************************** listdb.cpp **********************************