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