Fix compilation with GCC (at least 2.95.4).
[wxWidgets.git] / samples / internat / internat.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: internat.cpp
3 // Purpose: Demonstrates internationalisation (i18n) support
4 // Author: Vadim Zeitlin/Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/wx.h"
29 #endif
30
31 #include "wx/intl.h"
32 #include "wx/file.h"
33 #include "wx/log.h"
34
35 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
36 #include "mondrian.xpm"
37 #endif
38
39 // ----------------------------------------------------------------------------
40 // private classes
41 // ----------------------------------------------------------------------------
42
43 // Define a new application type
44 class MyApp: public wxApp
45 {
46 public:
47 virtual bool OnInit();
48
49 protected:
50 wxLocale m_locale; // locale we'll be using
51 };
52
53 // Define a new frame type
54 class MyFrame: public wxFrame
55 {
56 public:
57 MyFrame(wxLocale& m_locale);
58
59 public:
60 void OnQuit(wxCommandEvent& event);
61 void OnAbout(wxCommandEvent& event);
62 void OnPlay(wxCommandEvent& event);
63 void OnOpen(wxCommandEvent& event);
64
65 DECLARE_EVENT_TABLE()
66
67 wxLocale& m_locale;
68 };
69
70 // ----------------------------------------------------------------------------
71 // constants
72 // ----------------------------------------------------------------------------
73
74 // ID for the menu commands
75 enum
76 {
77 INTERNAT_QUIT = 1,
78 INTERNAT_TEXT,
79 INTERNAT_TEST,
80 INTERNAT_OPEN
81 };
82
83 // ----------------------------------------------------------------------------
84 // wxWindows macros
85 // ----------------------------------------------------------------------------
86
87 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
88 EVT_MENU(INTERNAT_QUIT, MyFrame::OnQuit)
89 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
90 EVT_MENU(INTERNAT_TEST, MyFrame::OnPlay)
91 EVT_MENU(INTERNAT_OPEN, MyFrame::OnOpen)
92 END_EVENT_TABLE()
93
94 IMPLEMENT_APP(MyApp)
95
96 // ============================================================================
97 // implementation
98 // ============================================================================
99
100 // ----------------------------------------------------------------------------
101 // MyApp
102 // ----------------------------------------------------------------------------
103
104 // `Main program' equivalent, creating windows and returning main app frame
105 bool MyApp::OnInit()
106 {
107 long lng = -1;
108
109 if ( argc == 2 )
110 {
111 // the parameter must be the lang index
112 wxString tmp(argv[1]);
113 tmp.ToLong(&lng);
114 }
115
116 static const wxLanguage langIds[] =
117 {
118 wxLANGUAGE_DEFAULT,
119 wxLANGUAGE_FRENCH,
120 wxLANGUAGE_GERMAN,
121 wxLANGUAGE_RUSSIAN,
122 wxLANGUAGE_JAPANESE,
123 wxLANGUAGE_ENGLISH,
124 wxLANGUAGE_ENGLISH_US,
125 };
126
127 if ( lng == -1 )
128 {
129 // note that it makes no sense to translate these strings, they are
130 // shown before we set the locale anyhow
131 const wxString langNames[] =
132 {
133 _T("System default"),
134 _T("French"),
135 _T("German"),
136 _T("Russian"),
137 _T("Japanese"),
138 _T("English"),
139 _T("English (U.S.)")
140 };
141
142 // the arrays should be in sync
143 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames) == WXSIZEOF(langIds),
144 LangArraysMismatch );
145
146 lng = wxGetSingleChoiceIndex
147 (
148 _T("Please choose language:"),
149 _T("Language"),
150 WXSIZEOF(langNames),
151 langNames
152 );
153 }
154
155 if ( lng != -1 )
156 m_locale.Init(langIds[lng]);
157
158
159 // Initialize the catalogs we'll be using
160 m_locale.AddCatalog(wxT("internat"));
161
162 // this catalog is installed in standard location on Linux systems and
163 // shows that you may make use of the standard message catalogs as well
164 //
165 // if it's not installed on your system, it is just silently ignored
166 #ifdef __LINUX__
167 {
168 wxLogNull noLog;
169 m_locale.AddCatalog(_T("fileutils"));
170 }
171 #endif
172
173 // Create the main frame window
174 MyFrame *frame = new MyFrame(m_locale);
175
176 // Give it an icon
177 frame->SetIcon(wxICON(mondrian));
178
179 // Make a menubar
180 wxMenu *file_menu = new wxMenu;
181 file_menu->Append(wxID_ABOUT, _("&About..."));
182 file_menu->AppendSeparator();
183 file_menu->Append(INTERNAT_QUIT, _("E&xit"));
184
185 wxMenu *test_menu = new wxMenu;
186 test_menu->Append(INTERNAT_OPEN, _("&Open bogus file"));
187 test_menu->Append(INTERNAT_TEST, _("&Play a game"));
188
189 wxMenuBar *menu_bar = new wxMenuBar;
190 menu_bar->Append(file_menu, _("&File"));
191 menu_bar->Append(test_menu, _("&Test"));
192 frame->SetMenuBar(menu_bar);
193
194 // Show the frame
195 frame->Show(TRUE);
196 SetTopWindow(frame);
197
198 return TRUE;
199 }
200
201 // ----------------------------------------------------------------------------
202 // MyFrame
203 // ----------------------------------------------------------------------------
204
205 // main frame constructor
206 MyFrame::MyFrame(wxLocale& locale)
207 : wxFrame(NULL,
208 -1,
209 _("International wxWindows App"),
210 wxPoint(50, 50),
211 wxSize(350, 60)),
212 m_locale(locale)
213 {
214 // Empty
215 }
216
217 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
218 {
219 Close(TRUE);
220 }
221
222 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
223 {
224 wxString localeInfo;
225 wxString locale = m_locale.GetLocale();
226 wxString sysname = m_locale.GetSysName();
227 wxString canname = m_locale.GetCanonicalName();
228
229 localeInfo.Printf( _("Language: %s\nSystem locale name: %s\nCanonical locale name: %s\n"),
230 locale.c_str(), sysname.c_str(), canname.c_str() );
231
232 wxMessageDialog(this, wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
233 + wxT("\n\n") + localeInfo,
234 _("About Internat"), wxOK | wxICON_INFORMATION).ShowModal();
235 }
236
237 void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
238 {
239 wxString str = wxGetTextFromUser(_("Enter your number:"),
240 _("Try to guess my number!"), wxEmptyString, this);
241
242 if ( str.IsEmpty() ) return;
243
244 int num;
245 wxSscanf(str, wxT("%d"), &num);
246 if ( num == 0 )
247 str = _("You've probably entered an invalid number.");
248 else if ( num == 9 ) // this message is not translated (not in catalog)
249 str = _T("You've found a bug in this program!");
250 else if ( num != 17 ) // a more implicit way to write _()
251 str = wxGetTranslation(wxT("Bad luck! try again..."));
252 else
253 {
254 str.Empty();
255 // string must be split in two -- otherwise the translation won't be found
256 str << _("Congratulations! you've won. Here is the magic phrase:")
257 << _("cannot create fifo `%s'");
258 }
259
260 wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
261 }
262
263 void MyFrame::OnOpen(wxCommandEvent&)
264 {
265 // open a bogus file -- the error message should be also translated if you've
266 // got wxstd.mo somewhere in the search path
267 wxFile file(wxT("NOTEXIST.ING"));
268 }