]> git.saurik.com Git - wxWidgets.git/blame - samples/ipc/client.cpp
Order of initializations according to class construction.
[wxWidgets.git] / samples / ipc / client.cpp
CommitLineData
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.
7921cf2b
JS
33#include "ddesetup.h"
34
618f2efa 35#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
7921cf2b
JS
36#include "mondrian.xpm"
37#endif
38
39#include "client.h"
40
4b89c618
VZ
41// ----------------------------------------------------------------------------
42// wxWin macros
43// ----------------------------------------------------------------------------
7921cf2b
JS
44
45IMPLEMENT_APP(MyApp)
46
4b89c618
VZ
47BEGIN_EVENT_TABLE(MyFrame, wxFrame)
48 EVT_MENU(CLIENT_QUIT, MyFrame::OnExit)
49 EVT_MENU(CLIENT_EXECUTE, MyFrame::OnExecute)
50 EVT_MENU(CLIENT_POKE, MyFrame::OnPoke)
51 EVT_MENU(CLIENT_REQUEST, MyFrame::OnRequest)
52END_EVENT_TABLE()
53
54// ----------------------------------------------------------------------------
55// globals
56// ----------------------------------------------------------------------------
57
7921cf2b
JS
58wxListBox *the_list = NULL;
59
60MyConnection *the_connection = NULL;
2f6c54eb 61MyClient *my_client;
7921cf2b 62
4b89c618
VZ
63// ============================================================================
64// implementation
65// ============================================================================
66
67// ----------------------------------------------------------------------------
68// MyApp
69// ----------------------------------------------------------------------------
70
7921cf2b
JS
71// The `main program' equivalent, creating the windows and returning the
72// main frame
73bool MyApp::OnInit()
74{
f6bcfd97 75 // service name (DDE classes) or port number (TCP/IP based classes)
d5172a58 76 wxString service = IPC_SERVICE;
f6bcfd97
BP
77
78 // ignored under DDE, host name in TCP/IP based classes
600683ca 79 wxString hostName = _T("localhost");
7921cf2b 80
4b89c618 81 if (argc > 1)
f6bcfd97 82 service = argv[1];
4b89c618
VZ
83 if (argc > 2)
84 hostName = argv[2];
7921cf2b 85
4b89c618
VZ
86 // Create a new client
87 my_client = new MyClient;
7921cf2b 88
b7e19213 89 // suppress the log messages from MakeConnection()
4b89c618 90 {
b7e19213 91 wxLogNull nolog;
d5172a58
VZ
92 the_connection = (MyConnection *)
93 my_client->MakeConnection(hostName, service, IPC_TOPIC);
b7e19213
VZ
94
95 while ( !the_connection )
4b89c618 96 {
600683ca
MB
97 if ( wxMessageBox(_T("Failed to make connection to server.\nRetry?"),
98 _T("Client Demo Error"),
b7e19213
VZ
99 wxICON_ERROR | wxYES_NO | wxCANCEL ) != wxYES )
100 {
101 // no server
b62ca03d 102 return false;
b7e19213
VZ
103 }
104
600683ca 105 the_connection = (MyConnection *)my_client->MakeConnection(hostName, service, _T("IPC TEST"));
4b89c618 106 }
4b89c618 107 }
7921cf2b 108
d5172a58 109 if (!the_connection->StartAdvise(IPC_ADVISE_NAME))
600683ca 110 wxMessageBox(_T("StartAdvise failed"), _T("Client Demo Error"));
7921cf2b 111
4b89c618 112 // Create the main frame window
b62ca03d 113 (new MyFrame(NULL, _T("Client")))->Show(true);
b178e0c7 114
b62ca03d 115 return true;
7921cf2b
JS
116}
117
118int MyApp::OnExit()
119{
4b89c618 120 // will delete the connection too
2f6c54eb
VZ
121 // Update: Seems it didn't delete the_connection, because there's a leak.
122 // Deletion is now explicitly done a few lines up.
f010ad48
JS
123 // another Update: in fact it's because OnDisconnect should delete it, but
124 // it wasn't
4b89c618 125 delete my_client;
7921cf2b 126
2f6c54eb
VZ
127
128
7921cf2b
JS
129 return 0;
130}
131
7921cf2b 132// Define my frame constructor
4b89c618 133MyFrame::MyFrame(wxFrame *frame, const wxString& title)
b62ca03d 134 : wxFrame(frame, wxID_ANY, title, wxDefaultPosition, wxSize(300, 200))
7921cf2b 135{
4b89c618
VZ
136 // Give it an icon
137 SetIcon(wxICON(mondrian));
138
139 // Make a menubar
140 wxMenu *file_menu = new wxMenu;
141
600683ca
MB
142 file_menu->Append(CLIENT_EXECUTE, _T("&Execute\tCtrl-E"));
143 file_menu->Append(CLIENT_REQUEST, _T("&Request\tCtrl-R"));
144 file_menu->Append(CLIENT_POKE, _T("&Poke\tCtrl-P"));
145 file_menu->Append(CLIENT_QUIT, _T("&Quit\tCtrl-Q"));
4b89c618
VZ
146
147 wxMenuBar *menu_bar = new wxMenuBar;
148
600683ca 149 menu_bar->Append(file_menu, _T("&File"));
4b89c618
VZ
150
151 // Associate the menu bar with the frame
152 SetMenuBar(menu_bar);
153
39d580f7
VZ
154 // Make a listbox which shows the choices made in the server
155 the_list = new wxListBox(this, CLIENT_LISTBOX, wxPoint(5, 5));
600683ca
MB
156 the_list->Append(_T("Apple"));
157 the_list->Append(_T("Pear"));
158 the_list->Append(_T("Orange"));
159 the_list->Append(_T("Banana"));
160 the_list->Append(_T("Fruit"));
7921cf2b
JS
161}
162
87728739 163void MyFrame::OnExecute(wxCommandEvent& WXUNUSED(event))
7921cf2b 164{
4b89c618 165 if (the_connection)
600683ca
MB
166 if (!the_connection->Execute(_T("Hello from the client!")))
167 wxMessageBox(_T("Execute failed"), _T("Client Demo Error"));
7921cf2b
JS
168}
169
87728739 170void MyFrame::OnPoke(wxCommandEvent& WXUNUSED(event))
7921cf2b 171{
4b89c618 172 if (the_connection)
600683ca
MB
173 if (!the_connection->Poke(_T("An item"), _T("Some data to poke at the server!")))
174 wxMessageBox(_T("Poke failed"), _T("Client Demo Error"));
7921cf2b
JS
175}
176
87728739 177void MyFrame::OnRequest(wxCommandEvent& WXUNUSED(event))
7921cf2b 178{
4b89c618
VZ
179 if (the_connection)
180 {
600683ca 181 wxChar *data = the_connection->Request(_T("An item"));
7921cf2b 182 if (data)
600683ca 183 wxMessageBox(data, _T("Client: Request"), wxOK);
b178e0c7 184 else
600683ca 185 wxMessageBox(_T("Request failed"), _T("Client Demo Error"));
4b89c618 186 }
7921cf2b
JS
187}
188
87728739 189void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
7921cf2b 190{
4b89c618 191 Close();
7921cf2b
JS
192}
193
4b89c618 194wxConnectionBase *MyClient::OnMakeConnection()
7921cf2b 195{
4b89c618 196 return new MyConnection;
7921cf2b
JS
197}
198
87728739 199bool MyConnection::OnAdvise(const wxString& WXUNUSED(topic), const wxString& WXUNUSED(item), wxChar *data, int WXUNUSED(size), wxIPCFormat WXUNUSED(format))
7921cf2b 200{
4b89c618
VZ
201 if (the_list)
202 {
203 int n = the_list->FindString(data);
b62ca03d 204 if (n > wxNOT_FOUND)
4b89c618
VZ
205 the_list->SetSelection(n);
206 }
b62ca03d 207 return true;
7921cf2b
JS
208}
209
210bool MyConnection::OnDisconnect()
211{
f010ad48 212 // when connection is terminated, quit whole program
4b89c618
VZ
213 wxWindow *win = wxTheApp->GetTopWindow();
214 if ( win )
215 win->Destroy();
7921cf2b 216
f010ad48
JS
217 // delete self
218 the_connection = NULL;
219 return wxConnection::OnDisconnect();
7921cf2b
JS
220}
221