]>
git.saurik.com Git - wxWidgets.git/blob - demos/dbbrowse/dbtree.cpp
1 //---------------------------------------------------------------------------
3 // Purpose: Programm Control with a Tree
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 DBTree::DBTree(wxWindow
*parent
, const wxWindowID id
,const wxPoint
& pos
, const wxSize
& size
, long style
)
61 : wxTreeCtrl(parent
, id
, pos
, size
, style
)
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
));
67 // Make an image list containing small icons
68 p_imageListNormal
= new wxImageList(16, 16, TRUE
);
70 // should correspond to TreeIc_xxx enum
72 #if !defined(__WXMSW__)
73 #include "bitmaps/logo.xpm"
74 #include "bitmaps/dsnclose.xpm"
75 #include "bitmaps/dsnopen.xpm"
76 #include "bitmaps/tab.xpm"
77 #include "bitmaps/key.xpm"
78 #include "bitmaps/keyf.xpm"
79 #include "bitmaps/d_open.xpm"
80 #include "bitmaps/d_closed.xpm"
81 #include "bitmaps/col.xpm"
84 p_imageListNormal
->Add(wxICON(Logo
));
85 p_imageListNormal
->Add(wxICON(DsnClosed
));
86 p_imageListNormal
->Add(wxICON(DsnOpen
));
87 p_imageListNormal
->Add(wxICON(TAB
));
88 p_imageListNormal
->Add(wxICON(COL
));
89 p_imageListNormal
->Add(wxICON(KEY
));
90 p_imageListNormal
->Add(wxICON(KEYF
));
91 p_imageListNormal
->Add(wxICON(DocOpen
));
92 p_imageListNormal
->Add(wxICON(DocOpen
));
98 SetImageList(p_imageListNormal
);
101 //---------------------------------------------------------------------------
104 // wxLogMessage("DBTree::~DBTree() - Vor OnCloseDB()");
105 (pDoc
->db_Br
+i_Which
)->OnCloseDB(FALSE
);
106 // wxLogMessage("DBTree::~DBTree() - Nach OnCloseDB()");
107 (pDoc
->db_Br
+i_Which
)->db_BrowserDB
= NULL
;
108 (pDoc
->db_Br
+i_Which
)->ct_BrowserDB
= NULL
;
109 (pDoc
->db_Br
+i_Which
)->cl_BrowserDB
= NULL
;
111 delete p_imageListNormal
;
113 //---------------------------------------------------------------------------
114 #undef TREE_EVENT_HANDLER
115 //---------------------------------------------------------------------------
116 int DBTree::OnPopulate()
118 wxTreeItemId Root
, Folder
, Docu
, Funkt
;
120 wxString SQL_TYPE
, DB_TYPE
;
122 //----------------------------------------------------------------------------------------------------------------------------
123 if((pDoc
->db_Br
+i_Which
)->Initialize(FALSE
))
126 ct_BrowserDB
= (pDoc
->db_Br
+i_Which
)->OnGetCatalog(FALSE
);
128 { // Use the wxDatabase Information
129 Temp0
.Printf("%s - (%s) (%s)", s_DSN
.c_str(),ct_BrowserDB
->catalog
, ct_BrowserDB
->schema
);
130 Root
= AddRoot(Temp0
,TreeIc_DsnOpen
,TreeIc_DsnOpen
,new DBTreeData("Root"));
131 for (x
=0;x
<ct_BrowserDB
->numTables
;x
++)
134 if (!wxStrcmp((ct_BrowserDB
->pTableInf
+x
)->tableType
,"TABLE")) // only TABLES
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 Temp1
.Printf("TN(%s",(ct_BrowserDB
->pTableInf
+x
)->tableName
);
141 (ct_BrowserDB
->pTableInf
+x
)->pColInf
= (pDoc
->db_Br
+i_Which
)->OnGetColumns((ct_BrowserDB
->pTableInf
+x
)->tableName
,(ct_BrowserDB
->pTableInf
+x
)->numCols
,FALSE
);
143 if ((ct_BrowserDB
->pTableInf
+x
)->pColInf
)
145 Temp0
.Printf(_("Tablename(%s) with (%d)Columns ; Remarks(%s)"), (ct_BrowserDB
->pTableInf
+x
)->tableName
,
146 (ct_BrowserDB
->pTableInf
+x
)->numCols
,
147 (ct_BrowserDB
->pTableInf
+x
)->tableRemarks
);
148 Folder
= AppendItem(Root
,Temp0
,TreeIc_TAB
,TreeIc_TAB
, new DBTreeData(Temp1
));
149 for (y
=0;y
<(ct_BrowserDB
->pTableInf
+x
)->numCols
;y
++)
151 Temp1
.Printf("FN(%s",((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->colName
);
152 // Here is where we find out if the Column is a Primary / Foreign Key
153 if (((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->PkCol
!= 0) // Primary Key
155 Docu
= AppendItem(Folder
,((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->colName
,TreeIc_KEY
,TreeIc_KEY
,new DBTreeData(Temp1
));
156 Temp2
.Printf(_("This Key is used in the following Tables : %s"),((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->PkTableName
);
157 Funkt
= AppendItem(Docu
,Temp2
,TreeIc_DocClosed
,TreeIc_DocOpen
,new DBTreeData("KEY"));
160 if (((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->FkCol
!= 0) // Foreign Key
162 Docu
= AppendItem(Folder
,((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->colName
,TreeIc_KEYF
,TreeIc_KEYF
,new DBTreeData(Temp1
));
163 Temp2
.Printf(_("This Foreign Key comes from the following Table : %s"),((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->FkTableName
);
164 Funkt
= AppendItem(Docu
,Temp2
,TreeIc_DocClosed
,TreeIc_DocOpen
,new DBTreeData("KEYF"));
167 Docu
= AppendItem(Folder
,((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->colName
,TreeIc_COL
,TreeIc_COL
,new DBTreeData(Temp1
));
168 SQL_TYPE
.Printf(_("SQL_C_???? (%d)"),((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->sqlDataType
);
169 DB_TYPE
.Printf(_("DB_DATA_TYPE_???? (%d)"),((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->dbDataType
);
170 for (i
=1;i
<=(pDoc
->db_Br
+i_Which
)->i_SqlTyp
[0];i
++)
172 if (((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->sqlDataType
== (pDoc
->db_Br
+i_Which
)->i_SqlTyp
[i
])
174 SQL_TYPE
.Printf("%s(%d) ; ",(pDoc
->db_Br
+i_Which
)->s_SqlTyp
[i
].c_str(),(pDoc
->db_Br
+i_Which
)->i_SqlTyp
[i
]);
176 } // for (i=1;i<=i_SqlTyp[0];i++)
177 for (i
=1;i
<=(pDoc
->db_Br
+i_Which
)->i_dbTyp
[0];i
++)
179 if (((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->dbDataType
== (pDoc
->db_Br
+i_Which
)->i_dbTyp
[i
])
181 DB_TYPE
.Printf("%s(%d)",(pDoc
->db_Br
+i_Which
)->s_dbTyp
[i
].c_str(),(pDoc
->db_Br
+i_Which
)->i_dbTyp
[i
]);
183 } // for (i=1;i<=i_dbTyp[0];i++)
185 Funkt
= AppendItem(Docu
,SQL_TYPE
,TreeIc_DocClosed
,TreeIc_DocOpen
,new DBTreeData(SQL_TYPE
));
186 SQL_TYPE
.Printf("%10s %d,%d",((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->typeName
,
187 ((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->columnSize
,
188 ((ct_BrowserDB
->pTableInf
+x
)->pColInf
+y
)->decimalDigits
);
189 Funkt
= AppendItem(Docu
,SQL_TYPE
,TreeIc_DocClosed
,TreeIc_DocOpen
,new DBTreeData(SQL_TYPE
));
190 } // for (y=0;y<(ct_BrowserDB->pTableInf+x)->numCols;y++)
191 } // if ((ct_BrowserDB->pTableInf+x)->pColInf)
193 Folder
= AppendItem(Root
,Temp0
,TreeIc_FolderClosed
,TreeIc_FolderOpen
, new DBTreeData(Temp1
));
194 } // if ((ct_BrowserDB->pTableInf+x)->tableType == "TABLE")
195 } // for (x=0;x<ct_BrowserDB->numTables;x++)
196 } // if (ct_BrowserDB)
198 wxLogMessage(_("\n-E-> DBTree::OnPopulate() : Invalid Catalog Pointer : Failed"));
200 } // if((pDoc->db_Br+i_Which)->Initialize(FALSE))
204 //wxLogMessage(_("\n-E-> DBTree::OnPopulate() : A valid Pointer could not be created : Failed"));
206 //----------------------------------------------------------------------------------------------------------------------------
208 //----------------------------------------------------------------------------------------------------------------------------
210 popupMenu1
= new wxMenu("");
211 popupMenu1
->Append(DATA_DB
, _("Make wxDB.cpp/h "));
212 popupMenu1
->AppendSeparator();
213 popupMenu1
->Append(DATA_TABLE_ALL
, _("Make all wxTable.cpp/h classes"));
215 popupMenu2
= new wxMenu("");
216 popupMenu2
->Append(DATA_SHOW
, _("Show Data"));
217 popupMenu2
->AppendSeparator();
218 popupMenu2
->Append(DATA_TABLE
, _("Make wxTable.cpp/h "));
219 //----------------------------------------------------------------------------------------------------------------------------
223 //---------------------------------------------------------------------------
224 void DBTree::OnSelChanged(wxTreeEvent
& WXUNUSED(event
))
228 pDoc
->p_MainFrame
->SetStatusText(Temp0
,0);
229 // Get the Information that we need
230 wxTreeItemId itemId
= GetSelection();
231 DBTreeData
*item
= (DBTreeData
*)GetItemData(itemId
);
235 Temp1
.Printf("%s",item
->m_desc
.c_str());
236 //-----------------------------------------------------------------------------------------
237 if (Temp1
.Contains("ODBC-"))
239 Temp1
= Temp1
.Mid(5,wxSTRING_MAXLEN
);
240 for (i
=0;i
<pDoc
->i_DSN
;i
++)
242 if (Temp1
== (pDoc
->p_DSN
+i
)->Dsn
)
244 // pDoc->OnChosenDSN(i);
249 //-----------------------------------------------------------------------------------------
252 //---------------------------------------------------
254 Temp0.Printf(_("Item '%s': %sselected, %sexpanded, %sbold,"
255 "%u children (%u immediately under this item)."),
256 item->m_desc.c_str(),
257 bool2String(IsSelected(itemId)),
258 bool2String(IsExpanded(itemId)),
259 bool2String(IsBold(itemId)),
260 GetChildrenCount(itemId),
261 GetChildrenCount(itemId));
262 LogBuf.Printf("-I-> DBTree::OnSelChanged - %s",Temp0.c_str());
263 wxLogMessage( "%s", LogBuf.c_str() );
265 //---------------------------------------------------
269 //---------------------------------------------------------------------------
270 void DBTree::OnRightSelect(wxTreeEvent
& WXUNUSED(event
))
274 // Get the Information that we need
275 wxTreeItemId itemId
= GetSelection();
276 DBTreeData
*item
= (DBTreeData
*)GetItemData(itemId
);
280 Temp1
.Printf("%s",item
->m_desc
.c_str());
281 //-----------------------------------------------------------------------------------------
282 if (!wxStrcmp("Root",Temp1
))
284 PopupMenu(popupMenu1
,TreePos
.x
,TreePos
.y
);
287 for (i
=0;i
<ct_BrowserDB
->numTables
;i
++)
289 Temp2
.Printf("TN(%s",(ct_BrowserDB
->pTableInf
+i
)->tableName
);
290 if (!wxStrcmp(Temp2
,Temp1
))
292 PopupMenu(popupMenu2
,TreePos
.x
,TreePos
.y
);
296 //-----------------------------------------------------------------------------------------
299 //---------------------------------------------------
301 Temp0.Printf(_("Item '%s': %sselected, %sexpanded, %sbold,"
302 "%u children (%u immediately under this item)."),
303 item->m_desc.c_str(),
304 bool2String(IsSelected(itemId)),
305 bool2String(IsExpanded(itemId)),
306 bool2String(IsBold(itemId)),
307 GetChildrenCount(itemId),
308 GetChildrenCount(itemId));
309 LogBuf.Printf("-I-> DBTree::OnSelChanged - %s",Temp0.c_str());
310 wxLogMessage( "%s", LogBuf.c_str() );
312 //---------------------------------------------------
316 //---------------------------------------------------------------------------
317 void DBTree::OnDBGrid(wxMenu
& , wxCommandEvent
& event
)
320 // Get the Information that we need
321 wxTreeItemId itemId
= GetSelection();
322 DBTreeData
*item
= (DBTreeData
*)GetItemData(itemId
);
325 Temp1
.Printf("%s",item
->m_desc
.c_str());
326 for (i
=0;i
<ct_BrowserDB
->numTables
;i
++)
328 Temp2
.Printf("TN(%s",(ct_BrowserDB
->pTableInf
+i
)->tableName
);
329 if (!wxStrcmp(Temp2
,Temp1
))
331 // Temp0.Printf("(%d) Here is where a GridCtrl for >%s< will be called! ",i,(ct_BrowserDB->pTableInf+i)->tableName);
332 pDoc
->OnChosenTbl(1,(ct_BrowserDB
->pTableInf
+i
)->tableName
);
333 // wxMessageBox(Temp0);
338 //---------------------------------------------------------------------------
339 void DBTree::OnDBClass(wxMenu
& , wxCommandEvent
& event
)
342 // Get the Information that we need
343 wxTreeItemId itemId
= GetSelection();
344 DBTreeData
*item
= (DBTreeData
*)GetItemData(itemId
);
347 Temp0
.Printf(_("Here is where a wxDB Class for >%s< will be made! "),s_DSN
.c_str());
351 //---------------------------------------------------------------------------
352 void DBTree::OnTableClass(wxMenu
& , wxCommandEvent
& event
)
355 // Get the Information that we need
356 wxTreeItemId itemId
= GetSelection();
357 DBTreeData
*item
= (DBTreeData
*)GetItemData(itemId
);
360 Temp1
.Printf("%s",item
->m_desc
.c_str());
361 for (i
=0;i
<ct_BrowserDB
->numTables
;i
++)
363 Temp2
.Printf("TN(%s",(ct_BrowserDB
->pTableInf
+i
)->tableName
);
364 if (!wxStrcmp(Temp2
,Temp1
))
366 Temp0
.Printf(_("(%d) Here is where a wxTable Class for >%s< will be made! "),i
,(ct_BrowserDB
->pTableInf
+i
)->tableName
);
372 //---------------------------------------------------------------------------
373 void DBTree::OnTableClassAll(wxMenu
& , wxCommandEvent
& event
)
376 // Get the Information that we need
377 wxTreeItemId itemId
= GetSelection();
378 DBTreeData
*item
= (DBTreeData
*)GetItemData(itemId
);
381 Temp0
.Printf(_("Here is where all wxTable Classes in >%s< will be made! "),s_DSN
.c_str());
385 //------------------------------------------------------------------------------
386 void DBTree::OnMouseMove(wxMouseEvent
&event
)
388 TreePos
= event
.GetPosition();
390 //------------------------------------------------------------------------------