]> git.saurik.com Git - wxWidgets.git/blame - samples/ipc/server.cpp
made wx[Array]TreeItemId[s] more backwards compatible
[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
600683ca 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
4b89c618
VZ
85 return TRUE;
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)
39d580f7 101 : wxFrame(frame, -1, title, wxDefaultPosition, wxSize(350, 250))
4b89c618 102{
4b89c618 103 CreateStatusBar();
7921cf2b 104
4b89c618
VZ
105 // Give it an icon
106 SetIcon(wxICON(mondrian));
7921cf2b 107
4b89c618
VZ
108 // Make a menubar
109 wxMenu *file_menu = new wxMenu;
7921cf2b 110
600683ca 111 file_menu->Append(SERVER_EXIT, _T("&Quit\tCtrl-Q"));
7921cf2b 112
4b89c618 113 wxMenuBar *menu_bar = new wxMenuBar;
7921cf2b 114
600683ca 115 menu_bar->Append(file_menu, _T("&File"));
7921cf2b 116
4b89c618
VZ
117 // Associate the menu bar with the frame
118 SetMenuBar(menu_bar);
7921cf2b 119
39d580f7
VZ
120 // Make a listbox
121 wxListBox *list = new wxListBox(this, SERVER_LISTBOX, wxPoint(5, 5));
600683ca
MB
122 list->Append(_T("Apple"));
123 list->Append(_T("Pear"));
124 list->Append(_T("Orange"));
125 list->Append(_T("Banana"));
126 list->Append(_T("Fruit"));
7921cf2b
JS
127}
128
129// Set the client process's listbox to this item
f6bcfd97 130void MyFrame::OnListBoxClick(wxCommandEvent& WXUNUSED(event))
7921cf2b 131{
39d580f7 132 wxListBox* listBox = (wxListBox*) FindWindow(SERVER_LISTBOX);
7921cf2b
JS
133 if (listBox)
134 {
135 wxString value = listBox->GetStringSelection();
f010ad48
JS
136
137 /* Because the_connection only holds one connection, in this sample only
138 one connection can receive advise messages */
7921cf2b
JS
139 if (the_connection)
140 {
600683ca 141 the_connection->Advise(IPC_ADVISE_NAME, (wxChar*)value.c_str());
7921cf2b
JS
142 }
143 }
144}
145
f6bcfd97
BP
146void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
147{
148 Close(TRUE);
149}
150
4b89c618
VZ
151// ----------------------------------------------------------------------------
152// IPCDialogBox
153// ----------------------------------------------------------------------------
7921cf2b 154
4b89c618
VZ
155IPCDialogBox::IPCDialogBox(wxWindow *parent, const wxString& title,
156 const wxPoint& pos, const wxSize& size,
157 MyConnection *connection)
158 : wxDialog(parent, -1, title, pos, size)
7921cf2b 159{
4b89c618 160 m_connection = connection;
600683ca 161 (void)new wxButton(this, SERVER_QUIT_BUTTON, _T("Quit this connection"),
4b89c618
VZ
162 wxPoint(5, 5));
163 Fit();
7921cf2b
JS
164}
165
f010ad48
JS
166IPCDialogBox::~IPCDialogBox( )
167{
168 // wxWindows exit code destroys dialog before destroying the connection in
169 // OnExit, so make sure connection won't try to delete the dialog later.
170 if (m_connection)
171 m_connection->dialog = NULL;
172}
173
7921cf2b
JS
174void IPCDialogBox::OnQuit(wxCommandEvent& event)
175{
4b89c618
VZ
176 m_connection->Disconnect();
177 delete m_connection;
7921cf2b
JS
178}
179
4b89c618
VZ
180// ----------------------------------------------------------------------------
181// MyServer
182// ----------------------------------------------------------------------------
183
7921cf2b
JS
184wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
185{
d5172a58 186 if ( topic == IPC_TOPIC )
f010ad48 187 return new MyConnection();
d5172a58
VZ
188
189 // unknown topic
190 return NULL;
7921cf2b
JS
191}
192
4b89c618
VZ
193// ----------------------------------------------------------------------------
194// MyConnection
195// ----------------------------------------------------------------------------
196
f010ad48
JS
197MyConnection::MyConnection()
198 : wxConnection()
7921cf2b 199{
600683ca 200 dialog = new IPCDialogBox(wxTheApp->GetTopWindow(), _T("Connection"),
4b89c618
VZ
201 wxPoint(100, 100), wxSize(500, 500), this);
202 dialog->Show(TRUE);
203 the_connection = this;
7921cf2b
JS
204}
205
4b89c618 206MyConnection::~MyConnection()
7921cf2b 207{
4b89c618
VZ
208 if (the_connection)
209 {
f010ad48
JS
210 if (dialog)
211 {
212 dialog->m_connection = NULL;
213 dialog->Destroy();
214 }
4b89c618
VZ
215 the_connection = NULL;
216 }
7921cf2b
JS
217}
218
4b89c618 219bool MyConnection::OnExecute(const wxString& WXUNUSED(topic),
600683ca 220 wxChar *data,
4b89c618
VZ
221 int WXUNUSED(size),
222 wxIPCFormat WXUNUSED(format))
7921cf2b 223{
4fce73fc 224 wxLogStatus(wxT("Execute command: %s"), data);
4b89c618 225 return TRUE;
7921cf2b
JS
226}
227
4b89c618
VZ
228bool MyConnection::OnPoke(const wxString& WXUNUSED(topic),
229 const wxString& item,
600683ca 230 wxChar *data,
4b89c618
VZ
231 int WXUNUSED(size),
232 wxIPCFormat WXUNUSED(format))
7921cf2b 233{
4fce73fc 234 wxLogStatus(wxT("Poke command: %s = %s"), item.c_str(), data);
4b89c618 235 return TRUE;
7921cf2b
JS
236}
237
600683ca 238wxChar *MyConnection::OnRequest(const wxString& WXUNUSED(topic),
4b89c618
VZ
239 const wxString& WXUNUSED(item),
240 int * WXUNUSED(size),
241 wxIPCFormat WXUNUSED(format))
7921cf2b 242{
600683ca 243 return _T("Here, have your data, client!");
7921cf2b
JS
244}
245
4b89c618
VZ
246bool MyConnection::OnStartAdvise(const wxString& WXUNUSED(topic),
247 const wxString& WXUNUSED(item))
7921cf2b 248{
4b89c618 249 return TRUE;
7921cf2b
JS
250}
251