]> git.saurik.com Git - wxWidgets.git/blob - demos/dbbrowse/dbtree.cpp
Applied patch [ 825396 ] dbbrowse demo compatible with Unicode
[wxWidgets.git] / demos / dbbrowse / dbtree.cpp
1 //----------------------------------------------------------------------------------------
2 // Name: DBTree.cpp/.h
3 // Purpose: Tree with Table and Views, branches show Field information
4 // Author: Mark Johnson
5 // Modified by:
6 // Created: 19991129
7 // RCS-ID: $Id$
8 // Copyright: (c) Mark Johnson, Berlin Germany
9 // Licence: wxWindows license
10 //----------------------------------------------------------------------------------------
11 //-- all #ifdefs that the whole Project needs. -------------------------------------------
12 //----------------------------------------------------------------------------------------
13 #ifdef __GNUG__
14 #pragma implementation
15 #pragma interface
16 #endif
17 //----------------------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
20 //----------------------------------------------------------------------------------------
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24 //----------------------------------------------------------------------------------------
25 #ifndef WX_PRECOMP
26 #include "wx/wx.h"
27 #endif
28 //----------------------------------------------------------------------------------------
29 #ifndef __WXMSW__
30 #endif
31 //----------------------------------------------------------------------------------------
32 //-- all #includes that every .cpp needs --- 19990807.mj10777 ----------------
33 //----------------------------------------------------------------------------------------
34 #include "std.h" // sorgsam Pflegen !
35
36 //----------------------------------------------------------------------------------------
37 //-- Global functions --------------------------------------------------------------------
38 //----------------------------------------------------------------------------------------
39 static inline const wxChar *bool2String(bool b)
40 {
41 return b ? _T("") : _T("not ");
42 }
43
44 //----------------------------------------------------------------------------------------
45 BEGIN_EVENT_TABLE(DBTree, wxTreeCtrl)
46 EVT_MOTION (DBTree::OnMouseMove)
47 EVT_TREE_SEL_CHANGED(TREE_CTRL_DB, DBTree::OnSelChanged)
48 EVT_TREE_ITEM_RIGHT_CLICK(TREE_CTRL_DB,DBTree::OnRightSelect)
49 EVT_MENU(DATA_SHOW,DBTree::OnDBGrid)
50 EVT_MENU(DATA_DB,DBTree::OnDBClass)
51 EVT_MENU(DATA_TABLE,DBTree::OnTableClass)
52 EVT_MENU(DATA_TABLE_ALL,DBTree::OnTableClassAll)
53 END_EVENT_TABLE()
54 //----------------------------------------------------------------------------------------
55 // DBTree implementation
56 //----------------------------------------------------------------------------------------
57 IMPLEMENT_DYNAMIC_CLASS(DBTree, wxTreeCtrl)
58
59 //----------------------------------------------------------------------------------------
60 DBTree::DBTree(wxWindow *parent) : wxTreeCtrl(parent)
61 {
62 }
63
64 //----------------------------------------------------------------------------------------
65 DBTree::DBTree(wxWindow *parent, const wxWindowID id,const wxPoint& pos, const wxSize& size, long style)
66 : wxTreeCtrl(parent, id, pos, size, style)
67 {
68 const int image_size = 16;
69 // Make an image list containing small icons
70 p_imageListNormal = new wxImageList(image_size, image_size, TRUE);
71 // should correspond to TreeIc_xxx enum
72 #if !defined(__WXMSW__)
73 #include "bitmaps/logo.xpm"
74 #include "bitmaps/dsnclose.xpm"
75 #include "bitmaps/dsnopen.xpm"
76 #include "bitmaps/tab.xpm"
77 #include "bitmaps/view.xpm"
78 #include "bitmaps/col.xpm"
79 #include "bitmaps/key.xpm"
80 #include "bitmaps/keyf.xpm"
81 #include "bitmaps/d_open.xpm"
82 #include "bitmaps/d_closed.xpm"
83 #endif
84 p_imageListNormal->Add(wxBitmap(wxBitmap(wxICON(aLogo)).ConvertToImage().Rescale(image_size, image_size)));
85 p_imageListNormal->Add(wxBitmap(wxBitmap(wxICON(DsnClosed)).ConvertToImage().Rescale(image_size, image_size)));
86 p_imageListNormal->Add(wxBitmap(wxBitmap(wxICON(DsnOpen)).ConvertToImage().Rescale(image_size, image_size)));
87 p_imageListNormal->Add(wxBitmap(wxBitmap(wxICON(TAB)).ConvertToImage().Rescale(image_size, image_size)));
88 p_imageListNormal->Add(wxBitmap(wxBitmap(wxICON(VIEW)).ConvertToImage().Rescale(image_size, image_size)));
89 p_imageListNormal->Add(wxBitmap(wxBitmap(wxICON(COL)).ConvertToImage().Rescale(image_size, image_size)));
90 p_imageListNormal->Add(wxBitmap(wxBitmap(wxICON(KEY)).ConvertToImage().Rescale(image_size, image_size)));
91 p_imageListNormal->Add(wxBitmap(wxBitmap(wxICON(KEYF)).ConvertToImage().Rescale(image_size, image_size)));
92 p_imageListNormal->Add(wxBitmap(wxBitmap(wxICON(DocOpen)).ConvertToImage().Rescale(image_size, image_size)));
93 p_imageListNormal->Add(wxBitmap(wxBitmap(wxICON(DocOpen)).ConvertToImage().Rescale(image_size, image_size)));
94 SetImageList(p_imageListNormal);
95 ct_BrowserDB = NULL;
96 popupMenu1 = NULL;
97 popupMenu2 = NULL;
98 }
99
100 //----------------------------------------------------------------------------------------
101 DBTree::~DBTree()
102 {
103 delete ct_BrowserDB;
104
105 // delete (pDoc->db_Br+i_Which);
106 // wxLogMessage("DBTree::~DBTree() - Vor OnCloseDB()");
107 (pDoc->db_Br+i_Which)->OnCloseDB(FALSE);
108 // wxLogMessage("DBTree::~DBTree() - Nach OnCloseDB()");
109 (pDoc->db_Br+i_Which)->db_BrowserDB = NULL;
110 (pDoc->db_Br+i_Which)->ct_BrowserDB = NULL;
111 (pDoc->db_Br+i_Which)->cl_BrowserDB = NULL;
112
113 delete p_imageListNormal;
114 if (popupMenu1) // If the DSN has no Tables, then no delete should be done !
115 delete popupMenu1;
116 if (popupMenu1) // If the DSN has no Tables, then no delete should be done !
117 delete popupMenu2;
118 }
119
120 //----------------------------------------------------------------------------------------
121 #undef TREE_EVENT_HANDLER
122
123 //----------------------------------------------------------------------------------------
124 int DBTree::OnPopulate()
125 {
126 wxTreeItemId Root, Folder, Docu, Funkt;
127 int i,x,y,z=0, TableType;
128 wxString SQL_TYPE, DB_TYPE;
129 SetFont(* pDoc->ft_Doc);
130 //---------------------------------------------------------------------------------------
131 if ((pDoc->db_Br+i_Which)->Initialize(FALSE))
132 {
133 wxStopWatch sw;
134 wxBeginBusyCursor();
135 ct_BrowserDB = (pDoc->db_Br+i_Which)->OnGetCatalog(FALSE);
136 if (ct_BrowserDB)
137 { // Use the wxDatabase Information
138 Temp0.Printf(_T("%s - (%s) (%s)"), s_DSN.c_str(),ct_BrowserDB->catalog, ct_BrowserDB->schema);
139 Root = AddRoot(Temp0,TreeIc_DsnOpen,TreeIc_DsnOpen,new DBTreeData(_T("Root")));
140 for (x=0;x<ct_BrowserDB->numTables;x++)
141 {
142 wxYield();
143 TableType = 0; // TABLE = 1 ; VIEW = 2 ; 0 We are not interested in
144 if (!wxStrcmp((ct_BrowserDB->pTableInf+x)->tableType,_T("TABLE"))) // only TABLES
145 TableType = 1;
146 if (!wxStrcmp((ct_BrowserDB->pTableInf+x)->tableType,_T("VIEW"))) // and VIEWS
147 TableType = 2;
148 if (TableType) // only TABLES or Views
149 {
150 Temp1.Printf(_T("TN(%s"),(ct_BrowserDB->pTableInf+x)->tableName);
151 //----
152 (ct_BrowserDB->pTableInf+x)->pColInf = (pDoc->db_Br+i_Which)->OnGetColumns((ct_BrowserDB->pTableInf+x)->tableName,(ct_BrowserDB->pTableInf+x)->numCols,FALSE);
153 //----
154 if ((ct_BrowserDB->pTableInf+x)->pColInf)
155 {
156 if (TableType == 1) // Table
157 {
158 Temp0.Printf(_("Table-Name(%s) with (%d)Columns ; Remarks(%s)"), (ct_BrowserDB->pTableInf+x)->tableName,
159 (ct_BrowserDB->pTableInf+x)->numCols,(ct_BrowserDB->pTableInf+x)->tableRemarks);
160 Folder = AppendItem(Root,Temp0,TreeIc_TAB,TreeIc_TAB, new DBTreeData(Temp1));
161 }
162 if (TableType == 2) // View
163 {
164 Temp0.Printf(_("View-Name(%s) with (%d)Columns ; Remarks(%s)"), (ct_BrowserDB->pTableInf+x)->tableName,
165 (ct_BrowserDB->pTableInf+x)->numCols,(ct_BrowserDB->pTableInf+x)->tableRemarks);
166 Folder = AppendItem(Root,Temp0,TreeIc_VIEW,TreeIc_VIEW, new DBTreeData(Temp1));
167 }
168 for (y=0;y<(ct_BrowserDB->pTableInf+x)->numCols;y++)
169 {
170 Temp1.Printf(_T("FN(%s"),((ct_BrowserDB->pTableInf+x)->pColInf+y)->colName);
171 // Here is where we find out if the Column is a Primary / Foreign Key
172 if (((ct_BrowserDB->pTableInf+x)->pColInf+y)->PkCol != 0) // Primary Key
173 {
174 Temp2.Printf(_T("(%d) - %s"),((ct_BrowserDB->pTableInf+x)->pColInf+y)->PkCol,((ct_BrowserDB->pTableInf+x)->pColInf+y)->colName);
175 Docu = AppendItem(Folder,Temp2,TreeIc_KEY,TreeIc_KEY,new DBTreeData(Temp1));
176 Temp2 = ((ct_BrowserDB->pTableInf+x)->pColInf+y)->PkTableName;
177 if (Temp2 == _T(""))
178 Temp2 = _("None");
179 Temp2.Printf(_("This Primary Key is used in the following Tables : %s"),Temp2.c_str());
180 Funkt = AppendItem(Docu,Temp2,TreeIc_DocClosed,TreeIc_DocOpen,new DBTreeData(_T("KEY")));
181 }
182 else
183 {
184 if (((ct_BrowserDB->pTableInf+x)->pColInf+y)->FkCol != 0) // Foreign Key
185 {
186 Temp2.Printf(_T("(%d) - %s"),((ct_BrowserDB->pTableInf+x)->pColInf+y)->FkCol,((ct_BrowserDB->pTableInf+x)->pColInf+y)->colName);
187 Docu = AppendItem(Folder,Temp2,TreeIc_KEYF,TreeIc_KEYF,new DBTreeData(Temp1));
188 Temp2.Printf(_("This Foreign Key comes from the following Table : %s"),((ct_BrowserDB->pTableInf+x)->pColInf+y)->FkTableName);
189 Funkt = AppendItem(Docu,Temp2,TreeIc_DocClosed,TreeIc_DocOpen,new DBTreeData(_T("KEYF")));
190 }
191 else
192 Docu = AppendItem(Folder,((ct_BrowserDB->pTableInf+x)->pColInf+y)->colName,TreeIc_COL,TreeIc_COL,new DBTreeData(Temp1));
193 }
194 SQL_TYPE.Printf(_T("SQL_C_???? (%d)"),((ct_BrowserDB->pTableInf+x)->pColInf+y)->sqlDataType);
195 DB_TYPE.Printf(_T("DB_DATA_TYPE_???? (%d)"),((ct_BrowserDB->pTableInf+x)->pColInf+y)->dbDataType);
196 for (i=1;i<=(pDoc->db_Br+i_Which)->i_SqlTyp[0];i++)
197 {
198 if (((ct_BrowserDB->pTableInf+x)->pColInf+y)->sqlDataType == (pDoc->db_Br+i_Which)->i_SqlTyp[i])
199 {
200 SQL_TYPE.Printf(_T("%s(%d) ; "),(pDoc->db_Br+i_Which)->s_SqlTyp[i].c_str(),(pDoc->db_Br+i_Which)->i_SqlTyp[i]);
201 }
202 } // for (i=1;i<=i_SqlTyp[0];i++)
203 wxYield();
204 for (i=1;i<=(pDoc->db_Br+i_Which)->i_dbTyp[0];i++)
205 {
206 if (((ct_BrowserDB->pTableInf+x)->pColInf+y)->dbDataType == (pDoc->db_Br+i_Which)->i_dbTyp[i])
207 {
208 DB_TYPE.Printf(_T("%s(%d)"),(pDoc->db_Br+i_Which)->s_dbTyp[i].c_str(),(pDoc->db_Br+i_Which)->i_dbTyp[i]);
209 }
210 } // for (i=1;i<=i_dbTyp[0];i++)
211 wxYield();
212 SQL_TYPE += DB_TYPE;
213 Funkt = AppendItem(Docu,SQL_TYPE,TreeIc_DocClosed,TreeIc_DocOpen,new DBTreeData(SQL_TYPE));
214 SQL_TYPE.Printf(_T("%10s %d,%d"),((ct_BrowserDB->pTableInf+x)->pColInf+y)->typeName,
215 ((ct_BrowserDB->pTableInf+x)->pColInf+y)->columnSize,((ct_BrowserDB->pTableInf+x)->pColInf+y)->decimalDigits);
216 Funkt = AppendItem(Docu,SQL_TYPE,TreeIc_DocClosed,TreeIc_DocOpen,new DBTreeData(SQL_TYPE));
217 } // for (y=0;y<(ct_BrowserDB->pTableInf+x)->numCols;y++)
218 } // if ((ct_BrowserDB->pTableInf+x)->pColInf)
219 else
220 Folder = AppendItem(Root,Temp0,TreeIc_FolderClosed,TreeIc_FolderOpen, new DBTreeData(Temp1));
221 z++;
222 // if (z % 10 == 0)
223 {
224 Temp0.Printf(_("-I-> DBTree::OnPopulate(%s) - Table %6d (from %d) has been read."),(ct_BrowserDB->pTableInf+x)->tableName,z,ct_BrowserDB->numTables);
225 pDoc->p_MainFrame->SetStatusText(Temp0, 0);
226 }
227 wxYield();
228 } // if ((ct_BrowserDB->pTableInf+x)->tableType == "TABLE" or VIEW)
229 // else
230 // wxLogMessage(_("\n-I-> if ! TABLE or VIEW >%s<"),(ct_BrowserDB->pTableInf+x)->tableType);
231 } // for (x=0;x<ct_BrowserDB->numTables;x++)
232 } // if (ct_BrowserDB)
233 else
234 wxLogMessage(_("\n-E-> DBTree::OnPopulate() : Invalid Catalog Pointer : Failed"));
235 wxEndBusyCursor();
236 Temp0.Printf(_("-I-> DBTree::OnPopulate() - %6d Tables have been read. - Time needed : %ld ms"),z,sw.Time());
237 wxLogMessage(Temp0);
238 pDoc->p_MainFrame->SetStatusText(Temp0, 0);
239 } // if((pDoc->db_Br+i_Which)->Initialize(FALSE))
240 else
241 {
242 wxLogMessage(_("\n-E-> DBTree::OnPopulate() : A valid Pointer could not be created : Failed"));
243 return 0;
244 }
245 //---------------------------------------------------------------------------------------
246 Expand(Root);
247 //---------------------------------------------------------------------------------------
248 popupMenu1 = NULL;
249 popupMenu1 = new wxMenu(_T(""));
250 popupMenu1->Append(DATA_DB, _("Make wxDB.cpp/h "));
251 popupMenu1->AppendSeparator();
252 popupMenu1->Append(DATA_TABLE_ALL, _("Make all wxTable.cpp/h classes"));
253 popupMenu2 = NULL;
254 popupMenu2 = new wxMenu(_T(""));
255 popupMenu2->Append(DATA_SHOW, _("Show Data"));
256 popupMenu2->AppendSeparator();
257 popupMenu2->Append(DATA_TABLE, _("Make wxTable.cpp/h "));
258 //---------------------------------------------------------------------------------------
259 return 0;
260 } // DBTree::OnPopulate()
261
262 //----------------------------------------------------------------------------------------
263 void DBTree::OnSelChanged(wxTreeEvent& WXUNUSED(event))
264 {
265 int i;
266 Temp0.Empty();
267 pDoc->p_MainFrame->SetStatusText(Temp0,0);
268 // Get the Information that we need
269 wxTreeItemId itemId = GetSelection();
270 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
271 if ( item != NULL )
272 {
273 int Treffer = 0;
274 Temp1.Printf(_T("%s"),item->m_desc.c_str());
275 //-------------------------------------------------------------------------------------
276 if (Temp1.Contains(_T("ODBC-")))
277 {
278 Temp1 = Temp1.Mid(5,wxSTRING_MAXLEN);
279 for (i=0;i<pDoc->i_DSN;i++)
280 {
281 if (Temp1 == (pDoc->p_DSN+i)->Dsn)
282 {
283 // pDoc->OnChosenDSN(i);
284 }
285 }
286 Treffer++;
287 }
288 //--------------------------------------------------------------------------------------
289 if (Treffer == 0)
290 {
291 //-------------------------------------------------------------------------------------
292 /*
293 Temp0.Printf(_("Item '%s': %sselected, %sexpanded, %sbold,"
294 "%u children (%u immediately under this item)."),
295 item->m_desc.c_str(),
296 bool2String(IsSelected(itemId)),
297 bool2String(IsExpanded(itemId)),
298 bool2String(IsBold(itemId)),
299 GetChildrenCount(itemId),
300 GetChildrenCount(itemId));
301 LogBuf.Printf("-I-> DBTree::OnSelChanged - %s",Temp0.c_str());
302 wxLogMessage( "%s", LogBuf.c_str() );
303 */
304 //-------------------------------------------------------------------------------------
305 }
306 }
307 }
308
309 //----------------------------------------------------------------------------------------
310 void DBTree::OnRightSelect(wxTreeEvent& WXUNUSED(event))
311 {
312 int i;
313 Temp0.Empty();
314 // Get the Information that we need
315 wxTreeItemId itemId = GetSelection();
316 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
317 if ( item != NULL )
318 {
319 int Treffer = 0;
320 Temp1.Printf(_T("%s"),item->m_desc.c_str());
321 //--------------------------------------------------------------------------------------
322 if (!wxStrcmp(_T("Root"),Temp1))
323 {
324 PopupMenu(popupMenu1,TreePos.x,TreePos.y);
325 Treffer++;
326 }
327 for (i=0;i<ct_BrowserDB->numTables;i++)
328 {
329 Temp2.Printf(_T("TN(%s"),(ct_BrowserDB->pTableInf+i)->tableName);
330 if (!wxStrcmp(Temp2,Temp1))
331 {
332 PopupMenu(popupMenu2,TreePos.x,TreePos.y);
333 Treffer++;
334 }
335 }
336 //--------------------------------------------------------------------------------------
337 if (Treffer == 0)
338 {
339 //-------------------------------------------------------------------------------------
340 /*
341 Temp0.Printf(_("Item '%s': %sselected, %sexpanded, %sbold,"
342 "%u children (%u immediately under this item)."),
343 item->m_desc.c_str(),
344 bool2String(IsSelected(itemId)),
345 bool2String(IsExpanded(itemId)),
346 bool2String(IsBold(itemId)),
347 GetChildrenCount(itemId),
348 GetChildrenCount(itemId));
349 LogBuf.Printf("-I-> DBTree::OnSelChanged - %s",Temp0.c_str());
350 wxLogMessage( "%s", LogBuf.c_str() );
351 */
352 //-------------------------------------------------------------------------------------
353 }
354 }
355 }
356
357 //----------------------------------------------------------------------------------------
358 void DBTree::OnDBGrid(wxCommandEvent& event)
359 {
360 int i;
361 // Get the Information that we need
362 wxTreeItemId itemId = GetSelection();
363 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
364 if ( item != NULL )
365 {
366 Temp1.Printf(_T("%s"),item->m_desc.c_str());
367 for (i=0;i<ct_BrowserDB->numTables;i++)
368 {
369 Temp2.Printf(_T("TN(%s"),(ct_BrowserDB->pTableInf+i)->tableName);
370 if (!wxStrcmp(Temp2,Temp1))
371 {
372 // Temp0.Printf("(%d) Here is where a GridCtrl for >%s< will be called! ",i,(ct_BrowserDB->pTableInf+i)->tableName);
373 pDoc->OnChosenTbl(1,(ct_BrowserDB->pTableInf+i)->tableName);
374 // wxMessageBox(Temp0);
375 }
376 }
377 }
378 }
379
380 //----------------------------------------------------------------------------------------
381 void DBTree::OnDBClass(wxCommandEvent& event)
382 {
383 // int i;
384 // Get the Information that we need
385 wxTreeItemId itemId = GetSelection();
386 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
387 if ( item != NULL )
388 {
389 Temp0.Printf(_("Here is where a wxDB Class for >%s< will be made! "),s_DSN.c_str());
390 wxMessageBox(Temp0);
391 }
392 }
393
394 //----------------------------------------------------------------------------------------
395 void DBTree::OnTableClass(wxCommandEvent& event)
396 {
397 int i;
398 // Get the Information that we need
399 wxTreeItemId itemId = GetSelection();
400 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
401 if ( item != NULL )
402 {
403 Temp1.Printf(_T("%s"),item->m_desc.c_str());
404 for (i=0;i<ct_BrowserDB->numTables;i++)
405 {
406 Temp2.Printf(_T("TN(%s"),(ct_BrowserDB->pTableInf+i)->tableName);
407 if (!wxStrcmp(Temp2,Temp1))
408 {
409 Temp0.Printf(_("(%d) Here is where a wxTable Class for >%s< will be made! "),i,(ct_BrowserDB->pTableInf+i)->tableName);
410 wxMessageBox(Temp0);
411 }
412 }
413 }
414 }
415
416 //----------------------------------------------------------------------------------------
417 void DBTree::OnTableClassAll(wxCommandEvent& event)
418 {
419 // int i;
420 // Get the Information that we need
421 wxTreeItemId itemId = GetSelection();
422 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
423 if ( item != NULL )
424 {
425 Temp0.Printf(_("Here is where all wxTable Classes in >%s< will be made! "),s_DSN.c_str());
426 wxMessageBox(Temp0);
427 }
428 }
429
430 //----------------------------------------------------------------------------------------
431 void DBTree::OnMouseMove(wxMouseEvent &event)
432 {
433 TreePos = event.GetPosition();
434 }
435 //----------------------------------------------------------------------------------------