]>
git.saurik.com Git - wxWidgets.git/blob - demos/dbbrowse/dbtree.cpp
1 //----------------------------------------------------------------------------------------
3 // Purpose: Tree with Table and Views, branches show Field information
4 // Author: Mark Johnson
8 // Copyright: (c) Mark Johnson, Berlin Germany, mj10777@gmx.net
9 // Licence: wxWindows license
10 //----------------------------------------------------------------------------------------
11 //-- all #ifdefs that the whole Project needs. -------------------------------------------
12 //----------------------------------------------------------------------------------------
14 #pragma implementation
17 //----------------------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
20 //----------------------------------------------------------------------------------------
24 //----------------------------------------------------------------------------------------
28 //----------------------------------------------------------------------------------------
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
)
40 return b
? "" : "not ";
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
)
52 //----------------------------------------------------------------------------------------
53 // DBTree implementation
54 //----------------------------------------------------------------------------------------
55 IMPLEMENT_DYNAMIC_CLASS(DBTree
, wxTreeCtrl
)
56 //----------------------------------------------------------------------------------------
57 DBTree::DBTree(wxWindow
*parent
) : wxTreeCtrl(parent
)
60 //----------------------------------------------------------------------------------------
61 DBTree::DBTree(wxWindow
*parent
, const wxWindowID id
,const wxPoint
& pos
, const wxSize
& size
, long style
)
62 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
64 // Make an image list containing small icons
65 p_imageListNormal
= new wxImageList(16, 16, TRUE
);
66 // should correspond to TreeIc_xxx enum
67 #if !defined(__WXMSW__)
68 #include "bitmaps/logo.xpm"
69 #include "bitmaps/dsnclose.xpm"
70 #include "bitmaps/dsnopen.xpm"
71 #include "bitmaps/tab.xpm"
72 #include "bitmaps/view.xpm"
73 #include "bitmaps/col.xpm"
74 #include "bitmaps/key.xpm"
75 #include "bitmaps/keyf.xpm"
76 #include "bitmaps/d_open.xpm"
77 #include "bitmaps/d_closed.xpm"
79 p_imageListNormal
->Add(wxICON(aLogo
));
80 p_imageListNormal
->Add(wxICON(DsnClosed
));
81 p_imageListNormal
->Add(wxICON(DsnOpen
));
82 p_imageListNormal
->Add(wxICON(TAB
));
83 p_imageListNormal
->Add(wxICON(VIEW
));
84 p_imageListNormal
->Add(wxICON(COL
));
85 p_imageListNormal
->Add(wxICON(KEY
));
86 p_imageListNormal
->Add(wxICON(KEYF
));
87 p_imageListNormal
->Add(wxICON(DocOpen
));
88 p_imageListNormal
->Add(wxICON(DocOpen
));
89 SetImageList(p_imageListNormal
);
92 //----------------------------------------------------------------------------------------
95 // wxLogMessage("DBTree::~DBTree() - Vor OnCloseDB()");
96 (pDoc
->db_Br
+i_Which
)->OnCloseDB(FALSE
);
97 // wxLogMessage("DBTree::~DBTree() - Nach OnCloseDB()");
98 (pDoc
->db_Br
+i_Which
)->db_BrowserDB
= NULL
;
99 (pDoc
->db_Br
+i_Which
)->ct_BrowserDB
= NULL
;
100 (pDoc
->db_Br
+i_Which
)->cl_BrowserDB
= NULL
;
102 delete p_imageListNormal
;
104 //----------------------------------------------------------------------------------------
105 #undef TREE_EVENT_HANDLER
106 //----------------------------------------------------------------------------------------
107 int DBTree::OnPopulate()
109 wxTreeItemId Root
, Folder
, Docu
, Funkt
;
110 int i
,x
,y
, TableType
;
111 wxString SQL_TYPE
, DB_TYPE
;
112 SetFont(* pDoc
->ft_Doc
);
113 //---------------------------------------------------------------------------------------
114 if ((pDoc
->db_Br
+i_Which
)->Initialize(FALSE
))
117 ct_BrowserDB
= (pDoc
->db_Br
+i_Which
)->OnGetCatalog(FALSE
);
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
++)
125 TableType
= 0; // TABLE = 1 ; VIEW = 2 ; 0 We are not interested in
126 if (!wxStrcmp((ct_BrowserDB
->pTableInf
+x
)->tableType
,"TABLE")) // only TABLES
128 if (!wxStrcmp((ct_BrowserDB
->pTableInf
+x
)->tableType
,"VIEW")) // and VIEWS
130 if (TableType
) // only TABLES or Views
132 Temp1
.Printf("TN(%s",(ct_BrowserDB
->pTableInf
+x
)->tableName
);
134 (ct_BrowserDB
->pTableInf
+x
)->pColInf
= (pDoc
->db_Br
+i_Which
)->OnGetColumns((ct_BrowserDB
->pTableInf
+x
)->tableName
,(ct_BrowserDB
->pTableInf
+x
)->numCols
,FALSE
);
136 if ((ct_BrowserDB
->pTableInf
+x
)->pColInf
)
138 if (TableType
== 1) // Table
140 Temp0
.Printf(_("Table-Name(%s) with (%d)Columns ; Remarks(%s)"), (ct_BrowserDB
->pTableInf
+x
)->tableName
,
141 (ct_BrowserDB
->pTableInf
+x
)->numCols
,(ct_BrowserDB
->pTableInf
+x
)->tableRemarks
);
142 Folder
= AppendItem(Root
,Temp0
,TreeIc_TAB
,TreeIc_TAB
, new DBTreeData(Temp1
));
144 if (TableType
== 2) // View
146 Temp0
.Printf(_("View-Name(%s) with (%d)Columns ; Remarks(%s)"), (ct_BrowserDB
->pTableInf
+x
)->tableName
,
147 (ct_BrowserDB
->pTableInf
+x
)->numCols
,(ct_BrowserDB
->pTableInf
+x
)->tableRemarks
);
148 Folder
= AppendItem(Root
,Temp0
,TreeIc_VIEW
,TreeIc_VIEW
, new DBTreeData(Temp1
));
150 for (y
=0;y
<(ct_BrowserDB
->pTableInf
+x
)->numCols
;y
++)
152 Temp1
.Printf("FN(%s",((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->colName
);
153 // Here is where we find out if the Column is a Primary / Foreign Key
154 if (((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->PkCol
!= 0) // Primary Key
156 Docu
= AppendItem(Folder
,((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->colName
,TreeIc_KEY
,TreeIc_KEY
,new DBTreeData(Temp1
));
157 Temp2
.Printf(_("This Key is used in the following Tables : %s"),((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->PkTableName
);
158 Funkt
= AppendItem(Docu
,Temp2
,TreeIc_DocClosed
,TreeIc_DocOpen
,new DBTreeData("KEY"));
162 if (((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->FkCol
!= 0) // Foreign Key
164 Docu
= AppendItem(Folder
,((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->colName
,TreeIc_KEYF
,TreeIc_KEYF
,new DBTreeData(Temp1
));
165 Temp2
.Printf(_("This Foreign Key comes from the following Table : %s"),((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->FkTableName
);
166 Funkt
= AppendItem(Docu
,Temp2
,TreeIc_DocClosed
,TreeIc_DocOpen
,new DBTreeData("KEYF"));
169 Docu
= AppendItem(Folder
,((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->colName
,TreeIc_COL
,TreeIc_COL
,new DBTreeData(Temp1
));
171 SQL_TYPE
.Printf(_("SQL_C_???? (%d)"),((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->sqlDataType
);
172 DB_TYPE
.Printf(_("DB_DATA_TYPE_???? (%d)"),((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->dbDataType
);
173 for (i
=1;i
<=(pDoc
->db_Br
+i_Which
)->i_SqlTyp
[0];i
++)
175 if (((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->sqlDataType
== (pDoc
->db_Br
+i_Which
)->i_SqlTyp
[i
])
177 SQL_TYPE
.Printf("%s(%d) ; ",(pDoc
->db_Br
+i_Which
)->s_SqlTyp
[i
].c_str(),(pDoc
->db_Br
+i_Which
)->i_SqlTyp
[i
]);
179 } // for (i=1;i<=i_SqlTyp[0];i++)
180 for (i
=1;i
<=(pDoc
->db_Br
+i_Which
)->i_dbTyp
[0];i
++)
182 if (((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->dbDataType
== (pDoc
->db_Br
+i_Which
)->i_dbTyp
[i
])
184 DB_TYPE
.Printf("%s(%d)",(pDoc
->db_Br
+i_Which
)->s_dbTyp
[i
].c_str(),(pDoc
->db_Br
+i_Which
)->i_dbTyp
[i
]);
186 } // for (i=1;i<=i_dbTyp[0];i++)
188 Funkt
= AppendItem(Docu
,SQL_TYPE
,TreeIc_DocClosed
,TreeIc_DocOpen
,new DBTreeData(SQL_TYPE
));
189 SQL_TYPE
.Printf("%10s %d,%d",((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->typeName
,
190 ((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->columnSize
,((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->decimalDigits
);
191 Funkt
= AppendItem(Docu
,SQL_TYPE
,TreeIc_DocClosed
,TreeIc_DocOpen
,new DBTreeData(SQL_TYPE
));
192 } // for (y=0;y<(ct_BrowserDB->pTableInf+x)->numCols;y++)
193 } // if ((ct_BrowserDB->pTableInf+x)->pColInf)
195 Folder
= AppendItem(Root
,Temp0
,TreeIc_FolderClosed
,TreeIc_FolderOpen
, new DBTreeData(Temp1
));
196 } // if ((ct_BrowserDB->pTableInf+x)->tableType == "TABLE" or VIEW)
198 // wxLogMessage(_("\n-I-> if ! TABLE or VIEW >%s<"),(ct_BrowserDB->pTableInf+x)->tableType);
199 } // for (x=0;x<ct_BrowserDB->numTables;x++)
200 } // if (ct_BrowserDB)
202 wxLogMessage(_("\n-E-> DBTree::OnPopulate() : Invalid Catalog Pointer : Failed"));
204 } // if((pDoc->db_Br+i_Which)->Initialize(FALSE))
207 wxLogMessage(_("\n-E-> DBTree::OnPopulate() : A valid Pointer could not be created : Failed"));
210 //---------------------------------------------------------------------------------------
212 //---------------------------------------------------------------------------------------
214 popupMenu1
= new wxMenu("");
215 popupMenu1
->Append(DATA_DB
, _("Make wxDB.cpp/h "));
216 popupMenu1
->AppendSeparator();
217 popupMenu1
->Append(DATA_TABLE_ALL
, _("Make all wxTable.cpp/h classes"));
219 popupMenu2
= new wxMenu("");
220 popupMenu2
->Append(DATA_SHOW
, _("Show Data"));
221 popupMenu2
->AppendSeparator();
222 popupMenu2
->Append(DATA_TABLE
, _("Make wxTable.cpp/h "));
223 //---------------------------------------------------------------------------------------
226 //----------------------------------------------------------------------------------------
227 void DBTree::OnSelChanged(wxTreeEvent
& WXUNUSED(event
))
231 pDoc
->p_MainFrame
->SetStatusText(Temp0
,0);
232 // Get the Information that we need
233 wxTreeItemId itemId
= GetSelection();
234 DBTreeData
*item
= (DBTreeData
*)GetItemData(itemId
);
238 Temp1
.Printf("%s",item
->m_desc
.c_str());
239 //-------------------------------------------------------------------------------------
240 if (Temp1
.Contains("ODBC-"))
242 Temp1
= Temp1
.Mid(5,wxSTRING_MAXLEN
);
243 for (i
=0;i
<pDoc
->i_DSN
;i
++)
245 if (Temp1
== (pDoc
->p_DSN
+i
)->Dsn
)
247 // pDoc->OnChosenDSN(i);
252 //--------------------------------------------------------------------------------------
255 //-------------------------------------------------------------------------------------
257 Temp0.Printf(_("Item '%s': %sselected, %sexpanded, %sbold,"
258 "%u children (%u immediately under this item)."),
259 item->m_desc.c_str(),
260 bool2String(IsSelected(itemId)),
261 bool2String(IsExpanded(itemId)),
262 bool2String(IsBold(itemId)),
263 GetChildrenCount(itemId),
264 GetChildrenCount(itemId));
265 LogBuf.Printf("-I-> DBTree::OnSelChanged - %s",Temp0.c_str());
266 wxLogMessage( "%s", LogBuf.c_str() );
268 //-------------------------------------------------------------------------------------
272 //----------------------------------------------------------------------------------------
273 void DBTree::OnRightSelect(wxTreeEvent
& WXUNUSED(event
))
277 // Get the Information that we need
278 wxTreeItemId itemId
= GetSelection();
279 DBTreeData
*item
= (DBTreeData
*)GetItemData(itemId
);
283 Temp1
.Printf("%s",item
->m_desc
.c_str());
284 //--------------------------------------------------------------------------------------
285 if (!wxStrcmp("Root",Temp1
))
287 PopupMenu(popupMenu1
,TreePos
.x
,TreePos
.y
);
290 for (i
=0;i
<ct_BrowserDB
->numTables
;i
++)
292 Temp2
.Printf("TN(%s",(ct_BrowserDB
->pTableInf
+i
)->tableName
);
293 if (!wxStrcmp(Temp2
,Temp1
))
295 PopupMenu(popupMenu2
,TreePos
.x
,TreePos
.y
);
299 //--------------------------------------------------------------------------------------
302 //-------------------------------------------------------------------------------------
304 Temp0.Printf(_("Item '%s': %sselected, %sexpanded, %sbold,"
305 "%u children (%u immediately under this item)."),
306 item->m_desc.c_str(),
307 bool2String(IsSelected(itemId)),
308 bool2String(IsExpanded(itemId)),
309 bool2String(IsBold(itemId)),
310 GetChildrenCount(itemId),
311 GetChildrenCount(itemId));
312 LogBuf.Printf("-I-> DBTree::OnSelChanged - %s",Temp0.c_str());
313 wxLogMessage( "%s", LogBuf.c_str() );
315 //-------------------------------------------------------------------------------------
319 //----------------------------------------------------------------------------------------
320 void DBTree::OnDBGrid(wxMenu
& , wxCommandEvent
& event
)
323 // Get the Information that we need
324 wxTreeItemId itemId
= GetSelection();
325 DBTreeData
*item
= (DBTreeData
*)GetItemData(itemId
);
328 Temp1
.Printf("%s",item
->m_desc
.c_str());
329 for (i
=0;i
<ct_BrowserDB
->numTables
;i
++)
331 Temp2
.Printf("TN(%s",(ct_BrowserDB
->pTableInf
+i
)->tableName
);
332 if (!wxStrcmp(Temp2
,Temp1
))
334 // Temp0.Printf("(%d) Here is where a GridCtrl for >%s< will be called! ",i,(ct_BrowserDB->pTableInf+i)->tableName);
335 pDoc
->OnChosenTbl(1,(ct_BrowserDB
->pTableInf
+i
)->tableName
);
336 // wxMessageBox(Temp0);
341 //----------------------------------------------------------------------------------------
342 void DBTree::OnDBClass(wxMenu
& , wxCommandEvent
& event
)
345 // Get the Information that we need
346 wxTreeItemId itemId
= GetSelection();
347 DBTreeData
*item
= (DBTreeData
*)GetItemData(itemId
);
350 Temp0
.Printf(_("Here is where a wxDB Class for >%s< will be made! "),s_DSN
.c_str());
354 //----------------------------------------------------------------------------------------
355 void DBTree::OnTableClass(wxMenu
& , wxCommandEvent
& event
)
358 // Get the Information that we need
359 wxTreeItemId itemId
= GetSelection();
360 DBTreeData
*item
= (DBTreeData
*)GetItemData(itemId
);
363 Temp1
.Printf("%s",item
->m_desc
.c_str());
364 for (i
=0;i
<ct_BrowserDB
->numTables
;i
++)
366 Temp2
.Printf("TN(%s",(ct_BrowserDB
->pTableInf
+i
)->tableName
);
367 if (!wxStrcmp(Temp2
,Temp1
))
369 Temp0
.Printf(_("(%d) Here is where a wxTable Class for >%s< will be made! "),i
,(ct_BrowserDB
->pTableInf
+i
)->tableName
);
375 //----------------------------------------------------------------------------------------
376 void DBTree::OnTableClassAll(wxMenu
& , wxCommandEvent
& event
)
379 // Get the Information that we need
380 wxTreeItemId itemId
= GetSelection();
381 DBTreeData
*item
= (DBTreeData
*)GetItemData(itemId
);
384 Temp0
.Printf(_("Here is where all wxTable Classes in >%s< will be made! "),s_DSN
.c_str());
388 //----------------------------------------------------------------------------------------
389 void DBTree::OnMouseMove(wxMouseEvent
&event
)
391 TreePos
= event
.GetPosition();
393 //----------------------------------------------------------------------------------------