]>
Commit | Line | Data |
---|---|---|
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 | ||
42993df2 | 36 | #if defined(__WXGTK__) || 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 | |
46 | IMPLEMENT_APP(MyApp) | |
47 | ||
4b89c618 VZ |
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 | ||
7921cf2b JS |
59 | char ipc_buffer[4000]; |
60 | wxListBox *the_list = NULL; | |
61 | ||
62 | MyConnection *the_connection = NULL; | |
2f6c54eb | 63 | MyClient *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 | |
75 | bool MyApp::OnInit() | |
76 | { | |
f6bcfd97 BP |
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"; | |
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; |
f6bcfd97 | 94 | the_connection = (MyConnection *)my_client->MakeConnection(hostName, service, "IPC TEST"); |
b7e19213 VZ |
95 | |
96 | while ( !the_connection ) | |
4b89c618 | 97 | { |
b7e19213 VZ |
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 | ||
f6bcfd97 | 106 | the_connection = (MyConnection *)my_client->MakeConnection(hostName, service, "IPC TEST"); |
4b89c618 | 107 | } |
4b89c618 | 108 | } |
7921cf2b | 109 | |
4b89c618 VZ |
110 | if (!the_connection->StartAdvise("Item")) |
111 | wxMessageBox("StartAdvise failed", "Client Demo Error"); | |
7921cf2b | 112 | |
4b89c618 VZ |
113 | // Create the main frame window |
114 | (new MyFrame(NULL, "Client"))->Show(TRUE); | |
b178e0c7 | 115 | |
4b89c618 | 116 | return TRUE; |
7921cf2b JS |
117 | } |
118 | ||
119 | int MyApp::OnExit() | |
120 | { | |
4b89c618 VZ |
121 | if (the_connection) |
122 | { | |
123 | the_connection->Disconnect(); | |
2f6c54eb VZ |
124 | delete the_connection; |
125 | the_connection = NULL; | |
4b89c618 VZ |
126 | } |
127 | ||
128 | // will delete the connection too | |
2f6c54eb VZ |
129 | // Update: Seems it didn't delete the_connection, because there's a leak. |
130 | // Deletion is now explicitly done a few lines up. | |
4b89c618 | 131 | delete my_client; |
7921cf2b | 132 | |
2f6c54eb VZ |
133 | |
134 | ||
7921cf2b JS |
135 | return 0; |
136 | } | |
137 | ||
7921cf2b | 138 | // Define my frame constructor |
4b89c618 VZ |
139 | MyFrame::MyFrame(wxFrame *frame, const wxString& title) |
140 | : wxFrame(frame, -1, title) | |
7921cf2b JS |
141 | { |
142 | panel = NULL; | |
4b89c618 VZ |
143 | |
144 | // Give it an icon | |
145 | SetIcon(wxICON(mondrian)); | |
146 | ||
147 | // Make a menubar | |
148 | wxMenu *file_menu = new wxMenu; | |
149 | ||
150 | file_menu->Append(CLIENT_EXECUTE, "Execute"); | |
151 | file_menu->Append(CLIENT_REQUEST, "Request"); | |
152 | file_menu->Append(CLIENT_POKE, "Poke"); | |
153 | file_menu->Append(CLIENT_QUIT, "Quit"); | |
154 | ||
155 | wxMenuBar *menu_bar = new wxMenuBar; | |
156 | ||
157 | menu_bar->Append(file_menu, "File"); | |
158 | ||
159 | // Associate the menu bar with the frame | |
160 | SetMenuBar(menu_bar); | |
161 | ||
162 | // Make a panel | |
163 | panel = new wxPanel(this); | |
164 | the_list = new wxListBox(panel, CLIENT_LISTBOX, wxPoint(5, 5)); | |
165 | the_list->Append("Apple"); | |
166 | the_list->Append("Pear"); | |
167 | the_list->Append("Orange"); | |
168 | the_list->Append("Banana"); | |
169 | the_list->Append("Fruit"); | |
170 | ||
171 | panel->Fit(); | |
172 | Fit(); | |
7921cf2b JS |
173 | } |
174 | ||
175 | void MyFrame::OnExecute(wxCommandEvent& event) | |
176 | { | |
4b89c618 | 177 | if (the_connection) |
b178e0c7 | 178 | if (!the_connection->Execute("Hello from the client!")) |
4b89c618 | 179 | wxMessageBox("Execute failed", "Client Demo Error"); |
7921cf2b JS |
180 | } |
181 | ||
182 | void MyFrame::OnPoke(wxCommandEvent& event) | |
183 | { | |
4b89c618 | 184 | if (the_connection) |
b178e0c7 | 185 | if (!the_connection->Poke("An item", "Some data to poke at the server!")) |
4b89c618 | 186 | wxMessageBox("Poke failed", "Client Demo Error"); |
7921cf2b JS |
187 | } |
188 | ||
189 | void MyFrame::OnRequest(wxCommandEvent& event) | |
190 | { | |
4b89c618 VZ |
191 | if (the_connection) |
192 | { | |
7921cf2b JS |
193 | char *data = the_connection->Request("An item"); |
194 | if (data) | |
4b89c618 | 195 | wxMessageBox(data, "Client: Request", wxOK); |
b178e0c7 | 196 | else |
4b89c618 VZ |
197 | wxMessageBox("Request failed", "Client Demo Error"); |
198 | } | |
7921cf2b JS |
199 | } |
200 | ||
201 | void MyFrame::OnExit(wxCommandEvent& event) | |
202 | { | |
4b89c618 | 203 | Close(); |
7921cf2b JS |
204 | } |
205 | ||
4b89c618 | 206 | wxConnectionBase *MyClient::OnMakeConnection() |
7921cf2b | 207 | { |
4b89c618 | 208 | return new MyConnection; |
7921cf2b JS |
209 | } |
210 | ||
4b89c618 VZ |
211 | MyConnection::MyConnection() |
212 | : wxConnection(ipc_buffer, WXSIZEOF(ipc_buffer)) | |
7921cf2b JS |
213 | { |
214 | } | |
215 | ||
7921cf2b JS |
216 | bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format) |
217 | { | |
4b89c618 VZ |
218 | if (the_list) |
219 | { | |
220 | int n = the_list->FindString(data); | |
221 | if (n > -1) | |
222 | the_list->SetSelection(n); | |
223 | } | |
224 | return TRUE; | |
7921cf2b JS |
225 | } |
226 | ||
227 | bool MyConnection::OnDisconnect() | |
228 | { | |
4b89c618 VZ |
229 | wxWindow *win = wxTheApp->GetTopWindow(); |
230 | if ( win ) | |
231 | win->Destroy(); | |
7921cf2b JS |
232 | |
233 | return TRUE; | |
234 | } | |
235 |