]> git.saurik.com Git - wxWidgets.git/blob - demos/dbbrowse/doc.cpp
873261e884eab4d585d1cbef89b5ddf7f4e384a8
[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()) // LogBook is now activ
84 return FALSE;
85 p_PgmCtrl->OnPopulate();
86 //-------------------------------------------------------------------
87 return TRUE;
88 }
89 //---------------------------------------------------------------------------
90 bool mjDoc::OnInitView()
91 {
92 Sash = p_ProgramCfg->Read("/MainFrame/Sash", 200);
93 // wxMessageBox("OnInitView() - Begin ","-I->mjDoc::OnInitView");
94 //--------------------------------------------------------------------------
95 // create "workplace" window
96 //--------------------------------------------------------------------------
97 p_TabArea = new wxTabbedWindow(); // Init the Pointer
98 p_TabArea->Create(p_Splitter, -1);
99 //--------------------------------------------------------------------------
100 p_PgmCtrl = new PgmCtrl(p_TabArea, TREE_CTRL_PGM,wxDefaultPosition, wxDefaultSize,
101 wxTR_HAS_BUTTONS | wxSUNKEN_BORDER);
102 p_PgmCtrl->i_TabArt = 0; // 0 = Tab ; 1 = Page
103 p_PgmCtrl->i_ViewNr = p_TabArea->GetTabCount()-1;
104 //--------------------------------------------------------------------------
105 wxBitmap *p_FolderClose = new wxBitmap("PgmCtrl"); //, wxBITMAP_TYPE_BMP_RESOURCE); // BJO20000115
106 //--------------------------------------------------------------------------
107 p_TabArea->AddTab(p_PgmCtrl,"PgmCtrl",p_FolderClose);
108 //--------------------------------------------------------------------------
109 // now create "output" window
110 //--------------------------------------------------------------------------
111 p_PageArea = new wxPaggedWindow(); // Init the Pointer
112 p_PageArea->Create(p_Splitter, -1);
113 //--------------------------------------------------------------------------
114 p_LogWin = new wxTextCtrl(p_PageArea,-1,wxEmptyString,
115 wxDefaultPosition, wxDefaultSize,wxTE_MULTILINE );
116 wxFont* ft_Temp = new wxFont(10,wxSWISS,wxNORMAL,wxBOLD,FALSE,"Comic Sans MS");
117 p_LogWin->SetFont(* ft_Temp);
118 // Don't forget ! This is always : i_TabArt = 0 ; i_ViewNr = 1;
119 //------------------------------------------------------------------
120 p_LogBook = new wxLogTextCtrl(p_LogWin); // make p_LogWin the LogBook
121 p_LogBook->SetActiveTarget(p_LogBook);
122 p_LogBook->SetTimestamp( NULL );
123 //------------------------------------------------------------------
124 p_PageArea->AddTab(p_LogWin,_("LogBook"), "what is this?" );
125 i_TabNr = p_TabArea->GetTabCount()-1; // Add one when a new AddTab is done;
126 i_PageNr = p_PageArea->GetTabCount()-1; // Add one when a new AddTab is done;
127 //--------------------------------------------------------------------------
128 p_PgmCtrl->pDoc = this;
129 p_TabArea->SetActiveTab(i_PageNr);
130 //--------------------------------------------------------------------------
131 p_Splitter->Initialize(p_TabArea);
132 p_Splitter->SplitHorizontally(p_TabArea,p_PageArea,Sash);
133 //--------------------------------------------------------------------------
134
135 if (!OnInitODBC())
136 return FALSE;
137 //--------------------------------------------------------------------------
138 Temp0.Printf("-I-> mjDoc::OnInitView() - End - %d DSN's found",i_DSN);
139 p_MainFrame->SetStatusText(Temp0, 0);
140 wxLogMessage(Temp0);
141 return TRUE;
142 }
143 //----------------------------------------------------------------------------------------
144 bool mjDoc::OnInitODBC()
145 {
146 char Dsn[SQL_MAX_DSN_LENGTH + 1];
147 char DsDesc[255]; // BJO20002501 instead of 512
148 Temp0 = "";
149 i_DSN = 0; // Counter
150 int i = 0;
151 //---------------------------------------------------------------------------
152 // Initialize the ODBC Environment for Database Operations
153
154 if (SQLAllocEnv(&DbConnectInf.Henv) != SQL_SUCCESS)
155 {
156 return FALSE;
157 }
158 //---------------------------------------------------------------------------
159
160 const char sep = 3; // separator character used in string between DSN ans DsDesc
161 wxStringList s_SortDSNList, s_SortDsDescList;
162 // BJO-20000127
163 // In order to have same sort result on both Dsn and DsDesc, create a 'keyed' string.
164 // The key will be removed after sorting
165 wxString KeyString;
166 //---------------------------------------------------------------------------
167 while(GetDataSource(DbConnectInf.Henv, Dsn, sizeof(Dsn), DsDesc, sizeof(DsDesc)))
168 {
169 i_DSN++; // How many Dsn have we ?
170 KeyString.Printf("%s%c%s",Dsn, sep, DsDesc);
171 s_SortDSNList.Add(Dsn);
172 s_SortDsDescList.Add(KeyString);
173 }
174 s_SortDSNList.Sort(); //BJO
175 s_SortDsDescList.Sort(); //BJO
176
177 char ** s_SortDSN = s_SortDSNList.ListToArray(); //BJO
178 char ** s_SortDsDesc = s_SortDsDescList.ListToArray(); //BJO
179 //---------------------------------------------------------------------------
180 // Allocate n ODBC-DSN objects to hold the information
181 p_DSN = new DSN[i_DSN]; //BJO
182 for (i=0;i<i_DSN;i++)
183 {
184 KeyString = s_SortDsDesc[i];
185 KeyString = KeyString.AfterFirst(sep);
186 strcpy(s_SortDsDesc[i],KeyString.c_str());
187 (p_DSN+i)->Dsn = s_SortDSN[i];
188 (p_DSN+i)->Drv = s_SortDsDesc[i];
189 (p_DSN+i)->Usr = "";
190 (p_DSN+i)->Pas = "";
191 Temp0.Printf("%02d) Dsn(%s) DsDesc(%s)",i,(p_DSN+i)->Dsn,(p_DSN+i)->Drv);
192 wxLogMessage(Temp0);
193 }
194 i = 0;
195 //---------------------------------------------------------------------------
196 // Allocate n wxDatabase objects to hold the column information
197 db_Br = new BrowserDB[i_DSN];
198 for (i=0;i<i_DSN;i++)
199 {
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 if (SQLFreeEnv(&DbConnectInf.Henv) != SQL_SUCCESS) // BJO20000125
209 {
210 // Error freeing environment handle
211 }
212
213 delete [] s_SortDSN;
214 delete [] s_SortDsDesc;
215
216 //---------------------------------------------------------------------------
217 if (!i_DSN)
218 {
219 wxMessageBox(_("No Dataset names found in ODBC!\n" \
220 " Program will exit!\n\n" \
221 " Ciao"),"-E-> Fatal situation");
222 return FALSE;
223 }
224 //---------------------------------------------------------------------------
225 return TRUE;
226 }
227 //----------------------------------------------------------------------------------------
228 bool mjDoc::OnChosenDSN(int Which)
229 {
230 // wxLogMessage("OnChosenDSN(%d) - Begin",Which);
231 //---------------------------------------------------------------------------
232 if (p_DBTree != NULL)
233 {
234 p_TabArea->Show(FALSE); // Deactivate the Window
235 p_TabArea->RemoveTab(p_DBTree->i_ViewNr);
236 p_TabArea->Show(TRUE); // Activate the Window
237 OnChosenTbl(77,"");
238 }
239 //-------------------------
240 p_TabArea->Show(FALSE); // Deactivate the Window
241 p_DBTree = new DBTree(p_TabArea, TREE_CTRL_DB,wxDefaultPosition, wxDefaultSize,
242 wxTR_HAS_BUTTONS | wxSUNKEN_BORDER);
243 p_TabArea->AddTab(p_DBTree,(p_DSN+Which)->Dsn," ? ");
244 p_DBTree->i_ViewNr = p_TabArea->GetTabCount()-1;
245 p_TabArea->Show(TRUE); // Deactivate the Window
246 p_DBTree->i_Which = Which;
247 p_DBTree->s_DSN = (p_DSN+Which)->Dsn;
248 p_DBTree->i_TabArt = 0;
249 p_DBTree->pDoc = this;
250 p_DBTree->OnPopulate();
251 p_TabArea->SetActiveTab(p_DBTree->i_ViewNr);
252 //---------------------------------------------------------------------------
253 // wxLogMessage("OnChosenDSN(%d) - End",Which);
254 return TRUE;
255 }
256 //----------------------------------------------------------------------------------------
257 bool mjDoc::OnChosenTbl(int Tab,wxString Table)
258 {
259 // wxLogMessage("OnChosenTbl(%d,%s)",Tab,Table.c_str());
260 //-------------------------
261 if (p_DBGrid != NULL)
262 {
263 if (p_DBGrid->i_TabArt == 0)
264 {
265 p_TabArea->Show(FALSE); // Deactivate the Window
266 p_TabArea->RemoveTab(p_DBGrid->i_ViewNr);
267 p_TabArea->Show(TRUE); // Activate the Window
268 }
269 if (p_DBGrid->i_TabArt == 1)
270 {
271 p_PageArea->Show(FALSE); // Deactivate the Window
272 p_PageArea->RemoveTab(p_DBGrid->i_ViewNr);
273 p_PageArea->Show(TRUE); // Activate the Window
274 }
275 p_DBGrid = NULL;
276 delete p_DBGrid;
277 }
278 if (Tab == 77) // Delete only
279 return TRUE;
280 //-------------------------
281 if (Tab == 0) // Tabview
282 {
283 p_TabArea->Show(FALSE); // Deactivate the Window
284 p_DBGrid = new DBGrid(p_TabArea,GRID_CTRL_DB,wxDefaultPosition, wxDefaultSize,
285 wxSUNKEN_BORDER);
286 p_TabArea->AddTab(p_DBGrid, Table, "");
287 p_DBGrid->i_ViewNr = p_TabArea->GetTabCount()-1;
288 p_DBGrid->pDoc = this;
289 p_DBGrid->db_Br = db_Br;
290 p_DBGrid->OnTableView(Table);
291 p_TabArea->SetActiveTab(p_DBGrid->i_ViewNr);
292 p_TabArea->Show(TRUE); // Activate the Window
293 }
294 if (Tab == 1) // Pageview
295 {
296 p_PageArea->Show(FALSE); // Deactivate the Window
297 p_DBGrid = new DBGrid(p_PageArea,GRID_CTRL_DB,wxDefaultPosition, wxDefaultSize,
298 wxSUNKEN_BORDER);
299 p_PageArea->AddTab(p_DBGrid, Table, "");
300 p_DBGrid->i_ViewNr = p_PageArea->GetTabCount()-1;
301 p_DBGrid->pDoc = this;
302 p_DBGrid->db_Br = db_Br;
303 p_DBGrid->i_Which = p_DBTree->i_Which;
304 p_PageArea->Show(TRUE); // Activate the Window
305 p_DBGrid->OnTableView(Table);
306 p_PageArea->SetActiveTab(p_DBGrid->i_ViewNr);
307 }
308 p_DBGrid->i_TabArt = Tab;
309 //---*----------------------
310 return TRUE;;
311 }
312 //----------------------------------------------------------------------------------------
313 void mjDoc::OnLeer(wxString Aufrufer)
314 {
315 // Temp0.Printf(_("\nmjDoc::OnLeer(%s) : auch diese funktion steht eines Tages zur Verfügung !"),Aufrufer.c_str());
316 Temp0.Printf(_("\nmjDoc::OnLeer(%s) : even this function will one day be available !"),Aufrufer.c_str());
317 wxLogMessage(Temp0); Temp0.Empty();
318 return;
319 }
320 //----------------------------------------------------------------------------------------
321 BEGIN_EVENT_TABLE(DocSplitterWindow, wxSplitterWindow)
322 END_EVENT_TABLE()
323 //----------------------------------------------------------------------------------------
324 // Define a constructor for my p_Splitter
325 DocSplitterWindow::DocSplitterWindow(wxWindow *parent, wxWindowID id) : wxSplitterWindow(parent, id)
326 {
327 }
328 //----------------------------------------------------------------------------------------