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