]> git.saurik.com Git - wxWidgets.git/blame - samples/ipc/client.cpp
Updated version
[wxWidgets.git] / samples / ipc / client.cpp
CommitLineData
7921cf2b
JS
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
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.
7921cf2b
JS
33#include "ddesetup.h"
34
618f2efa 35#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
7921cf2b
JS
36#include "mondrian.xpm"
37#endif
38
39#include "client.h"
40
4b89c618
VZ
41// ----------------------------------------------------------------------------
42// wxWin macros
43// ----------------------------------------------------------------------------
7921cf2b
JS
44
45IMPLEMENT_APP(MyApp)
46
4b89c618
VZ
47BEGIN_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)
52END_EVENT_TABLE()
53
54// ----------------------------------------------------------------------------
55// globals
56// ----------------------------------------------------------------------------
57
7921cf2b
JS
58wxListBox *the_list = NULL;
59
60MyConnection *the_connection = NULL;
2f6c54eb 61MyClient *my_client;
7921cf2b 62
4b89c618
VZ
63// ============================================================================
64// implementation
65// ============================================================================
66
67// ----------------------------------------------------------------------------
68// MyApp
69// ----------------------------------------------------------------------------
70
7921cf2b
JS
71// The `main program' equivalent, creating the windows and returning the
72// main frame
73bool MyApp::OnInit()
74{
f6bcfd97 75 // service name (DDE classes) or port number (TCP/IP based classes)
d5172a58 76 wxString service = IPC_SERVICE;
f6bcfd97
BP
77
78 // ignored under DDE, host name in TCP/IP based classes
79 wxString hostName = "localhost";
7921cf2b 80
4b89c618 81 if (argc > 1)
f6bcfd97 82 service = argv[1];
4b89c618
VZ
83 if (argc > 2)
84 hostName = argv[2];
7921cf2b 85
4b89c618
VZ
86 // Create a new client
87 my_client = new MyClient;
7921cf2b 88
b7e19213 89 // suppress the log messages from MakeConnection()
4b89c618 90 {
b7e19213 91 wxLogNull nolog;
d5172a58
VZ
92 the_connection = (MyConnection *)
93 my_client->MakeConnection(hostName, service, IPC_TOPIC);
b7e19213
VZ
94
95 while ( !the_connection )
4b89c618 96 {
b7e19213
VZ
97 if ( wxMessageBox("Failed to make connection to server.\nRetry?",
98 "Client Demo Error",
99 wxICON_ERROR | wxYES_NO | wxCANCEL ) != wxYES )
100 {
101 // no server
102 return FALSE;
103 }
104
f6bcfd97 105 the_connection = (MyConnection *)my_client->MakeConnection(hostName, service, "IPC TEST");
4b89c618 106 }
4b89c618 107 }
7921cf2b 108
d5172a58 109 if (!the_connection->StartAdvise(IPC_ADVISE_NAME))
4b89c618 110 wxMessageBox("StartAdvise failed", "Client Demo Error");
7921cf2b 111
4b89c618
VZ
112 // Create the main frame window
113 (new MyFrame(NULL, "Client"))->Show(TRUE);
b178e0c7 114
4b89c618 115 return TRUE;
7921cf2b
JS
116}
117
118int MyApp::OnExit()
119{
4b89c618 120 // will delete the connection too
2f6c54eb
VZ
121 // Update: Seems it didn't delete the_connection, because there's a leak.
122 // Deletion is now explicitly done a few lines up.
f010ad48
JS
123 // another Update: in fact it's because OnDisconnect should delete it, but
124 // it wasn't
4b89c618 125 delete my_client;
7921cf2b 126
2f6c54eb
VZ
127
128
7921cf2b
JS
129 return 0;
130}
131
7921cf2b 132// Define my frame constructor
4b89c618 133MyFrame::MyFrame(wxFrame *frame, const wxString& title)
39d580f7 134 : wxFrame(frame, -1, title, wxDefaultPosition, wxSize(300, 200))
7921cf2b 135{
4b89c618
VZ
136 // Give it an icon
137 SetIcon(wxICON(mondrian));
138
139 // Make a menubar
140 wxMenu *file_menu = new wxMenu;
141
39d580f7
VZ
142 file_menu->Append(CLIENT_EXECUTE, "&Execute\tCtrl-E");
143 file_menu->Append(CLIENT_REQUEST, "&Request\tCtrl-R");
144 file_menu->Append(CLIENT_POKE, "&Poke\tCtrl-P");
145 file_menu->Append(CLIENT_QUIT, "&Quit\tCtrl-Q");
4b89c618
VZ
146
147 wxMenuBar *menu_bar = new wxMenuBar;
148
39d580f7 149 menu_bar->Append(file_menu, "&File");
4b89c618
VZ
150
151 // Associate the menu bar with the frame
152 SetMenuBar(menu_bar);
153
39d580f7
VZ
154 // Make a listbox which shows the choices made in the server
155 the_list = new wxListBox(this, CLIENT_LISTBOX, wxPoint(5, 5));
4b89c618
VZ
156 the_list->Append("Apple");
157 the_list->Append("Pear");
158 the_list->Append("Orange");
159 the_list->Append("Banana");
160 the_list->Append("Fruit");
7921cf2b
JS
161}
162
163void MyFrame::OnExecute(wxCommandEvent& event)
164{
4b89c618 165 if (the_connection)
b178e0c7 166 if (!the_connection->Execute("Hello from the client!"))
4b89c618 167 wxMessageBox("Execute failed", "Client Demo Error");
7921cf2b
JS
168}
169
170void MyFrame::OnPoke(wxCommandEvent& event)
171{
4b89c618 172 if (the_connection)
b178e0c7 173 if (!the_connection->Poke("An item", "Some data to poke at the server!"))
4b89c618 174 wxMessageBox("Poke failed", "Client Demo Error");
7921cf2b
JS
175}
176
177void MyFrame::OnRequest(wxCommandEvent& event)
178{
4b89c618
VZ
179 if (the_connection)
180 {
7921cf2b
JS
181 char *data = the_connection->Request("An item");
182 if (data)
4b89c618 183 wxMessageBox(data, "Client: Request", wxOK);
b178e0c7 184 else
4b89c618
VZ
185 wxMessageBox("Request failed", "Client Demo Error");
186 }
7921cf2b
JS
187}
188
189void MyFrame::OnExit(wxCommandEvent& event)
190{
4b89c618 191 Close();
7921cf2b
JS
192}
193
4b89c618 194wxConnectionBase *MyClient::OnMakeConnection()
7921cf2b 195{
4b89c618 196 return new MyConnection;
7921cf2b
JS
197}
198
7921cf2b
JS
199bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format)
200{
4b89c618
VZ
201 if (the_list)
202 {
203 int n = the_list->FindString(data);
204 if (n > -1)
205 the_list->SetSelection(n);
206 }
207 return TRUE;
7921cf2b
JS
208}
209
210bool MyConnection::OnDisconnect()
211{
f010ad48 212 // when connection is terminated, quit whole program
4b89c618
VZ
213 wxWindow *win = wxTheApp->GetTopWindow();
214 if ( win )
215 win->Destroy();
7921cf2b 216
f010ad48
JS
217 // delete self
218 the_connection = NULL;
219 return wxConnection::OnDisconnect();
7921cf2b
JS
220}
221