The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3999 lines
133KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #ifdef _MSC_VER
  24. #pragma warning (disable: 4514)
  25. #pragma warning (push)
  26. #endif
  27. #include "win32_headers.h"
  28. #include <float.h>
  29. #include <windowsx.h>
  30. #include <shlobj.h>
  31. #if JUCE_OPENGL
  32. #include <gl/gl.h>
  33. #endif
  34. #ifdef _MSC_VER
  35. #pragma warning (pop)
  36. #pragma warning (disable: 4312 4244)
  37. #endif
  38. #undef GetSystemMetrics // multimon overrides this for some reason and causes a mess..
  39. // these are in the windows SDK, but need to be repeated here for GCC..
  40. #ifndef GET_APPCOMMAND_LPARAM
  41. #define FAPPCOMMAND_MASK 0xF000
  42. #define GET_APPCOMMAND_LPARAM(lParam) ((short) (HIWORD (lParam) & ~FAPPCOMMAND_MASK))
  43. #define APPCOMMAND_MEDIA_NEXTTRACK 11
  44. #define APPCOMMAND_MEDIA_PREVIOUSTRACK 12
  45. #define APPCOMMAND_MEDIA_STOP 13
  46. #define APPCOMMAND_MEDIA_PLAY_PAUSE 14
  47. #define WM_APPCOMMAND 0x0319
  48. #endif
  49. #include "../../../src/juce_core/basics/juce_StandardHeader.h"
  50. BEGIN_JUCE_NAMESPACE
  51. #include "../../../src/juce_core/text/juce_StringArray.h"
  52. #include "../../../src/juce_core/basics/juce_SystemStats.h"
  53. #include "../../../src/juce_core/basics/juce_Singleton.h"
  54. #include "../../../src/juce_core/threads/juce_Process.h"
  55. #include "../../../src/juce_core/misc/juce_PlatformUtilities.h"
  56. #include "../../../src/juce_appframework/events/juce_Timer.h"
  57. #include "../../../src/juce_appframework/events/juce_MessageManager.h"
  58. #include "../../../src/juce_appframework/application/juce_DeletedAtShutdown.h"
  59. #include "../../../src/juce_appframework/gui/components/keyboard/juce_KeyPress.h"
  60. #include "../../../src/juce_appframework/gui/components/mouse/juce_DragAndDropContainer.h"
  61. #include "../../../src/juce_appframework/gui/components/juce_Desktop.h"
  62. #include "../../../src/juce_appframework/gui/components/lookandfeel/juce_LookAndFeel.h"
  63. #include "../../../src/juce_appframework/gui/components/special/juce_OpenGLComponent.h"
  64. #include "../../../src/juce_appframework/gui/components/special/juce_DropShadower.h"
  65. #include "../../../src/juce_appframework/gui/components/special/juce_ActiveXControlComponent.h"
  66. #include "../../../src/juce_appframework/gui/components/special/juce_SystemTrayIconComponent.h"
  67. #include "../../../src/juce_appframework/gui/components/juce_ComponentDeletionWatcher.h"
  68. #include "../../../src/juce_appframework/gui/components/layout/juce_ComponentBoundsConstrainer.h"
  69. #include "../../../src/juce_appframework/gui/graphics/imaging/juce_ImageFileFormat.h"
  70. #include "../../../src/juce_appframework/gui/graphics/contexts/juce_LowLevelGraphicsSoftwareRenderer.h"
  71. #include "../../../src/juce_appframework/gui/graphics/geometry/juce_PathIterator.h"
  72. #include "../../../src/juce_appframework/gui/components/layout/juce_ComponentMovementWatcher.h"
  73. extern void juce_repeatLastProcessPriority() throw(); // in juce_win32_Threads.cpp
  74. extern void juce_CheckCurrentlyFocusedTopLevelWindow() throw(); // in juce_TopLevelWindow.cpp
  75. extern bool juce_IsRunningInWine() throw();
  76. const int juce_windowIsSemiTransparentFlag = (1 << 31); // also in component.cpp
  77. #ifndef ULW_ALPHA
  78. #define ULW_ALPHA 0x00000002
  79. #endif
  80. #ifndef AC_SRC_ALPHA
  81. #define AC_SRC_ALPHA 0x01
  82. #endif
  83. #define DEBUG_REPAINT_TIMES 0
  84. static HPALETTE palette = 0;
  85. static bool createPaletteIfNeeded = true;
  86. static bool shouldDeactivateTitleBar = true;
  87. static bool screenSaverAllowed = true;
  88. static HICON createHICONFromImage (const Image& image, const BOOL isIcon, int hotspotX, int hotspotY) throw();
  89. #define WM_TRAYNOTIFY WM_USER + 100
  90. //==============================================================================
  91. typedef BOOL (WINAPI* UpdateLayeredWinFunc) (HWND, HDC, POINT*, SIZE*, HDC, POINT*, COLORREF, BLENDFUNCTION*, DWORD);
  92. static UpdateLayeredWinFunc updateLayeredWindow = 0;
  93. bool Desktop::canUseSemiTransparentWindows() throw()
  94. {
  95. if (updateLayeredWindow == 0)
  96. {
  97. if (! juce_IsRunningInWine())
  98. {
  99. HMODULE user32Mod = GetModuleHandle (_T("user32.dll"));
  100. updateLayeredWindow = (UpdateLayeredWinFunc) GetProcAddress (user32Mod, "UpdateLayeredWindow");
  101. }
  102. }
  103. return updateLayeredWindow != 0;
  104. }
  105. //==============================================================================
  106. #undef DefWindowProc
  107. #define DefWindowProc DefWindowProcW
  108. //==============================================================================
  109. const int extendedKeyModifier = 0x10000;
  110. const int KeyPress::spaceKey = VK_SPACE;
  111. const int KeyPress::returnKey = VK_RETURN;
  112. const int KeyPress::escapeKey = VK_ESCAPE;
  113. const int KeyPress::backspaceKey = VK_BACK;
  114. const int KeyPress::deleteKey = VK_DELETE | extendedKeyModifier;
  115. const int KeyPress::insertKey = VK_INSERT | extendedKeyModifier;
  116. const int KeyPress::tabKey = VK_TAB;
  117. const int KeyPress::leftKey = VK_LEFT | extendedKeyModifier;
  118. const int KeyPress::rightKey = VK_RIGHT | extendedKeyModifier;
  119. const int KeyPress::upKey = VK_UP | extendedKeyModifier;
  120. const int KeyPress::downKey = VK_DOWN | extendedKeyModifier;
  121. const int KeyPress::homeKey = VK_HOME | extendedKeyModifier;
  122. const int KeyPress::endKey = VK_END | extendedKeyModifier;
  123. const int KeyPress::pageUpKey = VK_PRIOR | extendedKeyModifier;
  124. const int KeyPress::pageDownKey = VK_NEXT | extendedKeyModifier;
  125. const int KeyPress::F1Key = VK_F1 | extendedKeyModifier;
  126. const int KeyPress::F2Key = VK_F2 | extendedKeyModifier;
  127. const int KeyPress::F3Key = VK_F3 | extendedKeyModifier;
  128. const int KeyPress::F4Key = VK_F4 | extendedKeyModifier;
  129. const int KeyPress::F5Key = VK_F5 | extendedKeyModifier;
  130. const int KeyPress::F6Key = VK_F6 | extendedKeyModifier;
  131. const int KeyPress::F7Key = VK_F7 | extendedKeyModifier;
  132. const int KeyPress::F8Key = VK_F8 | extendedKeyModifier;
  133. const int KeyPress::F9Key = VK_F9 | extendedKeyModifier;
  134. const int KeyPress::F10Key = VK_F10 | extendedKeyModifier;
  135. const int KeyPress::F11Key = VK_F11 | extendedKeyModifier;
  136. const int KeyPress::F12Key = VK_F12 | extendedKeyModifier;
  137. const int KeyPress::F13Key = VK_F13 | extendedKeyModifier;
  138. const int KeyPress::F14Key = VK_F14 | extendedKeyModifier;
  139. const int KeyPress::F15Key = VK_F15 | extendedKeyModifier;
  140. const int KeyPress::F16Key = VK_F16 | extendedKeyModifier;
  141. const int KeyPress::numberPad0 = VK_NUMPAD0 | extendedKeyModifier;
  142. const int KeyPress::numberPad1 = VK_NUMPAD1 | extendedKeyModifier;
  143. const int KeyPress::numberPad2 = VK_NUMPAD2 | extendedKeyModifier;
  144. const int KeyPress::numberPad3 = VK_NUMPAD3 | extendedKeyModifier;
  145. const int KeyPress::numberPad4 = VK_NUMPAD4 | extendedKeyModifier;
  146. const int KeyPress::numberPad5 = VK_NUMPAD5 | extendedKeyModifier;
  147. const int KeyPress::numberPad6 = VK_NUMPAD6 | extendedKeyModifier;
  148. const int KeyPress::numberPad7 = VK_NUMPAD7 | extendedKeyModifier;
  149. const int KeyPress::numberPad8 = VK_NUMPAD8 | extendedKeyModifier;
  150. const int KeyPress::numberPad9 = VK_NUMPAD9 | extendedKeyModifier;
  151. const int KeyPress::numberPadAdd = VK_ADD | extendedKeyModifier;
  152. const int KeyPress::numberPadSubtract = VK_SUBTRACT | extendedKeyModifier;
  153. const int KeyPress::numberPadMultiply = VK_MULTIPLY | extendedKeyModifier;
  154. const int KeyPress::numberPadDivide = VK_DIVIDE | extendedKeyModifier;
  155. const int KeyPress::numberPadSeparator = VK_SEPARATOR | extendedKeyModifier;
  156. const int KeyPress::numberPadDecimalPoint = VK_DECIMAL | extendedKeyModifier;
  157. const int KeyPress::numberPadEquals = 0x92 /*VK_OEM_NEC_EQUAL*/ | extendedKeyModifier;
  158. const int KeyPress::numberPadDelete = VK_DELETE | extendedKeyModifier;
  159. const int KeyPress::playKey = 0x30000;
  160. const int KeyPress::stopKey = 0x30001;
  161. const int KeyPress::fastForwardKey = 0x30002;
  162. const int KeyPress::rewindKey = 0x30003;
  163. //==============================================================================
  164. class WindowsBitmapImage : public Image
  165. {
  166. public:
  167. //==============================================================================
  168. HBITMAP hBitmap;
  169. BITMAPV4HEADER bitmapInfo;
  170. HDC hdc;
  171. unsigned char* bitmapData;
  172. //==============================================================================
  173. WindowsBitmapImage (const PixelFormat format_,
  174. const int w, const int h, const bool clearImage)
  175. : Image (format_, w, h)
  176. {
  177. jassert (format_ == RGB || format_ == ARGB);
  178. pixelStride = (format_ == RGB) ? 3 : 4;
  179. zerostruct (bitmapInfo);
  180. bitmapInfo.bV4Size = sizeof (BITMAPV4HEADER);
  181. bitmapInfo.bV4Width = w;
  182. bitmapInfo.bV4Height = h;
  183. bitmapInfo.bV4Planes = 1;
  184. bitmapInfo.bV4BitCount = (unsigned short) (pixelStride * 8);
  185. if (format_ == ARGB)
  186. {
  187. bitmapInfo.bV4AlphaMask = 0xff000000;
  188. bitmapInfo.bV4RedMask = 0xff0000;
  189. bitmapInfo.bV4GreenMask = 0xff00;
  190. bitmapInfo.bV4BlueMask = 0xff;
  191. bitmapInfo.bV4V4Compression = BI_BITFIELDS;
  192. }
  193. else
  194. {
  195. bitmapInfo.bV4V4Compression = BI_RGB;
  196. }
  197. lineStride = -((w * pixelStride + 3) & ~3);
  198. HDC dc = GetDC (0);
  199. hdc = CreateCompatibleDC (dc);
  200. ReleaseDC (0, dc);
  201. SetMapMode (hdc, MM_TEXT);
  202. hBitmap = CreateDIBSection (hdc,
  203. (BITMAPINFO*) &(bitmapInfo),
  204. DIB_RGB_COLORS,
  205. (void**) &bitmapData,
  206. 0, 0);
  207. SelectObject (hdc, hBitmap);
  208. if (format_ == ARGB && clearImage)
  209. zeromem (bitmapData, abs (h * lineStride));
  210. imageData = bitmapData - (lineStride * (h - 1));
  211. }
  212. ~WindowsBitmapImage()
  213. {
  214. DeleteDC (hdc);
  215. DeleteObject (hBitmap);
  216. imageData = 0; // to stop the base class freeing this
  217. }
  218. void blitToWindow (HWND hwnd, HDC dc, const bool transparent,
  219. const int x, const int y,
  220. const RectangleList& maskedRegion) throw()
  221. {
  222. static HDRAWDIB hdd = 0;
  223. static bool needToCreateDrawDib = true;
  224. if (needToCreateDrawDib)
  225. {
  226. needToCreateDrawDib = false;
  227. HDC dc = GetDC (0);
  228. const int n = GetDeviceCaps (dc, BITSPIXEL);
  229. ReleaseDC (0, dc);
  230. // only open if we're not palettised
  231. if (n > 8)
  232. hdd = DrawDibOpen();
  233. }
  234. if (createPaletteIfNeeded)
  235. {
  236. HDC dc = GetDC (0);
  237. const int n = GetDeviceCaps (dc, BITSPIXEL);
  238. ReleaseDC (0, dc);
  239. if (n <= 8)
  240. palette = CreateHalftonePalette (dc);
  241. createPaletteIfNeeded = false;
  242. }
  243. if (palette != 0)
  244. {
  245. SelectPalette (dc, palette, FALSE);
  246. RealizePalette (dc);
  247. SetStretchBltMode (dc, HALFTONE);
  248. }
  249. SetMapMode (dc, MM_TEXT);
  250. if (transparent)
  251. {
  252. POINT p, pos;
  253. SIZE size;
  254. RECT windowBounds;
  255. GetWindowRect (hwnd, &windowBounds);
  256. p.x = -x;
  257. p.y = -y;
  258. pos.x = windowBounds.left;
  259. pos.y = windowBounds.top;
  260. size.cx = windowBounds.right - windowBounds.left;
  261. size.cy = windowBounds.bottom - windowBounds.top;
  262. BLENDFUNCTION bf;
  263. bf.AlphaFormat = AC_SRC_ALPHA;
  264. bf.BlendFlags = 0;
  265. bf.BlendOp = AC_SRC_OVER;
  266. bf.SourceConstantAlpha = 0xff;
  267. if (! maskedRegion.isEmpty())
  268. {
  269. for (RectangleList::Iterator i (maskedRegion); i.next();)
  270. {
  271. const Rectangle& r = *i.getRectangle();
  272. ExcludeClipRect (hdc, r.getX(), r.getY(), r.getRight(), r.getBottom());
  273. }
  274. }
  275. updateLayeredWindow (hwnd, 0, &pos, &size, hdc, &p, 0, &bf, ULW_ALPHA);
  276. }
  277. else
  278. {
  279. int savedDC = 0;
  280. if (! maskedRegion.isEmpty())
  281. {
  282. savedDC = SaveDC (dc);
  283. for (RectangleList::Iterator i (maskedRegion); i.next();)
  284. {
  285. const Rectangle& r = *i.getRectangle();
  286. ExcludeClipRect (dc, r.getX(), r.getY(), r.getRight(), r.getBottom());
  287. }
  288. }
  289. const int w = getWidth();
  290. const int h = getHeight();
  291. if (hdd == 0)
  292. {
  293. StretchDIBits (dc,
  294. x, y, w, h,
  295. 0, 0, w, h,
  296. bitmapData, (const BITMAPINFO*) &bitmapInfo,
  297. DIB_RGB_COLORS, SRCCOPY);
  298. }
  299. else
  300. {
  301. DrawDibDraw (hdd, dc, x, y, -1, -1,
  302. (BITMAPINFOHEADER*) &bitmapInfo, bitmapData,
  303. 0, 0, w, h, 0);
  304. }
  305. if (! maskedRegion.isEmpty())
  306. RestoreDC (dc, savedDC);
  307. }
  308. }
  309. juce_UseDebuggingNewOperator
  310. private:
  311. WindowsBitmapImage (const WindowsBitmapImage&);
  312. const WindowsBitmapImage& operator= (const WindowsBitmapImage&);
  313. };
  314. //==============================================================================
  315. long improbableWindowNumber = 0xf965aa01; // also referenced by messaging.cpp
  316. //==============================================================================
  317. static int currentModifiers = 0;
  318. static int modifiersAtLastCallback = 0;
  319. static void updateKeyModifiers() throw()
  320. {
  321. currentModifiers &= ~(ModifierKeys::shiftModifier
  322. | ModifierKeys::ctrlModifier
  323. | ModifierKeys::altModifier);
  324. if ((GetKeyState (VK_SHIFT) & 0x8000) != 0)
  325. currentModifiers |= ModifierKeys::shiftModifier;
  326. if ((GetKeyState (VK_CONTROL) & 0x8000) != 0)
  327. currentModifiers |= ModifierKeys::ctrlModifier;
  328. if ((GetKeyState (VK_MENU) & 0x8000) != 0)
  329. currentModifiers |= ModifierKeys::altModifier;
  330. if ((GetKeyState (VK_RMENU) & 0x8000) != 0)
  331. currentModifiers &= ~(ModifierKeys::ctrlModifier | ModifierKeys::altModifier);
  332. }
  333. void ModifierKeys::updateCurrentModifiers() throw()
  334. {
  335. currentModifierFlags = currentModifiers;
  336. }
  337. bool KeyPress::isKeyCurrentlyDown (const int keyCode) throw()
  338. {
  339. SHORT k = (SHORT) keyCode;
  340. if ((keyCode & extendedKeyModifier) == 0
  341. && (k >= (SHORT) T('a') && k <= (SHORT) T('z')))
  342. k += (SHORT) T('A') - (SHORT) T('a');
  343. const SHORT translatedValues[] = { (SHORT) ',', VK_OEM_COMMA,
  344. (SHORT) '+', VK_OEM_PLUS,
  345. (SHORT) '-', VK_OEM_MINUS,
  346. (SHORT) '.', VK_OEM_PERIOD,
  347. (SHORT) ';', VK_OEM_1,
  348. (SHORT) ':', VK_OEM_1,
  349. (SHORT) '/', VK_OEM_2,
  350. (SHORT) '?', VK_OEM_2,
  351. (SHORT) '[', VK_OEM_4,
  352. (SHORT) ']', VK_OEM_6 };
  353. for (int i = 0; i < numElementsInArray (translatedValues); i += 2)
  354. if (k == translatedValues [i])
  355. k = translatedValues [i + 1];
  356. return (GetKeyState (k) & 0x8000) != 0;
  357. }
  358. const ModifierKeys ModifierKeys::getCurrentModifiersRealtime() throw()
  359. {
  360. updateKeyModifiers();
  361. currentModifiers &= ~ModifierKeys::allMouseButtonModifiers;
  362. if ((GetKeyState (VK_LBUTTON) & 0x8000) != 0)
  363. currentModifiers |= ModifierKeys::leftButtonModifier;
  364. if ((GetKeyState (VK_RBUTTON) & 0x8000) != 0)
  365. currentModifiers |= ModifierKeys::rightButtonModifier;
  366. if ((GetKeyState (VK_MBUTTON) & 0x8000) != 0)
  367. currentModifiers |= ModifierKeys::middleButtonModifier;
  368. return ModifierKeys (currentModifiers);
  369. }
  370. static int64 getMouseEventTime() throw()
  371. {
  372. static int64 eventTimeOffset = 0;
  373. static DWORD lastMessageTime = 0;
  374. const DWORD thisMessageTime = GetMessageTime();
  375. if (thisMessageTime < lastMessageTime || lastMessageTime == 0)
  376. {
  377. lastMessageTime = thisMessageTime;
  378. eventTimeOffset = Time::currentTimeMillis() - thisMessageTime;
  379. }
  380. return eventTimeOffset + thisMessageTime;
  381. }
  382. //==============================================================================
  383. class Win32ComponentPeer : public ComponentPeer
  384. {
  385. public:
  386. //==============================================================================
  387. Win32ComponentPeer (Component* const component,
  388. const int windowStyleFlags)
  389. : ComponentPeer (component, windowStyleFlags),
  390. dontRepaint (false),
  391. fullScreen (false),
  392. isDragging (false),
  393. isMouseOver (false),
  394. currentWindowIcon (0),
  395. taskBarIcon (0),
  396. dropTarget (0)
  397. {
  398. MessageManager::getInstance()
  399. ->callFunctionOnMessageThread (&createWindowCallback, (void*) this);
  400. setTitle (component->getName());
  401. if ((windowStyleFlags & windowHasDropShadow) != 0
  402. && Desktop::canUseSemiTransparentWindows())
  403. {
  404. shadower = component->getLookAndFeel().createDropShadowerForComponent (component);
  405. if (shadower != 0)
  406. shadower->setOwner (component);
  407. }
  408. else
  409. {
  410. shadower = 0;
  411. }
  412. }
  413. ~Win32ComponentPeer()
  414. {
  415. setTaskBarIcon (0);
  416. deleteAndZero (shadower);
  417. // do this before the next bit to avoid messages arriving for this window
  418. // before it's destroyed
  419. SetWindowLongPtr (hwnd, GWLP_USERDATA, 0);
  420. MessageManager::getInstance()
  421. ->callFunctionOnMessageThread (&destroyWindowCallback, (void*) hwnd);
  422. if (currentWindowIcon != 0)
  423. DestroyIcon (currentWindowIcon);
  424. if (dropTarget != 0)
  425. {
  426. dropTarget->Release();
  427. dropTarget = 0;
  428. }
  429. }
  430. //==============================================================================
  431. void* getNativeHandle() const
  432. {
  433. return (void*) hwnd;
  434. }
  435. void setVisible (bool shouldBeVisible)
  436. {
  437. ShowWindow (hwnd, shouldBeVisible ? SW_SHOWNA : SW_HIDE);
  438. if (shouldBeVisible)
  439. InvalidateRect (hwnd, 0, 0);
  440. else
  441. lastPaintTime = 0;
  442. }
  443. void setTitle (const String& title)
  444. {
  445. SetWindowText (hwnd, title);
  446. }
  447. void setPosition (int x, int y)
  448. {
  449. offsetWithinParent (x, y);
  450. SetWindowPos (hwnd, 0,
  451. x - windowBorder.getLeft(),
  452. y - windowBorder.getTop(),
  453. 0, 0,
  454. SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  455. }
  456. void repaintNowIfTransparent()
  457. {
  458. if (isTransparent() && lastPaintTime > 0 && Time::getMillisecondCounter() > lastPaintTime + 30)
  459. handlePaintMessage();
  460. }
  461. void updateBorderSize()
  462. {
  463. WINDOWINFO info;
  464. info.cbSize = sizeof (info);
  465. if (GetWindowInfo (hwnd, &info))
  466. {
  467. windowBorder = BorderSize (info.rcClient.top - info.rcWindow.top,
  468. info.rcClient.left - info.rcWindow.left,
  469. info.rcWindow.bottom - info.rcClient.bottom,
  470. info.rcWindow.right - info.rcClient.right);
  471. }
  472. }
  473. void setSize (int w, int h)
  474. {
  475. SetWindowPos (hwnd, 0, 0, 0,
  476. w + windowBorder.getLeftAndRight(),
  477. h + windowBorder.getTopAndBottom(),
  478. SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  479. updateBorderSize();
  480. repaintNowIfTransparent();
  481. }
  482. void setBounds (int x, int y, int w, int h, const bool isNowFullScreen)
  483. {
  484. fullScreen = isNowFullScreen;
  485. offsetWithinParent (x, y);
  486. SetWindowPos (hwnd, 0,
  487. x - windowBorder.getLeft(),
  488. y - windowBorder.getTop(),
  489. w + windowBorder.getLeftAndRight(),
  490. h + windowBorder.getTopAndBottom(),
  491. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  492. updateBorderSize();
  493. repaintNowIfTransparent();
  494. }
  495. void getBounds (int& x, int& y, int& w, int& h) const
  496. {
  497. RECT r;
  498. GetWindowRect (hwnd, &r);
  499. x = r.left;
  500. y = r.top;
  501. w = r.right - x;
  502. h = r.bottom - y;
  503. HWND parentH = GetParent (hwnd);
  504. if (parentH != 0)
  505. {
  506. GetWindowRect (parentH, &r);
  507. x -= r.left;
  508. y -= r.top;
  509. }
  510. x += windowBorder.getLeft();
  511. y += windowBorder.getTop();
  512. w -= windowBorder.getLeftAndRight();
  513. h -= windowBorder.getTopAndBottom();
  514. }
  515. int getScreenX() const
  516. {
  517. RECT r;
  518. GetWindowRect (hwnd, &r);
  519. return r.left + windowBorder.getLeft();
  520. }
  521. int getScreenY() const
  522. {
  523. RECT r;
  524. GetWindowRect (hwnd, &r);
  525. return r.top + windowBorder.getTop();
  526. }
  527. void relativePositionToGlobal (int& x, int& y)
  528. {
  529. RECT r;
  530. GetWindowRect (hwnd, &r);
  531. x += r.left + windowBorder.getLeft();
  532. y += r.top + windowBorder.getTop();
  533. }
  534. void globalPositionToRelative (int& x, int& y)
  535. {
  536. RECT r;
  537. GetWindowRect (hwnd, &r);
  538. x -= r.left + windowBorder.getLeft();
  539. y -= r.top + windowBorder.getTop();
  540. }
  541. void setMinimised (bool shouldBeMinimised)
  542. {
  543. if (shouldBeMinimised != isMinimised())
  544. ShowWindow (hwnd, shouldBeMinimised ? SW_MINIMIZE : SW_SHOWNORMAL);
  545. }
  546. bool isMinimised() const
  547. {
  548. WINDOWPLACEMENT wp;
  549. wp.length = sizeof (WINDOWPLACEMENT);
  550. GetWindowPlacement (hwnd, &wp);
  551. return wp.showCmd == SW_SHOWMINIMIZED;
  552. }
  553. void setFullScreen (bool shouldBeFullScreen)
  554. {
  555. setMinimised (false);
  556. if (fullScreen != shouldBeFullScreen)
  557. {
  558. fullScreen = shouldBeFullScreen;
  559. const ComponentDeletionWatcher deletionChecker (component);
  560. if (! fullScreen)
  561. {
  562. const Rectangle boundsCopy (lastNonFullscreenBounds);
  563. if (hasTitleBar())
  564. ShowWindow (hwnd, SW_SHOWNORMAL);
  565. if (! boundsCopy.isEmpty())
  566. {
  567. setBounds (boundsCopy.getX(),
  568. boundsCopy.getY(),
  569. boundsCopy.getWidth(),
  570. boundsCopy.getHeight(),
  571. false);
  572. }
  573. }
  574. else
  575. {
  576. if (hasTitleBar())
  577. ShowWindow (hwnd, SW_SHOWMAXIMIZED);
  578. else
  579. SendMessageW (hwnd, WM_SETTINGCHANGE, 0, 0);
  580. }
  581. if (! deletionChecker.hasBeenDeleted())
  582. handleMovedOrResized();
  583. }
  584. }
  585. bool isFullScreen() const
  586. {
  587. if (! hasTitleBar())
  588. return fullScreen;
  589. WINDOWPLACEMENT wp;
  590. wp.length = sizeof (wp);
  591. GetWindowPlacement (hwnd, &wp);
  592. return wp.showCmd == SW_SHOWMAXIMIZED;
  593. }
  594. bool contains (int x, int y, bool trueIfInAChildWindow) const
  595. {
  596. RECT r;
  597. GetWindowRect (hwnd, &r);
  598. POINT p;
  599. p.x = x + r.left + windowBorder.getLeft();
  600. p.y = y + r.top + windowBorder.getTop();
  601. HWND w = WindowFromPoint (p);
  602. return w == hwnd || (trueIfInAChildWindow && (IsChild (hwnd, w) != 0));
  603. }
  604. const BorderSize getFrameSize() const
  605. {
  606. return windowBorder;
  607. }
  608. bool setAlwaysOnTop (bool alwaysOnTop)
  609. {
  610. const bool oldDeactivate = shouldDeactivateTitleBar;
  611. shouldDeactivateTitleBar = ((styleFlags & windowIsTemporary) == 0);
  612. SetWindowPos (hwnd, alwaysOnTop ? HWND_TOPMOST : HWND_NOTOPMOST,
  613. 0, 0, 0, 0,
  614. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING);
  615. shouldDeactivateTitleBar = oldDeactivate;
  616. if (shadower != 0)
  617. shadower->componentBroughtToFront (*component);
  618. return true;
  619. }
  620. void toFront (bool makeActive)
  621. {
  622. setMinimised (false);
  623. const bool oldDeactivate = shouldDeactivateTitleBar;
  624. shouldDeactivateTitleBar = ((styleFlags & windowIsTemporary) == 0);
  625. MessageManager::getInstance()
  626. ->callFunctionOnMessageThread (makeActive ? &toFrontCallback1
  627. : &toFrontCallback2,
  628. (void*) hwnd);
  629. shouldDeactivateTitleBar = oldDeactivate;
  630. if (! makeActive)
  631. {
  632. // in this case a broughttofront call won't have occured, so do it now..
  633. handleBroughtToFront();
  634. }
  635. }
  636. void toBehind (ComponentPeer* other)
  637. {
  638. Win32ComponentPeer* const otherPeer = dynamic_cast <Win32ComponentPeer*> (other);
  639. jassert (otherPeer != 0); // wrong type of window?
  640. if (otherPeer != 0)
  641. {
  642. setMinimised (false);
  643. SetWindowPos (hwnd, otherPeer->hwnd, 0, 0, 0, 0,
  644. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING);
  645. }
  646. }
  647. bool isFocused() const
  648. {
  649. return MessageManager::getInstance()
  650. ->callFunctionOnMessageThread (&getFocusCallback, 0) == (void*) hwnd;
  651. }
  652. void grabFocus()
  653. {
  654. const bool oldDeactivate = shouldDeactivateTitleBar;
  655. shouldDeactivateTitleBar = ((styleFlags & windowIsTemporary) == 0);
  656. MessageManager::getInstance()
  657. ->callFunctionOnMessageThread (&setFocusCallback, (void*) hwnd);
  658. shouldDeactivateTitleBar = oldDeactivate;
  659. }
  660. void repaint (int x, int y, int w, int h)
  661. {
  662. const RECT r = { x, y, x + w, y + h };
  663. InvalidateRect (hwnd, &r, FALSE);
  664. }
  665. void performAnyPendingRepaintsNow()
  666. {
  667. MSG m;
  668. if (component->isVisible() && PeekMessage (&m, hwnd, WM_PAINT, WM_PAINT, PM_REMOVE))
  669. DispatchMessage (&m);
  670. }
  671. //==============================================================================
  672. static Win32ComponentPeer* getOwnerOfWindow (HWND h) throw()
  673. {
  674. if (h != 0 && GetWindowLongPtr (h, GWLP_USERDATA) == improbableWindowNumber)
  675. return (Win32ComponentPeer*) GetWindowLongPtr (h, 8);
  676. return 0;
  677. }
  678. //==============================================================================
  679. void setTaskBarIcon (const Image* const image)
  680. {
  681. if (image != 0)
  682. {
  683. HICON hicon = createHICONFromImage (*image, TRUE, 0, 0);
  684. if (taskBarIcon == 0)
  685. {
  686. taskBarIcon = new NOTIFYICONDATA();
  687. taskBarIcon->cbSize = sizeof (NOTIFYICONDATA);
  688. taskBarIcon->hWnd = (HWND) hwnd;
  689. taskBarIcon->uID = (int) (pointer_sized_int) hwnd;
  690. taskBarIcon->uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  691. taskBarIcon->uCallbackMessage = WM_TRAYNOTIFY;
  692. taskBarIcon->hIcon = hicon;
  693. taskBarIcon->szTip[0] = 0;
  694. Shell_NotifyIcon (NIM_ADD, taskBarIcon);
  695. }
  696. else
  697. {
  698. HICON oldIcon = taskBarIcon->hIcon;
  699. taskBarIcon->hIcon = hicon;
  700. taskBarIcon->uFlags = NIF_ICON;
  701. Shell_NotifyIcon (NIM_MODIFY, taskBarIcon);
  702. DestroyIcon (oldIcon);
  703. }
  704. DestroyIcon (hicon);
  705. }
  706. else if (taskBarIcon != 0)
  707. {
  708. taskBarIcon->uFlags = 0;
  709. Shell_NotifyIcon (NIM_DELETE, taskBarIcon);
  710. DestroyIcon (taskBarIcon->hIcon);
  711. deleteAndZero (taskBarIcon);
  712. }
  713. }
  714. void setTaskBarIconToolTip (const String& toolTip) const
  715. {
  716. if (taskBarIcon != 0)
  717. {
  718. taskBarIcon->uFlags = NIF_TIP;
  719. toolTip.copyToBuffer (taskBarIcon->szTip, sizeof (taskBarIcon->szTip) - 1);
  720. Shell_NotifyIcon (NIM_MODIFY, taskBarIcon);
  721. }
  722. }
  723. //==============================================================================
  724. juce_UseDebuggingNewOperator
  725. bool dontRepaint;
  726. private:
  727. HWND hwnd;
  728. DropShadower* shadower;
  729. bool fullScreen, isDragging, isMouseOver;
  730. BorderSize windowBorder;
  731. HICON currentWindowIcon;
  732. NOTIFYICONDATA* taskBarIcon;
  733. IDropTarget* dropTarget;
  734. //==============================================================================
  735. class TemporaryImage : public Timer
  736. {
  737. public:
  738. //==============================================================================
  739. TemporaryImage()
  740. : image (0)
  741. {
  742. }
  743. ~TemporaryImage()
  744. {
  745. delete image;
  746. }
  747. //==============================================================================
  748. WindowsBitmapImage* getImage (const bool transparent, const int w, const int h) throw()
  749. {
  750. const Image::PixelFormat format = transparent ? Image::ARGB : Image::RGB;
  751. if (image == 0 || image->getWidth() < w || image->getHeight() < h || image->getFormat() != format)
  752. {
  753. delete image;
  754. image = new WindowsBitmapImage (format, (w + 31) & ~31, (h + 31) & ~31, false);
  755. }
  756. startTimer (3000);
  757. return image;
  758. }
  759. //==============================================================================
  760. void timerCallback()
  761. {
  762. stopTimer();
  763. deleteAndZero (image);
  764. }
  765. private:
  766. WindowsBitmapImage* image;
  767. TemporaryImage (const TemporaryImage&);
  768. const TemporaryImage& operator= (const TemporaryImage&);
  769. };
  770. TemporaryImage offscreenImageGenerator;
  771. //==============================================================================
  772. class WindowClassHolder : public DeletedAtShutdown
  773. {
  774. public:
  775. WindowClassHolder()
  776. : windowClassName ("JUCE_")
  777. {
  778. // this name has to be different for each app/dll instance because otherwise
  779. // poor old Win32 can get a bit confused (even despite it not being a process-global
  780. // window class).
  781. windowClassName << (int) (Time::currentTimeMillis() & 0x7fffffff);
  782. HINSTANCE moduleHandle = (HINSTANCE) PlatformUtilities::getCurrentModuleInstanceHandle();
  783. TCHAR moduleFile [1024];
  784. moduleFile[0] = 0;
  785. GetModuleFileName (moduleHandle, moduleFile, 1024);
  786. WORD iconNum = 0;
  787. WNDCLASSEX wcex;
  788. wcex.cbSize = sizeof (wcex);
  789. wcex.style = CS_OWNDC;
  790. wcex.lpfnWndProc = (WNDPROC) windowProc;
  791. wcex.lpszClassName = windowClassName;
  792. wcex.cbClsExtra = 0;
  793. wcex.cbWndExtra = 32;
  794. wcex.hInstance = moduleHandle;
  795. wcex.hIcon = ExtractAssociatedIcon (moduleHandle, moduleFile, &iconNum);
  796. iconNum = 1;
  797. wcex.hIconSm = ExtractAssociatedIcon (moduleHandle, moduleFile, &iconNum);
  798. wcex.hCursor = 0;
  799. wcex.hbrBackground = 0;
  800. wcex.lpszMenuName = 0;
  801. RegisterClassEx (&wcex);
  802. }
  803. ~WindowClassHolder()
  804. {
  805. if (ComponentPeer::getNumPeers() == 0)
  806. UnregisterClass (windowClassName, (HINSTANCE) PlatformUtilities::getCurrentModuleInstanceHandle());
  807. clearSingletonInstance();
  808. }
  809. String windowClassName;
  810. juce_DeclareSingleton_SingleThreaded_Minimal (WindowClassHolder);
  811. };
  812. //==============================================================================
  813. static void* createWindowCallback (void* userData)
  814. {
  815. ((Win32ComponentPeer*) userData)->createWindow();
  816. return 0;
  817. }
  818. void createWindow()
  819. {
  820. DWORD exstyle = WS_EX_ACCEPTFILES;
  821. DWORD type = WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  822. if (hasTitleBar())
  823. {
  824. type |= WS_OVERLAPPED;
  825. exstyle |= WS_EX_APPWINDOW;
  826. if ((styleFlags & windowHasCloseButton) != 0)
  827. {
  828. type |= WS_SYSMENU;
  829. }
  830. else
  831. {
  832. // annoyingly, windows won't let you have a min/max button without a close button
  833. jassert ((styleFlags & (windowHasMinimiseButton | windowHasMaximiseButton)) == 0);
  834. }
  835. if ((styleFlags & windowIsResizable) != 0)
  836. type |= WS_THICKFRAME;
  837. }
  838. else
  839. {
  840. type |= WS_POPUP | WS_SYSMENU;
  841. if ((styleFlags & windowAppearsOnTaskbar) == 0)
  842. exstyle |= WS_EX_TOOLWINDOW;
  843. else
  844. exstyle |= WS_EX_APPWINDOW;
  845. }
  846. if ((styleFlags & windowHasMinimiseButton) != 0)
  847. type |= WS_MINIMIZEBOX;
  848. if ((styleFlags & windowHasMaximiseButton) != 0)
  849. type |= WS_MAXIMIZEBOX;
  850. if ((styleFlags & windowIgnoresMouseClicks) != 0)
  851. exstyle |= WS_EX_TRANSPARENT;
  852. if ((styleFlags & juce_windowIsSemiTransparentFlag) != 0
  853. && Desktop::canUseSemiTransparentWindows())
  854. exstyle |= WS_EX_LAYERED;
  855. hwnd = CreateWindowEx (exstyle, WindowClassHolder::getInstance()->windowClassName, L"", type, 0, 0, 0, 0, 0, 0, 0, 0);
  856. if (hwnd != 0)
  857. {
  858. SetWindowLongPtr (hwnd, 0, 0);
  859. SetWindowLongPtr (hwnd, 8, (LONG_PTR) this);
  860. SetWindowLongPtr (hwnd, GWLP_USERDATA, improbableWindowNumber);
  861. if (dropTarget == 0)
  862. dropTarget = new JuceDropTarget (this);
  863. RegisterDragDrop (hwnd, dropTarget);
  864. updateBorderSize();
  865. // Calling this function here is (for some reason) necessary to make Windows
  866. // correctly enable the menu items that we specify in the wm_initmenu message.
  867. GetSystemMenu (hwnd, false);
  868. }
  869. else
  870. {
  871. jassertfalse
  872. }
  873. }
  874. static void* destroyWindowCallback (void* handle)
  875. {
  876. RevokeDragDrop ((HWND) handle);
  877. DestroyWindow ((HWND) handle);
  878. return 0;
  879. }
  880. static void* toFrontCallback1 (void* h)
  881. {
  882. SetForegroundWindow ((HWND) h);
  883. return 0;
  884. }
  885. static void* toFrontCallback2 (void* h)
  886. {
  887. SetWindowPos ((HWND) h, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING);
  888. return 0;
  889. }
  890. static void* setFocusCallback (void* h)
  891. {
  892. SetFocus ((HWND) h);
  893. return 0;
  894. }
  895. static void* getFocusCallback (void*)
  896. {
  897. return (void*) GetFocus();
  898. }
  899. void offsetWithinParent (int& x, int& y) const
  900. {
  901. if (isTransparent())
  902. {
  903. HWND parentHwnd = GetParent (hwnd);
  904. if (parentHwnd != 0)
  905. {
  906. RECT parentRect;
  907. GetWindowRect (parentHwnd, &parentRect);
  908. x += parentRect.left;
  909. y += parentRect.top;
  910. }
  911. }
  912. }
  913. bool isTransparent() const
  914. {
  915. return (GetWindowLong (hwnd, GWL_EXSTYLE) & WS_EX_LAYERED) != 0;
  916. }
  917. inline bool hasTitleBar() const throw() { return (styleFlags & windowHasTitleBar) != 0; }
  918. void setIcon (const Image& newIcon)
  919. {
  920. HICON hicon = createHICONFromImage (newIcon, TRUE, 0, 0);
  921. if (hicon != 0)
  922. {
  923. SendMessage (hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
  924. SendMessage (hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);
  925. if (currentWindowIcon != 0)
  926. DestroyIcon (currentWindowIcon);
  927. currentWindowIcon = hicon;
  928. }
  929. }
  930. //==============================================================================
  931. void handlePaintMessage()
  932. {
  933. #if DEBUG_REPAINT_TIMES
  934. const double paintStart = Time::getMillisecondCounterHiRes();
  935. #endif
  936. HRGN rgn = CreateRectRgn (0, 0, 0, 0);
  937. const int regionType = GetUpdateRgn (hwnd, rgn, false);
  938. PAINTSTRUCT paintStruct;
  939. HDC dc = BeginPaint (hwnd, &paintStruct); // Note this can immediately generate a WM_NCPAINT
  940. // message and become re-entrant, but that's OK
  941. // if something in a paint handler calls, e.g. a message box, this can become reentrant and
  942. // corrupt the image it's using to paint into, so do a check here.
  943. static bool reentrant = false;
  944. if (reentrant)
  945. {
  946. DeleteObject (rgn);
  947. EndPaint (hwnd, &paintStruct);
  948. return;
  949. }
  950. reentrant = true;
  951. // this is the rectangle to update..
  952. int x = paintStruct.rcPaint.left;
  953. int y = paintStruct.rcPaint.top;
  954. int w = paintStruct.rcPaint.right - x;
  955. int h = paintStruct.rcPaint.bottom - y;
  956. const bool transparent = isTransparent();
  957. if (transparent)
  958. {
  959. // it's not possible to have a transparent window with a title bar at the moment!
  960. jassert (! hasTitleBar());
  961. RECT r;
  962. GetWindowRect (hwnd, &r);
  963. x = y = 0;
  964. w = r.right - r.left;
  965. h = r.bottom - r.top;
  966. }
  967. if (w > 0 && h > 0)
  968. {
  969. clearMaskedRegion();
  970. WindowsBitmapImage* const offscreenImage = offscreenImageGenerator.getImage (transparent, w, h);
  971. LowLevelGraphicsSoftwareRenderer context (*offscreenImage);
  972. RectangleList* const contextClip = context.getRawClipRegion();
  973. contextClip->clear();
  974. context.setOrigin (-x, -y);
  975. bool needToPaintAll = true;
  976. if (regionType == COMPLEXREGION && ! transparent)
  977. {
  978. HRGN clipRgn = CreateRectRgnIndirect (&paintStruct.rcPaint);
  979. CombineRgn (rgn, rgn, clipRgn, RGN_AND);
  980. DeleteObject (clipRgn);
  981. char rgnData [8192];
  982. const DWORD res = GetRegionData (rgn, sizeof (rgnData), (RGNDATA*) rgnData);
  983. if (res > 0 && res <= sizeof (rgnData))
  984. {
  985. const RGNDATAHEADER* const hdr = &(((const RGNDATA*) rgnData)->rdh);
  986. if (hdr->iType == RDH_RECTANGLES
  987. && hdr->rcBound.right - hdr->rcBound.left >= w
  988. && hdr->rcBound.bottom - hdr->rcBound.top >= h)
  989. {
  990. needToPaintAll = false;
  991. const RECT* rects = (const RECT*) (rgnData + sizeof (RGNDATAHEADER));
  992. int num = ((RGNDATA*) rgnData)->rdh.nCount;
  993. while (--num >= 0)
  994. {
  995. // (need to move this one pixel to the left because of a win32 bug)
  996. const int cx = jmax (x, rects->left - 1);
  997. const int cy = rects->top;
  998. const int cw = rects->right - cx;
  999. const int ch = rects->bottom - rects->top;
  1000. if (cx + cw - x <= w && cy + ch - y <= h)
  1001. {
  1002. contextClip->addWithoutMerging (Rectangle (cx - x, cy - y, cw, ch));
  1003. }
  1004. else
  1005. {
  1006. needToPaintAll = true;
  1007. break;
  1008. }
  1009. ++rects;
  1010. }
  1011. }
  1012. }
  1013. }
  1014. if (needToPaintAll)
  1015. {
  1016. contextClip->clear();
  1017. contextClip->addWithoutMerging (Rectangle (0, 0, w, h));
  1018. }
  1019. if (transparent)
  1020. {
  1021. RectangleList::Iterator i (*contextClip);
  1022. while (i.next())
  1023. {
  1024. const Rectangle& r = *i.getRectangle();
  1025. offscreenImage->clear (r.getX(), r.getY(), r.getWidth(), r.getHeight());
  1026. }
  1027. }
  1028. // if the component's not opaque, this won't draw properly unless the platform can support this
  1029. jassert (Desktop::canUseSemiTransparentWindows() || component->isOpaque());
  1030. updateCurrentModifiers();
  1031. handlePaint (context);
  1032. if (! dontRepaint)
  1033. offscreenImage->blitToWindow (hwnd, dc, transparent, x, y, maskedRegion);
  1034. }
  1035. DeleteObject (rgn);
  1036. EndPaint (hwnd, &paintStruct);
  1037. reentrant = false;
  1038. #ifndef JUCE_GCC //xxx should add this fn for gcc..
  1039. _fpreset(); // because some graphics cards can unmask FP exceptions
  1040. #endif
  1041. lastPaintTime = Time::getMillisecondCounter();
  1042. #if DEBUG_REPAINT_TIMES
  1043. const double elapsed = Time::getMillisecondCounterHiRes() - paintStart;
  1044. Logger::outputDebugString (T("repaint time: ") + String (elapsed, 2));
  1045. #endif
  1046. }
  1047. //==============================================================================
  1048. void doMouseMove (const int x, const int y)
  1049. {
  1050. static uint32 lastMouseTime = 0;
  1051. // this can be set to throttle the mouse-messages to less than a
  1052. // certain number per second, as things can get unresponsive
  1053. // if each drag or move callback has to do a lot of work.
  1054. const int maxMouseMovesPerSecond = 60;
  1055. const int64 mouseEventTime = getMouseEventTime();
  1056. if (! isMouseOver)
  1057. {
  1058. isMouseOver = true;
  1059. TRACKMOUSEEVENT tme;
  1060. tme.cbSize = sizeof (tme);
  1061. tme.dwFlags = TME_LEAVE;
  1062. tme.hwndTrack = hwnd;
  1063. tme.dwHoverTime = 0;
  1064. if (! TrackMouseEvent (&tme))
  1065. {
  1066. jassertfalse;
  1067. }
  1068. updateKeyModifiers();
  1069. handleMouseEnter (x, y, mouseEventTime);
  1070. }
  1071. else if (! isDragging)
  1072. {
  1073. if (((unsigned int) x) < (unsigned int) component->getWidth()
  1074. && ((unsigned int) y) < (unsigned int) component->getHeight())
  1075. {
  1076. RECT r;
  1077. GetWindowRect (hwnd, &r);
  1078. POINT p;
  1079. p.x = x + r.left + windowBorder.getLeft();
  1080. p.y = y + r.top + windowBorder.getTop();
  1081. if (WindowFromPoint (p) == hwnd)
  1082. {
  1083. const uint32 now = Time::getMillisecondCounter();
  1084. if (now > lastMouseTime + 1000 / maxMouseMovesPerSecond)
  1085. {
  1086. lastMouseTime = now;
  1087. handleMouseMove (x, y, mouseEventTime);
  1088. }
  1089. }
  1090. }
  1091. }
  1092. else
  1093. {
  1094. const uint32 now = Time::getMillisecondCounter();
  1095. if (now > lastMouseTime + 1000 / maxMouseMovesPerSecond)
  1096. {
  1097. lastMouseTime = now;
  1098. handleMouseDrag (x, y, mouseEventTime);
  1099. }
  1100. }
  1101. }
  1102. void doMouseDown (const int x, const int y, const WPARAM wParam)
  1103. {
  1104. if (GetCapture() != hwnd)
  1105. SetCapture (hwnd);
  1106. doMouseMove (x, y);
  1107. currentModifiers &= ~ModifierKeys::allMouseButtonModifiers;
  1108. if ((wParam & MK_LBUTTON) != 0)
  1109. currentModifiers |= ModifierKeys::leftButtonModifier;
  1110. if ((wParam & MK_RBUTTON) != 0)
  1111. currentModifiers |= ModifierKeys::rightButtonModifier;
  1112. if ((wParam & MK_MBUTTON) != 0)
  1113. currentModifiers |= ModifierKeys::middleButtonModifier;
  1114. updateKeyModifiers();
  1115. isDragging = true;
  1116. handleMouseDown (x, y, getMouseEventTime());
  1117. }
  1118. void doMouseUp (const int x, const int y, const WPARAM wParam)
  1119. {
  1120. int numButtons = 0;
  1121. if ((wParam & MK_LBUTTON) != 0)
  1122. ++numButtons;
  1123. if ((wParam & MK_RBUTTON) != 0)
  1124. ++numButtons;
  1125. if ((wParam & MK_MBUTTON) != 0)
  1126. ++numButtons;
  1127. const int oldModifiers = currentModifiers;
  1128. // update the currentmodifiers only after the callback, so the callback
  1129. // knows which button was released.
  1130. currentModifiers &= ~ModifierKeys::allMouseButtonModifiers;
  1131. if ((wParam & MK_LBUTTON) != 0)
  1132. currentModifiers |= ModifierKeys::leftButtonModifier;
  1133. if ((wParam & MK_RBUTTON) != 0)
  1134. currentModifiers |= ModifierKeys::rightButtonModifier;
  1135. if ((wParam & MK_MBUTTON) != 0)
  1136. currentModifiers |= ModifierKeys::middleButtonModifier;
  1137. updateKeyModifiers();
  1138. isDragging = false;
  1139. // release the mouse capture if the user's not still got a button down
  1140. if (numButtons == 0 && hwnd == GetCapture())
  1141. ReleaseCapture();
  1142. handleMouseUp (oldModifiers, x, y, getMouseEventTime());
  1143. }
  1144. void doCaptureChanged()
  1145. {
  1146. if (isDragging)
  1147. {
  1148. RECT wr;
  1149. GetWindowRect (hwnd, &wr);
  1150. const DWORD mp = GetMessagePos();
  1151. doMouseUp (GET_X_LPARAM (mp) - wr.left - windowBorder.getLeft(),
  1152. GET_Y_LPARAM (mp) - wr.top - windowBorder.getTop(),
  1153. getMouseEventTime());
  1154. }
  1155. }
  1156. void doMouseExit()
  1157. {
  1158. if (isMouseOver)
  1159. {
  1160. isMouseOver = false;
  1161. RECT wr;
  1162. GetWindowRect (hwnd, &wr);
  1163. const DWORD mp = GetMessagePos();
  1164. handleMouseExit (GET_X_LPARAM (mp) - wr.left - windowBorder.getLeft(),
  1165. GET_Y_LPARAM (mp) - wr.top - windowBorder.getTop(),
  1166. getMouseEventTime());
  1167. }
  1168. }
  1169. void doMouseWheel (const WPARAM wParam, const bool isVertical)
  1170. {
  1171. updateKeyModifiers();
  1172. const int amount = jlimit (-1000, 1000, (int) (0.75f * (short) HIWORD (wParam)));
  1173. handleMouseWheel (isVertical ? 0 : amount,
  1174. isVertical ? amount : 0,
  1175. getMouseEventTime());
  1176. }
  1177. //==============================================================================
  1178. void sendModifierKeyChangeIfNeeded()
  1179. {
  1180. if (modifiersAtLastCallback != currentModifiers)
  1181. {
  1182. modifiersAtLastCallback = currentModifiers;
  1183. handleModifierKeysChange();
  1184. }
  1185. }
  1186. bool doKeyUp (const WPARAM key)
  1187. {
  1188. updateKeyModifiers();
  1189. switch (key)
  1190. {
  1191. case VK_SHIFT:
  1192. case VK_CONTROL:
  1193. case VK_MENU:
  1194. case VK_CAPITAL:
  1195. case VK_LWIN:
  1196. case VK_RWIN:
  1197. case VK_APPS:
  1198. case VK_NUMLOCK:
  1199. case VK_SCROLL:
  1200. case VK_LSHIFT:
  1201. case VK_RSHIFT:
  1202. case VK_LCONTROL:
  1203. case VK_LMENU:
  1204. case VK_RCONTROL:
  1205. case VK_RMENU:
  1206. sendModifierKeyChangeIfNeeded();
  1207. }
  1208. return handleKeyUpOrDown();
  1209. }
  1210. bool doKeyDown (const WPARAM key)
  1211. {
  1212. updateKeyModifiers();
  1213. bool used = false;
  1214. switch (key)
  1215. {
  1216. case VK_SHIFT:
  1217. case VK_LSHIFT:
  1218. case VK_RSHIFT:
  1219. case VK_CONTROL:
  1220. case VK_LCONTROL:
  1221. case VK_RCONTROL:
  1222. case VK_MENU:
  1223. case VK_LMENU:
  1224. case VK_RMENU:
  1225. case VK_LWIN:
  1226. case VK_RWIN:
  1227. case VK_CAPITAL:
  1228. case VK_NUMLOCK:
  1229. case VK_SCROLL:
  1230. case VK_APPS:
  1231. sendModifierKeyChangeIfNeeded();
  1232. break;
  1233. case VK_LEFT:
  1234. case VK_RIGHT:
  1235. case VK_UP:
  1236. case VK_DOWN:
  1237. case VK_PRIOR:
  1238. case VK_NEXT:
  1239. case VK_HOME:
  1240. case VK_END:
  1241. case VK_DELETE:
  1242. case VK_INSERT:
  1243. case VK_F1:
  1244. case VK_F2:
  1245. case VK_F3:
  1246. case VK_F4:
  1247. case VK_F5:
  1248. case VK_F6:
  1249. case VK_F7:
  1250. case VK_F8:
  1251. case VK_F9:
  1252. case VK_F10:
  1253. case VK_F11:
  1254. case VK_F12:
  1255. case VK_F13:
  1256. case VK_F14:
  1257. case VK_F15:
  1258. case VK_F16:
  1259. used = handleKeyUpOrDown();
  1260. used = handleKeyPress (extendedKeyModifier | (int) key, 0) || used;
  1261. break;
  1262. case VK_ADD:
  1263. case VK_SUBTRACT:
  1264. case VK_MULTIPLY:
  1265. case VK_DIVIDE:
  1266. case VK_SEPARATOR:
  1267. case VK_DECIMAL:
  1268. used = handleKeyUpOrDown();
  1269. break;
  1270. default:
  1271. used = handleKeyUpOrDown();
  1272. {
  1273. MSG msg;
  1274. if (! PeekMessage (&msg, hwnd, WM_CHAR, WM_DEADCHAR, PM_NOREMOVE))
  1275. {
  1276. // if there isn't a WM_CHAR or WM_DEADCHAR message pending, we need to
  1277. // manually generate the key-press event that matches this key-down.
  1278. const UINT keyChar = MapVirtualKey (key, 2);
  1279. used = handleKeyPress ((int) LOWORD (keyChar), 0) || used;
  1280. }
  1281. }
  1282. break;
  1283. }
  1284. return used;
  1285. }
  1286. bool doKeyChar (int key, const LPARAM flags)
  1287. {
  1288. updateKeyModifiers();
  1289. juce_wchar textChar = (juce_wchar) key;
  1290. const int virtualScanCode = (flags >> 16) & 0xff;
  1291. if (key >= '0' && key <= '9')
  1292. {
  1293. switch (virtualScanCode) // check for a numeric keypad scan-code
  1294. {
  1295. case 0x52:
  1296. case 0x4f:
  1297. case 0x50:
  1298. case 0x51:
  1299. case 0x4b:
  1300. case 0x4c:
  1301. case 0x4d:
  1302. case 0x47:
  1303. case 0x48:
  1304. case 0x49:
  1305. key = (key - '0') + KeyPress::numberPad0;
  1306. break;
  1307. default:
  1308. break;
  1309. }
  1310. }
  1311. else
  1312. {
  1313. // convert the scan code to an unmodified character code..
  1314. const UINT virtualKey = MapVirtualKey (virtualScanCode, 1);
  1315. UINT keyChar = MapVirtualKey (virtualKey, 2);
  1316. keyChar = LOWORD (keyChar);
  1317. if (keyChar != 0)
  1318. key = (int) keyChar;
  1319. // avoid sending junk text characters for some control-key combinations
  1320. if (textChar < ' ' && (currentModifiers & (ModifierKeys::ctrlModifier | ModifierKeys::altModifier)) != 0)
  1321. textChar = 0;
  1322. }
  1323. return handleKeyPress (key, textChar);
  1324. }
  1325. bool doAppCommand (const LPARAM lParam)
  1326. {
  1327. int key = 0;
  1328. switch (GET_APPCOMMAND_LPARAM (lParam))
  1329. {
  1330. case APPCOMMAND_MEDIA_PLAY_PAUSE:
  1331. key = KeyPress::playKey;
  1332. break;
  1333. case APPCOMMAND_MEDIA_STOP:
  1334. key = KeyPress::stopKey;
  1335. break;
  1336. case APPCOMMAND_MEDIA_NEXTTRACK:
  1337. key = KeyPress::fastForwardKey;
  1338. break;
  1339. case APPCOMMAND_MEDIA_PREVIOUSTRACK:
  1340. key = KeyPress::rewindKey;
  1341. break;
  1342. }
  1343. if (key != 0)
  1344. {
  1345. updateKeyModifiers();
  1346. if (hwnd == GetActiveWindow())
  1347. {
  1348. handleKeyPress (key, 0);
  1349. return true;
  1350. }
  1351. }
  1352. return false;
  1353. }
  1354. //==============================================================================
  1355. class JuceDropTarget : public IDropTarget
  1356. {
  1357. public:
  1358. JuceDropTarget (Win32ComponentPeer* const owner_)
  1359. : owner (owner_),
  1360. refCount (1)
  1361. {
  1362. }
  1363. virtual ~JuceDropTarget()
  1364. {
  1365. jassert (refCount == 0);
  1366. }
  1367. HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
  1368. {
  1369. if (id == IID_IUnknown || id == IID_IDropTarget)
  1370. {
  1371. AddRef();
  1372. *result = this;
  1373. return S_OK;
  1374. }
  1375. *result = 0;
  1376. return E_NOINTERFACE;
  1377. }
  1378. ULONG __stdcall AddRef() { return ++refCount; }
  1379. ULONG __stdcall Release() { jassert (refCount > 0); const int r = --refCount; if (r == 0) delete this; return r; }
  1380. HRESULT __stdcall DragEnter (IDataObject* pDataObject, DWORD /*grfKeyState*/, POINTL mousePos, DWORD* pdwEffect)
  1381. {
  1382. updateFileList (pDataObject);
  1383. int x = mousePos.x, y = mousePos.y;
  1384. owner->globalPositionToRelative (x, y);
  1385. owner->handleFileDragMove (files, x, y);
  1386. *pdwEffect = DROPEFFECT_COPY;
  1387. return S_OK;
  1388. }
  1389. HRESULT __stdcall DragLeave()
  1390. {
  1391. owner->handleFileDragExit (files);
  1392. return S_OK;
  1393. }
  1394. HRESULT __stdcall DragOver (DWORD /*grfKeyState*/, POINTL mousePos, DWORD* pdwEffect)
  1395. {
  1396. int x = mousePos.x, y = mousePos.y;
  1397. owner->globalPositionToRelative (x, y);
  1398. owner->handleFileDragMove (files, x, y);
  1399. *pdwEffect = DROPEFFECT_COPY;
  1400. return S_OK;
  1401. }
  1402. HRESULT __stdcall Drop (IDataObject* pDataObject, DWORD /*grfKeyState*/, POINTL mousePos, DWORD* pdwEffect)
  1403. {
  1404. updateFileList (pDataObject);
  1405. int x = mousePos.x, y = mousePos.y;
  1406. owner->globalPositionToRelative (x, y);
  1407. owner->handleFileDragDrop (files, x, y);
  1408. *pdwEffect = DROPEFFECT_COPY;
  1409. return S_OK;
  1410. }
  1411. private:
  1412. Win32ComponentPeer* const owner;
  1413. int refCount;
  1414. StringArray files;
  1415. void updateFileList (IDataObject* const pDataObject)
  1416. {
  1417. files.clear();
  1418. FORMATETC format = { CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  1419. STGMEDIUM medium = { TYMED_HGLOBAL, { 0 }, 0 };
  1420. if (pDataObject->GetData (&format, &medium) == S_OK)
  1421. {
  1422. const SIZE_T totalLen = GlobalSize (medium.hGlobal);
  1423. const LPDROPFILES pDropFiles = (const LPDROPFILES) GlobalLock (medium.hGlobal);
  1424. unsigned int i = 0;
  1425. if (pDropFiles->fWide)
  1426. {
  1427. const WCHAR* const fname = (WCHAR*) (((const char*) pDropFiles) + sizeof (DROPFILES));
  1428. for (;;)
  1429. {
  1430. unsigned int len = 0;
  1431. while (i + len < totalLen && fname [i + len] != 0)
  1432. ++len;
  1433. if (len == 0)
  1434. break;
  1435. files.add (String (fname + i, len));
  1436. i += len + 1;
  1437. }
  1438. }
  1439. else
  1440. {
  1441. const char* const fname = ((const char*) pDropFiles) + sizeof (DROPFILES);
  1442. for (;;)
  1443. {
  1444. unsigned int len = 0;
  1445. while (i + len < totalLen && fname [i + len] != 0)
  1446. ++len;
  1447. if (len == 0)
  1448. break;
  1449. files.add (String (fname + i, len));
  1450. i += len + 1;
  1451. }
  1452. }
  1453. GlobalUnlock (medium.hGlobal);
  1454. }
  1455. }
  1456. JuceDropTarget (const JuceDropTarget&);
  1457. const JuceDropTarget& operator= (const JuceDropTarget&);
  1458. };
  1459. void doSettingChange()
  1460. {
  1461. Desktop::getInstance().refreshMonitorSizes();
  1462. if (fullScreen && ! isMinimised())
  1463. {
  1464. const Rectangle r (component->getParentMonitorArea());
  1465. SetWindowPos (hwnd, 0,
  1466. r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  1467. SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOSENDCHANGING);
  1468. }
  1469. }
  1470. //==============================================================================
  1471. public:
  1472. static LRESULT CALLBACK windowProc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
  1473. {
  1474. Win32ComponentPeer* const peer = getOwnerOfWindow (h);
  1475. if (peer != 0)
  1476. return peer->peerWindowProc (h, message, wParam, lParam);
  1477. return DefWindowProc (h, message, wParam, lParam);
  1478. }
  1479. private:
  1480. LRESULT peerWindowProc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
  1481. {
  1482. {
  1483. const MessageManagerLock messLock;
  1484. if (isValidPeer (this))
  1485. {
  1486. switch (message)
  1487. {
  1488. case WM_NCHITTEST:
  1489. if (hasTitleBar())
  1490. break;
  1491. return HTCLIENT;
  1492. //==============================================================================
  1493. case WM_PAINT:
  1494. handlePaintMessage();
  1495. return 0;
  1496. case WM_NCPAINT:
  1497. if (wParam != 1)
  1498. handlePaintMessage();
  1499. if (hasTitleBar())
  1500. break;
  1501. return 0;
  1502. case WM_ERASEBKGND:
  1503. case WM_NCCALCSIZE:
  1504. if (hasTitleBar())
  1505. break;
  1506. return 1;
  1507. //==============================================================================
  1508. case WM_MOUSEMOVE:
  1509. doMouseMove (GET_X_LPARAM (lParam), GET_Y_LPARAM (lParam));
  1510. return 0;
  1511. case WM_MOUSELEAVE:
  1512. doMouseExit();
  1513. return 0;
  1514. case WM_LBUTTONDOWN:
  1515. case WM_MBUTTONDOWN:
  1516. case WM_RBUTTONDOWN:
  1517. doMouseDown (GET_X_LPARAM (lParam), GET_Y_LPARAM (lParam), wParam);
  1518. return 0;
  1519. case WM_LBUTTONUP:
  1520. case WM_MBUTTONUP:
  1521. case WM_RBUTTONUP:
  1522. doMouseUp (GET_X_LPARAM (lParam), GET_Y_LPARAM (lParam), wParam);
  1523. return 0;
  1524. case WM_CAPTURECHANGED:
  1525. doCaptureChanged();
  1526. return 0;
  1527. case WM_NCMOUSEMOVE:
  1528. if (hasTitleBar())
  1529. break;
  1530. return 0;
  1531. case 0x020A: /* WM_MOUSEWHEEL */
  1532. doMouseWheel (wParam, true);
  1533. return 0;
  1534. case 0x020E: /* WM_MOUSEHWHEEL */
  1535. doMouseWheel (wParam, false);
  1536. return 0;
  1537. //==============================================================================
  1538. case WM_WINDOWPOSCHANGING:
  1539. if ((styleFlags & (windowHasTitleBar | windowIsResizable)) == (windowHasTitleBar | windowIsResizable))
  1540. {
  1541. WINDOWPOS* const wp = (WINDOWPOS*) lParam;
  1542. if ((wp->flags & (SWP_NOMOVE | SWP_NOSIZE)) != (SWP_NOMOVE | SWP_NOSIZE))
  1543. {
  1544. if (constrainer != 0)
  1545. {
  1546. const Rectangle current (component->getX() - windowBorder.getLeft(),
  1547. component->getY() - windowBorder.getTop(),
  1548. component->getWidth() + windowBorder.getLeftAndRight(),
  1549. component->getHeight() + windowBorder.getTopAndBottom());
  1550. constrainer->checkBounds (wp->x, wp->y, wp->cx, wp->cy,
  1551. current,
  1552. Desktop::getInstance().getAllMonitorDisplayAreas().getBounds(),
  1553. wp->y != current.getY() && wp->y + wp->cy == current.getBottom(),
  1554. wp->x != current.getX() && wp->x + wp->cx == current.getRight(),
  1555. wp->y == current.getY() && wp->y + wp->cy != current.getBottom(),
  1556. wp->x == current.getX() && wp->x + wp->cx != current.getRight());
  1557. }
  1558. }
  1559. }
  1560. return 0;
  1561. case WM_WINDOWPOSCHANGED:
  1562. handleMovedOrResized();
  1563. if (dontRepaint)
  1564. break; // needed for non-accelerated openGL windows to draw themselves correctly..
  1565. else
  1566. return 0;
  1567. //==============================================================================
  1568. case WM_KEYDOWN:
  1569. case WM_SYSKEYDOWN:
  1570. if (doKeyDown (wParam))
  1571. return 0;
  1572. break;
  1573. case WM_KEYUP:
  1574. case WM_SYSKEYUP:
  1575. if (doKeyUp (wParam))
  1576. return 0;
  1577. break;
  1578. case WM_CHAR:
  1579. if (doKeyChar ((int) wParam, lParam))
  1580. return 0;
  1581. break;
  1582. case WM_APPCOMMAND:
  1583. if (doAppCommand (lParam))
  1584. return TRUE;
  1585. break;
  1586. //==============================================================================
  1587. case WM_SETFOCUS:
  1588. updateKeyModifiers();
  1589. handleFocusGain();
  1590. break;
  1591. case WM_KILLFOCUS:
  1592. handleFocusLoss();
  1593. break;
  1594. case WM_ACTIVATEAPP:
  1595. // Windows does weird things to process priority when you swap apps,
  1596. // so this forces an update when the app is brought to the front
  1597. if (wParam != FALSE)
  1598. juce_repeatLastProcessPriority();
  1599. juce_CheckCurrentlyFocusedTopLevelWindow();
  1600. modifiersAtLastCallback = -1;
  1601. return 0;
  1602. case WM_ACTIVATE:
  1603. if (LOWORD (wParam) == WA_ACTIVE || LOWORD (wParam) == WA_CLICKACTIVE)
  1604. {
  1605. modifiersAtLastCallback = -1;
  1606. updateKeyModifiers();
  1607. if (isMinimised())
  1608. {
  1609. component->repaint();
  1610. handleMovedOrResized();
  1611. if (! isValidMessageListener())
  1612. return 0;
  1613. }
  1614. if (LOWORD (wParam) == WA_CLICKACTIVE
  1615. && component->isCurrentlyBlockedByAnotherModalComponent())
  1616. {
  1617. int mx, my;
  1618. component->getMouseXYRelative (mx, my);
  1619. Component* const underMouse = component->getComponentAt (mx, my);
  1620. if (underMouse != 0 && underMouse->isCurrentlyBlockedByAnotherModalComponent())
  1621. Component::getCurrentlyModalComponent()->inputAttemptWhenModal();
  1622. return 0;
  1623. }
  1624. handleBroughtToFront();
  1625. return 0;
  1626. }
  1627. break;
  1628. case WM_NCACTIVATE:
  1629. // while a temporary window is being shown, prevent Windows from deactivating the
  1630. // title bars of our main windows.
  1631. if (wParam == 0 && ! shouldDeactivateTitleBar)
  1632. wParam = TRUE; // change this and let it get passed to the DefWindowProc.
  1633. break;
  1634. case WM_MOUSEACTIVATE:
  1635. if (! component->getMouseClickGrabsKeyboardFocus())
  1636. return MA_NOACTIVATE;
  1637. break;
  1638. case WM_SHOWWINDOW:
  1639. if (wParam != 0)
  1640. handleBroughtToFront();
  1641. break;
  1642. case WM_CLOSE:
  1643. handleUserClosingWindow();
  1644. return 0;
  1645. //==============================================================================
  1646. case WM_TRAYNOTIFY:
  1647. if (component->isCurrentlyBlockedByAnotherModalComponent())
  1648. {
  1649. if (lParam == WM_LBUTTONDOWN || lParam == WM_RBUTTONDOWN
  1650. || lParam == WM_LBUTTONDBLCLK || lParam == WM_LBUTTONDBLCLK)
  1651. {
  1652. Component* const current = Component::getCurrentlyModalComponent();
  1653. if (current != 0)
  1654. current->inputAttemptWhenModal();
  1655. }
  1656. }
  1657. else
  1658. {
  1659. MouseEvent e (0, 0, ModifierKeys::getCurrentModifiersRealtime(), component,
  1660. getMouseEventTime(), 0, 0, getMouseEventTime(), 1, false);
  1661. if (lParam == WM_LBUTTONDOWN || lParam == WM_LBUTTONDBLCLK)
  1662. e.mods = ModifierKeys (e.mods.getRawFlags() | ModifierKeys::leftButtonModifier);
  1663. else if (lParam == WM_RBUTTONDOWN || lParam == WM_RBUTTONDBLCLK)
  1664. e.mods = ModifierKeys (e.mods.getRawFlags() | ModifierKeys::rightButtonModifier);
  1665. if (lParam == WM_LBUTTONDOWN || lParam == WM_RBUTTONDOWN)
  1666. {
  1667. SetFocus (hwnd);
  1668. SetForegroundWindow (hwnd);
  1669. component->mouseDown (e);
  1670. }
  1671. else if (lParam == WM_LBUTTONUP || lParam == WM_RBUTTONUP)
  1672. component->mouseUp (e);
  1673. else if (lParam == WM_LBUTTONDBLCLK || lParam == WM_LBUTTONDBLCLK)
  1674. component->mouseDoubleClick (e);
  1675. else if (lParam == WM_MOUSEMOVE)
  1676. component->mouseMove (e);
  1677. }
  1678. break;
  1679. //==============================================================================
  1680. case WM_SYNCPAINT:
  1681. return 0;
  1682. case WM_PALETTECHANGED:
  1683. InvalidateRect (h, 0, 0);
  1684. break;
  1685. case WM_DISPLAYCHANGE:
  1686. InvalidateRect (h, 0, 0);
  1687. createPaletteIfNeeded = true;
  1688. // intentional fall-through...
  1689. case WM_SETTINGCHANGE: // note the fall-through in the previous case!
  1690. doSettingChange();
  1691. break;
  1692. case WM_INITMENU:
  1693. if (! hasTitleBar())
  1694. {
  1695. if (isFullScreen())
  1696. {
  1697. EnableMenuItem ((HMENU) wParam, SC_RESTORE, MF_BYCOMMAND | MF_ENABLED);
  1698. EnableMenuItem ((HMENU) wParam, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
  1699. }
  1700. else if (! isMinimised())
  1701. {
  1702. EnableMenuItem ((HMENU) wParam, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
  1703. }
  1704. }
  1705. break;
  1706. case WM_SYSCOMMAND:
  1707. switch (wParam & 0xfff0)
  1708. {
  1709. case SC_CLOSE:
  1710. if (hasTitleBar())
  1711. {
  1712. PostMessage (h, WM_CLOSE, 0, 0);
  1713. return 0;
  1714. }
  1715. break;
  1716. case SC_KEYMENU:
  1717. if (hasTitleBar() && h == GetCapture())
  1718. ReleaseCapture();
  1719. break;
  1720. case SC_MAXIMIZE:
  1721. setFullScreen (true);
  1722. return 0;
  1723. case SC_MINIMIZE:
  1724. if (! hasTitleBar())
  1725. {
  1726. setMinimised (true);
  1727. return 0;
  1728. }
  1729. break;
  1730. case SC_RESTORE:
  1731. if (hasTitleBar())
  1732. {
  1733. if (isFullScreen())
  1734. {
  1735. setFullScreen (false);
  1736. return 0;
  1737. }
  1738. }
  1739. else
  1740. {
  1741. if (isMinimised())
  1742. setMinimised (false);
  1743. else if (isFullScreen())
  1744. setFullScreen (false);
  1745. return 0;
  1746. }
  1747. break;
  1748. case SC_MONITORPOWER:
  1749. case SC_SCREENSAVE:
  1750. if (! screenSaverAllowed)
  1751. return 0;
  1752. break;
  1753. }
  1754. break;
  1755. case WM_NCLBUTTONDOWN:
  1756. case WM_NCRBUTTONDOWN:
  1757. case WM_NCMBUTTONDOWN:
  1758. if (component->isCurrentlyBlockedByAnotherModalComponent())
  1759. {
  1760. Component* const current = Component::getCurrentlyModalComponent();
  1761. if (current != 0)
  1762. current->inputAttemptWhenModal();
  1763. }
  1764. break;
  1765. //case WM_IME_STARTCOMPOSITION;
  1766. // return 0;
  1767. case WM_GETDLGCODE:
  1768. return DLGC_WANTALLKEYS;
  1769. default:
  1770. break;
  1771. }
  1772. }
  1773. }
  1774. // (the message manager lock exits before calling this, to avoid deadlocks if
  1775. // this calls into non-juce windows)
  1776. return DefWindowProc (h, message, wParam, lParam);
  1777. }
  1778. Win32ComponentPeer (const Win32ComponentPeer&);
  1779. const Win32ComponentPeer& operator= (const Win32ComponentPeer&);
  1780. };
  1781. ComponentPeer* Component::createNewPeer (int styleFlags, void* /*nativeWindowToAttachTo*/)
  1782. {
  1783. return new Win32ComponentPeer (this, styleFlags);
  1784. }
  1785. juce_ImplementSingleton_SingleThreaded (Win32ComponentPeer::WindowClassHolder);
  1786. //==============================================================================
  1787. void SystemTrayIconComponent::setIconImage (const Image& newImage)
  1788. {
  1789. Win32ComponentPeer* const wp = dynamic_cast <Win32ComponentPeer*> (getPeer());
  1790. if (wp != 0)
  1791. wp->setTaskBarIcon (&newImage);
  1792. }
  1793. void SystemTrayIconComponent::setIconTooltip (const String& tooltip)
  1794. {
  1795. Win32ComponentPeer* const wp = dynamic_cast <Win32ComponentPeer*> (getPeer());
  1796. if (wp != 0)
  1797. wp->setTaskBarIconToolTip (tooltip);
  1798. }
  1799. //==============================================================================
  1800. void juce_setWindowStyleBit (HWND h, const int styleType, const int feature, const bool bitIsSet) throw()
  1801. {
  1802. DWORD val = GetWindowLong (h, styleType);
  1803. if (bitIsSet)
  1804. val |= feature;
  1805. else
  1806. val &= ~feature;
  1807. SetWindowLongPtr (h, styleType, val);
  1808. SetWindowPos (h, 0, 0, 0, 0, 0,
  1809. SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER
  1810. | SWP_NOOWNERZORDER | SWP_FRAMECHANGED | SWP_NOSENDCHANGING);
  1811. }
  1812. //==============================================================================
  1813. bool Process::isForegroundProcess() throw()
  1814. {
  1815. HWND fg = GetForegroundWindow();
  1816. if (fg == 0)
  1817. return true;
  1818. DWORD processId = 0;
  1819. GetWindowThreadProcessId (fg, &processId);
  1820. return processId == GetCurrentProcessId();
  1821. }
  1822. //==============================================================================
  1823. void Desktop::getMousePosition (int& x, int& y) throw()
  1824. {
  1825. POINT mousePos;
  1826. GetCursorPos (&mousePos);
  1827. x = mousePos.x;
  1828. y = mousePos.y;
  1829. }
  1830. void Desktop::setMousePosition (int x, int y) throw()
  1831. {
  1832. SetCursorPos (x, y);
  1833. }
  1834. //==============================================================================
  1835. void Desktop::setScreenSaverEnabled (const bool isEnabled) throw()
  1836. {
  1837. screenSaverAllowed = isEnabled;
  1838. }
  1839. bool Desktop::isScreenSaverEnabled() throw()
  1840. {
  1841. return screenSaverAllowed;
  1842. }
  1843. //==============================================================================
  1844. static BOOL CALLBACK enumMonitorsProc (HMONITOR, HDC, LPRECT r, LPARAM userInfo)
  1845. {
  1846. Array <Rectangle>* const monitorCoords = (Array <Rectangle>*) userInfo;
  1847. monitorCoords->add (Rectangle (r->left, r->top, r->right - r->left, r->bottom - r->top));
  1848. return TRUE;
  1849. }
  1850. void juce_updateMultiMonitorInfo (Array <Rectangle>& monitorCoords, const bool clipToWorkArea) throw()
  1851. {
  1852. EnumDisplayMonitors (0, 0, &enumMonitorsProc, (LPARAM) &monitorCoords);
  1853. // make sure the first in the list is the main monitor
  1854. for (int i = 1; i < monitorCoords.size(); ++i)
  1855. if (monitorCoords[i].getX() == 0 && monitorCoords[i].getY() == 0)
  1856. monitorCoords.swap (i, 0);
  1857. if (monitorCoords.size() == 0)
  1858. {
  1859. RECT r;
  1860. GetWindowRect (GetDesktopWindow(), &r);
  1861. monitorCoords.add (Rectangle (r.left, r.top, r.right - r.left, r.bottom - r.top));
  1862. }
  1863. if (clipToWorkArea)
  1864. {
  1865. // clip the main monitor to the active non-taskbar area
  1866. RECT r;
  1867. SystemParametersInfo (SPI_GETWORKAREA, 0, &r, 0);
  1868. Rectangle& screen = monitorCoords.getReference (0);
  1869. screen.setPosition (jmax (screen.getX(), r.left),
  1870. jmax (screen.getY(), r.top));
  1871. screen.setSize (jmin (screen.getRight(), r.right) - screen.getX(),
  1872. jmin (screen.getBottom(), r.bottom) - screen.getY());
  1873. }
  1874. }
  1875. //==============================================================================
  1876. static Image* createImageFromHBITMAP (HBITMAP bitmap) throw()
  1877. {
  1878. Image* im = 0;
  1879. if (bitmap != 0)
  1880. {
  1881. BITMAP bm;
  1882. if (GetObject (bitmap, sizeof (BITMAP), &bm)
  1883. && bm.bmWidth > 0 && bm.bmHeight > 0)
  1884. {
  1885. HDC tempDC = GetDC (0);
  1886. HDC dc = CreateCompatibleDC (tempDC);
  1887. ReleaseDC (0, tempDC);
  1888. SelectObject (dc, bitmap);
  1889. im = new Image (Image::ARGB, bm.bmWidth, bm.bmHeight, true);
  1890. for (int y = bm.bmHeight; --y >= 0;)
  1891. {
  1892. for (int x = bm.bmWidth; --x >= 0;)
  1893. {
  1894. COLORREF col = GetPixel (dc, x, y);
  1895. im->setPixelAt (x, y, Colour ((uint8) GetRValue (col),
  1896. (uint8) GetGValue (col),
  1897. (uint8) GetBValue (col)));
  1898. }
  1899. }
  1900. DeleteDC (dc);
  1901. }
  1902. }
  1903. return im;
  1904. }
  1905. static Image* createImageFromHICON (HICON icon) throw()
  1906. {
  1907. ICONINFO info;
  1908. if (GetIconInfo (icon, &info))
  1909. {
  1910. Image* const mask = createImageFromHBITMAP (info.hbmMask);
  1911. if (mask == 0)
  1912. return 0;
  1913. Image* const image = createImageFromHBITMAP (info.hbmColor);
  1914. if (image == 0)
  1915. return mask;
  1916. for (int y = image->getHeight(); --y >= 0;)
  1917. {
  1918. for (int x = image->getWidth(); --x >= 0;)
  1919. {
  1920. const float brightness = mask->getPixelAt (x, y).getBrightness();
  1921. if (brightness > 0.0f)
  1922. image->multiplyAlphaAt (x, y, 1.0f - brightness);
  1923. }
  1924. }
  1925. delete mask;
  1926. return image;
  1927. }
  1928. return 0;
  1929. }
  1930. static HICON createHICONFromImage (const Image& image, const BOOL isIcon, int hotspotX, int hotspotY) throw()
  1931. {
  1932. HBITMAP mask = CreateBitmap (image.getWidth(), image.getHeight(), 1, 1, 0);
  1933. ICONINFO info;
  1934. info.fIcon = isIcon;
  1935. info.xHotspot = hotspotX;
  1936. info.yHotspot = hotspotY;
  1937. info.hbmMask = mask;
  1938. HICON hi = 0;
  1939. if (SystemStats::getOperatingSystemType() >= SystemStats::WinXP)
  1940. {
  1941. WindowsBitmapImage bitmap (Image::ARGB, image.getWidth(), image.getHeight(), true);
  1942. Graphics g (bitmap);
  1943. g.drawImageAt (&image, 0, 0);
  1944. info.hbmColor = bitmap.hBitmap;
  1945. hi = CreateIconIndirect (&info);
  1946. }
  1947. else
  1948. {
  1949. HBITMAP colour = CreateCompatibleBitmap (GetDC (0), image.getWidth(), image.getHeight());
  1950. HDC colDC = CreateCompatibleDC (GetDC (0));
  1951. HDC alphaDC = CreateCompatibleDC (GetDC (0));
  1952. SelectObject (colDC, colour);
  1953. SelectObject (alphaDC, mask);
  1954. for (int y = image.getHeight(); --y >= 0;)
  1955. {
  1956. for (int x = image.getWidth(); --x >= 0;)
  1957. {
  1958. const Colour c (image.getPixelAt (x, y));
  1959. SetPixel (colDC, x, y, COLORREF (c.getRed() | (c.getGreen() << 8) | (c.getBlue() << 16)));
  1960. SetPixel (alphaDC, x, y, COLORREF (0xffffff - (c.getAlpha() | (c.getAlpha() << 8) | (c.getAlpha() << 16))));
  1961. }
  1962. }
  1963. DeleteDC (colDC);
  1964. DeleteDC (alphaDC);
  1965. info.hbmColor = colour;
  1966. hi = CreateIconIndirect (&info);
  1967. DeleteObject (colour);
  1968. }
  1969. DeleteObject (mask);
  1970. return hi;
  1971. }
  1972. Image* juce_createIconForFile (const File& file)
  1973. {
  1974. Image* image = 0;
  1975. TCHAR filename [1024];
  1976. file.getFullPathName().copyToBuffer (filename, 1023);
  1977. WORD iconNum = 0;
  1978. HICON icon = ExtractAssociatedIcon ((HINSTANCE) PlatformUtilities::getCurrentModuleInstanceHandle(),
  1979. filename, &iconNum);
  1980. if (icon != 0)
  1981. {
  1982. image = createImageFromHICON (icon);
  1983. DestroyIcon (icon);
  1984. }
  1985. return image;
  1986. }
  1987. //==============================================================================
  1988. void* juce_createMouseCursorFromImage (const Image& image, int hotspotX, int hotspotY) throw()
  1989. {
  1990. const int maxW = GetSystemMetrics (SM_CXCURSOR);
  1991. const int maxH = GetSystemMetrics (SM_CYCURSOR);
  1992. const Image* im = &image;
  1993. Image* newIm = 0;
  1994. if (image.getWidth() > maxW || image.getHeight() > maxH)
  1995. {
  1996. im = newIm = image.createCopy (maxW, maxH);
  1997. hotspotX = (hotspotX * maxW) / image.getWidth();
  1998. hotspotY = (hotspotY * maxH) / image.getHeight();
  1999. }
  2000. void* cursorH = 0;
  2001. const SystemStats::OperatingSystemType os = SystemStats::getOperatingSystemType();
  2002. if (os == SystemStats::WinXP)
  2003. {
  2004. cursorH = (void*) createHICONFromImage (*im, FALSE, hotspotX, hotspotY);
  2005. }
  2006. else
  2007. {
  2008. const int stride = (maxW + 7) >> 3;
  2009. uint8* const andPlane = (uint8*) juce_calloc (stride * maxH);
  2010. uint8* const xorPlane = (uint8*) juce_calloc (stride * maxH);
  2011. int index = 0;
  2012. for (int y = 0; y < maxH; ++y)
  2013. {
  2014. for (int x = 0; x < maxW; ++x)
  2015. {
  2016. const unsigned char bit = (unsigned char) (1 << (7 - (x & 7)));
  2017. const Colour pixelColour (im->getPixelAt (x, y));
  2018. if (pixelColour.getAlpha() < 127)
  2019. andPlane [index + (x >> 3)] |= bit;
  2020. else if (pixelColour.getBrightness() >= 0.5f)
  2021. xorPlane [index + (x >> 3)] |= bit;
  2022. }
  2023. index += stride;
  2024. }
  2025. cursorH = CreateCursor (0, hotspotX, hotspotY, maxW, maxH, andPlane, xorPlane);
  2026. juce_free (andPlane);
  2027. juce_free (xorPlane);
  2028. }
  2029. delete newIm;
  2030. return cursorH;
  2031. }
  2032. void juce_deleteMouseCursor (void* const cursorHandle, const bool isStandard) throw()
  2033. {
  2034. if (cursorHandle != 0 && ! isStandard)
  2035. DestroyCursor ((HCURSOR) cursorHandle);
  2036. }
  2037. void* juce_createStandardMouseCursor (MouseCursor::StandardCursorType type) throw()
  2038. {
  2039. LPCTSTR cursorName = IDC_ARROW;
  2040. switch (type)
  2041. {
  2042. case MouseCursor::NormalCursor:
  2043. cursorName = IDC_ARROW;
  2044. break;
  2045. case MouseCursor::NoCursor:
  2046. return 0;
  2047. case MouseCursor::DraggingHandCursor:
  2048. {
  2049. static void* dragHandCursor = 0;
  2050. if (dragHandCursor == 0)
  2051. {
  2052. static const unsigned char dragHandData[] =
  2053. { 71,73,70,56,57,97,16,0,16,0,145,2,0,0,0,0,255,255,255,0,0,0,0,0,0,33,249,4,1,0,0,2,0,44,0,0,0,0,16,0,
  2054. 16,0,0,2,52,148,47,0,200,185,16,130,90,12,74,139,107,84,123,39, 132,117,151,116,132,146,248,60,209,138,
  2055. 98,22,203,114,34,236,37,52,77,217,247,154,191,119,110,240,193,128,193,95,163,56,60,234,98,135,2,0,59 };
  2056. Image* const image = ImageFileFormat::loadFrom ((const char*) dragHandData, sizeof (dragHandData));
  2057. dragHandCursor = juce_createMouseCursorFromImage (*image, 8, 7);
  2058. delete image;
  2059. }
  2060. return dragHandCursor;
  2061. }
  2062. case MouseCursor::WaitCursor:
  2063. cursorName = IDC_WAIT;
  2064. break;
  2065. case MouseCursor::IBeamCursor:
  2066. cursorName = IDC_IBEAM;
  2067. break;
  2068. case MouseCursor::PointingHandCursor:
  2069. cursorName = MAKEINTRESOURCE(32649);
  2070. break;
  2071. case MouseCursor::LeftRightResizeCursor:
  2072. case MouseCursor::LeftEdgeResizeCursor:
  2073. case MouseCursor::RightEdgeResizeCursor:
  2074. cursorName = IDC_SIZEWE;
  2075. break;
  2076. case MouseCursor::UpDownResizeCursor:
  2077. case MouseCursor::TopEdgeResizeCursor:
  2078. case MouseCursor::BottomEdgeResizeCursor:
  2079. cursorName = IDC_SIZENS;
  2080. break;
  2081. case MouseCursor::TopLeftCornerResizeCursor:
  2082. case MouseCursor::BottomRightCornerResizeCursor:
  2083. cursorName = IDC_SIZENWSE;
  2084. break;
  2085. case MouseCursor::TopRightCornerResizeCursor:
  2086. case MouseCursor::BottomLeftCornerResizeCursor:
  2087. cursorName = IDC_SIZENESW;
  2088. break;
  2089. case MouseCursor::UpDownLeftRightResizeCursor:
  2090. cursorName = IDC_SIZEALL;
  2091. break;
  2092. case MouseCursor::CrosshairCursor:
  2093. cursorName = IDC_CROSS;
  2094. break;
  2095. case MouseCursor::CopyingCursor:
  2096. // can't seem to find one of these in the win32 list..
  2097. break;
  2098. }
  2099. HCURSOR cursorH = LoadCursor (0, cursorName);
  2100. if (cursorH == 0)
  2101. cursorH = LoadCursor (0, IDC_ARROW);
  2102. return (void*) cursorH;
  2103. }
  2104. //==============================================================================
  2105. void MouseCursor::showInWindow (ComponentPeer*) const throw()
  2106. {
  2107. SetCursor ((HCURSOR) getHandle());
  2108. }
  2109. void MouseCursor::showInAllWindows() const throw()
  2110. {
  2111. showInWindow (0);
  2112. }
  2113. //==============================================================================
  2114. //==============================================================================
  2115. class JuceDropSource : public IDropSource
  2116. {
  2117. int refCount;
  2118. public:
  2119. JuceDropSource()
  2120. : refCount (1)
  2121. {
  2122. }
  2123. virtual ~JuceDropSource()
  2124. {
  2125. jassert (refCount == 0);
  2126. }
  2127. HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
  2128. {
  2129. if (id == IID_IUnknown || id == IID_IDropSource)
  2130. {
  2131. AddRef();
  2132. *result = this;
  2133. return S_OK;
  2134. }
  2135. *result = 0;
  2136. return E_NOINTERFACE;
  2137. }
  2138. ULONG __stdcall AddRef() { return ++refCount; }
  2139. ULONG __stdcall Release() { jassert (refCount > 0); const int r = --refCount; if (r == 0) delete this; return r; }
  2140. HRESULT __stdcall QueryContinueDrag (BOOL escapePressed, DWORD keys)
  2141. {
  2142. if (escapePressed)
  2143. return DRAGDROP_S_CANCEL;
  2144. if ((keys & (MK_LBUTTON | MK_RBUTTON)) == 0)
  2145. return DRAGDROP_S_DROP;
  2146. return S_OK;
  2147. }
  2148. HRESULT __stdcall GiveFeedback (DWORD)
  2149. {
  2150. return DRAGDROP_S_USEDEFAULTCURSORS;
  2151. }
  2152. };
  2153. class JuceEnumFormatEtc : public IEnumFORMATETC
  2154. {
  2155. public:
  2156. JuceEnumFormatEtc (const FORMATETC* const format_)
  2157. : refCount (1),
  2158. format (format_),
  2159. index (0)
  2160. {
  2161. }
  2162. virtual ~JuceEnumFormatEtc()
  2163. {
  2164. jassert (refCount == 0);
  2165. }
  2166. HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
  2167. {
  2168. if (id == IID_IUnknown || id == IID_IEnumFORMATETC)
  2169. {
  2170. AddRef();
  2171. *result = this;
  2172. return S_OK;
  2173. }
  2174. *result = 0;
  2175. return E_NOINTERFACE;
  2176. }
  2177. ULONG __stdcall AddRef() { return ++refCount; }
  2178. ULONG __stdcall Release() { jassert (refCount > 0); const int r = --refCount; if (r == 0) delete this; return r; }
  2179. HRESULT __stdcall Clone (IEnumFORMATETC** result)
  2180. {
  2181. if (result == 0)
  2182. return E_POINTER;
  2183. JuceEnumFormatEtc* const newOne = new JuceEnumFormatEtc (format);
  2184. newOne->index = index;
  2185. *result = newOne;
  2186. return S_OK;
  2187. }
  2188. HRESULT __stdcall Next (ULONG celt, LPFORMATETC lpFormatEtc, ULONG* pceltFetched)
  2189. {
  2190. if (pceltFetched != 0)
  2191. *pceltFetched = 0;
  2192. else if (celt != 1)
  2193. return S_FALSE;
  2194. if (index == 0 && celt > 0 && lpFormatEtc != 0)
  2195. {
  2196. copyFormatEtc (lpFormatEtc [0], *format);
  2197. ++index;
  2198. if (pceltFetched != 0)
  2199. *pceltFetched = 1;
  2200. return S_OK;
  2201. }
  2202. return S_FALSE;
  2203. }
  2204. HRESULT __stdcall Skip (ULONG celt)
  2205. {
  2206. if (index + (int) celt >= 1)
  2207. return S_FALSE;
  2208. index += celt;
  2209. return S_OK;
  2210. }
  2211. HRESULT __stdcall Reset()
  2212. {
  2213. index = 0;
  2214. return S_OK;
  2215. }
  2216. private:
  2217. int refCount;
  2218. const FORMATETC* const format;
  2219. int index;
  2220. static void copyFormatEtc (FORMATETC& dest, const FORMATETC& source)
  2221. {
  2222. dest = source;
  2223. if (source.ptd != 0)
  2224. {
  2225. dest.ptd = (DVTARGETDEVICE*) CoTaskMemAlloc (sizeof (DVTARGETDEVICE));
  2226. *(dest.ptd) = *(source.ptd);
  2227. }
  2228. }
  2229. JuceEnumFormatEtc (const JuceEnumFormatEtc&);
  2230. const JuceEnumFormatEtc& operator= (const JuceEnumFormatEtc&);
  2231. };
  2232. class JuceDataObject : public IDataObject
  2233. {
  2234. JuceDropSource* const dropSource;
  2235. const FORMATETC* const format;
  2236. const STGMEDIUM* const medium;
  2237. int refCount;
  2238. JuceDataObject (const JuceDataObject&);
  2239. const JuceDataObject& operator= (const JuceDataObject&);
  2240. public:
  2241. JuceDataObject (JuceDropSource* const dropSource_,
  2242. const FORMATETC* const format_,
  2243. const STGMEDIUM* const medium_)
  2244. : dropSource (dropSource_),
  2245. format (format_),
  2246. medium (medium_),
  2247. refCount (1)
  2248. {
  2249. }
  2250. virtual ~JuceDataObject()
  2251. {
  2252. jassert (refCount == 0);
  2253. }
  2254. HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
  2255. {
  2256. if (id == IID_IUnknown || id == IID_IDataObject)
  2257. {
  2258. AddRef();
  2259. *result = this;
  2260. return S_OK;
  2261. }
  2262. *result = 0;
  2263. return E_NOINTERFACE;
  2264. }
  2265. ULONG __stdcall AddRef() { return ++refCount; }
  2266. ULONG __stdcall Release() { jassert (refCount > 0); const int r = --refCount; if (r == 0) delete this; return r; }
  2267. HRESULT __stdcall GetData (FORMATETC __RPC_FAR* pFormatEtc, STGMEDIUM __RPC_FAR* pMedium)
  2268. {
  2269. if (pFormatEtc->tymed == format->tymed
  2270. && pFormatEtc->cfFormat == format->cfFormat
  2271. && pFormatEtc->dwAspect == format->dwAspect)
  2272. {
  2273. pMedium->tymed = format->tymed;
  2274. pMedium->pUnkForRelease = 0;
  2275. if (format->tymed == TYMED_HGLOBAL)
  2276. {
  2277. const SIZE_T len = GlobalSize (medium->hGlobal);
  2278. void* const src = GlobalLock (medium->hGlobal);
  2279. void* const dst = GlobalAlloc (GMEM_FIXED, len);
  2280. memcpy (dst, src, len);
  2281. GlobalUnlock (medium->hGlobal);
  2282. pMedium->hGlobal = dst;
  2283. return S_OK;
  2284. }
  2285. }
  2286. return DV_E_FORMATETC;
  2287. }
  2288. HRESULT __stdcall QueryGetData (FORMATETC __RPC_FAR* f)
  2289. {
  2290. if (f == 0)
  2291. return E_INVALIDARG;
  2292. if (f->tymed == format->tymed
  2293. && f->cfFormat == format->cfFormat
  2294. && f->dwAspect == format->dwAspect)
  2295. return S_OK;
  2296. return DV_E_FORMATETC;
  2297. }
  2298. HRESULT __stdcall GetCanonicalFormatEtc (FORMATETC __RPC_FAR*, FORMATETC __RPC_FAR* pFormatEtcOut)
  2299. {
  2300. pFormatEtcOut->ptd = 0;
  2301. return E_NOTIMPL;
  2302. }
  2303. HRESULT __stdcall EnumFormatEtc (DWORD direction, IEnumFORMATETC __RPC_FAR *__RPC_FAR *result)
  2304. {
  2305. if (result == 0)
  2306. return E_POINTER;
  2307. if (direction == DATADIR_GET)
  2308. {
  2309. *result = new JuceEnumFormatEtc (format);
  2310. return S_OK;
  2311. }
  2312. *result = 0;
  2313. return E_NOTIMPL;
  2314. }
  2315. HRESULT __stdcall GetDataHere (FORMATETC __RPC_FAR*, STGMEDIUM __RPC_FAR*) { return DATA_E_FORMATETC; }
  2316. HRESULT __stdcall SetData (FORMATETC __RPC_FAR*, STGMEDIUM __RPC_FAR*, BOOL) { return E_NOTIMPL; }
  2317. HRESULT __stdcall DAdvise (FORMATETC __RPC_FAR*, DWORD, IAdviseSink __RPC_FAR*, DWORD __RPC_FAR*) { return OLE_E_ADVISENOTSUPPORTED; }
  2318. HRESULT __stdcall DUnadvise (DWORD) { return E_NOTIMPL; }
  2319. HRESULT __stdcall EnumDAdvise (IEnumSTATDATA __RPC_FAR *__RPC_FAR *) { return OLE_E_ADVISENOTSUPPORTED; }
  2320. };
  2321. static HDROP createHDrop (const StringArray& fileNames) throw()
  2322. {
  2323. int totalChars = 0;
  2324. for (int i = fileNames.size(); --i >= 0;)
  2325. totalChars += fileNames[i].length() + 1;
  2326. HDROP hDrop = (HDROP) GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT,
  2327. sizeof (DROPFILES)
  2328. + sizeof (WCHAR) * (totalChars + 2));
  2329. if (hDrop != 0)
  2330. {
  2331. LPDROPFILES pDropFiles = (LPDROPFILES) GlobalLock (hDrop);
  2332. pDropFiles->pFiles = sizeof (DROPFILES);
  2333. pDropFiles->fWide = true;
  2334. WCHAR* fname = (WCHAR*) (((char*) pDropFiles) + sizeof (DROPFILES));
  2335. for (int i = 0; i < fileNames.size(); ++i)
  2336. {
  2337. fileNames[i].copyToBuffer (fname, 2048);
  2338. fname += fileNames[i].length() + 1;
  2339. }
  2340. *fname = 0;
  2341. GlobalUnlock (hDrop);
  2342. }
  2343. return hDrop;
  2344. }
  2345. static bool performDragDrop (FORMATETC* const format, STGMEDIUM* const medium, const DWORD whatToDo) throw()
  2346. {
  2347. JuceDropSource* const source = new JuceDropSource();
  2348. JuceDataObject* const data = new JuceDataObject (source, format, medium);
  2349. DWORD effect;
  2350. const HRESULT res = DoDragDrop (data, source, whatToDo, &effect);
  2351. data->Release();
  2352. source->Release();
  2353. return res == DRAGDROP_S_DROP;
  2354. }
  2355. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool canMove)
  2356. {
  2357. FORMATETC format = { CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  2358. STGMEDIUM medium = { TYMED_HGLOBAL, { 0 }, 0 };
  2359. medium.hGlobal = createHDrop (files);
  2360. return performDragDrop (&format, &medium, canMove ? (DROPEFFECT_COPY | DROPEFFECT_MOVE)
  2361. : DROPEFFECT_COPY);
  2362. }
  2363. bool DragAndDropContainer::performExternalDragDropOfText (const String& text)
  2364. {
  2365. FORMATETC format = { CF_TEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  2366. STGMEDIUM medium = { TYMED_HGLOBAL, { 0 }, 0 };
  2367. const int numChars = text.length();
  2368. medium.hGlobal = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, (numChars + 2) * sizeof (WCHAR));
  2369. char* d = (char*) GlobalLock (medium.hGlobal);
  2370. text.copyToBuffer ((WCHAR*) d, numChars + 1);
  2371. format.cfFormat = CF_UNICODETEXT;
  2372. GlobalUnlock (medium.hGlobal);
  2373. return performDragDrop (&format, &medium, DROPEFFECT_COPY | DROPEFFECT_MOVE);
  2374. }
  2375. //==============================================================================
  2376. //==============================================================================
  2377. #if JUCE_OPENGL
  2378. #define WGL_EXT_FUNCTION_INIT(extType, extFunc) \
  2379. ((extFunc = (extType) wglGetProcAddress (#extFunc)) != 0)
  2380. typedef const char* (WINAPI* PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
  2381. typedef BOOL (WINAPI * PFNWGLGETPIXELFORMATATTRIBIVARBPROC) (HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues);
  2382. typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int* piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
  2383. typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
  2384. typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
  2385. #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
  2386. #define WGL_DRAW_TO_WINDOW_ARB 0x2001
  2387. #define WGL_ACCELERATION_ARB 0x2003
  2388. #define WGL_SWAP_METHOD_ARB 0x2007
  2389. #define WGL_SUPPORT_OPENGL_ARB 0x2010
  2390. #define WGL_PIXEL_TYPE_ARB 0x2013
  2391. #define WGL_DOUBLE_BUFFER_ARB 0x2011
  2392. #define WGL_COLOR_BITS_ARB 0x2014
  2393. #define WGL_RED_BITS_ARB 0x2015
  2394. #define WGL_GREEN_BITS_ARB 0x2017
  2395. #define WGL_BLUE_BITS_ARB 0x2019
  2396. #define WGL_ALPHA_BITS_ARB 0x201B
  2397. #define WGL_DEPTH_BITS_ARB 0x2022
  2398. #define WGL_STENCIL_BITS_ARB 0x2023
  2399. #define WGL_FULL_ACCELERATION_ARB 0x2027
  2400. #define WGL_ACCUM_RED_BITS_ARB 0x201E
  2401. #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
  2402. #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
  2403. #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
  2404. #define WGL_STEREO_ARB 0x2012
  2405. #define WGL_SAMPLE_BUFFERS_ARB 0x2041
  2406. #define WGL_SAMPLES_ARB 0x2042
  2407. #define WGL_TYPE_RGBA_ARB 0x202B
  2408. static void getWglExtensions (HDC dc, StringArray& result) throw()
  2409. {
  2410. PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = 0;
  2411. if (WGL_EXT_FUNCTION_INIT (PFNWGLGETEXTENSIONSSTRINGARBPROC, wglGetExtensionsStringARB))
  2412. result.addTokens (String (wglGetExtensionsStringARB (dc)), false);
  2413. else
  2414. jassertfalse // If this fails, it may be because you didn't activate the openGL context
  2415. }
  2416. //==============================================================================
  2417. class WindowedGLContext : public OpenGLContext
  2418. {
  2419. public:
  2420. WindowedGLContext (Component* const component_,
  2421. HGLRC contextToShareWith,
  2422. const OpenGLPixelFormat& pixelFormat)
  2423. : renderContext (0),
  2424. nativeWindow (0),
  2425. dc (0),
  2426. component (component_)
  2427. {
  2428. jassert (component != 0);
  2429. createNativeWindow();
  2430. // Use a default pixel format that should be supported everywhere
  2431. PIXELFORMATDESCRIPTOR pfd;
  2432. zerostruct (pfd);
  2433. pfd.nSize = sizeof (pfd);
  2434. pfd.nVersion = 1;
  2435. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  2436. pfd.iPixelType = PFD_TYPE_RGBA;
  2437. pfd.cColorBits = 24;
  2438. pfd.cDepthBits = 16;
  2439. const int format = ChoosePixelFormat (dc, &pfd);
  2440. if (format != 0)
  2441. SetPixelFormat (dc, format, &pfd);
  2442. renderContext = wglCreateContext (dc);
  2443. makeActive();
  2444. setPixelFormat (pixelFormat);
  2445. if (contextToShareWith != 0 && renderContext != 0)
  2446. wglShareLists (contextToShareWith, renderContext);
  2447. }
  2448. ~WindowedGLContext()
  2449. {
  2450. makeInactive();
  2451. wglDeleteContext (renderContext);
  2452. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  2453. delete nativeWindow;
  2454. }
  2455. bool makeActive() const throw()
  2456. {
  2457. jassert (renderContext != 0);
  2458. return wglMakeCurrent (dc, renderContext) != 0;
  2459. }
  2460. bool makeInactive() const throw()
  2461. {
  2462. return (! isActive()) || (wglMakeCurrent (0, 0) != 0);
  2463. }
  2464. bool isActive() const throw()
  2465. {
  2466. return wglGetCurrentContext() == renderContext;
  2467. }
  2468. const OpenGLPixelFormat getPixelFormat() const
  2469. {
  2470. OpenGLPixelFormat pf;
  2471. makeActive();
  2472. StringArray availableExtensions;
  2473. getWglExtensions (dc, availableExtensions);
  2474. fillInPixelFormatDetails (GetPixelFormat (dc), pf, availableExtensions);
  2475. return pf;
  2476. }
  2477. void* getRawContext() const throw()
  2478. {
  2479. return renderContext;
  2480. }
  2481. bool setPixelFormat (const OpenGLPixelFormat& pixelFormat)
  2482. {
  2483. makeActive();
  2484. PIXELFORMATDESCRIPTOR pfd;
  2485. zerostruct (pfd);
  2486. pfd.nSize = sizeof (pfd);
  2487. pfd.nVersion = 1;
  2488. pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
  2489. pfd.iPixelType = PFD_TYPE_RGBA;
  2490. pfd.iLayerType = PFD_MAIN_PLANE;
  2491. pfd.cColorBits = pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits;
  2492. pfd.cRedBits = pixelFormat.redBits;
  2493. pfd.cGreenBits = pixelFormat.greenBits;
  2494. pfd.cBlueBits = pixelFormat.blueBits;
  2495. pfd.cAlphaBits = pixelFormat.alphaBits;
  2496. pfd.cDepthBits = pixelFormat.depthBufferBits;
  2497. pfd.cStencilBits = pixelFormat.stencilBufferBits;
  2498. pfd.cAccumBits = pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
  2499. + pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits;
  2500. pfd.cAccumRedBits = pixelFormat.accumulationBufferRedBits;
  2501. pfd.cAccumGreenBits = pixelFormat.accumulationBufferGreenBits;
  2502. pfd.cAccumBlueBits = pixelFormat.accumulationBufferBlueBits;
  2503. pfd.cAccumAlphaBits = pixelFormat.accumulationBufferAlphaBits;
  2504. int format = 0;
  2505. PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = 0;
  2506. StringArray availableExtensions;
  2507. getWglExtensions (dc, availableExtensions);
  2508. if (availableExtensions.contains ("WGL_ARB_pixel_format")
  2509. && WGL_EXT_FUNCTION_INIT (PFNWGLCHOOSEPIXELFORMATARBPROC, wglChoosePixelFormatARB))
  2510. {
  2511. int attributes[64];
  2512. int n = 0;
  2513. attributes[n++] = WGL_DRAW_TO_WINDOW_ARB;
  2514. attributes[n++] = GL_TRUE;
  2515. attributes[n++] = WGL_SUPPORT_OPENGL_ARB;
  2516. attributes[n++] = GL_TRUE;
  2517. attributes[n++] = WGL_ACCELERATION_ARB;
  2518. attributes[n++] = WGL_FULL_ACCELERATION_ARB;
  2519. attributes[n++] = WGL_DOUBLE_BUFFER_ARB;
  2520. attributes[n++] = GL_TRUE;
  2521. attributes[n++] = WGL_PIXEL_TYPE_ARB;
  2522. attributes[n++] = WGL_TYPE_RGBA_ARB;
  2523. attributes[n++] = WGL_COLOR_BITS_ARB;
  2524. attributes[n++] = pfd.cColorBits;
  2525. attributes[n++] = WGL_RED_BITS_ARB;
  2526. attributes[n++] = pixelFormat.redBits;
  2527. attributes[n++] = WGL_GREEN_BITS_ARB;
  2528. attributes[n++] = pixelFormat.greenBits;
  2529. attributes[n++] = WGL_BLUE_BITS_ARB;
  2530. attributes[n++] = pixelFormat.blueBits;
  2531. attributes[n++] = WGL_ALPHA_BITS_ARB;
  2532. attributes[n++] = pixelFormat.alphaBits;
  2533. attributes[n++] = WGL_DEPTH_BITS_ARB;
  2534. attributes[n++] = pixelFormat.depthBufferBits;
  2535. if (pixelFormat.stencilBufferBits > 0)
  2536. {
  2537. attributes[n++] = WGL_STENCIL_BITS_ARB;
  2538. attributes[n++] = pixelFormat.stencilBufferBits;
  2539. }
  2540. attributes[n++] = WGL_ACCUM_RED_BITS_ARB;
  2541. attributes[n++] = pixelFormat.accumulationBufferRedBits;
  2542. attributes[n++] = WGL_ACCUM_GREEN_BITS_ARB;
  2543. attributes[n++] = pixelFormat.accumulationBufferGreenBits;
  2544. attributes[n++] = WGL_ACCUM_BLUE_BITS_ARB;
  2545. attributes[n++] = pixelFormat.accumulationBufferBlueBits;
  2546. attributes[n++] = WGL_ACCUM_ALPHA_BITS_ARB;
  2547. attributes[n++] = pixelFormat.accumulationBufferAlphaBits;
  2548. if (availableExtensions.contains ("WGL_ARB_multisample")
  2549. && pixelFormat.fullSceneAntiAliasingNumSamples > 0)
  2550. {
  2551. attributes[n++] = WGL_SAMPLE_BUFFERS_ARB;
  2552. attributes[n++] = 1;
  2553. attributes[n++] = WGL_SAMPLES_ARB;
  2554. attributes[n++] = pixelFormat.fullSceneAntiAliasingNumSamples;
  2555. }
  2556. attributes[n++] = 0;
  2557. UINT formatsCount;
  2558. const BOOL ok = wglChoosePixelFormatARB (dc, attributes, 0, 1, &format, &formatsCount);
  2559. (void) ok;
  2560. jassert (ok);
  2561. }
  2562. else
  2563. {
  2564. format = ChoosePixelFormat (dc, &pfd);
  2565. }
  2566. if (format != 0)
  2567. {
  2568. makeInactive();
  2569. // win32 can't change the pixel format of a window, so need to delete the
  2570. // old one and create a new one..
  2571. jassert (nativeWindow != 0);
  2572. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  2573. delete nativeWindow;
  2574. createNativeWindow();
  2575. if (SetPixelFormat (dc, format, &pfd))
  2576. {
  2577. wglDeleteContext (renderContext);
  2578. renderContext = wglCreateContext (dc);
  2579. jassert (renderContext != 0);
  2580. return renderContext != 0;
  2581. }
  2582. }
  2583. return false;
  2584. }
  2585. void updateWindowPosition (int x, int y, int w, int h, int)
  2586. {
  2587. SetWindowPos ((HWND) nativeWindow->getNativeHandle(), 0,
  2588. x, y, w, h,
  2589. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  2590. }
  2591. void repaint()
  2592. {
  2593. int x, y, w, h;
  2594. nativeWindow->getBounds (x, y, w, h);
  2595. nativeWindow->repaint (0, 0, w, h);
  2596. }
  2597. void swapBuffers()
  2598. {
  2599. SwapBuffers (dc);
  2600. }
  2601. bool setSwapInterval (const int numFramesPerSwap)
  2602. {
  2603. makeActive();
  2604. StringArray availableExtensions;
  2605. getWglExtensions (dc, availableExtensions);
  2606. PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = 0;
  2607. return availableExtensions.contains ("WGL_EXT_swap_control")
  2608. && WGL_EXT_FUNCTION_INIT (PFNWGLSWAPINTERVALEXTPROC, wglSwapIntervalEXT)
  2609. && wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
  2610. }
  2611. int getSwapInterval() const
  2612. {
  2613. makeActive();
  2614. StringArray availableExtensions;
  2615. getWglExtensions (dc, availableExtensions);
  2616. PFNWGLGETSWAPINTERVALEXTPROC wglGetSwapIntervalEXT = 0;
  2617. if (availableExtensions.contains ("WGL_EXT_swap_control")
  2618. && WGL_EXT_FUNCTION_INIT (PFNWGLGETSWAPINTERVALEXTPROC, wglGetSwapIntervalEXT))
  2619. return wglGetSwapIntervalEXT();
  2620. return 0;
  2621. }
  2622. void findAlternativeOpenGLPixelFormats (OwnedArray <OpenGLPixelFormat>& results)
  2623. {
  2624. jassert (isActive());
  2625. StringArray availableExtensions;
  2626. getWglExtensions (dc, availableExtensions);
  2627. PFNWGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB = 0;
  2628. int numTypes = 0;
  2629. if (availableExtensions.contains("WGL_ARB_pixel_format")
  2630. && WGL_EXT_FUNCTION_INIT (PFNWGLGETPIXELFORMATATTRIBIVARBPROC, wglGetPixelFormatAttribivARB))
  2631. {
  2632. int attributes = WGL_NUMBER_PIXEL_FORMATS_ARB;
  2633. if (! wglGetPixelFormatAttribivARB (dc, 1, 0, 1, &attributes, &numTypes))
  2634. jassertfalse
  2635. }
  2636. else
  2637. {
  2638. numTypes = DescribePixelFormat (dc, 0, 0, 0);
  2639. }
  2640. OpenGLPixelFormat pf;
  2641. for (int i = 0; i < numTypes; ++i)
  2642. {
  2643. if (fillInPixelFormatDetails (i + 1, pf, availableExtensions))
  2644. {
  2645. bool alreadyListed = false;
  2646. for (int j = results.size(); --j >= 0;)
  2647. if (pf == *results.getUnchecked(j))
  2648. alreadyListed = true;
  2649. if (! alreadyListed)
  2650. results.add (new OpenGLPixelFormat (pf));
  2651. }
  2652. }
  2653. }
  2654. //==============================================================================
  2655. juce_UseDebuggingNewOperator
  2656. HGLRC renderContext;
  2657. private:
  2658. Win32ComponentPeer* nativeWindow;
  2659. Component* const component;
  2660. HDC dc;
  2661. //==============================================================================
  2662. void createNativeWindow()
  2663. {
  2664. nativeWindow = new Win32ComponentPeer (component, 0);
  2665. nativeWindow->dontRepaint = true;
  2666. nativeWindow->setVisible (true);
  2667. HWND hwnd = (HWND) nativeWindow->getNativeHandle();
  2668. Win32ComponentPeer* const peer = dynamic_cast <Win32ComponentPeer*> (component->getTopLevelComponent()->getPeer());
  2669. if (peer != 0)
  2670. {
  2671. SetParent (hwnd, (HWND) peer->getNativeHandle());
  2672. juce_setWindowStyleBit (hwnd, GWL_STYLE, WS_CHILD, true);
  2673. juce_setWindowStyleBit (hwnd, GWL_STYLE, WS_POPUP, false);
  2674. }
  2675. dc = GetDC (hwnd);
  2676. }
  2677. bool fillInPixelFormatDetails (const int pixelFormatIndex,
  2678. OpenGLPixelFormat& result,
  2679. const StringArray& availableExtensions) const throw()
  2680. {
  2681. PFNWGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB = 0;
  2682. if (availableExtensions.contains ("WGL_ARB_pixel_format")
  2683. && WGL_EXT_FUNCTION_INIT (PFNWGLGETPIXELFORMATATTRIBIVARBPROC, wglGetPixelFormatAttribivARB))
  2684. {
  2685. int attributes[32];
  2686. int numAttributes = 0;
  2687. attributes[numAttributes++] = WGL_DRAW_TO_WINDOW_ARB;
  2688. attributes[numAttributes++] = WGL_SUPPORT_OPENGL_ARB;
  2689. attributes[numAttributes++] = WGL_ACCELERATION_ARB;
  2690. attributes[numAttributes++] = WGL_DOUBLE_BUFFER_ARB;
  2691. attributes[numAttributes++] = WGL_PIXEL_TYPE_ARB;
  2692. attributes[numAttributes++] = WGL_RED_BITS_ARB;
  2693. attributes[numAttributes++] = WGL_GREEN_BITS_ARB;
  2694. attributes[numAttributes++] = WGL_BLUE_BITS_ARB;
  2695. attributes[numAttributes++] = WGL_ALPHA_BITS_ARB;
  2696. attributes[numAttributes++] = WGL_DEPTH_BITS_ARB;
  2697. attributes[numAttributes++] = WGL_STENCIL_BITS_ARB;
  2698. attributes[numAttributes++] = WGL_ACCUM_RED_BITS_ARB;
  2699. attributes[numAttributes++] = WGL_ACCUM_GREEN_BITS_ARB;
  2700. attributes[numAttributes++] = WGL_ACCUM_BLUE_BITS_ARB;
  2701. attributes[numAttributes++] = WGL_ACCUM_ALPHA_BITS_ARB;
  2702. if (availableExtensions.contains ("WGL_ARB_multisample"))
  2703. attributes[numAttributes++] = WGL_SAMPLES_ARB;
  2704. int values[32];
  2705. zeromem (values, sizeof (values));
  2706. if (wglGetPixelFormatAttribivARB (dc, pixelFormatIndex, 0, numAttributes, attributes, values))
  2707. {
  2708. int n = 0;
  2709. bool isValidFormat = (values[n++] == GL_TRUE); // WGL_DRAW_TO_WINDOW_ARB
  2710. isValidFormat = (values[n++] == GL_TRUE) && isValidFormat; // WGL_SUPPORT_OPENGL_ARB
  2711. isValidFormat = (values[n++] == WGL_FULL_ACCELERATION_ARB) && isValidFormat; // WGL_ACCELERATION_ARB
  2712. isValidFormat = (values[n++] == GL_TRUE) && isValidFormat; // WGL_DOUBLE_BUFFER_ARB:
  2713. isValidFormat = (values[n++] == WGL_TYPE_RGBA_ARB) && isValidFormat; // WGL_PIXEL_TYPE_ARB
  2714. result.redBits = values[n++]; // WGL_RED_BITS_ARB
  2715. result.greenBits = values[n++]; // WGL_GREEN_BITS_ARB
  2716. result.blueBits = values[n++]; // WGL_BLUE_BITS_ARB
  2717. result.alphaBits = values[n++]; // WGL_ALPHA_BITS_ARB
  2718. result.depthBufferBits = values[n++]; // WGL_DEPTH_BITS_ARB
  2719. result.stencilBufferBits = values[n++]; // WGL_STENCIL_BITS_ARB
  2720. result.accumulationBufferRedBits = values[n++]; // WGL_ACCUM_RED_BITS_ARB
  2721. result.accumulationBufferGreenBits = values[n++]; // WGL_ACCUM_GREEN_BITS_ARB
  2722. result.accumulationBufferBlueBits = values[n++]; // WGL_ACCUM_BLUE_BITS_ARB
  2723. result.accumulationBufferAlphaBits = values[n++]; // WGL_ACCUM_ALPHA_BITS_ARB
  2724. result.fullSceneAntiAliasingNumSamples = values[n++]; // WGL_SAMPLES_ARB
  2725. return isValidFormat;
  2726. }
  2727. else
  2728. {
  2729. jassertfalse
  2730. }
  2731. }
  2732. else
  2733. {
  2734. PIXELFORMATDESCRIPTOR pfd;
  2735. if (DescribePixelFormat (dc, pixelFormatIndex, sizeof (pfd), &pfd))
  2736. {
  2737. result.redBits = pfd.cRedBits;
  2738. result.greenBits = pfd.cGreenBits;
  2739. result.blueBits = pfd.cBlueBits;
  2740. result.alphaBits = pfd.cAlphaBits;
  2741. result.depthBufferBits = pfd.cDepthBits;
  2742. result.stencilBufferBits = pfd.cStencilBits;
  2743. result.accumulationBufferRedBits = pfd.cAccumRedBits;
  2744. result.accumulationBufferGreenBits = pfd.cAccumGreenBits;
  2745. result.accumulationBufferBlueBits = pfd.cAccumBlueBits;
  2746. result.accumulationBufferAlphaBits = pfd.cAccumAlphaBits;
  2747. result.fullSceneAntiAliasingNumSamples = 0;
  2748. return true;
  2749. }
  2750. else
  2751. {
  2752. jassertfalse
  2753. }
  2754. }
  2755. return false;
  2756. }
  2757. WindowedGLContext (const WindowedGLContext&);
  2758. const WindowedGLContext& operator= (const WindowedGLContext&);
  2759. };
  2760. //==============================================================================
  2761. OpenGLContext* OpenGLContext::createContextForWindow (Component* const component,
  2762. const OpenGLPixelFormat& pixelFormat,
  2763. const OpenGLContext* const contextToShareWith)
  2764. {
  2765. WindowedGLContext* c = new WindowedGLContext (component,
  2766. contextToShareWith != 0 ? (HGLRC) contextToShareWith->getRawContext() : 0,
  2767. pixelFormat);
  2768. if (c->renderContext == 0)
  2769. deleteAndZero (c);
  2770. return c;
  2771. }
  2772. void juce_glViewport (const int w, const int h)
  2773. {
  2774. glViewport (0, 0, w, h);
  2775. }
  2776. void OpenGLPixelFormat::getAvailablePixelFormats (Component* component,
  2777. OwnedArray <OpenGLPixelFormat>& results)
  2778. {
  2779. Component tempComp;
  2780. {
  2781. WindowedGLContext wc (component, 0, OpenGLPixelFormat (8, 8, 16, 0));
  2782. wc.makeActive();
  2783. wc.findAlternativeOpenGLPixelFormats (results);
  2784. }
  2785. }
  2786. #endif
  2787. //==============================================================================
  2788. //==============================================================================
  2789. class JuceIStorage : public IStorage
  2790. {
  2791. int refCount;
  2792. public:
  2793. JuceIStorage() : refCount (1) {}
  2794. virtual ~JuceIStorage()
  2795. {
  2796. jassert (refCount == 0);
  2797. }
  2798. HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
  2799. {
  2800. if (id == IID_IUnknown || id == IID_IStorage)
  2801. {
  2802. AddRef();
  2803. *result = this;
  2804. return S_OK;
  2805. }
  2806. *result = 0;
  2807. return E_NOINTERFACE;
  2808. }
  2809. ULONG __stdcall AddRef() { return ++refCount; }
  2810. ULONG __stdcall Release() { const int r = --refCount; if (r == 0) delete this; return r; }
  2811. HRESULT __stdcall CreateStream (const WCHAR*, DWORD, DWORD, DWORD, IStream**) { return E_NOTIMPL; }
  2812. HRESULT __stdcall OpenStream (const WCHAR*, void*, DWORD, DWORD, IStream**) { return E_NOTIMPL; }
  2813. HRESULT __stdcall CreateStorage (const WCHAR*, DWORD, DWORD, DWORD, IStorage**) { return E_NOTIMPL; }
  2814. HRESULT __stdcall OpenStorage (const WCHAR*, IStorage*, DWORD, SNB, DWORD, IStorage**) { return E_NOTIMPL; }
  2815. HRESULT __stdcall CopyTo (DWORD, IID const*, SNB, IStorage*) { return E_NOTIMPL; }
  2816. HRESULT __stdcall MoveElementTo (const OLECHAR*,IStorage*, const OLECHAR*, DWORD) { return E_NOTIMPL; }
  2817. HRESULT __stdcall Commit (DWORD) { return E_NOTIMPL; }
  2818. HRESULT __stdcall Revert() { return E_NOTIMPL; }
  2819. HRESULT __stdcall EnumElements (DWORD, void*, DWORD, IEnumSTATSTG**) { return E_NOTIMPL; }
  2820. HRESULT __stdcall DestroyElement (const OLECHAR*) { return E_NOTIMPL; }
  2821. HRESULT __stdcall RenameElement (const WCHAR*, const WCHAR*) { return E_NOTIMPL; }
  2822. HRESULT __stdcall SetElementTimes (const WCHAR*, FILETIME const*, FILETIME const*, FILETIME const*) { return E_NOTIMPL; }
  2823. HRESULT __stdcall SetClass (REFCLSID) { return S_OK; }
  2824. HRESULT __stdcall SetStateBits (DWORD, DWORD) { return E_NOTIMPL; }
  2825. HRESULT __stdcall Stat (STATSTG*, DWORD) { return E_NOTIMPL; }
  2826. juce_UseDebuggingNewOperator
  2827. };
  2828. class JuceOleInPlaceFrame : public IOleInPlaceFrame
  2829. {
  2830. int refCount;
  2831. HWND window;
  2832. public:
  2833. JuceOleInPlaceFrame (HWND window_)
  2834. : refCount (1),
  2835. window (window_)
  2836. {
  2837. }
  2838. virtual ~JuceOleInPlaceFrame()
  2839. {
  2840. jassert (refCount == 0);
  2841. }
  2842. HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
  2843. {
  2844. if (id == IID_IUnknown || id == IID_IOleInPlaceFrame)
  2845. {
  2846. AddRef();
  2847. *result = this;
  2848. return S_OK;
  2849. }
  2850. *result = 0;
  2851. return E_NOINTERFACE;
  2852. }
  2853. ULONG __stdcall AddRef() { return ++refCount; }
  2854. ULONG __stdcall Release() { const int r = --refCount; if (r == 0) delete this; return r; }
  2855. HRESULT __stdcall GetWindow (HWND* lphwnd) { *lphwnd = window; return S_OK; }
  2856. HRESULT __stdcall ContextSensitiveHelp (BOOL) { return E_NOTIMPL; }
  2857. HRESULT __stdcall GetBorder (LPRECT) { return E_NOTIMPL; }
  2858. HRESULT __stdcall RequestBorderSpace (LPCBORDERWIDTHS) { return E_NOTIMPL; }
  2859. HRESULT __stdcall SetBorderSpace (LPCBORDERWIDTHS) { return E_NOTIMPL; }
  2860. HRESULT __stdcall SetActiveObject (IOleInPlaceActiveObject*, LPCOLESTR) { return S_OK; }
  2861. HRESULT __stdcall InsertMenus (HMENU, LPOLEMENUGROUPWIDTHS) { return E_NOTIMPL; }
  2862. HRESULT __stdcall SetMenu (HMENU, HOLEMENU, HWND) { return S_OK; }
  2863. HRESULT __stdcall RemoveMenus (HMENU) { return E_NOTIMPL; }
  2864. HRESULT __stdcall SetStatusText (LPCOLESTR) { return S_OK; }
  2865. HRESULT __stdcall EnableModeless (BOOL) { return S_OK; }
  2866. HRESULT __stdcall TranslateAccelerator(LPMSG, WORD) { return E_NOTIMPL; }
  2867. juce_UseDebuggingNewOperator
  2868. };
  2869. class JuceIOleInPlaceSite : public IOleInPlaceSite
  2870. {
  2871. int refCount;
  2872. HWND window;
  2873. JuceOleInPlaceFrame* frame;
  2874. public:
  2875. JuceIOleInPlaceSite (HWND window_)
  2876. : refCount (1),
  2877. window (window_)
  2878. {
  2879. frame = new JuceOleInPlaceFrame (window);
  2880. }
  2881. virtual ~JuceIOleInPlaceSite()
  2882. {
  2883. jassert (refCount == 0);
  2884. frame->Release();
  2885. }
  2886. HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
  2887. {
  2888. if (id == IID_IUnknown || id == IID_IOleInPlaceSite)
  2889. {
  2890. AddRef();
  2891. *result = this;
  2892. return S_OK;
  2893. }
  2894. *result = 0;
  2895. return E_NOINTERFACE;
  2896. }
  2897. ULONG __stdcall AddRef() { return ++refCount; }
  2898. ULONG __stdcall Release() { const int r = --refCount; if (r == 0) delete this; return r; }
  2899. HRESULT __stdcall GetWindow (HWND* lphwnd) { *lphwnd = window; return S_OK; }
  2900. HRESULT __stdcall ContextSensitiveHelp (BOOL) { return E_NOTIMPL; }
  2901. HRESULT __stdcall CanInPlaceActivate() { return S_OK; }
  2902. HRESULT __stdcall OnInPlaceActivate() { return S_OK; }
  2903. HRESULT __stdcall OnUIActivate() { return S_OK; }
  2904. HRESULT __stdcall GetWindowContext (LPOLEINPLACEFRAME* lplpFrame, LPOLEINPLACEUIWINDOW* lplpDoc, LPRECT, LPRECT, LPOLEINPLACEFRAMEINFO lpFrameInfo)
  2905. {
  2906. frame->AddRef();
  2907. *lplpFrame = frame;
  2908. *lplpDoc = 0;
  2909. lpFrameInfo->fMDIApp = FALSE;
  2910. lpFrameInfo->hwndFrame = window;
  2911. lpFrameInfo->haccel = 0;
  2912. lpFrameInfo->cAccelEntries = 0;
  2913. return S_OK;
  2914. }
  2915. HRESULT __stdcall Scroll (SIZE) { return E_NOTIMPL; }
  2916. HRESULT __stdcall OnUIDeactivate (BOOL) { return S_OK; }
  2917. HRESULT __stdcall OnInPlaceDeactivate() { return S_OK; }
  2918. HRESULT __stdcall DiscardUndoState() { return E_NOTIMPL; }
  2919. HRESULT __stdcall DeactivateAndUndo() { return E_NOTIMPL; }
  2920. HRESULT __stdcall OnPosRectChange (LPCRECT) { return S_OK; }
  2921. juce_UseDebuggingNewOperator
  2922. };
  2923. class JuceIOleClientSite : public IOleClientSite
  2924. {
  2925. int refCount;
  2926. JuceIOleInPlaceSite* inplaceSite;
  2927. public:
  2928. JuceIOleClientSite (HWND window)
  2929. : refCount (1)
  2930. {
  2931. inplaceSite = new JuceIOleInPlaceSite (window);
  2932. }
  2933. virtual ~JuceIOleClientSite()
  2934. {
  2935. jassert (refCount == 0);
  2936. inplaceSite->Release();
  2937. }
  2938. HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
  2939. {
  2940. if (id == IID_IUnknown || id == IID_IOleClientSite)
  2941. {
  2942. AddRef();
  2943. *result = this;
  2944. return S_OK;
  2945. }
  2946. else if (id == IID_IOleInPlaceSite)
  2947. {
  2948. inplaceSite->AddRef();
  2949. *result = inplaceSite;
  2950. return S_OK;
  2951. }
  2952. *result = 0;
  2953. return E_NOINTERFACE;
  2954. }
  2955. ULONG __stdcall AddRef() { return ++refCount; }
  2956. ULONG __stdcall Release() { const int r = --refCount; if (r == 0) delete this; return r; }
  2957. HRESULT __stdcall SaveObject() { return E_NOTIMPL; }
  2958. HRESULT __stdcall GetMoniker (DWORD, DWORD, IMoniker**) { return E_NOTIMPL; }
  2959. HRESULT __stdcall GetContainer (LPOLECONTAINER* ppContainer) { *ppContainer = 0; return E_NOINTERFACE; }
  2960. HRESULT __stdcall ShowObject() { return S_OK; }
  2961. HRESULT __stdcall OnShowWindow (BOOL) { return E_NOTIMPL; }
  2962. HRESULT __stdcall RequestNewObjectLayout() { return E_NOTIMPL; }
  2963. juce_UseDebuggingNewOperator
  2964. };
  2965. //==============================================================================
  2966. class ActiveXControlData : public ComponentMovementWatcher
  2967. {
  2968. ActiveXControlComponent* const owner;
  2969. bool wasShowing;
  2970. public:
  2971. IStorage* storage;
  2972. IOleClientSite* clientSite;
  2973. IOleObject* control;
  2974. //==============================================================================
  2975. ActiveXControlData (HWND hwnd,
  2976. ActiveXControlComponent* const owner_)
  2977. : ComponentMovementWatcher (owner_),
  2978. owner (owner_),
  2979. wasShowing (owner_ != 0 && owner_->isShowing()),
  2980. storage (new JuceIStorage()),
  2981. clientSite (new JuceIOleClientSite (hwnd)),
  2982. control (0)
  2983. {
  2984. }
  2985. ~ActiveXControlData()
  2986. {
  2987. if (control != 0)
  2988. {
  2989. control->Close (OLECLOSE_NOSAVE);
  2990. control->Release();
  2991. }
  2992. clientSite->Release();
  2993. storage->Release();
  2994. }
  2995. //==============================================================================
  2996. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/)
  2997. {
  2998. Component* const topComp = owner->getTopLevelComponent();
  2999. if (topComp->getPeer() != 0)
  3000. {
  3001. int x = 0, y = 0;
  3002. owner->relativePositionToOtherComponent (topComp, x, y);
  3003. owner->setControlBounds (Rectangle (x, y, owner->getWidth(), owner->getHeight()));
  3004. }
  3005. }
  3006. void componentPeerChanged()
  3007. {
  3008. const bool isShowingNow = owner->isShowing();
  3009. if (wasShowing != isShowingNow)
  3010. {
  3011. wasShowing = isShowingNow;
  3012. owner->setControlVisible (isShowingNow);
  3013. }
  3014. }
  3015. void componentVisibilityChanged (Component&)
  3016. {
  3017. componentPeerChanged();
  3018. }
  3019. };
  3020. //==============================================================================
  3021. static VoidArray activeXComps;
  3022. static HWND getHWND (const ActiveXControlComponent* const component)
  3023. {
  3024. HWND hwnd = 0;
  3025. const IID iid = IID_IOleWindow;
  3026. IOleWindow* const window = (IOleWindow*) component->queryInterface (&iid);
  3027. if (window != 0)
  3028. {
  3029. window->GetWindow (&hwnd);
  3030. window->Release();
  3031. }
  3032. return hwnd;
  3033. }
  3034. static void offerActiveXMouseEventToPeer (ComponentPeer* const peer, HWND hwnd, UINT message, LPARAM lParam)
  3035. {
  3036. RECT activeXRect, peerRect;
  3037. GetWindowRect (hwnd, &activeXRect);
  3038. GetWindowRect ((HWND) peer->getNativeHandle(), &peerRect);
  3039. const int mx = GET_X_LPARAM (lParam) + activeXRect.left - peerRect.left;
  3040. const int my = GET_Y_LPARAM (lParam) + activeXRect.top - peerRect.top;
  3041. const int64 mouseEventTime = getMouseEventTime();
  3042. const int oldModifiers = currentModifiers;
  3043. ModifierKeys::getCurrentModifiersRealtime(); // to update the mouse button flags
  3044. switch (message)
  3045. {
  3046. case WM_MOUSEMOVE:
  3047. if (ModifierKeys (currentModifiers).isAnyMouseButtonDown())
  3048. peer->handleMouseDrag (mx, my, mouseEventTime);
  3049. else
  3050. peer->handleMouseMove (mx, my, mouseEventTime);
  3051. break;
  3052. case WM_LBUTTONDOWN:
  3053. case WM_MBUTTONDOWN:
  3054. case WM_RBUTTONDOWN:
  3055. peer->handleMouseDown (mx, my, mouseEventTime);
  3056. break;
  3057. case WM_LBUTTONUP:
  3058. case WM_MBUTTONUP:
  3059. case WM_RBUTTONUP:
  3060. peer->handleMouseUp (oldModifiers, mx, my, mouseEventTime);
  3061. break;
  3062. default:
  3063. break;
  3064. }
  3065. }
  3066. // intercepts events going to an activeX control, so we can sneakily use the mouse events
  3067. static LRESULT CALLBACK activeXHookWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  3068. {
  3069. for (int i = activeXComps.size(); --i >= 0;)
  3070. {
  3071. const ActiveXControlComponent* const ax = (const ActiveXControlComponent*) activeXComps.getUnchecked(i);
  3072. HWND controlHWND = getHWND (ax);
  3073. if (controlHWND == hwnd)
  3074. {
  3075. switch (message)
  3076. {
  3077. case WM_MOUSEMOVE:
  3078. case WM_LBUTTONDOWN:
  3079. case WM_MBUTTONDOWN:
  3080. case WM_RBUTTONDOWN:
  3081. case WM_LBUTTONUP:
  3082. case WM_MBUTTONUP:
  3083. case WM_RBUTTONUP:
  3084. case WM_LBUTTONDBLCLK:
  3085. case WM_MBUTTONDBLCLK:
  3086. case WM_RBUTTONDBLCLK:
  3087. if (ax->isShowing())
  3088. {
  3089. ComponentPeer* const peer = ax->getPeer();
  3090. if (peer != 0)
  3091. {
  3092. offerActiveXMouseEventToPeer (peer, hwnd, message, lParam);
  3093. if (! ax->areMouseEventsAllowed())
  3094. return 0;
  3095. }
  3096. }
  3097. break;
  3098. default:
  3099. break;
  3100. }
  3101. return CallWindowProc ((WNDPROC) (ax->originalWndProc), hwnd, message, wParam, lParam);
  3102. }
  3103. }
  3104. return DefWindowProc (hwnd, message, wParam, lParam);
  3105. }
  3106. ActiveXControlComponent::ActiveXControlComponent()
  3107. : originalWndProc (0),
  3108. control (0),
  3109. mouseEventsAllowed (true)
  3110. {
  3111. activeXComps.add (this);
  3112. }
  3113. ActiveXControlComponent::~ActiveXControlComponent()
  3114. {
  3115. deleteControl();
  3116. activeXComps.removeValue (this);
  3117. }
  3118. void ActiveXControlComponent::paint (Graphics& g)
  3119. {
  3120. if (control == 0)
  3121. g.fillAll (Colours::lightgrey);
  3122. }
  3123. bool ActiveXControlComponent::createControl (const void* controlIID)
  3124. {
  3125. deleteControl();
  3126. ComponentPeer* const peer = getPeer();
  3127. // the component must have already been added to a real window when you call this!
  3128. jassert (dynamic_cast <Win32ComponentPeer*> (peer) != 0);
  3129. if (dynamic_cast <Win32ComponentPeer*> (peer) != 0)
  3130. {
  3131. int x = 0, y = 0;
  3132. relativePositionToOtherComponent (getTopLevelComponent(), x, y);
  3133. HWND hwnd = (HWND) peer->getNativeHandle();
  3134. ActiveXControlData* const info = new ActiveXControlData (hwnd, this);
  3135. HRESULT hr;
  3136. if ((hr = OleCreate (*(const IID*) controlIID, IID_IOleObject, 1 /*OLERENDER_DRAW*/, 0,
  3137. info->clientSite, info->storage,
  3138. (void**) &(info->control))) == S_OK)
  3139. {
  3140. info->control->SetHostNames (L"Juce", 0);
  3141. if (OleSetContainedObject (info->control, TRUE) == S_OK)
  3142. {
  3143. RECT rect;
  3144. rect.left = x;
  3145. rect.top = y;
  3146. rect.right = x + getWidth();
  3147. rect.bottom = y + getHeight();
  3148. if (info->control->DoVerb (OLEIVERB_SHOW, 0, info->clientSite, 0, hwnd, &rect) == S_OK)
  3149. {
  3150. control = info;
  3151. setControlBounds (Rectangle (x, y, getWidth(), getHeight()));
  3152. HWND controlHWND = getHWND (this);
  3153. if (controlHWND != 0)
  3154. {
  3155. originalWndProc = (void*) GetWindowLongPtr (controlHWND, GWLP_WNDPROC);
  3156. SetWindowLongPtr (controlHWND, GWLP_WNDPROC, (LONG_PTR) activeXHookWndProc);
  3157. }
  3158. return true;
  3159. }
  3160. }
  3161. }
  3162. delete info;
  3163. }
  3164. return false;
  3165. }
  3166. void ActiveXControlComponent::deleteControl()
  3167. {
  3168. ActiveXControlData* const info = (ActiveXControlData*) control;
  3169. if (info != 0)
  3170. {
  3171. delete info;
  3172. control = 0;
  3173. originalWndProc = 0;
  3174. }
  3175. }
  3176. void* ActiveXControlComponent::queryInterface (const void* iid) const
  3177. {
  3178. ActiveXControlData* const info = (ActiveXControlData*) control;
  3179. void* result = 0;
  3180. if (info != 0 && info->control != 0
  3181. && info->control->QueryInterface (*(const IID*) iid, &result) == S_OK)
  3182. return result;
  3183. return 0;
  3184. }
  3185. void ActiveXControlComponent::setControlBounds (const Rectangle& newBounds) const
  3186. {
  3187. HWND hwnd = getHWND (this);
  3188. if (hwnd != 0)
  3189. MoveWindow (hwnd, newBounds.getX(), newBounds.getY(), newBounds.getWidth(), newBounds.getHeight(), TRUE);
  3190. }
  3191. void ActiveXControlComponent::setControlVisible (const bool shouldBeVisible) const
  3192. {
  3193. HWND hwnd = getHWND (this);
  3194. if (hwnd != 0)
  3195. ShowWindow (hwnd, shouldBeVisible ? SW_SHOWNA : SW_HIDE);
  3196. }
  3197. void ActiveXControlComponent::setMouseEventsAllowed (const bool eventsCanReachControl)
  3198. {
  3199. mouseEventsAllowed = eventsCanReachControl;
  3200. }
  3201. END_JUCE_NAMESPACE