changed internat sample so that it shows language choice dialog on startup
[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 #ifdef __GNUG__
13 #pragma implementation
14 #pragma interface
15 #endif
16
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "wx/intl.h"
29 #include "wx/file.h"
30 #include "wx/log.h"
31
32 #if defined(__WXGTK__) || defined(__WXMOTIF__)
33 #include "mondrian.xpm"
34 #endif
35
36 // Define a new application type
37 class MyApp: public wxApp
38 {
39 public:
40 virtual bool OnInit();
41
42 protected:
43 wxLocale m_locale; // locale we'll be using
44 };
45
46 // Define a new frame type
47 class MyFrame: public wxFrame
48 {
49 public:
50 MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h,
51 wxLocale& m_locale);
52
53 public:
54 void OnQuit(wxCommandEvent& event);
55 void OnAbout(wxCommandEvent& event);
56 void OnPlay(wxCommandEvent& event);
57 void OnOpen(wxCommandEvent& event);
58
59 DECLARE_EVENT_TABLE()
60
61 wxLocale& m_locale;
62 };
63
64 // ID for the menu commands
65 enum
66 {
67 MINIMAL_QUIT = 1,
68 MINIMAL_TEXT,
69 MINIMAL_ABOUT,
70 MINIMAL_TEST,
71 MINIMAL_OPEN
72 };
73
74 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
75 EVT_MENU(MINIMAL_QUIT, MyFrame::OnQuit)
76 EVT_MENU(MINIMAL_ABOUT, MyFrame::OnAbout)
77 EVT_MENU(MINIMAL_TEST, MyFrame::OnPlay)
78 EVT_MENU(MINIMAL_OPEN, MyFrame::OnOpen)
79 END_EVENT_TABLE()
80
81 IMPLEMENT_APP(MyApp)
82
83
84 // `Main program' equivalent, creating windows and returning main app frame
85 bool MyApp::OnInit()
86 {
87 wxString langs[] = {"(System default)","French","German"};
88 SetExitOnFrameDelete(FALSE);
89 int lng = wxGetSingleChoiceIndex("Please choose language:", "Language",
90 3, langs);
91 SetExitOnFrameDelete(TRUE);
92
93 switch (lng)
94 {
95 case 0 : m_locale.Init(wxLANGUAGE_DEFAULT); break;
96 case 1 : m_locale.Init(wxLANGUAGE_FRENCH); break;
97 case 2 : m_locale.Init(wxLANGUAGE_GERMAN); break;
98 default:
99 return FALSE;
100 }
101
102
103 // Initialize the catalogs we'll be using
104 /* not needed any more, done in wxLocale ctor
105 m_locale.AddCatalog("wxstd"); // 1) for library messages
106 */
107 m_locale.AddCatalog("internat"); // 2) our private one
108 /* this catalog is installed in standard location on Linux systems,
109 it might not be installed on yours - just ignore the errrors
110 or comment out this line then */
111 #ifdef __LINUX__
112 //m_locale.AddCatalog("fileutils"); // 3) and another just for testing
113 #endif
114
115 // Create the main frame window
116 MyFrame *frame = new MyFrame((wxFrame *) NULL, _("International wxWindows App"),
117 50, 50, 350, 60, m_locale);
118
119 // Give it an icon
120 frame->SetIcon(wxICON(mondrian));
121
122 // Make a menubar
123 wxMenu *file_menu = new wxMenu;
124 file_menu->Append(MINIMAL_ABOUT, _("&About..."));
125 file_menu->AppendSeparator();
126 file_menu->Append(MINIMAL_QUIT, _("E&xit"));
127
128 wxMenu *test_menu = new wxMenu;
129 test_menu->Append(MINIMAL_OPEN, _("&Open bogus file"));
130 test_menu->Append(MINIMAL_TEST, _("&Play a game"));
131
132 wxMenuBar *menu_bar = new wxMenuBar;
133 menu_bar->Append(file_menu, _("&File"));
134 menu_bar->Append(test_menu, _("&Test"));
135 frame->SetMenuBar(menu_bar);
136
137 // Show the frame
138 frame->Show(TRUE);
139 SetTopWindow(frame);
140
141 return TRUE;
142 }
143
144 // My frame constructor
145 MyFrame::MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h,
146 wxLocale& l)
147 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h)),
148 m_locale(l)
149 {
150 }
151
152 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
153 {
154 Close(TRUE);
155 }
156
157 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
158 {
159 wxString localeInfo;
160 localeInfo.Printf(_("Language: %s\n"
161 "System locale name: %s\n"
162 "Canonical locale name: %s\n"),
163 m_locale.GetLocale(),
164 m_locale.GetSysName().c_str(),
165 m_locale.GetCanonicalName().c_str());
166
167 wxMessageDialog(this, wxString(_("I18n sample\n"
168 "(c) 1998, 1999 Vadim Zeitlin and Julian Smart"))
169 + wxT("\n\n") + localeInfo,
170 _("About Internat"), wxOK | wxICON_INFORMATION).ShowModal();
171 }
172
173 void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
174 {
175 wxString str = wxGetTextFromUser(_("Enter your number:"),
176 _("Try to guess my number!"),
177 "", this);
178 if ( str.IsEmpty() )
179 return;
180
181 int num;
182 sscanf(str, "%d", &num);
183 if ( num == 0 )
184 str = _("You've probably entered an invalid number.");
185 else if ( num == 9 ) // this message is not translated (not in catalog)
186 str = "You've found a bug in this program!";
187 else if ( num != 17 ) // a more implicit way to write _()
188 str = wxGetTranslation("Bad luck! try again...");
189 else {
190 str.Empty();
191 // string must be split in two -- otherwise the translation won't be found
192 str << _("Congratulations! you've won. Here is the magic phrase:")
193 << _("cannot create fifo `%s'");
194 }
195
196 wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
197 }
198
199 void MyFrame::OnOpen(wxCommandEvent&)
200 {
201 // open a bogus file -- the error message should be also translated if you've
202 // got wxstd.mo somewhere in the search path
203 wxFile file("NOTEXIST.ING");
204 }