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