]>
git.saurik.com Git - wxWidgets.git/blob - samples/dialup/nettest.cpp
1e837dfb9bfaa435855bb50bccc68a761c36c895
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets sample demonstrating network-related functions
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
32 // for all others, include the necessary headers (this file is usually all you
33 // need because it includes almost all "standard" wxWidgets headers
38 #if !wxUSE_DIALUP_MANAGER
39 #error You must set wxUSE_DIALUP_MANAGER to 1 in setup.h!
42 #include "wx/dialup.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // Define a new application type, each program should derive a class from wxApp
49 class MyApp
: public wxApp
52 // override base class virtuals
53 // ----------------------------
55 // this one is called on application startup and is a good place for the app
56 // initialization (doing it here and not in the ctor allows to have an error
57 // return: if OnInit() returns false, the application terminates)
58 virtual bool OnInit();
60 // called before the application termination
64 void OnConnected(wxDialUpEvent
& event
);
66 // accessor to dial up manager
67 wxDialUpManager
*GetDialer() const { return m_dial
; }
70 wxDialUpManager
*m_dial
;
75 // Define a new frame type: this is going to be our main frame
76 class MyFrame
: public wxFrame
80 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
82 // event handlers (these functions should _not_ be virtual)
83 void OnQuit(wxCommandEvent
& event
);
84 void OnAbout(wxCommandEvent
& event
);
85 void OnHangUp(wxCommandEvent
& event
);
86 void OnDial(wxCommandEvent
& event
);
87 void OnEnumISPs(wxCommandEvent
& event
);
88 void OnCheck(wxCommandEvent
& event
);
89 void OnUpdateUI(wxUpdateUIEvent
& event
);
91 void OnIdle(wxIdleEvent
& event
);
94 // any class wishing to process wxWidgets events must use this macro
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 // IDs for the controls and the menu commands
115 // ----------------------------------------------------------------------------
116 // event tables and other macros for wxWidgets
117 // ----------------------------------------------------------------------------
119 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
120 EVT_DIALUP_CONNECTED(MyApp::OnConnected
)
121 EVT_DIALUP_DISCONNECTED(MyApp::OnConnected
)
124 // the event tables connect the wxWidgets events with the functions (event
125 // handlers) which process them. It can be also done at run-time, but for the
126 // simple menu events like this the static method is much simpler.
127 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
128 EVT_MENU(NetTest_Quit
, MyFrame::OnQuit
)
129 EVT_MENU(NetTest_About
, MyFrame::OnAbout
)
130 EVT_MENU(NetTest_HangUp
, MyFrame::OnHangUp
)
131 EVT_MENU(NetTest_Dial
, MyFrame::OnDial
)
132 EVT_MENU(NetTest_EnumISP
, MyFrame::OnEnumISPs
)
133 EVT_MENU(NetTest_Check
, MyFrame::OnCheck
)
135 EVT_UPDATE_UI(NetTest_Dial
, MyFrame::OnUpdateUI
)
137 EVT_IDLE(MyFrame::OnIdle
)
140 // Create a new application object: this macro will allow wxWidgets to create
141 // the application object during program execution (it's better than using a
142 // static object for many reasons) and also declares the accessor function
143 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
147 // ============================================================================
149 // ============================================================================
151 // ----------------------------------------------------------------------------
152 // the application class
153 // ----------------------------------------------------------------------------
155 // `Main program' equivalent: the program execution "starts" here
158 // Create the main application window
159 MyFrame
*frame
= new MyFrame(_T("Dial-up wxWidgets demo"),
160 wxPoint(50, 50), wxSize(450, 340));
162 // Show it and tell the application that it's our main window
166 // Init dial up manager
167 m_dial
= wxDialUpManager::Create();
169 if ( !m_dial
->IsOk() )
171 wxLogError(wxT("The sample can't run on this system."));
174 wxLog::GetActiveTarget()->Flush();
177 // do it here, OnExit() won't be called
184 frame
->SetStatusText(GetDialer()->IsAlwaysOnline() ? _T("LAN") : _T("No LAN"), 2);
185 #endif // wxUSE_STATUSBAR
194 // exit code is 0, everything is ok
198 void MyApp::OnConnected(wxDialUpEvent
& event
)
201 if ( event
.IsOwnEvent() )
203 msg
= event
.IsConnectedEvent() ? wxT("Successfully connected")
204 : wxT("Dialing failed");
206 wxLogStatus(wxEmptyString
);
210 msg
= event
.IsConnectedEvent() ? wxT("Just connected!")
211 : wxT("Disconnected");
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
222 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
223 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
226 wxMenu
*menuFile
= new wxMenu
;
228 menuFile
->Append(NetTest_Dial
, _T("&Dial\tCtrl-D"), _T("Dial default ISP"));
229 menuFile
->Append(NetTest_HangUp
, _T("&HangUp\tCtrl-H"), _T("Hang up modem"));
230 menuFile
->AppendSeparator();
231 menuFile
->Append(NetTest_EnumISP
, _T("&Enumerate ISPs...\tCtrl-E"));
232 menuFile
->Append(NetTest_Check
, _T("&Check connection status...\tCtrl-C"));
233 menuFile
->AppendSeparator();
234 menuFile
->Append(NetTest_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
235 menuFile
->AppendSeparator();
236 menuFile
->Append(NetTest_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
238 // now append the freshly created menu to the menu bar...
239 wxMenuBar
*menuBar
= new wxMenuBar
;
240 menuBar
->Append(menuFile
, _T("&File"));
242 // ... and attach this menu bar to the frame
246 // create status bar and fill the LAN field
248 static const int widths
[3] = { -1, 100, 60 };
249 SetStatusWidths(3, widths
);
250 #endif // wxUSE_STATUSBAR
256 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
258 // true is to force the frame to close
262 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
265 msg
.Printf( wxT("This is the network functions test sample.\n")
266 wxT("(c) 1999 Vadim Zeitlin") );
268 wxMessageBox(msg
, wxT("About NetTest"), wxOK
| wxICON_INFORMATION
, this);
271 void MyFrame::OnHangUp(wxCommandEvent
& WXUNUSED(event
))
273 if ( wxGetApp().GetDialer()->HangUp() )
275 wxLogStatus(this, wxT("Connection was successfully terminated."));
279 wxLogStatus(this, wxT("Failed to hang up."));
283 void MyFrame::OnDial(wxCommandEvent
& WXUNUSED(event
))
285 wxLogStatus(this, wxT("Preparing to dial..."));
289 if ( wxGetApp().GetDialer()->Dial() )
291 wxLogStatus(this, wxT("Dialing..."));
295 wxLogStatus(this, wxT("Dialing attempt failed."));
301 void MyFrame::OnCheck(wxCommandEvent
& WXUNUSED(event
))
303 if(wxGetApp().GetDialer()->IsOnline())
304 wxLogMessage(wxT("Network is online."));
306 wxLogMessage(wxT("Network is offline."));
309 void MyFrame::OnEnumISPs(wxCommandEvent
& WXUNUSED(event
))
312 size_t nCount
= wxGetApp().GetDialer()->GetISPNames(names
);
315 wxLogWarning(wxT("No ISPs found."));
319 wxString msg
= _T("Known ISPs:\n");
320 for ( size_t n
= 0; n
< nCount
; n
++ )
322 msg
<< names
[n
] << '\n';
329 void MyFrame::OnUpdateUI(wxUpdateUIEvent
& event
)
331 // disable this item while dialing
332 event
.Enable( !wxGetApp().GetDialer()->IsDialing() );
335 void MyFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
))
337 static int s_isOnline
= -1; // not true nor false
339 bool isOnline
= wxGetApp().GetDialer()->IsOnline();
340 if ( s_isOnline
!= (int)isOnline
)
342 s_isOnline
= isOnline
;
345 SetStatusText(isOnline
? _T("Online") : _T("Offline"), 1);
346 #endif // wxUSE_STATUSBAR