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 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/wx.h" | |
21 | #endif | |
22 | ||
23 | // Settings common to both executables: determines whether | |
24 | // we're using TCP/IP or real DDE. | |
25 | ||
26 | #include "ddesetup.h" | |
27 | ||
28 | #if defined(__WXGTK__) || defined(__WXMOTIF__) | |
29 | #include "mondrian.xpm" | |
30 | #endif | |
31 | ||
32 | #include "client.h" | |
33 | ||
34 | MyFrame *frame = NULL; | |
35 | ||
36 | IMPLEMENT_APP(MyApp) | |
37 | ||
38 | char ipc_buffer[4000]; | |
39 | wxListBox *the_list = NULL; | |
40 | ||
41 | MyConnection *the_connection = NULL; | |
42 | MyClient *my_client ; | |
43 | ||
44 | // The `main program' equivalent, creating the windows and returning the | |
45 | // main frame | |
46 | bool MyApp::OnInit() | |
47 | { | |
48 | // Create the main frame window | |
49 | frame = new MyFrame(NULL, "Client", wxPoint(400, 0), wxSize(400, 300)); | |
50 | ||
51 | // Give it an icon | |
52 | frame->SetIcon(wxICON(mondrian)); | |
53 | ||
54 | // Make a menubar | |
55 | wxMenu *file_menu = new wxMenu; | |
56 | ||
57 | file_menu->Append(CLIENT_EXECUTE, "Execute"); | |
58 | file_menu->Append(CLIENT_REQUEST, "Request"); | |
59 | file_menu->Append(CLIENT_POKE, "Poke"); | |
60 | file_menu->Append(CLIENT_QUIT, "Quit"); | |
61 | ||
62 | wxMenuBar *menu_bar = new wxMenuBar; | |
63 | ||
64 | menu_bar->Append(file_menu, "File"); | |
65 | ||
66 | // Associate the menu bar with the frame | |
67 | frame->SetMenuBar(menu_bar); | |
68 | ||
69 | // Make a panel | |
70 | frame->panel = new wxPanel(frame, -1, wxPoint(0, 0), wxSize(400, 250)); | |
71 | the_list = new wxListBox(frame->panel, CLIENT_LISTBOX, wxPoint(5, 5), wxSize(150, 120)); | |
72 | the_list->Append("Apple"); | |
73 | the_list->Append("Pear"); | |
74 | the_list->Append("Orange"); | |
75 | the_list->Append("Banana"); | |
76 | the_list->Append("Fruit"); | |
77 | ||
78 | frame->panel->Fit(); | |
79 | frame->Fit(); | |
80 | ||
81 | wxString server = "4242"; | |
b178e0c7 GRG |
82 | |
83 | #if wxUSE_DDE_FOR_SAMPLE | |
e90babdf | 84 | wxString hostName = wxGetHostName(); |
b178e0c7 GRG |
85 | #else |
86 | wxString hostName = "localhost"; | |
87 | #endif | |
7921cf2b JS |
88 | |
89 | if (argc > 1) | |
90 | server = argv[1]; | |
91 | if (argc > 2) | |
92 | hostName = argv[2]; | |
93 | ||
94 | // Create a new client | |
95 | my_client = new MyClient; | |
96 | the_connection = (MyConnection *)my_client->MakeConnection(hostName, server, "IPC TEST"); | |
97 | ||
98 | if (!the_connection) | |
99 | { | |
100 | wxMessageBox("Failed to make connection to server", "Client Demo Error"); | |
101 | #ifdef __WXMSW__ | |
102 | // extern void wxPrintDDEError(); | |
103 | // wxPrintDDEError(); | |
104 | #endif | |
105 | return FALSE; | |
106 | } | |
b178e0c7 GRG |
107 | if (!the_connection->StartAdvise("Item")) |
108 | wxMessageBox("StartAdvise failed", "Client Demo Error"); | |
7921cf2b JS |
109 | |
110 | frame->Show(TRUE); | |
111 | ||
112 | return TRUE; | |
113 | } | |
114 | ||
115 | int MyApp::OnExit() | |
116 | { | |
117 | if (my_client) | |
118 | delete my_client ; | |
119 | ||
120 | return 0; | |
121 | } | |
122 | ||
123 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
124 | EVT_MENU(CLIENT_QUIT, MyFrame::OnExit) | |
125 | EVT_MENU(CLIENT_EXECUTE, MyFrame::OnExecute) | |
126 | EVT_MENU(CLIENT_POKE, MyFrame::OnPoke) | |
127 | EVT_MENU(CLIENT_REQUEST, MyFrame::OnRequest) | |
128 | EVT_CLOSE(MyFrame::OnCloseWindow) | |
129 | END_EVENT_TABLE() | |
130 | ||
131 | // Define my frame constructor | |
132 | MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size): | |
133 | wxFrame(frame, -1, title, pos, size) | |
134 | { | |
135 | panel = NULL; | |
136 | } | |
137 | ||
138 | void MyFrame::OnExecute(wxCommandEvent& event) | |
139 | { | |
140 | if (the_connection) | |
b178e0c7 GRG |
141 | if (!the_connection->Execute("Hello from the client!")) |
142 | wxMessageBox("Execute failed", "Client Demo Error"); | |
7921cf2b JS |
143 | } |
144 | ||
145 | void MyFrame::OnPoke(wxCommandEvent& event) | |
146 | { | |
147 | if (the_connection) | |
b178e0c7 GRG |
148 | if (!the_connection->Poke("An item", "Some data to poke at the server!")) |
149 | wxMessageBox("Poke failed", "Client Demo Error"); | |
7921cf2b JS |
150 | } |
151 | ||
152 | void MyFrame::OnRequest(wxCommandEvent& event) | |
153 | { | |
154 | if (the_connection) | |
155 | { | |
156 | char *data = the_connection->Request("An item"); | |
157 | if (data) | |
158 | wxMessageBox(data, "Client: Request", wxOK); | |
b178e0c7 GRG |
159 | else |
160 | wxMessageBox("Request failed", "Client Demo Error"); | |
7921cf2b JS |
161 | } |
162 | } | |
163 | ||
164 | void MyFrame::OnExit(wxCommandEvent& event) | |
165 | { | |
166 | if (the_connection) | |
167 | the_connection->Disconnect(); | |
168 | ||
169 | this->Destroy(); | |
170 | } | |
171 | ||
172 | // Define the behaviour for the frame closing | |
173 | void MyFrame::OnCloseWindow(wxCloseEvent& event) | |
174 | { | |
175 | if (the_connection) | |
176 | { | |
177 | the_connection->Disconnect(); | |
178 | } | |
179 | this->Destroy(); | |
180 | } | |
181 | ||
182 | MyClient::MyClient(void) | |
183 | { | |
184 | } | |
185 | ||
186 | wxConnectionBase *MyClient::OnMakeConnection(void) | |
187 | { | |
188 | return new MyConnection; | |
189 | } | |
190 | ||
191 | MyConnection::MyConnection(void):wxConnection(ipc_buffer, 3999) | |
192 | { | |
193 | } | |
194 | ||
195 | MyConnection::~MyConnection(void) | |
196 | { | |
197 | the_connection = NULL; | |
198 | } | |
199 | ||
200 | bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format) | |
201 | { | |
202 | if (the_list) | |
203 | { | |
204 | int n = the_list->FindString(data); | |
205 | if (n > -1) | |
206 | the_list->SetSelection(n); | |
207 | } | |
208 | return TRUE; | |
209 | } | |
210 | ||
211 | bool MyConnection::OnDisconnect() | |
212 | { | |
213 | frame->Destroy(); | |
214 | ||
215 | the_connection = NULL; | |
216 | delete this; | |
217 | ||
218 | return TRUE; | |
219 | } | |
220 |