]>
git.saurik.com Git - wxWidgets.git/blob - samples/dialup/nettest.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWindows sample demonstrating network-related functions
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "nettest.cpp"
22 #pragma interface "nettest.cpp"
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" wxWindows headers
38 #include "wx/dialup.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // Define a new application type, each program should derive a class from wxApp
45 class MyApp
: public wxApp
48 // override base class virtuals
49 // ----------------------------
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();
56 // called before the application termination
60 void OnConnected(wxDialUpEvent
& event
);
62 // accessor to dial up manager
63 wxDialUpManager
*GetDialer() const { return m_dial
; }
66 wxDialUpManager
*m_dial
;
71 // Define a new frame type: this is going to be our main frame
72 class MyFrame
: public wxFrame
76 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
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
);
83 void OnEnumISPs(wxCommandEvent
& event
);
84 void OnCheck(wxCommandEvent
& event
);
85 void OnUpdateUI(wxUpdateUIEvent
& event
);
87 void OnIdle(wxIdleEvent
& event
);
90 // any class wishing to process wxWindows events must use this macro
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 // IDs for the controls and the menu commands
111 // ----------------------------------------------------------------------------
112 // event tables and other macros for wxWindows
113 // ----------------------------------------------------------------------------
115 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
116 EVT_DIALUP_CONNECTED(MyApp::OnConnected
)
117 EVT_DIALUP_DISCONNECTED(MyApp::OnConnected
)
120 // the event tables connect the wxWindows events with the functions (event
121 // handlers) which process them. It can be also done at run-time, but for the
122 // simple menu events like this the static method is much simpler.
123 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
124 EVT_MENU(NetTest_Quit
, MyFrame::OnQuit
)
125 EVT_MENU(NetTest_About
, MyFrame::OnAbout
)
126 EVT_MENU(NetTest_HangUp
, MyFrame::OnHangUp
)
127 EVT_MENU(NetTest_Dial
, MyFrame::OnDial
)
128 EVT_MENU(NetTest_EnumISP
, MyFrame::OnEnumISPs
)
129 EVT_MENU(NetTest_Check
, MyFrame::OnCheck
)
131 EVT_UPDATE_UI(NetTest_Dial
, MyFrame::OnUpdateUI
)
133 EVT_IDLE(MyFrame::OnIdle
)
136 // Create a new application object: this macro will allow wxWindows to create
137 // the application object during program execution (it's better than using a
138 // static object for many reasons) and also declares the accessor function
139 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
143 // ============================================================================
145 // ============================================================================
147 // ----------------------------------------------------------------------------
148 // the application class
149 // ----------------------------------------------------------------------------
151 // `Main program' equivalent: the program execution "starts" here
154 // Create the main application window
155 MyFrame
*frame
= new MyFrame("Dial-up wxWindows demo",
156 wxPoint(50, 50), wxSize(450, 340));
158 // Show it and tell the application that it's our main window
162 // Init dial up manager
163 m_dial
= wxDialUpManager::Create();
165 if ( !m_dial
->IsOk() )
167 wxLogError(wxT("The sample can't run on this system."));
169 wxLog::GetActiveTarget()->Flush();
171 // do it here, OnExit() won't be called
177 frame
->SetStatusText(GetDialer()->IsAlwaysOnline() ? "LAN" : "No LAN", 2);
186 // exit code is 0, everything is ok
190 void MyApp::OnConnected(wxDialUpEvent
& event
)
193 if ( event
.IsOwnEvent() )
195 msg
= event
.IsConnectedEvent() ? wxT("Successfully connected")
196 : wxT("Dialing failed");
198 wxLogStatus(wxT(""));
202 msg
= event
.IsConnectedEvent() ? wxT("Just connected!")
203 : wxT("Disconnected");
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
214 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
215 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
218 wxMenu
*menuFile
= new wxMenu
;
220 menuFile
->Append(NetTest_Dial
, "&Dial\tCtrl-D", "Dial default ISP");
221 menuFile
->Append(NetTest_HangUp
, "&HangUp\tCtrl-H", "Hang up modem");
222 menuFile
->AppendSeparator();
223 menuFile
->Append(NetTest_EnumISP
, "&Enumerate ISPs...\tCtrl-E");
224 menuFile
->Append(NetTest_Check
, "&Check connection status...\tCtrl-C");
225 menuFile
->AppendSeparator();
226 menuFile
->Append(NetTest_About
, "&About...\tCtrl-A", "Show about dialog");
227 menuFile
->AppendSeparator();
228 menuFile
->Append(NetTest_Quit
, "E&xit\tAlt-X", "Quit this program");
230 // now append the freshly created menu to the menu bar...
231 wxMenuBar
*menuBar
= new wxMenuBar
;
232 menuBar
->Append(menuFile
, "&File");
234 // ... and attach this menu bar to the frame
237 // create status bar and fill the LAN field
239 static const int widths
[3] = { -1, 100, 60 };
240 SetStatusWidths(3, widths
);
246 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
248 // TRUE is to force the frame to close
252 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
255 msg
.Printf(_T("This is the network functions test sample.\n"
256 "© 1999 Vadim Zeitlin"));
258 wxMessageBox(msg
, _T("About NetTest"), wxOK
| wxICON_INFORMATION
, this);
261 void MyFrame::OnHangUp(wxCommandEvent
& WXUNUSED(event
))
263 if ( wxGetApp().GetDialer()->HangUp() )
265 wxLogStatus(this, wxT("Connection was succesfully terminated."));
269 wxLogStatus(this, wxT("Failed to hang up."));
273 void MyFrame::OnDial(wxCommandEvent
& WXUNUSED(event
))
275 wxLogStatus(this, wxT("Preparing to dial..."));
279 if ( wxGetApp().GetDialer()->Dial() )
281 wxLogStatus(this, wxT("Dialing..."));
285 wxLogStatus(this, wxT("Dialing attempt failed."));
291 void MyFrame::OnCheck(wxCommandEvent
& WXUNUSED(event
))
293 if(wxGetApp().GetDialer()->IsOnline())
294 wxLogMessage(wxT("Network is online."));
296 wxLogMessage(wxT("Network is offline."));
299 void MyFrame::OnEnumISPs(wxCommandEvent
& WXUNUSED(event
))
302 size_t nCount
= wxGetApp().GetDialer()->GetISPNames(names
);
305 wxLogWarning(wxT("No ISPs found."));
309 wxString msg
= "Known ISPs:\n";
310 for ( size_t n
= 0; n
< nCount
; n
++ )
312 msg
<< names
[n
] << '\n';
319 void MyFrame::OnUpdateUI(wxUpdateUIEvent
& event
)
321 // disable this item while dialing
322 event
.Enable( !wxGetApp().GetDialer()->IsDialing() );
325 void MyFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
))
327 static int s_isOnline
= -1; // not TRUE nor FALSE
329 bool isOnline
= wxGetApp().GetDialer()->IsOnline();
330 if ( s_isOnline
!= (int)isOnline
)
332 s_isOnline
= isOnline
;
334 SetStatusText(isOnline
? "Online" : "Offline", 1);