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