]> git.saurik.com Git - wxWidgets.git/blame - samples/ipc/server.cpp
better approach to centering windows (in absence of somebody else's solution...)
[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
42993df2 35#if defined(__WXGTK__) || 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
JS
60char ipc_buffer[4000];
61MyConnection *the_connection = NULL;
4b89c618
VZ
62
63// ============================================================================
64// implementation
65// ============================================================================
66
67// ----------------------------------------------------------------------------
68// MyApp
69// ----------------------------------------------------------------------------
7921cf2b
JS
70
71bool MyApp::OnInit()
72{
4b89c618 73 // Create the main frame window
ed323aea 74 (new MyFrame(NULL, "Server"))->Show(TRUE);
7921cf2b 75
f6bcfd97
BP
76 // service name (DDE classes) or port number (TCP/IP based classes)
77 wxString service = "4242";
78
4b89c618 79 if (argc > 1)
f6bcfd97 80 service = argv[1];
7921cf2b 81
4b89c618
VZ
82 // Create a new server
83 m_server = new MyServer;
f6bcfd97 84 m_server->Create(service);
7921cf2b 85
4b89c618
VZ
86 return TRUE;
87}
7921cf2b 88
4b89c618
VZ
89int MyApp::OnExit()
90{
91 delete m_server;
92
93 return 0;
94}
7921cf2b 95
4b89c618
VZ
96// ----------------------------------------------------------------------------
97// MyFrame
98// ----------------------------------------------------------------------------
7921cf2b 99
4b89c618
VZ
100// Define my frame constructor
101MyFrame::MyFrame(wxFrame *frame, const wxString& title)
102 : wxFrame(frame, -1, title)
103{
104 panel = NULL;
7921cf2b 105
4b89c618 106 CreateStatusBar();
7921cf2b 107
4b89c618
VZ
108 // Give it an icon
109 SetIcon(wxICON(mondrian));
7921cf2b 110
4b89c618
VZ
111 // Make a menubar
112 wxMenu *file_menu = new wxMenu;
7921cf2b 113
f6bcfd97 114 file_menu->Append(SERVER_EXIT, "&Exit");
7921cf2b 115
4b89c618 116 wxMenuBar *menu_bar = new wxMenuBar;
7921cf2b 117
4b89c618 118 menu_bar->Append(file_menu, "&File");
7921cf2b 119
4b89c618
VZ
120 // Associate the menu bar with the frame
121 SetMenuBar(menu_bar);
7921cf2b 122
4b89c618
VZ
123 // Make a panel
124 panel = new wxPanel(this);
125 wxListBox *list = new wxListBox(panel, SERVER_LISTBOX, wxPoint(5, 5));
126 list->Append("Apple");
127 list->Append("Pear");
128 list->Append("Orange");
129 list->Append("Banana");
130 list->Append("Fruit");
7921cf2b 131
4b89c618
VZ
132 panel->Fit();
133 Fit();
7921cf2b
JS
134}
135
136// Set the client process's listbox to this item
f6bcfd97 137void MyFrame::OnListBoxClick(wxCommandEvent& WXUNUSED(event))
7921cf2b
JS
138{
139 wxListBox* listBox = (wxListBox*) panel->FindWindow(SERVER_LISTBOX);
140 if (listBox)
141 {
142 wxString value = listBox->GetStringSelection();
143 if (the_connection)
144 {
4b89c618 145 the_connection->Advise("Item", (wxChar *)value.c_str());
7921cf2b
JS
146 }
147 }
148}
149
f6bcfd97
BP
150void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
151{
152 Close(TRUE);
153}
154
4b89c618
VZ
155// ----------------------------------------------------------------------------
156// IPCDialogBox
157// ----------------------------------------------------------------------------
7921cf2b 158
4b89c618
VZ
159IPCDialogBox::IPCDialogBox(wxWindow *parent, const wxString& title,
160 const wxPoint& pos, const wxSize& size,
161 MyConnection *connection)
162 : wxDialog(parent, -1, title, pos, size)
7921cf2b 163{
4b89c618
VZ
164 m_connection = connection;
165 (void)new wxButton(this, SERVER_QUIT_BUTTON, "Quit this connection",
166 wxPoint(5, 5));
167 Fit();
7921cf2b
JS
168}
169
170void IPCDialogBox::OnQuit(wxCommandEvent& event)
171{
4b89c618
VZ
172 m_connection->Disconnect();
173 delete m_connection;
7921cf2b
JS
174}
175
4b89c618
VZ
176// ----------------------------------------------------------------------------
177// MyServer
178// ----------------------------------------------------------------------------
179
7921cf2b
JS
180wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
181{
4b89c618
VZ
182 if (strcmp(topic, "STDIO") != 0 && strcmp(topic, "IPC TEST") == 0)
183 return new MyConnection(ipc_buffer, WXSIZEOF(ipc_buffer));
184 else
185 return NULL;
7921cf2b
JS
186}
187
4b89c618
VZ
188// ----------------------------------------------------------------------------
189// MyConnection
190// ----------------------------------------------------------------------------
191
192MyConnection::MyConnection(char *buf, int size)
193 : wxConnection(buf, size)
7921cf2b 194{
4b89c618
VZ
195 dialog = new IPCDialogBox(wxTheApp->GetTopWindow(), "Connection",
196 wxPoint(100, 100), wxSize(500, 500), this);
197 dialog->Show(TRUE);
198 the_connection = this;
7921cf2b
JS
199}
200
4b89c618 201MyConnection::~MyConnection()
7921cf2b 202{
4b89c618
VZ
203 if (the_connection)
204 {
205 dialog->Destroy();
206 the_connection = NULL;
207 }
7921cf2b
JS
208}
209
4b89c618
VZ
210bool MyConnection::OnExecute(const wxString& WXUNUSED(topic),
211 char *data,
212 int WXUNUSED(size),
213 wxIPCFormat WXUNUSED(format))
7921cf2b 214{
4b89c618
VZ
215 wxLogStatus("Execute command: %s", data);
216 return TRUE;
7921cf2b
JS
217}
218
4b89c618
VZ
219bool MyConnection::OnPoke(const wxString& WXUNUSED(topic),
220 const wxString& item,
221 char *data,
222 int WXUNUSED(size),
223 wxIPCFormat WXUNUSED(format))
7921cf2b 224{
6097c3a2 225 wxLogStatus("Poke command: %s = %s", item.c_str(), data);
4b89c618 226 return TRUE;
7921cf2b
JS
227}
228
4b89c618
VZ
229char *MyConnection::OnRequest(const wxString& WXUNUSED(topic),
230 const wxString& WXUNUSED(item),
231 int * WXUNUSED(size),
232 wxIPCFormat WXUNUSED(format))
7921cf2b 233{
4b89c618 234 return "Here, have your data, client!";
7921cf2b
JS
235}
236
4b89c618
VZ
237bool MyConnection::OnStartAdvise(const wxString& WXUNUSED(topic),
238 const wxString& WXUNUSED(item))
7921cf2b 239{
4b89c618 240 return TRUE;
7921cf2b
JS
241}
242