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