d5172a588c
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15144 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
231 lines
6.1 KiB
C++
231 lines
6.1 KiB
C++
/////////////////////////////////////////////////////////////////////////////
|
|
// Name: client.cpp
|
|
// Purpose: DDE sample: client
|
|
// Author: Julian Smart
|
|
// Modified by:
|
|
// Created: 25/01/99
|
|
// RCS-ID: $Id$
|
|
// Copyright: (c) Julian Smart
|
|
// Licence: wxWindows licence
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ============================================================================
|
|
// declarations
|
|
// ============================================================================
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// headers
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// For compilers that support precompilation, includes "wx.h".
|
|
#include "wx/wxprec.h"
|
|
|
|
#ifdef __BORLANDC__
|
|
#pragma hdrstop
|
|
#endif
|
|
|
|
#ifndef WX_PRECOMP
|
|
#include "wx/wx.h"
|
|
#endif
|
|
|
|
// Settings common to both executables: determines whether
|
|
// we're using TCP/IP or real DDE.
|
|
|
|
#include "ddesetup.h"
|
|
|
|
#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
|
|
#include "mondrian.xpm"
|
|
#endif
|
|
|
|
#include "client.h"
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// wxWin macros
|
|
// ----------------------------------------------------------------------------
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
|
EVT_MENU(CLIENT_QUIT, MyFrame::OnExit)
|
|
EVT_MENU(CLIENT_EXECUTE, MyFrame::OnExecute)
|
|
EVT_MENU(CLIENT_POKE, MyFrame::OnPoke)
|
|
EVT_MENU(CLIENT_REQUEST, MyFrame::OnRequest)
|
|
END_EVENT_TABLE()
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// globals
|
|
// ----------------------------------------------------------------------------
|
|
|
|
char ipc_buffer[4000];
|
|
wxListBox *the_list = NULL;
|
|
|
|
MyConnection *the_connection = NULL;
|
|
MyClient *my_client;
|
|
|
|
// ============================================================================
|
|
// implementation
|
|
// ============================================================================
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// MyApp
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// The `main program' equivalent, creating the windows and returning the
|
|
// main frame
|
|
bool MyApp::OnInit()
|
|
{
|
|
// service name (DDE classes) or port number (TCP/IP based classes)
|
|
wxString service = IPC_SERVICE;
|
|
|
|
// ignored under DDE, host name in TCP/IP based classes
|
|
wxString hostName = "localhost";
|
|
|
|
if (argc > 1)
|
|
service = argv[1];
|
|
if (argc > 2)
|
|
hostName = argv[2];
|
|
|
|
// Create a new client
|
|
my_client = new MyClient;
|
|
|
|
// suppress the log messages from MakeConnection()
|
|
{
|
|
wxLogNull nolog;
|
|
the_connection = (MyConnection *)
|
|
my_client->MakeConnection(hostName, service, IPC_TOPIC);
|
|
|
|
while ( !the_connection )
|
|
{
|
|
if ( wxMessageBox("Failed to make connection to server.\nRetry?",
|
|
"Client Demo Error",
|
|
wxICON_ERROR | wxYES_NO | wxCANCEL ) != wxYES )
|
|
{
|
|
// no server
|
|
return FALSE;
|
|
}
|
|
|
|
the_connection = (MyConnection *)my_client->MakeConnection(hostName, service, "IPC TEST");
|
|
}
|
|
}
|
|
|
|
if (!the_connection->StartAdvise(IPC_ADVISE_NAME))
|
|
wxMessageBox("StartAdvise failed", "Client Demo Error");
|
|
|
|
// Create the main frame window
|
|
(new MyFrame(NULL, "Client"))->Show(TRUE);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
int MyApp::OnExit()
|
|
{
|
|
if (the_connection)
|
|
{
|
|
the_connection->Disconnect();
|
|
delete the_connection;
|
|
the_connection = NULL;
|
|
}
|
|
|
|
// will delete the connection too
|
|
// Update: Seems it didn't delete the_connection, because there's a leak.
|
|
// Deletion is now explicitly done a few lines up.
|
|
delete my_client;
|
|
|
|
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Define my frame constructor
|
|
MyFrame::MyFrame(wxFrame *frame, const wxString& title)
|
|
: wxFrame(frame, -1, title, wxDefaultPosition, wxSize(300, 200))
|
|
{
|
|
// Give it an icon
|
|
SetIcon(wxICON(mondrian));
|
|
|
|
// Make a menubar
|
|
wxMenu *file_menu = new wxMenu;
|
|
|
|
file_menu->Append(CLIENT_EXECUTE, "&Execute\tCtrl-E");
|
|
file_menu->Append(CLIENT_REQUEST, "&Request\tCtrl-R");
|
|
file_menu->Append(CLIENT_POKE, "&Poke\tCtrl-P");
|
|
file_menu->Append(CLIENT_QUIT, "&Quit\tCtrl-Q");
|
|
|
|
wxMenuBar *menu_bar = new wxMenuBar;
|
|
|
|
menu_bar->Append(file_menu, "&File");
|
|
|
|
// Associate the menu bar with the frame
|
|
SetMenuBar(menu_bar);
|
|
|
|
// Make a listbox which shows the choices made in the server
|
|
the_list = new wxListBox(this, CLIENT_LISTBOX, wxPoint(5, 5));
|
|
the_list->Append("Apple");
|
|
the_list->Append("Pear");
|
|
the_list->Append("Orange");
|
|
the_list->Append("Banana");
|
|
the_list->Append("Fruit");
|
|
}
|
|
|
|
void MyFrame::OnExecute(wxCommandEvent& event)
|
|
{
|
|
if (the_connection)
|
|
if (!the_connection->Execute("Hello from the client!"))
|
|
wxMessageBox("Execute failed", "Client Demo Error");
|
|
}
|
|
|
|
void MyFrame::OnPoke(wxCommandEvent& event)
|
|
{
|
|
if (the_connection)
|
|
if (!the_connection->Poke("An item", "Some data to poke at the server!"))
|
|
wxMessageBox("Poke failed", "Client Demo Error");
|
|
}
|
|
|
|
void MyFrame::OnRequest(wxCommandEvent& event)
|
|
{
|
|
if (the_connection)
|
|
{
|
|
char *data = the_connection->Request("An item");
|
|
if (data)
|
|
wxMessageBox(data, "Client: Request", wxOK);
|
|
else
|
|
wxMessageBox("Request failed", "Client Demo Error");
|
|
}
|
|
}
|
|
|
|
void MyFrame::OnExit(wxCommandEvent& event)
|
|
{
|
|
Close();
|
|
}
|
|
|
|
wxConnectionBase *MyClient::OnMakeConnection()
|
|
{
|
|
return new MyConnection;
|
|
}
|
|
|
|
MyConnection::MyConnection()
|
|
: wxConnection(ipc_buffer, WXSIZEOF(ipc_buffer))
|
|
{
|
|
}
|
|
|
|
bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format)
|
|
{
|
|
if (the_list)
|
|
{
|
|
int n = the_list->FindString(data);
|
|
if (n > -1)
|
|
the_list->SetSelection(n);
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
bool MyConnection::OnDisconnect()
|
|
{
|
|
wxWindow *win = wxTheApp->GetTopWindow();
|
|
if ( win )
|
|
win->Destroy();
|
|
|
|
return TRUE;
|
|
}
|
|
|