]> git.saurik.com Git - wxWidgets.git/blob - samples/internat/internat.cpp
by default the ctrl accepts keyboard input now (why wouldn't it?)
[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
52 public:
53 void OnQuit(wxCommandEvent& event);
54 void OnAbout(wxCommandEvent& event);
55 void OnPlay(wxCommandEvent& event);
56 void OnOpen(wxCommandEvent& event);
57 bool OnClose() { return TRUE; }
58
59 DECLARE_EVENT_TABLE()
60 };
61
62 // ID for the menu commands
63 enum
64 {
65 MINIMAL_QUIT,
66 MINIMAL_TEXT,
67 MINIMAL_ABOUT,
68 MINIMAL_TEST,
69 MINIMAL_OPEN
70 };
71
72 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
73 EVT_MENU(MINIMAL_QUIT, MyFrame::OnQuit)
74 EVT_MENU(MINIMAL_ABOUT, MyFrame::OnAbout)
75 EVT_MENU(MINIMAL_TEST, MyFrame::OnPlay)
76 EVT_MENU(MINIMAL_OPEN, MyFrame::OnOpen)
77 END_EVENT_TABLE()
78
79 IMPLEMENT_APP(MyApp)
80
81
82 // `Main program' equivalent, creating windows and returning main app frame
83 bool MyApp::OnInit()
84 {
85 // set the language to use
86 const char *language = NULL;
87 const char *langid = NULL;
88 switch ( argc )
89 {
90 default:
91 // ignore the other args, fall through
92
93 case 3:
94 language = argv[1];
95 langid = argv[2];
96 break;
97
98 case 2:
99 language = argv[1];
100 break;
101
102 case 1:
103 language = "french";
104 langid = "fr";
105 };
106
107 // there are very few systems right now which support locales other than "C"
108 m_locale.Init(language, langid, "C");
109
110 // Initialize the catalogs we'll be using
111 /* not needed any more, done in wxLocale ctor
112 m_locale.AddCatalog("wxstd"); // 1) for library messages
113 */
114 m_locale.AddCatalog("internat"); // 2) our private one
115 /* this catalog is installed in standard location on Linux systems,
116 it might not be installed on yours - just ignore the errrors
117 or comment out this line then */
118 m_locale.AddCatalog("fileutils"); // 3) and another just for testing
119
120 // Create the main frame window
121 MyFrame *frame = new MyFrame((wxFrame *) NULL, _("International wxWindows App"),
122 50, 50, 250, 40);
123
124 // Give it an icon
125 frame->SetIcon(wxICON(mondrian));
126
127 // Make a menubar
128 wxMenu *file_menu = new wxMenu;
129 file_menu->Append(MINIMAL_ABOUT, _("&About..."));
130 file_menu->AppendSeparator();
131 file_menu->Append(MINIMAL_QUIT, _("E&xit"));
132
133 wxMenu *test_menu = new wxMenu;
134 test_menu->Append(MINIMAL_OPEN, _("&Open bogus file"));
135 test_menu->Append(MINIMAL_TEST, _("&Play a game"));
136
137 wxMenuBar *menu_bar = new wxMenuBar;
138 menu_bar->Append(file_menu, _("&File"));
139 menu_bar->Append(test_menu, _("&Test"));
140 frame->SetMenuBar(menu_bar);
141
142 // Show the frame
143 frame->Show(TRUE);
144 SetTopWindow(frame);
145
146 return TRUE;
147 }
148
149 // My frame constructor
150 MyFrame::MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h)
151 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
152 {
153 }
154
155 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
156 {
157 Close(TRUE);
158 }
159
160 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
161 {
162 wxMessageDialog(this, _("I18n sample\n"
163 "© 1998, 1999 Vadim Zeitlin and Julian Smart"),
164 _("About Internat"), wxOK | wxICON_INFORMATION).ShowModal();
165 }
166
167 void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
168 {
169 wxString str = wxGetTextFromUser(_("Enter your number:"),
170 _("Try to guess my number!"),
171 "", this);
172 if ( str.IsEmpty() )
173 return;
174
175 int num;
176 sscanf(str, "%d", &num);
177 if ( num == 0 )
178 str = _("You've probably entered an invalid number.");
179 else if ( num == 9 ) // this message is not translated (not in catalog)
180 str = "You've found a bug in this program!";
181 else if ( num != 17 ) // a more implicit way to write _()
182 str = wxGetTranslation("Bad luck! try again...");
183 else {
184 str.Empty();
185 // string must be split in two -- otherwise the translation won't be found
186 str << _("Congratulations! you've won. Here is the magic phrase:")
187 << _("cannot create fifo `%s'");
188 }
189
190 wxMessageBox(str, _("Result"), wxOK | wxICON_INFORMATION);
191 }
192
193 void MyFrame::OnOpen(wxCommandEvent&)
194 {
195 // open a bogus file -- the error message should be also translated if you've
196 // got wxstd.mo somewhere in the search path
197 wxFile file("NOTEXIST.ING");
198 }