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 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
) : wxDialogBox (parent
, "Select...", 1, -1, -1, 400, 290)
110 strcpy(ListDB_Selection
,"");
111 widgetPtrsSet
= FALSE
;
118 SetLabelPosition(wxVERTICAL
);
120 wxFont
*ButtonFont
= new wxFont(12,wxSWISS
,wxNORMAL
,wxBOLD
);
121 wxFont
*TextFont
= new wxFont(12,wxSWISS
,wxNORMAL
,wxNORMAL
);
123 SetButtonFont(ButtonFont
);
124 SetLabelFont(TextFont
);
125 SetLabelPosition(wxVERTICAL
);
127 pLookUpSelectList
= new wxListBox(this, NULL
, "", wxSINGLE
|wxALWAYS_SB
, 5, 15, 384, 195, 0, 0, 0, "LookUpSelectList");
128 pLookUpOkBtn
= new wxButton(this, NULL
, "&Ok", 113, 222, 70, 35, 0, "LookUpOkBtn");
129 pLookUpCancelBtn
= new wxButton(this, NULL
, "C&ancel", 212, 222, 70, 35, 0, "LookUpCancelBtn");
131 widgetPtrsSet
= TRUE
;
133 // Query the lookup table and display the result set
134 if (!(lookup
= new Clookup(tableName
, colName
)))
136 wxMessageBox("Error allocating memory for 'Clookup'object.","Error...");
144 tStr
.sprintf("Unable to open the table '%s'.",tableName
);
145 wxMessageBox(tStr
.GetData(),"ODBC Error...");
150 lookup
->orderBy
= orderBy
;
151 lookup
->where
= where
;
152 if (!lookup
->Query())
154 wxMessageBox("ODBC error during Query()","ODBC Error...");
159 // Fill in the list box from the query result set
160 while (lookup
->GetNext())
161 pLookUpSelectList
->Append(lookup
->lookupCol
);
163 // Highlight the first list item
164 pLookUpSelectList
->SetSelection(0);
166 // Make the OK activate by pressing Enter
167 if (pLookUpSelectList
->Number())
168 pLookUpOkBtn
->SetDefault();
171 pLookUpCancelBtn
->SetDefault();
172 pLookUpOkBtn
->Enable(FALSE
);
175 // Display the dialog window
176 SetTitle(windowTitle
);
181 } // Generic lookup constructor
185 // This is a generic lookup constructor that will work with any table and any column.
186 // It extends the capabilites of the lookup dialog in the following ways:
188 // 1) 2 columns rather than one
189 // 2) The ability to select DISTINCT column values
191 // Only set distinctValues equal to true if necessary. In many cases, the constraints
192 // of the index(es) will enforce this uniqueness. Selecting DISTINCT does require
193 // overhead by the database to ensure that all values returned are distinct. Therefore,
194 // use this ONLY when you need it.
196 // For complicated queries, you can pass in the sql select statement. This would be
197 // necessary if joins are involved since by default both columns must come from the
200 // If you do query by sql statement, you must pass in the maximum lenght of column1,
201 // since it cannot be derived when you query using your own sql statement.
203 // The optional database connection can be used if you'd like the lookup class
204 // to use a database pointer other than the global READONLY_DB. This is necessary if
205 // records are being saved, but not committed to the db, yet should be included
206 // in the lookup window.
208 ClookUpDlg::ClookUpDlg(wxWindow
*parent
, char *windowTitle
, char *tableName
,
209 char *dispCol1
, char *dispCol2
, char *where
, char *orderBy
, bool distinctValues
,
210 char *selectStmt
, int maxLenCol1
, wxDB
*pDb
, bool allowOk
) : wxDialogBox (parent
, "Select...", 1, -1, -1, 400, 290)
214 strcpy(ListDB_Selection
,"");
215 strcpy(ListDB_Selection2
,"");
216 widgetPtrsSet
= FALSE
;
219 noDisplayCols
= (strlen(dispCol2
) ? 2 : 1);
223 SetLabelPosition(wxVERTICAL
);
225 wxFont
*ButtonFont
= new wxFont(12,wxSWISS
,wxNORMAL
,wxBOLD
);
226 wxFont
*TextFont
= new wxFont(12,wxSWISS
,wxNORMAL
,wxNORMAL
);
227 wxFont
*FixedFont
= new wxFont(12,wxMODERN
,wxNORMAL
,wxNORMAL
);
229 SetButtonFont(ButtonFont
);
230 SetLabelFont(TextFont
);
231 SetLabelPosition(wxVERTICAL
);
233 // this is done with fixed font so that the second column (if any) will be left
234 // justified in the second column
235 SetButtonFont(FixedFont
);
236 pLookUpSelectList
= new wxListBox(this, NULL
, "", wxSINGLE
|wxALWAYS_SB
, 5, 15, 384, 195, 0, 0, 0, "LookUpSelectList");
237 SetButtonFont(ButtonFont
);
238 pLookUpOkBtn
= new wxButton(this, NULL
, "&Ok", 113, 222, 70, 35, 0, "LookUpOkBtn");
239 pLookUpCancelBtn
= new wxButton(this, NULL
, "C&ancel", 212, 222, 70, 35, 0, "LookUpCancelBtn");
241 widgetPtrsSet
= TRUE
;
243 // Query the lookup table and display the result set
244 if (!(lookup2
= new Clookup2(tableName
, dispCol1
, dispCol2
, pDb
)))
246 wxMessageBox("Error allocating memory for 'Clookup2'object.","Error...");
251 if (!lookup2
->Open())
254 tStr
.sprintf("Unable to open the table '%s'.",tableName
);
255 wxMessageBox(tStr
.GetData(),"ODBC Error...");
260 // If displaying 2 columns, determine the maximum length of column1
263 maxColLen
= col1Len
= maxLenCol1
; // user passed in max col length for column 1
266 maxColLen
= LOOKUP_COL_LEN
;
267 if (strlen(dispCol2
))
269 wxString q
= "SELECT MAX({fn LENGTH(";
279 if (!lookup2
->QueryBySqlStmt(q
.GetData()))
281 wxMessageBox("ODBC error during QueryBySqlStmt()","ODBC Error...");
285 if (lookup2
->GetNext())
286 maxColLen
= col1Len
= atoi(lookup2
->lookupCol1
);
288 wxMessageBox("ODBC error during GetNext()","ODBC Error...");
292 // Query the actual record set
293 if (selectStmt
&& strlen(selectStmt
)) // Query by sql stmt passed in
295 if (!lookup2
->QueryBySqlStmt(selectStmt
))
297 wxMessageBox("ODBC error during QueryBySqlStmt()","ODBC Error...");
302 else // Query using where and order by clauses
304 lookup2
->orderBy
= orderBy
;
305 lookup2
->where
= where
;
306 if (!lookup2
->Query(FALSE
, distinctValues
))
308 wxMessageBox("ODBC error during Query()","ODBC Error...");
314 // Fill in the list box from the query result set
316 while (lookup2
->GetNext())
318 s
= lookup2
->lookupCol1
;
319 if (strlen(dispCol2
)) // Append the optional column 2
321 s
.Append(' ', (maxColLen
+ LISTDB_NO_SPACES_BETWEEN_COLS
- strlen(lookup2
->lookupCol1
)));
322 s
.Append(lookup2
->lookupCol2
);
324 pLookUpSelectList
->Append(s
.GetData());
327 // Highlight the first list item
328 pLookUpSelectList
->SetSelection(0);
330 // Make the OK activate by pressing Enter
331 if (pLookUpSelectList
->Number())
332 pLookUpOkBtn
->SetDefault();
335 pLookUpCancelBtn
->SetDefault();
336 pLookUpOkBtn
->Enable(FALSE
);
339 pLookUpOkBtn
->Enable(allowOk
);
341 // Display the dialog window
342 SetTitle(windowTitle
);
347 } // Generic lookup constructor 2
350 bool ClookUpDlg::OnClose(void)
352 widgetPtrsSet
= FALSE
;
353 GetParent()->Enable(TRUE
);
363 } // ClookUpDlg::OnClose
366 void ClookUpDlg::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
368 wxString widgetName
= win
.GetName();
373 if (widgetName
== pLookUpOkBtn
->GetName())
375 if (pLookUpSelectList
->GetSelection() != -1)
377 if (noDisplayCols
== 1)
378 strcpy (ListDB_Selection
, pLookUpSelectList
->GetStringSelection());
379 else // 2 display columns
381 wxString s
= pLookUpSelectList
->GetStringSelection();
383 s
= s
.SubString(0, col1Len
-1);
385 strcpy(ListDB_Selection
, s
.GetData());
387 s
= pLookUpSelectList
->GetStringSelection();
388 s
= s
.From(col1Len
+ LISTDB_NO_SPACES_BETWEEN_COLS
);
390 strcpy(ListDB_Selection2
, s
.GetData());
395 strcpy(ListDB_Selection
,"");
396 strcpy(ListDB_Selection2
,"");
402 if (widgetName
== pLookUpCancelBtn
->GetName())
404 strcpy (ListDB_Selection
,"");
405 strcpy (ListDB_Selection2
,"");
410 }; // ClookUpDlg::OnCommand
412 // *********************************** listdb.cpp **********************************