]> git.saurik.com Git - wxWidgets.git/blob - samples/nettest/nettest.cpp
minor correction
[wxWidgets.git] / samples / nettest / nettest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: net.cpp
3 // Purpose: wxWindows sample demonstrating network-related functions
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 07.07.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "nettest.cpp"
22 #pragma interface "nettest.cpp"
23 #endif
24
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 // for all others, include the necessary headers (this file is usually all you
33 // need because it includes almost all "standard" wxWindows headers
34 #ifndef WX_PRECOMP
35 #include "wx/wx.h"
36 #endif
37
38 #include "wx/net.h"
39
40 // ----------------------------------------------------------------------------
41 // private classes
42 // ----------------------------------------------------------------------------
43
44 // Define a new application type, each program should derive a class from wxApp
45 class MyApp : public wxApp
46 {
47 public:
48 // override base class virtuals
49 // ----------------------------
50
51 // this one is called on application startup and is a good place for the app
52 // initialization (doing it here and not in the ctor allows to have an error
53 // return: if OnInit() returns false, the application terminates)
54 virtual bool OnInit();
55
56 // event handlers
57 void OnConnected(wxDialUpEvent& event);
58
59 private:
60 DECLARE_EVENT_TABLE();
61 };
62
63 // Define a new frame type: this is going to be our main frame
64 class MyFrame : public wxFrame
65 {
66 public:
67 // ctor(s)
68 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
69
70 // event handlers (these functions should _not_ be virtual)
71 void OnQuit(wxCommandEvent& event);
72 void OnAbout(wxCommandEvent& event);
73 void OnHangUp(wxCommandEvent& event);
74 void OnDial(wxCommandEvent& event);
75
76 void OnIdle(wxIdleEvent& event);
77
78 private:
79 // any class wishing to process wxWindows events must use this macro
80 DECLARE_EVENT_TABLE()
81 };
82
83 // ----------------------------------------------------------------------------
84 // constants
85 // ----------------------------------------------------------------------------
86
87 // IDs for the controls and the menu commands
88 enum
89 {
90 // menu items
91 NetTest_Quit = 1,
92 NetTest_About,
93 NetTest_HangUp,
94 NetTest_Dial
95 };
96
97 // ----------------------------------------------------------------------------
98 // event tables and other macros for wxWindows
99 // ----------------------------------------------------------------------------
100
101 BEGIN_EVENT_TABLE(MyApp, wxApp)
102 EVT_DIALUP_CONNECTED(MyApp::OnConnected)
103 EVT_DIALUP_DISCONNECTED(MyApp::OnConnected)
104 END_EVENT_TABLE()
105
106 // the event tables connect the wxWindows events with the functions (event
107 // handlers) which process them. It can be also done at run-time, but for the
108 // simple menu events like this the static method is much simpler.
109 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
110 EVT_MENU(NetTest_Quit, MyFrame::OnQuit)
111 EVT_MENU(NetTest_About, MyFrame::OnAbout)
112 EVT_MENU(NetTest_HangUp, MyFrame::OnHangUp)
113 EVT_MENU(NetTest_Dial, MyFrame::OnDial)
114
115 EVT_IDLE(MyFrame::OnIdle)
116 END_EVENT_TABLE()
117
118 // Create a new application object: this macro will allow wxWindows to create
119 // the application object during program execution (it's better than using a
120 // static object for many reasons) and also declares the accessor function
121 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
122 // not wxApp)
123 IMPLEMENT_APP(MyApp)
124
125 // ============================================================================
126 // implementation
127 // ============================================================================
128
129 // ----------------------------------------------------------------------------
130 // the application class
131 // ----------------------------------------------------------------------------
132
133 // `Main program' equivalent: the program execution "starts" here
134 bool MyApp::OnInit()
135 {
136 // Create the main application window
137 MyFrame *frame = new MyFrame("Minimal wxWindows App",
138 wxPoint(50, 50), wxSize(450, 340));
139
140 // Show it and tell the application that it's our main window
141 frame->Show(TRUE);
142 SetTopWindow(frame);
143
144 // success: wxApp::OnRun() will be called which will enter the main message
145 // loop and the application will run. If we returned FALSE here, the
146 // application would exit immediately.
147 return TRUE;
148 }
149
150 void MyApp::OnConnected(wxDialUpEvent& event)
151 {
152 wxMessageBox(event.IsConnectedEvent() ? "Just connected!"
153 : "Disconnected",
154 "Dial Up Manager Notification",
155 wxOK | wxICON_INFORMATION,
156 GetTopWindow());
157 }
158
159 // ----------------------------------------------------------------------------
160 // main frame
161 // ----------------------------------------------------------------------------
162
163 // frame constructor
164 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
165 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
166 {
167 // set the frame icon
168 SetIcon(wxICON(mondrian));
169
170 // create a menu bar
171 wxMenu *menuFile = new wxMenu;
172
173 menuFile->Append(NetTest_Dial, "&Dial\tCtrl-D", "Dial default ISP");
174 menuFile->Append(NetTest_HangUp, "&HangUp\tCtrl-H", "Hang up modem");
175 menuFile->AppendSeparator();
176 menuFile->Append(NetTest_About, "&About...\tCtrl-A", "Show about dialog");
177 menuFile->AppendSeparator();
178 menuFile->Append(NetTest_Quit, "E&xit\tAlt-X", "Quit this program");
179
180 // now append the freshly created menu to the menu bar...
181 wxMenuBar *menuBar = new wxMenuBar;
182 menuBar->Append(menuFile, "&File");
183
184 // ... and attach this menu bar to the frame
185 SetMenuBar(menuBar);
186
187 CreateStatusBar(2);
188 }
189
190
191 // event handlers
192
193 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
194 {
195 // TRUE is to force the frame to close
196 Close(TRUE);
197 }
198
199 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
200 {
201 wxString msg;
202 msg.Printf(_T("This is the network functions test sample.\n"
203 "© 1999 Vadim Zeitlin"));
204
205 wxMessageBox(msg, _T("About NetTest"), wxOK | wxICON_INFORMATION, this);
206 }
207
208 void MyFrame::OnHangUp(wxCommandEvent& WXUNUSED(event))
209 {
210 if ( wxDialUpManager::Get()->HangUp() )
211 {
212 wxLogStatus(this, "Connection was succesfully terminated.");
213 }
214 else
215 {
216 wxLogStatus(this, "Failed to hang up.");
217 }
218 }
219
220 void MyFrame::OnDial(wxCommandEvent& WXUNUSED(event))
221 {
222 wxLogStatus(this, "Dialing...");
223 wxYield();
224 wxBeginBusyCursor();
225
226 if ( wxDialUpManager::Get()->Dial("Free",
227 "zeitlin", "") )
228 {
229 wxLogStatus(this, "Connection was succesfully established.");
230 }
231 else
232 {
233 wxLogStatus(this, "Dialing attempt failed.");
234 }
235
236 wxEndBusyCursor();
237 }
238
239 void MyFrame::OnIdle(wxIdleEvent& WXUNUSED(event))
240 {
241 static int s_isOnline = -1; // not TRUE nor FALSE
242
243 bool isOnline = wxDialUpManager::Get()->IsOnline();
244 if ( s_isOnline != (int)isOnline )
245 {
246 s_isOnline = isOnline;
247
248 SetStatusText(isOnline ? "Online" : "Offline", 1);
249 }
250 }