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