]> git.saurik.com Git - wxWidgets.git/blob - samples/dde/server.cpp
added SpinCtrl,
[wxWidgets.git] / samples / dde / server.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: server.cpp
3 // Purpose: DDE 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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 // Settings common to both executables: determines whether
24 // we're using TCP/IP or real DDE.
25
26 #include "ddesetup.h"
27
28 #if defined(__WXGTK__) || defined(__WXMOTIF__)
29 #include "mondrian.xpm"
30 #endif
31
32 #include "server.h"
33
34 MyFrame *frame = NULL;
35
36 IMPLEMENT_APP(MyApp)
37
38 char ipc_buffer[4000];
39 MyConnection *the_connection = NULL;
40 MyServer *my_server ;
41
42 bool MyApp::OnInit()
43 {
44 // Create the main frame window
45 frame = new MyFrame(NULL, "Server", wxDefaultPosition, wxSize(400, 500));
46
47 frame->CreateStatusBar();
48
49 // Give it an icon
50 frame->SetIcon(wxICON(mondrian));
51
52 // Make a menubar
53 wxMenu *file_menu = new wxMenu;
54
55 file_menu->Append(SERVER_QUIT, "&Exit");
56
57 wxMenuBar *menu_bar = new wxMenuBar;
58
59 menu_bar->Append(file_menu, "&File");
60
61 // Associate the menu bar with the frame
62 frame->SetMenuBar(menu_bar);
63
64 // Make a panel
65 frame->panel = new wxPanel(frame, 0, 0, 400, 250);
66 wxListBox *list = new wxListBox(frame->panel, SERVER_LISTBOX,
67 wxPoint(5, 5), wxSize(150, 120));
68 list->Append("Apple");
69 list->Append("Pear");
70 list->Append("Orange");
71 list->Append("Banana");
72 list->Append("Fruit");
73
74 frame->panel->Fit();
75 frame->Fit();
76
77 wxString server_name = "4242";
78 if (argc > 1)
79 server_name = argv[1];
80
81 // Create a new server
82 my_server = new MyServer;
83 my_server->Create(server_name);
84
85 frame->Show(TRUE);
86
87 return TRUE;
88 }
89
90 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
91 EVT_MENU(SERVER_QUIT, MyFrame::OnExit)
92 EVT_CLOSE(MyFrame::OnCloseWindow)
93 EVT_LISTBOX(SERVER_LISTBOX, MyFrame::OnListBoxClick)
94 END_EVENT_TABLE()
95
96 // Define my frame constructor
97 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
98 wxFrame(frame, -1, title, pos, size)
99 {
100 panel = NULL;
101 }
102
103 void MyFrame::OnExit(wxCommandEvent& event)
104 {
105 if (my_server)
106 delete my_server;
107 this->Destroy();
108 }
109
110 // Set the client process's listbox to this item
111 void MyFrame::OnListBoxClick(wxCommandEvent& event)
112 {
113 wxListBox* listBox = (wxListBox*) panel->FindWindow(SERVER_LISTBOX);
114 if (listBox)
115 {
116 wxString value = listBox->GetStringSelection();
117 if (the_connection)
118 {
119 the_connection->Advise("Item", (char*) (const char*) value);
120 }
121 }
122 }
123
124 void MyFrame::OnCloseWindow(wxCloseEvent& event)
125 {
126 if (my_server)
127 delete my_server;
128 this->Destroy();
129 }
130
131 BEGIN_EVENT_TABLE(IPCDialogBox, wxDialog)
132 EVT_BUTTON(SERVER_QUIT_BUTTON, IPCDialogBox::OnQuit)
133 END_EVENT_TABLE()
134
135 IPCDialogBox::IPCDialogBox(wxFrame *parent, const wxString& title,
136 const wxPoint& pos, const wxSize& size, MyConnection *the_connection):
137 wxDialog(parent, -1, title, pos, size)
138 {
139 connection = the_connection;
140 (void)new wxButton(this, SERVER_QUIT_BUTTON, "Quit this connection", wxPoint(5, 5));
141 Fit();
142 }
143
144 void IPCDialogBox::OnQuit(wxCommandEvent& event)
145 {
146 connection->Disconnect();
147 delete connection;
148 }
149
150 wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
151 {
152 if (strcmp(topic, "STDIO") != 0 && strcmp(topic, "IPC TEST") == 0)
153 return new MyConnection(ipc_buffer, 4000);
154 else
155 return NULL;
156 }
157
158 MyConnection::MyConnection(char *buf, int size):wxConnection(buf, size)
159 {
160 dialog = new IPCDialogBox(frame, "Connection", wxPoint(100, 100), wxSize(500, 500), this);
161 dialog->Show(TRUE);
162 the_connection = this;
163 }
164
165 MyConnection::~MyConnection(void)
166 {
167 dialog->Destroy();
168 the_connection = NULL;
169 }
170
171 bool MyConnection::OnExecute(const wxString& topic, char *data, int size, wxIPCFormat format)
172 {
173 char buf[300];
174 sprintf(buf, "Execute command: %s", data);
175 frame->SetStatusText(buf);
176 return TRUE;
177 }
178
179 bool MyConnection::OnPoke(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format)
180 {
181 char buf[300];
182 sprintf(buf, "Poke command: %s", data);
183 frame->SetStatusText(buf);
184 return TRUE;
185 }
186
187 char *MyConnection::OnRequest(const wxString& topic, const wxString& item, int *size, wxIPCFormat format)
188 {
189 return "Here, have your data, client!";
190 }
191
192 bool MyConnection::OnStartAdvise(const wxString& topic, const wxString& item)
193 {
194 return TRUE;
195 }
196