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