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