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