]>
Commit | Line | Data |
---|---|---|
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"; | |
82 | wxString hostName = wxGetHostName(); | |
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 | if (!the_connection) | |
94 | { | |
95 | wxMessageBox("Failed to make connection to server", "Client Demo Error"); | |
96 | #ifdef __WXMSW__ | |
97 | // extern void wxPrintDDEError(); | |
98 | // wxPrintDDEError(); | |
99 | #endif | |
100 | return FALSE; | |
101 | } | |
102 | the_connection->StartAdvise("Item"); | |
103 | ||
104 | frame->Show(TRUE); | |
105 | ||
106 | return TRUE; | |
107 | } | |
108 | ||
109 | int MyApp::OnExit() | |
110 | { | |
111 | if (my_client) | |
112 | delete my_client ; | |
113 | ||
114 | return 0; | |
115 | } | |
116 | ||
117 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
118 | EVT_MENU(CLIENT_QUIT, MyFrame::OnExit) | |
119 | EVT_MENU(CLIENT_EXECUTE, MyFrame::OnExecute) | |
120 | EVT_MENU(CLIENT_POKE, MyFrame::OnPoke) | |
121 | EVT_MENU(CLIENT_REQUEST, MyFrame::OnRequest) | |
122 | EVT_CLOSE(MyFrame::OnCloseWindow) | |
123 | END_EVENT_TABLE() | |
124 | ||
125 | // Define my frame constructor | |
126 | MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size): | |
127 | wxFrame(frame, -1, title, pos, size) | |
128 | { | |
129 | panel = NULL; | |
130 | } | |
131 | ||
132 | void MyFrame::OnExecute(wxCommandEvent& event) | |
133 | { | |
134 | if (the_connection) | |
135 | the_connection->Execute("Hello from the client!"); | |
136 | } | |
137 | ||
138 | void MyFrame::OnPoke(wxCommandEvent& event) | |
139 | { | |
140 | if (the_connection) | |
141 | the_connection->Poke("An item", "Some data to poke at the server!"); | |
142 | } | |
143 | ||
144 | void MyFrame::OnRequest(wxCommandEvent& event) | |
145 | { | |
146 | if (the_connection) | |
147 | { | |
148 | char *data = the_connection->Request("An item"); | |
149 | if (data) | |
150 | wxMessageBox(data, "Client: Request", wxOK); | |
151 | } | |
152 | } | |
153 | ||
154 | void MyFrame::OnExit(wxCommandEvent& event) | |
155 | { | |
156 | if (the_connection) | |
157 | the_connection->Disconnect(); | |
158 | ||
159 | this->Destroy(); | |
160 | } | |
161 | ||
162 | // Define the behaviour for the frame closing | |
163 | void MyFrame::OnCloseWindow(wxCloseEvent& event) | |
164 | { | |
165 | if (the_connection) | |
166 | { | |
167 | the_connection->Disconnect(); | |
168 | } | |
169 | this->Destroy(); | |
170 | } | |
171 | ||
172 | MyClient::MyClient(void) | |
173 | { | |
174 | } | |
175 | ||
176 | wxConnectionBase *MyClient::OnMakeConnection(void) | |
177 | { | |
178 | return new MyConnection; | |
179 | } | |
180 | ||
181 | MyConnection::MyConnection(void):wxConnection(ipc_buffer, 3999) | |
182 | { | |
183 | } | |
184 | ||
185 | MyConnection::~MyConnection(void) | |
186 | { | |
187 | the_connection = NULL; | |
188 | } | |
189 | ||
190 | bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format) | |
191 | { | |
192 | if (the_list) | |
193 | { | |
194 | int n = the_list->FindString(data); | |
195 | if (n > -1) | |
196 | the_list->SetSelection(n); | |
197 | } | |
198 | return TRUE; | |
199 | } | |
200 | ||
201 | bool MyConnection::OnDisconnect() | |
202 | { | |
203 | frame->Destroy(); | |
204 | ||
205 | the_connection = NULL; | |
206 | delete this; | |
207 | ||
208 | return TRUE; | |
209 | } | |
210 |