]> git.saurik.com Git - wxWidgets.git/blob - samples/ipc/client.cpp
Defaults in wxWinCE documented.
[wxWidgets.git] / samples / ipc / client.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: client.cpp
3 // Purpose: DDE sample: client
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 25/01/99
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/wx.h"
29 #endif
30
31 // Settings common to both executables: determines whether
32 // we're using TCP/IP or real DDE.
33 #include "ddesetup.h"
34
35 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
36 #include "mondrian.xpm"
37 #endif
38
39 #include "client.h"
40
41 // ----------------------------------------------------------------------------
42 // wxWin macros
43 // ----------------------------------------------------------------------------
44
45 IMPLEMENT_APP(MyApp)
46
47 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
48 EVT_MENU(CLIENT_QUIT, MyFrame::OnExit)
49 EVT_MENU(CLIENT_EXECUTE, MyFrame::OnExecute)
50 EVT_MENU(CLIENT_POKE, MyFrame::OnPoke)
51 EVT_MENU(CLIENT_REQUEST, MyFrame::OnRequest)
52 END_EVENT_TABLE()
53
54 // ----------------------------------------------------------------------------
55 // globals
56 // ----------------------------------------------------------------------------
57
58 wxListBox *the_list = NULL;
59
60 MyConnection *the_connection = NULL;
61 MyClient *my_client;
62
63 // ============================================================================
64 // implementation
65 // ============================================================================
66
67 // ----------------------------------------------------------------------------
68 // MyApp
69 // ----------------------------------------------------------------------------
70
71 // The `main program' equivalent, creating the windows and returning the
72 // main frame
73 bool MyApp::OnInit()
74 {
75 // service name (DDE classes) or port number (TCP/IP based classes)
76 wxString service = IPC_SERVICE;
77
78 // ignored under DDE, host name in TCP/IP based classes
79 wxString hostName = _T("localhost");
80
81 if (argc > 1)
82 service = argv[1];
83 if (argc > 2)
84 hostName = argv[2];
85
86 // Create a new client
87 my_client = new MyClient;
88
89 // suppress the log messages from MakeConnection()
90 {
91 wxLogNull nolog;
92 the_connection = (MyConnection *)
93 my_client->MakeConnection(hostName, service, IPC_TOPIC);
94
95 while ( !the_connection )
96 {
97 if ( wxMessageBox(_T("Failed to make connection to server.\nRetry?"),
98 _T("Client Demo Error"),
99 wxICON_ERROR | wxYES_NO | wxCANCEL ) != wxYES )
100 {
101 // no server
102 return false;
103 }
104
105 the_connection = (MyConnection *)my_client->MakeConnection(hostName, service, _T("IPC TEST"));
106 }
107 }
108
109 if (!the_connection->StartAdvise(IPC_ADVISE_NAME))
110 wxMessageBox(_T("StartAdvise failed"), _T("Client Demo Error"));
111
112 // Create the main frame window
113 (new MyFrame(NULL, _T("Client")))->Show(true);
114
115 return true;
116 }
117
118 int MyApp::OnExit()
119 {
120 // will delete the connection too
121 // Update: Seems it didn't delete the_connection, because there's a leak.
122 // Deletion is now explicitly done a few lines up.
123 // another Update: in fact it's because OnDisconnect should delete it, but
124 // it wasn't
125 delete my_client;
126
127
128
129 return 0;
130 }
131
132 // Define my frame constructor
133 MyFrame::MyFrame(wxFrame *frame, const wxString& title)
134 : wxFrame(frame, wxID_ANY, title, wxDefaultPosition, wxSize(300, 200))
135 {
136 // Give it an icon
137 SetIcon(wxICON(mondrian));
138
139 // Make a menubar
140 wxMenu *file_menu = new wxMenu;
141
142 file_menu->Append(CLIENT_EXECUTE, _T("&Execute\tCtrl-E"));
143 file_menu->Append(CLIENT_REQUEST, _T("&Request\tCtrl-R"));
144 file_menu->Append(CLIENT_POKE, _T("&Poke\tCtrl-P"));
145 file_menu->Append(CLIENT_QUIT, _T("&Quit\tCtrl-Q"));
146
147 wxMenuBar *menu_bar = new wxMenuBar;
148
149 menu_bar->Append(file_menu, _T("&File"));
150
151 // Associate the menu bar with the frame
152 SetMenuBar(menu_bar);
153
154 // Make a listbox which shows the choices made in the server
155 the_list = new wxListBox(this, CLIENT_LISTBOX, wxPoint(5, 5));
156 the_list->Append(_T("Apple"));
157 the_list->Append(_T("Pear"));
158 the_list->Append(_T("Orange"));
159 the_list->Append(_T("Banana"));
160 the_list->Append(_T("Fruit"));
161 }
162
163 void MyFrame::OnExecute(wxCommandEvent& WXUNUSED(event))
164 {
165 if (the_connection)
166 if (!the_connection->Execute(_T("Hello from the client!")))
167 wxMessageBox(_T("Execute failed"), _T("Client Demo Error"));
168 }
169
170 void MyFrame::OnPoke(wxCommandEvent& WXUNUSED(event))
171 {
172 if (the_connection)
173 if (!the_connection->Poke(_T("An item"), _T("Some data to poke at the server!")))
174 wxMessageBox(_T("Poke failed"), _T("Client Demo Error"));
175 }
176
177 void MyFrame::OnRequest(wxCommandEvent& WXUNUSED(event))
178 {
179 if (the_connection)
180 {
181 wxChar *data = the_connection->Request(_T("An item"));
182 if (data)
183 wxMessageBox(data, _T("Client: Request"), wxOK);
184 else
185 wxMessageBox(_T("Request failed"), _T("Client Demo Error"));
186 }
187 }
188
189 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
190 {
191 Close();
192 }
193
194 wxConnectionBase *MyClient::OnMakeConnection()
195 {
196 return new MyConnection;
197 }
198
199 bool MyConnection::OnAdvise(const wxString& WXUNUSED(topic), const wxString& WXUNUSED(item), wxChar *data, int WXUNUSED(size), wxIPCFormat WXUNUSED(format))
200 {
201 if (the_list)
202 {
203 int n = the_list->FindString(data);
204 if (n > wxNOT_FOUND)
205 the_list->SetSelection(n);
206 }
207 return true;
208 }
209
210 bool MyConnection::OnDisconnect()
211 {
212 // when connection is terminated, quit whole program
213 wxWindow *win = wxTheApp->GetTopWindow();
214 if ( win )
215 win->Destroy();
216
217 // delete self
218 the_connection = NULL;
219 return wxConnection::OnDisconnect();
220 }
221