File: QClamVstEditor.cxx

package info (click to toggle)
clam 1.4.0-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,836 kB
  • ctags: 20,981
  • sloc: cpp: 92,504; python: 9,721; ansic: 1,602; xml: 444; sh: 239; makefile: 153; perl: 54; asm: 15
file content (322 lines) | stat: -rw-r--r-- 8,866 bytes parent folder | download | duplicates (2)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <QtGui/QApplication>
#include <QtGui/QHBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QCloseEvent>
#include <QtGui/QResizeEvent>
#include <QtGui/QMoveEvent>
#include <QtUiTools/QUiLoader>
#include <QtCore/QBuffer>
#include <sstream>
#include "QClamVstEditor.hxx"
#include "VstNetworkExporter.hxx"

extern HINSTANCE hInstance;

std::string windowMessage2String(UINT message)
{
	#define WM2STR_TRANSLATE(message) case message: return #message;
	switch (message)
	{
		WM2STR_TRANSLATE(WM_NCCREATE);
		WM2STR_TRANSLATE(WM_NCCALCSIZE);
		WM2STR_TRANSLATE(WM_CREATE);
		WM2STR_TRANSLATE(WM_SIZE);
		WM2STR_TRANSLATE(WM_MOVE);
		WM2STR_TRANSLATE(WM_SHOWWINDOW);
		WM2STR_TRANSLATE(WM_WINDOWPOSCHANGING);
		WM2STR_TRANSLATE(WM_WINDOWPOSCHANGED);
		WM2STR_TRANSLATE(WM_STYLECHANGING);
		WM2STR_TRANSLATE(WM_STYLECHANGED);
		WM2STR_TRANSLATE(WM_MOUSEWHEEL);
		WM2STR_TRANSLATE(WM_CTLCOLOREDIT);
		WM2STR_TRANSLATE(WM_DESTROY);
		WM2STR_TRANSLATE(WM_NCDESTROY);
		WM2STR_TRANSLATE(WM_LBUTTONDOWN);
		WM2STR_TRANSLATE(WM_RBUTTONDOWN);
		WM2STR_TRANSLATE(WM_MBUTTONDOWN);
		WM2STR_TRANSLATE(WM_DRAWITEM);
		WM2STR_TRANSLATE(WM_MEASUREITEM);
		WM2STR_TRANSLATE(WM_PAINT);
		default:
			std::ostringstream os;
			os << message;
			return os.str();
	}
	#undef WM2STR_TRANSLATE
}

LONG_PTR WINAPI WindowProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	std::cout << "WindowProc: " << windowMessage2String(message) << std::endl;
	return DefWindowProc (hwnd, message, wParam, lParam);
}

const std::string & PluginEditorWindowClassName()
{
	static std::string windowClassName;
	if (windowClassName != "") return windowClassName;

	std::ostringstream os;
	os << "Plugin_" << hInstance << std::ends;
	windowClassName = os.str();
	WNDCLASS windowClass;
	windowClass.style = CS_GLOBALCLASS | CS_PARENTDC;//|CS_OWNDC; // add Private-DC constant
	windowClass.lpfnWndProc = WindowProc;
	windowClass.cbClsExtra  = 0;
	windowClass.cbWndExtra  = 0;
	windowClass.hInstance   = hInstance;
	windowClass.hIcon = 0;
	windowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
	windowClass.hbrBackground = GetSysColorBrush (COLOR_BTNFACE);
	windowClass.lpszMenuName  = 0;
	windowClass.lpszClassName = windowClassName.c_str();
	RegisterClass (&windowClass);
	return windowClassName;
}

QVstWindow::QVstWindow(WId handle, QClamVstEditor * editor)
	: QWidget(0)
	, _editor(editor)
//	, _oldWndProc(0)
//	, _oldWndData(0)
	, _parent(handle)
{
	std::cout << "Widget Construction" << std::endl;
//	_oldWndProc = GetWindowLongPtr(handle, GWLP_WNDPROC);
//	_oldWndData = GetWindowLongPtr(handle, GWLP_USERDATA);

	RECT rect;
	GetWindowRect((WId)_parent, &rect);

	WId child = CreateWindowEx (
		0, PluginEditorWindowClassName().c_str(), "Window",
		WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 
		0, 0, rect.right-rect.left, rect.bottom-rect.top, 
		_parent, 0, hInstance, NULL);

//	SetWindowLongPtr ((HWND)child, GWLP_USERDATA, (LONG_PTR)this);
	std::cout << "Widget Creating" << std::endl;
	create(child, true, true); 
	std::cout << "Widget Created" << std::endl;
}


void QVstWindow::closeEvent(QCloseEvent * event)
{
	std::cout << "Widget Closing" << std::endl;
	QWidget::closeEvent(event);
	std::cout << "QWidget Closed" << std::endl;
	if (not _editor->isOpen())
		std::cout << "Widget Was not open" << std::endl;
	else _editor->close();
	std::cout << "Widget closed" << std::endl;
}

std::string rectString(WId nativeWindow)
{
	std::ostringstream os;
	RECT rect;
	GetWindowRect(nativeWindow, &rect);
	os
		<< rect.left << ","
		<< rect.top << ":"
		<< rect.right-rect.left << ","
		<< rect.bottom-rect.top;
	return os.str();
}

std::ostream & operator << (std::ostream & os, ERect & rect)
{
	return os
		<< rect.left << ","
		<< rect.top << ":"
		<< rect.right-rect.left << ","
		<< rect.bottom-rect.top;
}

std::ostream & operator<< (std::ostream & os, const QSize & size)
{
	return os
		<< size.width() << ","
		<< size.height();
}

bool resizeFrame(WId parent, int width, int height)
{
	long frameSize = (2 * GetSystemMetrics (SM_CYFIXEDFRAME));

	long diffWidth  = 0;
	long diffHeight = 0;

	HWND window = (HWND)parent;

	while ((diffWidth != frameSize) && (window)) // look for FrameWindow
	{
		std::string mdiClientClass = "MDIClient";
		HWND hTempParentWnd = GetParent (window);
		char buffer[1024];
		GetClassName (hTempParentWnd, buffer, 1024);
		if (!hTempParentWnd) break;
		if (buffer != mdiClientClass) break;
		RECT currentRect;
		GetWindowRect (window, &currentRect);
		RECT parentRect;
		GetWindowRect (hTempParentWnd, &parentRect);

		SetWindowPos (window, HWND_TOP, 0, 0, width + diffWidth, height + diffHeight, SWP_NOMOVE);

		diffWidth  += (parentRect.right - parentRect.left) - (currentRect.right - currentRect.left);
		diffHeight += (parentRect.bottom - parentRect.top) - (currentRect.bottom - currentRect.top);

		if ((diffWidth > 80) || (diffHeight > 80)) // parent belongs to host
		{
			std::cout << "Parent belongs to host" << std::endl;
			return true;
		}

		if (diffWidth < 0)
			diffWidth = 0;
		if (diffHeight < 0)
			diffHeight = 0;

		window = hTempParentWnd;
	}

	if (window)
		SetWindowPos (window, HWND_TOP, 0, 0, width + diffWidth, height + diffHeight, SWP_NOMOVE);


}

void QVstWindow::resizeEvent(QResizeEvent * event)
{
	std::cout << "Widget Resizing "  << event->size() << std::endl;
	std::cout << " Parent size " << rectString(_parent) << std::endl; 
	QWidget::resizeEvent(event);
	ERect * rect;
	_editor->getRect(&rect);
	resizeFrame(winId(), event->size().width(), event->size().height());
	std::cout << " Rect before " << *rect << std::endl; 
	rect->right =rect->left+width();
	rect->bottom = rect->top +height();
	
	std::cout << " Rect after " << *rect << " adapted" << std::endl; 
	std::cout << "Widget Resized " << size() << std::endl;
}

void QVstWindow::moveEvent(QMoveEvent * event)
{
	std::cout << "Widget Moving " 
		<< event->pos().x() << "," 
		<< event->pos().y() << std::endl;
	QWidget::moveEvent(event);
}

QVstWindow::~QVstWindow()
{
	std::cout << "Widget Destroying" << std::endl;
	_editor->detachWidget();
	std::cout << "Widget Detaching" << std::endl;
	create(0, false, true); // Detach the window without destroying it
	// Restoring previous data
//	SetWindowLongPtr(winId(), GWLP_WNDPROC, _oldWndProc);
//	SetWindowLongPtr(winId(), GWLP_USERDATA, _oldWndData);
	std::cout << "Widget Destroyed" << std::endl;
}

void requireQApp()
{
	std::cout << "Assuring Application" << std::endl;
	if (qApp) return;
	std::cout << "Application init" << std::endl;
	int argc=0;
	char * argv[] = {0};
	new QApplication(argc, argv);
}

ERect defaultRect = {0,0,100,400};

QClamVstEditor::QClamVstEditor(CLAM::VstNetworkExporter * effect, const std::string & uifile)
	: AEffEditor(effect)
	, _uifile(uifile)
	, _widget(0)
	, _app(0)
	, _rect(defaultRect)
{
	std::cout << "Creating editor" << std::endl;
	requireQApp();
	effect->setEditor (this);
}

void QClamVstEditor::idle ()
{
//	std::cout << (systemWindow?".":"0") << std::flush;
//	requireQApp();
	if (qApp ) QApplication::processEvents();
	else std::cout << "T" << std::flush;
}

bool QClamVstEditor::open(void * ptr)
{
	std::cout << "Opening Editor " << ptr << std::endl;
	AEffEditor::open(ptr);
	requireQApp();
	_widget = new QVstWindow((WId)ptr, this);
	std::cout << "Widget Showing" << std::endl;
	std::cout << "Widget Shown" << std::endl;
	std::cout << "Setting the layout" << std::endl;
	_widget->show();
	QHBoxLayout * layout = new QHBoxLayout(_widget);
	std::cout << "Adding the widget" << std::endl;
	QUiLoader loader;
	QBuffer file;
	file.setData(_uifile.c_str(), _uifile.size());
	QWidget * ui = loader.load(&file, _widget);
	if (not ui) std::cerr << "Error loading interface" << std::endl;
	else layout->addWidget(ui);
//	_widget->setStyleSheet("background-color: blue; border: red solid 2pt;");
//	ui->setStyleSheet("background-color: white; padding: 3pt; border: yellow solid 2pt;");
	ui->adjustSize();
	return true;
}

void QClamVstEditor::close ()
{
	std::cout << "Closing Editor" << std::endl;
	AEffEditor::close();
	std::cout << "AEffEditor::close called" << std::endl;
	if (_widget) delete _widget;
	std::cout << "Closed Editor" << std::endl;
}

QClamVstEditor::~QClamVstEditor()
{
	std::cout << "Destroying Editor" << std::endl;
	if (_widget) delete _widget;
}


void QClamVstEditor::setParameter (VstInt32 index, float value)
{
	std::cout << "Edit: Setting Parameter " << index << " to " << value << std::endl;
	// TODO: Propagate the parameter to the widgets
}

bool QClamVstEditor::getRect (ERect **ppErect)
{
	if (not systemWindow)
	{
		_rect = defaultRect;
		return true;
	}
	WId sizeReference = _widget? _widget->winId() : (WId) systemWindow;
	RECT rect;
	GetWindowRect(sizeReference, &rect);
	_rect.right = rect.right;
	_rect.left = rect.left;
	_rect.top = rect.top;
	_rect.bottom = rect.bottom;
	*ppErect = &_rect;
	return true;
}