]> git.saurik.com Git - wxWidgets.git/blame - samples/ipc/client.cpp
These two makefiles were *horrid* abominations.
[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
36#if defined(__WXGTK__) || defined(__WXMOTIF__)
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;
63MyClient *my_client ;
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{
4b89c618 77 wxString server = "4242";
4b89c618 78 wxString hostName = wxGetHostName();
7921cf2b 79
4b89c618
VZ
80 if (argc > 1)
81 server = argv[1];
82 if (argc > 2)
83 hostName = argv[2];
7921cf2b 84
4b89c618
VZ
85 // Create a new client
86 my_client = new MyClient;
7921cf2b 87
b7e19213 88 // suppress the log messages from MakeConnection()
4b89c618 89 {
b7e19213
VZ
90 wxLogNull nolog;
91 the_connection = (MyConnection *)my_client->MakeConnection(hostName, server, "IPC TEST");
92
93 while ( !the_connection )
4b89c618 94 {
b7e19213
VZ
95 if ( wxMessageBox("Failed to make connection to server.\nRetry?",
96 "Client Demo Error",
97 wxICON_ERROR | wxYES_NO | wxCANCEL ) != wxYES )
98 {
99 // no server
100 return FALSE;
101 }
102
103 the_connection = (MyConnection *)my_client->MakeConnection(hostName, server, "IPC TEST");
4b89c618 104 }
4b89c618 105 }
7921cf2b 106
4b89c618
VZ
107 if (!the_connection->StartAdvise("Item"))
108 wxMessageBox("StartAdvise failed", "Client Demo Error");
7921cf2b 109
4b89c618
VZ
110 // Create the main frame window
111 (new MyFrame(NULL, "Client"))->Show(TRUE);
b178e0c7 112
4b89c618 113 return TRUE;
7921cf2b
JS
114}
115
116int MyApp::OnExit()
117{
4b89c618
VZ
118 if (the_connection)
119 {
120 the_connection->Disconnect();
121 }
122
123 // will delete the connection too
124 delete my_client;
7921cf2b
JS
125
126 return 0;
127}
128
7921cf2b 129// Define my frame constructor
4b89c618
VZ
130MyFrame::MyFrame(wxFrame *frame, const wxString& title)
131 : wxFrame(frame, -1, title)
7921cf2b
JS
132{
133 panel = NULL;
4b89c618
VZ
134
135 // Give it an icon
136 SetIcon(wxICON(mondrian));
137
138 // Make a menubar
139 wxMenu *file_menu = new wxMenu;
140
141 file_menu->Append(CLIENT_EXECUTE, "Execute");
142 file_menu->Append(CLIENT_REQUEST, "Request");
143 file_menu->Append(CLIENT_POKE, "Poke");
144 file_menu->Append(CLIENT_QUIT, "Quit");
145
146 wxMenuBar *menu_bar = new wxMenuBar;
147
148 menu_bar->Append(file_menu, "File");
149
150 // Associate the menu bar with the frame
151 SetMenuBar(menu_bar);
152
153 // Make a panel
154 panel = new wxPanel(this);
155 the_list = new wxListBox(panel, CLIENT_LISTBOX, wxPoint(5, 5));
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");
161
162 panel->Fit();
163 Fit();
7921cf2b
JS
164}
165
166void MyFrame::OnExecute(wxCommandEvent& event)
167{
4b89c618 168 if (the_connection)
b178e0c7 169 if (!the_connection->Execute("Hello from the client!"))
4b89c618 170 wxMessageBox("Execute failed", "Client Demo Error");
7921cf2b
JS
171}
172
173void MyFrame::OnPoke(wxCommandEvent& event)
174{
4b89c618 175 if (the_connection)
b178e0c7 176 if (!the_connection->Poke("An item", "Some data to poke at the server!"))
4b89c618 177 wxMessageBox("Poke failed", "Client Demo Error");
7921cf2b
JS
178}
179
180void MyFrame::OnRequest(wxCommandEvent& event)
181{
4b89c618
VZ
182 if (the_connection)
183 {
7921cf2b
JS
184 char *data = the_connection->Request("An item");
185 if (data)
4b89c618 186 wxMessageBox(data, "Client: Request", wxOK);
b178e0c7 187 else
4b89c618
VZ
188 wxMessageBox("Request failed", "Client Demo Error");
189 }
7921cf2b
JS
190}
191
192void MyFrame::OnExit(wxCommandEvent& event)
193{
4b89c618 194 Close();
7921cf2b
JS
195}
196
4b89c618 197wxConnectionBase *MyClient::OnMakeConnection()
7921cf2b 198{
4b89c618 199 return new MyConnection;
7921cf2b
JS
200}
201
4b89c618
VZ
202MyConnection::MyConnection()
203 : wxConnection(ipc_buffer, WXSIZEOF(ipc_buffer))
7921cf2b
JS
204{
205}
206
4b89c618 207MyConnection::~MyConnection()
7921cf2b 208{
4b89c618 209 the_connection = NULL;
7921cf2b
JS
210}
211
212bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format)
213{
4b89c618
VZ
214 if (the_list)
215 {
216 int n = the_list->FindString(data);
217 if (n > -1)
218 the_list->SetSelection(n);
219 }
220 return TRUE;
7921cf2b
JS
221}
222
223bool MyConnection::OnDisconnect()
224{
4b89c618
VZ
225 wxWindow *win = wxTheApp->GetTopWindow();
226 if ( win )
227 win->Destroy();
7921cf2b
JS
228
229 return TRUE;
230}
231