]> git.saurik.com Git - wxWidgets.git/blame - samples/ipc/client.cpp
correcting wxX11 for wxkeysym as was done for wxMOTIF
[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
9d860992 35#include "wx/datetime.h"
7921cf2b
JS
36#include "client.h"
37
3cb332c1
VZ
38#if !defined(__WXMSW__) && !defined(__WXPM__)
39 #include "../sample.xpm"
40#endif
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
9a83f860 83 m_frame = new MyFrame(NULL, wxT("Client"));
9d860992 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 99 // Give it an icon
3cb332c1 100 SetIcon(wxICON(sample));
4b89c618
VZ
101
102 // Make a menubar
103 wxMenu *file_menu = new wxMenu;
104
9a83f860 105 file_menu->Append(wxID_EXIT, wxT("&Quit\tCtrl-Q"));
4b89c618
VZ
106
107 wxMenuBar *menu_bar = new wxMenuBar;
108
9a83f860 109 menu_bar->Append(file_menu, wxT("&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 119 {
9a83f860 120 IPC_SERVICE, wxT("...")
9d860992 121 };
81ba6107 122 wxString strs5[] =
9d860992 123 {
9a83f860 124 IPC_HOST, wxT("...")
9d860992 125 };
81ba6107 126 wxString strs6[] =
9d860992 127 {
9a83f860 128 IPC_TOPIC, wxT("...")
9d860992
JS
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);
9a83f860 213 wxLogMessage(wxT("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 {
5276b0a5 237 wxDELETE(m_client);
9d860992
JS
238 }
239 event.Skip();
240}
241
242void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
243{
244 Close();
245}
246
247void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event))
248{
249 // Connect to the client
250 wxString servername = GetServername()->GetStringSelection();
251 wxString hostname = GetHostname()->GetStringSelection();
252 wxString topic = GetTopic()->GetStringSelection();
253
254 m_client = new MyClient;
255 bool retval = m_client->Connect(hostname, servername, topic);
256
9a83f860 257 wxLogMessage(wxT("Client host=\"%s\" port=\"%s\" topic=\"%s\" %s"),
9d860992 258 hostname.c_str(), servername.c_str(), topic.c_str(),
9a83f860 259 retval ? wxT("connected") : wxT("failed to connect"));
9d860992
JS
260
261 if (!retval)
262 {
5276b0a5 263 wxDELETE(m_client);
9d860992 264 }
5e530f02 265 EnableControls();
9d860992
JS
266}
267
268void MyFrame::OnServername( wxCommandEvent& WXUNUSED(event) )
269{
9a83f860 270 if (GetServername()->GetStringSelection() == wxT("..."))
9d860992 271 {
9a83f860
VZ
272 wxString s = wxGetTextFromUser(wxT("Specify the name of the server"),
273 wxT("Server Name"), wxEmptyString, this);
9d860992
JS
274 if (!s.IsEmpty() && s != IPC_SERVICE)
275 {
276 GetServername()->Insert(s, 0);
277 GetServername()->SetSelection(0);
278 }
279 }
280}
281
282void MyFrame::OnHostname( wxCommandEvent& WXUNUSED(event) )
283{
9a83f860 284 if (GetHostname()->GetStringSelection() == wxT("..."))
9d860992 285 {
9a83f860
VZ
286 wxString s = wxGetTextFromUser(wxT("Specify the name of the host (ignored under DDE)"),
287 wxT("Host Name"), wxEmptyString, this);
9d860992
JS
288 if (!s.IsEmpty() && s != IPC_HOST)
289 {
290 GetHostname()->Insert(s, 0);
291 GetHostname()->SetSelection(0);
292 }
293 }
294}
295
296void MyFrame::OnTopic( wxCommandEvent& WXUNUSED(event) )
297{
9a83f860 298 if (GetTopic()->GetStringSelection() == wxT("..."))
9d860992 299 {
9a83f860
VZ
300 wxString s = wxGetTextFromUser(wxT("Specify the name of the topic"),
301 wxT("Topic Name"), wxEmptyString, this);
9d860992
JS
302 if (!s.IsEmpty() && s != IPC_TOPIC)
303 {
304 GetTopic()->Insert(s, 0);
305 GetTopic()->SetSelection(0);
306 }
307 }
308}
309
310void MyFrame::OnDisconnect(wxCommandEvent& WXUNUSED(event))
311{
81ba6107 312 Disconnect();
9d860992
JS
313}
314
315void MyFrame::Disconnect()
316{
5276b0a5 317 wxDELETE(m_client);
5e530f02 318 EnableControls();
9d860992
JS
319}
320
321void MyFrame::OnStartAdvise(wxCommandEvent& WXUNUSED(event))
322{
9a83f860 323 m_client->GetConnection()->StartAdvise(wxT("something"));
9d860992
JS
324}
325
326void MyFrame::OnStopAdvise(wxCommandEvent& WXUNUSED(event))
327{
9a83f860 328 m_client->GetConnection()->StopAdvise(wxT("something"));
7921cf2b
JS
329}
330
87728739 331void MyFrame::OnExecute(wxCommandEvent& WXUNUSED(event))
7921cf2b 332{
9d860992
JS
333 if (m_client->IsConnected())
334 {
9a83f860 335 wxString s = wxT("Date");
9d860992 336
50c549b9
VZ
337 m_client->GetConnection()->Execute(s);
338 m_client->GetConnection()->Execute((const char *)s.c_str(), s.length() + 1);
9d860992 339 char bytes[3];
521d3436
VZ
340 bytes[0] = '1';
341 bytes[1] = '2';
342 bytes[2] = '3';
343 m_client->GetConnection()->Execute(bytes, WXSIZEOF(bytes));
9d860992 344 }
7921cf2b
JS
345}
346
87728739 347void MyFrame::OnPoke(wxCommandEvent& WXUNUSED(event))
7921cf2b 348{
9d860992
JS
349 if (m_client->IsConnected())
350 {
351 wxString s = wxDateTime::Now().Format();
9a83f860
VZ
352 m_client->GetConnection()->Poke(wxT("Date"), s);
353 s = wxDateTime::Now().FormatTime() + wxT(" ") + wxDateTime::Now().FormatDate();
354 m_client->GetConnection()->Poke(wxT("Date"), (const char *)s.c_str(), s.length() + 1);
9d860992
JS
355 char bytes[3];
356 bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
9a83f860 357 m_client->GetConnection()->Poke(wxT("bytes[3]"), bytes, 3, wxIPC_PRIVATE);
9d860992 358 }
7921cf2b
JS
359}
360
87728739 361void MyFrame::OnRequest(wxCommandEvent& WXUNUSED(event))
7921cf2b 362{
9d860992 363 if (m_client->IsConnected())
4b89c618 364 {
50c549b9 365 size_t size;
9a83f860
VZ
366 m_client->GetConnection()->Request(wxT("Date"));
367 m_client->GetConnection()->Request(wxT("Date+len"), &size);
368 m_client->GetConnection()->Request(wxT("bytes[3]"), &size, wxIPC_PRIVATE);
4b89c618 369 }
7921cf2b
JS
370}
371
9d860992
JS
372// ----------------------------------------------------------------------------
373// MyClient
374// ----------------------------------------------------------------------------
375MyClient::MyClient() : wxClient()
7921cf2b 376{
9d860992
JS
377 m_connection = NULL;
378}
379
380bool MyClient::Connect(const wxString& sHost, const wxString& sService, const wxString& sTopic)
381{
382 // suppress the log messages from MakeConnection()
383 wxLogNull nolog;
81ba6107 384
9d860992
JS
385 m_connection = (MyConnection *)MakeConnection(sHost, sService, sTopic);
386 return m_connection != NULL;
7921cf2b
JS
387}
388
4b89c618 389wxConnectionBase *MyClient::OnMakeConnection()
7921cf2b 390{
4b89c618 391 return new MyConnection;
7921cf2b
JS
392}
393
9d860992
JS
394void MyClient::Disconnect()
395{
396 if (m_connection)
397 {
398 m_connection->Disconnect();
5276b0a5 399 wxDELETE(m_connection);
5e530f02 400 wxGetApp().GetFrame()->EnableControls();
9a83f860 401 wxLogMessage(wxT("Client disconnected from server"));
9d860992
JS
402 }
403}
404
405MyClient::~MyClient()
406{
407 Disconnect();
408}
409
410// ----------------------------------------------------------------------------
411// MyConnection
412// ----------------------------------------------------------------------------
413
50c549b9
VZ
414bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, const void *data,
415 size_t size, wxIPCFormat format)
9d860992 416{
9a83f860 417 Log(wxT("OnAdvise"), topic, item, data, size, format);
b62ca03d 418 return true;
7921cf2b
JS
419}
420
421bool MyConnection::OnDisconnect()
422{
9a83f860 423 wxLogMessage(wxT("OnDisconnect()"));
9d860992
JS
424 wxGetApp().GetFrame()->Disconnect();
425 return true;
426}
427
50c549b9 428bool MyConnection::DoExecute(const void *data, size_t size, wxIPCFormat format)
9d860992 429{
9a83f860 430 Log(wxT("Execute"), wxEmptyString, wxEmptyString, data, size, format);
50c549b9 431 bool retval = wxConnection::DoExecute(data, size, format);
9d860992 432 if (!retval)
43b2d5e7 433 {
9a83f860 434 wxLogMessage(wxT("Execute failed!"));
43b2d5e7 435 }
9d860992
JS
436 return retval;
437}
7921cf2b 438
50c549b9 439const void *MyConnection::Request(const wxString& item, size_t *size, wxIPCFormat format)
9d860992 440{
50c549b9 441 const void *data = wxConnection::Request(item, size, format);
9a83f860 442 Log(wxT("Request"), wxEmptyString, item, data, size ? *size : wxNO_LEN, format);
9d860992
JS
443 return data;
444}
445
50c549b9 446bool MyConnection::DoPoke(const wxString& item, const void *data, size_t size, wxIPCFormat format)
9d860992 447{
9a83f860 448 Log(wxT("Poke"), wxEmptyString, item, data, size, format);
50c549b9 449 return wxConnection::DoPoke(item, data, size, format);
7921cf2b 450}