fix XPM name to correct compilation under Unix
[wxWidgets.git] / demos / dbbrowse / dbbrowse.cpp
1 //----------------------------------------------------------------------------------------
2 // Name: dbbrowse.cpp
3 // Purpose: Through ODBC - Databases Browsen
4 // Author: Mark Johnson
5 // Modified by:
6 // BJO : Bart A.M. JOURQUIN
7 // Created: 19991127
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 #include "wx/stockitem.h"
25 //----------------------------------------------------------------------------------------
26 #ifndef __WXMSW__
27 #include "bitmaps/logo.xpm"
28 #endif
29 //----------------------------------------------------------------------------------------
30 //-- all #includes that every .cpp needs --- 19990807.mj10777 ----------------
31 //----------------------------------------------------------------------------------------
32 #include "std.h" // sorgsam Pflegen !
33 // #include <iostream>
34 //----------------------------------------------------------------------------------------
35 //-- Some Global Vars for this file ------------------------------------------------------
36 //----------------------------------------------------------------------------------------
37 BEGIN_EVENT_TABLE(MainFrame, wxFrame)
38 EVT_MENU(wxID_EXIT, MainFrame::OnQuit) // Program End
39 EVT_MENU(wxID_ABOUT, MainFrame::OnAbout) // Program Discription
40 EVT_MENU(wxID_HELP, MainFrame::OnHelp) // Program Help
41 END_EVENT_TABLE()
42
43 //----------------------------------------------------------------------------------------
44 IMPLEMENT_APP(MainApp) // This declares wxApp::MainApp as "the" Application
45
46 //----------------------------------------------------------------------------------------
47 // 'Main program' equivalent, creating windows and returning main app frame
48 //----------------------------------------------------------------------------------------
49 bool MainApp::OnInit(void) // Does everything needed for a program start
50 {
51 wxString Temp0; // Use as needed
52 //---------------------------------------------------------------------------------------
53 // set the language to use // Help.?? (.std = english, .de = german etc.)
54 const wxChar *language = NULL; // czech, german, french, polish
55 const wxChar *langid = NULL; // std = english , cz, de = german, fr = french, pl = polish
56 wxString s_LangHelp; // Directory/Filename.hhp of the Help-Project file
57 wxString s_LangId, s_Language;
58 s_Language.Empty(); s_LangId.Empty(); s_LangHelp.Empty();
59 //---------------------------------------------------------------------------------------
60 //-- Graphic File suport - use only when needed, otherwise big .exe's
61 //---------------------------------------------------------------------------------------
62 #if wxUSE_LIBPNG
63 wxImage::AddHandler( new wxPNGHandler ); // needed for help System
64 #endif
65 /*
66 #if wxUSE_LIBJPEG
67 wxImage::AddHandler(new wxJPEGHandler ); // use only when needed, otherwise big .exe's
68 #endif
69 wxImage::AddHandler( new wxGIFHandler ); // use only when needed, otherwise big .exe's
70 wxImage::AddHandler( new wxPCXHandler ); // use only when needed, otherwise big .exe's
71 wxImage::AddHandler( new wxPNMHandler ); // use only when needed, otherwise big .exe's
72 */
73 #ifdef __WXMSW__
74 // wxBitmap::AddHandler( new wxXPMFileHandler ); // Attempt to use XPS instead of ico
75 // wxBitmap::AddHandler( new wxXPMDataHandler ); // - Attempt failed
76 #endif
77 //---------------------------------------------------------------------------------------
78 switch ( argc )
79 {
80 default:
81 // ignore the other args, fall through
82 case 3:
83 language = argv[2]; // czech, english, french, german , polish
84 langid = argv[1]; // cz, std, fr, de , pl
85 break;
86 case 2:
87 langid = argv[1]; // cz, std, fr, de , pl
88 break;
89 case 1:
90 case 0:
91 break;
92 };
93 //---------------------------------------------------------------------------------------
94 // Win-Registry : Workplace\HKEY_CURRENT_USERS\Software\%GetVendorName()\%GetAppName()
95 //---------------------------------------------------------------------------------------
96 SetVendorName(_T("mj10777")); // Needed to get Configuration Information
97 SetAppName(_T("DBBrowse")); // "" , also needed for s_LangHelp
98 //---------------------------------------------------------------------------------------
99 // we're using wxConfig's "create-on-demand" feature: it will create the
100 // config object when it's used for the first time. It has a number of
101 // advantages compared with explicitly creating our wxConfig:
102 // 1) we don't pay for it if we don't use it
103 // 2) there is no danger to create it twice
104
105 // application and vendor name are used by wxConfig to construct the name
106 // of the config file/registry key and must be set before the first call
107 // to Get() if you want to override the default values (the application
108 // name is the name of the executable and the vendor name is the same)
109 //---------------------------------------------------------------------------------------
110 p_ProgramCfg = wxConfigBase::Get(); // Get Program Configuration from Registry
111 // p_ProgramCfg->DeleteAll(); // This is how the Config can be erased
112 p_ProgramCfg->SetPath(_T("/")); // Start at root
113 //---------------------------------------------------------------------------------------
114 //-- Set the Language and remember it for the next time. --------------------------------
115 //---------------------------------------------------------------------------------------
116 if (langid == NULL) // No Parameter was given
117 {
118 Temp0.Empty();
119 p_ProgramCfg->Read(_T("/Local/langid"),&Temp0); // >const char *langid< can't be used here
120 if (Temp0.empty())
121 langid = _T("std"); // Standard language is "std" = english
122 else
123 langid = Temp0;
124 }
125 Temp0.Printf(_T("%s"),langid);
126 //---------------------------------------------------------------------------------------
127 // Support the following languages (std = english)
128 if ((Temp0 == _T("a")) || (Temp0 == _T("cz")) || (Temp0 == _T("de")) ||
129 (Temp0 == _T("fr")) || (Temp0 == _T("pl")))
130 { // The three-letter language-string codes are only valid in Windows NT and Windows 95.
131 if (Temp0 == _T("cz"))
132 language = _T("czech"); // csy or czech
133 if ((Temp0 == _T("de")) || (Temp0 == _T("a")))
134 {
135 language = _T("german"); // deu or german
136 if (Temp0 == _T("a"))
137 { langid = Temp0 = _T("de"); } // Austrian = german
138 } // german / austrian
139 if (Temp0 == _T("fr"))
140 language = _T("french"); // fra or french
141 if (Temp0 == _T("pl"))
142 language = _T("polish"); // plk or polish
143 if (!m_locale.Init(language, langid, language)) // Don't do this for english (std)
144 { // You should recieve errors here for cz and pl since there is no cz/ and pl/ directory
145 wxLogMessage(_T("-E-> %s : SetLocale error : langid(%s) ; language(%s)"),GetAppName().c_str(),langid,language);
146 langid = _T("std");
147 language = _T("C"); // english, english-aus , -can , -nz , -uk , -usa
148 }
149 else
150 { // Read in Foreign language's text for GetAppName() and Help
151 Temp0 = GetAppName();
152 Temp0 = Temp0.Lower();
153 m_locale.AddCatalog(Temp0.c_str());
154 m_locale.AddCatalog(_T("help"));
155 }
156 } // Support the following languages (std = english)
157 else
158 {
159 langid = _T("std");
160 language = _T("C"); // english, english-aus , -can , -nz , -uk , -usa
161 }
162 s_Language.Printf(_T("%s"),language); // language is a pointer
163 s_LangId.Printf(_T("%s"),langid); // langid is a pointer
164 p_ProgramCfg->Write(_T("/Local/language"),s_Language);
165 p_ProgramCfg->Write(_T("/Local/langid"),s_LangId);
166 s_LangHelp.Printf(_T("help.%s/%s.hhp"),s_LangId.c_str(),GetAppName().c_str()); // "help.std/Garantie.hhp";
167 s_LangHelp = s_LangHelp.Lower(); // A must for Linux
168 //---------------------------------------------------------------------------------------
169 Temp0 = _T("NONE"); // I don't remember why I did this
170 p_ProgramCfg->Write(_T("/NONE"),Temp0); // I don't remember why I did this
171 p_ProgramCfg->Write(_T("/Paths/NONE"),Temp0); // I don't remember why I did this
172 p_ProgramCfg->Write(_T("/MainFrame/NONE"),Temp0); // I don't remember why I did this
173 //---------------------------------------------------------------------------------------
174 p_ProgramCfg->Write(_T("/Paths/Work"),wxGetCwd()); // Get current Working Path
175 p_ProgramCfg->SetPath(_T("/"));
176 //---------------------------------------------------------------------------------------
177 // restore frame position and size, if empty start Values (1,1) and (750,600)
178 int x = p_ProgramCfg->Read(_T("/MainFrame/x"), 1), y = p_ProgramCfg->Read(_T("/MainFrame/y"), 1),
179 w = p_ProgramCfg->Read(_T("/MainFrame/w"), 750), h = p_ProgramCfg->Read(_T("/MainFrame/h"), 600);
180 //---------------------------------------------------------------------------------------
181 // Create the main frame window
182 Temp0.Printf(_T("%s - %s"),GetAppName().c_str(),GetVendorName().c_str());
183 frame = new MainFrame((wxFrame *) NULL,(wxChar *) Temp0.c_str(),wxPoint(x,y),wxSize(w,h));
184 //---------------------------------------------------------------------------------------
185 // Set the Backgroundcolour (only needed if you are NOT using wxSYS_COLOUR_BACKGROUND)
186 frame->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BACKGROUND));
187 // frame->SetBackgroundColour(wxColour(255, 255, 255));
188 // frame->SetBackgroundColour(* wxWHITE);
189 //---------------------------------------------------------------------------------------
190 // Give it an icon
191 //---------------------------------------------------------------------------------------
192 // 12.02.2000 - Guillermo Rodriguez Garcia :
193 //---------------------------------------------------------------------------------------
194 // This is different for Win9x and WinNT; one of them takes the first ico
195 // in the .rc file, while the other takes the icon with the lowest name,
196 // so to be sure that it always work, put your icon the first *and* give
197 // it a name such a 'appicon' or something.
198 //---------------------------------------------------------------------------------------
199 // mj10777 : any special rule in Linux ?
200 //---------------------------------------------------------------------------------------
201 frame->SetIcon(wxICON(aLogo)); // lowest name and first entry in RC File
202 //---------------------------------------------------------------------------------------
203 // Make a menubar
204 wxMenu *file_menu = new wxMenu;
205 wxMenu *help_menu = new wxMenu;
206
207 help_menu->Append(wxID_HELP, wxGetStockLabel(wxID_HELP));
208 help_menu->AppendSeparator();
209 help_menu->Append(wxID_ABOUT, _("&About"));
210 file_menu->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT));
211
212 wxMenuBar *menu_bar = new wxMenuBar;
213 menu_bar->Append(file_menu, _("&File"));
214 menu_bar->Append(help_menu, _("&Help"));
215 frame->SetMenuBar(menu_bar);
216 #if wxUSE_STATUSBAR
217 frame->CreateStatusBar(1);
218 Temp0.Printf(_("%s has started !"),p_ProgramCfg->GetAppName().c_str());
219 frame->SetStatusText(Temp0, 0);
220 #endif // wxUSE_STATUSBAR
221 //---------------------------------------------------------------------------------------
222 int width, height;
223 frame->GetClientSize(&width, &height);
224 //---------------------------------------------------------------------------------------
225 frame->p_Splitter = new DocSplitterWindow(frame,wxID_ANY);
226 // p_Splitter->SetCursor(wxCursor(wxCURSOR_PENCIL));
227 frame->pDoc = new MainDoc();
228 frame->pDoc->p_MainFrame = frame;
229 frame->pDoc->p_Splitter = frame->p_Splitter;
230 frame->pDoc->p_Splitter->pDoc = frame->pDoc; // ControlBase: saving the Sash
231 //---------------------------------------------------------------------------------------
232 //-- Problem : GetClientSize(Width,Height) are not the same as the values given in the ---
233 //-- construction of the Frame. ---
234 //-- Solved : GetClientSize is called here and the difference is noted. When the ---
235 //-- Window is closed the diff. is added to the result of GetClientSize. ---
236 //---------------------------------------------------------------------------------------
237 frame->GetClientSize(&frame->DiffW, &frame->DiffH); frame->DiffW-=w; frame->DiffH-=h;
238 //----------------------------------------------------------------------------
239 //-- Help : Load the help.%langid/%GetAppName().hhp (help.std/dbbrowse.hhp) file ---
240 //----------------------------------------------------------------------------
241 frame->p_Help = new wxHtmlHelpController(); // construct the Help System
242 frame->p_Help->UseConfig(p_ProgramCfg); // Don't rember what this was for
243 // You should recieve errors here for fr since there is no help.fr/ directory
244 if (!frame->p_Help->AddBook(s_LangHelp)) // Use the language set
245 { // You should recieve errors here for fr since there is no help.fr/ but a fr/ directory
246 wxLogMessage(_T("-E-> %s : AddBook error : s_LangHelp(%s)"),GetAppName().c_str(),s_LangHelp.c_str());
247 }
248 frame->pDoc->p_Help = frame->p_Help; // Save the information to the document
249 //---------------------------------------------------------------------------------------
250 frame->Show(true); // Show the frame
251 SetTopWindow(frame); // At this point the frame can be seen
252 //---------------------------------------------------------------------------------------
253 // If you need a "Splash Screen" because of a long OnNewDocument, do it here
254 if (!frame->pDoc->OnNewDocument())
255 frame->Close(true);
256 // Kill a "Splash Screen" because OnNewDocument, if you have one
257 //---------------------------------------------------------------------------------------
258 p_ProgramCfg->Flush(true); // save the configuration
259 return true;
260 } // bool MainApp::OnInit(void)
261
262 //----------------------------------------------------------------------------------------
263 // My frame constructor
264 //----------------------------------------------------------------------------------------
265 MainFrame::MainFrame(wxFrame *frame, wxChar *title, const wxPoint& pos, const wxSize& size):
266 wxFrame(frame, wxID_ANY, title, pos, size)
267 {
268 p_Splitter = NULL; pDoc = NULL; p_Help = NULL; // Keep the Pointers clean !
269 //--- Everything else is done in MainApp::OnInit() --------------------------------------
270 }
271
272 //----------------------------------------------------------------------------------------
273 MainFrame::~MainFrame(void)
274 {
275 // Close the help frame; this will cause the config data to get written.
276 if (p_Help->GetFrame()) // returns NULL if no help frame active
277 p_Help->GetFrame()->Close(true);
278 delete p_Help; // Memory Leak
279 p_Help = NULL;
280 // save the control's values to the config
281 if (p_ProgramCfg == NULL)
282 return;
283 // save the frame position before it is destroyed
284 int x, y, w, h;
285 GetPosition(&x, &y);
286 GetClientSize(&w, &h); w -= DiffW; h -= DiffH;
287 p_ProgramCfg->Write(_T("/MainFrame/x"), (long) x);
288 p_ProgramCfg->Write(_T("/MainFrame/y"), (long) y);
289 p_ProgramCfg->Write(_T("/MainFrame/w"), (long) w);
290 p_ProgramCfg->Write(_T("/MainFrame/h"), (long) h);
291 p_ProgramCfg->Write(_T("/MainFrame/Sash"), (long) pDoc->Sash);
292 // clean up: Set() returns the active config object as Get() does, but unlike
293 // Get() it doesn't try to create one if there is none (definitely not what
294 // we want here!)
295 // delete wxConfigBase::Set((wxConfigBase *) NULL);
296 p_ProgramCfg->Flush(true); // saves Objekt
297 if (pDoc) // If we have a Valid Document
298 delete pDoc; // Cleanup (MainDoc::~MainDoc)
299 } // MainFrame::~MainFrame(void)
300
301 //----------------------------------------------------------------------------------------
302 void MainFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
303 {
304 Close(true);
305 }
306
307 //----------------------------------------------------------------------------------------
308 void MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
309 {
310 wxString Temp0, Temp1;
311 Temp0.Printf(_("%s\nMark Johnson\nBerlin, Germany\nwxWindows@mj10777.de\n (c) 2000"),p_ProgramCfg->GetAppName().c_str());
312 Temp1.Printf(_("About %s"),p_ProgramCfg->GetAppName().c_str());
313 wxMessageDialog dialog(this, Temp0,Temp1,wxOK|wxCANCEL);
314 dialog.ShowModal();
315 }
316
317 //----------------------------------------------------------------------------------------
318 void MainFrame::OnHelp(wxCommandEvent& WXUNUSED(event))
319 {
320 p_Help->Display(_T("Main page"));
321 }
322 //----------------------------------------------------------------------------------------