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