]> git.saurik.com Git - wxWidgets.git/blame - samples/ipc/server.cpp
Enhanced stock labels usage. Source cleaning.
[wxWidgets.git] / samples / ipc / server.cpp
CommitLineData
7921cf2b
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: server.cpp
4b89c618 3// Purpose: IPC sample: server
7921cf2b
JS
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__)
4b89c618 36 #include "mondrian.xpm"
7921cf2b
JS
37#endif
38
39#include "server.h"
40
4b89c618
VZ
41// ----------------------------------------------------------------------------
42// wxWin macros
43// ----------------------------------------------------------------------------
7921cf2b
JS
44
45IMPLEMENT_APP(MyApp)
46
4b89c618 47BEGIN_EVENT_TABLE(MyFrame, wxFrame)
f6bcfd97 48 EVT_MENU (SERVER_EXIT, MyFrame::OnExit)
4b89c618
VZ
49 EVT_LISTBOX(SERVER_LISTBOX, MyFrame::OnListBoxClick)
50END_EVENT_TABLE()
51
52BEGIN_EVENT_TABLE(IPCDialogBox, wxDialog)
53 EVT_BUTTON(SERVER_QUIT_BUTTON, IPCDialogBox::OnQuit)
54END_EVENT_TABLE()
55
56// ----------------------------------------------------------------------------
57// global variables
58// ----------------------------------------------------------------------------
59
7921cf2b 60MyConnection *the_connection = NULL;
4b89c618
VZ
61
62// ============================================================================
63// implementation
64// ============================================================================
65
66// ----------------------------------------------------------------------------
67// MyApp
68// ----------------------------------------------------------------------------
7921cf2b
JS
69
70bool MyApp::OnInit()
71{
4b89c618 72 // Create the main frame window
b62ca03d 73 (new MyFrame(NULL, _T("Server")))->Show(true);
7921cf2b 74
f6bcfd97 75 // service name (DDE classes) or port number (TCP/IP based classes)
d5172a58 76 wxString service = IPC_SERVICE;
f6bcfd97 77
4b89c618 78 if (argc > 1)
f6bcfd97 79 service = argv[1];
7921cf2b 80
4b89c618
VZ
81 // Create a new server
82 m_server = new MyServer;
f6bcfd97 83 m_server->Create(service);
7921cf2b 84
b62ca03d 85 return true;
4b89c618 86}
7921cf2b 87
4b89c618
VZ
88int MyApp::OnExit()
89{
90 delete m_server;
91
92 return 0;
93}
7921cf2b 94
4b89c618
VZ
95// ----------------------------------------------------------------------------
96// MyFrame
97// ----------------------------------------------------------------------------
7921cf2b 98
4b89c618
VZ
99// Define my frame constructor
100MyFrame::MyFrame(wxFrame *frame, const wxString& title)
b62ca03d 101 : wxFrame(frame, wxID_ANY, title, wxDefaultPosition, wxSize(350, 250))
4b89c618 102{
8520f137 103#if wxUSE_STATUSBAR
4b89c618 104 CreateStatusBar();
8520f137 105#endif // wxUSE_STATUSBAR
7921cf2b 106
4b89c618
VZ
107 // Give it an icon
108 SetIcon(wxICON(mondrian));
7921cf2b 109
4b89c618
VZ
110 // Make a menubar
111 wxMenu *file_menu = new wxMenu;
7921cf2b 112
600683ca 113 file_menu->Append(SERVER_EXIT, _T("&Quit\tCtrl-Q"));
7921cf2b 114
4b89c618 115 wxMenuBar *menu_bar = new wxMenuBar;
7921cf2b 116
600683ca 117 menu_bar->Append(file_menu, _T("&File"));
7921cf2b 118
4b89c618
VZ
119 // Associate the menu bar with the frame
120 SetMenuBar(menu_bar);
7921cf2b 121
39d580f7 122 // Make a listbox
b61d8079 123 wxListBox *list = new wxListBox(this, SERVER_LISTBOX);
600683ca
MB
124 list->Append(_T("Apple"));
125 list->Append(_T("Pear"));
126 list->Append(_T("Orange"));
127 list->Append(_T("Banana"));
128 list->Append(_T("Fruit"));
7921cf2b
JS
129}
130
131// Set the client process's listbox to this item
f6bcfd97 132void MyFrame::OnListBoxClick(wxCommandEvent& WXUNUSED(event))
7921cf2b 133{
39d580f7 134 wxListBox* listBox = (wxListBox*) FindWindow(SERVER_LISTBOX);
7921cf2b
JS
135 if (listBox)
136 {
137 wxString value = listBox->GetStringSelection();
f010ad48
JS
138
139 /* Because the_connection only holds one connection, in this sample only
140 one connection can receive advise messages */
7921cf2b
JS
141 if (the_connection)
142 {
600683ca 143 the_connection->Advise(IPC_ADVISE_NAME, (wxChar*)value.c_str());
7921cf2b
JS
144 }
145 }
146}
147
f6bcfd97
BP
148void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
149{
b62ca03d 150 Close(true);
f6bcfd97
BP
151}
152
4b89c618
VZ
153// ----------------------------------------------------------------------------
154// IPCDialogBox
155// ----------------------------------------------------------------------------
7921cf2b 156
4b89c618
VZ
157IPCDialogBox::IPCDialogBox(wxWindow *parent, const wxString& title,
158 const wxPoint& pos, const wxSize& size,
159 MyConnection *connection)
b62ca03d 160 : wxDialog(parent, wxID_ANY, title, pos, size)
7921cf2b 161{
4b89c618 162 m_connection = connection;
600683ca 163 (void)new wxButton(this, SERVER_QUIT_BUTTON, _T("Quit this connection"),
4b89c618
VZ
164 wxPoint(5, 5));
165 Fit();
7921cf2b
JS
166}
167
f010ad48
JS
168IPCDialogBox::~IPCDialogBox( )
169{
be5a51fb 170 // wxWidgets exit code destroys dialog before destroying the connection in
f010ad48
JS
171 // OnExit, so make sure connection won't try to delete the dialog later.
172 if (m_connection)
173 m_connection->dialog = NULL;
174}
175
87728739 176void IPCDialogBox::OnQuit(wxCommandEvent& WXUNUSED(event))
7921cf2b 177{
4b89c618
VZ
178 m_connection->Disconnect();
179 delete m_connection;
7921cf2b
JS
180}
181
4b89c618
VZ
182// ----------------------------------------------------------------------------
183// MyServer
184// ----------------------------------------------------------------------------
185
7921cf2b
JS
186wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
187{
d5172a58 188 if ( topic == IPC_TOPIC )
f010ad48 189 return new MyConnection();
d5172a58
VZ
190
191 // unknown topic
192 return NULL;
7921cf2b
JS
193}
194
4b89c618
VZ
195// ----------------------------------------------------------------------------
196// MyConnection
197// ----------------------------------------------------------------------------
198
f010ad48
JS
199MyConnection::MyConnection()
200 : wxConnection()
7921cf2b 201{
600683ca 202 dialog = new IPCDialogBox(wxTheApp->GetTopWindow(), _T("Connection"),
b61d8079 203 wxDefaultPosition, wxDefaultSize, this);
b62ca03d 204 dialog->Show(true);
4b89c618 205 the_connection = this;
7921cf2b
JS
206}
207
4b89c618 208MyConnection::~MyConnection()
7921cf2b 209{
4b89c618
VZ
210 if (the_connection)
211 {
f010ad48
JS
212 if (dialog)
213 {
214 dialog->m_connection = NULL;
215 dialog->Destroy();
216 }
4b89c618
VZ
217 the_connection = NULL;
218 }
7921cf2b
JS
219}
220
4b89c618 221bool MyConnection::OnExecute(const wxString& WXUNUSED(topic),
600683ca 222 wxChar *data,
4b89c618
VZ
223 int WXUNUSED(size),
224 wxIPCFormat WXUNUSED(format))
7921cf2b 225{
4fce73fc 226 wxLogStatus(wxT("Execute command: %s"), data);
b62ca03d 227 return true;
7921cf2b
JS
228}
229
4b89c618
VZ
230bool MyConnection::OnPoke(const wxString& WXUNUSED(topic),
231 const wxString& item,
600683ca 232 wxChar *data,
4b89c618
VZ
233 int WXUNUSED(size),
234 wxIPCFormat WXUNUSED(format))
7921cf2b 235{
4fce73fc 236 wxLogStatus(wxT("Poke command: %s = %s"), item.c_str(), data);
b62ca03d 237 return true;
7921cf2b
JS
238}
239
600683ca 240wxChar *MyConnection::OnRequest(const wxString& WXUNUSED(topic),
4b89c618
VZ
241 const wxString& WXUNUSED(item),
242 int * WXUNUSED(size),
243 wxIPCFormat WXUNUSED(format))
7921cf2b 244{
600683ca 245 return _T("Here, have your data, client!");
7921cf2b
JS
246}
247
4b89c618
VZ
248bool MyConnection::OnStartAdvise(const wxString& WXUNUSED(topic),
249 const wxString& WXUNUSED(item))
7921cf2b 250{
b62ca03d 251 return true;
7921cf2b
JS
252}
253