]> git.saurik.com Git - wxWidgets.git/blame - samples/ipc/client.cpp
should have been part of r74664: Avoid calling gtk_window_get_position() from "config...
[wxWidgets.git] / samples / ipc / client.cpp
CommitLineData
7921cf2b
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: client.cpp
3// Purpose: DDE sample: client
4// Author: Julian Smart
9d860992 5// Modified by: Jurgen Doornik
7921cf2b 6// Created: 25/01/99
7921cf2b
JS
7// Copyright: (c) Julian Smart
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
4b89c618
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
7921cf2b
JS
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
4b89c618 23 #pragma hdrstop
7921cf2b
JS
24#endif
25
26#ifndef WX_PRECOMP
4b89c618 27 #include "wx/wx.h"
7921cf2b
JS
28#endif
29
30// Settings common to both executables: determines whether
31// we're using TCP/IP or real DDE.
9d860992 32#include "ipcsetup.h"
7921cf2b 33
9d860992 34#include "wx/datetime.h"
7921cf2b
JS
35#include "client.h"
36
e7092398 37#ifndef wxHAS_IMAGES_IN_RESOURCES
3cb332c1
VZ
38 #include "../sample.xpm"
39#endif
40
4b89c618
VZ
41// ----------------------------------------------------------------------------
42// wxWin macros
43// ----------------------------------------------------------------------------
7921cf2b
JS
44
45IMPLEMENT_APP(MyApp)
46
4b89c618 47BEGIN_EVENT_TABLE(MyFrame, wxFrame)
9d860992
JS
48 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
49 EVT_CLOSE( MyFrame::OnClose )
50 EVT_BUTTON( ID_START, MyFrame::OnStart )
51 EVT_CHOICE( ID_SERVERNAME, MyFrame::OnServername )
52 EVT_CHOICE( ID_HOSTNAME, MyFrame::OnHostname )
53 EVT_CHOICE( ID_TOPIC, MyFrame::OnTopic )
54 EVT_BUTTON( ID_DISCONNECT, MyFrame::OnDisconnect )
55 EVT_BUTTON( ID_STARTADVISE, MyFrame::OnStartAdvise )
56 EVT_BUTTON( ID_STOPADVISE, MyFrame::OnStopAdvise )
57 EVT_BUTTON( ID_POKE, MyFrame::OnPoke )
58 EVT_BUTTON( ID_EXECUTE, MyFrame::OnExecute )
59 EVT_BUTTON( ID_REQUEST, MyFrame::OnRequest )
4b89c618
VZ
60END_EVENT_TABLE()
61
62// ----------------------------------------------------------------------------
63// globals
64// ----------------------------------------------------------------------------
65
4b89c618
VZ
66// ============================================================================
67// implementation
68// ============================================================================
69
70// ----------------------------------------------------------------------------
71// MyApp
72// ----------------------------------------------------------------------------
73
7921cf2b
JS
74// The `main program' equivalent, creating the windows and returning the
75// main frame
76bool MyApp::OnInit()
77{
45e6e6f8
VZ
78 if ( !wxApp::OnInit() )
79 return false;
80
4b89c618 81 // Create the main frame window
9a83f860 82 m_frame = new MyFrame(NULL, wxT("Client"));
9d860992 83 m_frame->Show(true);
b178e0c7 84
b62ca03d 85 return true;
7921cf2b
JS
86}
87
88int MyApp::OnExit()
89{
2f6c54eb 90
7921cf2b
JS
91 return 0;
92}
93
7921cf2b 94// Define my frame constructor
4b89c618 95MyFrame::MyFrame(wxFrame *frame, const wxString& title)
9d860992 96 : wxFrame(frame, wxID_ANY, title, wxDefaultPosition, wxSize(400, 300))
7921cf2b 97{
4b89c618 98 // Give it an icon
3cb332c1 99 SetIcon(wxICON(sample));
4b89c618
VZ
100
101 // Make a menubar
102 wxMenu *file_menu = new wxMenu;
103
9a83f860 104 file_menu->Append(wxID_EXIT, wxT("&Quit\tCtrl-Q"));
4b89c618
VZ
105
106 wxMenuBar *menu_bar = new wxMenuBar;
107
9a83f860 108 menu_bar->Append(file_menu, wxT("&File"));
4b89c618
VZ
109
110 // Associate the menu bar with the frame
111 SetMenuBar(menu_bar);
112
9d860992
JS
113 // set a dialog background
114 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
115
116 // add the controls to the frame
81ba6107 117 wxString strs4[] =
9d860992 118 {
9a83f860 119 IPC_SERVICE, wxT("...")
9d860992 120 };
81ba6107 121 wxString strs5[] =
9d860992 122 {
9a83f860 123 IPC_HOST, wxT("...")
9d860992 124 };
81ba6107 125 wxString strs6[] =
9d860992 126 {
9a83f860 127 IPC_TOPIC, wxT("...")
9d860992
JS
128 };
129
130 wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
131
132 wxBoxSizer *item1 = new wxBoxSizer( wxHORIZONTAL );
133
134 wxGridSizer *item2 = new wxGridSizer( 4, 0, 0 );
135
136 wxButton *item3 = new wxButton( this, ID_START, wxT("Connect to server"), wxDefaultPosition, wxDefaultSize, 0 );
137 item2->Add( item3, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
138
139 wxChoice *item5 = new wxChoice( this, ID_HOSTNAME, wxDefaultPosition, wxSize(100,-1), 2, strs5, 0 );
140 item2->Add( item5, 0, wxALIGN_CENTER|wxALL, 5 );
141
142 wxChoice *item4 = new wxChoice( this, ID_SERVERNAME, wxDefaultPosition, wxSize(100,-1), 2, strs4, 0 );
143 item2->Add( item4, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
144
145 wxChoice *item6 = new wxChoice( this, ID_TOPIC, wxDefaultPosition, wxSize(100,-1), 2, strs6, 0 );
146 item2->Add( item6, 0, wxALIGN_CENTER|wxALL, 5 );
147
148 wxButton *item7 = new wxButton( this, ID_DISCONNECT, wxT("Disconnect "), wxDefaultPosition, wxDefaultSize, 0 );
149 item2->Add( item7, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
150
151 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
152
153 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
154
155 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
156
157 wxButton *item8 = new wxButton( this, ID_STARTADVISE, wxT("StartAdvise"), wxDefaultPosition, wxDefaultSize, 0 );
158 item2->Add( item8, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
159
160 wxButton *item9 = new wxButton( this, ID_STOPADVISE, wxT("StopAdvise"), wxDefaultPosition, wxDefaultSize, 0 );
161 item2->Add( item9, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
162
163 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
164
165 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
166
167 wxButton *item10 = new wxButton( this, ID_EXECUTE, wxT("Execute"), wxDefaultPosition, wxDefaultSize, 0 );
168 item2->Add( item10, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
169
170 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
171
172 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
173
174 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
175
176 wxButton *item11 = new wxButton( this, ID_POKE, wxT("Poke"), wxDefaultPosition, wxDefaultSize, 0 );
177 item2->Add( item11, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
178
179 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
180
181 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
182
183 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
184
185 wxButton *item12 = new wxButton( this, ID_REQUEST, wxT("Request"), wxDefaultPosition, wxDefaultSize, 0 );
186 item2->Add( item12, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
187
188 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
189
190 item1->Add( item2, 1, wxALIGN_CENTER|wxALL, 5 );
191
192 item0->Add( item1, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
193
194 wxStaticBox *item14 = new wxStaticBox( this, -1, wxT("Client log") );
195 wxStaticBoxSizer *item13 = new wxStaticBoxSizer( item14, wxVERTICAL );
196
81ba6107 197 wxTextCtrl *item15 = new wxTextCtrl( this, ID_LOG, wxEmptyString, wxDefaultPosition, wxSize(500,140), wxTE_MULTILINE );
9d860992
JS
198 item13->Add( item15, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
199
200 item0->Add( item13, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
201
202 this->SetSizer( item0 );
203 item0->SetSizeHints( this );
204
205 // status
206 m_client = NULL;
207 GetServername()->SetSelection(0);
208 GetHostname()->SetSelection(0);
209 GetTopic()->SetSelection(0);
210 wxLogTextCtrl *logWindow = new wxLogTextCtrl(GetLog());
211 delete wxLog::SetActiveTarget(logWindow);
9a83f860 212 wxLogMessage(wxT("Click on Connect to connect to the server"));
81ba6107 213 EnableControls();
9d860992
JS
214}
215
81ba6107 216void MyFrame::EnableControls()
9d860992
JS
217{
218 GetStart()->Enable(m_client == NULL);
219 GetServername()->Enable(m_client == NULL);
220 GetHostname()->Enable(m_client == NULL);
221 GetTopic()->Enable(m_client == NULL);
b21348b4 222
5e530f02 223 const bool isConnected = (m_client != NULL && m_client->IsConnected());
b21348b4
VZ
224 GetDisconnect()->Enable(m_client != NULL && isConnected);
225 GetStartAdvise()->Enable(m_client != NULL && isConnected);
226 GetStopAdvise()->Enable(m_client != NULL && isConnected);
227 GetExecute()->Enable(m_client != NULL && isConnected);
228 GetPoke()->Enable(m_client != NULL && isConnected);
229 GetRequest()->Enable(m_client != NULL && isConnected);
9d860992
JS
230}
231
232void MyFrame::OnClose(wxCloseEvent& event)
233{
6e91bf4d
VZ
234 wxDELETE(m_client);
235
9d860992
JS
236 event.Skip();
237}
238
239void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
240{
241 Close();
242}
243
244void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event))
245{
246 // Connect to the client
247 wxString servername = GetServername()->GetStringSelection();
248 wxString hostname = GetHostname()->GetStringSelection();
249 wxString topic = GetTopic()->GetStringSelection();
250
251 m_client = new MyClient;
252 bool retval = m_client->Connect(hostname, servername, topic);
253
9a83f860 254 wxLogMessage(wxT("Client host=\"%s\" port=\"%s\" topic=\"%s\" %s"),
9d860992 255 hostname.c_str(), servername.c_str(), topic.c_str(),
9a83f860 256 retval ? wxT("connected") : wxT("failed to connect"));
9d860992
JS
257
258 if (!retval)
259 {
5276b0a5 260 wxDELETE(m_client);
9d860992 261 }
5e530f02 262 EnableControls();
9d860992
JS
263}
264
265void MyFrame::OnServername( wxCommandEvent& WXUNUSED(event) )
266{
9a83f860 267 if (GetServername()->GetStringSelection() == wxT("..."))
9d860992 268 {
9a83f860
VZ
269 wxString s = wxGetTextFromUser(wxT("Specify the name of the server"),
270 wxT("Server Name"), wxEmptyString, this);
9d860992
JS
271 if (!s.IsEmpty() && s != IPC_SERVICE)
272 {
273 GetServername()->Insert(s, 0);
274 GetServername()->SetSelection(0);
275 }
276 }
277}
278
279void MyFrame::OnHostname( wxCommandEvent& WXUNUSED(event) )
280{
9a83f860 281 if (GetHostname()->GetStringSelection() == wxT("..."))
9d860992 282 {
9a83f860
VZ
283 wxString s = wxGetTextFromUser(wxT("Specify the name of the host (ignored under DDE)"),
284 wxT("Host Name"), wxEmptyString, this);
9d860992
JS
285 if (!s.IsEmpty() && s != IPC_HOST)
286 {
287 GetHostname()->Insert(s, 0);
288 GetHostname()->SetSelection(0);
289 }
290 }
291}
292
293void MyFrame::OnTopic( wxCommandEvent& WXUNUSED(event) )
294{
9a83f860 295 if (GetTopic()->GetStringSelection() == wxT("..."))
9d860992 296 {
9a83f860
VZ
297 wxString s = wxGetTextFromUser(wxT("Specify the name of the topic"),
298 wxT("Topic Name"), wxEmptyString, this);
9d860992
JS
299 if (!s.IsEmpty() && s != IPC_TOPIC)
300 {
301 GetTopic()->Insert(s, 0);
302 GetTopic()->SetSelection(0);
303 }
304 }
305}
306
307void MyFrame::OnDisconnect(wxCommandEvent& WXUNUSED(event))
308{
81ba6107 309 Disconnect();
9d860992
JS
310}
311
312void MyFrame::Disconnect()
313{
5276b0a5 314 wxDELETE(m_client);
5e530f02 315 EnableControls();
9d860992
JS
316}
317
318void MyFrame::OnStartAdvise(wxCommandEvent& WXUNUSED(event))
319{
9a83f860 320 m_client->GetConnection()->StartAdvise(wxT("something"));
9d860992
JS
321}
322
323void MyFrame::OnStopAdvise(wxCommandEvent& WXUNUSED(event))
324{
9a83f860 325 m_client->GetConnection()->StopAdvise(wxT("something"));
7921cf2b
JS
326}
327
87728739 328void MyFrame::OnExecute(wxCommandEvent& WXUNUSED(event))
7921cf2b 329{
9d860992
JS
330 if (m_client->IsConnected())
331 {
9a83f860 332 wxString s = wxT("Date");
9d860992 333
50c549b9
VZ
334 m_client->GetConnection()->Execute(s);
335 m_client->GetConnection()->Execute((const char *)s.c_str(), s.length() + 1);
9d860992 336 char bytes[3];
521d3436
VZ
337 bytes[0] = '1';
338 bytes[1] = '2';
339 bytes[2] = '3';
340 m_client->GetConnection()->Execute(bytes, WXSIZEOF(bytes));
9d860992 341 }
7921cf2b
JS
342}
343
87728739 344void MyFrame::OnPoke(wxCommandEvent& WXUNUSED(event))
7921cf2b 345{
9d860992
JS
346 if (m_client->IsConnected())
347 {
348 wxString s = wxDateTime::Now().Format();
9a83f860
VZ
349 m_client->GetConnection()->Poke(wxT("Date"), s);
350 s = wxDateTime::Now().FormatTime() + wxT(" ") + wxDateTime::Now().FormatDate();
351 m_client->GetConnection()->Poke(wxT("Date"), (const char *)s.c_str(), s.length() + 1);
9d860992
JS
352 char bytes[3];
353 bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
9a83f860 354 m_client->GetConnection()->Poke(wxT("bytes[3]"), bytes, 3, wxIPC_PRIVATE);
9d860992 355 }
7921cf2b
JS
356}
357
87728739 358void MyFrame::OnRequest(wxCommandEvent& WXUNUSED(event))
7921cf2b 359{
9d860992 360 if (m_client->IsConnected())
4b89c618 361 {
50c549b9 362 size_t size;
9a83f860
VZ
363 m_client->GetConnection()->Request(wxT("Date"));
364 m_client->GetConnection()->Request(wxT("Date+len"), &size);
365 m_client->GetConnection()->Request(wxT("bytes[3]"), &size, wxIPC_PRIVATE);
4b89c618 366 }
7921cf2b
JS
367}
368
9d860992
JS
369// ----------------------------------------------------------------------------
370// MyClient
371// ----------------------------------------------------------------------------
372MyClient::MyClient() : wxClient()
7921cf2b 373{
9d860992
JS
374 m_connection = NULL;
375}
376
377bool MyClient::Connect(const wxString& sHost, const wxString& sService, const wxString& sTopic)
378{
379 // suppress the log messages from MakeConnection()
380 wxLogNull nolog;
81ba6107 381
9d860992
JS
382 m_connection = (MyConnection *)MakeConnection(sHost, sService, sTopic);
383 return m_connection != NULL;
7921cf2b
JS
384}
385
4b89c618 386wxConnectionBase *MyClient::OnMakeConnection()
7921cf2b 387{
4b89c618 388 return new MyConnection;
7921cf2b
JS
389}
390
9d860992
JS
391void MyClient::Disconnect()
392{
393 if (m_connection)
394 {
395 m_connection->Disconnect();
5276b0a5 396 wxDELETE(m_connection);
5e530f02 397 wxGetApp().GetFrame()->EnableControls();
9a83f860 398 wxLogMessage(wxT("Client disconnected from server"));
9d860992
JS
399 }
400}
401
402MyClient::~MyClient()
403{
404 Disconnect();
405}
406
407// ----------------------------------------------------------------------------
408// MyConnection
409// ----------------------------------------------------------------------------
410
50c549b9
VZ
411bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, const void *data,
412 size_t size, wxIPCFormat format)
9d860992 413{
9a83f860 414 Log(wxT("OnAdvise"), topic, item, data, size, format);
b62ca03d 415 return true;
7921cf2b
JS
416}
417
418bool MyConnection::OnDisconnect()
419{
9a83f860 420 wxLogMessage(wxT("OnDisconnect()"));
9d860992
JS
421 wxGetApp().GetFrame()->Disconnect();
422 return true;
423}
424
50c549b9 425bool MyConnection::DoExecute(const void *data, size_t size, wxIPCFormat format)
9d860992 426{
9a83f860 427 Log(wxT("Execute"), wxEmptyString, wxEmptyString, data, size, format);
50c549b9 428 bool retval = wxConnection::DoExecute(data, size, format);
9d860992 429 if (!retval)
43b2d5e7 430 {
9a83f860 431 wxLogMessage(wxT("Execute failed!"));
43b2d5e7 432 }
9d860992
JS
433 return retval;
434}
7921cf2b 435
50c549b9 436const void *MyConnection::Request(const wxString& item, size_t *size, wxIPCFormat format)
9d860992 437{
50c549b9 438 const void *data = wxConnection::Request(item, size, format);
9a83f860 439 Log(wxT("Request"), wxEmptyString, item, data, size ? *size : wxNO_LEN, format);
9d860992
JS
440 return data;
441}
442
50c549b9 443bool MyConnection::DoPoke(const wxString& item, const void *data, size_t size, wxIPCFormat format)
9d860992 444{
9a83f860 445 Log(wxT("Poke"), wxEmptyString, item, data, size, format);
50c549b9 446 return wxConnection::DoPoke(item, data, size, format);
7921cf2b 447}