]> git.saurik.com Git - wxWidgets.git/blob - demos/dbbrowse/doc.cpp
5a3c0cdb2ae18fd35f4187127d340f245b938b53
[wxWidgets.git] / demos / dbbrowse / doc.cpp
1 //---------------------------------------------------------------------------
2 // Name: Doc.cpp
3 // Purpose: Holds information for DBBrowser
4 // Author: Mark Johnson, mj10777@gmx.net
5 // Modified by: 19990808.mj10777
6 // BJO : Bart A.M. JOURQUIN
7 // Created: 19990808
8 // Copyright: (c) Mark Johnson
9 // Licence: wxWindows license
10 // RCS-ID: $Id$
11 //---------------------------------------------------------------------------
12 //-- all #ifdefs that the whole Project needs. ------------------------------
13 //---------------------------------------------------------------------------
14 #ifdef __GNUG__
15 #pragma implementation
16 #pragma interface
17 #endif
18 //---------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21 //---------------------------------------------------------------------------
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25 //---------------------------------------------------------------------------
26 #ifndef WX_PRECOMP
27 #include "wx/wx.h"
28 #endif
29 //---------------------------------------------------------------------------
30 //---------------------------------------------------------------------------
31 //-- all #includes that every .cpp needs ----19990807.mj10777 ---
32 //---------------------------------------------------------------------------
33 #include "std.h" // sorgsam Pflegen !
34 //---------------------------------------------------------------------------
35 //-- Some Global Vars for all Files (extern in ?.h needed) -----------------
36 // Global structure for holding ODBC connection information
37 struct DbStuff DbConnectInf;
38 //---------------------------------------------------------------------------
39 wxConfigBase *p_ProgramCfg; // All Config and Path information
40 wxLogTextCtrl *p_LogBook; // All Log messages
41 wxString LogBuf; // String for all Logs
42 //---------------------------------------------------------------------------
43 mjDoc::mjDoc()
44 {
45 db_Br = NULL;
46 p_DSN = NULL;
47 i_DSN = 0;
48 p_Splitter = NULL;
49 p_MainFrame = NULL;
50 p_PgmCtrl = NULL; // Is not active
51 p_DBTree = NULL;
52 p_DBGrid = NULL;
53 p_LogWin = NULL;
54 p_TabArea = NULL;
55 p_PageArea = NULL;
56 i_TabNr = 0;
57 i_PageNr = 0;
58 }
59 //---------------------------------------------------------------------------
60 mjDoc::~mjDoc()
61 {
62 // ----------------------------------------------------------
63 // -E-> The Tree Controls take to long to close : Why ??
64 // ----------------------------------------------------------
65 // wxMessageBox("-I-> end Doc");
66 p_TabArea->Show(FALSE); // Deactivate the Window
67 p_PageArea->Show(FALSE); // Deactivate the Window
68 p_PgmCtrl = NULL;
69 delete p_PgmCtrl;
70 delete [] p_DSN;
71 delete p_DBTree;
72 p_TabArea = NULL; delete p_TabArea;
73 p_PageArea = NULL; delete p_PageArea;
74 p_Splitter = NULL;
75 delete p_Splitter;
76 delete [] db_Br;
77 // wxMessageBox("~mjDoc");
78 }
79 //---------------------------------------------------------------------------
80 bool mjDoc::OnNewDocument()
81 {
82 //-------------------------------------------------------------------
83 if (!OnInitView())
84 {
85 return FALSE;
86 }
87 p_PgmCtrl->OnPopulate();
88 //-------------------------------------------------------------------
89 return TRUE;
90 }
91 //---------------------------------------------------------------------------
92 bool mjDoc::OnInitView()
93 {
94 Sash = p_ProgramCfg->Read("/MainFrame/Sash", 200);
95 // wxMessageBox("OnInitView() - Begin ","-I->mjDoc::OnInitView");
96 //--------------------------------------------------------------------------
97 // create "workplace" window
98 //--------------------------------------------------------------------------
99 p_TabArea = new wxTabbedWindow(); // Init the Pointer
100 p_TabArea->Create(p_Splitter, -1);
101 //--------------------------------------------------------------------------
102 p_PgmCtrl = new PgmCtrl(p_TabArea, TREE_CTRL_PGM,wxDefaultPosition, wxDefaultSize,
103 wxTR_HAS_BUTTONS | wxSUNKEN_BORDER);
104 p_PgmCtrl->i_TabArt = 0; // 0 = Tab ; 1 = Page
105 p_PgmCtrl->i_ViewNr = p_TabArea->GetTabCount()-1;
106 //--------------------------------------------------------------------------
107 wxBitmap *p_FolderClose = new wxBitmap("PgmCtrl"); //, wxBITMAP_TYPE_BMP_RESOURCE); // BJO20000115
108 //--------------------------------------------------------------------------
109 p_TabArea->AddTab(p_PgmCtrl,"PgmCtrl",p_FolderClose);
110 //--------------------------------------------------------------------------
111 // now create "output" window
112 //--------------------------------------------------------------------------
113 p_PageArea = new wxPaggedWindow(); // Init the Pointer
114 p_PageArea->Create(p_Splitter, -1);
115 //--------------------------------------------------------------------------
116 p_LogWin = new wxTextCtrl(p_PageArea,-1,wxEmptyString,
117 wxDefaultPosition, wxDefaultSize,wxTE_MULTILINE );
118 //wxFont* ft_Temp = new wxFont(10,wxSWISS,wxNORMAL,wxBOLD,FALSE,"Comic Sans MS");
119 //p_LogWin->SetFont(* ft_Temp);
120 // Don't forget ! This is always : i_TabArt = 0 ; i_ViewNr = 1;
121 //------------------------------------------------------------------
122 p_LogBook = new wxLogTextCtrl(p_LogWin); // make p_LogWin the LogBook
123 p_LogBook->SetActiveTarget(p_LogBook);
124 p_LogBook->SetTimestamp( NULL );
125 //------------------------------------------------------------------
126 p_PageArea->AddTab(p_LogWin,_("LogBook"), "what is this?" );
127 i_TabNr = p_TabArea->GetTabCount()-1; // Add one when a new AddTab is done;
128 i_PageNr = p_PageArea->GetTabCount()-1; // Add one when a new AddTab is done;
129 //--------------------------------------------------------------------------
130 p_PgmCtrl->pDoc = this;
131 p_TabArea->SetActiveTab(i_PageNr);
132 //--------------------------------------------------------------------------
133 p_Splitter->Initialize(p_TabArea);
134 p_Splitter->SplitHorizontally(p_TabArea,p_PageArea,Sash);
135 //--------------------------------------------------------------------------
136
137 if (!OnInitODBC())
138 return FALSE;
139 //--------------------------------------------------------------------------
140 Temp0.Printf("-I-> mjDoc::OnInitView() - End - %d DSN's found",i_DSN);
141 p_MainFrame->SetStatusText(Temp0, 0);
142 wxLogMessage(Temp0);
143 return TRUE;
144 }
145 //----------------------------------------------------------------------------------------
146 bool mjDoc::OnInitODBC()
147 {
148 char Dsn[SQL_MAX_DSN_LENGTH + 1];
149 char DsDesc[255]; // BJO20002501 instead of 512
150 Temp0 = "";
151 i_DSN = 0; // Counter
152 int i = 0;
153 //---------------------------------------------------------------------------
154 // Initialize the ODBC Environment for Database Operations
155
156 if (SQLAllocEnv(&DbConnectInf.Henv) != SQL_SUCCESS)
157 {
158 return FALSE;
159 }
160 //---------------------------------------------------------------------------
161
162 const char sep = 3; // separator character used in string between DSN ans DsDesc
163 wxStringList s_SortDSNList, s_SortDsDescList;
164 // BJO-20000127
165 // In order to have same sort result on both Dsn and DsDesc, create a 'keyed' string.
166 // The key will be removed after sorting
167 wxString KeyString;
168 //---------------------------------------------------------------------------
169 while(GetDataSource(DbConnectInf.Henv, Dsn, sizeof(Dsn), DsDesc, sizeof(DsDesc)))
170 {
171 i_DSN++; // How many Dsn have we ?
172 KeyString.Printf("%s%c%s",Dsn, sep, DsDesc);
173 s_SortDSNList.Add(Dsn);
174 s_SortDsDescList.Add(KeyString);
175 }
176 s_SortDSNList.Sort(); //BJO
177 s_SortDsDescList.Sort(); //BJO
178
179 char ** s_SortDSN = s_SortDSNList.ListToArray(); //BJO
180 char ** s_SortDsDesc = s_SortDsDescList.ListToArray(); //BJO
181 //---------------------------------------------------------------------------
182 // Allocate n ODBC-DSN objects to hold the information
183 p_DSN = new DSN[i_DSN]; //BJO
184 for (i=0;i<i_DSN;i++)
185 {
186 KeyString = s_SortDsDesc[i];
187 KeyString = KeyString.AfterFirst(sep);
188 strcpy(s_SortDsDesc[i],KeyString.c_str());
189 (p_DSN+i)->Dsn = s_SortDSN[i];
190 (p_DSN+i)->Drv = s_SortDsDesc[i];
191 (p_DSN+i)->Usr = "";
192 (p_DSN+i)->Pas = "";
193 Temp0.Printf("%02d) Dsn(%s) DsDesc(%s)",i,(p_DSN+i)->Dsn,(p_DSN+i)->Drv);
194 wxLogMessage(Temp0);
195 }
196 i = 0;
197 //---------------------------------------------------------------------------
198 // Allocate n wxDatabase objects to hold the column information
199 db_Br = new BrowserDB[i_DSN];
200 for (i=0;i<i_DSN;i++)
201 {
202 (db_Br+i)->p_LogWindow = p_LogWin;
203 (db_Br+i)->ODBCSource = (p_DSN+i)->Dsn;
204 (db_Br+i)->UserName = (p_DSN+i)->Usr;
205 (db_Br+i)->Password = (p_DSN+i)->Pas;
206 (db_Br+i)->pDoc = this;
207 (db_Br+i)->i_Which = i;
208 }
209
210 if (SQLFreeEnv(&DbConnectInf.Henv) != SQL_SUCCESS) // BJO20000125
211 {
212 // Error freeing environment handle
213 }
214
215 delete [] s_SortDSN;
216 delete [] s_SortDsDesc;
217
218 //---------------------------------------------------------------------------
219 if (!i_DSN)
220 {
221 wxMessageBox(_("No Dataset names found in ODBC!\n" \
222 " Program will exit!\n\n" \
223 " Ciao"),"-E-> Fatal situation");
224 return FALSE;
225 }
226 //---------------------------------------------------------------------------
227 return TRUE;
228 }
229 //----------------------------------------------------------------------------------------
230 bool mjDoc::OnChosenDSN(int Which)
231 {
232 // wxLogMessage("OnChosenDSN(%d) - Begin",Which);
233 //---------------------------------------------------------------------------
234 if (p_DBTree != NULL)
235 {
236 p_TabArea->Show(FALSE); // Deactivate the Window
237 p_TabArea->RemoveTab(p_DBTree->i_ViewNr);
238 p_TabArea->Show(TRUE); // Activate the Window
239 OnChosenTbl(77,"");
240 }
241 //-------------------------
242 p_TabArea->Show(FALSE); // Deactivate the Window
243 p_DBTree = new DBTree(p_TabArea, TREE_CTRL_DB,wxDefaultPosition, wxDefaultSize,
244 wxTR_HAS_BUTTONS | wxSUNKEN_BORDER);
245 p_TabArea->AddTab(p_DBTree,(p_DSN+Which)->Dsn," ? ");
246 p_DBTree->i_ViewNr = p_TabArea->GetTabCount()-1;
247 p_TabArea->Show(TRUE); // Deactivate the Window
248 p_DBTree->i_Which = Which;
249 p_DBTree->s_DSN = (p_DSN+Which)->Dsn;
250 p_DBTree->i_TabArt = 0;
251 p_DBTree->pDoc = this;
252 p_DBTree->OnPopulate();
253 p_TabArea->SetActiveTab(p_DBTree->i_ViewNr);
254 //---------------------------------------------------------------------------
255 // wxLogMessage("OnChosenDSN(%d) - End",Which);
256 return TRUE;
257 }
258 //----------------------------------------------------------------------------------------
259 bool mjDoc::OnChosenTbl(int Tab,wxString Table)
260 {
261 // wxLogMessage("OnChosenTbl(%d,%s)",Tab,Table.c_str());
262 //-------------------------
263 if (p_DBGrid != NULL)
264 {
265 if (p_DBGrid->i_TabArt == 0)
266 {
267 p_TabArea->Show(FALSE); // Deactivate the Window
268 p_TabArea->RemoveTab(p_DBGrid->i_ViewNr);
269 p_TabArea->Show(TRUE); // Activate the Window
270 }
271 if (p_DBGrid->i_TabArt == 1)
272 {
273 p_PageArea->Show(FALSE); // Deactivate the Window
274 p_PageArea->RemoveTab(p_DBGrid->i_ViewNr);
275 p_PageArea->Show(TRUE); // Activate the Window
276 }
277 p_DBGrid = NULL;
278 delete p_DBGrid;
279 }
280 if (Tab == 77) // Delete only
281 return TRUE;
282 //-------------------------
283 if (Tab == 0) // Tabview
284 {
285 p_TabArea->Show(FALSE); // Deactivate the Window
286 p_DBGrid = new DBGrid(p_TabArea,GRID_CTRL_DB,wxDefaultPosition, wxDefaultSize,
287 wxSUNKEN_BORDER);
288 p_TabArea->AddTab(p_DBGrid, Table, "");
289 p_DBGrid->i_ViewNr = p_TabArea->GetTabCount()-1;
290 p_DBGrid->pDoc = this;
291 p_DBGrid->db_Br = db_Br;
292 p_DBGrid->OnTableView(Table);
293 p_TabArea->SetActiveTab(p_DBGrid->i_ViewNr);
294 p_TabArea->Show(TRUE); // Activate the Window
295 }
296 if (Tab == 1) // Pageview
297 {
298 p_PageArea->Show(FALSE); // Deactivate the Window
299 p_DBGrid = new DBGrid(p_PageArea,GRID_CTRL_DB,wxDefaultPosition, wxDefaultSize,
300 wxSUNKEN_BORDER);
301 p_PageArea->AddTab(p_DBGrid, Table, "");
302 p_DBGrid->i_ViewNr = p_PageArea->GetTabCount()-1;
303 p_DBGrid->pDoc = this;
304 p_DBGrid->db_Br = db_Br;
305 p_DBGrid->i_Which = p_DBTree->i_Which;
306 p_PageArea->Show(TRUE); // Activate the Window
307 p_DBGrid->OnTableView(Table);
308 p_PageArea->SetActiveTab(p_DBGrid->i_ViewNr);
309 }
310 p_DBGrid->i_TabArt = Tab;
311 //---*----------------------
312 return TRUE;;
313 }
314 //----------------------------------------------------------------------------------------
315 void mjDoc::OnLeer(wxString Aufrufer)
316 {
317 // Temp0.Printf(_("\nmjDoc::OnLeer(%s) : auch diese funktion steht eines Tages zur Verfügung !"),Aufrufer.c_str());
318 Temp0.Printf(_("\nmjDoc::OnLeer(%s) : even this function will one day be available !"),Aufrufer.c_str());
319 wxLogMessage(Temp0); Temp0.Empty();
320 return;
321 }
322 //----------------------------------------------------------------------------------------
323 BEGIN_EVENT_TABLE(DocSplitterWindow, wxSplitterWindow)
324 END_EVENT_TABLE()
325 //----------------------------------------------------------------------------------------
326 // Define a constructor for my p_Splitter
327 DocSplitterWindow::DocSplitterWindow(wxWindow *parent, wxWindowID id) : wxSplitterWindow(parent, id)
328 {
329 }
330 //----------------------------------------------------------------------------------------