]> git.saurik.com Git - wxWidgets.git/blob - samples/ipc/client.cpp
Added missing files to listing
[wxWidgets.git] / samples / ipc / client.cpp
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
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/wx.h"
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
42 // ----------------------------------------------------------------------------
43 // wxWin macros
44 // ----------------------------------------------------------------------------
45
46 IMPLEMENT_APP(MyApp)
47
48 BEGIN_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)
53 END_EVENT_TABLE()
54
55 // ----------------------------------------------------------------------------
56 // globals
57 // ----------------------------------------------------------------------------
58
59 char ipc_buffer[4000];
60 wxListBox *the_list = NULL;
61
62 MyConnection *the_connection = NULL;
63 MyClient *my_client ;
64
65 // ============================================================================
66 // implementation
67 // ============================================================================
68
69 // ----------------------------------------------------------------------------
70 // MyApp
71 // ----------------------------------------------------------------------------
72
73 // The `main program' equivalent, creating the windows and returning the
74 // main frame
75 bool MyApp::OnInit()
76 {
77 // service name (DDE classes) or port number (TCP/IP based classes)
78 wxString service = "4242";
79
80 // ignored under DDE, host name in TCP/IP based classes
81 wxString hostName = "localhost";
82
83 if (argc > 1)
84 service = argv[1];
85 if (argc > 2)
86 hostName = argv[2];
87
88 // Create a new client
89 my_client = new MyClient;
90
91 // suppress the log messages from MakeConnection()
92 {
93 wxLogNull nolog;
94 the_connection = (MyConnection *)my_client->MakeConnection(hostName, service, "IPC TEST");
95
96 while ( !the_connection )
97 {
98 if ( wxMessageBox("Failed to make connection to server.\nRetry?",
99 "Client Demo Error",
100 wxICON_ERROR | wxYES_NO | wxCANCEL ) != wxYES )
101 {
102 // no server
103 return FALSE;
104 }
105
106 the_connection = (MyConnection *)my_client->MakeConnection(hostName, service, "IPC TEST");
107 }
108 }
109
110 if (!the_connection->StartAdvise("Item"))
111 wxMessageBox("StartAdvise failed", "Client Demo Error");
112
113 // Create the main frame window
114 (new MyFrame(NULL, "Client"))->Show(TRUE);
115
116 return TRUE;
117 }
118
119 int MyApp::OnExit()
120 {
121 if (the_connection)
122 {
123 the_connection->Disconnect();
124 }
125
126 // will delete the connection too
127 delete my_client;
128
129 return 0;
130 }
131
132 // Define my frame constructor
133 MyFrame::MyFrame(wxFrame *frame, const wxString& title)
134 : wxFrame(frame, -1, title)
135 {
136 panel = NULL;
137
138 // Give it an icon
139 SetIcon(wxICON(mondrian));
140
141 // Make a menubar
142 wxMenu *file_menu = new wxMenu;
143
144 file_menu->Append(CLIENT_EXECUTE, "Execute");
145 file_menu->Append(CLIENT_REQUEST, "Request");
146 file_menu->Append(CLIENT_POKE, "Poke");
147 file_menu->Append(CLIENT_QUIT, "Quit");
148
149 wxMenuBar *menu_bar = new wxMenuBar;
150
151 menu_bar->Append(file_menu, "File");
152
153 // Associate the menu bar with the frame
154 SetMenuBar(menu_bar);
155
156 // Make a panel
157 panel = new wxPanel(this);
158 the_list = new wxListBox(panel, CLIENT_LISTBOX, wxPoint(5, 5));
159 the_list->Append("Apple");
160 the_list->Append("Pear");
161 the_list->Append("Orange");
162 the_list->Append("Banana");
163 the_list->Append("Fruit");
164
165 panel->Fit();
166 Fit();
167 }
168
169 void MyFrame::OnExecute(wxCommandEvent& event)
170 {
171 if (the_connection)
172 if (!the_connection->Execute("Hello from the client!"))
173 wxMessageBox("Execute failed", "Client Demo Error");
174 }
175
176 void MyFrame::OnPoke(wxCommandEvent& event)
177 {
178 if (the_connection)
179 if (!the_connection->Poke("An item", "Some data to poke at the server!"))
180 wxMessageBox("Poke failed", "Client Demo Error");
181 }
182
183 void MyFrame::OnRequest(wxCommandEvent& event)
184 {
185 if (the_connection)
186 {
187 char *data = the_connection->Request("An item");
188 if (data)
189 wxMessageBox(data, "Client: Request", wxOK);
190 else
191 wxMessageBox("Request failed", "Client Demo Error");
192 }
193 }
194
195 void MyFrame::OnExit(wxCommandEvent& event)
196 {
197 Close();
198 }
199
200 wxConnectionBase *MyClient::OnMakeConnection()
201 {
202 return new MyConnection;
203 }
204
205 MyConnection::MyConnection()
206 : wxConnection(ipc_buffer, WXSIZEOF(ipc_buffer))
207 {
208 }
209
210 MyConnection::~MyConnection()
211 {
212 the_connection = NULL;
213 }
214
215 bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format)
216 {
217 if (the_list)
218 {
219 int n = the_list->FindString(data);
220 if (n > -1)
221 the_list->SetSelection(n);
222 }
223 return TRUE;
224 }
225
226 bool MyConnection::OnDisconnect()
227 {
228 wxWindow *win = wxTheApp->GetTopWindow();
229 if ( win )
230 win->Destroy();
231
232 return TRUE;
233 }
234