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.

4011 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. #include "../../../src/juce_appframework/application/juce_Application.h"
  74. extern void juce_repeatLastProcessPriority() throw(); // in juce_win32_Threads.cpp
  75. extern void juce_CheckCurrentlyFocusedTopLevelWindow() throw(); // in juce_TopLevelWindow.cpp
  76. extern bool juce_IsRunningInWine() throw();
  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 & windowIsSemiTransparent) != 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. case WM_QUIT:
  1646. JUCEApplication::quit();
  1647. return 0;
  1648. //==============================================================================
  1649. case WM_TRAYNOTIFY:
  1650. if (component->isCurrentlyBlockedByAnotherModalComponent())
  1651. {
  1652. if (lParam == WM_LBUTTONDOWN || lParam == WM_RBUTTONDOWN
  1653. || lParam == WM_LBUTTONDBLCLK || lParam == WM_LBUTTONDBLCLK)
  1654. {
  1655. Component* const current = Component::getCurrentlyModalComponent();
  1656. if (current != 0)
  1657. current->inputAttemptWhenModal();
  1658. }
  1659. }
  1660. else
  1661. {
  1662. const int oldModifiers = currentModifiers;
  1663. MouseEvent e (0, 0, ModifierKeys::getCurrentModifiersRealtime(), component,
  1664. getMouseEventTime(), 0, 0, getMouseEventTime(), 1, false);
  1665. if (lParam == WM_LBUTTONDOWN || lParam == WM_LBUTTONDBLCLK)
  1666. e.mods = ModifierKeys (e.mods.getRawFlags() | ModifierKeys::leftButtonModifier);
  1667. else if (lParam == WM_RBUTTONDOWN || lParam == WM_RBUTTONDBLCLK)
  1668. e.mods = ModifierKeys (e.mods.getRawFlags() | ModifierKeys::rightButtonModifier);
  1669. if (lParam == WM_LBUTTONDOWN || lParam == WM_RBUTTONDOWN)
  1670. {
  1671. SetFocus (hwnd);
  1672. SetForegroundWindow (hwnd);
  1673. component->mouseDown (e);
  1674. }
  1675. else if (lParam == WM_LBUTTONUP || lParam == WM_RBUTTONUP)
  1676. {
  1677. e.mods = ModifierKeys (oldModifiers);
  1678. component->mouseUp (e);
  1679. }
  1680. else if (lParam == WM_LBUTTONDBLCLK || lParam == WM_LBUTTONDBLCLK)
  1681. {
  1682. e.mods = ModifierKeys (oldModifiers);
  1683. component->mouseDoubleClick (e);
  1684. }
  1685. else if (lParam == WM_MOUSEMOVE)
  1686. {
  1687. component->mouseMove (e);
  1688. }
  1689. }
  1690. break;
  1691. //==============================================================================
  1692. case WM_SYNCPAINT:
  1693. return 0;
  1694. case WM_PALETTECHANGED:
  1695. InvalidateRect (h, 0, 0);
  1696. break;
  1697. case WM_DISPLAYCHANGE:
  1698. InvalidateRect (h, 0, 0);
  1699. createPaletteIfNeeded = true;
  1700. // intentional fall-through...
  1701. case WM_SETTINGCHANGE: // note the fall-through in the previous case!
  1702. doSettingChange();
  1703. break;
  1704. case WM_INITMENU:
  1705. if (! hasTitleBar())
  1706. {
  1707. if (isFullScreen())
  1708. {
  1709. EnableMenuItem ((HMENU) wParam, SC_RESTORE, MF_BYCOMMAND | MF_ENABLED);
  1710. EnableMenuItem ((HMENU) wParam, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
  1711. }
  1712. else if (! isMinimised())
  1713. {
  1714. EnableMenuItem ((HMENU) wParam, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
  1715. }
  1716. }
  1717. break;
  1718. case WM_SYSCOMMAND:
  1719. switch (wParam & 0xfff0)
  1720. {
  1721. case SC_CLOSE:
  1722. if (hasTitleBar())
  1723. {
  1724. PostMessage (h, WM_CLOSE, 0, 0);
  1725. return 0;
  1726. }
  1727. break;
  1728. case SC_KEYMENU:
  1729. if (hasTitleBar() && h == GetCapture())
  1730. ReleaseCapture();
  1731. break;
  1732. case SC_MAXIMIZE:
  1733. setFullScreen (true);
  1734. return 0;
  1735. case SC_MINIMIZE:
  1736. if (! hasTitleBar())
  1737. {
  1738. setMinimised (true);
  1739. return 0;
  1740. }
  1741. break;
  1742. case SC_RESTORE:
  1743. if (hasTitleBar())
  1744. {
  1745. if (isFullScreen())
  1746. {
  1747. setFullScreen (false);
  1748. return 0;
  1749. }
  1750. }
  1751. else
  1752. {
  1753. if (isMinimised())
  1754. setMinimised (false);
  1755. else if (isFullScreen())
  1756. setFullScreen (false);
  1757. return 0;
  1758. }
  1759. break;
  1760. case SC_MONITORPOWER:
  1761. case SC_SCREENSAVE:
  1762. if (! screenSaverAllowed)
  1763. return 0;
  1764. break;
  1765. }
  1766. break;
  1767. case WM_NCLBUTTONDOWN:
  1768. case WM_NCRBUTTONDOWN:
  1769. case WM_NCMBUTTONDOWN:
  1770. if (component->isCurrentlyBlockedByAnotherModalComponent())
  1771. {
  1772. Component* const current = Component::getCurrentlyModalComponent();
  1773. if (current != 0)
  1774. current->inputAttemptWhenModal();
  1775. }
  1776. break;
  1777. //case WM_IME_STARTCOMPOSITION;
  1778. // return 0;
  1779. case WM_GETDLGCODE:
  1780. return DLGC_WANTALLKEYS;
  1781. default:
  1782. break;
  1783. }
  1784. }
  1785. }
  1786. // (the message manager lock exits before calling this, to avoid deadlocks if
  1787. // this calls into non-juce windows)
  1788. return DefWindowProc (h, message, wParam, lParam);
  1789. }
  1790. Win32ComponentPeer (const Win32ComponentPeer&);
  1791. const Win32ComponentPeer& operator= (const Win32ComponentPeer&);
  1792. };
  1793. ComponentPeer* Component::createNewPeer (int styleFlags, void* /*nativeWindowToAttachTo*/)
  1794. {
  1795. return new Win32ComponentPeer (this, styleFlags);
  1796. }
  1797. juce_ImplementSingleton_SingleThreaded (Win32ComponentPeer::WindowClassHolder);
  1798. //==============================================================================
  1799. void SystemTrayIconComponent::setIconImage (const Image& newImage)
  1800. {
  1801. Win32ComponentPeer* const wp = dynamic_cast <Win32ComponentPeer*> (getPeer());
  1802. if (wp != 0)
  1803. wp->setTaskBarIcon (&newImage);
  1804. }
  1805. void SystemTrayIconComponent::setIconTooltip (const String& tooltip)
  1806. {
  1807. Win32ComponentPeer* const wp = dynamic_cast <Win32ComponentPeer*> (getPeer());
  1808. if (wp != 0)
  1809. wp->setTaskBarIconToolTip (tooltip);
  1810. }
  1811. //==============================================================================
  1812. void juce_setWindowStyleBit (HWND h, const int styleType, const int feature, const bool bitIsSet) throw()
  1813. {
  1814. DWORD val = GetWindowLong (h, styleType);
  1815. if (bitIsSet)
  1816. val |= feature;
  1817. else
  1818. val &= ~feature;
  1819. SetWindowLongPtr (h, styleType, val);
  1820. SetWindowPos (h, 0, 0, 0, 0, 0,
  1821. SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER
  1822. | SWP_NOOWNERZORDER | SWP_FRAMECHANGED | SWP_NOSENDCHANGING);
  1823. }
  1824. //==============================================================================
  1825. bool Process::isForegroundProcess() throw()
  1826. {
  1827. HWND fg = GetForegroundWindow();
  1828. if (fg == 0)
  1829. return true;
  1830. DWORD processId = 0;
  1831. GetWindowThreadProcessId (fg, &processId);
  1832. return processId == GetCurrentProcessId();
  1833. }
  1834. //==============================================================================
  1835. void Desktop::getMousePosition (int& x, int& y) throw()
  1836. {
  1837. POINT mousePos;
  1838. GetCursorPos (&mousePos);
  1839. x = mousePos.x;
  1840. y = mousePos.y;
  1841. }
  1842. void Desktop::setMousePosition (int x, int y) throw()
  1843. {
  1844. SetCursorPos (x, y);
  1845. }
  1846. //==============================================================================
  1847. void Desktop::setScreenSaverEnabled (const bool isEnabled) throw()
  1848. {
  1849. screenSaverAllowed = isEnabled;
  1850. }
  1851. bool Desktop::isScreenSaverEnabled() throw()
  1852. {
  1853. return screenSaverAllowed;
  1854. }
  1855. //==============================================================================
  1856. static BOOL CALLBACK enumMonitorsProc (HMONITOR, HDC, LPRECT r, LPARAM userInfo)
  1857. {
  1858. Array <Rectangle>* const monitorCoords = (Array <Rectangle>*) userInfo;
  1859. monitorCoords->add (Rectangle (r->left, r->top, r->right - r->left, r->bottom - r->top));
  1860. return TRUE;
  1861. }
  1862. void juce_updateMultiMonitorInfo (Array <Rectangle>& monitorCoords, const bool clipToWorkArea) throw()
  1863. {
  1864. EnumDisplayMonitors (0, 0, &enumMonitorsProc, (LPARAM) &monitorCoords);
  1865. // make sure the first in the list is the main monitor
  1866. for (int i = 1; i < monitorCoords.size(); ++i)
  1867. if (monitorCoords[i].getX() == 0 && monitorCoords[i].getY() == 0)
  1868. monitorCoords.swap (i, 0);
  1869. if (monitorCoords.size() == 0)
  1870. {
  1871. RECT r;
  1872. GetWindowRect (GetDesktopWindow(), &r);
  1873. monitorCoords.add (Rectangle (r.left, r.top, r.right - r.left, r.bottom - r.top));
  1874. }
  1875. if (clipToWorkArea)
  1876. {
  1877. // clip the main monitor to the active non-taskbar area
  1878. RECT r;
  1879. SystemParametersInfo (SPI_GETWORKAREA, 0, &r, 0);
  1880. Rectangle& screen = monitorCoords.getReference (0);
  1881. screen.setPosition (jmax (screen.getX(), r.left),
  1882. jmax (screen.getY(), r.top));
  1883. screen.setSize (jmin (screen.getRight(), r.right) - screen.getX(),
  1884. jmin (screen.getBottom(), r.bottom) - screen.getY());
  1885. }
  1886. }
  1887. //==============================================================================
  1888. static Image* createImageFromHBITMAP (HBITMAP bitmap) throw()
  1889. {
  1890. Image* im = 0;
  1891. if (bitmap != 0)
  1892. {
  1893. BITMAP bm;
  1894. if (GetObject (bitmap, sizeof (BITMAP), &bm)
  1895. && bm.bmWidth > 0 && bm.bmHeight > 0)
  1896. {
  1897. HDC tempDC = GetDC (0);
  1898. HDC dc = CreateCompatibleDC (tempDC);
  1899. ReleaseDC (0, tempDC);
  1900. SelectObject (dc, bitmap);
  1901. im = new Image (Image::ARGB, bm.bmWidth, bm.bmHeight, true);
  1902. for (int y = bm.bmHeight; --y >= 0;)
  1903. {
  1904. for (int x = bm.bmWidth; --x >= 0;)
  1905. {
  1906. COLORREF col = GetPixel (dc, x, y);
  1907. im->setPixelAt (x, y, Colour ((uint8) GetRValue (col),
  1908. (uint8) GetGValue (col),
  1909. (uint8) GetBValue (col)));
  1910. }
  1911. }
  1912. DeleteDC (dc);
  1913. }
  1914. }
  1915. return im;
  1916. }
  1917. static Image* createImageFromHICON (HICON icon) throw()
  1918. {
  1919. ICONINFO info;
  1920. if (GetIconInfo (icon, &info))
  1921. {
  1922. Image* const mask = createImageFromHBITMAP (info.hbmMask);
  1923. if (mask == 0)
  1924. return 0;
  1925. Image* const image = createImageFromHBITMAP (info.hbmColor);
  1926. if (image == 0)
  1927. return mask;
  1928. for (int y = image->getHeight(); --y >= 0;)
  1929. {
  1930. for (int x = image->getWidth(); --x >= 0;)
  1931. {
  1932. const float brightness = mask->getPixelAt (x, y).getBrightness();
  1933. if (brightness > 0.0f)
  1934. image->multiplyAlphaAt (x, y, 1.0f - brightness);
  1935. }
  1936. }
  1937. delete mask;
  1938. return image;
  1939. }
  1940. return 0;
  1941. }
  1942. static HICON createHICONFromImage (const Image& image, const BOOL isIcon, int hotspotX, int hotspotY) throw()
  1943. {
  1944. HBITMAP mask = CreateBitmap (image.getWidth(), image.getHeight(), 1, 1, 0);
  1945. ICONINFO info;
  1946. info.fIcon = isIcon;
  1947. info.xHotspot = hotspotX;
  1948. info.yHotspot = hotspotY;
  1949. info.hbmMask = mask;
  1950. HICON hi = 0;
  1951. if (SystemStats::getOperatingSystemType() >= SystemStats::WinXP)
  1952. {
  1953. WindowsBitmapImage bitmap (Image::ARGB, image.getWidth(), image.getHeight(), true);
  1954. Graphics g (bitmap);
  1955. g.drawImageAt (&image, 0, 0);
  1956. info.hbmColor = bitmap.hBitmap;
  1957. hi = CreateIconIndirect (&info);
  1958. }
  1959. else
  1960. {
  1961. HBITMAP colour = CreateCompatibleBitmap (GetDC (0), image.getWidth(), image.getHeight());
  1962. HDC colDC = CreateCompatibleDC (GetDC (0));
  1963. HDC alphaDC = CreateCompatibleDC (GetDC (0));
  1964. SelectObject (colDC, colour);
  1965. SelectObject (alphaDC, mask);
  1966. for (int y = image.getHeight(); --y >= 0;)
  1967. {
  1968. for (int x = image.getWidth(); --x >= 0;)
  1969. {
  1970. const Colour c (image.getPixelAt (x, y));
  1971. SetPixel (colDC, x, y, COLORREF (c.getRed() | (c.getGreen() << 8) | (c.getBlue() << 16)));
  1972. SetPixel (alphaDC, x, y, COLORREF (0xffffff - (c.getAlpha() | (c.getAlpha() << 8) | (c.getAlpha() << 16))));
  1973. }
  1974. }
  1975. DeleteDC (colDC);
  1976. DeleteDC (alphaDC);
  1977. info.hbmColor = colour;
  1978. hi = CreateIconIndirect (&info);
  1979. DeleteObject (colour);
  1980. }
  1981. DeleteObject (mask);
  1982. return hi;
  1983. }
  1984. Image* juce_createIconForFile (const File& file)
  1985. {
  1986. Image* image = 0;
  1987. TCHAR filename [1024];
  1988. file.getFullPathName().copyToBuffer (filename, 1023);
  1989. WORD iconNum = 0;
  1990. HICON icon = ExtractAssociatedIcon ((HINSTANCE) PlatformUtilities::getCurrentModuleInstanceHandle(),
  1991. filename, &iconNum);
  1992. if (icon != 0)
  1993. {
  1994. image = createImageFromHICON (icon);
  1995. DestroyIcon (icon);
  1996. }
  1997. return image;
  1998. }
  1999. //==============================================================================
  2000. void* juce_createMouseCursorFromImage (const Image& image, int hotspotX, int hotspotY) throw()
  2001. {
  2002. const int maxW = GetSystemMetrics (SM_CXCURSOR);
  2003. const int maxH = GetSystemMetrics (SM_CYCURSOR);
  2004. const Image* im = &image;
  2005. Image* newIm = 0;
  2006. if (image.getWidth() > maxW || image.getHeight() > maxH)
  2007. {
  2008. im = newIm = image.createCopy (maxW, maxH);
  2009. hotspotX = (hotspotX * maxW) / image.getWidth();
  2010. hotspotY = (hotspotY * maxH) / image.getHeight();
  2011. }
  2012. void* cursorH = 0;
  2013. const SystemStats::OperatingSystemType os = SystemStats::getOperatingSystemType();
  2014. if (os == SystemStats::WinXP)
  2015. {
  2016. cursorH = (void*) createHICONFromImage (*im, FALSE, hotspotX, hotspotY);
  2017. }
  2018. else
  2019. {
  2020. const int stride = (maxW + 7) >> 3;
  2021. uint8* const andPlane = (uint8*) juce_calloc (stride * maxH);
  2022. uint8* const xorPlane = (uint8*) juce_calloc (stride * maxH);
  2023. int index = 0;
  2024. for (int y = 0; y < maxH; ++y)
  2025. {
  2026. for (int x = 0; x < maxW; ++x)
  2027. {
  2028. const unsigned char bit = (unsigned char) (1 << (7 - (x & 7)));
  2029. const Colour pixelColour (im->getPixelAt (x, y));
  2030. if (pixelColour.getAlpha() < 127)
  2031. andPlane [index + (x >> 3)] |= bit;
  2032. else if (pixelColour.getBrightness() >= 0.5f)
  2033. xorPlane [index + (x >> 3)] |= bit;
  2034. }
  2035. index += stride;
  2036. }
  2037. cursorH = CreateCursor (0, hotspotX, hotspotY, maxW, maxH, andPlane, xorPlane);
  2038. juce_free (andPlane);
  2039. juce_free (xorPlane);
  2040. }
  2041. delete newIm;
  2042. return cursorH;
  2043. }
  2044. void juce_deleteMouseCursor (void* const cursorHandle, const bool isStandard) throw()
  2045. {
  2046. if (cursorHandle != 0 && ! isStandard)
  2047. DestroyCursor ((HCURSOR) cursorHandle);
  2048. }
  2049. void* juce_createStandardMouseCursor (MouseCursor::StandardCursorType type) throw()
  2050. {
  2051. LPCTSTR cursorName = IDC_ARROW;
  2052. switch (type)
  2053. {
  2054. case MouseCursor::NormalCursor:
  2055. cursorName = IDC_ARROW;
  2056. break;
  2057. case MouseCursor::NoCursor:
  2058. return 0;
  2059. case MouseCursor::DraggingHandCursor:
  2060. {
  2061. static void* dragHandCursor = 0;
  2062. if (dragHandCursor == 0)
  2063. {
  2064. static const unsigned char dragHandData[] =
  2065. { 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,
  2066. 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,
  2067. 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 };
  2068. Image* const image = ImageFileFormat::loadFrom ((const char*) dragHandData, sizeof (dragHandData));
  2069. dragHandCursor = juce_createMouseCursorFromImage (*image, 8, 7);
  2070. delete image;
  2071. }
  2072. return dragHandCursor;
  2073. }
  2074. case MouseCursor::WaitCursor:
  2075. cursorName = IDC_WAIT;
  2076. break;
  2077. case MouseCursor::IBeamCursor:
  2078. cursorName = IDC_IBEAM;
  2079. break;
  2080. case MouseCursor::PointingHandCursor:
  2081. cursorName = MAKEINTRESOURCE(32649);
  2082. break;
  2083. case MouseCursor::LeftRightResizeCursor:
  2084. case MouseCursor::LeftEdgeResizeCursor:
  2085. case MouseCursor::RightEdgeResizeCursor:
  2086. cursorName = IDC_SIZEWE;
  2087. break;
  2088. case MouseCursor::UpDownResizeCursor:
  2089. case MouseCursor::TopEdgeResizeCursor:
  2090. case MouseCursor::BottomEdgeResizeCursor:
  2091. cursorName = IDC_SIZENS;
  2092. break;
  2093. case MouseCursor::TopLeftCornerResizeCursor:
  2094. case MouseCursor::BottomRightCornerResizeCursor:
  2095. cursorName = IDC_SIZENWSE;
  2096. break;
  2097. case MouseCursor::TopRightCornerResizeCursor:
  2098. case MouseCursor::BottomLeftCornerResizeCursor:
  2099. cursorName = IDC_SIZENESW;
  2100. break;
  2101. case MouseCursor::UpDownLeftRightResizeCursor:
  2102. cursorName = IDC_SIZEALL;
  2103. break;
  2104. case MouseCursor::CrosshairCursor:
  2105. cursorName = IDC_CROSS;
  2106. break;
  2107. case MouseCursor::CopyingCursor:
  2108. // can't seem to find one of these in the win32 list..
  2109. break;
  2110. }
  2111. HCURSOR cursorH = LoadCursor (0, cursorName);
  2112. if (cursorH == 0)
  2113. cursorH = LoadCursor (0, IDC_ARROW);
  2114. return (void*) cursorH;
  2115. }
  2116. //==============================================================================
  2117. void MouseCursor::showInWindow (ComponentPeer*) const throw()
  2118. {
  2119. SetCursor ((HCURSOR) getHandle());
  2120. }
  2121. void MouseCursor::showInAllWindows() const throw()
  2122. {
  2123. showInWindow (0);
  2124. }
  2125. //==============================================================================
  2126. //==============================================================================
  2127. class JuceDropSource : public IDropSource
  2128. {
  2129. int refCount;
  2130. public:
  2131. JuceDropSource()
  2132. : refCount (1)
  2133. {
  2134. }
  2135. virtual ~JuceDropSource()
  2136. {
  2137. jassert (refCount == 0);
  2138. }
  2139. HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
  2140. {
  2141. if (id == IID_IUnknown || id == IID_IDropSource)
  2142. {
  2143. AddRef();
  2144. *result = this;
  2145. return S_OK;
  2146. }
  2147. *result = 0;
  2148. return E_NOINTERFACE;
  2149. }
  2150. ULONG __stdcall AddRef() { return ++refCount; }
  2151. ULONG __stdcall Release() { jassert (refCount > 0); const int r = --refCount; if (r == 0) delete this; return r; }
  2152. HRESULT __stdcall QueryContinueDrag (BOOL escapePressed, DWORD keys)
  2153. {
  2154. if (escapePressed)
  2155. return DRAGDROP_S_CANCEL;
  2156. if ((keys & (MK_LBUTTON | MK_RBUTTON)) == 0)
  2157. return DRAGDROP_S_DROP;
  2158. return S_OK;
  2159. }
  2160. HRESULT __stdcall GiveFeedback (DWORD)
  2161. {
  2162. return DRAGDROP_S_USEDEFAULTCURSORS;
  2163. }
  2164. };
  2165. class JuceEnumFormatEtc : public IEnumFORMATETC
  2166. {
  2167. public:
  2168. JuceEnumFormatEtc (const FORMATETC* const format_)
  2169. : refCount (1),
  2170. format (format_),
  2171. index (0)
  2172. {
  2173. }
  2174. virtual ~JuceEnumFormatEtc()
  2175. {
  2176. jassert (refCount == 0);
  2177. }
  2178. HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
  2179. {
  2180. if (id == IID_IUnknown || id == IID_IEnumFORMATETC)
  2181. {
  2182. AddRef();
  2183. *result = this;
  2184. return S_OK;
  2185. }
  2186. *result = 0;
  2187. return E_NOINTERFACE;
  2188. }
  2189. ULONG __stdcall AddRef() { return ++refCount; }
  2190. ULONG __stdcall Release() { jassert (refCount > 0); const int r = --refCount; if (r == 0) delete this; return r; }
  2191. HRESULT __stdcall Clone (IEnumFORMATETC** result)
  2192. {
  2193. if (result == 0)
  2194. return E_POINTER;
  2195. JuceEnumFormatEtc* const newOne = new JuceEnumFormatEtc (format);
  2196. newOne->index = index;
  2197. *result = newOne;
  2198. return S_OK;
  2199. }
  2200. HRESULT __stdcall Next (ULONG celt, LPFORMATETC lpFormatEtc, ULONG* pceltFetched)
  2201. {
  2202. if (pceltFetched != 0)
  2203. *pceltFetched = 0;
  2204. else if (celt != 1)
  2205. return S_FALSE;
  2206. if (index == 0 && celt > 0 && lpFormatEtc != 0)
  2207. {
  2208. copyFormatEtc (lpFormatEtc [0], *format);
  2209. ++index;
  2210. if (pceltFetched != 0)
  2211. *pceltFetched = 1;
  2212. return S_OK;
  2213. }
  2214. return S_FALSE;
  2215. }
  2216. HRESULT __stdcall Skip (ULONG celt)
  2217. {
  2218. if (index + (int) celt >= 1)
  2219. return S_FALSE;
  2220. index += celt;
  2221. return S_OK;
  2222. }
  2223. HRESULT __stdcall Reset()
  2224. {
  2225. index = 0;
  2226. return S_OK;
  2227. }
  2228. private:
  2229. int refCount;
  2230. const FORMATETC* const format;
  2231. int index;
  2232. static void copyFormatEtc (FORMATETC& dest, const FORMATETC& source)
  2233. {
  2234. dest = source;
  2235. if (source.ptd != 0)
  2236. {
  2237. dest.ptd = (DVTARGETDEVICE*) CoTaskMemAlloc (sizeof (DVTARGETDEVICE));
  2238. *(dest.ptd) = *(source.ptd);
  2239. }
  2240. }
  2241. JuceEnumFormatEtc (const JuceEnumFormatEtc&);
  2242. const JuceEnumFormatEtc& operator= (const JuceEnumFormatEtc&);
  2243. };
  2244. class JuceDataObject : public IDataObject
  2245. {
  2246. JuceDropSource* const dropSource;
  2247. const FORMATETC* const format;
  2248. const STGMEDIUM* const medium;
  2249. int refCount;
  2250. JuceDataObject (const JuceDataObject&);
  2251. const JuceDataObject& operator= (const JuceDataObject&);
  2252. public:
  2253. JuceDataObject (JuceDropSource* const dropSource_,
  2254. const FORMATETC* const format_,
  2255. const STGMEDIUM* const medium_)
  2256. : dropSource (dropSource_),
  2257. format (format_),
  2258. medium (medium_),
  2259. refCount (1)
  2260. {
  2261. }
  2262. virtual ~JuceDataObject()
  2263. {
  2264. jassert (refCount == 0);
  2265. }
  2266. HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
  2267. {
  2268. if (id == IID_IUnknown || id == IID_IDataObject)
  2269. {
  2270. AddRef();
  2271. *result = this;
  2272. return S_OK;
  2273. }
  2274. *result = 0;
  2275. return E_NOINTERFACE;
  2276. }
  2277. ULONG __stdcall AddRef() { return ++refCount; }
  2278. ULONG __stdcall Release() { jassert (refCount > 0); const int r = --refCount; if (r == 0) delete this; return r; }
  2279. HRESULT __stdcall GetData (FORMATETC __RPC_FAR* pFormatEtc, STGMEDIUM __RPC_FAR* pMedium)
  2280. {
  2281. if (pFormatEtc->tymed == format->tymed
  2282. && pFormatEtc->cfFormat == format->cfFormat
  2283. && pFormatEtc->dwAspect == format->dwAspect)
  2284. {
  2285. pMedium->tymed = format->tymed;
  2286. pMedium->pUnkForRelease = 0;
  2287. if (format->tymed == TYMED_HGLOBAL)
  2288. {
  2289. const SIZE_T len = GlobalSize (medium->hGlobal);
  2290. void* const src = GlobalLock (medium->hGlobal);
  2291. void* const dst = GlobalAlloc (GMEM_FIXED, len);
  2292. memcpy (dst, src, len);
  2293. GlobalUnlock (medium->hGlobal);
  2294. pMedium->hGlobal = dst;
  2295. return S_OK;
  2296. }
  2297. }
  2298. return DV_E_FORMATETC;
  2299. }
  2300. HRESULT __stdcall QueryGetData (FORMATETC __RPC_FAR* f)
  2301. {
  2302. if (f == 0)
  2303. return E_INVALIDARG;
  2304. if (f->tymed == format->tymed
  2305. && f->cfFormat == format->cfFormat
  2306. && f->dwAspect == format->dwAspect)
  2307. return S_OK;
  2308. return DV_E_FORMATETC;
  2309. }
  2310. HRESULT __stdcall GetCanonicalFormatEtc (FORMATETC __RPC_FAR*, FORMATETC __RPC_FAR* pFormatEtcOut)
  2311. {
  2312. pFormatEtcOut->ptd = 0;
  2313. return E_NOTIMPL;
  2314. }
  2315. HRESULT __stdcall EnumFormatEtc (DWORD direction, IEnumFORMATETC __RPC_FAR *__RPC_FAR *result)
  2316. {
  2317. if (result == 0)
  2318. return E_POINTER;
  2319. if (direction == DATADIR_GET)
  2320. {
  2321. *result = new JuceEnumFormatEtc (format);
  2322. return S_OK;
  2323. }
  2324. *result = 0;
  2325. return E_NOTIMPL;
  2326. }
  2327. HRESULT __stdcall GetDataHere (FORMATETC __RPC_FAR*, STGMEDIUM __RPC_FAR*) { return DATA_E_FORMATETC; }
  2328. HRESULT __stdcall SetData (FORMATETC __RPC_FAR*, STGMEDIUM __RPC_FAR*, BOOL) { return E_NOTIMPL; }
  2329. HRESULT __stdcall DAdvise (FORMATETC __RPC_FAR*, DWORD, IAdviseSink __RPC_FAR*, DWORD __RPC_FAR*) { return OLE_E_ADVISENOTSUPPORTED; }
  2330. HRESULT __stdcall DUnadvise (DWORD) { return E_NOTIMPL; }
  2331. HRESULT __stdcall EnumDAdvise (IEnumSTATDATA __RPC_FAR *__RPC_FAR *) { return OLE_E_ADVISENOTSUPPORTED; }
  2332. };
  2333. static HDROP createHDrop (const StringArray& fileNames) throw()
  2334. {
  2335. int totalChars = 0;
  2336. for (int i = fileNames.size(); --i >= 0;)
  2337. totalChars += fileNames[i].length() + 1;
  2338. HDROP hDrop = (HDROP) GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT,
  2339. sizeof (DROPFILES)
  2340. + sizeof (WCHAR) * (totalChars + 2));
  2341. if (hDrop != 0)
  2342. {
  2343. LPDROPFILES pDropFiles = (LPDROPFILES) GlobalLock (hDrop);
  2344. pDropFiles->pFiles = sizeof (DROPFILES);
  2345. pDropFiles->fWide = true;
  2346. WCHAR* fname = (WCHAR*) (((char*) pDropFiles) + sizeof (DROPFILES));
  2347. for (int i = 0; i < fileNames.size(); ++i)
  2348. {
  2349. fileNames[i].copyToBuffer (fname, 2048);
  2350. fname += fileNames[i].length() + 1;
  2351. }
  2352. *fname = 0;
  2353. GlobalUnlock (hDrop);
  2354. }
  2355. return hDrop;
  2356. }
  2357. static bool performDragDrop (FORMATETC* const format, STGMEDIUM* const medium, const DWORD whatToDo) throw()
  2358. {
  2359. JuceDropSource* const source = new JuceDropSource();
  2360. JuceDataObject* const data = new JuceDataObject (source, format, medium);
  2361. DWORD effect;
  2362. const HRESULT res = DoDragDrop (data, source, whatToDo, &effect);
  2363. data->Release();
  2364. source->Release();
  2365. return res == DRAGDROP_S_DROP;
  2366. }
  2367. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool canMove)
  2368. {
  2369. FORMATETC format = { CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  2370. STGMEDIUM medium = { TYMED_HGLOBAL, { 0 }, 0 };
  2371. medium.hGlobal = createHDrop (files);
  2372. return performDragDrop (&format, &medium, canMove ? (DROPEFFECT_COPY | DROPEFFECT_MOVE)
  2373. : DROPEFFECT_COPY);
  2374. }
  2375. bool DragAndDropContainer::performExternalDragDropOfText (const String& text)
  2376. {
  2377. FORMATETC format = { CF_TEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  2378. STGMEDIUM medium = { TYMED_HGLOBAL, { 0 }, 0 };
  2379. const int numChars = text.length();
  2380. medium.hGlobal = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, (numChars + 2) * sizeof (WCHAR));
  2381. char* d = (char*) GlobalLock (medium.hGlobal);
  2382. text.copyToBuffer ((WCHAR*) d, numChars + 1);
  2383. format.cfFormat = CF_UNICODETEXT;
  2384. GlobalUnlock (medium.hGlobal);
  2385. return performDragDrop (&format, &medium, DROPEFFECT_COPY | DROPEFFECT_MOVE);
  2386. }
  2387. //==============================================================================
  2388. //==============================================================================
  2389. #if JUCE_OPENGL
  2390. #define WGL_EXT_FUNCTION_INIT(extType, extFunc) \
  2391. ((extFunc = (extType) wglGetProcAddress (#extFunc)) != 0)
  2392. typedef const char* (WINAPI* PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
  2393. typedef BOOL (WINAPI * PFNWGLGETPIXELFORMATATTRIBIVARBPROC) (HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues);
  2394. typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int* piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
  2395. typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
  2396. typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
  2397. #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
  2398. #define WGL_DRAW_TO_WINDOW_ARB 0x2001
  2399. #define WGL_ACCELERATION_ARB 0x2003
  2400. #define WGL_SWAP_METHOD_ARB 0x2007
  2401. #define WGL_SUPPORT_OPENGL_ARB 0x2010
  2402. #define WGL_PIXEL_TYPE_ARB 0x2013
  2403. #define WGL_DOUBLE_BUFFER_ARB 0x2011
  2404. #define WGL_COLOR_BITS_ARB 0x2014
  2405. #define WGL_RED_BITS_ARB 0x2015
  2406. #define WGL_GREEN_BITS_ARB 0x2017
  2407. #define WGL_BLUE_BITS_ARB 0x2019
  2408. #define WGL_ALPHA_BITS_ARB 0x201B
  2409. #define WGL_DEPTH_BITS_ARB 0x2022
  2410. #define WGL_STENCIL_BITS_ARB 0x2023
  2411. #define WGL_FULL_ACCELERATION_ARB 0x2027
  2412. #define WGL_ACCUM_RED_BITS_ARB 0x201E
  2413. #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
  2414. #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
  2415. #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
  2416. #define WGL_STEREO_ARB 0x2012
  2417. #define WGL_SAMPLE_BUFFERS_ARB 0x2041
  2418. #define WGL_SAMPLES_ARB 0x2042
  2419. #define WGL_TYPE_RGBA_ARB 0x202B
  2420. static void getWglExtensions (HDC dc, StringArray& result) throw()
  2421. {
  2422. PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = 0;
  2423. if (WGL_EXT_FUNCTION_INIT (PFNWGLGETEXTENSIONSSTRINGARBPROC, wglGetExtensionsStringARB))
  2424. result.addTokens (String (wglGetExtensionsStringARB (dc)), false);
  2425. else
  2426. jassertfalse // If this fails, it may be because you didn't activate the openGL context
  2427. }
  2428. //==============================================================================
  2429. class WindowedGLContext : public OpenGLContext
  2430. {
  2431. public:
  2432. WindowedGLContext (Component* const component_,
  2433. HGLRC contextToShareWith,
  2434. const OpenGLPixelFormat& pixelFormat)
  2435. : renderContext (0),
  2436. nativeWindow (0),
  2437. dc (0),
  2438. component (component_)
  2439. {
  2440. jassert (component != 0);
  2441. createNativeWindow();
  2442. // Use a default pixel format that should be supported everywhere
  2443. PIXELFORMATDESCRIPTOR pfd;
  2444. zerostruct (pfd);
  2445. pfd.nSize = sizeof (pfd);
  2446. pfd.nVersion = 1;
  2447. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  2448. pfd.iPixelType = PFD_TYPE_RGBA;
  2449. pfd.cColorBits = 24;
  2450. pfd.cDepthBits = 16;
  2451. const int format = ChoosePixelFormat (dc, &pfd);
  2452. if (format != 0)
  2453. SetPixelFormat (dc, format, &pfd);
  2454. renderContext = wglCreateContext (dc);
  2455. makeActive();
  2456. setPixelFormat (pixelFormat);
  2457. if (contextToShareWith != 0 && renderContext != 0)
  2458. wglShareLists (contextToShareWith, renderContext);
  2459. }
  2460. ~WindowedGLContext()
  2461. {
  2462. makeInactive();
  2463. wglDeleteContext (renderContext);
  2464. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  2465. delete nativeWindow;
  2466. }
  2467. bool makeActive() const throw()
  2468. {
  2469. jassert (renderContext != 0);
  2470. return wglMakeCurrent (dc, renderContext) != 0;
  2471. }
  2472. bool makeInactive() const throw()
  2473. {
  2474. return (! isActive()) || (wglMakeCurrent (0, 0) != 0);
  2475. }
  2476. bool isActive() const throw()
  2477. {
  2478. return wglGetCurrentContext() == renderContext;
  2479. }
  2480. const OpenGLPixelFormat getPixelFormat() const
  2481. {
  2482. OpenGLPixelFormat pf;
  2483. makeActive();
  2484. StringArray availableExtensions;
  2485. getWglExtensions (dc, availableExtensions);
  2486. fillInPixelFormatDetails (GetPixelFormat (dc), pf, availableExtensions);
  2487. return pf;
  2488. }
  2489. void* getRawContext() const throw()
  2490. {
  2491. return renderContext;
  2492. }
  2493. bool setPixelFormat (const OpenGLPixelFormat& pixelFormat)
  2494. {
  2495. makeActive();
  2496. PIXELFORMATDESCRIPTOR pfd;
  2497. zerostruct (pfd);
  2498. pfd.nSize = sizeof (pfd);
  2499. pfd.nVersion = 1;
  2500. pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
  2501. pfd.iPixelType = PFD_TYPE_RGBA;
  2502. pfd.iLayerType = PFD_MAIN_PLANE;
  2503. pfd.cColorBits = pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits;
  2504. pfd.cRedBits = pixelFormat.redBits;
  2505. pfd.cGreenBits = pixelFormat.greenBits;
  2506. pfd.cBlueBits = pixelFormat.blueBits;
  2507. pfd.cAlphaBits = pixelFormat.alphaBits;
  2508. pfd.cDepthBits = pixelFormat.depthBufferBits;
  2509. pfd.cStencilBits = pixelFormat.stencilBufferBits;
  2510. pfd.cAccumBits = pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
  2511. + pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits;
  2512. pfd.cAccumRedBits = pixelFormat.accumulationBufferRedBits;
  2513. pfd.cAccumGreenBits = pixelFormat.accumulationBufferGreenBits;
  2514. pfd.cAccumBlueBits = pixelFormat.accumulationBufferBlueBits;
  2515. pfd.cAccumAlphaBits = pixelFormat.accumulationBufferAlphaBits;
  2516. int format = 0;
  2517. PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = 0;
  2518. StringArray availableExtensions;
  2519. getWglExtensions (dc, availableExtensions);
  2520. if (availableExtensions.contains ("WGL_ARB_pixel_format")
  2521. && WGL_EXT_FUNCTION_INIT (PFNWGLCHOOSEPIXELFORMATARBPROC, wglChoosePixelFormatARB))
  2522. {
  2523. int attributes[64];
  2524. int n = 0;
  2525. attributes[n++] = WGL_DRAW_TO_WINDOW_ARB;
  2526. attributes[n++] = GL_TRUE;
  2527. attributes[n++] = WGL_SUPPORT_OPENGL_ARB;
  2528. attributes[n++] = GL_TRUE;
  2529. attributes[n++] = WGL_ACCELERATION_ARB;
  2530. attributes[n++] = WGL_FULL_ACCELERATION_ARB;
  2531. attributes[n++] = WGL_DOUBLE_BUFFER_ARB;
  2532. attributes[n++] = GL_TRUE;
  2533. attributes[n++] = WGL_PIXEL_TYPE_ARB;
  2534. attributes[n++] = WGL_TYPE_RGBA_ARB;
  2535. attributes[n++] = WGL_COLOR_BITS_ARB;
  2536. attributes[n++] = pfd.cColorBits;
  2537. attributes[n++] = WGL_RED_BITS_ARB;
  2538. attributes[n++] = pixelFormat.redBits;
  2539. attributes[n++] = WGL_GREEN_BITS_ARB;
  2540. attributes[n++] = pixelFormat.greenBits;
  2541. attributes[n++] = WGL_BLUE_BITS_ARB;
  2542. attributes[n++] = pixelFormat.blueBits;
  2543. attributes[n++] = WGL_ALPHA_BITS_ARB;
  2544. attributes[n++] = pixelFormat.alphaBits;
  2545. attributes[n++] = WGL_DEPTH_BITS_ARB;
  2546. attributes[n++] = pixelFormat.depthBufferBits;
  2547. if (pixelFormat.stencilBufferBits > 0)
  2548. {
  2549. attributes[n++] = WGL_STENCIL_BITS_ARB;
  2550. attributes[n++] = pixelFormat.stencilBufferBits;
  2551. }
  2552. attributes[n++] = WGL_ACCUM_RED_BITS_ARB;
  2553. attributes[n++] = pixelFormat.accumulationBufferRedBits;
  2554. attributes[n++] = WGL_ACCUM_GREEN_BITS_ARB;
  2555. attributes[n++] = pixelFormat.accumulationBufferGreenBits;
  2556. attributes[n++] = WGL_ACCUM_BLUE_BITS_ARB;
  2557. attributes[n++] = pixelFormat.accumulationBufferBlueBits;
  2558. attributes[n++] = WGL_ACCUM_ALPHA_BITS_ARB;
  2559. attributes[n++] = pixelFormat.accumulationBufferAlphaBits;
  2560. if (availableExtensions.contains ("WGL_ARB_multisample")
  2561. && pixelFormat.fullSceneAntiAliasingNumSamples > 0)
  2562. {
  2563. attributes[n++] = WGL_SAMPLE_BUFFERS_ARB;
  2564. attributes[n++] = 1;
  2565. attributes[n++] = WGL_SAMPLES_ARB;
  2566. attributes[n++] = pixelFormat.fullSceneAntiAliasingNumSamples;
  2567. }
  2568. attributes[n++] = 0;
  2569. UINT formatsCount;
  2570. const BOOL ok = wglChoosePixelFormatARB (dc, attributes, 0, 1, &format, &formatsCount);
  2571. (void) ok;
  2572. jassert (ok);
  2573. }
  2574. else
  2575. {
  2576. format = ChoosePixelFormat (dc, &pfd);
  2577. }
  2578. if (format != 0)
  2579. {
  2580. makeInactive();
  2581. // win32 can't change the pixel format of a window, so need to delete the
  2582. // old one and create a new one..
  2583. jassert (nativeWindow != 0);
  2584. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  2585. delete nativeWindow;
  2586. createNativeWindow();
  2587. if (SetPixelFormat (dc, format, &pfd))
  2588. {
  2589. wglDeleteContext (renderContext);
  2590. renderContext = wglCreateContext (dc);
  2591. jassert (renderContext != 0);
  2592. return renderContext != 0;
  2593. }
  2594. }
  2595. return false;
  2596. }
  2597. void updateWindowPosition (int x, int y, int w, int h, int)
  2598. {
  2599. SetWindowPos ((HWND) nativeWindow->getNativeHandle(), 0,
  2600. x, y, w, h,
  2601. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  2602. }
  2603. void repaint()
  2604. {
  2605. int x, y, w, h;
  2606. nativeWindow->getBounds (x, y, w, h);
  2607. nativeWindow->repaint (0, 0, w, h);
  2608. }
  2609. void swapBuffers()
  2610. {
  2611. SwapBuffers (dc);
  2612. }
  2613. bool setSwapInterval (const int numFramesPerSwap)
  2614. {
  2615. makeActive();
  2616. StringArray availableExtensions;
  2617. getWglExtensions (dc, availableExtensions);
  2618. PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = 0;
  2619. return availableExtensions.contains ("WGL_EXT_swap_control")
  2620. && WGL_EXT_FUNCTION_INIT (PFNWGLSWAPINTERVALEXTPROC, wglSwapIntervalEXT)
  2621. && wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
  2622. }
  2623. int getSwapInterval() const
  2624. {
  2625. makeActive();
  2626. StringArray availableExtensions;
  2627. getWglExtensions (dc, availableExtensions);
  2628. PFNWGLGETSWAPINTERVALEXTPROC wglGetSwapIntervalEXT = 0;
  2629. if (availableExtensions.contains ("WGL_EXT_swap_control")
  2630. && WGL_EXT_FUNCTION_INIT (PFNWGLGETSWAPINTERVALEXTPROC, wglGetSwapIntervalEXT))
  2631. return wglGetSwapIntervalEXT();
  2632. return 0;
  2633. }
  2634. void findAlternativeOpenGLPixelFormats (OwnedArray <OpenGLPixelFormat>& results)
  2635. {
  2636. jassert (isActive());
  2637. StringArray availableExtensions;
  2638. getWglExtensions (dc, availableExtensions);
  2639. PFNWGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB = 0;
  2640. int numTypes = 0;
  2641. if (availableExtensions.contains("WGL_ARB_pixel_format")
  2642. && WGL_EXT_FUNCTION_INIT (PFNWGLGETPIXELFORMATATTRIBIVARBPROC, wglGetPixelFormatAttribivARB))
  2643. {
  2644. int attributes = WGL_NUMBER_PIXEL_FORMATS_ARB;
  2645. if (! wglGetPixelFormatAttribivARB (dc, 1, 0, 1, &attributes, &numTypes))
  2646. jassertfalse
  2647. }
  2648. else
  2649. {
  2650. numTypes = DescribePixelFormat (dc, 0, 0, 0);
  2651. }
  2652. OpenGLPixelFormat pf;
  2653. for (int i = 0; i < numTypes; ++i)
  2654. {
  2655. if (fillInPixelFormatDetails (i + 1, pf, availableExtensions))
  2656. {
  2657. bool alreadyListed = false;
  2658. for (int j = results.size(); --j >= 0;)
  2659. if (pf == *results.getUnchecked(j))
  2660. alreadyListed = true;
  2661. if (! alreadyListed)
  2662. results.add (new OpenGLPixelFormat (pf));
  2663. }
  2664. }
  2665. }
  2666. //==============================================================================
  2667. juce_UseDebuggingNewOperator
  2668. HGLRC renderContext;
  2669. private:
  2670. Win32ComponentPeer* nativeWindow;
  2671. Component* const component;
  2672. HDC dc;
  2673. //==============================================================================
  2674. void createNativeWindow()
  2675. {
  2676. nativeWindow = new Win32ComponentPeer (component, 0);
  2677. nativeWindow->dontRepaint = true;
  2678. nativeWindow->setVisible (true);
  2679. HWND hwnd = (HWND) nativeWindow->getNativeHandle();
  2680. Win32ComponentPeer* const peer = dynamic_cast <Win32ComponentPeer*> (component->getTopLevelComponent()->getPeer());
  2681. if (peer != 0)
  2682. {
  2683. SetParent (hwnd, (HWND) peer->getNativeHandle());
  2684. juce_setWindowStyleBit (hwnd, GWL_STYLE, WS_CHILD, true);
  2685. juce_setWindowStyleBit (hwnd, GWL_STYLE, WS_POPUP, false);
  2686. }
  2687. dc = GetDC (hwnd);
  2688. }
  2689. bool fillInPixelFormatDetails (const int pixelFormatIndex,
  2690. OpenGLPixelFormat& result,
  2691. const StringArray& availableExtensions) const throw()
  2692. {
  2693. PFNWGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB = 0;
  2694. if (availableExtensions.contains ("WGL_ARB_pixel_format")
  2695. && WGL_EXT_FUNCTION_INIT (PFNWGLGETPIXELFORMATATTRIBIVARBPROC, wglGetPixelFormatAttribivARB))
  2696. {
  2697. int attributes[32];
  2698. int numAttributes = 0;
  2699. attributes[numAttributes++] = WGL_DRAW_TO_WINDOW_ARB;
  2700. attributes[numAttributes++] = WGL_SUPPORT_OPENGL_ARB;
  2701. attributes[numAttributes++] = WGL_ACCELERATION_ARB;
  2702. attributes[numAttributes++] = WGL_DOUBLE_BUFFER_ARB;
  2703. attributes[numAttributes++] = WGL_PIXEL_TYPE_ARB;
  2704. attributes[numAttributes++] = WGL_RED_BITS_ARB;
  2705. attributes[numAttributes++] = WGL_GREEN_BITS_ARB;
  2706. attributes[numAttributes++] = WGL_BLUE_BITS_ARB;
  2707. attributes[numAttributes++] = WGL_ALPHA_BITS_ARB;
  2708. attributes[numAttributes++] = WGL_DEPTH_BITS_ARB;
  2709. attributes[numAttributes++] = WGL_STENCIL_BITS_ARB;
  2710. attributes[numAttributes++] = WGL_ACCUM_RED_BITS_ARB;
  2711. attributes[numAttributes++] = WGL_ACCUM_GREEN_BITS_ARB;
  2712. attributes[numAttributes++] = WGL_ACCUM_BLUE_BITS_ARB;
  2713. attributes[numAttributes++] = WGL_ACCUM_ALPHA_BITS_ARB;
  2714. if (availableExtensions.contains ("WGL_ARB_multisample"))
  2715. attributes[numAttributes++] = WGL_SAMPLES_ARB;
  2716. int values[32];
  2717. zeromem (values, sizeof (values));
  2718. if (wglGetPixelFormatAttribivARB (dc, pixelFormatIndex, 0, numAttributes, attributes, values))
  2719. {
  2720. int n = 0;
  2721. bool isValidFormat = (values[n++] == GL_TRUE); // WGL_DRAW_TO_WINDOW_ARB
  2722. isValidFormat = (values[n++] == GL_TRUE) && isValidFormat; // WGL_SUPPORT_OPENGL_ARB
  2723. isValidFormat = (values[n++] == WGL_FULL_ACCELERATION_ARB) && isValidFormat; // WGL_ACCELERATION_ARB
  2724. isValidFormat = (values[n++] == GL_TRUE) && isValidFormat; // WGL_DOUBLE_BUFFER_ARB:
  2725. isValidFormat = (values[n++] == WGL_TYPE_RGBA_ARB) && isValidFormat; // WGL_PIXEL_TYPE_ARB
  2726. result.redBits = values[n++]; // WGL_RED_BITS_ARB
  2727. result.greenBits = values[n++]; // WGL_GREEN_BITS_ARB
  2728. result.blueBits = values[n++]; // WGL_BLUE_BITS_ARB
  2729. result.alphaBits = values[n++]; // WGL_ALPHA_BITS_ARB
  2730. result.depthBufferBits = values[n++]; // WGL_DEPTH_BITS_ARB
  2731. result.stencilBufferBits = values[n++]; // WGL_STENCIL_BITS_ARB
  2732. result.accumulationBufferRedBits = values[n++]; // WGL_ACCUM_RED_BITS_ARB
  2733. result.accumulationBufferGreenBits = values[n++]; // WGL_ACCUM_GREEN_BITS_ARB
  2734. result.accumulationBufferBlueBits = values[n++]; // WGL_ACCUM_BLUE_BITS_ARB
  2735. result.accumulationBufferAlphaBits = values[n++]; // WGL_ACCUM_ALPHA_BITS_ARB
  2736. result.fullSceneAntiAliasingNumSamples = values[n++]; // WGL_SAMPLES_ARB
  2737. return isValidFormat;
  2738. }
  2739. else
  2740. {
  2741. jassertfalse
  2742. }
  2743. }
  2744. else
  2745. {
  2746. PIXELFORMATDESCRIPTOR pfd;
  2747. if (DescribePixelFormat (dc, pixelFormatIndex, sizeof (pfd), &pfd))
  2748. {
  2749. result.redBits = pfd.cRedBits;
  2750. result.greenBits = pfd.cGreenBits;
  2751. result.blueBits = pfd.cBlueBits;
  2752. result.alphaBits = pfd.cAlphaBits;
  2753. result.depthBufferBits = pfd.cDepthBits;
  2754. result.stencilBufferBits = pfd.cStencilBits;
  2755. result.accumulationBufferRedBits = pfd.cAccumRedBits;
  2756. result.accumulationBufferGreenBits = pfd.cAccumGreenBits;
  2757. result.accumulationBufferBlueBits = pfd.cAccumBlueBits;
  2758. result.accumulationBufferAlphaBits = pfd.cAccumAlphaBits;
  2759. result.fullSceneAntiAliasingNumSamples = 0;
  2760. return true;
  2761. }
  2762. else
  2763. {
  2764. jassertfalse
  2765. }
  2766. }
  2767. return false;
  2768. }
  2769. WindowedGLContext (const WindowedGLContext&);
  2770. const WindowedGLContext& operator= (const WindowedGLContext&);
  2771. };
  2772. //==============================================================================
  2773. OpenGLContext* OpenGLContext::createContextForWindow (Component* const component,
  2774. const OpenGLPixelFormat& pixelFormat,
  2775. const OpenGLContext* const contextToShareWith)
  2776. {
  2777. WindowedGLContext* c = new WindowedGLContext (component,
  2778. contextToShareWith != 0 ? (HGLRC) contextToShareWith->getRawContext() : 0,
  2779. pixelFormat);
  2780. if (c->renderContext == 0)
  2781. deleteAndZero (c);
  2782. return c;
  2783. }
  2784. void juce_glViewport (const int w, const int h)
  2785. {
  2786. glViewport (0, 0, w, h);
  2787. }
  2788. void OpenGLPixelFormat::getAvailablePixelFormats (Component* component,
  2789. OwnedArray <OpenGLPixelFormat>& results)
  2790. {
  2791. Component tempComp;
  2792. {
  2793. WindowedGLContext wc (component, 0, OpenGLPixelFormat (8, 8, 16, 0));
  2794. wc.makeActive();
  2795. wc.findAlternativeOpenGLPixelFormats (results);
  2796. }
  2797. }
  2798. #endif
  2799. //==============================================================================
  2800. //==============================================================================
  2801. class JuceIStorage : public IStorage
  2802. {
  2803. int refCount;
  2804. public:
  2805. JuceIStorage() : refCount (1) {}
  2806. virtual ~JuceIStorage()
  2807. {
  2808. jassert (refCount == 0);
  2809. }
  2810. HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
  2811. {
  2812. if (id == IID_IUnknown || id == IID_IStorage)
  2813. {
  2814. AddRef();
  2815. *result = this;
  2816. return S_OK;
  2817. }
  2818. *result = 0;
  2819. return E_NOINTERFACE;
  2820. }
  2821. ULONG __stdcall AddRef() { return ++refCount; }
  2822. ULONG __stdcall Release() { const int r = --refCount; if (r == 0) delete this; return r; }
  2823. HRESULT __stdcall CreateStream (const WCHAR*, DWORD, DWORD, DWORD, IStream**) { return E_NOTIMPL; }
  2824. HRESULT __stdcall OpenStream (const WCHAR*, void*, DWORD, DWORD, IStream**) { return E_NOTIMPL; }
  2825. HRESULT __stdcall CreateStorage (const WCHAR*, DWORD, DWORD, DWORD, IStorage**) { return E_NOTIMPL; }
  2826. HRESULT __stdcall OpenStorage (const WCHAR*, IStorage*, DWORD, SNB, DWORD, IStorage**) { return E_NOTIMPL; }
  2827. HRESULT __stdcall CopyTo (DWORD, IID const*, SNB, IStorage*) { return E_NOTIMPL; }
  2828. HRESULT __stdcall MoveElementTo (const OLECHAR*,IStorage*, const OLECHAR*, DWORD) { return E_NOTIMPL; }
  2829. HRESULT __stdcall Commit (DWORD) { return E_NOTIMPL; }
  2830. HRESULT __stdcall Revert() { return E_NOTIMPL; }
  2831. HRESULT __stdcall EnumElements (DWORD, void*, DWORD, IEnumSTATSTG**) { return E_NOTIMPL; }
  2832. HRESULT __stdcall DestroyElement (const OLECHAR*) { return E_NOTIMPL; }
  2833. HRESULT __stdcall RenameElement (const WCHAR*, const WCHAR*) { return E_NOTIMPL; }
  2834. HRESULT __stdcall SetElementTimes (const WCHAR*, FILETIME const*, FILETIME const*, FILETIME const*) { return E_NOTIMPL; }
  2835. HRESULT __stdcall SetClass (REFCLSID) { return S_OK; }
  2836. HRESULT __stdcall SetStateBits (DWORD, DWORD) { return E_NOTIMPL; }
  2837. HRESULT __stdcall Stat (STATSTG*, DWORD) { return E_NOTIMPL; }
  2838. juce_UseDebuggingNewOperator
  2839. };
  2840. class JuceOleInPlaceFrame : public IOleInPlaceFrame
  2841. {
  2842. int refCount;
  2843. HWND window;
  2844. public:
  2845. JuceOleInPlaceFrame (HWND window_)
  2846. : refCount (1),
  2847. window (window_)
  2848. {
  2849. }
  2850. virtual ~JuceOleInPlaceFrame()
  2851. {
  2852. jassert (refCount == 0);
  2853. }
  2854. HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
  2855. {
  2856. if (id == IID_IUnknown || id == IID_IOleInPlaceFrame)
  2857. {
  2858. AddRef();
  2859. *result = this;
  2860. return S_OK;
  2861. }
  2862. *result = 0;
  2863. return E_NOINTERFACE;
  2864. }
  2865. ULONG __stdcall AddRef() { return ++refCount; }
  2866. ULONG __stdcall Release() { const int r = --refCount; if (r == 0) delete this; return r; }
  2867. HRESULT __stdcall GetWindow (HWND* lphwnd) { *lphwnd = window; return S_OK; }
  2868. HRESULT __stdcall ContextSensitiveHelp (BOOL) { return E_NOTIMPL; }
  2869. HRESULT __stdcall GetBorder (LPRECT) { return E_NOTIMPL; }
  2870. HRESULT __stdcall RequestBorderSpace (LPCBORDERWIDTHS) { return E_NOTIMPL; }
  2871. HRESULT __stdcall SetBorderSpace (LPCBORDERWIDTHS) { return E_NOTIMPL; }
  2872. HRESULT __stdcall SetActiveObject (IOleInPlaceActiveObject*, LPCOLESTR) { return S_OK; }
  2873. HRESULT __stdcall InsertMenus (HMENU, LPOLEMENUGROUPWIDTHS) { return E_NOTIMPL; }
  2874. HRESULT __stdcall SetMenu (HMENU, HOLEMENU, HWND) { return S_OK; }
  2875. HRESULT __stdcall RemoveMenus (HMENU) { return E_NOTIMPL; }
  2876. HRESULT __stdcall SetStatusText (LPCOLESTR) { return S_OK; }
  2877. HRESULT __stdcall EnableModeless (BOOL) { return S_OK; }
  2878. HRESULT __stdcall TranslateAccelerator(LPMSG, WORD) { return E_NOTIMPL; }
  2879. juce_UseDebuggingNewOperator
  2880. };
  2881. class JuceIOleInPlaceSite : public IOleInPlaceSite
  2882. {
  2883. int refCount;
  2884. HWND window;
  2885. JuceOleInPlaceFrame* frame;
  2886. public:
  2887. JuceIOleInPlaceSite (HWND window_)
  2888. : refCount (1),
  2889. window (window_)
  2890. {
  2891. frame = new JuceOleInPlaceFrame (window);
  2892. }
  2893. virtual ~JuceIOleInPlaceSite()
  2894. {
  2895. jassert (refCount == 0);
  2896. frame->Release();
  2897. }
  2898. HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
  2899. {
  2900. if (id == IID_IUnknown || id == IID_IOleInPlaceSite)
  2901. {
  2902. AddRef();
  2903. *result = this;
  2904. return S_OK;
  2905. }
  2906. *result = 0;
  2907. return E_NOINTERFACE;
  2908. }
  2909. ULONG __stdcall AddRef() { return ++refCount; }
  2910. ULONG __stdcall Release() { const int r = --refCount; if (r == 0) delete this; return r; }
  2911. HRESULT __stdcall GetWindow (HWND* lphwnd) { *lphwnd = window; return S_OK; }
  2912. HRESULT __stdcall ContextSensitiveHelp (BOOL) { return E_NOTIMPL; }
  2913. HRESULT __stdcall CanInPlaceActivate() { return S_OK; }
  2914. HRESULT __stdcall OnInPlaceActivate() { return S_OK; }
  2915. HRESULT __stdcall OnUIActivate() { return S_OK; }
  2916. HRESULT __stdcall GetWindowContext (LPOLEINPLACEFRAME* lplpFrame, LPOLEINPLACEUIWINDOW* lplpDoc, LPRECT, LPRECT, LPOLEINPLACEFRAMEINFO lpFrameInfo)
  2917. {
  2918. frame->AddRef();
  2919. *lplpFrame = frame;
  2920. *lplpDoc = 0;
  2921. lpFrameInfo->fMDIApp = FALSE;
  2922. lpFrameInfo->hwndFrame = window;
  2923. lpFrameInfo->haccel = 0;
  2924. lpFrameInfo->cAccelEntries = 0;
  2925. return S_OK;
  2926. }
  2927. HRESULT __stdcall Scroll (SIZE) { return E_NOTIMPL; }
  2928. HRESULT __stdcall OnUIDeactivate (BOOL) { return S_OK; }
  2929. HRESULT __stdcall OnInPlaceDeactivate() { return S_OK; }
  2930. HRESULT __stdcall DiscardUndoState() { return E_NOTIMPL; }
  2931. HRESULT __stdcall DeactivateAndUndo() { return E_NOTIMPL; }
  2932. HRESULT __stdcall OnPosRectChange (LPCRECT) { return S_OK; }
  2933. juce_UseDebuggingNewOperator
  2934. };
  2935. class JuceIOleClientSite : public IOleClientSite
  2936. {
  2937. int refCount;
  2938. JuceIOleInPlaceSite* inplaceSite;
  2939. public:
  2940. JuceIOleClientSite (HWND window)
  2941. : refCount (1)
  2942. {
  2943. inplaceSite = new JuceIOleInPlaceSite (window);
  2944. }
  2945. virtual ~JuceIOleClientSite()
  2946. {
  2947. jassert (refCount == 0);
  2948. inplaceSite->Release();
  2949. }
  2950. HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
  2951. {
  2952. if (id == IID_IUnknown || id == IID_IOleClientSite)
  2953. {
  2954. AddRef();
  2955. *result = this;
  2956. return S_OK;
  2957. }
  2958. else if (id == IID_IOleInPlaceSite)
  2959. {
  2960. inplaceSite->AddRef();
  2961. *result = inplaceSite;
  2962. return S_OK;
  2963. }
  2964. *result = 0;
  2965. return E_NOINTERFACE;
  2966. }
  2967. ULONG __stdcall AddRef() { return ++refCount; }
  2968. ULONG __stdcall Release() { const int r = --refCount; if (r == 0) delete this; return r; }
  2969. HRESULT __stdcall SaveObject() { return E_NOTIMPL; }
  2970. HRESULT __stdcall GetMoniker (DWORD, DWORD, IMoniker**) { return E_NOTIMPL; }
  2971. HRESULT __stdcall GetContainer (LPOLECONTAINER* ppContainer) { *ppContainer = 0; return E_NOINTERFACE; }
  2972. HRESULT __stdcall ShowObject() { return S_OK; }
  2973. HRESULT __stdcall OnShowWindow (BOOL) { return E_NOTIMPL; }
  2974. HRESULT __stdcall RequestNewObjectLayout() { return E_NOTIMPL; }
  2975. juce_UseDebuggingNewOperator
  2976. };
  2977. //==============================================================================
  2978. class ActiveXControlData : public ComponentMovementWatcher
  2979. {
  2980. ActiveXControlComponent* const owner;
  2981. bool wasShowing;
  2982. public:
  2983. IStorage* storage;
  2984. IOleClientSite* clientSite;
  2985. IOleObject* control;
  2986. //==============================================================================
  2987. ActiveXControlData (HWND hwnd,
  2988. ActiveXControlComponent* const owner_)
  2989. : ComponentMovementWatcher (owner_),
  2990. owner (owner_),
  2991. wasShowing (owner_ != 0 && owner_->isShowing()),
  2992. storage (new JuceIStorage()),
  2993. clientSite (new JuceIOleClientSite (hwnd)),
  2994. control (0)
  2995. {
  2996. }
  2997. ~ActiveXControlData()
  2998. {
  2999. if (control != 0)
  3000. {
  3001. control->Close (OLECLOSE_NOSAVE);
  3002. control->Release();
  3003. }
  3004. clientSite->Release();
  3005. storage->Release();
  3006. }
  3007. //==============================================================================
  3008. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/)
  3009. {
  3010. Component* const topComp = owner->getTopLevelComponent();
  3011. if (topComp->getPeer() != 0)
  3012. {
  3013. int x = 0, y = 0;
  3014. owner->relativePositionToOtherComponent (topComp, x, y);
  3015. owner->setControlBounds (Rectangle (x, y, owner->getWidth(), owner->getHeight()));
  3016. }
  3017. }
  3018. void componentPeerChanged()
  3019. {
  3020. const bool isShowingNow = owner->isShowing();
  3021. if (wasShowing != isShowingNow)
  3022. {
  3023. wasShowing = isShowingNow;
  3024. owner->setControlVisible (isShowingNow);
  3025. }
  3026. }
  3027. void componentVisibilityChanged (Component&)
  3028. {
  3029. componentPeerChanged();
  3030. }
  3031. };
  3032. //==============================================================================
  3033. static VoidArray activeXComps;
  3034. static HWND getHWND (const ActiveXControlComponent* const component)
  3035. {
  3036. HWND hwnd = 0;
  3037. const IID iid = IID_IOleWindow;
  3038. IOleWindow* const window = (IOleWindow*) component->queryInterface (&iid);
  3039. if (window != 0)
  3040. {
  3041. window->GetWindow (&hwnd);
  3042. window->Release();
  3043. }
  3044. return hwnd;
  3045. }
  3046. static void offerActiveXMouseEventToPeer (ComponentPeer* const peer, HWND hwnd, UINT message, LPARAM lParam)
  3047. {
  3048. RECT activeXRect, peerRect;
  3049. GetWindowRect (hwnd, &activeXRect);
  3050. GetWindowRect ((HWND) peer->getNativeHandle(), &peerRect);
  3051. const int mx = GET_X_LPARAM (lParam) + activeXRect.left - peerRect.left;
  3052. const int my = GET_Y_LPARAM (lParam) + activeXRect.top - peerRect.top;
  3053. const int64 mouseEventTime = getMouseEventTime();
  3054. const int oldModifiers = currentModifiers;
  3055. ModifierKeys::getCurrentModifiersRealtime(); // to update the mouse button flags
  3056. switch (message)
  3057. {
  3058. case WM_MOUSEMOVE:
  3059. if (ModifierKeys (currentModifiers).isAnyMouseButtonDown())
  3060. peer->handleMouseDrag (mx, my, mouseEventTime);
  3061. else
  3062. peer->handleMouseMove (mx, my, mouseEventTime);
  3063. break;
  3064. case WM_LBUTTONDOWN:
  3065. case WM_MBUTTONDOWN:
  3066. case WM_RBUTTONDOWN:
  3067. peer->handleMouseDown (mx, my, mouseEventTime);
  3068. break;
  3069. case WM_LBUTTONUP:
  3070. case WM_MBUTTONUP:
  3071. case WM_RBUTTONUP:
  3072. peer->handleMouseUp (oldModifiers, mx, my, mouseEventTime);
  3073. break;
  3074. default:
  3075. break;
  3076. }
  3077. }
  3078. // intercepts events going to an activeX control, so we can sneakily use the mouse events
  3079. static LRESULT CALLBACK activeXHookWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  3080. {
  3081. for (int i = activeXComps.size(); --i >= 0;)
  3082. {
  3083. const ActiveXControlComponent* const ax = (const ActiveXControlComponent*) activeXComps.getUnchecked(i);
  3084. HWND controlHWND = getHWND (ax);
  3085. if (controlHWND == hwnd)
  3086. {
  3087. switch (message)
  3088. {
  3089. case WM_MOUSEMOVE:
  3090. case WM_LBUTTONDOWN:
  3091. case WM_MBUTTONDOWN:
  3092. case WM_RBUTTONDOWN:
  3093. case WM_LBUTTONUP:
  3094. case WM_MBUTTONUP:
  3095. case WM_RBUTTONUP:
  3096. case WM_LBUTTONDBLCLK:
  3097. case WM_MBUTTONDBLCLK:
  3098. case WM_RBUTTONDBLCLK:
  3099. if (ax->isShowing())
  3100. {
  3101. ComponentPeer* const peer = ax->getPeer();
  3102. if (peer != 0)
  3103. {
  3104. offerActiveXMouseEventToPeer (peer, hwnd, message, lParam);
  3105. if (! ax->areMouseEventsAllowed())
  3106. return 0;
  3107. }
  3108. }
  3109. break;
  3110. default:
  3111. break;
  3112. }
  3113. return CallWindowProc ((WNDPROC) (ax->originalWndProc), hwnd, message, wParam, lParam);
  3114. }
  3115. }
  3116. return DefWindowProc (hwnd, message, wParam, lParam);
  3117. }
  3118. ActiveXControlComponent::ActiveXControlComponent()
  3119. : originalWndProc (0),
  3120. control (0),
  3121. mouseEventsAllowed (true)
  3122. {
  3123. activeXComps.add (this);
  3124. }
  3125. ActiveXControlComponent::~ActiveXControlComponent()
  3126. {
  3127. deleteControl();
  3128. activeXComps.removeValue (this);
  3129. }
  3130. void ActiveXControlComponent::paint (Graphics& g)
  3131. {
  3132. if (control == 0)
  3133. g.fillAll (Colours::lightgrey);
  3134. }
  3135. bool ActiveXControlComponent::createControl (const void* controlIID)
  3136. {
  3137. deleteControl();
  3138. ComponentPeer* const peer = getPeer();
  3139. // the component must have already been added to a real window when you call this!
  3140. jassert (dynamic_cast <Win32ComponentPeer*> (peer) != 0);
  3141. if (dynamic_cast <Win32ComponentPeer*> (peer) != 0)
  3142. {
  3143. int x = 0, y = 0;
  3144. relativePositionToOtherComponent (getTopLevelComponent(), x, y);
  3145. HWND hwnd = (HWND) peer->getNativeHandle();
  3146. ActiveXControlData* const info = new ActiveXControlData (hwnd, this);
  3147. HRESULT hr;
  3148. if ((hr = OleCreate (*(const IID*) controlIID, IID_IOleObject, 1 /*OLERENDER_DRAW*/, 0,
  3149. info->clientSite, info->storage,
  3150. (void**) &(info->control))) == S_OK)
  3151. {
  3152. info->control->SetHostNames (L"Juce", 0);
  3153. if (OleSetContainedObject (info->control, TRUE) == S_OK)
  3154. {
  3155. RECT rect;
  3156. rect.left = x;
  3157. rect.top = y;
  3158. rect.right = x + getWidth();
  3159. rect.bottom = y + getHeight();
  3160. if (info->control->DoVerb (OLEIVERB_SHOW, 0, info->clientSite, 0, hwnd, &rect) == S_OK)
  3161. {
  3162. control = info;
  3163. setControlBounds (Rectangle (x, y, getWidth(), getHeight()));
  3164. HWND controlHWND = getHWND (this);
  3165. if (controlHWND != 0)
  3166. {
  3167. originalWndProc = (void*) GetWindowLongPtr (controlHWND, GWLP_WNDPROC);
  3168. SetWindowLongPtr (controlHWND, GWLP_WNDPROC, (LONG_PTR) activeXHookWndProc);
  3169. }
  3170. return true;
  3171. }
  3172. }
  3173. }
  3174. delete info;
  3175. }
  3176. return false;
  3177. }
  3178. void ActiveXControlComponent::deleteControl()
  3179. {
  3180. ActiveXControlData* const info = (ActiveXControlData*) control;
  3181. if (info != 0)
  3182. {
  3183. delete info;
  3184. control = 0;
  3185. originalWndProc = 0;
  3186. }
  3187. }
  3188. void* ActiveXControlComponent::queryInterface (const void* iid) const
  3189. {
  3190. ActiveXControlData* const info = (ActiveXControlData*) control;
  3191. void* result = 0;
  3192. if (info != 0 && info->control != 0
  3193. && info->control->QueryInterface (*(const IID*) iid, &result) == S_OK)
  3194. return result;
  3195. return 0;
  3196. }
  3197. void ActiveXControlComponent::setControlBounds (const Rectangle& newBounds) const
  3198. {
  3199. HWND hwnd = getHWND (this);
  3200. if (hwnd != 0)
  3201. MoveWindow (hwnd, newBounds.getX(), newBounds.getY(), newBounds.getWidth(), newBounds.getHeight(), TRUE);
  3202. }
  3203. void ActiveXControlComponent::setControlVisible (const bool shouldBeVisible) const
  3204. {
  3205. HWND hwnd = getHWND (this);
  3206. if (hwnd != 0)
  3207. ShowWindow (hwnd, shouldBeVisible ? SW_SHOWNA : SW_HIDE);
  3208. }
  3209. void ActiveXControlComponent::setMouseEventsAllowed (const bool eventsCanReachControl)
  3210. {
  3211. mouseEventsAllowed = eventsCanReachControl;
  3212. }
  3213. END_JUCE_NAMESPACE