]> git.saurik.com Git - wxWidgets.git/blame - samples/internat/internat.cpp
Fixed wxMac's wxTextCtrl::SetSelection so that (-1,-1) will select the
[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,
122 wxLANGUAGE_JAPANESE,
123 wxLANGUAGE_ENGLISH,
124 wxLANGUAGE_ENGLISH_US,
125 };
126
017fcf32
VZ
127 if ( lng == -1 )
128 {
a2f26963
VZ
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[] =
017fcf32 132 {
a2f26963 133 _T("System default"),
017fcf32
VZ
134 _T("French"),
135 _T("German"),
136 _T("Russian"),
085c26ac 137 _T("Japanese"), // this will only work in Unicode build
017fcf32
VZ
138 _T("English"),
139 _T("English (U.S.)")
140 };
141
a2f26963
VZ
142 // the arrays should be in sync
143 wxCOMPILE_TIME_ASSERT( WXSIZEOF(langNames) == WXSIZEOF(langIds),
144 LangArraysMismatch );
145
017fcf32
VZ
146 lng = wxGetSingleChoiceIndex
147 (
148 _T("Please choose language:"),
149 _T("Language"),
a2f26963
VZ
150 WXSIZEOF(langNames),
151 langNames
017fcf32
VZ
152 );
153 }
154
a2f26963
VZ
155 if ( lng != -1 )
156 m_locale.Init(langIds[lng]);
aec18ff7 157
017fcf32 158
aec18ff7 159 // Initialize the catalogs we'll be using
017fcf32
VZ
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
8e97b17b 166#ifdef __LINUX__
aec18ff7 167 {
017fcf32
VZ
168 wxLogNull noLog;
169 m_locale.AddCatalog(_T("fileutils"));
aec18ff7 170 }
8e97b17b 171#endif
aec18ff7
MB
172
173 // Create the main frame window
017fcf32 174 MyFrame *frame = new MyFrame(m_locale);
aec18ff7
MB
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();
017fcf32 183 file_menu->Append(INTERNAT_QUIT, _("E&xit"));
aec18ff7
MB
184
185 wxMenu *test_menu = new wxMenu;
017fcf32
VZ
186 test_menu->Append(INTERNAT_OPEN, _("&Open bogus file"));
187 test_menu->Append(INTERNAT_TEST, _("&Play a game"));
aec18ff7
MB
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;
c801d85f
KB
199}
200
017fcf32
VZ
201// ----------------------------------------------------------------------------
202// MyFrame
203// ----------------------------------------------------------------------------
204
205// main frame constructor
206MyFrame::MyFrame(wxLocale& locale)
207 : wxFrame(NULL,
208 -1,
209 _("International wxWindows App"),
210 wxPoint(50, 50),
211 wxSize(350, 60)),
212 m_locale(locale)
c7f3b78b 213{
aec18ff7 214 // Empty
c7f3b78b 215}
c801d85f 216
bd7d06f2 217void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
c801d85f 218{
aec18ff7 219 Close(TRUE);
c801d85f
KB
220}
221
bd7d06f2 222void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
c801d85f 223{
aec18ff7 224 wxString localeInfo;
2b5f62a0
VZ
225 wxString locale = m_locale.GetLocale();
226 wxString sysname = m_locale.GetSysName();
227 wxString canname = m_locale.GetCanonicalName();
228
085c26ac 229 localeInfo.Printf(_("Language: %s\nSystem locale name: %s\nCanonical locale name: %s\n"),
2b5f62a0 230 locale.c_str(), sysname.c_str(), canname.c_str() );
aec18ff7 231
085c26ac
VZ
232 wxMessageDialog
233 (
234 this,
235 wxString(_("I18n sample\n(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
236 + wxT("\n\n")
237 + localeInfo,
238 _("About Internat"),
239 wxOK | wxICON_INFORMATION
240 ).ShowModal();
c801d85f
KB
241}
242
bd7d06f2 243void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
c7f3b78b 244{
085c26ac
VZ
245 wxString str = wxGetTextFromUser
246 (
247 _("Enter your number:"),
248 _("Try to guess my number!"),
249 wxEmptyString,
250 this
251 );
252
253 if ( str.empty() )
254 {
255 // cancelled
256 return;
257 }
aec18ff7 258
085c26ac
VZ
259 long num;
260 if ( !str.ToLong(&num) || num < 0 )
261 {
aec18ff7 262 str = _("You've probably entered an invalid number.");
085c26ac
VZ
263 }
264 else if ( num == 9 )
265 {
266 // this message is not translated (not in catalog) because we used _T()
267 // and not _() around it
2b5f62a0 268 str = _T("You've found a bug in this program!");
085c26ac
VZ
269 }
270 else if ( num == 17 )
aec18ff7 271 {
085c26ac
VZ
272 str.clear();
273
274 // string must be split in two -- otherwise the translation would't be
275 // found
aec18ff7
MB
276 str << _("Congratulations! you've won. Here is the magic phrase:")
277 << _("cannot create fifo `%s'");
278 }
085c26ac
VZ
279 else
280 {
281 // this is a more implicit way to write _() but note that if you use it
282 // you must ensure that the strings get extracted in the message
283 // catalog as by default xgettext won't do it (it only knows of _(),
284 // not wxGetTranslation())
285 str = wxGetTranslation(_T("Bad luck! try again..."));
286 }
aec18ff7
MB
287
288 wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
c7f3b78b
VZ
289}
290
291void MyFrame::OnOpen(wxCommandEvent&)
292{
aec18ff7
MB
293 // open a bogus file -- the error message should be also translated if you've
294 // got wxstd.mo somewhere in the search path
295 wxFile file(wxT("NOTEXIST.ING"));
071cc2be 296}
085c26ac 297