Audio plugin host https://kx.studio/carla
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.

juce_mac_NSViewComponentPeer.mm 82KB

8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
9 years ago
10 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
10 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. typedef void (*AppFocusChangeCallback)();
  20. extern AppFocusChangeCallback appFocusChangeCallback;
  21. typedef bool (*CheckEventBlockedByModalComps) (NSEvent*);
  22. extern CheckEventBlockedByModalComps isEventBlockedByModalComps;
  23. //==============================================================================
  24. #if ! (defined (MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7)
  25. } // (juce namespace)
  26. @interface NSEvent (JuceDeviceDelta)
  27. - (CGFloat) scrollingDeltaX;
  28. - (CGFloat) scrollingDeltaY;
  29. - (BOOL) hasPreciseScrollingDeltas;
  30. - (BOOL) isDirectionInvertedFromDevice;
  31. @end
  32. namespace juce {
  33. #endif
  34. //==============================================================================
  35. static CGFloat getMainScreenHeight() noexcept
  36. {
  37. return [[[NSScreen screens] objectAtIndex: 0] frame].size.height;
  38. }
  39. static void flipScreenRect (NSRect& r) noexcept
  40. {
  41. r.origin.y = getMainScreenHeight() - (r.origin.y + r.size.height);
  42. }
  43. static NSRect flippedScreenRect (NSRect r) noexcept
  44. {
  45. flipScreenRect (r);
  46. return r;
  47. }
  48. #if JUCE_MODULE_AVAILABLE_juce_opengl
  49. void componentPeerAboutToChange (Component&, bool);
  50. #endif
  51. //==============================================================================
  52. class NSViewComponentPeer : public ComponentPeer,
  53. private Timer
  54. {
  55. public:
  56. NSViewComponentPeer (Component& comp, const int windowStyleFlags, NSView* viewToAttachTo)
  57. : ComponentPeer (comp, windowStyleFlags),
  58. isSharedWindow (viewToAttachTo != nil),
  59. lastRepaintTime (Time::getMillisecondCounter())
  60. {
  61. appFocusChangeCallback = appFocusChanged;
  62. isEventBlockedByModalComps = checkEventBlockedByModalComps;
  63. NSRect r = makeNSRect (component.getLocalBounds());
  64. view = [createViewInstance() initWithFrame: r];
  65. setOwner (view, this);
  66. [view registerForDraggedTypes: getSupportedDragTypes()];
  67. notificationCenter = [NSNotificationCenter defaultCenter];
  68. [notificationCenter addObserver: view
  69. selector: @selector (frameChanged:)
  70. name: NSViewFrameDidChangeNotification
  71. object: view];
  72. if (! isSharedWindow)
  73. {
  74. [notificationCenter addObserver: view
  75. selector: @selector (frameChanged:)
  76. name: NSWindowDidMoveNotification
  77. object: window];
  78. [notificationCenter addObserver: view
  79. selector: @selector (frameChanged:)
  80. name: NSWindowDidMiniaturizeNotification
  81. object: window];
  82. [notificationCenter addObserver: view
  83. selector: @selector (frameChanged:)
  84. name: NSWindowDidDeminiaturizeNotification
  85. object: window];
  86. }
  87. [view setPostsFrameChangedNotifications: YES];
  88. if (isSharedWindow)
  89. {
  90. window = [viewToAttachTo window];
  91. [viewToAttachTo addSubview: view];
  92. }
  93. else
  94. {
  95. r.origin.x = (CGFloat) component.getX();
  96. r.origin.y = (CGFloat) component.getY();
  97. flipScreenRect (r);
  98. window = [createWindowInstance() initWithContentRect: r
  99. styleMask: getNSWindowStyleMask (windowStyleFlags)
  100. backing: NSBackingStoreBuffered
  101. defer: YES];
  102. setOwner (window, this);
  103. [window orderOut: nil];
  104. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  105. [window setDelegate: (id<NSWindowDelegate>) window];
  106. #else
  107. [window setDelegate: window];
  108. #endif
  109. [window setOpaque: component.isOpaque()];
  110. [window setHasShadow: ((windowStyleFlags & windowHasDropShadow) != 0)];
  111. if (component.isAlwaysOnTop())
  112. setAlwaysOnTop (true);
  113. [window setContentView: view];
  114. [window setAutodisplay: YES];
  115. [window setAcceptsMouseMovedEvents: YES];
  116. // We'll both retain and also release this on closing because plugin hosts can unexpectedly
  117. // close the window for us, and also tend to get cause trouble if setReleasedWhenClosed is NO.
  118. [window setReleasedWhenClosed: YES];
  119. [window retain];
  120. [window setExcludedFromWindowsMenu: (windowStyleFlags & windowIsTemporary) != 0];
  121. [window setIgnoresMouseEvents: (windowStyleFlags & windowIgnoresMouseClicks) != 0];
  122. #if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
  123. if ((windowStyleFlags & (windowHasMaximiseButton | windowHasTitleBar)) == (windowHasMaximiseButton | windowHasTitleBar))
  124. [window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary];
  125. if ([window respondsToSelector: @selector (setRestorable:)])
  126. [window setRestorable: NO];
  127. #endif
  128. }
  129. const float alpha = component.getAlpha();
  130. if (alpha < 1.0f)
  131. setAlpha (alpha);
  132. setTitle (component.getName());
  133. }
  134. ~NSViewComponentPeer()
  135. {
  136. [notificationCenter removeObserver: view];
  137. setOwner (view, nullptr);
  138. if ([view superview] != nil)
  139. {
  140. redirectWillMoveToWindow (nullptr);
  141. [view removeFromSuperview];
  142. }
  143. [view release];
  144. if (! isSharedWindow)
  145. {
  146. setOwner (window, nullptr);
  147. [window close];
  148. [window release];
  149. }
  150. }
  151. //==============================================================================
  152. void* getNativeHandle() const override { return view; }
  153. void setVisible (bool shouldBeVisible) override
  154. {
  155. if (isSharedWindow)
  156. {
  157. [view setHidden: ! shouldBeVisible];
  158. }
  159. else
  160. {
  161. if (shouldBeVisible)
  162. {
  163. ++insideToFrontCall;
  164. [window orderFront: nil];
  165. --insideToFrontCall;
  166. handleBroughtToFront();
  167. }
  168. else
  169. {
  170. [window orderOut: nil];
  171. }
  172. }
  173. }
  174. void setTitle (const String& title) override
  175. {
  176. JUCE_AUTORELEASEPOOL
  177. {
  178. if (! isSharedWindow)
  179. [window setTitle: juceStringToNS (title)];
  180. }
  181. }
  182. bool setDocumentEditedStatus (bool edited) override
  183. {
  184. if (! hasNativeTitleBar())
  185. return false;
  186. [window setDocumentEdited: edited];
  187. return true;
  188. }
  189. void setRepresentedFile (const File& file) override
  190. {
  191. if (! isSharedWindow)
  192. [window setRepresentedFilename: juceStringToNS (file != File()
  193. ? file.getFullPathName()
  194. : String())];
  195. }
  196. void setBounds (const Rectangle<int>& newBounds, bool isNowFullScreen) override
  197. {
  198. fullScreen = isNowFullScreen;
  199. NSRect r = makeNSRect (newBounds);
  200. NSSize oldViewSize = [view frame].size;
  201. if (isSharedWindow)
  202. {
  203. r.origin.y = [[view superview] frame].size.height - (r.origin.y + r.size.height);
  204. [view setFrame: r];
  205. }
  206. else
  207. {
  208. // Repaint behaviour of setFrame seemed to change in 10.11, and the drawing became synchronous,
  209. // causing performance issues. But sending an async update causes flickering in older versions,
  210. // hence this version check to use the old behaviour on pre 10.11 machines
  211. static bool isPre10_11 = SystemStats::getOperatingSystemType() <= SystemStats::MacOSX_10_10;
  212. [window setFrame: [window frameRectForContentRect: flippedScreenRect (r)]
  213. display: isPre10_11];
  214. }
  215. if (oldViewSize.width != r.size.width || oldViewSize.height != r.size.height)
  216. [view setNeedsDisplay: true];
  217. }
  218. Rectangle<int> getBounds (const bool global) const
  219. {
  220. NSRect r = [view frame];
  221. NSWindow* viewWindow = [view window];
  222. if (global && viewWindow != nil)
  223. {
  224. r = [[view superview] convertRect: r toView: nil];
  225. #if defined (MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7
  226. r = [viewWindow convertRectToScreen: r];
  227. #else
  228. r.origin = [viewWindow convertBaseToScreen: r.origin];
  229. #endif
  230. flipScreenRect (r);
  231. }
  232. else
  233. {
  234. r.origin.y = [[view superview] frame].size.height - r.origin.y - r.size.height;
  235. }
  236. return convertToRectInt (r);
  237. }
  238. Rectangle<int> getBounds() const override
  239. {
  240. return getBounds (! isSharedWindow);
  241. }
  242. Point<float> localToGlobal (Point<float> relativePosition) override
  243. {
  244. return relativePosition + getBounds (true).getPosition().toFloat();
  245. }
  246. Point<float> globalToLocal (Point<float> screenPosition) override
  247. {
  248. return screenPosition - getBounds (true).getPosition().toFloat();
  249. }
  250. void setAlpha (float newAlpha) override
  251. {
  252. if (isSharedWindow)
  253. [view setAlphaValue: (CGFloat) newAlpha];
  254. else
  255. [window setAlphaValue: (CGFloat) newAlpha];
  256. }
  257. void setMinimised (bool shouldBeMinimised) override
  258. {
  259. if (! isSharedWindow)
  260. {
  261. if (shouldBeMinimised)
  262. [window miniaturize: nil];
  263. else
  264. [window deminiaturize: nil];
  265. }
  266. }
  267. bool isMinimised() const override
  268. {
  269. return [window isMiniaturized];
  270. }
  271. void setFullScreen (bool shouldBeFullScreen) override
  272. {
  273. if (! isSharedWindow)
  274. {
  275. Rectangle<int> r (lastNonFullscreenBounds);
  276. if (isMinimised())
  277. setMinimised (false);
  278. if (fullScreen != shouldBeFullScreen)
  279. {
  280. if (shouldBeFullScreen && hasNativeTitleBar())
  281. {
  282. fullScreen = true;
  283. [window performZoom: nil];
  284. }
  285. else
  286. {
  287. if (shouldBeFullScreen)
  288. r = component.getParentMonitorArea();
  289. // (can't call the component's setBounds method because that'll reset our fullscreen flag)
  290. if (r != component.getBounds() && ! r.isEmpty())
  291. setBounds (ScalingHelpers::scaledScreenPosToUnscaled (component, r), shouldBeFullScreen);
  292. }
  293. }
  294. }
  295. }
  296. bool isFullScreen() const override
  297. {
  298. return fullScreen;
  299. }
  300. bool isKioskMode() const override
  301. {
  302. #if defined (MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
  303. if (hasNativeTitleBar() && ([window styleMask] & NSWindowStyleMaskFullScreen) != 0)
  304. return true;
  305. #endif
  306. return ComponentPeer::isKioskMode();
  307. }
  308. static bool isWindowAtPoint (NSWindow* w, NSPoint screenPoint)
  309. {
  310. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  311. if ([NSWindow respondsToSelector: @selector (windowNumberAtPoint:belowWindowWithWindowNumber:)])
  312. return [NSWindow windowNumberAtPoint: screenPoint belowWindowWithWindowNumber: 0] == [w windowNumber];
  313. #endif
  314. return true;
  315. }
  316. bool contains (Point<int> localPos, bool trueIfInAChildWindow) const override
  317. {
  318. NSRect viewFrame = [view frame];
  319. if (! (isPositiveAndBelow (localPos.getX(), (int) viewFrame.size.width)
  320. && isPositiveAndBelow (localPos.getY(), (int) viewFrame.size.height)))
  321. return false;
  322. if (! SystemStats::isRunningInAppExtensionSandbox())
  323. {
  324. if (NSWindow* const viewWindow = [view window])
  325. {
  326. const NSRect windowFrame = [viewWindow frame];
  327. const NSPoint windowPoint = [view convertPoint: NSMakePoint (localPos.x, viewFrame.size.height - localPos.y) toView: nil];
  328. const NSPoint screenPoint = NSMakePoint (windowFrame.origin.x + windowPoint.x,
  329. windowFrame.origin.y + windowPoint.y);
  330. if (! isWindowAtPoint (viewWindow, screenPoint))
  331. return false;
  332. }
  333. }
  334. NSView* v = [view hitTest: NSMakePoint (viewFrame.origin.x + localPos.getX(),
  335. viewFrame.origin.y + viewFrame.size.height - localPos.getY())];
  336. return trueIfInAChildWindow ? (v != nil)
  337. : (v == view);
  338. }
  339. BorderSize<int> getFrameSize() const override
  340. {
  341. BorderSize<int> b;
  342. if (! isSharedWindow)
  343. {
  344. NSRect v = [view convertRect: [view frame] toView: nil];
  345. NSRect w = [window frame];
  346. b.setTop ((int) (w.size.height - (v.origin.y + v.size.height)));
  347. b.setBottom ((int) v.origin.y);
  348. b.setLeft ((int) v.origin.x);
  349. b.setRight ((int) (w.size.width - (v.origin.x + v.size.width)));
  350. }
  351. return b;
  352. }
  353. void updateFullscreenStatus()
  354. {
  355. if (hasNativeTitleBar())
  356. {
  357. const Rectangle<int> screen (getFrameSize().subtractedFrom (component.getParentMonitorArea()));
  358. fullScreen = component.getScreenBounds().expanded (2, 2).contains (screen);
  359. }
  360. }
  361. bool hasNativeTitleBar() const
  362. {
  363. return (getStyleFlags() & windowHasTitleBar) != 0;
  364. }
  365. bool setAlwaysOnTop (bool alwaysOnTop) override
  366. {
  367. if (! isSharedWindow)
  368. [window setLevel: alwaysOnTop ? ((getStyleFlags() & windowIsTemporary) != 0 ? NSPopUpMenuWindowLevel
  369. : NSFloatingWindowLevel)
  370. : NSNormalWindowLevel];
  371. return true;
  372. }
  373. void toFront (bool makeActiveWindow) override
  374. {
  375. if (isSharedWindow)
  376. [[view superview] addSubview: view
  377. positioned: NSWindowAbove
  378. relativeTo: nil];
  379. if (window != nil && component.isVisible())
  380. {
  381. ++insideToFrontCall;
  382. if (makeActiveWindow)
  383. [window makeKeyAndOrderFront: nil];
  384. else
  385. [window orderFront: nil];
  386. if (insideToFrontCall <= 1)
  387. {
  388. Desktop::getInstance().getMainMouseSource().forceMouseCursorUpdate();
  389. handleBroughtToFront();
  390. }
  391. --insideToFrontCall;
  392. }
  393. }
  394. void toBehind (ComponentPeer* other) override
  395. {
  396. if (auto* otherPeer = dynamic_cast<NSViewComponentPeer*> (other))
  397. {
  398. if (isSharedWindow)
  399. {
  400. [[view superview] addSubview: view
  401. positioned: NSWindowBelow
  402. relativeTo: otherPeer->view];
  403. }
  404. else if (component.isVisible())
  405. {
  406. [window orderWindow: NSWindowBelow
  407. relativeTo: [otherPeer->window windowNumber]];
  408. }
  409. }
  410. else
  411. {
  412. jassertfalse; // wrong type of window?
  413. }
  414. }
  415. void setIcon (const Image&) override
  416. {
  417. // to do..
  418. }
  419. StringArray getAvailableRenderingEngines() override
  420. {
  421. StringArray s ("Software Renderer");
  422. #if USE_COREGRAPHICS_RENDERING
  423. s.add ("CoreGraphics Renderer");
  424. #endif
  425. return s;
  426. }
  427. int getCurrentRenderingEngine() const override
  428. {
  429. return usingCoreGraphics ? 1 : 0;
  430. }
  431. void setCurrentRenderingEngine (int index) override
  432. {
  433. #if USE_COREGRAPHICS_RENDERING
  434. if (usingCoreGraphics != (index > 0))
  435. {
  436. usingCoreGraphics = index > 0;
  437. [view setNeedsDisplay: true];
  438. }
  439. #else
  440. ignoreUnused (index);
  441. #endif
  442. }
  443. void redirectMouseDown (NSEvent* ev)
  444. {
  445. if (! Process::isForegroundProcess())
  446. Process::makeForegroundProcess();
  447. currentModifiers = currentModifiers.withFlags (getModifierForButtonNumber ([ev buttonNumber]));
  448. sendMouseEvent (ev);
  449. }
  450. void redirectMouseUp (NSEvent* ev)
  451. {
  452. currentModifiers = currentModifiers.withoutFlags (getModifierForButtonNumber ([ev buttonNumber]));
  453. sendMouseEvent (ev);
  454. showArrowCursorIfNeeded();
  455. }
  456. void redirectMouseDrag (NSEvent* ev)
  457. {
  458. currentModifiers = currentModifiers.withFlags (getModifierForButtonNumber ([ev buttonNumber]));
  459. sendMouseEvent (ev);
  460. }
  461. void redirectMouseMove (NSEvent* ev)
  462. {
  463. currentModifiers = currentModifiers.withoutMouseButtons();
  464. NSPoint windowPos = [ev locationInWindow];
  465. #if defined (MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7
  466. NSPoint screenPos = [[ev window] convertRectToScreen: NSMakeRect (windowPos.x, windowPos.y, 1.0f, 1.0f)].origin;
  467. #else
  468. NSPoint screenPos = [[ev window] convertBaseToScreen: windowPos];
  469. #endif
  470. if (isWindowAtPoint ([ev window], screenPos))
  471. sendMouseEvent (ev);
  472. else
  473. // moved into another window which overlaps this one, so trigger an exit
  474. handleMouseEvent (MouseInputSource::InputSourceType::mouse, { -1.0f, -1.0f }, currentModifiers,
  475. getMousePressure (ev), MouseInputSource::invalidOrientation, getMouseTime (ev));
  476. showArrowCursorIfNeeded();
  477. }
  478. void redirectMouseEnter (NSEvent* ev)
  479. {
  480. Desktop::getInstance().getMainMouseSource().forceMouseCursorUpdate();
  481. currentModifiers = currentModifiers.withoutMouseButtons();
  482. sendMouseEvent (ev);
  483. }
  484. void redirectMouseExit (NSEvent* ev)
  485. {
  486. currentModifiers = currentModifiers.withoutMouseButtons();
  487. sendMouseEvent (ev);
  488. }
  489. static float checkDeviceDeltaReturnValue (float v) noexcept
  490. {
  491. // (deviceDeltaX can fail and return NaN, so need to sanity-check the result)
  492. v *= 0.5f / 256.0f;
  493. return (v > -1000.0f && v < 1000.0f) ? v : 0.0f;
  494. }
  495. void redirectMouseWheel (NSEvent* ev)
  496. {
  497. updateModifiers (ev);
  498. MouseWheelDetails wheel;
  499. wheel.deltaX = 0;
  500. wheel.deltaY = 0;
  501. wheel.isReversed = false;
  502. wheel.isSmooth = false;
  503. wheel.isInertial = false;
  504. @try
  505. {
  506. #if defined (MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
  507. if ([ev respondsToSelector: @selector (isDirectionInvertedFromDevice)])
  508. wheel.isReversed = [ev isDirectionInvertedFromDevice];
  509. wheel.isInertial = ([ev momentumPhase] != NSEventPhaseNone);
  510. if ([ev respondsToSelector: @selector (hasPreciseScrollingDeltas)])
  511. {
  512. if ([ev hasPreciseScrollingDeltas])
  513. {
  514. const float scale = 0.5f / 256.0f;
  515. wheel.deltaX = scale * (float) [ev scrollingDeltaX];
  516. wheel.deltaY = scale * (float) [ev scrollingDeltaY];
  517. wheel.isSmooth = true;
  518. }
  519. }
  520. else
  521. #endif
  522. if ([ev respondsToSelector: @selector (deviceDeltaX)])
  523. {
  524. wheel.deltaX = checkDeviceDeltaReturnValue ((float) getMsgSendFPRetFn() (ev, @selector (deviceDeltaX)));
  525. wheel.deltaY = checkDeviceDeltaReturnValue ((float) getMsgSendFPRetFn() (ev, @selector (deviceDeltaY)));
  526. }
  527. }
  528. @catch (...)
  529. {}
  530. if (wheel.deltaX == 0.0f && wheel.deltaY == 0.0f)
  531. {
  532. const float scale = 10.0f / 256.0f;
  533. wheel.deltaX = scale * (float) [ev deltaX];
  534. wheel.deltaY = scale * (float) [ev deltaY];
  535. }
  536. handleMouseWheel (MouseInputSource::InputSourceType::mouse, getMousePos (ev, view), getMouseTime (ev), wheel);
  537. }
  538. void redirectMagnify (NSEvent* ev)
  539. {
  540. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  541. const float invScale = 1.0f - (float) [ev magnification];
  542. if (invScale > 0.0f)
  543. handleMagnifyGesture (MouseInputSource::InputSourceType::mouse, getMousePos (ev, view), getMouseTime (ev), 1.0f / invScale);
  544. #endif
  545. ignoreUnused (ev);
  546. }
  547. void redirectCopy (NSObject*) { handleKeyPress (KeyPress ('c', ModifierKeys (ModifierKeys::commandModifier), 'c')); }
  548. void redirectPaste (NSObject*) { handleKeyPress (KeyPress ('v', ModifierKeys (ModifierKeys::commandModifier), 'v')); }
  549. void redirectCut (NSObject*) { handleKeyPress (KeyPress ('x', ModifierKeys (ModifierKeys::commandModifier), 'x')); }
  550. void redirectWillMoveToWindow (NSWindow* newWindow)
  551. {
  552. #if JUCE_MODULE_AVAILABLE_juce_opengl
  553. if ([view window] == window)
  554. componentPeerAboutToChange (getComponent(), newWindow == nullptr);
  555. #else
  556. ignoreUnused (newWindow);
  557. #endif
  558. }
  559. void sendMouseEvent (NSEvent* ev)
  560. {
  561. updateModifiers (ev);
  562. handleMouseEvent (MouseInputSource::InputSourceType::mouse, getMousePos (ev, view), currentModifiers,
  563. getMousePressure (ev), MouseInputSource::invalidOrientation, getMouseTime (ev));
  564. }
  565. bool handleKeyEvent (NSEvent* ev, bool isKeyDown)
  566. {
  567. const String unicode (nsStringToJuce ([ev characters]));
  568. const int keyCode = getKeyCodeFromEvent (ev);
  569. #if JUCE_DEBUG_KEYCODES
  570. DBG ("unicode: " + unicode + " " + String::toHexString ((int) unicode[0]));
  571. String unmodified (nsStringToJuce ([ev charactersIgnoringModifiers]));
  572. DBG ("unmodified: " + unmodified + " " + String::toHexString ((int) unmodified[0]));
  573. #endif
  574. if (keyCode != 0 || unicode.isNotEmpty())
  575. {
  576. if (isKeyDown)
  577. {
  578. bool used = false;
  579. for (String::CharPointerType u (unicode.getCharPointer()); ! u.isEmpty();)
  580. {
  581. juce_wchar textCharacter = u.getAndAdvance();
  582. switch (keyCode)
  583. {
  584. case NSLeftArrowFunctionKey:
  585. case NSRightArrowFunctionKey:
  586. case NSUpArrowFunctionKey:
  587. case NSDownArrowFunctionKey:
  588. case NSPageUpFunctionKey:
  589. case NSPageDownFunctionKey:
  590. case NSEndFunctionKey:
  591. case NSHomeFunctionKey:
  592. case NSDeleteFunctionKey:
  593. textCharacter = 0;
  594. break; // (these all seem to generate unwanted garbage unicode strings)
  595. default:
  596. if (([ev modifierFlags] & NSEventModifierFlagCommand) != 0
  597. || (keyCode >= NSF1FunctionKey && keyCode <= NSF35FunctionKey))
  598. textCharacter = 0;
  599. break;
  600. }
  601. used = handleKeyUpOrDown (true) || used;
  602. used = handleKeyPress (keyCode, textCharacter) || used;
  603. }
  604. return used;
  605. }
  606. if (handleKeyUpOrDown (false))
  607. return true;
  608. }
  609. return false;
  610. }
  611. bool redirectKeyDown (NSEvent* ev)
  612. {
  613. // (need to retain this in case a modal loop runs in handleKeyEvent and
  614. // our event object gets lost)
  615. const NSObjectRetainer<NSEvent> r (ev);
  616. updateKeysDown (ev, true);
  617. bool used = handleKeyEvent (ev, true);
  618. if (([ev modifierFlags] & NSEventModifierFlagCommand) != 0)
  619. {
  620. // for command keys, the key-up event is thrown away, so simulate one..
  621. updateKeysDown (ev, false);
  622. used = (isValidPeer (this) && handleKeyEvent (ev, false)) || used;
  623. }
  624. // (If we're running modally, don't allow unused keystrokes to be passed
  625. // along to other blocked views..)
  626. if (Component::getCurrentlyModalComponent() != nullptr)
  627. used = true;
  628. return used;
  629. }
  630. bool redirectKeyUp (NSEvent* ev)
  631. {
  632. updateKeysDown (ev, false);
  633. return handleKeyEvent (ev, false)
  634. || Component::getCurrentlyModalComponent() != nullptr;
  635. }
  636. void redirectModKeyChange (NSEvent* ev)
  637. {
  638. // (need to retain this in case a modal loop runs and our event object gets lost)
  639. const NSObjectRetainer<NSEvent> r (ev);
  640. keysCurrentlyDown.clear();
  641. handleKeyUpOrDown (true);
  642. updateModifiers (ev);
  643. handleModifierKeysChange();
  644. }
  645. //==============================================================================
  646. void drawRect (NSRect r)
  647. {
  648. if (r.size.width < 1.0f || r.size.height < 1.0f)
  649. return;
  650. CGContextRef cg = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
  651. if (! component.isOpaque())
  652. CGContextClearRect (cg, CGContextGetClipBoundingBox (cg));
  653. float displayScale = 1.0f;
  654. #if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
  655. NSScreen* screen = [[view window] screen];
  656. if ([screen respondsToSelector: @selector (backingScaleFactor)])
  657. displayScale = (float) screen.backingScaleFactor;
  658. #endif
  659. #if USE_COREGRAPHICS_RENDERING && JUCE_COREGRAPHICS_RENDER_WITH_MULTIPLE_PAINT_CALLS
  660. // This option invokes a separate paint call for each rectangle of the clip region.
  661. // It's a long story, but this is a basically a workaround for a CGContext not having
  662. // a way of finding whether a rectangle falls within its clip region
  663. if (usingCoreGraphics)
  664. {
  665. const NSRect* rects = nullptr;
  666. NSInteger numRects = 0;
  667. [view getRectsBeingDrawn: &rects count: &numRects];
  668. if (numRects > 1)
  669. {
  670. for (int i = 0; i < numRects; ++i)
  671. {
  672. NSRect rect = rects[i];
  673. CGContextSaveGState (cg);
  674. CGContextClipToRect (cg, CGRectMake (rect.origin.x, rect.origin.y, rect.size.width, rect.size.height));
  675. drawRect (cg, rect, displayScale);
  676. CGContextRestoreGState (cg);
  677. }
  678. return;
  679. }
  680. }
  681. #endif
  682. drawRect (cg, r, displayScale);
  683. }
  684. void drawRect (CGContextRef cg, NSRect r, float displayScale)
  685. {
  686. #if USE_COREGRAPHICS_RENDERING
  687. if (usingCoreGraphics)
  688. {
  689. CoreGraphicsContext context (cg, (float) [view frame].size.height, displayScale);
  690. invokePaint (context);
  691. }
  692. else
  693. #endif
  694. {
  695. const Point<int> offset (-roundToInt (r.origin.x),
  696. -roundToInt ([view frame].size.height - (r.origin.y + r.size.height)));
  697. const int clipW = (int) (r.size.width + 0.5f);
  698. const int clipH = (int) (r.size.height + 0.5f);
  699. RectangleList<int> clip;
  700. getClipRects (clip, offset, clipW, clipH);
  701. if (! clip.isEmpty())
  702. {
  703. Image temp (component.isOpaque() ? Image::RGB : Image::ARGB,
  704. roundToInt (clipW * displayScale),
  705. roundToInt (clipH * displayScale),
  706. ! component.isOpaque());
  707. {
  708. const int intScale = roundToInt (displayScale);
  709. if (intScale != 1)
  710. clip.scaleAll (intScale);
  711. ScopedPointer<LowLevelGraphicsContext> context (component.getLookAndFeel()
  712. .createGraphicsContext (temp, offset * intScale, clip));
  713. if (intScale != 1)
  714. context->addTransform (AffineTransform::scale (displayScale));
  715. invokePaint (*context);
  716. }
  717. CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
  718. CGImageRef image = juce_createCoreGraphicsImage (temp, colourSpace, false);
  719. CGColorSpaceRelease (colourSpace);
  720. CGContextDrawImage (cg, CGRectMake (r.origin.x, r.origin.y, clipW, clipH), image);
  721. CGImageRelease (image);
  722. }
  723. }
  724. }
  725. void repaint (const Rectangle<int>& area) override
  726. {
  727. // In 10.11 changes were made to the way the OS handles repaint regions, and it seems that it can
  728. // no longer be trusted to coalesce all the regions, or to even remember them all without losing
  729. // a few when there's a lot of activity.
  730. // As a work around for this, we use a RectangleList to do our own coalescing of regions before
  731. // asynchronously asking the OS to repaint them.
  732. deferredRepaints.add ((float) area.getX(), (float) ([view frame].size.height - area.getBottom()),
  733. (float) area.getWidth(), (float) area.getHeight());
  734. if (isTimerRunning())
  735. return;
  736. const uint32 now = Time::getMillisecondCounter();
  737. uint32 msSinceLastRepaint = (lastRepaintTime >= now) ? now - lastRepaintTime
  738. : (std::numeric_limits<uint32>::max() - lastRepaintTime) + now;
  739. static uint32 minimumRepaintInterval = 1000 / 30; // 30fps
  740. // When windows are being resized, artificially throttling high-frequency repaints helps
  741. // to stop the event queue getting clogged, and keeps everything working smoothly.
  742. // For some reason Logic also needs this throttling to recored parameter events correctly.
  743. if (msSinceLastRepaint < minimumRepaintInterval && shouldThrottleRepaint())
  744. {
  745. startTimer (static_cast<int> (minimumRepaintInterval - msSinceLastRepaint));
  746. return;
  747. }
  748. setNeedsDisplayRectangles();
  749. }
  750. static bool shouldThrottleRepaint()
  751. {
  752. return areAnyWindowsInLiveResize() || ! JUCEApplication::isStandaloneApp();
  753. }
  754. void timerCallback() override
  755. {
  756. setNeedsDisplayRectangles();
  757. stopTimer();
  758. }
  759. void setNeedsDisplayRectangles()
  760. {
  761. for (auto& i : deferredRepaints)
  762. [view setNeedsDisplayInRect: makeNSRect (i)];
  763. lastRepaintTime = Time::getMillisecondCounter();
  764. deferredRepaints.clear();
  765. }
  766. void invokePaint (LowLevelGraphicsContext& context)
  767. {
  768. handlePaint (context);
  769. }
  770. void performAnyPendingRepaintsNow() override
  771. {
  772. [view displayIfNeeded];
  773. }
  774. static bool areAnyWindowsInLiveResize() noexcept
  775. {
  776. for (NSWindow* w in [NSApp windows])
  777. if ([w inLiveResize])
  778. return true;
  779. return false;
  780. }
  781. //==============================================================================
  782. bool sendModalInputAttemptIfBlocked()
  783. {
  784. if (auto* modal = Component::getCurrentlyModalComponent())
  785. {
  786. if (insideToFrontCall == 0
  787. && (! getComponent().isParentOf (modal))
  788. && getComponent().isCurrentlyBlockedByAnotherModalComponent())
  789. {
  790. modal->inputAttemptWhenModal();
  791. return true;
  792. }
  793. }
  794. return false;
  795. }
  796. bool canBecomeKeyWindow()
  797. {
  798. return (getStyleFlags() & juce::ComponentPeer::windowIgnoresKeyPresses) == 0;
  799. }
  800. bool canBecomeMainWindow()
  801. {
  802. return dynamic_cast<ResizableWindow*> (&component) != nullptr;
  803. }
  804. bool worksWhenModal() const
  805. {
  806. // In plugins, the host could put our plugin window inside a modal window, so this
  807. // allows us to successfully open other popups. Feels like there could be edge-case
  808. // problems caused by this, so let us know if you spot any issues..
  809. return ! JUCEApplication::isStandaloneApp();
  810. }
  811. void becomeKeyWindow()
  812. {
  813. handleBroughtToFront();
  814. grabFocus();
  815. }
  816. bool windowShouldClose()
  817. {
  818. if (! isValidPeer (this))
  819. return YES;
  820. handleUserClosingWindow();
  821. return NO;
  822. }
  823. void redirectMovedOrResized()
  824. {
  825. updateFullscreenStatus();
  826. handleMovedOrResized();
  827. }
  828. void viewMovedToWindow()
  829. {
  830. if (isSharedWindow)
  831. window = [view window];
  832. }
  833. void liveResizingStart()
  834. {
  835. if (constrainer != nullptr)
  836. {
  837. constrainer->resizeStart();
  838. isFirstLiveResize = true;
  839. }
  840. }
  841. void liveResizingEnd()
  842. {
  843. if (constrainer != nullptr)
  844. constrainer->resizeEnd();
  845. }
  846. NSRect constrainRect (NSRect r)
  847. {
  848. if (constrainer != nullptr && ! isKioskMode())
  849. {
  850. const float scale = getComponent().getDesktopScaleFactor();
  851. auto pos = ScalingHelpers::unscaledScreenPosToScaled (scale, convertToRectInt (flippedScreenRect (r)));
  852. auto original = ScalingHelpers::unscaledScreenPosToScaled (scale, convertToRectInt (flippedScreenRect ([window frame])));
  853. const Rectangle<int> screenBounds (Desktop::getInstance().getDisplays().getTotalBounds (true));
  854. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
  855. const bool inLiveResize = [window inLiveResize];
  856. #else
  857. const bool inLiveResize = [window respondsToSelector: @selector (inLiveResize)]
  858. && [window performSelector: @selector (inLiveResize)];
  859. #endif
  860. if (! inLiveResize || isFirstLiveResize)
  861. {
  862. isFirstLiveResize = false;
  863. isStretchingTop = (pos.getY() != original.getY() && pos.getBottom() == original.getBottom());
  864. isStretchingLeft = (pos.getX() != original.getX() && pos.getRight() == original.getRight());
  865. isStretchingBottom = (pos.getY() == original.getY() && pos.getBottom() != original.getBottom());
  866. isStretchingRight = (pos.getX() == original.getX() && pos.getRight() != original.getRight());
  867. }
  868. constrainer->checkBounds (pos, original, screenBounds,
  869. isStretchingTop, isStretchingLeft, isStretchingBottom, isStretchingRight);
  870. pos = ScalingHelpers::scaledScreenPosToUnscaled (scale, pos);
  871. r = flippedScreenRect (makeNSRect (pos));
  872. }
  873. return r;
  874. }
  875. static void showArrowCursorIfNeeded()
  876. {
  877. auto& desktop = Desktop::getInstance();
  878. auto mouse = desktop.getMainMouseSource();
  879. if (mouse.getComponentUnderMouse() == nullptr
  880. && desktop.findComponentAt (mouse.getScreenPosition().roundToInt()) == nullptr)
  881. {
  882. [[NSCursor arrowCursor] set];
  883. }
  884. }
  885. static void updateModifiers (NSEvent* e)
  886. {
  887. updateModifiers ([e modifierFlags]);
  888. }
  889. static void updateModifiers (const NSUInteger flags)
  890. {
  891. int m = 0;
  892. if ((flags & NSEventModifierFlagShift) != 0) m |= ModifierKeys::shiftModifier;
  893. if ((flags & NSEventModifierFlagControl) != 0) m |= ModifierKeys::ctrlModifier;
  894. if ((flags & NSEventModifierFlagOption) != 0) m |= ModifierKeys::altModifier;
  895. if ((flags & NSEventModifierFlagCommand) != 0) m |= ModifierKeys::commandModifier;
  896. currentModifiers = currentModifiers.withOnlyMouseButtons().withFlags (m);
  897. }
  898. static void updateKeysDown (NSEvent* ev, bool isKeyDown)
  899. {
  900. updateModifiers (ev);
  901. int keyCode = getKeyCodeFromEvent (ev);
  902. if (keyCode != 0)
  903. {
  904. if (isKeyDown)
  905. keysCurrentlyDown.addIfNotAlreadyThere (keyCode);
  906. else
  907. keysCurrentlyDown.removeFirstMatchingValue (keyCode);
  908. }
  909. }
  910. static int getKeyCodeFromEvent (NSEvent* ev)
  911. {
  912. // Unfortunately, charactersIgnoringModifiers does not ignore the shift key.
  913. // Using [ev keyCode] is not a solution either as this will,
  914. // for example, return VK_KEY_Y if the key is pressed which
  915. // is typically located at the Y key position on a QWERTY
  916. // keyboard. However, on international keyboards this might not
  917. // be the key labeled Y (for example, on German keyboards this key
  918. // has a Z label). Therefore, we need to query the current keyboard
  919. // layout to figure out what character the key would have produced
  920. // if the shift key was not pressed
  921. String unmodified;
  922. #if JUCE_SUPPORT_CARBON
  923. if (TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource())
  924. {
  925. CFDataRef layoutData = (CFDataRef) TISGetInputSourceProperty (currentKeyboard,
  926. kTISPropertyUnicodeKeyLayoutData);
  927. if (layoutData != nullptr)
  928. {
  929. if (auto* layoutPtr = (const UCKeyboardLayout*) CFDataGetBytePtr (layoutData))
  930. {
  931. UInt32 keysDown = 0;
  932. UniChar buffer[4];
  933. UniCharCount actual;
  934. if (UCKeyTranslate (layoutPtr, [ev keyCode], kUCKeyActionDown, 0, LMGetKbdType(),
  935. kUCKeyTranslateNoDeadKeysBit, &keysDown, sizeof (buffer) / sizeof (UniChar),
  936. &actual, buffer) == 0)
  937. unmodified = String (CharPointer_UTF16 (reinterpret_cast<CharPointer_UTF16::CharType*> (buffer)), 4);
  938. }
  939. }
  940. CFRelease (currentKeyboard);
  941. }
  942. // did the above layout conversion fail
  943. if (unmodified.isEmpty())
  944. #endif
  945. {
  946. unmodified = nsStringToJuce ([ev charactersIgnoringModifiers]);
  947. }
  948. int keyCode = unmodified[0];
  949. if (keyCode == 0x19) // (backwards-tab)
  950. keyCode = '\t';
  951. else if (keyCode == 0x03) // (enter)
  952. keyCode = '\r';
  953. else
  954. keyCode = (int) CharacterFunctions::toUpperCase ((juce_wchar) keyCode);
  955. if (([ev modifierFlags] & NSEventModifierFlagNumericPad) != 0)
  956. {
  957. const int numPadConversions[] = { '0', KeyPress::numberPad0, '1', KeyPress::numberPad1,
  958. '2', KeyPress::numberPad2, '3', KeyPress::numberPad3,
  959. '4', KeyPress::numberPad4, '5', KeyPress::numberPad5,
  960. '6', KeyPress::numberPad6, '7', KeyPress::numberPad7,
  961. '8', KeyPress::numberPad8, '9', KeyPress::numberPad9,
  962. '+', KeyPress::numberPadAdd, '-', KeyPress::numberPadSubtract,
  963. '*', KeyPress::numberPadMultiply, '/', KeyPress::numberPadDivide,
  964. '.', KeyPress::numberPadDecimalPoint,
  965. ',', KeyPress::numberPadDecimalPoint, // (to deal with non-english kbds)
  966. '=', KeyPress::numberPadEquals };
  967. for (int i = 0; i < numElementsInArray (numPadConversions); i += 2)
  968. if (keyCode == numPadConversions [i])
  969. keyCode = numPadConversions [i + 1];
  970. }
  971. return keyCode;
  972. }
  973. static int64 getMouseTime (NSEvent* e) noexcept
  974. {
  975. return (Time::currentTimeMillis() - Time::getMillisecondCounter())
  976. + (int64) ([e timestamp] * 1000.0);
  977. }
  978. static float getMousePressure (NSEvent* e) noexcept
  979. {
  980. @try
  981. {
  982. if (e.type != NSEventTypeMouseEntered && e.type != NSEventTypeMouseExited)
  983. return (float) e.pressure;
  984. }
  985. @catch (NSException* e) {}
  986. @finally {}
  987. return 0.0f;
  988. }
  989. static Point<float> getMousePos (NSEvent* e, NSView* view)
  990. {
  991. NSPoint p = [view convertPoint: [e locationInWindow] fromView: nil];
  992. return { (float) p.x, (float) ([view frame].size.height - p.y) };
  993. }
  994. static int getModifierForButtonNumber (const NSInteger num)
  995. {
  996. return num == 0 ? ModifierKeys::leftButtonModifier
  997. : (num == 1 ? ModifierKeys::rightButtonModifier
  998. : (num == 2 ? ModifierKeys::middleButtonModifier : 0));
  999. }
  1000. static unsigned int getNSWindowStyleMask (const int flags) noexcept
  1001. {
  1002. unsigned int style = (flags & windowHasTitleBar) != 0 ? NSWindowStyleMaskTitled
  1003. : NSWindowStyleMaskBorderless;
  1004. if ((flags & windowHasMinimiseButton) != 0) style |= NSWindowStyleMaskMiniaturizable;
  1005. if ((flags & windowHasCloseButton) != 0) style |= NSWindowStyleMaskClosable;
  1006. if ((flags & windowIsResizable) != 0) style |= NSWindowStyleMaskResizable;
  1007. return style;
  1008. }
  1009. static NSArray* getSupportedDragTypes()
  1010. {
  1011. return [NSArray arrayWithObjects: NSFilenamesPboardType, NSFilesPromisePboardType, NSStringPboardType, nil];
  1012. }
  1013. BOOL sendDragCallback (const int type, id <NSDraggingInfo> sender)
  1014. {
  1015. NSPasteboard* pasteboard = [sender draggingPasteboard];
  1016. NSString* contentType = [pasteboard availableTypeFromArray: getSupportedDragTypes()];
  1017. if (contentType == nil)
  1018. return false;
  1019. NSPoint p = [view convertPoint: [sender draggingLocation] fromView: nil];
  1020. ComponentPeer::DragInfo dragInfo;
  1021. dragInfo.position.setXY ((int) p.x, (int) ([view frame].size.height - p.y));
  1022. if (contentType == NSStringPboardType)
  1023. dragInfo.text = nsStringToJuce ([pasteboard stringForType: NSStringPboardType]);
  1024. else
  1025. dragInfo.files = getDroppedFiles (pasteboard, contentType);
  1026. if (! dragInfo.isEmpty())
  1027. {
  1028. switch (type)
  1029. {
  1030. case 0: return handleDragMove (dragInfo);
  1031. case 1: return handleDragExit (dragInfo);
  1032. case 2: return handleDragDrop (dragInfo);
  1033. default: jassertfalse; break;
  1034. }
  1035. }
  1036. return false;
  1037. }
  1038. StringArray getDroppedFiles (NSPasteboard* pasteboard, NSString* contentType)
  1039. {
  1040. StringArray files;
  1041. NSString* iTunesPasteboardType = nsStringLiteral ("CorePasteboardFlavorType 0x6974756E"); // 'itun'
  1042. if (contentType == NSFilesPromisePboardType
  1043. && [[pasteboard types] containsObject: iTunesPasteboardType])
  1044. {
  1045. id list = [pasteboard propertyListForType: iTunesPasteboardType];
  1046. if ([list isKindOfClass: [NSDictionary class]])
  1047. {
  1048. NSDictionary* iTunesDictionary = (NSDictionary*) list;
  1049. NSArray* tracks = [iTunesDictionary valueForKey: nsStringLiteral ("Tracks")];
  1050. NSEnumerator* enumerator = [tracks objectEnumerator];
  1051. NSDictionary* track;
  1052. while ((track = [enumerator nextObject]) != nil)
  1053. {
  1054. NSURL* url = [NSURL URLWithString: [track valueForKey: nsStringLiteral ("Location")]];
  1055. if ([url isFileURL])
  1056. files.add (nsStringToJuce ([url path]));
  1057. }
  1058. }
  1059. }
  1060. else
  1061. {
  1062. id list = [pasteboard propertyListForType: NSFilenamesPboardType];
  1063. if ([list isKindOfClass: [NSArray class]])
  1064. {
  1065. NSArray* items = (NSArray*) [pasteboard propertyListForType: NSFilenamesPboardType];
  1066. for (unsigned int i = 0; i < [items count]; ++i)
  1067. files.add (nsStringToJuce ((NSString*) [items objectAtIndex: i]));
  1068. }
  1069. }
  1070. return files;
  1071. }
  1072. //==============================================================================
  1073. void viewFocusGain()
  1074. {
  1075. if (currentlyFocusedPeer != this)
  1076. {
  1077. if (ComponentPeer::isValidPeer (currentlyFocusedPeer))
  1078. currentlyFocusedPeer->handleFocusLoss();
  1079. currentlyFocusedPeer = this;
  1080. handleFocusGain();
  1081. }
  1082. }
  1083. void viewFocusLoss()
  1084. {
  1085. if (currentlyFocusedPeer == this)
  1086. {
  1087. currentlyFocusedPeer = nullptr;
  1088. handleFocusLoss();
  1089. }
  1090. }
  1091. bool isFocused() const override
  1092. {
  1093. return (isSharedWindow || ! JUCEApplication::isStandaloneApp())
  1094. ? this == currentlyFocusedPeer
  1095. : [window isKeyWindow];
  1096. }
  1097. void grabFocus() override
  1098. {
  1099. if (window != nil)
  1100. {
  1101. [window makeKeyWindow];
  1102. [window makeFirstResponder: view];
  1103. viewFocusGain();
  1104. }
  1105. }
  1106. void textInputRequired (Point<int>, TextInputTarget&) override {}
  1107. //==============================================================================
  1108. NSWindow* window = nil;
  1109. NSView* view = nil;
  1110. bool isSharedWindow = false, fullScreen = false;
  1111. #if USE_COREGRAPHICS_RENDERING
  1112. bool usingCoreGraphics = true;
  1113. #else
  1114. bool usingCoreGraphics = false;
  1115. #endif
  1116. bool isZooming = false, isFirstLiveResize = false, textWasInserted = false;
  1117. bool isStretchingTop = false, isStretchingLeft = false, isStretchingBottom = false, isStretchingRight = false;
  1118. String stringBeingComposed;
  1119. NSNotificationCenter* notificationCenter = nil;
  1120. RectangleList<float> deferredRepaints;
  1121. uint32 lastRepaintTime;
  1122. static ModifierKeys currentModifiers;
  1123. static ComponentPeer* currentlyFocusedPeer;
  1124. static Array<int> keysCurrentlyDown;
  1125. static int insideToFrontCall;
  1126. private:
  1127. static NSView* createViewInstance();
  1128. static NSWindow* createWindowInstance();
  1129. static void setOwner (id viewOrWindow, NSViewComponentPeer* newOwner)
  1130. {
  1131. object_setInstanceVariable (viewOrWindow, "owner", newOwner);
  1132. }
  1133. void getClipRects (RectangleList<int>& clip, const Point<int> offset, const int clipW, const int clipH)
  1134. {
  1135. const NSRect* rects = nullptr;
  1136. NSInteger numRects = 0;
  1137. [view getRectsBeingDrawn: &rects count: &numRects];
  1138. const Rectangle<int> clipBounds (clipW, clipH);
  1139. const CGFloat viewH = [view frame].size.height;
  1140. clip.ensureStorageAllocated ((int) numRects);
  1141. for (int i = 0; i < numRects; ++i)
  1142. clip.addWithoutMerging (clipBounds.getIntersection (Rectangle<int> (roundToInt (rects[i].origin.x) + offset.x,
  1143. roundToInt (viewH - (rects[i].origin.y + rects[i].size.height)) + offset.y,
  1144. roundToInt (rects[i].size.width),
  1145. roundToInt (rects[i].size.height))));
  1146. }
  1147. static void appFocusChanged()
  1148. {
  1149. keysCurrentlyDown.clear();
  1150. if (isValidPeer (currentlyFocusedPeer))
  1151. {
  1152. if (Process::isForegroundProcess())
  1153. {
  1154. currentlyFocusedPeer->handleFocusGain();
  1155. ModalComponentManager::getInstance()->bringModalComponentsToFront();
  1156. }
  1157. else
  1158. {
  1159. currentlyFocusedPeer->handleFocusLoss();
  1160. }
  1161. }
  1162. }
  1163. static bool checkEventBlockedByModalComps (NSEvent* e)
  1164. {
  1165. if (Component::getNumCurrentlyModalComponents() == 0)
  1166. return false;
  1167. NSWindow* const w = [e window];
  1168. if (w == nil || [w worksWhenModal])
  1169. return false;
  1170. bool isKey = false, isInputAttempt = false;
  1171. switch ([e type])
  1172. {
  1173. case NSEventTypeKeyDown:
  1174. case NSEventTypeKeyUp:
  1175. isKey = isInputAttempt = true;
  1176. break;
  1177. case NSEventTypeLeftMouseDown:
  1178. case NSEventTypeRightMouseDown:
  1179. case NSEventTypeOtherMouseDown:
  1180. isInputAttempt = true;
  1181. break;
  1182. case NSEventTypeLeftMouseDragged:
  1183. case NSEventTypeRightMouseDragged:
  1184. case NSEventTypeLeftMouseUp:
  1185. case NSEventTypeRightMouseUp:
  1186. case NSEventTypeOtherMouseUp:
  1187. case NSEventTypeOtherMouseDragged:
  1188. if (Desktop::getInstance().getDraggingMouseSource(0) != nullptr)
  1189. return false;
  1190. break;
  1191. case NSEventTypeMouseMoved:
  1192. case NSEventTypeMouseEntered:
  1193. case NSEventTypeMouseExited:
  1194. case NSEventTypeCursorUpdate:
  1195. case NSEventTypeScrollWheel:
  1196. case NSEventTypeTabletPoint:
  1197. case NSEventTypeTabletProximity:
  1198. break;
  1199. default:
  1200. return false;
  1201. }
  1202. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  1203. {
  1204. if (auto* peer = dynamic_cast<NSViewComponentPeer*> (ComponentPeer::getPeer (i)))
  1205. {
  1206. if ([peer->view window] == w)
  1207. {
  1208. if (isKey)
  1209. {
  1210. if (peer->view == [w firstResponder])
  1211. return false;
  1212. }
  1213. else
  1214. {
  1215. if (peer->isSharedWindow
  1216. ? NSPointInRect ([peer->view convertPoint: [e locationInWindow] fromView: nil], [peer->view bounds])
  1217. : NSPointInRect ([e locationInWindow], NSMakeRect (0, 0, [w frame].size.width, [w frame].size.height)))
  1218. return false;
  1219. }
  1220. }
  1221. }
  1222. }
  1223. if (isInputAttempt)
  1224. {
  1225. if (! [NSApp isActive])
  1226. [NSApp activateIgnoringOtherApps: YES];
  1227. if (Component* const modal = Component::getCurrentlyModalComponent())
  1228. modal->inputAttemptWhenModal();
  1229. }
  1230. return true;
  1231. }
  1232. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NSViewComponentPeer)
  1233. };
  1234. int NSViewComponentPeer::insideToFrontCall = 0;
  1235. //==============================================================================
  1236. struct JuceNSViewClass : public ObjCClass<NSView>
  1237. {
  1238. JuceNSViewClass() : ObjCClass<NSView> ("JUCEView_")
  1239. {
  1240. addIvar<NSViewComponentPeer*> ("owner");
  1241. addMethod (@selector (isOpaque), isOpaque, "c@:");
  1242. addMethod (@selector (drawRect:), drawRect, "v@:", @encode (NSRect));
  1243. addMethod (@selector (mouseDown:), mouseDown, "v@:@");
  1244. addMethod (@selector (asyncMouseDown:), asyncMouseDown, "v@:@");
  1245. addMethod (@selector (mouseUp:), mouseUp, "v@:@");
  1246. addMethod (@selector (asyncMouseUp:), asyncMouseUp, "v@:@");
  1247. addMethod (@selector (mouseDragged:), mouseDragged, "v@:@");
  1248. addMethod (@selector (mouseMoved:), mouseMoved, "v@:@");
  1249. addMethod (@selector (mouseEntered:), mouseEntered, "v@:@");
  1250. addMethod (@selector (mouseExited:), mouseExited, "v@:@");
  1251. addMethod (@selector (rightMouseDown:), mouseDown, "v@:@");
  1252. addMethod (@selector (rightMouseDragged:), mouseDragged, "v@:@");
  1253. addMethod (@selector (rightMouseUp:), mouseUp, "v@:@");
  1254. addMethod (@selector (otherMouseDown:), mouseDown, "v@:@");
  1255. addMethod (@selector (otherMouseDragged:), mouseDragged, "v@:@");
  1256. addMethod (@selector (otherMouseUp:), mouseUp, "v@:@");
  1257. addMethod (@selector (scrollWheel:), scrollWheel, "v@:@");
  1258. addMethod (@selector (magnifyWithEvent:), magnify, "v@:@");
  1259. addMethod (@selector (acceptsFirstMouse:), acceptsFirstMouse, "c@:@");
  1260. addMethod (@selector (frameChanged:), frameChanged, "v@:@");
  1261. addMethod (@selector (wantsDefaultClipping:), wantsDefaultClipping, "c@:");
  1262. addMethod (@selector (worksWhenModal), worksWhenModal, "c@:");
  1263. addMethod (@selector (viewDidMoveToWindow), viewDidMoveToWindow, "v@:");
  1264. addMethod (@selector (keyDown:), keyDown, "v@:@");
  1265. addMethod (@selector (keyUp:), keyUp, "v@:@");
  1266. addMethod (@selector (insertText:), insertText, "v@:@");
  1267. addMethod (@selector (doCommandBySelector:), doCommandBySelector, "v@::");
  1268. addMethod (@selector (setMarkedText:selectedRange:), setMarkedText, "v@:@", @encode (NSRange));
  1269. addMethod (@selector (unmarkText), unmarkText, "v@:");
  1270. addMethod (@selector (hasMarkedText), hasMarkedText, "c@:");
  1271. addMethod (@selector (conversationIdentifier), conversationIdentifier, "l@:");
  1272. addMethod (@selector (attributedSubstringFromRange:), attributedSubstringFromRange, "@@:", @encode (NSRange));
  1273. addMethod (@selector (markedRange), markedRange, @encode (NSRange), "@:");
  1274. addMethod (@selector (selectedRange), selectedRange, @encode (NSRange), "@:");
  1275. addMethod (@selector (firstRectForCharacterRange:), firstRectForCharacterRange, @encode (NSRect), "@:", @encode (NSRange));
  1276. addMethod (@selector (validAttributesForMarkedText), validAttributesForMarkedText, "@@:");
  1277. addMethod (@selector (flagsChanged:), flagsChanged, "v@:@");
  1278. addMethod (@selector (becomeFirstResponder), becomeFirstResponder, "c@:");
  1279. addMethod (@selector (resignFirstResponder), resignFirstResponder, "c@:");
  1280. addMethod (@selector (acceptsFirstResponder), acceptsFirstResponder, "c@:");
  1281. addMethod (@selector (draggingEntered:), draggingEntered, @encode (NSDragOperation), "@:@");
  1282. addMethod (@selector (draggingUpdated:), draggingUpdated, @encode (NSDragOperation), "@:@");
  1283. addMethod (@selector (draggingEnded:), draggingEnded, "v@:@");
  1284. addMethod (@selector (draggingExited:), draggingExited, "v@:@");
  1285. addMethod (@selector (prepareForDragOperation:), prepareForDragOperation, "c@:@");
  1286. addMethod (@selector (performDragOperation:), performDragOperation, "c@:@");
  1287. addMethod (@selector (concludeDragOperation:), concludeDragOperation, "v@:@");
  1288. addMethod (@selector (paste:), paste, "v@:@");
  1289. addMethod (@selector (copy:), copy, "v@:@");
  1290. addMethod (@selector (cut:), cut, "v@:@");
  1291. addMethod (@selector (viewWillMoveToWindow:), willMoveToWindow, "v@:@");
  1292. addProtocol (@protocol (NSTextInput));
  1293. registerClass();
  1294. }
  1295. private:
  1296. static NSViewComponentPeer* getOwner (id self)
  1297. {
  1298. return getIvar<NSViewComponentPeer*> (self, "owner");
  1299. }
  1300. static void mouseDown (id self, SEL s, NSEvent* ev)
  1301. {
  1302. if (JUCEApplicationBase::isStandaloneApp())
  1303. asyncMouseDown (self, s, ev);
  1304. else
  1305. // In some host situations, the host will stop modal loops from working
  1306. // correctly if they're called from a mouse event, so we'll trigger
  1307. // the event asynchronously..
  1308. [self performSelectorOnMainThread: @selector (asyncMouseDown:)
  1309. withObject: ev
  1310. waitUntilDone: NO];
  1311. }
  1312. static void mouseUp (id self, SEL s, NSEvent* ev)
  1313. {
  1314. if (JUCEApplicationBase::isStandaloneApp())
  1315. asyncMouseUp (self, s, ev);
  1316. else
  1317. // In some host situations, the host will stop modal loops from working
  1318. // correctly if they're called from a mouse event, so we'll trigger
  1319. // the event asynchronously..
  1320. [self performSelectorOnMainThread: @selector (asyncMouseUp:)
  1321. withObject: ev
  1322. waitUntilDone: NO];
  1323. }
  1324. static void asyncMouseDown (id self, SEL, NSEvent* ev) { if (auto* p = getOwner (self)) p->redirectMouseDown (ev); }
  1325. static void asyncMouseUp (id self, SEL, NSEvent* ev) { if (auto* p = getOwner (self)) p->redirectMouseUp (ev); }
  1326. static void mouseDragged (id self, SEL, NSEvent* ev) { if (auto* p = getOwner (self)) p->redirectMouseDrag (ev); }
  1327. static void mouseMoved (id self, SEL, NSEvent* ev) { if (auto* p = getOwner (self)) p->redirectMouseMove (ev); }
  1328. static void mouseEntered (id self, SEL, NSEvent* ev) { if (auto* p = getOwner (self)) p->redirectMouseEnter (ev); }
  1329. static void mouseExited (id self, SEL, NSEvent* ev) { if (auto* p = getOwner (self)) p->redirectMouseExit (ev); }
  1330. static void scrollWheel (id self, SEL, NSEvent* ev) { if (auto* p = getOwner (self)) p->redirectMouseWheel (ev); }
  1331. static void magnify (id self, SEL, NSEvent* ev) { if (auto* p = getOwner (self)) p->redirectMagnify (ev); }
  1332. static void copy (id self, SEL, NSObject* s) { if (auto* p = getOwner (self)) p->redirectCopy (s); }
  1333. static void paste (id self, SEL, NSObject* s) { if (auto* p = getOwner (self)) p->redirectPaste (s); }
  1334. static void cut (id self, SEL, NSObject* s) { if (auto* p = getOwner (self)) p->redirectCut (s); }
  1335. static void willMoveToWindow (id self, SEL, NSWindow* w) { if (auto* p = getOwner (self)) p->redirectWillMoveToWindow (w); }
  1336. static BOOL acceptsFirstMouse (id, SEL, NSEvent*) { return YES; }
  1337. static BOOL wantsDefaultClipping (id, SEL) { return YES; } // (this is the default, but may want to customise it in future)
  1338. static BOOL worksWhenModal (id self, SEL) { if (auto* p = getOwner (self)) return p->worksWhenModal(); return NO; }
  1339. static void drawRect (id self, SEL, NSRect r) { if (auto* p = getOwner (self)) p->drawRect (r); }
  1340. static void frameChanged (id self, SEL, NSNotification*) { if (auto* p = getOwner (self)) p->redirectMovedOrResized(); }
  1341. static void viewDidMoveToWindow (id self, SEL) { if (auto* p = getOwner (self)) p->viewMovedToWindow(); }
  1342. static BOOL isOpaque (id self, SEL)
  1343. {
  1344. auto* owner = getOwner (self);
  1345. return owner == nullptr || owner->getComponent().isOpaque();
  1346. }
  1347. //==============================================================================
  1348. static void keyDown (id self, SEL, NSEvent* ev)
  1349. {
  1350. if (auto* owner = getOwner (self))
  1351. {
  1352. auto* target = owner->findCurrentTextInputTarget();
  1353. owner->textWasInserted = false;
  1354. if (target != nullptr)
  1355. [(NSView*) self interpretKeyEvents: [NSArray arrayWithObject: ev]];
  1356. else
  1357. owner->stringBeingComposed.clear();
  1358. if (! (owner->textWasInserted || owner->redirectKeyDown (ev)))
  1359. {
  1360. objc_super s = { self, [NSView class] };
  1361. getMsgSendSuperFn() (&s, @selector (keyDown:), ev);
  1362. }
  1363. }
  1364. }
  1365. static void keyUp (id self, SEL, NSEvent* ev)
  1366. {
  1367. auto* owner = getOwner (self);
  1368. if (owner == nullptr || ! owner->redirectKeyUp (ev))
  1369. {
  1370. objc_super s = { self, [NSView class] };
  1371. getMsgSendSuperFn() (&s, @selector (keyUp:), ev);
  1372. }
  1373. }
  1374. //==============================================================================
  1375. static void insertText (id self, SEL, id aString)
  1376. {
  1377. // This commits multi-byte text when return is pressed, or after every keypress for western keyboards
  1378. if (auto* owner = getOwner (self))
  1379. {
  1380. NSString* newText = [aString isKindOfClass: [NSAttributedString class]] ? [aString string] : aString;
  1381. if ([newText length] > 0)
  1382. {
  1383. if (auto* target = owner->findCurrentTextInputTarget())
  1384. {
  1385. target->insertTextAtCaret (nsStringToJuce (newText));
  1386. owner->textWasInserted = true;
  1387. }
  1388. }
  1389. owner->stringBeingComposed.clear();
  1390. }
  1391. }
  1392. static void doCommandBySelector (id, SEL, SEL) {}
  1393. static void setMarkedText (id self, SEL, id aString, NSRange)
  1394. {
  1395. if (auto* owner = getOwner (self))
  1396. {
  1397. owner->stringBeingComposed = nsStringToJuce ([aString isKindOfClass: [NSAttributedString class]]
  1398. ? [aString string] : aString);
  1399. if (auto* target = owner->findCurrentTextInputTarget())
  1400. {
  1401. auto currentHighlight = target->getHighlightedRegion();
  1402. target->insertTextAtCaret (owner->stringBeingComposed);
  1403. target->setHighlightedRegion (currentHighlight.withLength (owner->stringBeingComposed.length()));
  1404. owner->textWasInserted = true;
  1405. }
  1406. }
  1407. }
  1408. static void unmarkText (id self, SEL)
  1409. {
  1410. if (auto* owner = getOwner (self))
  1411. {
  1412. if (owner->stringBeingComposed.isNotEmpty())
  1413. {
  1414. if (auto* target = owner->findCurrentTextInputTarget())
  1415. {
  1416. target->insertTextAtCaret (owner->stringBeingComposed);
  1417. owner->textWasInserted = true;
  1418. }
  1419. owner->stringBeingComposed.clear();
  1420. }
  1421. }
  1422. }
  1423. static BOOL hasMarkedText (id self, SEL)
  1424. {
  1425. auto* owner = getOwner (self);
  1426. return owner != nullptr && owner->stringBeingComposed.isNotEmpty();
  1427. }
  1428. static long conversationIdentifier (id self, SEL)
  1429. {
  1430. return (long) (pointer_sized_int) self;
  1431. }
  1432. static NSAttributedString* attributedSubstringFromRange (id self, SEL, NSRange theRange)
  1433. {
  1434. if (auto* owner = getOwner (self))
  1435. {
  1436. if (auto* target = owner->findCurrentTextInputTarget())
  1437. {
  1438. const Range<int> r ((int) theRange.location,
  1439. (int) (theRange.location + theRange.length));
  1440. return [[[NSAttributedString alloc] initWithString: juceStringToNS (target->getTextInRange (r))] autorelease];
  1441. }
  1442. }
  1443. return nil;
  1444. }
  1445. static NSRange markedRange (id self, SEL)
  1446. {
  1447. if (auto* owner = getOwner (self))
  1448. if (owner->stringBeingComposed.isNotEmpty())
  1449. return NSMakeRange (0, (NSUInteger) owner->stringBeingComposed.length());
  1450. return NSMakeRange (NSNotFound, 0);
  1451. }
  1452. static NSRange selectedRange (id self, SEL)
  1453. {
  1454. if (auto* owner = getOwner (self))
  1455. {
  1456. if (auto* target = owner->findCurrentTextInputTarget())
  1457. {
  1458. auto highlight = target->getHighlightedRegion();
  1459. if (! highlight.isEmpty())
  1460. return NSMakeRange ((NSUInteger) highlight.getStart(),
  1461. (NSUInteger) highlight.getLength());
  1462. }
  1463. }
  1464. return NSMakeRange (NSNotFound, 0);
  1465. }
  1466. static NSRect firstRectForCharacterRange (id self, SEL, NSRange)
  1467. {
  1468. if (auto* owner = getOwner (self))
  1469. if (auto* comp = dynamic_cast<Component*> (owner->findCurrentTextInputTarget()))
  1470. return flippedScreenRect (makeNSRect (comp->getScreenBounds()));
  1471. return NSZeroRect;
  1472. }
  1473. static NSUInteger characterIndexForPoint (id, SEL, NSPoint) { return NSNotFound; }
  1474. static NSArray* validAttributesForMarkedText (id, SEL) { return [NSArray array]; }
  1475. //==============================================================================
  1476. static void flagsChanged (id self, SEL, NSEvent* ev)
  1477. {
  1478. if (auto* owner = getOwner (self))
  1479. owner->redirectModKeyChange (ev);
  1480. }
  1481. static BOOL becomeFirstResponder (id self, SEL)
  1482. {
  1483. if (auto* owner = getOwner (self))
  1484. owner->viewFocusGain();
  1485. return YES;
  1486. }
  1487. static BOOL resignFirstResponder (id self, SEL)
  1488. {
  1489. if (auto* owner = getOwner (self))
  1490. owner->viewFocusLoss();
  1491. return YES;
  1492. }
  1493. static BOOL acceptsFirstResponder (id self, SEL)
  1494. {
  1495. auto* owner = getOwner (self);
  1496. return owner != nullptr && owner->canBecomeKeyWindow();
  1497. }
  1498. //==============================================================================
  1499. static NSDragOperation draggingEntered (id self, SEL s, id<NSDraggingInfo> sender)
  1500. {
  1501. return draggingUpdated (self, s, sender);
  1502. }
  1503. static NSDragOperation draggingUpdated (id self, SEL, id<NSDraggingInfo> sender)
  1504. {
  1505. if (auto* owner = getOwner (self))
  1506. if (owner->sendDragCallback (0, sender))
  1507. return NSDragOperationCopy | NSDragOperationMove | NSDragOperationGeneric;
  1508. return NSDragOperationNone;
  1509. }
  1510. static void draggingEnded (id self, SEL s, id<NSDraggingInfo> sender)
  1511. {
  1512. draggingExited (self, s, sender);
  1513. }
  1514. static void draggingExited (id self, SEL, id<NSDraggingInfo> sender)
  1515. {
  1516. if (auto* owner = getOwner (self))
  1517. owner->sendDragCallback (1, sender);
  1518. }
  1519. static BOOL prepareForDragOperation (id, SEL, id<NSDraggingInfo>)
  1520. {
  1521. return YES;
  1522. }
  1523. static BOOL performDragOperation (id self, SEL, id<NSDraggingInfo> sender)
  1524. {
  1525. auto* owner = getOwner (self);
  1526. return owner != nullptr && owner->sendDragCallback (2, sender);
  1527. }
  1528. static void concludeDragOperation (id, SEL, id<NSDraggingInfo>) {}
  1529. };
  1530. //==============================================================================
  1531. struct JuceNSWindowClass : public ObjCClass<NSWindow>
  1532. {
  1533. JuceNSWindowClass() : ObjCClass<NSWindow> ("JUCEWindow_")
  1534. {
  1535. addIvar<NSViewComponentPeer*> ("owner");
  1536. addMethod (@selector (canBecomeKeyWindow), canBecomeKeyWindow, "c@:");
  1537. addMethod (@selector (canBecomeMainWindow), canBecomeMainWindow, "c@:");
  1538. addMethod (@selector (becomeKeyWindow), becomeKeyWindow, "v@:");
  1539. addMethod (@selector (windowShouldClose:), windowShouldClose, "c@:@");
  1540. addMethod (@selector (constrainFrameRect:toScreen:), constrainFrameRect, @encode (NSRect), "@:", @encode (NSRect), "@");
  1541. addMethod (@selector (windowWillResize:toSize:), windowWillResize, @encode (NSSize), "@:@", @encode (NSSize));
  1542. addMethod (@selector (windowDidExitFullScreen:), windowDidExitFullScreen, "v@:@");
  1543. addMethod (@selector (zoom:), zoom, "v@:@");
  1544. addMethod (@selector (windowWillMove:), windowWillMove, "v@:@");
  1545. addMethod (@selector (windowWillStartLiveResize:), windowWillStartLiveResize, "v@:@");
  1546. addMethod (@selector (windowDidEndLiveResize:), windowDidEndLiveResize, "v@:@");
  1547. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  1548. addProtocol (@protocol (NSWindowDelegate));
  1549. #endif
  1550. registerClass();
  1551. }
  1552. private:
  1553. static NSViewComponentPeer* getOwner (id self)
  1554. {
  1555. return getIvar<NSViewComponentPeer*> (self, "owner");
  1556. }
  1557. //==============================================================================
  1558. static BOOL canBecomeKeyWindow (id self, SEL)
  1559. {
  1560. auto* owner = getOwner (self);
  1561. return owner != nullptr
  1562. && owner->canBecomeKeyWindow()
  1563. && ! owner->sendModalInputAttemptIfBlocked();
  1564. }
  1565. static BOOL canBecomeMainWindow (id self, SEL)
  1566. {
  1567. auto* owner = getOwner (self);
  1568. return owner != nullptr
  1569. && owner->canBecomeMainWindow()
  1570. && ! owner->sendModalInputAttemptIfBlocked();
  1571. }
  1572. static void becomeKeyWindow (id self, SEL)
  1573. {
  1574. sendSuperclassMessage (self, @selector (becomeKeyWindow));
  1575. if (auto* owner = getOwner (self))
  1576. owner->becomeKeyWindow();
  1577. }
  1578. static BOOL windowShouldClose (id self, SEL, id /*window*/)
  1579. {
  1580. auto* owner = getOwner (self);
  1581. return owner == nullptr || owner->windowShouldClose();
  1582. }
  1583. static NSRect constrainFrameRect (id self, SEL, NSRect frameRect, NSScreen*)
  1584. {
  1585. if (auto* owner = getOwner (self))
  1586. frameRect = owner->constrainRect (frameRect);
  1587. return frameRect;
  1588. }
  1589. static NSSize windowWillResize (id self, SEL, NSWindow*, NSSize proposedFrameSize)
  1590. {
  1591. auto* owner = getOwner (self);
  1592. if (owner == nullptr || owner->isZooming)
  1593. return proposedFrameSize;
  1594. NSRect frameRect = [(NSWindow*) self frame];
  1595. frameRect.origin.y -= proposedFrameSize.height - frameRect.size.height;
  1596. frameRect.size = proposedFrameSize;
  1597. frameRect = owner->constrainRect (frameRect);
  1598. if (owner->hasNativeTitleBar())
  1599. owner->sendModalInputAttemptIfBlocked();
  1600. return frameRect.size;
  1601. }
  1602. static void windowDidExitFullScreen (id, SEL, NSNotification*)
  1603. {
  1604. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  1605. [NSApp setPresentationOptions: NSApplicationPresentationDefault];
  1606. #endif
  1607. }
  1608. static void zoom (id self, SEL, id sender)
  1609. {
  1610. if (auto* owner = getOwner (self))
  1611. {
  1612. owner->isZooming = true;
  1613. objc_super s = { self, [NSWindow class] };
  1614. getMsgSendSuperFn() (&s, @selector (zoom:), sender);
  1615. owner->isZooming = false;
  1616. owner->redirectMovedOrResized();
  1617. }
  1618. }
  1619. static void windowWillMove (id self, SEL, NSNotification*)
  1620. {
  1621. if (auto* owner = getOwner (self))
  1622. if (owner->hasNativeTitleBar())
  1623. owner->sendModalInputAttemptIfBlocked();
  1624. }
  1625. static void windowWillStartLiveResize (id self, SEL, NSNotification*)
  1626. {
  1627. if (auto* owner = getOwner (self))
  1628. owner->liveResizingStart();
  1629. }
  1630. static void windowDidEndLiveResize (id self, SEL, NSNotification*)
  1631. {
  1632. if (auto* owner = getOwner (self))
  1633. owner->liveResizingEnd();
  1634. }
  1635. };
  1636. NSView* NSViewComponentPeer::createViewInstance()
  1637. {
  1638. static JuceNSViewClass cls;
  1639. return cls.createInstance();
  1640. }
  1641. NSWindow* NSViewComponentPeer::createWindowInstance()
  1642. {
  1643. static JuceNSWindowClass cls;
  1644. return cls.createInstance();
  1645. }
  1646. //==============================================================================
  1647. ModifierKeys NSViewComponentPeer::currentModifiers;
  1648. ComponentPeer* NSViewComponentPeer::currentlyFocusedPeer = nullptr;
  1649. Array<int> NSViewComponentPeer::keysCurrentlyDown;
  1650. //==============================================================================
  1651. bool KeyPress::isKeyCurrentlyDown (const int keyCode)
  1652. {
  1653. if (NSViewComponentPeer::keysCurrentlyDown.contains (keyCode))
  1654. return true;
  1655. if (keyCode >= 'A' && keyCode <= 'Z'
  1656. && NSViewComponentPeer::keysCurrentlyDown.contains ((int) CharacterFunctions::toLowerCase ((juce_wchar) keyCode)))
  1657. return true;
  1658. if (keyCode >= 'a' && keyCode <= 'z'
  1659. && NSViewComponentPeer::keysCurrentlyDown.contains ((int) CharacterFunctions::toUpperCase ((juce_wchar) keyCode)))
  1660. return true;
  1661. return false;
  1662. }
  1663. ModifierKeys ModifierKeys::getCurrentModifiersRealtime() noexcept
  1664. {
  1665. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  1666. if ([NSEvent respondsToSelector: @selector (modifierFlags)])
  1667. NSViewComponentPeer::updateModifiers ((NSUInteger) [NSEvent modifierFlags]);
  1668. #endif
  1669. return NSViewComponentPeer::currentModifiers;
  1670. }
  1671. void ModifierKeys::updateCurrentModifiers() noexcept
  1672. {
  1673. currentModifiers = NSViewComponentPeer::currentModifiers;
  1674. }
  1675. //==============================================================================
  1676. bool MouseInputSource::SourceList::addSource()
  1677. {
  1678. if (sources.size() == 0)
  1679. {
  1680. addSource (0, MouseInputSource::InputSourceType::mouse);
  1681. return true;
  1682. }
  1683. return false;
  1684. }
  1685. bool MouseInputSource::SourceList::canUseTouch()
  1686. {
  1687. return false;
  1688. }
  1689. //==============================================================================
  1690. void Desktop::setKioskComponent (Component* kioskComp, bool shouldBeEnabled, bool allowMenusAndBars)
  1691. {
  1692. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
  1693. auto* peer = dynamic_cast<NSViewComponentPeer*> (kioskComp->getPeer());
  1694. jassert (peer != nullptr); // (this should have been checked by the caller)
  1695. #if defined (MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
  1696. if (peer->hasNativeTitleBar()
  1697. && [peer->window respondsToSelector: @selector (toggleFullScreen:)])
  1698. {
  1699. if (shouldBeEnabled && ! allowMenusAndBars)
  1700. [NSApp setPresentationOptions: NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar];
  1701. [peer->window performSelector: @selector (toggleFullScreen:) withObject: nil];
  1702. }
  1703. else
  1704. #endif
  1705. {
  1706. if (shouldBeEnabled)
  1707. {
  1708. if (peer->hasNativeTitleBar())
  1709. [peer->window setStyleMask: NSWindowStyleMaskBorderless];
  1710. [NSApp setPresentationOptions: (allowMenusAndBars ? (NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)
  1711. : (NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar))];
  1712. kioskComp->setBounds (Desktop::getInstance().getDisplays().getMainDisplay().totalArea);
  1713. peer->becomeKeyWindow();
  1714. }
  1715. else
  1716. {
  1717. if (peer->hasNativeTitleBar())
  1718. {
  1719. [peer->window setStyleMask: (NSViewComponentPeer::getNSWindowStyleMask (peer->getStyleFlags()))];
  1720. peer->setTitle (peer->getComponent().getName()); // required to force the OS to update the title
  1721. }
  1722. [NSApp setPresentationOptions: NSApplicationPresentationDefault];
  1723. }
  1724. }
  1725. #elif JUCE_SUPPORT_CARBON
  1726. if (shouldBeEnabled)
  1727. {
  1728. SetSystemUIMode (kUIModeAllSuppressed, allowMenusAndBars ? kUIOptionAutoShowMenuBar : 0);
  1729. kioskComp->setBounds (Desktop::getInstance().getDisplays().getMainDisplay().totalArea);
  1730. }
  1731. else
  1732. {
  1733. SetSystemUIMode (kUIModeNormal, 0);
  1734. }
  1735. #else
  1736. ignoreUnused (kioskComp, shouldBeEnabled, allowMenusAndBars);
  1737. // If you're targeting OSes earlier than 10.6 and want to use this feature,
  1738. // you'll need to enable JUCE_SUPPORT_CARBON.
  1739. jassertfalse;
  1740. #endif
  1741. }
  1742. void Desktop::allowedOrientationsChanged() {}
  1743. //==============================================================================
  1744. ComponentPeer* Component::createNewPeer (int styleFlags, void* windowToAttachTo)
  1745. {
  1746. return new NSViewComponentPeer (*this, styleFlags, (NSView*) windowToAttachTo);
  1747. }
  1748. //==============================================================================
  1749. const int KeyPress::spaceKey = ' ';
  1750. const int KeyPress::returnKey = 0x0d;
  1751. const int KeyPress::escapeKey = 0x1b;
  1752. const int KeyPress::backspaceKey = 0x7f;
  1753. const int KeyPress::leftKey = NSLeftArrowFunctionKey;
  1754. const int KeyPress::rightKey = NSRightArrowFunctionKey;
  1755. const int KeyPress::upKey = NSUpArrowFunctionKey;
  1756. const int KeyPress::downKey = NSDownArrowFunctionKey;
  1757. const int KeyPress::pageUpKey = NSPageUpFunctionKey;
  1758. const int KeyPress::pageDownKey = NSPageDownFunctionKey;
  1759. const int KeyPress::endKey = NSEndFunctionKey;
  1760. const int KeyPress::homeKey = NSHomeFunctionKey;
  1761. const int KeyPress::deleteKey = NSDeleteFunctionKey;
  1762. const int KeyPress::insertKey = -1;
  1763. const int KeyPress::tabKey = 9;
  1764. const int KeyPress::F1Key = NSF1FunctionKey;
  1765. const int KeyPress::F2Key = NSF2FunctionKey;
  1766. const int KeyPress::F3Key = NSF3FunctionKey;
  1767. const int KeyPress::F4Key = NSF4FunctionKey;
  1768. const int KeyPress::F5Key = NSF5FunctionKey;
  1769. const int KeyPress::F6Key = NSF6FunctionKey;
  1770. const int KeyPress::F7Key = NSF7FunctionKey;
  1771. const int KeyPress::F8Key = NSF8FunctionKey;
  1772. const int KeyPress::F9Key = NSF9FunctionKey;
  1773. const int KeyPress::F10Key = NSF10FunctionKey;
  1774. const int KeyPress::F11Key = NSF11FunctionKey;
  1775. const int KeyPress::F12Key = NSF12FunctionKey;
  1776. const int KeyPress::F13Key = NSF13FunctionKey;
  1777. const int KeyPress::F14Key = NSF14FunctionKey;
  1778. const int KeyPress::F15Key = NSF15FunctionKey;
  1779. const int KeyPress::F16Key = NSF16FunctionKey;
  1780. const int KeyPress::numberPad0 = 0x30020;
  1781. const int KeyPress::numberPad1 = 0x30021;
  1782. const int KeyPress::numberPad2 = 0x30022;
  1783. const int KeyPress::numberPad3 = 0x30023;
  1784. const int KeyPress::numberPad4 = 0x30024;
  1785. const int KeyPress::numberPad5 = 0x30025;
  1786. const int KeyPress::numberPad6 = 0x30026;
  1787. const int KeyPress::numberPad7 = 0x30027;
  1788. const int KeyPress::numberPad8 = 0x30028;
  1789. const int KeyPress::numberPad9 = 0x30029;
  1790. const int KeyPress::numberPadAdd = 0x3002a;
  1791. const int KeyPress::numberPadSubtract = 0x3002b;
  1792. const int KeyPress::numberPadMultiply = 0x3002c;
  1793. const int KeyPress::numberPadDivide = 0x3002d;
  1794. const int KeyPress::numberPadSeparator = 0x3002e;
  1795. const int KeyPress::numberPadDecimalPoint = 0x3002f;
  1796. const int KeyPress::numberPadEquals = 0x30030;
  1797. const int KeyPress::numberPadDelete = 0x30031;
  1798. const int KeyPress::playKey = 0x30000;
  1799. const int KeyPress::stopKey = 0x30001;
  1800. const int KeyPress::fastForwardKey = 0x30002;
  1801. const int KeyPress::rewindKey = 0x30003;