]>
Commit | Line | Data |
---|---|---|
09884325 VZ |
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 | ||
e90c1d2a | 38 | #include "wx/dialup.h" |
09884325 VZ |
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 | ||
24df4c19 VZ |
56 | // called before the application termination |
57 | virtual int OnExit(); | |
58 | ||
09884325 VZ |
59 | // event handlers |
60 | void OnConnected(wxDialUpEvent& event); | |
61 | ||
24df4c19 VZ |
62 | // accessor to dial up manager |
63 | wxDialUpManager *GetDialer() const { return m_dial; } | |
64 | ||
09884325 | 65 | private: |
24df4c19 VZ |
66 | wxDialUpManager *m_dial; |
67 | ||
09884325 VZ |
68 | DECLARE_EVENT_TABLE(); |
69 | }; | |
70 | ||
71 | // Define a new frame type: this is going to be our main frame | |
72 | class MyFrame : public wxFrame | |
73 | { | |
74 | public: | |
75 | // ctor(s) | |
76 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
77 | ||
78 | // event handlers (these functions should _not_ be virtual) | |
79 | void OnQuit(wxCommandEvent& event); | |
80 | void OnAbout(wxCommandEvent& event); | |
81 | void OnHangUp(wxCommandEvent& event); | |
82 | void OnDial(wxCommandEvent& event); | |
2690830e | 83 | void OnEnumISPs(wxCommandEvent& event); |
09884325 | 84 | |
24df4c19 VZ |
85 | void OnUpdateUI(wxUpdateUIEvent& event); |
86 | ||
09884325 VZ |
87 | void OnIdle(wxIdleEvent& event); |
88 | ||
89 | private: | |
90 | // any class wishing to process wxWindows events must use this macro | |
91 | DECLARE_EVENT_TABLE() | |
92 | }; | |
93 | ||
94 | // ---------------------------------------------------------------------------- | |
95 | // constants | |
96 | // ---------------------------------------------------------------------------- | |
97 | ||
98 | // IDs for the controls and the menu commands | |
99 | enum | |
100 | { | |
101 | // menu items | |
102 | NetTest_Quit = 1, | |
103 | NetTest_About, | |
104 | NetTest_HangUp, | |
2690830e VZ |
105 | NetTest_Dial, |
106 | NetTest_EnumISP, | |
107 | NetTest_Max | |
09884325 VZ |
108 | }; |
109 | ||
110 | // ---------------------------------------------------------------------------- | |
111 | // event tables and other macros for wxWindows | |
112 | // ---------------------------------------------------------------------------- | |
113 | ||
114 | BEGIN_EVENT_TABLE(MyApp, wxApp) | |
115 | EVT_DIALUP_CONNECTED(MyApp::OnConnected) | |
116 | EVT_DIALUP_DISCONNECTED(MyApp::OnConnected) | |
117 | END_EVENT_TABLE() | |
118 | ||
119 | // the event tables connect the wxWindows events with the functions (event | |
120 | // handlers) which process them. It can be also done at run-time, but for the | |
121 | // simple menu events like this the static method is much simpler. | |
122 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
123 | EVT_MENU(NetTest_Quit, MyFrame::OnQuit) | |
124 | EVT_MENU(NetTest_About, MyFrame::OnAbout) | |
125 | EVT_MENU(NetTest_HangUp, MyFrame::OnHangUp) | |
126 | EVT_MENU(NetTest_Dial, MyFrame::OnDial) | |
2690830e | 127 | EVT_MENU(NetTest_EnumISP, MyFrame::OnEnumISPs) |
09884325 | 128 | |
24df4c19 VZ |
129 | EVT_UPDATE_UI(NetTest_Dial, MyFrame::OnUpdateUI) |
130 | ||
09884325 VZ |
131 | EVT_IDLE(MyFrame::OnIdle) |
132 | END_EVENT_TABLE() | |
133 | ||
134 | // Create a new application object: this macro will allow wxWindows to create | |
135 | // the application object during program execution (it's better than using a | |
136 | // static object for many reasons) and also declares the accessor function | |
137 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
138 | // not wxApp) | |
139 | IMPLEMENT_APP(MyApp) | |
140 | ||
141 | // ============================================================================ | |
142 | // implementation | |
143 | // ============================================================================ | |
144 | ||
145 | // ---------------------------------------------------------------------------- | |
146 | // the application class | |
147 | // ---------------------------------------------------------------------------- | |
148 | ||
149 | // `Main program' equivalent: the program execution "starts" here | |
150 | bool MyApp::OnInit() | |
151 | { | |
152 | // Create the main application window | |
24df4c19 | 153 | MyFrame *frame = new MyFrame("Dial-up wxWindows demo", |
09884325 VZ |
154 | wxPoint(50, 50), wxSize(450, 340)); |
155 | ||
156 | // Show it and tell the application that it's our main window | |
157 | frame->Show(TRUE); | |
158 | SetTopWindow(frame); | |
159 | ||
24df4c19 VZ |
160 | // Init dial up manager |
161 | m_dial = wxDialUpManager::Create(); | |
162 | ||
163 | if ( !m_dial->IsOk() ) | |
164 | { | |
165 | wxLogError("The sample can't run on this system."); | |
166 | ||
167 | wxLog::GetActiveTarget()->Flush(); | |
168 | ||
169 | // do it here, OnExit() won't be called | |
170 | delete m_dial; | |
171 | ||
172 | return FALSE; | |
173 | } | |
174 | ||
2690830e VZ |
175 | frame->SetStatusText(GetDialer()->IsAlwaysOnline() ? "LAN" : "No LAN", 2); |
176 | ||
09884325 VZ |
177 | return TRUE; |
178 | } | |
179 | ||
24df4c19 VZ |
180 | int MyApp::OnExit() |
181 | { | |
182 | delete m_dial; | |
183 | ||
184 | // exit code is 0, everything is ok | |
185 | return 0; | |
186 | } | |
187 | ||
09884325 VZ |
188 | void MyApp::OnConnected(wxDialUpEvent& event) |
189 | { | |
24df4c19 VZ |
190 | const char *msg; |
191 | if ( event.IsOwnEvent() ) | |
192 | { | |
193 | msg = event.IsConnectedEvent() ? "Successfully connected" | |
194 | : "Dialing failed"; | |
195 | ||
196 | wxLogStatus(""); | |
197 | } | |
198 | else | |
199 | { | |
200 | msg = event.IsConnectedEvent() ? "Just connected!" | |
201 | : "Disconnected"; | |
202 | } | |
203 | ||
2690830e | 204 | wxLogMessage(msg); |
09884325 VZ |
205 | } |
206 | ||
207 | // ---------------------------------------------------------------------------- | |
208 | // main frame | |
209 | // ---------------------------------------------------------------------------- | |
210 | ||
211 | // frame constructor | |
212 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
213 | : wxFrame((wxFrame *)NULL, -1, title, pos, size) | |
214 | { | |
09884325 VZ |
215 | // create a menu bar |
216 | wxMenu *menuFile = new wxMenu; | |
217 | ||
218 | menuFile->Append(NetTest_Dial, "&Dial\tCtrl-D", "Dial default ISP"); | |
219 | menuFile->Append(NetTest_HangUp, "&HangUp\tCtrl-H", "Hang up modem"); | |
220 | menuFile->AppendSeparator(); | |
2690830e VZ |
221 | menuFile->Append(NetTest_EnumISP, "&Enumerate ISPs...\tCtrl-E"); |
222 | menuFile->AppendSeparator(); | |
09884325 VZ |
223 | menuFile->Append(NetTest_About, "&About...\tCtrl-A", "Show about dialog"); |
224 | menuFile->AppendSeparator(); | |
225 | menuFile->Append(NetTest_Quit, "E&xit\tAlt-X", "Quit this program"); | |
226 | ||
227 | // now append the freshly created menu to the menu bar... | |
228 | wxMenuBar *menuBar = new wxMenuBar; | |
229 | menuBar->Append(menuFile, "&File"); | |
230 | ||
231 | // ... and attach this menu bar to the frame | |
232 | SetMenuBar(menuBar); | |
233 | ||
2690830e VZ |
234 | // create status bar and fill the LAN field |
235 | CreateStatusBar(3); | |
236 | static const int widths[3] = { -1, 100, 60 }; | |
237 | SetStatusWidths(3, widths); | |
09884325 VZ |
238 | } |
239 | ||
240 | ||
241 | // event handlers | |
242 | ||
243 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
244 | { | |
245 | // TRUE is to force the frame to close | |
246 | Close(TRUE); | |
247 | } | |
248 | ||
249 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
250 | { | |
251 | wxString msg; | |
252 | msg.Printf(_T("This is the network functions test sample.\n" | |
253 |