corrected (after wxTString untimely death) and expanded
[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 // Define a new application type
33 class MyApp: public wxApp
34 {
35 public:
36 MyApp();
37
38 virtual bool OnInit();
39
40 protected:
41 wxLocale m_locale; // locale we'll be using
42 };
43
44 // Define a new frame type
45 class MyFrame: public wxFrame
46 {
47 public:
48 MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h);
49
50 public:
51 void OnQuit(wxCommandEvent& event);
52 void OnAbout(wxCommandEvent& event);
53 void OnPlay(wxCommandEvent& event);
54 void OnOpen(wxCommandEvent& event);
55 bool OnClose(void) { return TRUE; }
56
57 DECLARE_EVENT_TABLE()
58 };
59
60 // ID for the menu commands
61 enum
62 {
63 MINIMAL_QUIT,
64 MINIMAL_TEXT,
65 MINIMAL_ABOUT,
66 MINIMAL_TEST,
67 MINIMAL_OPEN
68 };
69
70 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
71 EVT_MENU(MINIMAL_QUIT, MyFrame::OnQuit)
72 EVT_MENU(MINIMAL_ABOUT, MyFrame::OnAbout)
73 EVT_MENU(MINIMAL_TEST, MyFrame::OnPlay)
74 EVT_MENU(MINIMAL_OPEN, MyFrame::OnOpen)
75 END_EVENT_TABLE()
76
77 IMPLEMENT_APP(MyApp)
78
79
80 MyApp::MyApp() : m_locale("french", "fr", "C")
81 {
82 // catalogs we'll be using:
83 /* not needed any more, done in wxLocale ctor
84 m_locale.AddCatalog("wxstd"); // 1) for library messages
85 */
86 m_locale.AddCatalog("internat"); // 2) our private one
87 /* this catalog is installed in standard location on Linux systems,
88 it might not be installed on yours - just ignore the errrors
89 or comment out this line then */
90 m_locale.AddCatalog("fileutils"); // 3) and another just for testing
91
92 }
93
94 // `Main program' equivalent, creating windows and returning main app frame
95 bool MyApp::OnInit(void)
96 {
97 // Create the main frame window
98 MyFrame *frame = new MyFrame(NULL, _("Minimal wxWindows App"), 50, 50, 150, 40);
99
100 // Give it an icon
101 #ifdef __WXMSW__
102 frame->SetIcon(wxIcon("mondrian"));
103 #endif
104 #ifdef __X__
105 frame->SetIcon(wxIcon("aiai.xbm"));
106 #endif
107
108 // Make a menubar
109 wxMenu *file_menu = new wxMenu;
110 file_menu->Append(MINIMAL_ABOUT, _("&About"));
111 file_menu->AppendSeparator();
112 file_menu->Append(MINIMAL_QUIT, _("E&xit"));
113
114 wxMenu *test_menu = new wxMenu;
115 test_menu->Append(MINIMAL_OPEN, _("&Open bogus file"));
116 test_menu->Append(MINIMAL_TEST, _("&Play a game"));
117
118 wxMenuBar *menu_bar = new wxMenuBar;
119 menu_bar->Append(file_menu, _("&File"));
120 menu_bar->Append(test_menu, _("&Test"));
121 frame->SetMenuBar(menu_bar);
122
123 // Show the frame
124 frame->Show(TRUE);
125 SetTopWindow(frame);
126
127 return TRUE;
128 }
129
130 // My frame constructor
131 MyFrame::MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h)
132 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
133 {
134 }
135
136 void MyFrame::OnQuit(wxCommandEvent& event)
137 {
138 Close(TRUE);
139 }
140
141 void MyFrame::OnAbout(wxCommandEvent& event)
142 {
143 wxMessageDialog(this, _("I18n sample\n© Vadim Zeitlin & Julian Smart"),
144 _("About Internat"), wxOK | wxICON_INFORMATION).ShowModal();
145 }
146
147 void MyFrame::OnPlay(wxCommandEvent& event)
148 {
149 wxString str = wxGetTextFromUser(_("Enter your number:"),
150 _("Try to guess my number!"),
151 "", this);
152 int num;
153 sscanf(str, "%d", &num);
154 if ( num == 0 )
155 str = _("you've probably entered an invalid number.");
156 else if ( num == 9 ) // this message is not translated (not in catalog)
157 str = _("you've found a bug in this program!");
158 else if ( num != 17 ) // a more implicit way to write _()
159 str = wxGetTranslation("bad luck! try again...");
160 else {
161 str.Empty();
162 // string must be split in two -- otherwise the translation won't be found
163 str << _("congratulations! you've won. Here is the magic phrase:")
164 << _("cannot create fifo `%s'");
165 }
166
167 wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
168 }
169
170 void MyFrame::OnOpen(wxCommandEvent&)
171 {
172 // open a bogus file -- the error message should be also translated if you've
173 // got wxstd.mo somewhere in the search path
174 wxFile file("NOTEXIST.ING");
175 }