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