]>
git.saurik.com Git - wxWidgets.git/blob - samples/dialup/nettest.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets sample demonstrating network-related functions
4 // Author: Vadim Zeitlin
7 // Copyright: (c) Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers (this file is usually all you
27 // need because it includes almost all "standard" wxWidgets headers
32 #if !wxUSE_DIALUP_MANAGER
33 #error You must set wxUSE_DIALUP_MANAGER to 1 in setup.h!
36 #include "wx/dialup.h"
38 #ifndef wxHAS_IMAGES_IN_RESOURCES
39 #include "../sample.xpm"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // Define a new application type, each program should derive a class from wxApp
47 class MyApp
: public wxApp
50 // override base class virtuals
51 // ----------------------------
53 // this one is called on application startup and is a good place for the app
54 // initialization (doing it here and not in the ctor allows to have an error
55 // return: if OnInit() returns false, the application terminates)
56 virtual bool OnInit();
58 // called before the application termination
62 void OnConnected(wxDialUpEvent
& event
);
64 // accessor to dial up manager
65 wxDialUpManager
*GetDialer() const { return m_dial
; }
68 wxDialUpManager
*m_dial
;
73 // Define a new frame type: this is going to be our main frame
74 class MyFrame
: public wxFrame
78 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
80 // event handlers (these functions should _not_ be virtual)
81 void OnQuit(wxCommandEvent
& event
);
82 void OnAbout(wxCommandEvent
& event
);
83 void OnHangUp(wxCommandEvent
& event
);
84 void OnDial(wxCommandEvent
& event
);
85 void OnEnumISPs(wxCommandEvent
& event
);
86 void OnCheck(wxCommandEvent
& event
);
87 void OnUpdateUI(wxUpdateUIEvent
& event
);
89 void OnIdle(wxIdleEvent
& event
);
92 // any class wishing to process wxWidgets events must use this macro
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 // IDs for the controls and the menu commands
113 // ----------------------------------------------------------------------------
114 // event tables and other macros for wxWidgets
115 // ----------------------------------------------------------------------------
117 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
118 EVT_DIALUP_CONNECTED(MyApp::OnConnected
)
119 EVT_DIALUP_DISCONNECTED(MyApp::OnConnected
)
122 // the event tables connect the wxWidgets events with the functions (event
123 // handlers) which process them. It can be also done at run-time, but for the
124 // simple menu events like this the static method is much simpler.
125 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
126 EVT_MENU(NetTest_Quit
, MyFrame::OnQuit
)
127 EVT_MENU(NetTest_About
, MyFrame::OnAbout
)
128 EVT_MENU(NetTest_HangUp
, MyFrame::OnHangUp
)
129 EVT_MENU(NetTest_Dial
, MyFrame::OnDial
)
130 EVT_MENU(NetTest_EnumISP
, MyFrame::OnEnumISPs
)
131 EVT_MENU(NetTest_Check
, MyFrame::OnCheck
)
133 EVT_UPDATE_UI(NetTest_Dial
, MyFrame::OnUpdateUI
)
135 EVT_IDLE(MyFrame::OnIdle
)
138 // Create a new application object: this macro will allow wxWidgets to create
139 // the application object during program execution (it's better than using a
140 // static object for many reasons) and also declares the accessor function
141 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
145 // ============================================================================
147 // ============================================================================
149 // ----------------------------------------------------------------------------
150 // the application class
151 // ----------------------------------------------------------------------------
153 // `Main program' equivalent: the program execution "starts" here
156 if ( !wxApp::OnInit() )
159 // Create the main application window
160 MyFrame
*frame
= new MyFrame(wxT("Dial-up wxWidgets demo"),
161 wxPoint(50, 50), wxSize(450, 340));
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() ? wxT("LAN") : wxT("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
)
225 SetIcon(wxICON(sample
));
228 wxMenu
*menuFile
= new wxMenu
;
230 menuFile
->Append(NetTest_Dial
, wxT("&Dial\tCtrl-D"), wxT("Dial default ISP"));
231 menuFile
->Append(NetTest_HangUp
, wxT("&HangUp\tCtrl-H"), wxT("Hang up modem"));
232 menuFile
->AppendSeparator();
233 menuFile
->Append(NetTest_EnumISP
, wxT("&Enumerate ISPs...\tCtrl-E"));
234 menuFile
->Append(NetTest_Check
, wxT("&Check connection status...\tCtrl-C"));
235 menuFile
->AppendSeparator();
236 menuFile
->Append(NetTest_About
, wxT("&About\tCtrl-A"), wxT("Show about dialog"));
237 menuFile
->AppendSeparator();
238 menuFile
->Append(NetTest_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
240 // now append the freshly created menu to the menu bar...
241 wxMenuBar
*menuBar
= new wxMenuBar
;
242 menuBar
->Append(menuFile
, wxT("&File"));
244 // ... and attach this menu bar to the frame
248 // create status bar and fill the LAN field
250 static const int widths
[3] = { -1, 100, 60 };
251 SetStatusWidths(3, widths
);
252 #endif // wxUSE_STATUSBAR
258 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
260 // true is to force the frame to close
264 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
267 msg
.Printf( wxT("This is the network functions test sample.\n")
268 wxT("(c) 1999 Vadim Zeitlin") );
270 wxMessageBox(msg
, wxT("About NetTest"), wxOK
| wxICON_INFORMATION
, this);
273 void MyFrame::OnHangUp(wxCommandEvent
& WXUNUSED(event
))
275 if ( wxGetApp().GetDialer()->HangUp() )
277 wxLogStatus(this, wxT("Connection was successfully terminated."));
281 wxLogStatus(this, wxT("Failed to hang up."));
285 void MyFrame::OnDial(wxCommandEvent
& WXUNUSED(event
))
287 wxLogStatus(this, wxT("Preparing to dial..."));
291 if ( wxGetApp().GetDialer()->Dial() )
293 wxLogStatus(this, wxT("Dialing..."));
297 wxLogStatus(this, wxT("Dialing attempt failed."));
303 void MyFrame::OnCheck(wxCommandEvent
& WXUNUSED(event
))
305 if(wxGetApp().GetDialer()->IsOnline())
307 wxLogMessage(wxT("Network is online."));
311 wxLogMessage(wxT("Network is offline."));
315 void MyFrame::OnEnumISPs(wxCommandEvent
& WXUNUSED(event
))
318 size_t nCount
= wxGetApp().GetDialer()->GetISPNames(names
);
321 wxLogWarning(wxT("No ISPs found."));
325 wxString msg
= wxT("Known ISPs:\n");
326 for ( size_t n
= 0; n
< nCount
; n
++ )
328 msg
<< names
[n
] << '\n';
335 void MyFrame::OnUpdateUI(wxUpdateUIEvent
& event
)
337 // disable this item while dialing
338 event
.Enable( !wxGetApp().GetDialer()->IsDialing() );
341 void MyFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
))
343 static int s_isOnline
= -1; // not true nor false
345 bool isOnline
= wxGetApp().GetDialer()->IsOnline();
346 if ( s_isOnline
!= (int)isOnline
)
348 s_isOnline
= isOnline
;
351 SetStatusText(isOnline
? wxT("Online") : wxT("Offline"), 1);
352 #endif // wxUSE_STATUSBAR