]> git.saurik.com Git - wxWidgets.git/blob - samples/ipc/server.cpp
implement wxColourDialog::Move() (patch 548048)
[wxWidgets.git] / samples / ipc / server.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: server.cpp
3 // Purpose: IPC sample: server
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 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/wx.h"
29 #endif
30
31 // Settings common to both executables: determines whether
32 // we're using TCP/IP or real DDE.
33 #include "ddesetup.h"
34
35 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
36 #include "mondrian.xpm"
37 #endif
38
39 #include "server.h"
40
41 // ----------------------------------------------------------------------------
42 // wxWin macros
43 // ----------------------------------------------------------------------------
44
45 IMPLEMENT_APP(MyApp)
46
47 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
48 EVT_MENU (SERVER_EXIT, MyFrame::OnExit)
49 EVT_LISTBOX(SERVER_LISTBOX, MyFrame::OnListBoxClick)
50 END_EVENT_TABLE()
51
52 BEGIN_EVENT_TABLE(IPCDialogBox, wxDialog)
53 EVT_BUTTON(SERVER_QUIT_BUTTON, IPCDialogBox::OnQuit)
54 END_EVENT_TABLE()
55
56 // ----------------------------------------------------------------------------
57 // global variables
58 // ----------------------------------------------------------------------------
59
60 char ipc_buffer[4000];
61 MyConnection *the_connection = NULL;
62
63 // ============================================================================
64 // implementation
65 // ============================================================================
66
67 // ----------------------------------------------------------------------------
68 // MyApp
69 // ----------------------------------------------------------------------------
70
71 bool MyApp::OnInit()
72 {
73 // Create the main frame window
74 (new MyFrame(NULL, "Server"))->Show(TRUE);
75
76 // service name (DDE classes) or port number (TCP/IP based classes)
77 wxString service = IPC_SERVICE;
78
79 if (argc > 1)
80 service = argv[1];
81
82 // Create a new server
83 m_server = new MyServer;
84 m_server->Create(service);
85
86 return TRUE;
87 }
88
89 int MyApp::OnExit()
90 {
91 delete m_server;
92
93 return 0;
94 }
95
96 // ----------------------------------------------------------------------------
97 // MyFrame
98 // ----------------------------------------------------------------------------
99
100 // Define my frame constructor
101 MyFrame::MyFrame(wxFrame *frame, const wxString& title)
102 : wxFrame(frame, -1, title, wxDefaultPosition, wxSize(350, 250))
103 {
104 CreateStatusBar();
105
106 // Give it an icon
107 SetIcon(wxICON(mondrian));
108
109 // Make a menubar
110 wxMenu *file_menu = new wxMenu;
111
112 file_menu->Append(SERVER_EXIT, "&Quit\tCtrl-Q");
113
114 wxMenuBar *menu_bar = new wxMenuBar;
115
116 menu_bar->Append(file_menu, "&File");
117
118 // Associate the menu bar with the frame
119 SetMenuBar(menu_bar);
120
121 // Make a listbox
122 wxListBox *list = new wxListBox(this, SERVER_LISTBOX, wxPoint(5, 5));
123 list->Append("Apple");
124 list->Append("Pear");
125 list->Append("Orange");
126 list->Append("Banana");
127 list->Append("Fruit");
128 }
129
130 // Set the client process's listbox to this item
131 void MyFrame::OnListBoxClick(wxCommandEvent& WXUNUSED(event))
132 {
133 wxListBox* listBox = (wxListBox*) FindWindow(SERVER_LISTBOX);
134 if (listBox)
135 {
136 wxString value = listBox->GetStringSelection();
137 if (the_connection)
138 {
139 the_connection->Advise(IPC_ADVISE_NAME, (wxChar *)value.c_str());
140 }
141 }
142 }
143
144 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
145 {
146 Close(TRUE);
147 }
148
149 // ----------------------------------------------------------------------------
150 // IPCDialogBox
151 // ----------------------------------------------------------------------------
152
153 IPCDialogBox::IPCDialogBox(wxWindow *parent, const wxString& title,
154 const wxPoint& pos, const wxSize& size,
155 MyConnection *connection)
156 : wxDialog(parent, -1, title, pos, size)
157 {
158 m_connection = connection;
159 (void)new wxButton(this, SERVER_QUIT_BUTTON, "Quit this connection",
160 wxPoint(5, 5));
161 Fit();
162 }
163
164 void IPCDialogBox::OnQuit(wxCommandEvent& event)
165 {
166 m_connection->Disconnect();
167 delete m_connection;
168 }
169
170 // ----------------------------------------------------------------------------
171 // MyServer
172 // ----------------------------------------------------------------------------
173
174 wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
175 {
176 if ( topic == IPC_TOPIC )
177 return new MyConnection(ipc_buffer, WXSIZEOF(ipc_buffer));
178
179 // unknown topic
180 return NULL;
181 }
182
183 // ----------------------------------------------------------------------------
184 // MyConnection
185 // ----------------------------------------------------------------------------
186
187 MyConnection::MyConnection(char *buf, int size)
188 : wxConnection(buf, size)
189 {
190 dialog = new IPCDialogBox(wxTheApp->GetTopWindow(), "Connection",
191 wxPoint(100, 100), wxSize(500, 500), this);
192 dialog->Show(TRUE);
193 the_connection = this;
194 }
195
196 MyConnection::~MyConnection()
197 {
198 if (the_connection)
199 {
200 dialog->Destroy();
201 the_connection = NULL;
202 }
203 }
204
205 bool MyConnection::OnExecute(const wxString& WXUNUSED(topic),
206 char *data,
207 int WXUNUSED(size),
208 wxIPCFormat WXUNUSED(format))
209 {
210 wxLogStatus("Execute command: %s", data);
211 return TRUE;
212 }
213
214 bool MyConnection::OnPoke(const wxString& WXUNUSED(topic),
215 const wxString& item,
216 char *data,
217 int WXUNUSED(size),
218 wxIPCFormat WXUNUSED(format))
219 {
220 wxLogStatus("Poke command: %s = %s", item.c_str(), data);
221 return TRUE;
222 }
223
224 char *MyConnection::OnRequest(const wxString& WXUNUSED(topic),
225 const wxString& WXUNUSED(item),
226 int * WXUNUSED(size),
227 wxIPCFormat WXUNUSED(format))
228 {
229 return "Here, have your data, client!";
230 }
231
232 bool MyConnection::OnStartAdvise(const wxString& WXUNUSED(topic),
233 const wxString& WXUNUSED(item))
234 {
235 return TRUE;
236 }
237