]> git.saurik.com Git - wxWidgets.git/blob - demos/dbbrowse/dbtree.cpp
re-indent sources
[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:
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 if((pDoc->db_Br+i_Which)->Initialize(FALSE))
116 {
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 if (!wxStrcmp((ct_BrowserDB->pTableInf+x)->tableType,"TABLE")) // only TABLES
125 {
126 Temp0.Printf(_("Tablename(%s) with (%d)Columns ; Remarks(%s)"), (ct_BrowserDB->pTableInf+x)->tableName,
127 (ct_BrowserDB->pTableInf+x)->numCols,
128 (ct_BrowserDB->pTableInf+x)->tableRemarks);
129 Temp1.Printf("TN(%s",(ct_BrowserDB->pTableInf+x)->tableName);
130 //----
131 (ct_BrowserDB->pTableInf+x)->pColInf = (pDoc->db_Br+i_Which)->OnGetColumns((ct_BrowserDB->pTableInf+x)->tableName,(ct_BrowserDB->pTableInf+x)->numCols,FALSE);
132 //----
133 if ((ct_BrowserDB->pTableInf+x)->pColInf)
134 {
135 Temp0.Printf(_("Tablename(%s) with (%d)Columns ; Remarks(%s)"), (ct_BrowserDB->pTableInf+x)->tableName,
136 (ct_BrowserDB->pTableInf+x)->numCols,
137 (ct_BrowserDB->pTableInf+x)->tableRemarks);
138 Folder = AppendItem(Root,Temp0,TreeIc_TAB,TreeIc_TAB, new DBTreeData(Temp1));
139 for (y=0;y<(ct_BrowserDB->pTableInf+x)->numCols;y++)
140 {
141 Temp1.Printf("FN(%s",((ct_BrowserDB->pTableInf+x)->pColInf+y)->colName);
142 // Here is where we find out if the Column is a Primary / Foreign Key
143 if (((ct_BrowserDB->pTableInf+x)->pColInf+y)->PkCol != 0) // Primary Key
144 {
145 Docu = AppendItem(Folder,((ct_BrowserDB->pTableInf+x)->pColInf+y)->colName,TreeIc_KEY,TreeIc_KEY,new DBTreeData(Temp1));
146 Temp2.Printf(_("This Key is used in the following Tables : %s"),((ct_BrowserDB->pTableInf+x)->pColInf+y)->PkTableName);
147 Funkt = AppendItem(Docu,Temp2,TreeIc_DocClosed,TreeIc_DocOpen,new DBTreeData("KEY"));
148 }
149 else
150 if (((ct_BrowserDB->pTableInf+x)->pColInf+y)->FkCol != 0) // Foreign Key
151 {
152 Docu = AppendItem(Folder,((ct_BrowserDB->pTableInf+x)->pColInf+y)->colName,TreeIc_KEYF,TreeIc_KEYF,new DBTreeData(Temp1));
153 Temp2.Printf(_("This Foreign Key comes from the following Table : %s"),((ct_BrowserDB->pTableInf+x)->pColInf+y)->FkTableName);
154 Funkt = AppendItem(Docu,Temp2,TreeIc_DocClosed,TreeIc_DocOpen,new DBTreeData("KEYF"));
155 }
156 else
157 Docu = AppendItem(Folder,((ct_BrowserDB->pTableInf+x)->pColInf+y)->colName,TreeIc_COL,TreeIc_COL,new DBTreeData(Temp1));
158 SQL_TYPE.Printf(_("SQL_C_???? (%d)"),((ct_BrowserDB->pTableInf+x)->pColInf+y)->sqlDataType);
159 DB_TYPE.Printf(_("DB_DATA_TYPE_???? (%d)"),((ct_BrowserDB->pTableInf+x)->pColInf+y)->dbDataType);
160 for (i=1;i<=(pDoc->db_Br+i_Which)->i_SqlTyp[0];i++)
161 {
162 if (((ct_BrowserDB->pTableInf+x)->pColInf+y)->sqlDataType == (pDoc->db_Br+i_Which)->i_SqlTyp[i])
163 {
164 SQL_TYPE.Printf("%s(%d) ; ",(pDoc->db_Br+i_Which)->s_SqlTyp[i].c_str(),(pDoc->db_Br+i_Which)->i_SqlTyp[i]);
165 }
166 } // for (i=1;i<=i_SqlTyp[0];i++)
167 for (i=1;i<=(pDoc->db_Br+i_Which)->i_dbTyp[0];i++)
168 {
169 if (((ct_BrowserDB->pTableInf+x)->pColInf+y)->dbDataType == (pDoc->db_Br+i_Which)->i_dbTyp[i])
170 {
171 DB_TYPE.Printf("%s(%d)",(pDoc->db_Br+i_Which)->s_dbTyp[i].c_str(),(pDoc->db_Br+i_Which)->i_dbTyp[i]);
172 }
173 } // for (i=1;i<=i_dbTyp[0];i++)
174 SQL_TYPE += DB_TYPE;
175 Funkt = AppendItem(Docu,SQL_TYPE,TreeIc_DocClosed,TreeIc_DocOpen,new DBTreeData(SQL_TYPE));
176 SQL_TYPE.Printf("%10s %d,%d",((ct_BrowserDB->pTableInf+x)->pColInf+y)->typeName,
177 ((ct_BrowserDB->pTableInf+x)->pColInf+y)->columnSize,
178 ((ct_BrowserDB->pTableInf+x)->pColInf+y)->decimalDigits);
179 Funkt = AppendItem(Docu,SQL_TYPE,TreeIc_DocClosed,TreeIc_DocOpen,new DBTreeData(SQL_TYPE));
180 } // for (y=0;y<(ct_BrowserDB->pTableInf+x)->numCols;y++)
181 } // if ((ct_BrowserDB->pTableInf+x)->pColInf)
182 else
183 Folder = AppendItem(Root,Temp0,TreeIc_FolderClosed,TreeIc_FolderOpen, new DBTreeData(Temp1));
184 } // if ((ct_BrowserDB->pTableInf+x)->tableType == "TABLE")
185 } // for (x=0;x<ct_BrowserDB->numTables;x++)
186 } // if (ct_BrowserDB)
187 else
188 wxLogMessage(_("\n-E-> DBTree::OnPopulate() : Invalid Catalog Pointer : Failed"));
189 } // if((pDoc->db_Br+i_Which)->Initialize(FALSE))
190 else
191 {
192 wxLogMessage(_("\n-E-> DBTree::OnPopulate() : A valid Pointer could not be created : Failed"));
193 }
194 //----------------------------------------------------------------------------------------------------------------------------
195 Expand(Root);
196 //----------------------------------------------------------------------------------------------------------------------------
197 popupMenu1 = NULL;
198 popupMenu1 = new wxMenu("");
199 popupMenu1->Append(DATA_DB, _("Make wxDB.cpp/h "));
200 popupMenu1->AppendSeparator();
201 popupMenu1->Append(DATA_TABLE_ALL, _("Make all wxTable.cpp/h classes"));
202 popupMenu2 = NULL;
203 popupMenu2 = new wxMenu("");
204 popupMenu2->Append(DATA_SHOW, _("Show Data"));
205 popupMenu2->AppendSeparator();
206 popupMenu2->Append(DATA_TABLE, _("Make wxTable.cpp/h "));
207 //----------------------------------------------------------------------------------------------------------------------------
208 return 0;
209 }
210 //---------------------------------------------------------------------------
211 void DBTree::OnSelChanged(wxTreeEvent& WXUNUSED(event))
212 {
213 int i;
214 Temp0.Empty();
215 pDoc->p_MainFrame->SetStatusText(Temp0,0);
216 // Get the Information that we need
217 wxTreeItemId itemId = GetSelection();
218 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
219 if ( item != NULL )
220 {
221 int Treffer = 0;
222 Temp1.Printf("%s",item->m_desc.c_str());
223 //-----------------------------------------------------------------------------------------
224 if (Temp1.Contains("ODBC-"))
225 {
226 Temp1 = Temp1.Mid(5,wxSTRING_MAXLEN);
227 for (i=0;i<pDoc->i_DSN;i++)
228 {
229 if (Temp1 == (pDoc->p_DSN+i)->Dsn)
230 {
231 // pDoc->OnChosenDSN(i);
232 }
233 }
234 Treffer++;
235 }
236 //-----------------------------------------------------------------------------------------
237 if (Treffer == 0)
238 {
239 //---------------------------------------------------
240 /*
241 Temp0.Printf(_("Item '%s': %sselected, %sexpanded, %sbold,"
242 "%u children (%u immediately under this item)."),
243 item->m_desc.c_str(),
244 bool2String(IsSelected(itemId)),
245 bool2String(IsExpanded(itemId)),
246 bool2String(IsBold(itemId)),
247 GetChildrenCount(itemId),
248 GetChildrenCount(itemId));
249 LogBuf.Printf("-I-> DBTree::OnSelChanged - %s",Temp0.c_str());
250 wxLogMessage( "%s", LogBuf.c_str() );
251 */
252 //---------------------------------------------------
253 }
254 }
255 }
256 //---------------------------------------------------------------------------
257 void DBTree::OnRightSelect(wxTreeEvent& WXUNUSED(event))
258 {
259 int i;
260 Temp0.Empty();
261 // Get the Information that we need
262 wxTreeItemId itemId = GetSelection();
263 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
264 if ( item != NULL )
265 {
266 int Treffer = 0;
267 Temp1.Printf("%s",item->m_desc.c_str());
268 //-----------------------------------------------------------------------------------------
269 if (!wxStrcmp("Root",Temp1))
270 {
271 PopupMenu(popupMenu1,TreePos.x,TreePos.y);
272 Treffer++;
273 }
274 for (i=0;i<ct_BrowserDB->numTables;i++)
275 {
276 Temp2.Printf("TN(%s",(ct_BrowserDB->pTableInf+i)->tableName);
277 if (!wxStrcmp(Temp2,Temp1))
278 {
279 PopupMenu(popupMenu2,TreePos.x,TreePos.y);
280 Treffer++;
281 }
282 }
283 //-----------------------------------------------------------------------------------------
284 if (Treffer == 0)
285 {
286 //---------------------------------------------------
287 /*
288 Temp0.Printf(_("Item '%s': %sselected, %sexpanded, %sbold,"
289 "%u children (%u immediately under this item)."),
290 item->m_desc.c_str(),
291 bool2String(IsSelected(itemId)),
292 bool2String(IsExpanded(itemId)),
293 bool2String(IsBold(itemId)),
294 GetChildrenCount(itemId),
295 GetChildrenCount(itemId));
296 LogBuf.Printf("-I-> DBTree::OnSelChanged - %s",Temp0.c_str());
297 wxLogMessage( "%s", LogBuf.c_str() );
298 */
299 //---------------------------------------------------
300 }
301 }
302 }
303 //---------------------------------------------------------------------------
304 void DBTree::OnDBGrid(wxMenu& , wxCommandEvent& event)
305 {
306 int i;
307 // Get the Information that we need
308 wxTreeItemId itemId = GetSelection();
309 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
310 if ( item != NULL )
311 {
312 Temp1.Printf("%s",item->m_desc.c_str());
313 for (i=0;i<ct_BrowserDB->numTables;i++)
314 {
315 Temp2.Printf("TN(%s",(ct_BrowserDB->pTableInf+i)->tableName);
316 if (!wxStrcmp(Temp2,Temp1))
317 {
318 // Temp0.Printf("(%d) Here is where a GridCtrl for >%s< will be called! ",i,(ct_BrowserDB->pTableInf+i)->tableName);
319 pDoc->OnChosenTbl(1,(ct_BrowserDB->pTableInf+i)->tableName);
320 // wxMessageBox(Temp0);
321 }
322 }
323 }
324 }
325 //---------------------------------------------------------------------------
326 void DBTree::OnDBClass(wxMenu& , wxCommandEvent& event)
327 {
328 // int i;
329 // Get the Information that we need
330 wxTreeItemId itemId = GetSelection();
331 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
332 if ( item != NULL )
333 {
334 Temp0.Printf(_("Here is where a wxDB Class for >%s< will be made! "),s_DSN.c_str());
335 wxMessageBox(Temp0);
336 }
337 }
338 //---------------------------------------------------------------------------
339 void DBTree::OnTableClass(wxMenu& , wxCommandEvent& event)
340 {
341 int i;
342 // Get the Information that we need
343 wxTreeItemId itemId = GetSelection();
344 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
345 if ( item != NULL )
346 {
347 Temp1.Printf("%s",item->m_desc.c_str());
348 for (i=0;i<ct_BrowserDB->numTables;i++)
349 {
350 Temp2.Printf("TN(%s",(ct_BrowserDB->pTableInf+i)->tableName);
351 if (!wxStrcmp(Temp2,Temp1))
352 {
353 Temp0.Printf(_("(%d) Here is where a wxTable Class for >%s< will be made! "),i,(ct_BrowserDB->pTableInf+i)->tableName);
354 wxMessageBox(Temp0);
355 }
356 }
357 }
358 }
359 //---------------------------------------------------------------------------
360 void DBTree::OnTableClassAll(wxMenu& , wxCommandEvent& event)
361 {
362 // int i;
363 // Get the Information that we need
364 wxTreeItemId itemId = GetSelection();
365 DBTreeData *item = (DBTreeData *)GetItemData(itemId);
366 if ( item != NULL )
367 {
368 Temp0.Printf(_("Here is where all wxTable Classes in >%s< will be made! "),s_DSN.c_str());
369 wxMessageBox(Temp0);
370 }
371 }
372 //------------------------------------------------------------------------------
373 void DBTree::OnMouseMove(wxMouseEvent &event)
374 {
375 TreePos = event.GetPosition();
376 }
377 //------------------------------------------------------------------------------
378
379