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