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.

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