summaryrefslogtreecommitdiff
path: root/Bus/Ivy/IvySynchroWnd.cxx
blob: 297c64e76066c02dc8a333d016b69471ab20b9ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// SynchroWnd.cpp : implementation file
//

#include "stdafx.h"

#include "IvySynchroWnd.h"


#define WM_MESSAGE_CB WM_USER + 1001
#define WM_APPCONNECTED_CB WM_USER + 1002
#define WM_APPDISCONNECTED_CB WM_USER + 1003

IvySynchroWnd* IvySynchronousCallback::m_synchro = NULL;
IvySynchroWnd* IvySynchroWnd::m_synchro = NULL;

/////////////////////////////////////////////////////////////////////////////
// IvySynchroWnd

IvySynchroWnd::IvySynchroWnd()
{
	m_hWnd = NULL;
	m_synchro = this;

	WNDCLASS wc; 
 
    // Fill in the window class structure with parameters 
    // that describe the main window. 
 
    wc.style = 0;                     // noredraw if size changes 
    wc.lpfnWndProc = WindowProc;			// points to window procedure 
    wc.cbClsExtra = 0;                // no extra class memory 
    wc.cbWndExtra = 0;                // no extra window memory 
    wc.hInstance = 0;							    // handle to instance 
    wc.hIcon = NULL;									// predefined app. icon 
    wc.hCursor = NULL;                // predefined arrow 
    wc.hbrBackground = 0;             // white background brush 
    wc.lpszMenuName =  NULL;					// no  menu 
    wc.lpszClassName = "IvySynchroClass";  // name of window class 
 
    // Register the window class. 
 
    if ( ! RegisterClass(&wc) )
			{
				TRACE("Warning: unable to create Ivy Synchro notify window!\n");
				//AfxThrowResourceException();
			}

		// Create the syncrho window. 
    m_hWnd  = CreateWindowEx(0, "IvySynchroClass","Ivy Synchro Notification Sink",
				WS_OVERLAPPED, 0, 0, 0, 0, NULL , NULL, NULL, NULL);
		if (!m_hWnd)
			{
				TRACE("Warning: unable to create Ivy Synchro notify window!\n");
				//AfxThrowResourceException();
			}

}
IvySynchroWnd::~IvySynchroWnd()
{
}


LRESULT CALLBACK IvySynchroWnd::WindowProc( HWND hwnd, UINT uMsg,  WPARAM wParam,  LPARAM lParam   )
{
	switch ( uMsg )
		{
		case WM_MESSAGE_CB:
			m_synchro->OnMessageCB( wParam, lParam );
			break;
		case WM_APPCONNECTED_CB:
			m_synchro->OnApplicationConnectedCB( wParam, lParam );
			break;
		case WM_APPDISCONNECTED_CB:
			m_synchro->OnApplicationDisconnectedCB( wParam, lParam );
			break;

    // 
    // Process other messages. 
    // 

    default: 
        return DefWindowProc(hwnd, uMsg, wParam, lParam); 
    } 
    return 0; 
}




/////////////////////////////////////////////////////////////////////////////
// IvySynchroWnd message handlers


LRESULT IvySynchroWnd::OnMessageCB(WPARAM wParam, LPARAM lParam)
{
	IvySynchronousMessageCallback *param = ( IvySynchronousMessageCallback * ) lParam;
//	TRACE( "IvySynchroWnd::OnMessageCB msg count %d\n",wParam);
	param->target->OnMessage( param->app, param->argc, (const char **) param->argv );
	for( int i = 0; i < param->argc ; i++ )
		free( param->argv[i] );		// allocated with strdup 
	delete  param->argv;			// allocated with new
	delete param;
	return 0L;
}
LRESULT IvySynchroWnd::OnApplicationConnectedCB(WPARAM wParam, LPARAM lParam)
{
	IvySynchronousApplicationCallback *param = ( IvySynchronousApplicationCallback * ) lParam;
//	TRACE( "IvySynchroWnd::OnSynchroNotify End Synchro\n");
	param->target->OnApplicationConnected( param->app );
	delete param;
	return 0L;
}
LRESULT IvySynchroWnd::OnApplicationDisconnectedCB(WPARAM wParam, LPARAM lParam)
{
	IvySynchronousApplicationCallback *param = ( IvySynchronousApplicationCallback * ) lParam;
//	TRACE( "IvySynchroWnd::OnSynchroNotify End Synchro\n");
	param->target->OnApplicationDisconnected( param->app );
	delete param;
	return 0L;
}
//
// 
IvySynchronousMessageCallback::IvySynchronousMessageCallback( IvyMessageCallback *cb )
{
	target = cb;
}
void IvySynchronousMessageCallback::OnMessage(IvyApplication *app, int argc, const char **argv )
{
	static int msg_count = 0;
	// duplicate on the Message Queue
	IvySynchronousMessageCallback *param = new IvySynchronousMessageCallback(target);
	param->app = app;
	param->argc = argc;
	param->argv = new char*[argc];
	for( int i = 0; i < argc ; i++ )
		param->argv[i] = strdup( argv[i]);
//	TRACE( "IvySynchronousMessageCallback::OnMessage msg count %d\n",wParam);
	assert(PostMessage(m_synchro->m_hWnd, WM_MESSAGE_CB, msg_count++, (LPARAM)param ));
		
}
IvySynchronousApplicationCallback::IvySynchronousApplicationCallback( IvyApplicationCallback *cb )
{
	target = cb;
}
void IvySynchronousApplicationCallback::OnApplicationConnected( IvyApplication *app)
{
	// duplicate on the Message Queue
	IvySynchronousApplicationCallback *param = new IvySynchronousApplicationCallback(target);
	param->app = app;
	assert(PostMessage(m_synchro->m_hWnd, WM_APPCONNECTED_CB, 0, (LPARAM)param ));
}
void IvySynchronousApplicationCallback::OnApplicationDisconnected(  IvyApplication *app)
{
	// duplicate on the Message Queue
	IvySynchronousApplicationCallback *param = new IvySynchronousApplicationCallback(target);
	param->app = app;
	assert(PostMessage(m_synchro->m_hWnd, WM_APPDISCONNECTED_CB, 0, (LPARAM)param ));
}