cleaned IPC samples a bit
[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 wxString server = "4242";
78 #if wxUSE_DDE_FOR_SAMPLE
79 wxString hostName = wxGetHostName();
80 #else
81 wxString hostName = "localhost";
82 #endif
83
84 if (argc > 1)
85 server = argv[1];
86 if (argc > 2)
87 hostName = argv[2];
88
89 // Create a new client
90 my_client = new MyClient;
91 the_connection = (MyConnection *)my_client->MakeConnection(hostName, server, "IPC TEST");
92
93 while ( !the_connection )
94 {
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");
104 }
105
106 if (!the_connection->StartAdvise("Item"))
107 wxMessageBox("StartAdvise failed", "Client Demo Error");
108
109 // Create the main frame window
110 (new MyFrame(NULL, "Client"))->Show(TRUE);
111
112 return TRUE;
113 }
114
115 int MyApp::OnExit()
116 {
117 if (the_connection)
118 {
119 the_connection->Disconnect();
120 }
121
122 // will delete the connection too
123 delete my_client;
124
125 return 0;
126 }
127
128 // Define my frame constructor
129 MyFrame::MyFrame(wxFrame *frame, const wxString& title)
130 : wxFrame(frame, -1, title)
131 {
132 panel = NULL;
133
134 // Give it an icon
135 SetIcon(wxICON(mondrian));
136
137 // Make a menubar
138 wxMenu *file_menu = new wxMenu;
139
140 file_menu->Append(CLIENT_EXECUTE, "Execute");
141 file_menu->Append(CLIENT_REQUEST, "Request");
142 file_menu->Append(CLIENT_POKE, "Poke");
143 file_menu->Append(CLIENT_QUIT, "Quit");
144
145 wxMenuBar *menu_bar = new wxMenuBar;
146
147 menu_bar->Append(file_menu, "File");
148
149 // Associate the menu bar with the frame
150 SetMenuBar(menu_bar);
151
152 // Make a panel
153 panel = new wxPanel(this);
154 the_list = new wxListBox(panel, CLIENT_LISTBOX, wxPoint(5, 5));
155 the_list->Append("Apple");
156 the_list->Append("Pear");
157 the_list->Append("Orange");
158 the_list->Append("Banana");
159 the_list->Append("Fruit");
160
161 panel->Fit();
162 Fit();
163 }
164
165 void MyFrame::OnExecute(wxCommandEvent& event)
166 {
167 if (the_connection)
168 if (!the_connection->Execute("Hello from the client!"))
169 wxMessageBox("Execute failed", "Client Demo Error");
170 }
171
172 void MyFrame::OnPoke(wxCommandEvent& event)
173 {
174 if (the_connection)
175 if (!the_connection->Poke("An item", "Some data to poke at the server!"))
176 wxMessageBox("Poke failed", "Client Demo Error");
177 }
178
179 void MyFrame::OnRequest(wxCommandEvent& event)
180 {
181 if (the_connection)
182 {
183 char *data = the_connection->Request("An item");
184 if (data)
185 wxMessageBox(data, "Client: Request", wxOK);
186 else
187 wxMessageBox("Request failed", "Client Demo Error");
188 }
189 }
190
191 void MyFrame::OnExit(wxCommandEvent& event)
192 {
193 Close();
194 }
195
196 wxConnectionBase *MyClient::OnMakeConnection()
197 {
198 return new MyConnection;
199 }
200
201 MyConnection::MyConnection()
202 : wxConnection(ipc_buffer, WXSIZEOF(ipc_buffer))
203 {
204 }
205
206 MyConnection::~MyConnection()
207 {
208 the_connection = NULL;
209 }
210
211 bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format)
212 {
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;
220 }
221
222 bool MyConnection::OnDisconnect()
223 {
224 wxWindow *win = wxTheApp->GetTopWindow();
225 if ( win )
226 win->Destroy();
227
228 return TRUE;
229 }
230