]> git.saurik.com Git - wxWidgets.git/blame - demos/dbbrowse/dbtree.cpp
suppres some warnings, suppress assert failure for fonts
[wxWidgets.git] / demos / dbbrowse / dbtree.cpp
CommitLineData
b5ffecfc
GT
1//---------------------------------------------------------------------------
2// Name: DBTree.h
3// Purpose: Programm Control with a Tree
4// Author: Mark Johnson
5// Modified by:
6// Created: 19991129
c09d434d 7/// RCS-ID: $Id$
b5ffecfc
GT
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__
b5ce269b
BJ
14#pragma implementation
15#pragma interface
b5ffecfc
GT
16#endif
17//---------------------------------------------------------------------------
18// For compilers that support precompilation, includes "wx/wx.h".
19#include "wx/wxprec.h"
20//---------------------------------------------------------------------------
21#ifdef __BORLANDC__
b5ce269b 22#pragma hdrstop
b5ffecfc
GT
23#endif
24//---------------------------------------------------------------------------
25#ifndef WX_PRECOMP
b5ce269b 26#include "wx/wx.h"
b5ffecfc
GT
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//---------------------------------------------------------------------------
38static inline const char *bool2String(bool b)
39{
b5ce269b 40 return b ? "" : "not ";
b5ffecfc
GT
41}
42//---------------------------------------------------------------------------
43BEGIN_EVENT_TABLE(DBTree, wxTreeCtrl)
b5ce269b
BJ
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)
b5ffecfc
GT
58{
59}
60DBTree::DBTree(wxWindow *parent, const wxWindowID id,const wxPoint& pos, const wxSize& size, long style)
b5ce269b 61 : wxTreeCtrl(parent, id, pos, size, style)
b5ffecfc 62{
b5ce269b
BJ
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
13b59a33
BJ
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
b5ce269b
BJ
90
91 SetImageList(p_imageListNormal);
92 ct_BrowserDB = NULL;
b5ffecfc
GT
93}
94//---------------------------------------------------------------------------
95DBTree::~DBTree()
96{
b5ce269b
BJ
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;
b5ffecfc
GT
105}
106//---------------------------------------------------------------------------
107#undef TREE_EVENT_HANDLER
108//---------------------------------------------------------------------------
109int DBTree::OnPopulate()
110{
b5ce269b
BJ
111 wxTreeItemId Root, Folder, Docu, Funkt;
112 int i,x,y;
113 wxString SQL_TYPE, DB_TYPE;
13b59a33 114 wxBeginBusyCursor();
b5ce269b
BJ
115 //----------------------------------------------------------------------------------------------------------------------------
116 if((pDoc->db_Br+i_Which)->Initialize(FALSE))
b5ffecfc 117 {
b5ce269b
BJ
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);
122 Root = AddRoot(Temp0,TreeIc_DsnOpen,TreeIc_DsnOpen,new DBTreeData("Root"));
123 for (x=0;x<ct_BrowserDB->numTables;x++)
124 {
125 if (!wxStrcmp((ct_BrowserDB->pTableInf+x)->tableType,"TABLE")) // only TABLES
126 {
127 Temp0.Printf(_("Tablename(%s) with (%d)Columns ; Remarks(%s)"), (ct_BrowserDB->pTableInf+x)->tableName,
128 (ct_BrowserDB->pTableInf+x)->numCols,
129 (ct_BrowserDB->pTableInf+x)->tableRemarks);
130 Temp1.Printf("TN(%s",(ct_BrowserDB->pTableInf+x)->tableName);
131 //----
132 (ct_BrowserDB->pTableInf+x)->pColInf = (pDoc->db_Br+i_Which)->OnGetColumns((ct_BrowserDB->pTableInf+x)->tableName,(ct_BrowserDB->pTableInf+x)->numCols,FALSE);
133 //----
134 if ((ct_BrowserDB->pTableInf+x)->pColInf)
135 {
136 Temp0.Printf(_("Tablename(%s) with (%d)Columns ; Remarks(%s)"), (ct_BrowserDB->pTableInf+x)->tableName,
137 (ct_BrowserDB->pTableInf+x)->numCols,
138 (ct_BrowserDB->pTableInf+x)->tableRemarks);
139 Folder = AppendItem(Root,Temp0,TreeIc_TAB,TreeIc_TAB, new DBTreeData(Temp1));
140 for (y=0;y<(ct_BrowserDB->pTableInf+x)->numCols;y++)
141 {
142 Temp1.Printf("FN(%s",((ct_BrowserDB->pTableInf+x)->pColInf+y)->colName);
143 // Here is where we find out if the Column is a Primary / Foreign Key
144 if (((ct_BrowserDB->pTableInf+x)->pColInf+y)->PkCol != 0) // Primary Key
145 {
146 Docu = AppendItem(Folder,((ct_BrowserDB->pTableInf+x)->pColInf+y)->colName,TreeIc_KEY,TreeIc_KEY,new DBTreeData(Temp1));
147 Temp2.Printf(_("This Key is used in the following Tables : %s"),((ct_BrowserDB->pTableInf+x)->pColInf+y)->PkTableName);
148 Funkt = AppendItem(Docu,Temp2,TreeIc_DocClosed,TreeIc_DocOpen,new DBTreeData("KEY"));
149 }
b5ffecfc 150 else
b5ce269b
BJ
151 if (((ct_BrowserDB->pTableInf+x)->pColInf+y)->FkCol != 0) // Foreign Key
152 {
153 Docu = AppendItem(Folder,((ct_BrowserDB->pTableInf+x)->pColInf+y)->colName,TreeIc_KEYF,TreeIc_KEYF,new DBTreeData(Temp1));
154 Temp2.Printf(_("This Foreign Key comes from the following Table : %s"),((ct_BrowserDB->pTableInf+x)->pColInf+y)->FkTableName);
155 Funkt = AppendItem(Docu,Temp2,TreeIc_DocClosed,TreeIc_DocOpen,new DBTreeData("KEYF"));
156 }
157 else
158 Docu = AppendItem(Folder,((ct_BrowserDB->pTableInf+x)->pColInf+y)->colName,TreeIc_COL,TreeIc_COL,new DBTreeData(Temp1));
159 SQL_TYPE.Printf(_("SQL_C_???? (%d)"),((ct_BrowserDB->pTableInf+x)->pColInf+y)->sqlDataType);
160 DB_TYPE.Printf(_("DB_DATA_TYPE_???? (%d)"),((ct_BrowserDB->pTableInf+x)->pColInf+y)->dbDataType);
161 for (i=1;i<=(pDoc->db_Br+i_Which)->i_SqlTyp[0];i++)
162 {
163 if (((ct_BrowserDB->pTableInf+x)->pColInf+y)->sqlDataType == (pDoc->db_Br+i_Which)->i_SqlTyp[i])
164 {
165 SQL_TYPE.Printf("%s(%d) ; ",(pDoc->db_Br+i_Which)->s_SqlTyp[i].c_str(),(pDoc->db_Br+i_Which)->i_SqlTyp[i]);
166 }
167 } // for (i=1;i<=i_SqlTyp[0];i++)
168 for (i=1;i<=(pDoc->db_Br+i_Which)->i_dbTyp[0];i++)
169 {
170 if (((ct_BrowserDB->pTableInf+x)->pColInf+y)->dbDataType == (pDoc->db_Br+i_Which)->i_dbTyp[i])
171 {
172 DB_TYPE.Printf("%s(%d)",(pDoc->db_Br+i_Which)->s_dbTyp[i].c_str(),(pDoc->db_Br+i_Which)->i_dbTyp[i]);
173 }
174 } // for (i=1;i<=i_dbTyp[0];i++)
175 SQL_TYPE += DB_TYPE;
176 Funkt = AppendItem(Docu,SQL_TYPE,TreeIc_DocClosed,TreeIc_DocOpen,new DBTreeData(SQL_TYPE));
177 SQL_TYPE.Printf("%10s %d,%d",((ct_BrowserDB->pTableInf+x)->pColInf+y)->typeName,
178 ((ct_BrowserDB->pTableInf+x)->pColInf+y)->columnSize,
179 ((ct_BrowserDB->pTableInf+x)->pColInf+y)->decimalDigits);
180 Funkt = AppendItem(Docu,SQL_TYPE,TreeIc_DocClosed,TreeIc_DocOpen,new DBTreeData(SQL_TYPE));
181 } // for (y=0;y<(ct_BrowserDB->pTableInf+x)->numCols;y++)
182 } // if ((ct_BrowserDB->pTableInf+x)->pColInf)
183 else
184 Folder = AppendItem(Root,Temp0,TreeIc_FolderClosed,TreeIc_FolderOpen, new DBTreeData(Temp1));
185 } // if ((ct_BrowserDB->pTableInf+x)->tableType == "TABLE")
186 } // for (x=0;x<ct_BrowserDB->numTables;x++)
187 } // if (ct_BrowserDB)
188 else
189 wxLogMessage(_("\n-E-> DBTree::OnPopulate() : Invalid Catalog Pointer : Failed"));
190 } // if((pDoc->db_Br+i_Which)->Initialize(FALSE))
b5ffecfc 191 else
b5ce269b 192 {
13b59a33 193 wxEndBusyCursor();
ed6e827e 194 return 0;
13b59a33 195 //wxLogMessage(_("\n-E-> DBTree::OnPopulate() : A valid Pointer could not be created : Failed"));
b5ce269b
BJ
196 }
197 //----------------------------------------------------------------------------------------------------------------------------
198 Expand(Root);
199 //----------------------------------------------------------------------------------------------------------------------------
200 popupMenu1 = NULL;
201 popupMenu1 = new wxMenu("");
202 popupMenu1->Append(DATA_DB, _("Make wxDB.cpp/h "));
203 popupMenu1->AppendSeparator();
204 popupMenu1->Append(DATA_TABLE_ALL, _("Make all wxTable.cpp/h classes"));
205 popupMenu2 = NULL;
206 popupMenu2 = new wxMenu("");
207 popupMenu2->Append(DATA_SHOW, _("Show Data"));
208 popupMenu2->AppendSeparator();
209 popupMenu2->Append(DATA_TABLE, _("Make wxTable.cpp/h "));
210 //----------------------------------------------------------------------------------------------------------------------------
13b59a33 211 wxEndBusyCursor();
b5ce269b 212 return 0;
b5ffecfc
GT
213}
214//---------------------------------------------------------------------------
215void DBTree::OnSelChanged(wxTreeEvent& WXUNUSED(event))
216{
b5ce269b
BJ
217 int i;
218 Temp0.Empty();
219 pDoc->p_MainFrame->SetStatusText(Temp0,0);
220 // Get the Information that we need
221 wxTreeItemId itemId = GetSelection();
222 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
223 if ( item != NULL )
b5ffecfc 224 {
b5ce269b
BJ
225 int Treffer = 0;
226 Temp1.Printf("%s",item->m_desc.c_str());
227 //-----------------------------------------------------------------------------------------
228 if (Temp1.Contains("ODBC-"))
229 {
230 Temp1 = Temp1.Mid(5,wxSTRING_MAXLEN);
231 for (i=0;i<pDoc->i_DSN;i++)
232 {
233 if (Temp1 == (pDoc->p_DSN+i)->Dsn)
234 {
235 // pDoc->OnChosenDSN(i);
236 }
237 }
238 Treffer++;
239 }
240 //-----------------------------------------------------------------------------------------
241 if (Treffer == 0)
242 {
243 //---------------------------------------------------
244 /*
245 Temp0.Printf(_("Item '%s': %sselected, %sexpanded, %sbold,"
246 "%u children (%u immediately under this item)."),
247 item->m_desc.c_str(),
248 bool2String(IsSelected(itemId)),
249 bool2String(IsExpanded(itemId)),
250 bool2String(IsBold(itemId)),
251 GetChildrenCount(itemId),
252 GetChildrenCount(itemId));
253 LogBuf.Printf("-I-> DBTree::OnSelChanged - %s",Temp0.c_str());
254 wxLogMessage( "%s", LogBuf.c_str() );
255 */
256 //---------------------------------------------------
257 }
b5ffecfc 258 }
b5ffecfc
GT
259}
260//---------------------------------------------------------------------------
261void DBTree::OnRightSelect(wxTreeEvent& WXUNUSED(event))
262{
b5ce269b
BJ
263 int i;
264 Temp0.Empty();
265 // Get the Information that we need
266 wxTreeItemId itemId = GetSelection();
267 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
268 if ( item != NULL )
269 {
270 int Treffer = 0;
271 Temp1.Printf("%s",item->m_desc.c_str());
272 //-----------------------------------------------------------------------------------------
273 if (!wxStrcmp("Root",Temp1))
274 {
275 PopupMenu(popupMenu1,TreePos.x,TreePos.y);
276 Treffer++;
277 }
278 for (i=0;i<ct_BrowserDB->numTables;i++)
279 {
280 Temp2.Printf("TN(%s",(ct_BrowserDB->pTableInf+i)->tableName);
281 if (!wxStrcmp(Temp2,Temp1))
282 {
283 PopupMenu(popupMenu2,TreePos.x,TreePos.y);
284 Treffer++;
285 }
286 }
287 //-----------------------------------------------------------------------------------------
288 if (Treffer == 0)
289 {
290 //---------------------------------------------------
291 /*
292 Temp0.Printf(_("Item '%s': %sselected, %sexpanded, %sbold,"
293 "%u children (%u immediately under this item)."),
294 item->m_desc.c_str(),
295 bool2String(IsSelected(itemId)),
296 bool2String(IsExpanded(itemId)),
297 bool2String(IsBold(itemId)),
298 GetChildrenCount(itemId),
299 GetChildrenCount(itemId));
300 LogBuf.Printf("-I-> DBTree::OnSelChanged - %s",Temp0.c_str());
301 wxLogMessage( "%s", LogBuf.c_str() );
302 */
303 //---------------------------------------------------
304 }
305 }
b5ffecfc
GT
306}
307//---------------------------------------------------------------------------
308void DBTree::OnDBGrid(wxMenu& , wxCommandEvent& event)
309{
b5ce269b
BJ
310 int i;
311 // Get the Information that we need
312 wxTreeItemId itemId = GetSelection();
313 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
314 if ( item != NULL )
315 {
316 Temp1.Printf("%s",item->m_desc.c_str());
317 for (i=0;i<ct_BrowserDB->numTables;i++)
318 {
319 Temp2.Printf("TN(%s",(ct_BrowserDB->pTableInf+i)->tableName);
320 if (!wxStrcmp(Temp2,Temp1))
321 {
322 // Temp0.Printf("(%d) Here is where a GridCtrl for >%s< will be called! ",i,(ct_BrowserDB->pTableInf+i)->tableName);
323 pDoc->OnChosenTbl(1,(ct_BrowserDB->pTableInf+i)->tableName);
324 // wxMessageBox(Temp0);
325 }
326 }
327 }
b5ffecfc
GT
328}
329//---------------------------------------------------------------------------
330void DBTree::OnDBClass(wxMenu& , wxCommandEvent& event)
331{
b5ce269b
BJ
332 // int i;
333 // Get the Information that we need
334 wxTreeItemId itemId = GetSelection();
335 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
336 if ( item != NULL )
337 {
338 Temp0.Printf(_("Here is where a wxDB Class for >%s< will be made! "),s_DSN.c_str());
339 wxMessageBox(Temp0);
340 }
b5ffecfc
GT
341}
342//---------------------------------------------------------------------------
343void DBTree::OnTableClass(wxMenu& , wxCommandEvent& event)
344{
b5ce269b
BJ
345 int i;
346 // Get the Information that we need
347 wxTreeItemId itemId = GetSelection();
348 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
349 if ( item != NULL )
350 {
351 Temp1.Printf("%s",item->m_desc.c_str());
352 for (i=0;i<ct_BrowserDB->numTables;i++)
353 {
354 Temp2.Printf("TN(%s",(ct_BrowserDB->pTableInf+i)->tableName);
355 if (!wxStrcmp(Temp2,Temp1))
356 {
357 Temp0.Printf(_("(%d) Here is where a wxTable Class for >%s< will be made! "),i,(ct_BrowserDB->pTableInf+i)->tableName);
358 wxMessageBox(Temp0);
359 }
360 }
361 }
b5ffecfc
GT
362}
363//---------------------------------------------------------------------------
364void DBTree::OnTableClassAll(wxMenu& , wxCommandEvent& event)
365{
b5ce269b
BJ
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 Temp0.Printf(_("Here is where all wxTable Classes in >%s< will be made! "),s_DSN.c_str());
373 wxMessageBox(Temp0);
374 }
b5ffecfc
GT
375}
376//------------------------------------------------------------------------------
377void DBTree::OnMouseMove(wxMouseEvent &event)
378{
b5ce269b 379 TreePos = event.GetPosition();
b5ffecfc
GT
380}
381//------------------------------------------------------------------------------
b5ce269b
BJ
382
383