]> git.saurik.com Git - wxWidgets.git/blame - samples/ipc/client.cpp
added wxUSE_COMBOBCONTROL and wxUSE_OWNERDRAWNCOMBOBOX to wx/setup.h files
[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{
4b89c618 79 // Create the main frame window
9d860992
JS
80 m_frame = new MyFrame(NULL, _T("Client"));
81 m_frame->Show(true);
b178e0c7 82
b62ca03d 83 return true;
7921cf2b
JS
84}
85
86int MyApp::OnExit()
87{
2f6c54eb 88
7921cf2b
JS
89 return 0;
90}
91
7921cf2b 92// Define my frame constructor
4b89c618 93MyFrame::MyFrame(wxFrame *frame, const wxString& title)
9d860992 94 : wxFrame(frame, wxID_ANY, title, wxDefaultPosition, wxSize(400, 300))
7921cf2b 95{
4b89c618
VZ
96 // Give it an icon
97 SetIcon(wxICON(mondrian));
98
99 // Make a menubar
100 wxMenu *file_menu = new wxMenu;
101
9d860992 102 file_menu->Append(wxID_EXIT, _T("&Quit\tCtrl-Q"));
4b89c618
VZ
103
104 wxMenuBar *menu_bar = new wxMenuBar;
105
600683ca 106 menu_bar->Append(file_menu, _T("&File"));
4b89c618
VZ
107
108 // Associate the menu bar with the frame
109 SetMenuBar(menu_bar);
110
9d860992
JS
111 // set a dialog background
112 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
113
114 // add the controls to the frame
81ba6107 115 wxString strs4[] =
9d860992
JS
116 {
117 IPC_SERVICE, _T("...")
118 };
81ba6107 119 wxString strs5[] =
9d860992
JS
120 {
121 IPC_HOST, _T("...")
122 };
81ba6107 123 wxString strs6[] =
9d860992
JS
124 {
125 IPC_TOPIC, _T("...")
126 };
127
128 wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
129
130 wxBoxSizer *item1 = new wxBoxSizer( wxHORIZONTAL );
131
132 wxGridSizer *item2 = new wxGridSizer( 4, 0, 0 );
133
134 wxButton *item3 = new wxButton( this, ID_START, wxT("Connect to server"), wxDefaultPosition, wxDefaultSize, 0 );
135 item2->Add( item3, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
136
137 wxChoice *item5 = new wxChoice( this, ID_HOSTNAME, wxDefaultPosition, wxSize(100,-1), 2, strs5, 0 );
138 item2->Add( item5, 0, wxALIGN_CENTER|wxALL, 5 );
139
140 wxChoice *item4 = new wxChoice( this, ID_SERVERNAME, wxDefaultPosition, wxSize(100,-1), 2, strs4, 0 );
141 item2->Add( item4, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
142
143 wxChoice *item6 = new wxChoice( this, ID_TOPIC, wxDefaultPosition, wxSize(100,-1), 2, strs6, 0 );
144 item2->Add( item6, 0, wxALIGN_CENTER|wxALL, 5 );
145
146 wxButton *item7 = new wxButton( this, ID_DISCONNECT, wxT("Disconnect "), wxDefaultPosition, wxDefaultSize, 0 );
147 item2->Add( item7, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
148
149 item2->Add( 20, 20, 0, wxALIGN_CENTER|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 wxButton *item8 = new wxButton( this, ID_STARTADVISE, wxT("StartAdvise"), wxDefaultPosition, wxDefaultSize, 0 );
156 item2->Add( item8, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
157
158 wxButton *item9 = new wxButton( this, ID_STOPADVISE, wxT("StopAdvise"), wxDefaultPosition, wxDefaultSize, 0 );
159 item2->Add( item9, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
160
161 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
162
163 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
164
165 wxButton *item10 = new wxButton( this, ID_EXECUTE, wxT("Execute"), wxDefaultPosition, wxDefaultSize, 0 );
166 item2->Add( item10, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
167
168 item2->Add( 20, 20, 0, wxALIGN_CENTER|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 wxButton *item11 = new wxButton( this, ID_POKE, wxT("Poke"), wxDefaultPosition, wxDefaultSize, 0 );
175 item2->Add( item11, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
176
177 item2->Add( 20, 20, 0, wxALIGN_CENTER|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 wxButton *item12 = new wxButton( this, ID_REQUEST, wxT("Request"), wxDefaultPosition, wxDefaultSize, 0 );
184 item2->Add( item12, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
185
186 item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
187
188 item1->Add( item2, 1, wxALIGN_CENTER|wxALL, 5 );
189
190 item0->Add( item1, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
191
192 wxStaticBox *item14 = new wxStaticBox( this, -1, wxT("Client log") );
193 wxStaticBoxSizer *item13 = new wxStaticBoxSizer( item14, wxVERTICAL );
194
81ba6107 195 wxTextCtrl *item15 = new wxTextCtrl( this, ID_LOG, wxEmptyString, wxDefaultPosition, wxSize(500,140), wxTE_MULTILINE );
9d860992
JS
196 item13->Add( item15, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
197
198 item0->Add( item13, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
199
200 this->SetSizer( item0 );
201 item0->SetSizeHints( this );
202
203 // status
204 m_client = NULL;
205 GetServername()->SetSelection(0);
206 GetHostname()->SetSelection(0);
207 GetTopic()->SetSelection(0);
208 wxLogTextCtrl *logWindow = new wxLogTextCtrl(GetLog());
209 delete wxLog::SetActiveTarget(logWindow);
210 wxLogMessage(_T("Click on Connect to connect to the server"));
81ba6107 211 EnableControls();
9d860992
JS
212}
213
81ba6107 214void MyFrame::EnableControls()
9d860992
JS
215{
216 GetStart()->Enable(m_client == NULL);
217 GetServername()->Enable(m_client == NULL);
218 GetHostname()->Enable(m_client == NULL);
219 GetTopic()->Enable(m_client == NULL);
b21348b4
VZ
220
221 const bool isConnected = m_client->IsConnected();
222 GetDisconnect()->Enable(m_client != NULL && isConnected);
223 GetStartAdvise()->Enable(m_client != NULL && isConnected);
224 GetStopAdvise()->Enable(m_client != NULL && isConnected);
225 GetExecute()->Enable(m_client != NULL && isConnected);
226 GetPoke()->Enable(m_client != NULL && isConnected);
227 GetRequest()->Enable(m_client != NULL && isConnected);
9d860992
JS
228}
229
230void MyFrame::OnClose(wxCloseEvent& event)
231{
232 if (m_client)
233 {
234 delete m_client;
235 m_client = NULL;
236 }
237 event.Skip();
238}
239
240void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
241{
242 Close();
243}
244
245void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event))
246{
247 // Connect to the client
248 wxString servername = GetServername()->GetStringSelection();
249 wxString hostname = GetHostname()->GetStringSelection();
250 wxString topic = GetTopic()->GetStringSelection();
251
252 m_client = new MyClient;
253 bool retval = m_client->Connect(hostname, servername, topic);
254
255 wxLogMessage(_T("Client host=\"%s\" port=\"%s\" topic=\"%s\" %s"),
256 hostname.c_str(), servername.c_str(), topic.c_str(),
257 retval ? _T("connected") : _T("failed to connect"));
258
259 if (!retval)
260 {
261 delete m_client;
262 m_client = NULL;
263 }
264 Enable();
265}
266
267void MyFrame::OnServername( wxCommandEvent& WXUNUSED(event) )
268{
269 if (GetServername()->GetStringSelection() == _T("..."))
270 {
271 wxString s = wxGetTextFromUser(_T("Specify the name of the server"),
81ba6107 272 _T("Server Name"), wxEmptyString, this);
9d860992
JS
273 if (!s.IsEmpty() && s != IPC_SERVICE)
274 {
275 GetServername()->Insert(s, 0);
276 GetServername()->SetSelection(0);
277 }
278 }
279}
280
281void MyFrame::OnHostname( wxCommandEvent& WXUNUSED(event) )
282{
283 if (GetHostname()->GetStringSelection() == _T("..."))
284 {
285 wxString s = wxGetTextFromUser(_T("Specify the name of the host (ignored under DDE)"),
81ba6107 286 _T("Host Name"), wxEmptyString, this);
9d860992
JS
287 if (!s.IsEmpty() && s != IPC_HOST)
288 {
289 GetHostname()->Insert(s, 0);
290 GetHostname()->SetSelection(0);
291 }
292 }
293}
294
295void MyFrame::OnTopic( wxCommandEvent& WXUNUSED(event) )
296{
297 if (GetTopic()->GetStringSelection() == _T("..."))
298 {
299 wxString s = wxGetTextFromUser(_T("Specify the name of the topic"),
81ba6107 300 _T("Topic Name"), wxEmptyString, this);
9d860992
JS
301 if (!s.IsEmpty() && s != IPC_TOPIC)
302 {
303 GetTopic()->Insert(s, 0);
304 GetTopic()->SetSelection(0);
305 }
306 }
307}
308
309void MyFrame::OnDisconnect(wxCommandEvent& WXUNUSED(event))
310{
81ba6107 311 Disconnect();
9d860992
JS
312}
313
314void MyFrame::Disconnect()
315{
81ba6107 316 delete m_client;
9d860992
JS
317 m_client = NULL;
318 Enable();
319}
320
321void MyFrame::OnStartAdvise(wxCommandEvent& WXUNUSED(event))
322{
81ba6107 323 m_client->GetConnection()->StartAdvise(_T("something"));
9d860992
JS
324}
325
326void MyFrame::OnStopAdvise(wxCommandEvent& WXUNUSED(event))
327{
81ba6107 328 m_client->GetConnection()->StopAdvise(_T("something"));
7921cf2b
JS
329}
330
87728739 331void MyFrame::OnExecute(wxCommandEvent& WXUNUSED(event))
7921cf2b 332{
9d860992
JS
333 if (m_client->IsConnected())
334 {
335 wxString s = _T("Date");
336
337 m_client->GetConnection()->Execute((wxChar *)s.c_str());
338 m_client->GetConnection()->Execute((wxChar *)s.c_str(), (s.Length() + 1) * sizeof(wxChar));
339#if wxUSE_DDE_FOR_IPC
340 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."));
341#endif
342 char bytes[3];
343 bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
344 m_client->GetConnection()->Execute((wxChar *)bytes, 3, wxIPC_PRIVATE);
345 }
7921cf2b
JS
346}
347
87728739 348void MyFrame::OnPoke(wxCommandEvent& WXUNUSED(event))
7921cf2b 349{
9d860992
JS
350 if (m_client->IsConnected())
351 {
352 wxString s = wxDateTime::Now().Format();
353 m_client->GetConnection()->Poke(_T("Date"), (wxChar *)s.c_str());
354 s = wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate();
355 m_client->GetConnection()->Poke(_T("Date"), (wxChar *)s.c_str(), (s.Length() + 1) * sizeof(wxChar));
356 char bytes[3];
357 bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
358 m_client->GetConnection()->Poke(_T("bytes[3]"), (wxChar *)bytes, 3, wxIPC_PRIVATE);
359 }
7921cf2b
JS
360}
361
87728739 362void MyFrame::OnRequest(wxCommandEvent& WXUNUSED(event))
7921cf2b 363{
9d860992 364 if (m_client->IsConnected())
4b89c618 365 {
9d860992
JS
366 int size;
367 m_client->GetConnection()->Request(_T("Date"));
368 m_client->GetConnection()->Request(_T("Date+len"), &size);
369 m_client->GetConnection()->Request(_T("bytes[3]"), &size, wxIPC_PRIVATE);
4b89c618 370 }
7921cf2b
JS
371}
372
9d860992
JS
373// ----------------------------------------------------------------------------
374// MyClient
375// ----------------------------------------------------------------------------
376MyClient::MyClient() : wxClient()
7921cf2b 377{
9d860992
JS
378 m_connection = NULL;
379}
380
381bool MyClient::Connect(const wxString& sHost, const wxString& sService, const wxString& sTopic)
382{
383 // suppress the log messages from MakeConnection()
384 wxLogNull nolog;
81ba6107 385
9d860992
JS
386 m_connection = (MyConnection *)MakeConnection(sHost, sService, sTopic);
387 return m_connection != NULL;
7921cf2b
JS
388}
389
4b89c618 390wxConnectionBase *MyClient::OnMakeConnection()
7921cf2b 391{
4b89c618 392 return new MyConnection;
7921cf2b
JS
393}
394
9d860992
JS
395void MyClient::Disconnect()
396{
397 if (m_connection)
398 {
399 m_connection->Disconnect();
400 delete m_connection;
401 m_connection = NULL;
402 wxGetApp().GetFrame()->Enable();
403 wxLogMessage(_T("Client disconnected from server"));
404 }
405}
406
407MyClient::~MyClient()
408{
409 Disconnect();
410}
411
412// ----------------------------------------------------------------------------
413// MyConnection
414// ----------------------------------------------------------------------------
415
416void MyConnection::Log(const wxString& command, const wxString& topic,
417 const wxString& item, wxChar *data, int size, wxIPCFormat format)
7921cf2b 418{
9d860992
JS
419 wxString s;
420 if (topic.IsEmpty() && item.IsEmpty())
d2797f55 421 s.Printf(_T("%s("), command.c_str());
9d860992 422 else if (topic.IsEmpty())
d2797f55 423 s.Printf(_T("%s(item=\"%s\","), command.c_str(), item.c_str());
9d860992 424 else if (item.IsEmpty())
d2797f55 425 s.Printf(_T("%s(topic=\"%s\","), command.c_str(), topic.c_str());
9d860992 426 else
d2797f55 427 s.Printf(_T("%s(topic=\"%s\",item=\"%s\","), command.c_str(), topic.c_str(), item.c_str());
9d860992 428
81ba6107 429 if (format == wxIPC_TEXT || format == wxIPC_UNICODETEXT)
d2797f55 430 wxLogMessage(_T("%s\"%s\",%d)"), s.c_str(), data, size);
9d860992 431 else if (format == wxIPC_PRIVATE)
4b89c618 432 {
9d860992
JS
433 if (size == 3)
434 {
435 char *bytes = (char *)data;
d2797f55 436 wxLogMessage(_T("%s'%c%c%c',%d)"), s.c_str(), bytes[0], bytes[1], bytes[2], size);
9d860992
JS
437 }
438 else
d2797f55 439 wxLogMessage(_T("%s...,%d)"), s.c_str(), size);
4b89c618 440 }
81ba6107 441 else if (format == wxIPC_INVALID)
d2797f55 442 wxLogMessage(_T("%s[invalid data],%d)"), s.c_str(), size);
9d860992
JS
443}
444
445bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, wxChar *data,
446 int size, wxIPCFormat format)
447{
448 Log(_T("OnAdvise"), topic, item, data, size, format);
b62ca03d 449 return true;
7921cf2b
JS
450}
451
452bool MyConnection::OnDisconnect()
453{
9d860992
JS
454 wxLogMessage(_T("OnDisconnect()"));
455 wxGetApp().GetFrame()->Disconnect();
456 return true;
457}
458
459bool MyConnection::Execute(const wxChar *data, int size, wxIPCFormat format)
460{
81ba6107 461 Log(_T("Execute"), wxEmptyString, wxEmptyString, (wxChar *)data, size, format);
9d860992
JS
462 bool retval = wxConnection::Execute(data, size, format);
463 if (!retval)
464 wxLogMessage(_T("Execute failed!"));
465 return retval;
466}
7921cf2b 467
9d860992
JS
468wxChar *MyConnection::Request(const wxString& item, int *size, wxIPCFormat format)
469{
470 wxChar *data = wxConnection::Request(item, size, format);
81ba6107 471 Log(_T("Request"), wxEmptyString, item, data, size ? *size : -1, format);
9d860992
JS
472 return data;
473}
474
475bool MyConnection::Poke(const wxString& item, wxChar *data, int size, wxIPCFormat format)
476{
81ba6107 477 Log(_T("Poke"), wxEmptyString, item, data, size, format);
9d860992 478 return wxConnection::Poke(item, data, size, format);
7921cf2b 479}