]>
git.saurik.com Git - wxWidgets.git/blob - samples/dialup/nettest.cpp
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 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers
33 #if !wxUSE_DIALUP_MANAGER
34 #error You must set wxUSE_DIALUP_MANAGER to 1 in setup.h!
37 #include "wx/dialup.h"
39 #ifndef wxHAS_IMAGES_IN_RESOURCES
40 #include "../sample.xpm"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // Define a new application type, each program should derive a class from wxApp
48 class MyApp
: public wxApp
51 // override base class virtuals
52 // ----------------------------
54 // this one is called on application startup and is a good place for the app
55 // initialization (doing it here and not in the ctor allows to have an error
56 // return: if OnInit() returns false, the application terminates)
57 virtual bool OnInit();
59 // called before the application termination
63 void OnConnected(wxDialUpEvent
& event
);
65 // accessor to dial up manager
66 wxDialUpManager
*GetDialer() const { return m_dial
; }
69 wxDialUpManager
*m_dial
;
74 // Define a new frame type: this is going to be our main frame
75 class MyFrame
: public wxFrame
79 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
81 // event handlers (these functions should _not_ be virtual)
82 void OnQuit(wxCommandEvent
& event
);
83 void OnAbout(wxCommandEvent
& event
);
84 void OnHangUp(wxCommandEvent
& event
);
85 void OnDial(wxCommandEvent
& event
);
86 void OnEnumISPs(wxCommandEvent
& event
);
87 void OnCheck(wxCommandEvent
& event
);
88 void OnUpdateUI(wxUpdateUIEvent
& event
);
90 void OnIdle(wxIdleEvent
& event
);
93 // any class wishing to process wxWidgets events must use this macro
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 // IDs for the controls and the menu commands
114 // ----------------------------------------------------------------------------
115 // event tables and other macros for wxWidgets
116 // ----------------------------------------------------------------------------
118 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
119 EVT_DIALUP_CONNECTED(MyApp::OnConnected
)
120 EVT_DIALUP_DISCONNECTED(MyApp::OnConnected
)
123 // the event tables connect the wxWidgets events with the functions (event
124 // handlers) which process them. It can be also done at run-time, but for the
125 // simple menu events like this the static method is much simpler.
126 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
127 EVT_MENU(NetTest_Quit
, MyFrame::OnQuit
)
128 EVT_MENU(NetTest_About
, MyFrame::OnAbout
)
129 EVT_MENU(NetTest_HangUp
, MyFrame::OnHangUp
)
130 EVT_MENU(NetTest_Dial
, MyFrame::OnDial
)
131 EVT_MENU(NetTest_EnumISP
, MyFrame::OnEnumISPs
)
132 EVT_MENU(NetTest_Check
, MyFrame::OnCheck
)
134 EVT_UPDATE_UI(NetTest_Dial
, MyFrame::OnUpdateUI
)
136 EVT_IDLE(MyFrame::OnIdle
)
139 // Create a new application object: this macro will allow wxWidgets to create
140 // the application object during program execution (it's better than using a
141 // static object for many reasons) and also declares the accessor function
142 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
146 // ============================================================================
148 // ============================================================================
150 // ----------------------------------------------------------------------------
151 // the application class
152 // ----------------------------------------------------------------------------
154 // `Main program' equivalent: the program execution "starts" here
157 if ( !wxApp::OnInit() )
160 // Create the main application window
161 MyFrame
*frame
= new MyFrame(wxT("Dial-up wxWidgets demo"),
162 wxPoint(50, 50), wxSize(450, 340));
167 // Init dial up manager
168 m_dial
= wxDialUpManager::Create();
170 if ( !m_dial
->IsOk() )
172 wxLogError(wxT("The sample can't run on this system."));
175 wxLog::GetActiveTarget()->Flush();
178 // do it here, OnExit() won't be called
185 frame
->SetStatusText(GetDialer()->IsAlwaysOnline() ? wxT("LAN") : wxT("No LAN"), 2);
186 #endif // wxUSE_STATUSBAR
195 // exit code is 0, everything is ok
199 void MyApp::OnConnected(wxDialUpEvent
& event
)
202 if ( event
.IsOwnEvent() )
204 msg
= event
.IsConnectedEvent() ? wxT("Successfully connected")
205 : wxT("Dialing failed");
207 wxLogStatus(wxEmptyString
);
211 msg
= event
.IsConnectedEvent() ? wxT("Just connected!")
212 : wxT("Disconnected");
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
223 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
224 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
226 SetIcon(wxICON(sample
));
229 wxMenu
*menuFile
= new wxMenu
;
231 menuFile
->Append(NetTest_Dial
, wxT("&Dial\tCtrl-D"), wxT("Dial default ISP"));
232 menuFile
->Append(NetTest_HangUp
, wxT("&HangUp\tCtrl-H"), wxT("Hang up modem"));
233 menuFile
->AppendSeparator();
234 menuFile
->Append(NetTest_EnumISP
, wxT("&Enumerate ISPs...\tCtrl-E"));
235 menuFile
->Append(NetTest_Check
, wxT("&Check connection status...\tCtrl-C"));
236 menuFile
->AppendSeparator();
237 menuFile
->Append(NetTest_About
, wxT("&About\tCtrl-A"), wxT("Show about dialog"));
238 menuFile
->AppendSeparator();
239 menuFile
->Append(NetTest_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
241 // now append the freshly created menu to the menu bar...
242 wxMenuBar
*menuBar
= new wxMenuBar
;
243 menuBar
->Append(menuFile
, wxT("&File"));
245 // ... and attach this menu bar to the frame
249 // create status bar and fill the LAN field
251 static const int widths
[3] = { -1, 100, 60 };
252 SetStatusWidths(3, widths
);
253 #endif // wxUSE_STATUSBAR
259 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
261 // true is to force the frame to close
265 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
268 msg
.Printf( wxT("This is the network functions test sample.\n")
269 wxT("(c) 1999 Vadim Zeitlin") );
271 wxMessageBox(msg
, wxT("About NetTest"), wxOK
| wxICON_INFORMATION
, this);
274 void MyFrame::OnHangUp(wxCommandEvent
& WXUNUSED(event
))
276 if ( wxGetApp().GetDialer()->HangUp() )
278 wxLogStatus(this, wxT("Connection was successfully terminated."));
282 wxLogStatus(this, wxT("Failed to hang up."));
286 void MyFrame::OnDial(wxCommandEvent
& WXUNUSED(event
))
288 wxLogStatus(this, wxT("Preparing to dial..."));
292 if ( wxGetApp().GetDialer()->Dial() )
294 wxLogStatus(this, wxT("Dialing..."));
298 wxLogStatus(this, wxT("Dialing attempt failed."));
304 void MyFrame::OnCheck(wxCommandEvent
& WXUNUSED(event
))
306 if(wxGetApp().GetDialer()->IsOnline())
308 wxLogMessage(wxT("Network is online."));
312 wxLogMessage(wxT("Network is offline."));
316 void MyFrame::OnEnumISPs(wxCommandEvent
& WXUNUSED(event
))
319 size_t nCount
= wxGetApp().GetDialer()->GetISPNames(names
);
322 wxLogWarning(wxT("No ISPs found."));
326 wxString msg
= wxT("Known ISPs:\n");
327 for ( size_t n
= 0; n
< nCount
; n
++ )
329 msg
<< names
[n
] << '\n';
336 void MyFrame::OnUpdateUI(wxUpdateUIEvent
& event
)
338 // disable this item while dialing
339 event
.Enable( !wxGetApp().GetDialer()->IsDialing() );
342 void MyFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
))
344 static int s_isOnline
= -1; // not true nor false
346 bool isOnline
= wxGetApp().GetDialer()->IsOnline();
347 if ( s_isOnline
!= (int)isOnline
)
349 s_isOnline
= isOnline
;
352 SetStatusText(isOnline
? wxT("Online") : wxT("Offline"), 1);
353 #endif // wxUSE_STATUSBAR