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