]>
git.saurik.com Git - wxWidgets.git/blob - samples/ipc/client.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: DDE sample: client
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
31 // Settings common to both executables: determines whether
32 // we're using TCP/IP or real DDE.
36 #if defined(__WXGTK__) || defined(__WXMOTIF__)
37 #include "mondrian.xpm"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
48 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
49 EVT_MENU(CLIENT_QUIT
, MyFrame::OnExit
)
50 EVT_MENU(CLIENT_EXECUTE
, MyFrame::OnExecute
)
51 EVT_MENU(CLIENT_POKE
, MyFrame::OnPoke
)
52 EVT_MENU(CLIENT_REQUEST
, MyFrame::OnRequest
)
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 char ipc_buffer
[4000];
60 wxListBox
*the_list
= NULL
;
62 MyConnection
*the_connection
= NULL
;
65 // ============================================================================
67 // ============================================================================
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // The `main program' equivalent, creating the windows and returning the
77 wxString server
= "4242";
78 wxString hostName
= wxGetHostName();
85 // Create a new client
86 my_client
= new MyClient
;
88 // suppress the log messages from MakeConnection()
91 the_connection
= (MyConnection
*)my_client
->MakeConnection(hostName
, server
, "IPC TEST");
93 while ( !the_connection
)
95 if ( wxMessageBox("Failed to make connection to server.\nRetry?",
97 wxICON_ERROR
| wxYES_NO
| wxCANCEL
) != wxYES
)
103 the_connection
= (MyConnection
*)my_client
->MakeConnection(hostName
, server
, "IPC TEST");
107 if (!the_connection
->StartAdvise("Item"))
108 wxMessageBox("StartAdvise failed", "Client Demo Error");
110 // Create the main frame window
111 (new MyFrame(NULL
, "Client"))->Show(TRUE
);
120 the_connection
->Disconnect();
123 // will delete the connection too
129 // Define my frame constructor
130 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
)
131 : wxFrame(frame
, -1, title
)
136 SetIcon(wxICON(mondrian
));
139 wxMenu
*file_menu
= new wxMenu
;
141 file_menu
->Append(CLIENT_EXECUTE
, "Execute");
142 file_menu
->Append(CLIENT_REQUEST
, "Request");
143 file_menu
->Append(CLIENT_POKE
, "Poke");
144 file_menu
->Append(CLIENT_QUIT
, "Quit");
146 wxMenuBar
*menu_bar
= new wxMenuBar
;
148 menu_bar
->Append(file_menu
, "File");
150 // Associate the menu bar with the frame
151 SetMenuBar(menu_bar
);
154 panel
= new wxPanel(this);
155 the_list
= new wxListBox(panel
, CLIENT_LISTBOX
, wxPoint(5, 5));
156 the_list
->Append("Apple");
157 the_list
->Append("Pear");
158 the_list
->Append("Orange");
159 the_list
->Append("Banana");
160 the_list
->Append("Fruit");
166 void MyFrame::OnExecute(wxCommandEvent
& event
)
169 if (!the_connection
->Execute("Hello from the client!"))
170 wxMessageBox("Execute failed", "Client Demo Error");
173 void MyFrame::OnPoke(wxCommandEvent
& event
)
176 if (!the_connection
->Poke("An item", "Some data to poke at the server!"))
177 wxMessageBox("Poke failed", "Client Demo Error");
180 void MyFrame::OnRequest(wxCommandEvent
& event
)
184 char *data
= the_connection
->Request("An item");
186 wxMessageBox(data
, "Client: Request", wxOK
);
188 wxMessageBox("Request failed", "Client Demo Error");
192 void MyFrame::OnExit(wxCommandEvent
& event
)
197 wxConnectionBase
*MyClient::OnMakeConnection()
199 return new MyConnection
;
202 MyConnection::MyConnection()
203 : wxConnection(ipc_buffer
, WXSIZEOF(ipc_buffer
))
207 MyConnection::~MyConnection()
209 the_connection
= NULL
;
212 bool MyConnection::OnAdvise(const wxString
& topic
, const wxString
& item
, char *data
, int size
, wxIPCFormat format
)
216 int n
= the_list
->FindString(data
);
218 the_list
->SetSelection(n
);
223 bool MyConnection::OnDisconnect()
225 wxWindow
*win
= wxTheApp
->GetTopWindow();