1
/* $Id: vidwin.cpp 4060 2012-04-17 09:55:30Z ming $ */
3
* Copyright (C) 2011 Teluu Inc. (http://www.teluu.com)
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
#define THIS_FILE "vidwin.cpp"
23
#define TRACE_(...) PJ_LOG(4,(THIS_FILE, __VA_ARGS__))
25
VidWin::VidWin(const pjmedia_vid_dev_hwnd *hwnd_,
28
QWidget(parent, f), orig_parent(NULL),
29
size_hint(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)
31
setAttribute(Qt::WA_NativeWindow);
33
/* Make this widget a bit "lighter" */
34
setAttribute(Qt::WA_UpdatesDisabled);
35
setAttribute(Qt::WA_PaintOnScreen);
36
setAttribute(Qt::WA_NoSystemBackground);
37
setAttribute(Qt::WA_PaintOutsidePaintEvent);
38
setUpdatesEnabled(false);
40
pj_bzero(&hwnd, sizeof(hwnd));
53
void VidWin::putIntoLayout(QBoxLayout *box)
55
box->addWidget(this, 1);
60
bool VidWin::event(QEvent *e)
68
case QEvent::ParentChange:
72
if (qr.width() > size_hint.width())
73
size_hint.setWidth(qr.width());
74
if (qr.height() > size_hint.height())
75
size_hint.setWidth(qr.height());
77
setFixedSize(size_hint);
83
// revert to default size hint, make it resizable
84
setFixedSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
95
return QWidget::event(e);
98
/* Platform specific code */
100
#if defined(_WIN32) && !defined(_WIN32_WINCE)
104
void VidWin::attach()
106
if (!hwnd.info.win.hwnd) return;
108
HWND w = (HWND)hwnd.info.win.hwnd;
109
HWND new_parent = (HWND)winId();
110
orig_parent = GetParent(w);
112
//SetWindowLong(w, GWL_STYLE, WS_CHILD);
113
SetParent(w, new_parent);
114
TRACE_("%p new parent handle = %p", w, new_parent);
117
void VidWin::detach()
119
if (!hwnd.info.win.hwnd) return;
121
HWND w = (HWND)hwnd.info.win.hwnd;
122
SetParent(w, (HWND)orig_parent);
123
TRACE_("%p revert parent handle to %p", w, orig_parent);
126
void VidWin::set_size()
128
if (!hwnd.info.win.hwnd) return;
130
HWND w = (HWND)hwnd.info.win.hwnd;
132
UINT swp_flag = SWP_NOACTIVATE;
133
SetWindowPos(w, HWND_TOP, 0, 0, qr.width(), qr.height(), swp_flag);
134
TRACE_("%p new size = %dx%d", w, qr.width(), qr.height());
137
void VidWin::get_size()
139
if (!hwnd.info.win.hwnd) return;
141
HWND w = (HWND)hwnd.info.win.hwnd;
143
if (GetWindowRect(w, &r))
144
size_hint = QSize(r.right-r.left+1, r.bottom-r.top+1);
145
TRACE_("%p size = %dx%d", w, size_hint.width(), size_hint.height());
148
void VidWin::show_sdl(bool visible)
150
if (!hwnd.info.win.hwnd) return;
152
HWND w = (HWND)hwnd.info.win.hwnd;
153
ShowWindow(w, visible ? SW_SHOW : SW_HIDE);
156
#elif defined(__APPLE__)
158
#import<Cocoa/Cocoa.h>
160
void VidWin::attach()
162
if (!hwnd.info.cocoa.window) return;
164
/* Embed hwnd to widget */
165
NSWindow *w = (NSWindow*)hwnd.info.cocoa.window;
166
NSWindow *parent = [(NSView*)winId() window];
167
orig_parent = [w parentWindow];
169
//[w setStyleMask:NSBorderlessWindowMask];
171
//Can't use this, as sometime the video window may not get reparented.
172
//[w setParentWindow:parent];
174
[parent addChildWindow:w ordered:NSWindowAbove];
175
TRACE_("%p new parent handle = %p", w, parent);
179
void VidWin::detach()
181
if (!hwnd.info.cocoa.window) return;
183
NSWindow *w = (NSWindow*)hwnd.info.cocoa.window;
184
NSWindow *parent = [(NSView*)winId() window];
185
[parent removeChildWindow:w];
189
void VidWin::set_size()
191
if (!hwnd.info.cocoa.window) return;
193
/* Update position and size */
194
NSWindow *w = (NSWindow*)hwnd.info.cocoa.window;
197
NSView* v = (NSView*)winId();
199
r = [v convertRectToBase:r];
200
r.origin = [[v window] convertBaseToScreen:r.origin];
203
[w setFrame:r display:NO];
205
TRACE_("%p new size = %dx%d", w, qr.width(), qr.height());
208
void VidWin::get_size()
210
if (!hwnd.info.cocoa.window) return;
212
NSWindow *w = (NSWindow*)hwnd.info.cocoa.window;
214
size_hint = QSize(300, 200);
216
TRACE_("%p size = %dx%d", 0, size_hint.width(), size_hint.height());
219
void VidWin::show_sdl(bool visible)
221
if (!hwnd.info.cocoa.window) return;
223
NSWindow *w = (NSWindow*)hwnd.info.cocoa.window;
226
[[w contentView]setHidden:NO];
228
[[w contentView]setHidden:YES];
232
#elif defined(linux) || defined(__linux)
234
#include <X11/Xlib.h>
235
#include <X11/Xutil.h>
239
#define GET_DISPLAY() QX11Info::display()
240
//#define GET_DISPLAY() (Display*)hwnd.info.x11.display
242
void VidWin::attach()
244
if (!hwnd.info.x11.window) return;
246
/* Embed hwnd to widget */
248
// Use Qt X11 display here, using window creator X11 display may cause
249
// the window failing to embed to this QWidget.
250
//Display *d = (Display*)hwnd.info.x11.display;
251
Display *d = GET_DISPLAY();
252
Window w = (Window)hwnd.info.x11.window;
253
Window parent = (Window)this->winId();
254
int err = XReparentWindow(d, w, parent, 0, 0);
255
TRACE_("%p new parent handle = %p, err = %d",
256
(void*)w,(void*)parent, err);
260
void VidWin::detach()
265
void VidWin::set_size()
267
if (!hwnd.info.x11.window) return;
269
/* Update position and size */
270
Display *d = GET_DISPLAY();
271
Window w = (Window)hwnd.info.x11.window;
274
int err = XResizeWindow(d, w, qr.width(), qr.height());
275
TRACE_("[%p,%p] new size = %dx%d, err = %d",
276
(void*)d, (void*)w, qr.width(), qr.height(), err);
279
void VidWin::get_size()
281
if (!hwnd.info.x11.window) return;
283
Display *d = GET_DISPLAY();
284
Window w = (Window)hwnd.info.x11.window;
286
XWindowAttributes attr;
287
XGetWindowAttributes(d, w, &attr);
288
size_hint = QSize(attr.width, attr.height);
289
TRACE_("%p size = %dx%d", w, size_hint.width(), size_hint.height());
292
void VidWin::show_sdl(bool visible)
294
if (!hwnd.info.x11.window) return;
296
Display *d = GET_DISPLAY();
297
Window w = (Window)hwnd.info.x11.window;