]>
git.saurik.com Git - wxWidgets.git/blob - samples/nettest/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
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();
57 void OnConnected(wxDialUpEvent
& event
);
60 DECLARE_EVENT_TABLE();
63 // Define a new frame type: this is going to be our main frame
64 class MyFrame
: public wxFrame
68 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
70 // event handlers (these functions should _not_ be virtual)
71 void OnQuit(wxCommandEvent
& event
);
72 void OnAbout(wxCommandEvent
& event
);
73 void OnHangUp(wxCommandEvent
& event
);
74 void OnDial(wxCommandEvent
& event
);
76 void OnIdle(wxIdleEvent
& event
);
79 // any class wishing to process wxWindows events must use this macro
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 // IDs for the controls and the menu commands
97 // ----------------------------------------------------------------------------
98 // event tables and other macros for wxWindows
99 // ----------------------------------------------------------------------------
101 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
102 EVT_DIALUP_CONNECTED(MyApp::OnConnected
)
103 EVT_DIALUP_DISCONNECTED(MyApp::OnConnected
)
106 // the event tables connect the wxWindows events with the functions (event
107 // handlers) which process them. It can be also done at run-time, but for the
108 // simple menu events like this the static method is much simpler.
109 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
110 EVT_MENU(NetTest_Quit
, MyFrame::OnQuit
)
111 EVT_MENU(NetTest_About
, MyFrame::OnAbout
)
112 EVT_MENU(NetTest_HangUp
, MyFrame::OnHangUp
)
113 EVT_MENU(NetTest_Dial
, MyFrame::OnDial
)
115 EVT_IDLE(MyFrame::OnIdle
)
118 // Create a new application object: this macro will allow wxWindows to create
119 // the application object during program execution (it's better than using a
120 // static object for many reasons) and also declares the accessor function
121 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
125 // ============================================================================
127 // ============================================================================
129 // ----------------------------------------------------------------------------
130 // the application class
131 // ----------------------------------------------------------------------------
133 // `Main program' equivalent: the program execution "starts" here
136 // Create the main application window
137 MyFrame
*frame
= new MyFrame("Minimal wxWindows App",
138 wxPoint(50, 50), wxSize(450, 340));
140 // Show it and tell the application that it's our main window
144 // success: wxApp::OnRun() will be called which will enter the main message
145 // loop and the application will run. If we returned FALSE here, the
146 // application would exit immediately.
150 void MyApp::OnConnected(wxDialUpEvent
& event
)
152 wxMessageBox(event
.IsConnectedEvent() ? "Just connected!"
154 "Dial Up Manager Notification",
155 wxOK
| wxICON_INFORMATION
,
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
164 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
165 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
167 // set the frame icon
168 SetIcon(wxICON(mondrian
));
171 wxMenu
*menuFile
= new wxMenu
;
173 menuFile
->Append(NetTest_Dial
, "&Dial\tCtrl-D", "Dial default ISP");
174 menuFile
->Append(NetTest_HangUp
, "&HangUp\tCtrl-H", "Hang up modem");
175 menuFile
->AppendSeparator();
176 menuFile
->Append(NetTest_About
, "&About...\tCtrl-A", "Show about dialog");
177 menuFile
->AppendSeparator();
178 menuFile
->Append(NetTest_Quit
, "E&xit\tAlt-X", "Quit this program");
180 // now append the freshly created menu to the menu bar...
181 wxMenuBar
*menuBar
= new wxMenuBar
;
182 menuBar
->Append(menuFile
, "&File");
184 // ... and attach this menu bar to the frame
193 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
195 // TRUE is to force the frame to close
199 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
202 msg
.Printf(_T("This is the network functions test sample.\n"
203 "© 1999 Vadim Zeitlin"));
205 wxMessageBox(msg
, _T("About NetTest"), wxOK
| wxICON_INFORMATION
, this);
208 void MyFrame::OnHangUp(wxCommandEvent
& WXUNUSED(event
))
210 if ( wxDialUpManager::Get()->HangUp() )
212 wxLogStatus(this, "Connection was succesfully terminated.");
216 wxLogStatus(this, "Failed to hang up.");
220 void MyFrame::OnDial(wxCommandEvent
& WXUNUSED(event
))
222 wxLogStatus(this, "Dialing...");
226 if ( wxDialUpManager::Get()->Dial("Free",
229 wxLogStatus(this, "Connection was succesfully established.");
233 wxLogStatus(this, "Dialing attempt failed.");
239 void MyFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
))
241 static int s_isOnline
= -1; // not TRUE nor FALSE
243 bool isOnline
= wxDialUpManager::Get()->IsOnline();
244 if ( s_isOnline
!= (int)isOnline
)
246 s_isOnline
= isOnline
;
248 SetStatusText(isOnline
? "Online" : "Offline", 1);