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_Component.h 122KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. The base class for all JUCE user-interface objects.
  23. @tags{GUI}
  24. */
  25. class JUCE_API Component : public MouseListener
  26. {
  27. public:
  28. //==============================================================================
  29. /** Creates a component.
  30. To get it to actually appear, you'll also need to:
  31. - Either add it to a parent component or use the addToDesktop() method to
  32. make it a desktop window
  33. - Set its size and position to something sensible
  34. - Use setVisible() to make it visible
  35. And for it to serve any useful purpose, you'll need to write a
  36. subclass of Component or use one of the other types of component from
  37. the library.
  38. */
  39. Component() noexcept;
  40. /** Destructor.
  41. Note that when a component is deleted, any child components it contains are NOT
  42. automatically deleted. It's your responsibility to manage their lifespan - you
  43. may want to use helper methods like deleteAllChildren(), or less haphazard
  44. approaches like using std::unique_ptrs or normal object aggregation to manage them.
  45. If the component being deleted is currently the child of another one, then during
  46. deletion, it will be removed from its parent, and the parent will receive a childrenChanged()
  47. callback. Any ComponentListener objects that have registered with it will also have their
  48. ComponentListener::componentBeingDeleted() methods called.
  49. */
  50. ~Component() override;
  51. //==============================================================================
  52. /** Creates a component, setting its name at the same time.
  53. @see getName, setName
  54. */
  55. explicit Component (const String& componentName) noexcept;
  56. /** Returns the name of this component.
  57. @see setName
  58. */
  59. String getName() const noexcept { return componentName; }
  60. /** Sets the name of this component.
  61. When the name changes, all registered ComponentListeners will receive a
  62. ComponentListener::componentNameChanged() callback.
  63. @see getName
  64. */
  65. virtual void setName (const String& newName);
  66. /** Returns the ID string that was set by setComponentID().
  67. @see setComponentID, findChildWithID
  68. */
  69. String getComponentID() const noexcept { return componentID; }
  70. /** Sets the component's ID string.
  71. You can retrieve the ID using getComponentID().
  72. @see getComponentID, findChildWithID
  73. */
  74. void setComponentID (const String& newID);
  75. //==============================================================================
  76. /** Makes the component visible or invisible.
  77. This method will show or hide the component.
  78. Note that components default to being non-visible when first created.
  79. Also note that visible components won't be seen unless all their parent components
  80. are also visible.
  81. This method will call visibilityChanged() and also componentVisibilityChanged()
  82. for any component listeners that are interested in this component.
  83. @param shouldBeVisible whether to show or hide the component
  84. @see isVisible, isShowing, visibilityChanged, ComponentListener::componentVisibilityChanged
  85. */
  86. virtual void setVisible (bool shouldBeVisible);
  87. /** Tests whether the component is visible or not.
  88. this doesn't necessarily tell you whether this comp is actually on the screen
  89. because this depends on whether all the parent components are also visible - use
  90. isShowing() to find this out.
  91. @see isShowing, setVisible
  92. */
  93. bool isVisible() const noexcept { return flags.visibleFlag; }
  94. /** Called when this component's visibility changes.
  95. @see setVisible, isVisible
  96. */
  97. virtual void visibilityChanged();
  98. /** Tests whether this component and all its parents are visible.
  99. @returns true only if this component and all its parents are visible.
  100. @see isVisible
  101. */
  102. bool isShowing() const;
  103. //==============================================================================
  104. /** Makes this component appear as a window on the desktop.
  105. Note that before calling this, you should make sure that the component's opacity is
  106. set correctly using setOpaque(). If the component is non-opaque, the windowing
  107. system will try to create a special transparent window for it, which will generally take
  108. a lot more CPU to operate (and might not even be possible on some platforms).
  109. If the component is inside a parent component at the time this method is called, it
  110. will first be removed from that parent. Likewise if a component is on the desktop
  111. and is subsequently added to another component, it'll be removed from the desktop.
  112. @param windowStyleFlags a combination of the flags specified in the
  113. ComponentPeer::StyleFlags enum, which define the
  114. window's characteristics.
  115. @param nativeWindowToAttachTo this allows an OS object to be passed-in as the window
  116. in which the juce component should place itself. On Windows,
  117. this would be a HWND, a HIViewRef on the Mac. Not necessarily
  118. supported on all platforms, and best left as 0 unless you know
  119. what you're doing.
  120. @see removeFromDesktop, isOnDesktop, userTriedToCloseWindow,
  121. getPeer, ComponentPeer::setMinimised, ComponentPeer::StyleFlags,
  122. ComponentPeer::getStyleFlags, ComponentPeer::setFullScreen
  123. */
  124. virtual void addToDesktop (int windowStyleFlags,
  125. void* nativeWindowToAttachTo = nullptr);
  126. /** If the component is currently showing on the desktop, this will hide it.
  127. You can also use setVisible() to hide a desktop window temporarily, but
  128. removeFromDesktop() will free any system resources that are being used up.
  129. @see addToDesktop, isOnDesktop
  130. */
  131. void removeFromDesktop();
  132. /** Returns true if this component is currently showing on the desktop.
  133. @see addToDesktop, removeFromDesktop
  134. */
  135. bool isOnDesktop() const noexcept;
  136. /** Returns the heavyweight window that contains this component.
  137. If this component is itself on the desktop, this will return the window
  138. object that it is using. Otherwise, it will return the window of
  139. its top-level parent component.
  140. This may return nullptr if there isn't a desktop component.
  141. @see addToDesktop, isOnDesktop
  142. */
  143. ComponentPeer* getPeer() const;
  144. /** For components on the desktop, this is called if the system wants to close the window.
  145. This is a signal that either the user or the system wants the window to close. The
  146. default implementation of this method will trigger an assertion to warn you that your
  147. component should do something about it, but you can override this to ignore the event
  148. if you want.
  149. */
  150. virtual void userTriedToCloseWindow();
  151. /** Called for a desktop component which has just been minimised or un-minimised.
  152. This will only be called for components on the desktop.
  153. @see getPeer, ComponentPeer::setMinimised, ComponentPeer::isMinimised
  154. */
  155. virtual void minimisationStateChanged (bool isNowMinimised);
  156. /** Returns the default scale factor to use for this component when it is placed
  157. on the desktop.
  158. The default implementation of this method just returns the value from
  159. Desktop::getGlobalScaleFactor(), but it can be overridden if a particular component
  160. has different requirements. The method only used if this component is added
  161. to the desktop - it has no effect for child components.
  162. */
  163. virtual float getDesktopScaleFactor() const;
  164. //==============================================================================
  165. /** Brings the component to the front of its siblings.
  166. If some of the component's siblings have had their 'always-on-top' flag set,
  167. then they will still be kept in front of this one (unless of course this
  168. one is also 'always-on-top').
  169. @param shouldAlsoGainKeyboardFocus if true, this will also try to assign
  170. keyboard focus to the component (see
  171. grabKeyboardFocus() for more details)
  172. @see toBack, toBehind, setAlwaysOnTop
  173. */
  174. void toFront (bool shouldAlsoGainKeyboardFocus);
  175. /** Changes this component's z-order to be at the back of all its siblings.
  176. If the component is set to be 'always-on-top', it will only be moved to the
  177. back of the other other 'always-on-top' components.
  178. @see toFront, toBehind, setAlwaysOnTop
  179. */
  180. void toBack();
  181. /** Changes this component's z-order so that it's just behind another component.
  182. @see toFront, toBack
  183. */
  184. void toBehind (Component* other);
  185. /** Sets whether the component should always be kept at the front of its siblings.
  186. @see isAlwaysOnTop
  187. */
  188. void setAlwaysOnTop (bool shouldStayOnTop);
  189. /** Returns true if this component is set to always stay in front of its siblings.
  190. @see setAlwaysOnTop
  191. */
  192. bool isAlwaysOnTop() const noexcept;
  193. //==============================================================================
  194. /** Returns the x coordinate of the component's left edge.
  195. This is a distance in pixels from the left edge of the component's parent.
  196. Note that if you've used setTransform() to apply a transform, then the component's
  197. bounds will no longer be a direct reflection of the position at which it appears within
  198. its parent, as the transform will be applied to its bounding box.
  199. */
  200. int getX() const noexcept { return boundsRelativeToParent.getX(); }
  201. /** Returns the y coordinate of the top of this component.
  202. This is a distance in pixels from the top edge of the component's parent.
  203. Note that if you've used setTransform() to apply a transform, then the component's
  204. bounds will no longer be a direct reflection of the position at which it appears within
  205. its parent, as the transform will be applied to its bounding box.
  206. */
  207. int getY() const noexcept { return boundsRelativeToParent.getY(); }
  208. /** Returns the component's width in pixels. */
  209. int getWidth() const noexcept { return boundsRelativeToParent.getWidth(); }
  210. /** Returns the component's height in pixels. */
  211. int getHeight() const noexcept { return boundsRelativeToParent.getHeight(); }
  212. /** Returns the x coordinate of the component's right-hand edge.
  213. This is a distance in pixels from the left edge of the component's parent.
  214. Note that if you've used setTransform() to apply a transform, then the component's
  215. bounds will no longer be a direct reflection of the position at which it appears within
  216. its parent, as the transform will be applied to its bounding box.
  217. */
  218. int getRight() const noexcept { return boundsRelativeToParent.getRight(); }
  219. /** Returns the component's top-left position as a Point. */
  220. Point<int> getPosition() const noexcept { return boundsRelativeToParent.getPosition(); }
  221. /** Returns the y coordinate of the bottom edge of this component.
  222. This is a distance in pixels from the top edge of the component's parent.
  223. Note that if you've used setTransform() to apply a transform, then the component's
  224. bounds will no longer be a direct reflection of the position at which it appears within
  225. its parent, as the transform will be applied to its bounding box.
  226. */
  227. int getBottom() const noexcept { return boundsRelativeToParent.getBottom(); }
  228. /** Returns this component's bounding box.
  229. The rectangle returned is relative to the top-left of the component's parent.
  230. Note that if you've used setTransform() to apply a transform, then the component's
  231. bounds will no longer be a direct reflection of the position at which it appears within
  232. its parent, as the transform will be applied to its bounding box.
  233. */
  234. Rectangle<int> getBounds() const noexcept { return boundsRelativeToParent; }
  235. /** Returns the component's bounds, relative to its own origin.
  236. This is like getBounds(), but returns the rectangle in local coordinates, In practice, it'll
  237. return a rectangle with position (0, 0), and the same size as this component.
  238. */
  239. Rectangle<int> getLocalBounds() const noexcept;
  240. /** Returns the area of this component's parent which this component covers.
  241. The returned area is relative to the parent's coordinate space.
  242. If the component has an affine transform specified, then the resulting area will be
  243. the smallest rectangle that fully covers the component's transformed bounding box.
  244. If this component has no parent, the return value will simply be the same as getBounds().
  245. */
  246. Rectangle<int> getBoundsInParent() const noexcept;
  247. //==============================================================================
  248. /** Returns this component's x coordinate relative the screen's top-left origin.
  249. @see getX, localPointToGlobal
  250. */
  251. int getScreenX() const;
  252. /** Returns this component's y coordinate relative the screen's top-left origin.
  253. @see getY, localPointToGlobal
  254. */
  255. int getScreenY() const;
  256. /** Returns the position of this component's top-left corner relative to the screen's top-left.
  257. @see getScreenBounds
  258. */
  259. Point<int> getScreenPosition() const;
  260. /** Returns the bounds of this component, relative to the screen's top-left.
  261. @see getScreenPosition
  262. */
  263. Rectangle<int> getScreenBounds() const;
  264. /** Converts a point to be relative to this component's coordinate space.
  265. This takes a point relative to a different component, and returns its position relative to this
  266. component. If the sourceComponent parameter is null, the source point is assumed to be a global
  267. screen coordinate.
  268. */
  269. Point<int> getLocalPoint (const Component* sourceComponent,
  270. Point<int> pointRelativeToSourceComponent) const;
  271. /** Converts a point to be relative to this component's coordinate space.
  272. This takes a point relative to a different component, and returns its position relative to this
  273. component. If the sourceComponent parameter is null, the source point is assumed to be a global
  274. screen coordinate.
  275. */
  276. Point<float> getLocalPoint (const Component* sourceComponent,
  277. Point<float> pointRelativeToSourceComponent) const;
  278. /** Converts a rectangle to be relative to this component's coordinate space.
  279. This takes a rectangle that is relative to a different component, and returns its position relative
  280. to this component. If the sourceComponent parameter is null, the source rectangle is assumed to be
  281. a screen coordinate.
  282. If you've used setTransform() to apply one or more transforms to components, then the source rectangle
  283. may not actually be rectangular when converted to the target space, so in that situation this will return
  284. the smallest rectangle that fully contains the transformed area.
  285. */
  286. Rectangle<int> getLocalArea (const Component* sourceComponent,
  287. Rectangle<int> areaRelativeToSourceComponent) const;
  288. /** Converts a rectangle to be relative to this component's coordinate space.
  289. This takes a rectangle that is relative to a different component, and returns its position relative
  290. to this component. If the sourceComponent parameter is null, the source rectangle is assumed to be
  291. a screen coordinate.
  292. If you've used setTransform() to apply one or more transforms to components, then the source rectangle
  293. may not actually be rectangular when converted to the target space, so in that situation this will return
  294. the smallest rectangle that fully contains the transformed area.
  295. */
  296. Rectangle<float> getLocalArea (const Component* sourceComponent,
  297. Rectangle<float> areaRelativeToSourceComponent) const;
  298. /** Converts a point relative to this component's top-left into a screen coordinate.
  299. @see getLocalPoint, localAreaToGlobal
  300. */
  301. Point<int> localPointToGlobal (Point<int> localPoint) const;
  302. /** Converts a point relative to this component's top-left into a screen coordinate.
  303. @see getLocalPoint, localAreaToGlobal
  304. */
  305. Point<float> localPointToGlobal (Point<float> localPoint) const;
  306. /** Converts a rectangle from this component's coordinate space to a screen coordinate.
  307. If you've used setTransform() to apply one or more transforms to components, then the source rectangle
  308. may not actually be rectangular when converted to the target space, so in that situation this will return
  309. the smallest rectangle that fully contains the transformed area.
  310. @see getLocalPoint, localPointToGlobal
  311. */
  312. Rectangle<int> localAreaToGlobal (Rectangle<int> localArea) const;
  313. /** Converts a rectangle from this component's coordinate space to a screen coordinate.
  314. If you've used setTransform() to apply one or more transforms to components, then the source rectangle
  315. may not actually be rectangular when converted to the target space, so in that situation this will return
  316. the smallest rectangle that fully contains the transformed area.
  317. @see getLocalPoint, localPointToGlobal
  318. */
  319. Rectangle<float> localAreaToGlobal (Rectangle<float> localArea) const;
  320. //==============================================================================
  321. /** Moves the component to a new position.
  322. Changes the component's top-left position (without changing its size).
  323. The position is relative to the top-left of the component's parent.
  324. If the component actually moves, this method will make a synchronous call to moved().
  325. Note that if you've used setTransform() to apply a transform, then the component's
  326. bounds will no longer be a direct reflection of the position at which it appears within
  327. its parent, as the transform will be applied to whatever bounds you set for it.
  328. @see setBounds, ComponentListener::componentMovedOrResized
  329. */
  330. void setTopLeftPosition (int x, int y);
  331. /** Moves the component to a new position.
  332. Changes the component's top-left position (without changing its size).
  333. The position is relative to the top-left of the component's parent.
  334. If the component actually moves, this method will make a synchronous call to moved().
  335. Note that if you've used setTransform() to apply a transform, then the component's
  336. bounds will no longer be a direct reflection of the position at which it appears within
  337. its parent, as the transform will be applied to whatever bounds you set for it.
  338. @see setBounds, ComponentListener::componentMovedOrResized
  339. */
  340. void setTopLeftPosition (Point<int> newTopLeftPosition);
  341. /** Moves the component to a new position.
  342. Changes the position of the component's top-right corner (keeping it the same size).
  343. The position is relative to the top-left of the component's parent.
  344. If the component actually moves, this method will make a synchronous call to moved().
  345. Note that if you've used setTransform() to apply a transform, then the component's
  346. bounds will no longer be a direct reflection of the position at which it appears within
  347. its parent, as the transform will be applied to whatever bounds you set for it.
  348. */
  349. void setTopRightPosition (int x, int y);
  350. /** Changes the size of the component.
  351. A synchronous call to resized() will occur if the size actually changes.
  352. Note that if you've used setTransform() to apply a transform, then the component's
  353. bounds will no longer be a direct reflection of the position at which it appears within
  354. its parent, as the transform will be applied to whatever bounds you set for it.
  355. */
  356. void setSize (int newWidth, int newHeight);
  357. /** Changes the component's position and size.
  358. The coordinates are relative to the top-left of the component's parent, or relative
  359. to the origin of the screen if the component is on the desktop.
  360. If this method changes the component's top-left position, it will make a synchronous
  361. call to moved(). If it changes the size, it will also make a call to resized().
  362. Note that if you've used setTransform() to apply a transform, then the component's
  363. bounds will no longer be a direct reflection of the position at which it appears within
  364. its parent, as the transform will be applied to whatever bounds you set for it.
  365. @see setTopLeftPosition, setSize, ComponentListener::componentMovedOrResized
  366. */
  367. void setBounds (int x, int y, int width, int height);
  368. /** Changes the component's position and size.
  369. The coordinates are relative to the top-left of the component's parent, or relative
  370. to the origin of the screen if the component is on the desktop.
  371. If this method changes the component's top-left position, it will make a synchronous
  372. call to moved(). If it changes the size, it will also make a call to resized().
  373. Note that if you've used setTransform() to apply a transform, then the component's
  374. bounds will no longer be a direct reflection of the position at which it appears within
  375. its parent, as the transform will be applied to whatever bounds you set for it.
  376. @see setBounds
  377. */
  378. void setBounds (Rectangle<int> newBounds);
  379. /** Changes the component's position and size in terms of fractions of its parent's size.
  380. The values are factors of the parent's size, so for example
  381. setBoundsRelative (0.2f, 0.2f, 0.5f, 0.5f) would give it half the
  382. width and height of the parent, with its top-left position 20% of
  383. the way across and down the parent.
  384. @see setBounds
  385. */
  386. void setBoundsRelative (float proportionalX, float proportionalY,
  387. float proportionalWidth, float proportionalHeight);
  388. /** Changes the component's position and size in terms of fractions of its parent's size.
  389. The values are factors of the parent's size, so for example
  390. setBoundsRelative ({ 0.2f, 0.2f, 0.5f, 0.5f }) would give it half the
  391. width and height of the parent, with its top-left position 20% of
  392. the way across and down the parent.
  393. @see setBounds
  394. */
  395. void setBoundsRelative (Rectangle<float> proportionalArea);
  396. /** Changes the component's position and size based on the amount of space to leave around it.
  397. This will position the component within its parent, leaving the specified number of
  398. pixels around each edge.
  399. @see setBounds
  400. */
  401. void setBoundsInset (BorderSize<int> borders);
  402. /** Positions the component within a given rectangle, keeping its proportions
  403. unchanged.
  404. If onlyReduceInSize is false, the component will be resized to fill as much of the
  405. rectangle as possible without changing its aspect ratio (the component's
  406. current size is used to determine its aspect ratio, so a zero-size component
  407. won't work here). If onlyReduceInSize is true, it will only be resized if it's
  408. too big to fit inside the rectangle.
  409. It will then be positioned within the rectangle according to the justification flags
  410. specified.
  411. @see setBounds
  412. */
  413. void setBoundsToFit (Rectangle<int> targetArea,
  414. Justification justification,
  415. bool onlyReduceInSize);
  416. /** Changes the position of the component's centre.
  417. Leaves the component's size unchanged, but sets the position of its centre
  418. relative to its parent's top-left.
  419. @see setBounds
  420. */
  421. void setCentrePosition (int x, int y);
  422. /** Changes the position of the component's centre.
  423. Leaves the component's size unchanged, but sets the position of its centre
  424. relative to its parent's top-left.
  425. @see setBounds
  426. */
  427. void setCentrePosition (Point<int> newCentrePosition);
  428. /** Changes the position of the component's centre.
  429. Leaves the size unchanged, but positions its centre relative to its parent's size.
  430. E.g. setCentreRelative (0.5f, 0.5f) would place it centrally in its parent.
  431. */
  432. void setCentreRelative (float x, float y);
  433. /** Changes the component's size and centres it within its parent.
  434. After changing the size, the component will be moved so that it's
  435. centred within its parent. If the component is on the desktop (or has no
  436. parent component), then it'll be centred within the main monitor area.
  437. */
  438. void centreWithSize (int width, int height);
  439. //==============================================================================
  440. /** Sets a transform matrix to be applied to this component.
  441. If you set a transform for a component, the component's position will be warped by it, relative to
  442. the component's parent's top-left origin. This means that the values you pass into setBounds() will no
  443. longer reflect the actual area within the parent that the component covers, as the bounds will be
  444. transformed and the component will probably end up actually appearing somewhere else within its parent.
  445. When using transforms you need to be extremely careful when converting coordinates between the
  446. coordinate spaces of different components or the screen - you should always use getLocalPoint(),
  447. getLocalArea(), etc to do this, and never just manually add a component's position to a point in order to
  448. convert it between different components (but I'm sure you would never have done that anyway...).
  449. Currently, transforms are not supported for desktop windows, so the transform will be ignored if you
  450. put a component on the desktop.
  451. To remove a component's transform, simply pass AffineTransform() as the parameter to this method.
  452. */
  453. void setTransform (const AffineTransform& transform);
  454. /** Returns the transform that is currently being applied to this component.
  455. For more details about transforms, see setTransform().
  456. @see setTransform
  457. */
  458. AffineTransform getTransform() const;
  459. /** Returns true if a non-identity transform is being applied to this component.
  460. For more details about transforms, see setTransform().
  461. @see setTransform
  462. */
  463. bool isTransformed() const noexcept;
  464. /** Returns the approximate scale factor for a given component by traversing its parent hierarchy
  465. and applying each transform and finally scaling this by the global scale factor.
  466. */
  467. static float JUCE_CALLTYPE getApproximateScaleFactorForComponent (const Component* targetComponent);
  468. //==============================================================================
  469. /** Returns a proportion of the component's width.
  470. This is a handy equivalent of (getWidth() * proportion).
  471. */
  472. int proportionOfWidth (float proportion) const noexcept;
  473. /** Returns a proportion of the component's height.
  474. This is a handy equivalent of (getHeight() * proportion).
  475. */
  476. int proportionOfHeight (float proportion) const noexcept;
  477. /** Returns the width of the component's parent.
  478. If the component has no parent (i.e. if it's on the desktop), this will return
  479. the width of the screen.
  480. */
  481. int getParentWidth() const noexcept;
  482. /** Returns the height of the component's parent.
  483. If the component has no parent (i.e. if it's on the desktop), this will return
  484. the height of the screen.
  485. */
  486. int getParentHeight() const noexcept;
  487. /** Returns the screen coordinates of the monitor that contains this component.
  488. If there's only one monitor, this will return its size - if there are multiple
  489. monitors, it will return the area of the monitor that contains the component's
  490. centre.
  491. */
  492. Rectangle<int> getParentMonitorArea() const;
  493. //==============================================================================
  494. /** Returns the number of child components that this component contains.
  495. @see getChildren, getChildComponent, getIndexOfChildComponent
  496. */
  497. int getNumChildComponents() const noexcept;
  498. /** Returns one of this component's child components, by it index.
  499. The component with index 0 is at the back of the z-order, the one at the
  500. front will have index (getNumChildComponents() - 1).
  501. If the index is out-of-range, this will return a null pointer.
  502. @see getChildren, getNumChildComponents, getIndexOfChildComponent
  503. */
  504. Component* getChildComponent (int index) const noexcept;
  505. /** Returns the index of this component in the list of child components.
  506. A value of 0 means it is first in the list (i.e. behind all other components). Higher
  507. values are further towards the front.
  508. Returns -1 if the component passed-in is not a child of this component.
  509. @see getChildren, getNumChildComponents, getChildComponent, addChildComponent, toFront, toBack, toBehind
  510. */
  511. int getIndexOfChildComponent (const Component* child) const noexcept;
  512. /** Provides access to the underlying array of child components.
  513. The most likely reason you may want to use this is for iteration in a range-based for loop.
  514. */
  515. const Array<Component*>& getChildren() const noexcept { return childComponentList; }
  516. /** Looks for a child component with the specified ID.
  517. @see setComponentID, getComponentID
  518. */
  519. Component* findChildWithID (StringRef componentID) const noexcept;
  520. /** Adds a child component to this one.
  521. Adding a child component does not mean that the component will own or delete the child - it's
  522. your responsibility to delete the component. Note that it's safe to delete a component
  523. without first removing it from its parent - doing so will automatically remove it and
  524. send out the appropriate notifications before the deletion completes.
  525. If the child is already a child of this component, then no action will be taken, and its
  526. z-order will be left unchanged.
  527. @param child the new component to add. If the component passed-in is already
  528. the child of another component, it'll first be removed from its current parent.
  529. @param zOrder The index in the child-list at which this component should be inserted.
  530. A value of -1 will insert it in front of the others, 0 is the back.
  531. @see removeChildComponent, addAndMakeVisible, addChildAndSetID, getChild, ComponentListener::componentChildrenChanged
  532. */
  533. void addChildComponent (Component* child, int zOrder = -1);
  534. /** Adds a child component to this one.
  535. Adding a child component does not mean that the component will own or delete the child - it's
  536. your responsibility to delete the component. Note that it's safe to delete a component
  537. without first removing it from its parent - doing so will automatically remove it and
  538. send out the appropriate notifications before the deletion completes.
  539. If the child is already a child of this component, then no action will be taken, and its
  540. z-order will be left unchanged.
  541. @param child the new component to add. If the component passed-in is already
  542. the child of another component, it'll first be removed from its current parent.
  543. @param zOrder The index in the child-list at which this component should be inserted.
  544. A value of -1 will insert it in front of the others, 0 is the back.
  545. @see removeChildComponent, addAndMakeVisible, addChildAndSetID, getChild, ComponentListener::componentChildrenChanged
  546. */
  547. void addChildComponent (Component& child, int zOrder = -1);
  548. /** Adds a child component to this one, and also makes the child visible if it isn't already.
  549. This is the same as calling setVisible (true) on the child and then addChildComponent().
  550. See addChildComponent() for more details.
  551. @param child the new component to add. If the component passed-in is already
  552. the child of another component, it'll first be removed from its current parent.
  553. @param zOrder The index in the child-list at which this component should be inserted.
  554. A value of -1 will insert it in front of the others, 0 is the back.
  555. */
  556. void addAndMakeVisible (Component* child, int zOrder = -1);
  557. /** Adds a child component to this one, and also makes the child visible if it isn't already.
  558. This is the same as calling setVisible (true) on the child and then addChildComponent().
  559. See addChildComponent() for more details.
  560. @param child the new component to add. If the component passed-in is already
  561. the child of another component, it'll first be removed from its current parent.
  562. @param zOrder The index in the child-list at which this component should be inserted.
  563. A value of -1 will insert it in front of the others, 0 is the back.
  564. */
  565. void addAndMakeVisible (Component& child, int zOrder = -1);
  566. /** Adds a child component to this one, makes it visible, and sets its component ID.
  567. @see addAndMakeVisible, addChildComponent
  568. */
  569. void addChildAndSetID (Component* child, const String& componentID);
  570. /** Removes one of this component's child-components.
  571. If the child passed-in isn't actually a child of this component (either because
  572. it's invalid or is the child of a different parent), then no action is taken.
  573. Note that removing a child will not delete it! But it's ok to delete a component
  574. without first removing it - doing so will automatically remove it and send out the
  575. appropriate notifications before the deletion completes.
  576. @see addChildComponent, ComponentListener::componentChildrenChanged
  577. */
  578. void removeChildComponent (Component* childToRemove);
  579. /** Removes one of this component's child-components by index.
  580. This will return a pointer to the component that was removed, or null if
  581. the index was out-of-range.
  582. Note that removing a child will not delete it! But it's ok to delete a component
  583. without first removing it - doing so will automatically remove it and send out the
  584. appropriate notifications before the deletion completes.
  585. @see addChildComponent, ComponentListener::componentChildrenChanged
  586. */
  587. Component* removeChildComponent (int childIndexToRemove);
  588. /** Removes all this component's children.
  589. Note that this won't delete them! To do that, use deleteAllChildren() instead.
  590. */
  591. void removeAllChildren();
  592. /** Removes and deletes all of this component's children.
  593. My advice is to avoid this method! It's an old function that is only kept here for
  594. backwards-compatibility with legacy code, and should be viewed with extreme
  595. suspicion by anyone attempting to write modern C++. In almost all cases, it's much
  596. smarter to manage the lifetimes of your child components via modern RAII techniques
  597. such as simply making them member variables, or using std::unique_ptr, OwnedArray,
  598. etc to manage their lifetimes appropriately.
  599. @see removeAllChildren
  600. */
  601. void deleteAllChildren();
  602. /** Returns the component which this component is inside.
  603. If this is the highest-level component or hasn't yet been added to
  604. a parent, this will return null.
  605. */
  606. Component* getParentComponent() const noexcept { return parentComponent; }
  607. /** Searches the parent components for a component of a specified class.
  608. For example findParentComponentOfClass \<MyComp\>() would return the first parent
  609. component that can be dynamically cast to a MyComp, or will return nullptr if none
  610. of the parents are suitable.
  611. */
  612. template <class TargetClass>
  613. TargetClass* findParentComponentOfClass() const
  614. {
  615. for (auto* p = parentComponent; p != nullptr; p = p->parentComponent)
  616. if (auto* target = dynamic_cast<TargetClass*> (p))
  617. return target;
  618. return nullptr;
  619. }
  620. /** Returns the highest-level component which contains this one or its parents.
  621. This will search upwards in the parent-hierarchy from this component, until it
  622. finds the highest one that doesn't have a parent (i.e. is on the desktop or
  623. not yet added to a parent), and will return that.
  624. */
  625. Component* getTopLevelComponent() const noexcept;
  626. /** Checks whether a component is anywhere inside this component or its children.
  627. This will recursively check through this component's children to see if the
  628. given component is anywhere inside.
  629. */
  630. bool isParentOf (const Component* possibleChild) const noexcept;
  631. //==============================================================================
  632. /** Called to indicate that the component's parents have changed.
  633. When a component is added or removed from its parent, this method will
  634. be called on all of its children (recursively - so all children of its
  635. children will also be called as well).
  636. Subclasses can override this if they need to react to this in some way.
  637. @see getParentComponent, isShowing, ComponentListener::componentParentHierarchyChanged
  638. */
  639. virtual void parentHierarchyChanged();
  640. /** Subclasses can use this callback to be told when children are added or removed, or
  641. when their z-order changes.
  642. @see parentHierarchyChanged, ComponentListener::componentChildrenChanged
  643. */
  644. virtual void childrenChanged();
  645. //==============================================================================
  646. /** Tests whether a given point is inside the component.
  647. Overriding this method allows you to create components which only intercept
  648. mouse-clicks within a user-defined area.
  649. This is called to find out whether a particular x, y coordinate is
  650. considered to be inside the component or not, and is used by methods such
  651. as contains() and getComponentAt() to work out which component
  652. the mouse is clicked on.
  653. Components with custom shapes will probably want to override it to perform
  654. some more complex hit-testing.
  655. The default implementation of this method returns either true or false,
  656. depending on the value that was set by calling setInterceptsMouseClicks() (true
  657. is the default return value).
  658. Note that the hit-test region is not related to the opacity with which
  659. areas of a component are painted.
  660. Applications should never call hitTest() directly - instead use the
  661. contains() method, because this will also test for occlusion by the
  662. component's parent.
  663. Note that for components on the desktop, this method will be ignored, because it's
  664. not always possible to implement this behaviour on all platforms.
  665. @param x the x coordinate to test, relative to the left hand edge of this
  666. component. This value is guaranteed to be greater than or equal to
  667. zero, and less than the component's width
  668. @param y the y coordinate to test, relative to the top edge of this
  669. component. This value is guaranteed to be greater than or equal to
  670. zero, and less than the component's height
  671. @returns true if the click is considered to be inside the component
  672. @see setInterceptsMouseClicks, contains
  673. */
  674. virtual bool hitTest (int x, int y);
  675. /** Changes the default return value for the hitTest() method.
  676. Setting this to false is an easy way to make a component pass all its mouse events
  677. (not just clicks) through to the components behind it.
  678. When a component is created, the default setting for this is true.
  679. @param allowClicksOnThisComponent if true, hitTest() will always return true; if false, it will
  680. return false (or true for child components if allowClicksOnChildComponents
  681. is true)
  682. @param allowClicksOnChildComponents if this is true and allowClicksOnThisComponent is false, then child
  683. components can be clicked on as normal but clicks on this component pass
  684. straight through; if this is false and allowClicksOnThisComponent
  685. is false, then neither this component nor any child components can
  686. be clicked on
  687. @see hitTest, getInterceptsMouseClicks
  688. */
  689. void setInterceptsMouseClicks (bool allowClicksOnThisComponent,
  690. bool allowClicksOnChildComponents) noexcept;
  691. /** Retrieves the current state of the mouse-click interception flags.
  692. On return, the two parameters are set to the state used in the last call to
  693. setInterceptsMouseClicks().
  694. @see setInterceptsMouseClicks
  695. */
  696. void getInterceptsMouseClicks (bool& allowsClicksOnThisComponent,
  697. bool& allowsClicksOnChildComponents) const noexcept;
  698. /** Returns true if a given point lies within this component or one of its children.
  699. Never override this method! Use hitTest to create custom hit regions.
  700. @param localPoint the coordinate to test, relative to this component's top-left.
  701. @returns true if the point is within the component's hit-test area, but only if
  702. that part of the component isn't clipped by its parent component. Note
  703. that this won't take into account any overlapping sibling components
  704. which might be in the way - for that, see reallyContains()
  705. @see hitTest, reallyContains, getComponentAt
  706. */
  707. bool contains (Point<int> localPoint);
  708. /** Returns true if a given point lies within this component or one of its children.
  709. Never override this method! Use hitTest to create custom hit regions.
  710. @param localPoint the coordinate to test, relative to this component's top-left.
  711. @returns true if the point is within the component's hit-test area, but only if
  712. that part of the component isn't clipped by its parent component. Note
  713. that this won't take into account any overlapping sibling components
  714. which might be in the way - for that, see reallyContains()
  715. @see hitTest, reallyContains, getComponentAt
  716. */
  717. bool contains (Point<float> localPoint);
  718. /** Returns true if a given point lies in this component, taking any overlapping
  719. siblings into account.
  720. @param localPoint the coordinate to test, relative to this component's top-left.
  721. @param returnTrueIfWithinAChild if the point actually lies within a child of this component,
  722. this determines whether that is counted as a hit.
  723. @see contains, getComponentAt
  724. */
  725. bool reallyContains (Point<int> localPoint, bool returnTrueIfWithinAChild);
  726. /** Returns true if a given point lies in this component, taking any overlapping
  727. siblings into account.
  728. @param localPoint the coordinate to test, relative to this component's top-left.
  729. @param returnTrueIfWithinAChild if the point actually lies within a child of this component,
  730. this determines whether that is counted as a hit.
  731. @see contains, getComponentAt
  732. */
  733. bool reallyContains (Point<float> localPoint, bool returnTrueIfWithinAChild);
  734. /** Returns the component at a certain point within this one.
  735. @param x the x coordinate to test, relative to this component's left edge.
  736. @param y the y coordinate to test, relative to this component's top edge.
  737. @returns the component that is at this position - which may be 0, this component,
  738. or one of its children. Note that overlapping siblings that might actually
  739. be in the way are not taken into account by this method - to account for these,
  740. instead call getComponentAt on the top-level parent of this component.
  741. @see hitTest, contains, reallyContains
  742. */
  743. Component* getComponentAt (int x, int y);
  744. /** Returns the component at a certain point within this one.
  745. @param position the coordinate to test, relative to this component's top-left.
  746. @returns the component that is at this position - which may be 0, this component,
  747. or one of its children. Note that overlapping siblings that might actually
  748. be in the way are not taken into account by this method - to account for these,
  749. instead call getComponentAt on the top-level parent of this component.
  750. @see hitTest, contains, reallyContains
  751. */
  752. Component* getComponentAt (Point<int> position);
  753. /** Returns the component at a certain point within this one.
  754. @param position the coordinate to test, relative to this component's top-left.
  755. @returns the component that is at this position - which may be 0, this component,
  756. or one of its children. Note that overlapping siblings that might actually
  757. be in the way are not taken into account by this method - to account for these,
  758. instead call getComponentAt on the top-level parent of this component.
  759. @see hitTest, contains, reallyContains
  760. */
  761. Component* getComponentAt (Point<float> position);
  762. //==============================================================================
  763. /** Marks the whole component as needing to be redrawn.
  764. Calling this will not do any repainting immediately, but will mark the component
  765. as 'dirty'. At some point in the near future the operating system will send a paint
  766. message, which will redraw all the dirty regions of all components.
  767. There's no guarantee about how soon after calling repaint() the redraw will actually
  768. happen, and other queued events may be delivered before a redraw is done.
  769. If the setBufferedToImage() method has been used to cause this component to use a
  770. buffer, the repaint() call will invalidate the cached buffer. If setCachedComponentImage()
  771. has been used to provide a custom image cache, that cache will be invalidated appropriately.
  772. To redraw just a subsection of the component rather than the whole thing,
  773. use the repaint (int, int, int, int) method.
  774. @see paint
  775. */
  776. void repaint();
  777. /** Marks a subsection of this component as needing to be redrawn.
  778. Calling this will not do any repainting immediately, but will mark the given region
  779. of the component as 'dirty'. At some point in the near future the operating system
  780. will send a paint message, which will redraw all the dirty regions of all components.
  781. There's no guarantee about how soon after calling repaint() the redraw will actually
  782. happen, and other queued events may be delivered before a redraw is done.
  783. The region that is passed in will be clipped to keep it within the bounds of this
  784. component.
  785. @see repaint()
  786. */
  787. void repaint (int x, int y, int width, int height);
  788. /** Marks a subsection of this component as needing to be redrawn.
  789. Calling this will not do any repainting immediately, but will mark the given region
  790. of the component as 'dirty'. At some point in the near future the operating system
  791. will send a paint message, which will redraw all the dirty regions of all components.
  792. There's no guarantee about how soon after calling repaint() the redraw will actually
  793. happen, and other queued events may be delivered before a redraw is done.
  794. The region that is passed in will be clipped to keep it within the bounds of this
  795. component.
  796. @see repaint()
  797. */
  798. void repaint (Rectangle<int> area);
  799. //==============================================================================
  800. /** Makes the component use an internal buffer to optimise its redrawing.
  801. Setting this flag to true will cause the component to allocate an
  802. internal buffer into which it paints itself and all its child components, so that
  803. when asked to redraw itself, it can use this buffer rather than actually calling
  804. the paint() method.
  805. Parts of the buffer are invalidated when repaint() is called on this component
  806. or its children. The buffer is then repainted at the next paint() callback.
  807. @see repaint, paint, createComponentSnapshot
  808. */
  809. void setBufferedToImage (bool shouldBeBuffered);
  810. /** Generates a snapshot of part of this component.
  811. This will return a new Image, the size of the rectangle specified,
  812. containing a snapshot of the specified area of the component and all
  813. its children.
  814. The image may or may not have an alpha-channel, depending on whether the
  815. image is opaque or not.
  816. If the clipImageToComponentBounds parameter is true and the area is greater than
  817. the size of the component, it'll be clipped. If clipImageToComponentBounds is false
  818. then parts of the component beyond its bounds can be drawn.
  819. @see paintEntireComponent
  820. */
  821. Image createComponentSnapshot (Rectangle<int> areaToGrab,
  822. bool clipImageToComponentBounds = true,
  823. float scaleFactor = 1.0f);
  824. /** Draws this component and all its subcomponents onto the specified graphics
  825. context.
  826. You should very rarely have to use this method, it's simply there in case you need
  827. to draw a component with a custom graphics context for some reason, e.g. for
  828. creating a snapshot of the component.
  829. It calls paint(), paintOverChildren() and recursively calls paintEntireComponent()
  830. on its children in order to render the entire tree.
  831. The graphics context may be left in an undefined state after this method returns,
  832. so you may need to reset it if you're going to use it again.
  833. If ignoreAlphaLevel is false, then the component will be drawn with the opacity level
  834. specified by getAlpha(); if ignoreAlphaLevel is true, then this will be ignored and
  835. an alpha of 1.0 will be used.
  836. */
  837. void paintEntireComponent (Graphics& context, bool ignoreAlphaLevel);
  838. /** This allows you to indicate that this component doesn't require its graphics
  839. context to be clipped when it is being painted.
  840. Most people will never need to use this setting, but in situations where you have a very large
  841. number of simple components being rendered, and where they are guaranteed never to do any drawing
  842. beyond their own boundaries, setting this to true will reduce the overhead involved in clipping
  843. the graphics context that gets passed to the component's paint() callback.
  844. If you enable this mode, you'll need to make sure your paint method doesn't call anything like
  845. Graphics::fillAll(), and doesn't draw beyond the component's bounds, because that'll produce
  846. artifacts. This option will have no effect on components that contain any child components.
  847. */
  848. void setPaintingIsUnclipped (bool shouldPaintWithoutClipping) noexcept;
  849. /** Returns true if this component doesn't require its graphics context to be clipped
  850. when it is being painted.
  851. */
  852. bool isPaintingUnclipped() const noexcept;
  853. //==============================================================================
  854. /** Adds an effect filter to alter the component's appearance.
  855. When a component has an effect filter set, then this is applied to the
  856. results of its paint() method. There are a few preset effects, such as
  857. a drop-shadow or glow, but they can be user-defined as well.
  858. The effect that is passed in will not be deleted by the component - the
  859. caller must take care of deleting it.
  860. To remove an effect from a component, pass a null pointer in as the parameter.
  861. @see ImageEffectFilter, DropShadowEffect, GlowEffect
  862. */
  863. void setComponentEffect (ImageEffectFilter* newEffect);
  864. /** Returns the current component effect.
  865. @see setComponentEffect
  866. */
  867. ImageEffectFilter* getComponentEffect() const noexcept { return effect; }
  868. //==============================================================================
  869. /** Finds the appropriate look-and-feel to use for this component.
  870. If the component hasn't had a look-and-feel explicitly set, this will
  871. return the parent's look-and-feel, or just the default one if there's no
  872. parent.
  873. @see setLookAndFeel, lookAndFeelChanged
  874. */
  875. LookAndFeel& getLookAndFeel() const noexcept;
  876. /** Sets the look and feel to use for this component.
  877. This will also change the look and feel for any child components that haven't
  878. had their look set explicitly.
  879. The object passed in will not be deleted by the component, so it's the caller's
  880. responsibility to manage it. It may be used at any time until this component
  881. has been deleted.
  882. Calling this method will also invoke the sendLookAndFeelChange() method.
  883. @see getLookAndFeel, lookAndFeelChanged
  884. */
  885. void setLookAndFeel (LookAndFeel* newLookAndFeel);
  886. /** Called to let the component react to a change in the look-and-feel setting.
  887. When the look-and-feel is changed for a component, this will be called in
  888. all its child components, recursively.
  889. It can also be triggered manually by the sendLookAndFeelChange() method, in case
  890. an application uses a LookAndFeel class that might have changed internally.
  891. @see sendLookAndFeelChange, getLookAndFeel
  892. */
  893. virtual void lookAndFeelChanged();
  894. /** Calls the lookAndFeelChanged() method in this component and all its children.
  895. This will recurse through the children and their children, calling lookAndFeelChanged()
  896. on them all.
  897. @see lookAndFeelChanged
  898. */
  899. void sendLookAndFeelChange();
  900. //==============================================================================
  901. /** Indicates whether any parts of the component might be transparent.
  902. Components that always paint all of their contents with solid colour and
  903. thus completely cover any components behind them should use this method
  904. to tell the repaint system that they are opaque.
  905. This information is used to optimise drawing, because it means that
  906. objects underneath opaque windows don't need to be painted.
  907. By default, components are considered transparent, unless this is used to
  908. make it otherwise.
  909. @see isOpaque
  910. */
  911. void setOpaque (bool shouldBeOpaque);
  912. /** Returns true if no parts of this component are transparent.
  913. @returns the value that was set by setOpaque, (the default being false)
  914. @see setOpaque
  915. */
  916. bool isOpaque() const noexcept;
  917. //==============================================================================
  918. /** Indicates whether the component should be brought to the front when clicked.
  919. Setting this flag to true will cause the component to be brought to the front
  920. when the mouse is clicked somewhere inside it or its child components.
  921. Note that a top-level desktop window might still be brought to the front by the
  922. operating system when it's clicked, depending on how the OS works.
  923. By default this is set to false.
  924. @see setMouseClickGrabsKeyboardFocus
  925. */
  926. void setBroughtToFrontOnMouseClick (bool shouldBeBroughtToFront) noexcept;
  927. /** Indicates whether the component should be brought to the front when clicked-on.
  928. @see setBroughtToFrontOnMouseClick
  929. */
  930. bool isBroughtToFrontOnMouseClick() const noexcept;
  931. //==============================================================================
  932. // Focus methods
  933. /** Sets the focus order of this component.
  934. The focus order is used by the default traverser implementation returned by
  935. createFocusTraverser() as part of its algorithm for deciding the order in
  936. which components should be traversed. A value of 0 or less is taken to mean
  937. that no explicit order is wanted, and that traversal should use other
  938. factors, like the component's position.
  939. @see getExplicitFocusOrder, FocusTraverser, createFocusTraverser
  940. */
  941. void setExplicitFocusOrder (int newFocusOrderIndex);
  942. /** Returns the focus order of this component, if one has been specified.
  943. By default components don't have a focus order - in that case, this will
  944. return 0.
  945. @see setExplicitFocusOrder
  946. */
  947. int getExplicitFocusOrder() const;
  948. /** A focus container type that can be passed to setFocusContainerType().
  949. If a component is marked as a focus container or keyboard focus container then
  950. it will act as the top-level component within which focus or keyboard focus is
  951. passed around. By default components are considered "focusable" if they are visible
  952. and enabled and "keyboard focusable" if `getWantsKeyboardFocus() == true`.
  953. The order of traversal within a focus container is determined by the objects
  954. returned by createFocusTraverser() and createKeyboardFocusTraverser(),
  955. respectively - see the documentation of the default FocusContainer and
  956. KeyboardFocusContainer implementations for more information.
  957. */
  958. enum class FocusContainerType
  959. {
  960. /** The component will not act as a focus container.
  961. This is the default setting for non top-level components and means that it and any
  962. sub-components are navigable within their containing focus container.
  963. */
  964. none,
  965. /** The component will act as a top-level component within which focus is passed around.
  966. The default traverser implementation returned by createFocusTraverser() will use this
  967. flag to find the first parent component (of the currently focused one) that wants to
  968. be a focus container.
  969. This is currently used when determining the hierarchy of accessible UI elements presented
  970. to screen reader clients on supported platforms. See the AccessibilityHandler class for
  971. more information.
  972. */
  973. focusContainer,
  974. /** The component will act as a top-level component within which keyboard focus is passed around.
  975. The default traverser implementation returned by createKeyboardFocusTraverser() will
  976. use this flag to find the first parent component (of the currently focused one) that
  977. wants to be a keyboard focus container.
  978. This is currently used when determining how keyboard focus is passed between components
  979. that have been marked as keyboard focusable with setWantsKeyboardFocus() when clicking
  980. on components and navigating with the tab key.
  981. */
  982. keyboardFocusContainer
  983. };
  984. /** Sets whether this component is a container for components that can have
  985. their focus traversed, and the type of focus traversal that it supports.
  986. @see FocusContainerType, isFocusContainer, isKeyboardFocusContainer,
  987. FocusTraverser, createFocusTraverser,
  988. KeyboardFocusTraverser, createKeyboardFocusTraverser
  989. */
  990. void setFocusContainerType (FocusContainerType containerType) noexcept;
  991. /** Returns true if this component has been marked as a focus container.
  992. @see setFocusContainerType
  993. */
  994. bool isFocusContainer() const noexcept;
  995. /** Returns true if this component has been marked as a keyboard focus container.
  996. @see setFocusContainerType
  997. */
  998. bool isKeyboardFocusContainer() const noexcept;
  999. /** Returns the focus container for this component.
  1000. @see isFocusContainer, setFocusContainerType
  1001. */
  1002. Component* findFocusContainer() const;
  1003. /** Returns the keyboard focus container for this component.
  1004. @see isFocusContainer, setFocusContainerType
  1005. */
  1006. Component* findKeyboardFocusContainer() const;
  1007. //==============================================================================
  1008. /** Sets a flag to indicate whether this component wants keyboard focus or not.
  1009. By default components aren't actually interested in gaining the keyboard
  1010. focus, but this method can be used to turn this on.
  1011. See the grabKeyboardFocus() method for details about the way a component
  1012. is chosen to receive the focus.
  1013. @see grabKeyboardFocus, giveAwayKeyboardFocus, getWantsKeyboardFocus
  1014. */
  1015. void setWantsKeyboardFocus (bool wantsFocus) noexcept;
  1016. /** Returns true if the component is interested in getting keyboard focus.
  1017. This returns the flag set by setWantsKeyboardFocus(). The default setting
  1018. is false.
  1019. @see setWantsKeyboardFocus
  1020. */
  1021. bool getWantsKeyboardFocus() const noexcept;
  1022. /** Chooses whether a click on this component automatically grabs the focus.
  1023. By default this is set to true, but you might want a component which can
  1024. be focused, but where you don't want the user to be able to affect it
  1025. directly by clicking.
  1026. */
  1027. void setMouseClickGrabsKeyboardFocus (bool shouldGrabFocus);
  1028. /** Returns the last value set with setMouseClickGrabsKeyboardFocus().
  1029. @see setMouseClickGrabsKeyboardFocus
  1030. */
  1031. bool getMouseClickGrabsKeyboardFocus() const noexcept;
  1032. /** Tries to give keyboard focus to this component.
  1033. When the user clicks on a component or its grabKeyboardFocus() method is
  1034. called, the following procedure is used to work out which component should
  1035. get it:
  1036. - if the component that was clicked on actually wants focus (as indicated
  1037. by calling getWantsKeyboardFocus), it gets it.
  1038. - if the component itself doesn't want focus, it will try to pass it
  1039. on to whichever of its children is the default component, as determined by
  1040. the getDefaultComponent() implementation of the ComponentTraverser returned
  1041. by createKeyboardFocusTraverser().
  1042. - if none of its children want focus at all, it will pass it up to its
  1043. parent instead, unless it's a top-level component without a parent,
  1044. in which case it just takes the focus itself.
  1045. Important note! It's obviously not possible for a component to be focused
  1046. unless it's actually visible, on-screen, and inside a window that is also
  1047. visible. So there's no point trying to call this in the component's own
  1048. constructor or before all of its parent hierarchy has been fully instantiated.
  1049. @see giveAwayKeyboardFocus, setWantsKeyboardFocus, getWantsKeyboardFocus,
  1050. hasKeyboardFocus, getCurrentlyFocusedComponent, focusGained, focusLost,
  1051. keyPressed, keyStateChanged
  1052. */
  1053. void grabKeyboardFocus();
  1054. /** If this component or any of its children currently have the keyboard focus,
  1055. this will defocus it, send a focus change notification, and try to pass the
  1056. focus to the next component.
  1057. @see grabKeyboardFocus, setWantsKeyboardFocus, getCurrentlyFocusedComponent,
  1058. focusGained, focusLost
  1059. */
  1060. void giveAwayKeyboardFocus();
  1061. /** Returns true if this component currently has the keyboard focus.
  1062. @param trueIfChildIsFocused if this is true, then the method returns true if
  1063. either this component or any of its children (recursively)
  1064. have the focus. If false, the method only returns true if
  1065. this component has the focus.
  1066. @see grabKeyboardFocus, giveAwayKeyboardFocus, setWantsKeyboardFocus,
  1067. getCurrentlyFocusedComponent, focusGained, focusLost
  1068. */
  1069. bool hasKeyboardFocus (bool trueIfChildIsFocused) const;
  1070. /** Tries to move the keyboard focus to one of this component's siblings.
  1071. This will try to move focus to either the next or previous component, as
  1072. determined by the getNextComponent() and getPreviousComponent() implementations
  1073. of the ComponentTraverser returned by createKeyboardFocusTraverser().
  1074. This is the method that is used when shifting focus by pressing the tab key.
  1075. @param moveToNext if true, the focus will move forwards; if false, it will
  1076. move backwards
  1077. @see grabKeyboardFocus, giveAwayKeyboardFocus, setFocusContainerType, setWantsKeyboardFocus
  1078. */
  1079. void moveKeyboardFocusToSibling (bool moveToNext);
  1080. /** Returns the component that currently has the keyboard focus.
  1081. @returns the focused component, or nullptr if nothing is focused.
  1082. */
  1083. static Component* JUCE_CALLTYPE getCurrentlyFocusedComponent() noexcept;
  1084. /** If any component has keyboard focus, this will defocus it. */
  1085. static void JUCE_CALLTYPE unfocusAllComponents();
  1086. //==============================================================================
  1087. /** Creates a ComponentTraverser object to determine the logic by which focus should be
  1088. passed from this component.
  1089. The default implementation of this method will return an instance of FocusTraverser
  1090. if this component is a focus container (as determined by the setFocusContainerType()
  1091. method). If the component isn't a focus container, then it will recursively call
  1092. createFocusTraverser() on its parents.
  1093. If you override this to return a custom traverser object, then this component and
  1094. all its sub-components will use the new object to make their focusing decisions.
  1095. */
  1096. virtual std::unique_ptr<ComponentTraverser> createFocusTraverser();
  1097. /** Creates a ComponentTraverser object to use to determine the logic by which keyboard
  1098. focus should be passed from this component.
  1099. The default implementation of this method will return an instance of
  1100. KeyboardFocusTraverser if this component is a keyboard focus container (as determined by
  1101. the setFocusContainerType() method). If the component isn't a keyboard focus container,
  1102. then it will recursively call createKeyboardFocusTraverser() on its parents.
  1103. If you override this to return a custom traverser object, then this component and
  1104. all its sub-components will use the new object to make their keyboard focusing
  1105. decisions.
  1106. */
  1107. virtual std::unique_ptr<ComponentTraverser> createKeyboardFocusTraverser();
  1108. /** Use this to indicate that the component should have an outline drawn around it
  1109. when it has keyboard focus.
  1110. If this is set to true, then when the component gains keyboard focus the
  1111. LookAndFeel::createFocusOutlineForComponent() method will be used to draw an outline
  1112. around it.
  1113. @see FocusOutline, hasFocusOutline
  1114. */
  1115. void setHasFocusOutline (bool hasFocusOutline) noexcept { flags.hasFocusOutlineFlag = hasFocusOutline; }
  1116. /** Returns true if this component should have a focus outline.
  1117. @see FocusOutline, setHasFocusOutline
  1118. */
  1119. bool hasFocusOutline() const noexcept { return flags.hasFocusOutlineFlag; }
  1120. //==============================================================================
  1121. /** Returns true if the component (and all its parents) are enabled.
  1122. Components are enabled by default, and can be disabled with setEnabled(). Exactly
  1123. what difference this makes to the component depends on the type. E.g. buttons
  1124. and sliders will choose to draw themselves differently, etc.
  1125. Note that if one of this component's parents is disabled, this will always
  1126. return false, even if this component itself is enabled.
  1127. @see setEnabled, enablementChanged
  1128. */
  1129. bool isEnabled() const noexcept;
  1130. /** Enables or disables this component.
  1131. Disabling a component will also cause all of its child components to become
  1132. disabled.
  1133. Similarly, enabling a component which is inside a disabled parent
  1134. component won't make any difference until the parent is re-enabled.
  1135. @see isEnabled, enablementChanged
  1136. */
  1137. void setEnabled (bool shouldBeEnabled);
  1138. /** Callback to indicate that this component has been enabled or disabled.
  1139. This can be triggered by one of the component's parent components
  1140. being enabled or disabled, as well as changes to the component itself.
  1141. The default implementation of this method does nothing; your class may
  1142. wish to repaint itself or something when this happens.
  1143. @see setEnabled, isEnabled
  1144. */
  1145. virtual void enablementChanged();
  1146. //==============================================================================
  1147. /** Returns the component's current transparency level.
  1148. See setAlpha() for more details.
  1149. */
  1150. float getAlpha() const noexcept;
  1151. /** Changes the transparency of this component.
  1152. When painted, the entire component and all its children will be rendered
  1153. with this as the overall opacity level, where 0 is completely invisible, and
  1154. 1.0 is fully opaque (i.e. normal).
  1155. @see getAlpha, alphaChanged
  1156. */
  1157. void setAlpha (float newAlpha);
  1158. /** Called when setAlpha() is used to change the alpha value of this component.
  1159. If you override this, you should also invoke the base class's implementation
  1160. during your overridden function, as it performs some repainting behaviour.
  1161. */
  1162. virtual void alphaChanged();
  1163. //==============================================================================
  1164. /** Changes the mouse cursor shape to use when the mouse is over this component.
  1165. Note that the cursor set by this method can be overridden by the getMouseCursor
  1166. method.
  1167. @see MouseCursor
  1168. */
  1169. void setMouseCursor (const MouseCursor& cursorType);
  1170. /** Returns the mouse cursor shape to use when the mouse is over this component.
  1171. The default implementation will return the cursor that was set by setCursor()
  1172. but can be overridden for more specialised purposes, e.g. returning different
  1173. cursors depending on the mouse position.
  1174. @see MouseCursor
  1175. */
  1176. virtual MouseCursor getMouseCursor();
  1177. /** Forces the current mouse cursor to be updated.
  1178. If you're overriding the getMouseCursor() method to control which cursor is
  1179. displayed, then this will only be checked each time the user moves the mouse. So
  1180. if you want to force the system to check that the cursor being displayed is
  1181. up-to-date (even if the mouse is just sitting there), call this method.
  1182. (If you're changing the cursor using setMouseCursor(), you don't need to bother
  1183. calling this).
  1184. */
  1185. void updateMouseCursor() const;
  1186. //==============================================================================
  1187. /** Components can override this method to draw their content.
  1188. The paint() method gets called when a region of a component needs redrawing,
  1189. either because the component's repaint() method has been called, or because
  1190. something has happened on the screen that means a section of a window needs
  1191. to be redrawn.
  1192. Any child components will draw themselves over whatever this method draws. If
  1193. you need to paint over the top of your child components, you can also implement
  1194. the paintOverChildren() method to do this.
  1195. If you want to cause a component to redraw itself, this is done asynchronously -
  1196. calling the repaint() method marks a region of the component as "dirty", and the
  1197. paint() method will automatically be called sometime later, by the message thread,
  1198. to paint any bits that need refreshing. In JUCE (and almost all modern UI frameworks),
  1199. you never redraw something synchronously.
  1200. You should never need to call this method directly - to take a snapshot of the
  1201. component you could use createComponentSnapshot() or paintEntireComponent().
  1202. @param g the graphics context that must be used to do the drawing operations.
  1203. @see repaint, paintOverChildren, Graphics
  1204. */
  1205. virtual void paint (Graphics& g);
  1206. /** Components can override this method to draw over the top of their children.
  1207. For most drawing operations, it's better to use the normal paint() method,
  1208. but if you need to overlay something on top of the children, this can be
  1209. used.
  1210. @see paint, Graphics
  1211. */
  1212. virtual void paintOverChildren (Graphics& g);
  1213. //==============================================================================
  1214. /** Called when the mouse moves inside a component.
  1215. If the mouse button isn't pressed and the mouse moves over a component,
  1216. this will be called to let the component react to this.
  1217. A component will always get a mouseEnter callback before a mouseMove.
  1218. @param event details about the position and status of the mouse event, including
  1219. the source component in which it occurred
  1220. @see mouseEnter, mouseExit, mouseDrag, contains
  1221. */
  1222. void mouseMove (const MouseEvent& event) override;
  1223. /** Called when the mouse first enters a component.
  1224. If the mouse button isn't pressed and the mouse moves into a component,
  1225. this will be called to let the component react to this.
  1226. When the mouse button is pressed and held down while being moved in
  1227. or out of a component, no mouseEnter or mouseExit callbacks are made - only
  1228. mouseDrag messages are sent to the component that the mouse was originally
  1229. clicked on, until the button is released.
  1230. @param event details about the position and status of the mouse event, including
  1231. the source component in which it occurred
  1232. @see mouseExit, mouseDrag, mouseMove, contains
  1233. */
  1234. void mouseEnter (const MouseEvent& event) override;
  1235. /** Called when the mouse moves out of a component.
  1236. This will be called when the mouse moves off the edge of this
  1237. component.
  1238. If the mouse button was pressed, and it was then dragged off the
  1239. edge of the component and released, then this callback will happen
  1240. when the button is released, after the mouseUp callback.
  1241. @param event details about the position and status of the mouse event, including
  1242. the source component in which it occurred
  1243. @see mouseEnter, mouseDrag, mouseMove, contains
  1244. */
  1245. void mouseExit (const MouseEvent& event) override;
  1246. /** Called when a mouse button is pressed.
  1247. The MouseEvent object passed in contains lots of methods for finding out
  1248. which button was pressed, as well as which modifier keys (e.g. shift, ctrl)
  1249. were held down at the time.
  1250. Once a button is held down, the mouseDrag method will be called when the
  1251. mouse moves, until the button is released.
  1252. @param event details about the position and status of the mouse event, including
  1253. the source component in which it occurred
  1254. @see mouseUp, mouseDrag, mouseDoubleClick, contains
  1255. */
  1256. void mouseDown (const MouseEvent& event) override;
  1257. /** Called when the mouse is moved while a button is held down.
  1258. When a mouse button is pressed inside a component, that component
  1259. receives mouseDrag callbacks each time the mouse moves, even if the
  1260. mouse strays outside the component's bounds.
  1261. @param event details about the position and status of the mouse event, including
  1262. the source component in which it occurred
  1263. @see mouseDown, mouseUp, mouseMove, contains, setDragRepeatInterval
  1264. */
  1265. void mouseDrag (const MouseEvent& event) override;
  1266. /** Called when a mouse button is released.
  1267. A mouseUp callback is sent to the component in which a button was pressed
  1268. even if the mouse is actually over a different component when the
  1269. button is released.
  1270. The MouseEvent object passed in contains lots of methods for finding out
  1271. which buttons were down just before they were released.
  1272. @param event details about the position and status of the mouse event, including
  1273. the source component in which it occurred
  1274. @see mouseDown, mouseDrag, mouseDoubleClick, contains
  1275. */
  1276. void mouseUp (const MouseEvent& event) override;
  1277. /** Called when a mouse button has been double-clicked on a component.
  1278. The MouseEvent object passed in contains lots of methods for finding out
  1279. which button was pressed, as well as which modifier keys (e.g. shift, ctrl)
  1280. were held down at the time.
  1281. @param event details about the position and status of the mouse event, including
  1282. the source component in which it occurred
  1283. @see mouseDown, mouseUp
  1284. */
  1285. void mouseDoubleClick (const MouseEvent& event) override;
  1286. /** Called when the mouse-wheel is moved.
  1287. This callback is sent to the component that the mouse is over when the
  1288. wheel is moved.
  1289. If not overridden, a component will forward this message to its parent, so
  1290. that parent components can collect mouse-wheel messages that happen to
  1291. child components which aren't interested in them. (Bear in mind that if
  1292. you attach a component as a mouse-listener to other components, then
  1293. those wheel moves will also end up calling this method and being passed up
  1294. to the parents, which may not be what you intended to happen).
  1295. @param event details about the mouse event
  1296. @param wheel details about the mouse wheel movement
  1297. */
  1298. void mouseWheelMove (const MouseEvent& event,
  1299. const MouseWheelDetails& wheel) override;
  1300. /** Called when a pinch-to-zoom mouse-gesture is used.
  1301. If not overridden, a component will forward this message to its parent, so
  1302. that parent components can collect gesture messages that are unused by child
  1303. components.
  1304. @param event details about the mouse event
  1305. @param scaleFactor a multiplier to indicate by how much the size of the target
  1306. should be changed. A value of 1.0 would indicate no change,
  1307. values greater than 1.0 mean it should be enlarged.
  1308. */
  1309. void mouseMagnify (const MouseEvent& event, float scaleFactor) override;
  1310. //==============================================================================
  1311. /** Ensures that a non-stop stream of mouse-drag events will be sent during the
  1312. current mouse-drag operation.
  1313. This allows you to make sure that mouseDrag() events are sent continuously, even
  1314. when the mouse isn't moving. This can be useful for things like auto-scrolling
  1315. components when the mouse is near an edge.
  1316. Call this method during a mouseDown() or mouseDrag() callback, specifying the
  1317. minimum interval between consecutive mouse drag callbacks. The callbacks
  1318. will continue until the mouse is released, and then the interval will be reset,
  1319. so you need to make sure it's called every time you begin a drag event.
  1320. Passing an interval of 0 or less will cancel the auto-repeat.
  1321. @see mouseDrag, Desktop::beginDragAutoRepeat
  1322. */
  1323. static void JUCE_CALLTYPE beginDragAutoRepeat (int millisecondsBetweenCallbacks);
  1324. /** Causes automatic repaints when the mouse enters or exits this component.
  1325. If turned on, then when the mouse enters/exits, or when the button is pressed/released
  1326. on the component, it will trigger a repaint.
  1327. This is handy for things like buttons that need to draw themselves differently when
  1328. the mouse moves over them, and it avoids having to override all the different mouse
  1329. callbacks and call repaint().
  1330. @see mouseEnter, mouseExit, mouseDown, mouseUp
  1331. */
  1332. void setRepaintsOnMouseActivity (bool shouldRepaint) noexcept;
  1333. /** Registers a listener to be told when mouse events occur in this component.
  1334. If you need to get informed about mouse events in a component but can't or
  1335. don't want to override its methods, you can attach any number of listeners
  1336. to the component, and these will get told about the events in addition to
  1337. the component's own callbacks being called.
  1338. Note that a MouseListener can also be attached to more than one component.
  1339. @param newListener the listener to register
  1340. @param wantsEventsForAllNestedChildComponents if true, the listener will receive callbacks
  1341. for events that happen to any child component
  1342. within this component, including deeply-nested
  1343. child components. If false, it will only be
  1344. told about events that this component handles.
  1345. @see MouseListener, removeMouseListener
  1346. */
  1347. void addMouseListener (MouseListener* newListener,
  1348. bool wantsEventsForAllNestedChildComponents);
  1349. /** Deregisters a mouse listener.
  1350. @see addMouseListener, MouseListener
  1351. */
  1352. void removeMouseListener (MouseListener* listenerToRemove);
  1353. //==============================================================================
  1354. /** Adds a listener that wants to hear about keypresses that this component receives.
  1355. The listeners that are registered with a component are called by its keyPressed() or
  1356. keyStateChanged() methods (assuming these haven't been overridden to do something else).
  1357. If you add an object as a key listener, be careful to remove it when the object
  1358. is deleted, or the component will be left with a dangling pointer.
  1359. @see keyPressed, keyStateChanged, removeKeyListener
  1360. */
  1361. void addKeyListener (KeyListener* newListener);
  1362. /** Removes a previously-registered key listener.
  1363. @see addKeyListener
  1364. */
  1365. void removeKeyListener (KeyListener* listenerToRemove);
  1366. /** Called when a key is pressed.
  1367. When a key is pressed, the component that has the keyboard focus will have this
  1368. method called. Remember that a component will only be given the focus if its
  1369. setWantsKeyboardFocus() method has been used to enable this.
  1370. If your implementation returns true, the event will be consumed and not passed
  1371. on to any other listeners. If it returns false, the key will be passed to any
  1372. KeyListeners that have been registered with this component. As soon as one of these
  1373. returns true, the process will stop, but if they all return false, the event will
  1374. be passed upwards to this component's parent, and so on.
  1375. The default implementation of this method does nothing and returns false.
  1376. @see keyStateChanged, getCurrentlyFocusedComponent, addKeyListener
  1377. */
  1378. virtual bool keyPressed (const KeyPress& key);
  1379. /** Called when a key is pressed or released.
  1380. Whenever a key on the keyboard is pressed or released (including modifier keys
  1381. like shift and ctrl), this method will be called on the component that currently
  1382. has the keyboard focus. Remember that a component will only be given the focus if
  1383. its setWantsKeyboardFocus() method has been used to enable this.
  1384. If your implementation returns true, the event will be consumed and not passed
  1385. on to any other listeners. If it returns false, then any KeyListeners that have
  1386. been registered with this component will have their keyStateChanged methods called.
  1387. As soon as one of these returns true, the process will stop, but if they all return
  1388. false, the event will be passed upwards to this component's parent, and so on.
  1389. The default implementation of this method does nothing and returns false.
  1390. To find out which keys are up or down at any time, see the KeyPress::isKeyCurrentlyDown()
  1391. method.
  1392. @param isKeyDown true if a key has been pressed; false if it has been released
  1393. @see keyPressed, KeyPress, getCurrentlyFocusedComponent, addKeyListener
  1394. */
  1395. virtual bool keyStateChanged (bool isKeyDown);
  1396. /** Called when a modifier key is pressed or released.
  1397. Whenever the shift, control, alt or command keys are pressed or released,
  1398. this method will be called.
  1399. The component that is currently under the main mouse pointer will be tried first and,
  1400. if there is no component currently under the pointer, the component that currently
  1401. has the keyboard focus will have this method called. Remember that a component will
  1402. only be given the focus if its setWantsKeyboardFocus() method has been used to enable this.
  1403. The default implementation of this method actually calls its parent's modifierKeysChanged
  1404. method, so that focused components which aren't interested in this will give their
  1405. parents a chance to act on the event instead.
  1406. @see keyStateChanged, ModifierKeys
  1407. */
  1408. virtual void modifierKeysChanged (const ModifierKeys& modifiers);
  1409. //==============================================================================
  1410. /** Enumeration used by the focusGained() and focusLost() methods. */
  1411. enum FocusChangeType
  1412. {
  1413. focusChangedByMouseClick, /**< Means that the user clicked the mouse to change focus. */
  1414. focusChangedByTabKey, /**< Means that the user pressed the tab key to move the focus. */
  1415. focusChangedDirectly /**< Means that the focus was changed by a call to grabKeyboardFocus(). */
  1416. };
  1417. /** Called to indicate that this component has just acquired the keyboard focus.
  1418. @see focusLost, setWantsKeyboardFocus, getCurrentlyFocusedComponent, hasKeyboardFocus
  1419. */
  1420. virtual void focusGained (FocusChangeType cause);
  1421. /** Called to indicate that this component has just lost the keyboard focus.
  1422. @see focusGained, setWantsKeyboardFocus, getCurrentlyFocusedComponent, hasKeyboardFocus
  1423. */
  1424. virtual void focusLost (FocusChangeType cause);
  1425. /** Called to indicate a change in whether or not this component is the parent of the
  1426. currently-focused component.
  1427. Essentially this is called when the return value of a call to hasKeyboardFocus (true) has
  1428. changed. It happens when focus moves from one of this component's children (at any depth)
  1429. to a component that isn't contained in this one, (or vice-versa).
  1430. Note that this method does NOT get called to when focus simply moves from one of its
  1431. child components to another.
  1432. @see focusGained, setWantsKeyboardFocus, getCurrentlyFocusedComponent, hasKeyboardFocus
  1433. */
  1434. virtual void focusOfChildComponentChanged (FocusChangeType cause);
  1435. //==============================================================================
  1436. /** Returns true if the mouse is currently over this component.
  1437. If the mouse isn't over the component, this will return false, even if the
  1438. mouse is currently being dragged - so you can use this in your mouseDrag
  1439. method to find out whether it's really over the component or not.
  1440. Note that when the mouse button is being held down, then the only component
  1441. for which this method will return true is the one that was originally
  1442. clicked on.
  1443. Also note that on a touch-screen device, this will only return true when a finger
  1444. is actually down - as soon as all touch is released, isMouseOver will always
  1445. return false.
  1446. If includeChildren is true, then this will also return true if the mouse is over
  1447. any of the component's children (recursively) as well as the component itself.
  1448. @see isMouseButtonDown. isMouseOverOrDragging, mouseDrag
  1449. */
  1450. bool isMouseOver (bool includeChildren = false) const;
  1451. /** Returns true if the mouse button is currently held down in this component.
  1452. Note that this is a test to see whether the mouse is being pressed in this
  1453. component, so it'll return false if called on component A when the mouse
  1454. is actually being dragged in component B.
  1455. @see isMouseButtonDownAnywhere, isMouseOver, isMouseOverOrDragging
  1456. */
  1457. bool isMouseButtonDown (bool includeChildren = false) const;
  1458. /** True if the mouse is over this component, or if it's being dragged in this component.
  1459. This is a handy equivalent to (isMouseOver() || isMouseButtonDown()).
  1460. @see isMouseOver, isMouseButtonDown, isMouseButtonDownAnywhere
  1461. */
  1462. bool isMouseOverOrDragging (bool includeChildren = false) const;
  1463. /** Returns true if a mouse button is currently down.
  1464. Unlike isMouseButtonDown, this will test the current state of the
  1465. buttons without regard to which component (if any) it has been
  1466. pressed in.
  1467. @see isMouseButtonDown, ModifierKeys
  1468. */
  1469. static bool JUCE_CALLTYPE isMouseButtonDownAnywhere() noexcept;
  1470. /** Returns the mouse's current position, relative to this component.
  1471. The return value is relative to the component's top-left corner.
  1472. */
  1473. Point<int> getMouseXYRelative() const;
  1474. //==============================================================================
  1475. /** Called when this component's size has been changed.
  1476. A component can implement this method to do things such as laying out its
  1477. child components when its width or height changes.
  1478. The method is called synchronously as a result of the setBounds or setSize
  1479. methods, so repeatedly changing a components size will repeatedly call its
  1480. resized method (unlike things like repainting, where multiple calls to repaint
  1481. are coalesced together).
  1482. If the component is a top-level window on the desktop, its size could also
  1483. be changed by operating-system factors beyond the application's control.
  1484. @see moved, setSize
  1485. */
  1486. virtual void resized();
  1487. /** Called when this component's position has been changed.
  1488. This is called when the position relative to its parent changes, not when
  1489. its absolute position on the screen changes (so it won't be called for
  1490. all child components when a parent component is moved).
  1491. The method is called synchronously as a result of the setBounds, setTopLeftPosition
  1492. or any of the other repositioning methods, and like resized(), it will be
  1493. called each time those methods are called.
  1494. If the component is a top-level window on the desktop, its position could also
  1495. be changed by operating-system factors beyond the application's control.
  1496. @see resized, setBounds
  1497. */
  1498. virtual void moved();
  1499. /** Called when one of this component's children is moved or resized.
  1500. If the parent wants to know about changes to its immediate children (not
  1501. to children of its children), this is the method to override.
  1502. @see moved, resized, parentSizeChanged
  1503. */
  1504. virtual void childBoundsChanged (Component* child);
  1505. /** Called when this component's immediate parent has been resized.
  1506. If the component is a top-level window, this indicates that the screen size
  1507. has changed.
  1508. @see childBoundsChanged, moved, resized
  1509. */
  1510. virtual void parentSizeChanged();
  1511. /** Called when this component has been moved to the front of its siblings.
  1512. The component may have been brought to the front by the toFront() method, or
  1513. by the operating system if it's a top-level window.
  1514. @see toFront
  1515. */
  1516. virtual void broughtToFront();
  1517. /** Adds a listener to be told about changes to the component hierarchy or position.
  1518. Component listeners get called when this component's size, position or children
  1519. change - see the ComponentListener class for more details.
  1520. @param newListener the listener to register - if this is already registered, it
  1521. will be ignored.
  1522. @see ComponentListener, removeComponentListener
  1523. */
  1524. void addComponentListener (ComponentListener* newListener);
  1525. /** Removes a component listener.
  1526. @see addComponentListener
  1527. */
  1528. void removeComponentListener (ComponentListener* listenerToRemove);
  1529. //==============================================================================
  1530. /** Dispatches a numbered message to this component.
  1531. This is a quick and cheap way of allowing simple asynchronous messages to
  1532. be sent to components. It's also safe, because if the component that you
  1533. send the message to is a null or dangling pointer, this won't cause an error.
  1534. The command ID is later delivered to the component's handleCommandMessage() method by
  1535. the application's message queue.
  1536. @see handleCommandMessage
  1537. */
  1538. void postCommandMessage (int commandId);
  1539. /** Called to handle a command that was sent by postCommandMessage().
  1540. This is called by the message thread when a command message arrives, and
  1541. the component can override this method to process it in any way it needs to.
  1542. @see postCommandMessage
  1543. */
  1544. virtual void handleCommandMessage (int commandId);
  1545. //==============================================================================
  1546. #if JUCE_MODAL_LOOPS_PERMITTED
  1547. /** Runs a component modally, waiting until the loop terminates.
  1548. This method first makes the component visible, brings it to the front and
  1549. gives it the keyboard focus.
  1550. It then runs a loop, dispatching messages from the system message queue, but
  1551. blocking all mouse or keyboard messages from reaching any components other
  1552. than this one and its children.
  1553. This loop continues until the component's exitModalState() method is called (or
  1554. the component is deleted), and then this method returns, returning the value
  1555. passed into exitModalState().
  1556. Note that you SHOULD NEVER USE THIS METHOD! Modal loops are a dangerous construct
  1557. because things that happen during the events that they dispatch could affect the
  1558. state of objects which are currently in use somewhere on the stack, so when the
  1559. loop finishes and the stack unwinds, horrible problems can occur. This is especially
  1560. bad in plugins, where the host may choose to delete the plugin during runModalLoop(),
  1561. so that when it returns, the entire DLL could have been unloaded from memory!
  1562. Also, some OSes deliberately make it impossible to run modal loops (e.g. Android),
  1563. so this method won't even exist on some platforms.
  1564. @see enterModalState, exitModalState, isCurrentlyModal, getCurrentlyModalComponent,
  1565. isCurrentlyBlockedByAnotherModalComponent, ModalComponentManager
  1566. */
  1567. int runModalLoop();
  1568. #endif
  1569. /** Puts the component into a modal state.
  1570. This makes the component modal, so that messages are blocked from reaching
  1571. any components other than this one and its children, but unlike runModalLoop(),
  1572. this method returns immediately.
  1573. If takeKeyboardFocus is true, the component will use grabKeyboardFocus() to
  1574. get the focus, which is usually what you'll want it to do. If not, it will leave
  1575. the focus unchanged.
  1576. The callback is an optional object which will receive a callback when the modal
  1577. component loses its modal status, either by being hidden or when exitModalState()
  1578. is called. If you pass an object in here, the system will take care of deleting it
  1579. later, after making the callback
  1580. If deleteWhenDismissed is true, then when it is dismissed, the component will be
  1581. deleted and then the callback will be called. (This will safely handle the situation
  1582. where the component is deleted before its exitModalState() method is called).
  1583. @see exitModalState, runModalLoop, ModalComponentManager::attachCallback
  1584. */
  1585. void enterModalState (bool takeKeyboardFocus = true,
  1586. ModalComponentManager::Callback* callback = nullptr,
  1587. bool deleteWhenDismissed = false);
  1588. /** Ends a component's modal state.
  1589. If this component is currently modal, this will turn off its modalness, and return
  1590. a value to the runModalLoop() method that might have be running its modal loop.
  1591. @see runModalLoop, enterModalState, isCurrentlyModal
  1592. */
  1593. void exitModalState (int returnValue);
  1594. /** Returns true if this component is the modal one.
  1595. It's possible to have nested modal components, e.g. a pop-up dialog box
  1596. that launches another pop-up. If onlyConsiderForemostModalComponent is
  1597. true then isCurrentlyModal will only return true for the one at the top
  1598. of the stack. If onlyConsiderForemostModalComponent is false then
  1599. isCurrentlyModal will return true for any modal component in the stack.
  1600. @see getCurrentlyModalComponent
  1601. */
  1602. bool isCurrentlyModal (bool onlyConsiderForemostModalComponent = true) const noexcept;
  1603. /** Returns the number of components that are currently in a modal state.
  1604. @see getCurrentlyModalComponent
  1605. */
  1606. static int JUCE_CALLTYPE getNumCurrentlyModalComponents() noexcept;
  1607. /** Returns one of the components that are currently modal.
  1608. The index specifies which of the possible modal components to return. The order
  1609. of the components in this list is the reverse of the order in which they became
  1610. modal - so the component at index 0 is always the active component, and the others
  1611. are progressively earlier ones that are themselves now blocked by later ones.
  1612. @returns the modal component, or null if no components are modal (or if the
  1613. index is out of range)
  1614. @see getNumCurrentlyModalComponents, runModalLoop, isCurrentlyModal
  1615. */
  1616. static Component* JUCE_CALLTYPE getCurrentlyModalComponent (int index = 0) noexcept;
  1617. /** Checks whether there's a modal component somewhere that's stopping this one
  1618. from receiving messages.
  1619. If there is a modal component, its canModalEventBeSentToComponent() method
  1620. will be called to see if it will still allow this component to receive events.
  1621. @see runModalLoop, getCurrentlyModalComponent
  1622. */
  1623. bool isCurrentlyBlockedByAnotherModalComponent() const;
  1624. /** When a component is modal, this callback allows it to choose which other
  1625. components can still receive events.
  1626. When a modal component is active and the user clicks on a non-modal component,
  1627. this method is called on the modal component, and if it returns true, the
  1628. event is allowed to reach its target. If it returns false, the event is blocked
  1629. and the inputAttemptWhenModal() callback is made.
  1630. It called by the isCurrentlyBlockedByAnotherModalComponent() method. The default
  1631. implementation just returns false in all cases.
  1632. */
  1633. virtual bool canModalEventBeSentToComponent (const Component* targetComponent);
  1634. /** Called when the user tries to click on a component that is blocked by another
  1635. modal component.
  1636. When a component is modal and the user clicks on one of the other components,
  1637. the modal component will receive this callback.
  1638. The default implementation of this method will play a beep, and bring the currently
  1639. modal component to the front, but it can be overridden to do other tasks.
  1640. @see isCurrentlyBlockedByAnotherModalComponent, canModalEventBeSentToComponent
  1641. */
  1642. virtual void inputAttemptWhenModal();
  1643. //==============================================================================
  1644. /** Returns the set of properties that belong to this component.
  1645. Each component has a NamedValueSet object which you can use to attach arbitrary
  1646. items of data to it.
  1647. */
  1648. NamedValueSet& getProperties() noexcept { return properties; }
  1649. /** Returns the set of properties that belong to this component.
  1650. Each component has a NamedValueSet object which you can use to attach arbitrary
  1651. items of data to it.
  1652. */
  1653. const NamedValueSet& getProperties() const noexcept { return properties; }
  1654. //==============================================================================
  1655. /** Looks for a colour that has been registered with the given colour ID number.
  1656. If a colour has been set for this ID number using setColour(), then it is
  1657. returned. If none has been set, the method will try calling the component's
  1658. LookAndFeel class's findColour() method. If none has been registered with the
  1659. look-and-feel either, it will just return black.
  1660. The colour IDs for various purposes are stored as enums in the components that
  1661. they are relevant to - for an example, see Slider::ColourIds,
  1662. Label::ColourIds, TextEditor::ColourIds, TreeView::ColourIds, etc.
  1663. @see setColour, isColourSpecified, colourChanged, LookAndFeel::findColour, LookAndFeel::setColour
  1664. */
  1665. Colour findColour (int colourID, bool inheritFromParent = false) const;
  1666. /** Registers a colour to be used for a particular purpose.
  1667. Changing a colour will cause a synchronous callback to the colourChanged()
  1668. method, which your component can override if it needs to do something when
  1669. colours are altered.
  1670. For more details about colour IDs, see the comments for findColour().
  1671. @see findColour, isColourSpecified, colourChanged, LookAndFeel::findColour, LookAndFeel::setColour
  1672. */
  1673. void setColour (int colourID, Colour newColour);
  1674. /** If a colour has been set with setColour(), this will remove it.
  1675. This allows you to make a colour revert to its default state.
  1676. */
  1677. void removeColour (int colourID);
  1678. /** Returns true if the specified colour ID has been explicitly set for this
  1679. component using the setColour() method.
  1680. */
  1681. bool isColourSpecified (int colourID) const;
  1682. /** This looks for any colours that have been specified for this component,
  1683. and copies them to the specified target component.
  1684. */
  1685. void copyAllExplicitColoursTo (Component& target) const;
  1686. /** This method is called when a colour is changed by the setColour() method.
  1687. @see setColour, findColour
  1688. */
  1689. virtual void colourChanged();
  1690. //==============================================================================
  1691. /** Returns the underlying native window handle for this component.
  1692. This is platform-dependent and strictly for power-users only!
  1693. */
  1694. void* getWindowHandle() const;
  1695. //==============================================================================
  1696. /** Holds a pointer to some type of Component, which automatically becomes null if
  1697. the component is deleted.
  1698. If you're using a component which may be deleted by another event that's outside
  1699. of your control, use a SafePointer instead of a normal pointer to refer to it,
  1700. and you can test whether it's null before using it to see if something has deleted
  1701. it.
  1702. The ComponentType template parameter must be Component, or some subclass of Component.
  1703. You may also want to use a WeakReference<Component> object for the same purpose.
  1704. */
  1705. template <class ComponentType>
  1706. class SafePointer
  1707. {
  1708. public:
  1709. /** Creates a null SafePointer. */
  1710. SafePointer() = default;
  1711. /** Creates a SafePointer that points at the given component. */
  1712. SafePointer (ComponentType* component) : weakRef (component) {}
  1713. /** Creates a copy of another SafePointer. */
  1714. SafePointer (const SafePointer& other) noexcept : weakRef (other.weakRef) {}
  1715. /** Copies another pointer to this one. */
  1716. SafePointer& operator= (const SafePointer& other) { weakRef = other.weakRef; return *this; }
  1717. /** Copies another pointer to this one. */
  1718. SafePointer& operator= (ComponentType* newComponent) { weakRef = newComponent; return *this; }
  1719. /** Returns the component that this pointer refers to, or null if the component no longer exists. */
  1720. ComponentType* getComponent() const noexcept { return dynamic_cast<ComponentType*> (weakRef.get()); }
  1721. /** Returns the component that this pointer refers to, or null if the component no longer exists. */
  1722. operator ComponentType*() const noexcept { return getComponent(); }
  1723. /** Returns the component that this pointer refers to, or null if the component no longer exists. */
  1724. ComponentType* operator->() const noexcept { return getComponent(); }
  1725. /** If the component is valid, this deletes it and sets this pointer to null. */
  1726. void deleteAndZero() { delete getComponent(); }
  1727. bool operator== (ComponentType* component) const noexcept { return weakRef == component; }
  1728. bool operator!= (ComponentType* component) const noexcept { return weakRef != component; }
  1729. private:
  1730. WeakReference<Component> weakRef;
  1731. };
  1732. //==============================================================================
  1733. /** A class to keep an eye on a component and check for it being deleted.
  1734. This is designed for use with the ListenerList::callChecked() methods, to allow
  1735. the list iterator to stop cleanly if the component is deleted by a listener callback
  1736. while the list is still being iterated.
  1737. */
  1738. class JUCE_API BailOutChecker
  1739. {
  1740. public:
  1741. /** Creates a checker that watches one component. */
  1742. BailOutChecker (Component* component);
  1743. /** Returns true if either of the two components have been deleted since this object was created. */
  1744. bool shouldBailOut() const noexcept;
  1745. private:
  1746. const WeakReference<Component> safePointer;
  1747. JUCE_DECLARE_NON_COPYABLE (BailOutChecker)
  1748. };
  1749. //==============================================================================
  1750. /**
  1751. Base class for objects that can be used to automatically position a component according to
  1752. some kind of algorithm.
  1753. The component class simply holds onto a reference to a Positioner, but doesn't actually do
  1754. anything with it - all the functionality must be implemented by the positioner itself (e.g.
  1755. it might choose to watch some kind of value and move the component when the value changes).
  1756. */
  1757. class JUCE_API Positioner
  1758. {
  1759. public:
  1760. /** Creates a Positioner which can control the specified component. */
  1761. explicit Positioner (Component& component) noexcept;
  1762. /** Destructor. */
  1763. virtual ~Positioner() = default;
  1764. /** Returns the component that this positioner controls. */
  1765. Component& getComponent() const noexcept { return component; }
  1766. /** Attempts to set the component's position to the given rectangle.
  1767. Unlike simply calling Component::setBounds(), this may involve the positioner
  1768. being smart enough to adjust itself to fit the new bounds.
  1769. */
  1770. virtual void applyNewBounds (const Rectangle<int>& newBounds) = 0;
  1771. private:
  1772. Component& component;
  1773. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Positioner)
  1774. };
  1775. /** Returns the Positioner object that has been set for this component.
  1776. @see setPositioner()
  1777. */
  1778. Positioner* getPositioner() const noexcept;
  1779. /** Sets a new Positioner object for this component.
  1780. If there's currently another positioner set, it will be deleted. The object that is passed in
  1781. will be deleted automatically by this component when it's no longer required. Pass a null pointer
  1782. to clear the current positioner.
  1783. @see getPositioner()
  1784. */
  1785. void setPositioner (Positioner* newPositioner);
  1786. /** Gives the component a CachedComponentImage that should be used to buffer its painting.
  1787. The object that is passed-in will be owned by this component, and will be deleted automatically
  1788. later on.
  1789. @see setBufferedToImage
  1790. */
  1791. void setCachedComponentImage (CachedComponentImage* newCachedImage);
  1792. /** Returns the object that was set by setCachedComponentImage().
  1793. @see setCachedComponentImage
  1794. */
  1795. CachedComponentImage* getCachedComponentImage() const noexcept { return cachedImage.get(); }
  1796. /** Sets a flag to indicate whether mouse drag events on this Component should be ignored when it is inside a
  1797. Viewport with drag-to-scroll functionality enabled. This is useful for Components such as sliders that
  1798. should not move when their parent Viewport when dragged.
  1799. */
  1800. void setViewportIgnoreDragFlag (bool ignoreDrag) noexcept { flags.viewportIgnoreDragFlag = ignoreDrag; }
  1801. /** Retrieves the current state of the Viewport drag-to-scroll functionality flag.
  1802. @see setViewportIgnoreDragFlag
  1803. */
  1804. bool getViewportIgnoreDragFlag() const noexcept { return flags.viewportIgnoreDragFlag; }
  1805. //==============================================================================
  1806. /** Returns the title text for this component.
  1807. @see setTitle
  1808. */
  1809. String getTitle() const noexcept { return componentTitle; }
  1810. /** Sets the title for this component.
  1811. If this component supports accessibility using the default AccessibilityHandler
  1812. implementation, this string will be passed to accessibility clients requesting a
  1813. title and may be read out by a screen reader.
  1814. @see getTitle, getAccessibilityHandler
  1815. */
  1816. void setTitle (const String& newTitle);
  1817. /** Returns the description for this component.
  1818. @see setDescription
  1819. */
  1820. String getDescription() const noexcept { return componentDescription; }
  1821. /** Sets the description for this component.
  1822. If this component supports accessibility using the default AccessibilityHandler
  1823. implementation, this string will be passed to accessibility clients requesting a
  1824. description and may be read out by a screen reader.
  1825. @see getDescription, getAccessibilityHandler
  1826. */
  1827. void setDescription (const String& newDescription);
  1828. /** Returns the help text for this component.
  1829. @see setHelpText
  1830. */
  1831. String getHelpText() const noexcept { return componentHelpText; }
  1832. /** Sets the help text for this component.
  1833. If this component supports accessibility using the default AccessibilityHandler
  1834. implementation, this string will be passed to accessibility clients requesting help text
  1835. and may be read out by a screen reader.
  1836. @see getHelpText, getAccessibilityHandler
  1837. */
  1838. void setHelpText (const String& newHelpText);
  1839. /** Sets whether this component and its children are visible to accessibility clients.
  1840. If this flag is set to false then the getAccessibilityHandler() method will return nullptr
  1841. and this component and its children will not be visible to any accessibility clients.
  1842. By default this is set to true.
  1843. @see isAccessible, getAccessibilityHandler
  1844. */
  1845. void setAccessible (bool shouldBeAccessible);
  1846. /** Returns true if this component and its children are visible to accessibility clients.
  1847. @see setAccessible
  1848. */
  1849. bool isAccessible() const noexcept;
  1850. /** Returns the accessibility handler for this component, or nullptr if this component is not
  1851. accessible.
  1852. @see setAccessible
  1853. */
  1854. AccessibilityHandler* getAccessibilityHandler();
  1855. /** Invalidates the AccessibilityHandler that is currently being used for this component.
  1856. Use this to indicate that something in the accessible component has changed
  1857. and its handler needs to be updated. This will trigger a call to
  1858. createAccessibilityHandler().
  1859. */
  1860. void invalidateAccessibilityHandler();
  1861. //==============================================================================
  1862. #ifndef DOXYGEN
  1863. [[deprecated ("Use the setFocusContainerType that takes a more descriptive enum.")]]
  1864. void setFocusContainer (bool shouldBeFocusContainer) noexcept
  1865. {
  1866. setFocusContainerType (shouldBeFocusContainer ? FocusContainerType::keyboardFocusContainer
  1867. : FocusContainerType::none);
  1868. }
  1869. [[deprecated ("Use the contains that takes a Point<int>.")]]
  1870. void contains (int, int) = delete;
  1871. #endif
  1872. private:
  1873. //==============================================================================
  1874. /** Override this method to return a custom AccessibilityHandler for this component.
  1875. The default implementation creates and returns a AccessibilityHandler object with an
  1876. unspecified role, meaning that it will be visible to accessibility clients but
  1877. without a specific role, action callbacks or interfaces. To control how accessibility
  1878. clients see and interact with your component subclass AccessibilityHandler, implement
  1879. the desired behaviours, and return an instance of it from this method in your
  1880. component subclass.
  1881. The accessibility handler you return here is guaranteed to be destroyed before
  1882. its Component, so it's safe to store and use a reference back to the Component
  1883. inside the AccessibilityHandler if necessary.
  1884. @see getAccessibilityHandler
  1885. */
  1886. virtual std::unique_ptr<AccessibilityHandler> createAccessibilityHandler();
  1887. //==============================================================================
  1888. friend class ComponentPeer;
  1889. friend class MouseInputSourceInternal;
  1890. #ifndef DOXYGEN
  1891. static Component* currentlyFocusedComponent;
  1892. //==============================================================================
  1893. String componentName, componentID, componentTitle, componentDescription, componentHelpText;
  1894. Component* parentComponent = nullptr;
  1895. Rectangle<int> boundsRelativeToParent;
  1896. std::unique_ptr<Positioner> positioner;
  1897. std::unique_ptr<AffineTransform> affineTransform;
  1898. Array<Component*> childComponentList;
  1899. WeakReference<LookAndFeel> lookAndFeel;
  1900. MouseCursor cursor;
  1901. ImageEffectFilter* effect = nullptr;
  1902. std::unique_ptr<CachedComponentImage> cachedImage;
  1903. class MouseListenerList;
  1904. std::unique_ptr<MouseListenerList> mouseListeners;
  1905. std::unique_ptr<Array<KeyListener*>> keyListeners;
  1906. ListenerList<ComponentListener> componentListeners;
  1907. NamedValueSet properties;
  1908. friend class WeakReference<Component>;
  1909. WeakReference<Component>::Master masterReference;
  1910. std::unique_ptr<AccessibilityHandler> accessibilityHandler;
  1911. struct ComponentFlags
  1912. {
  1913. bool hasHeavyweightPeerFlag : 1;
  1914. bool visibleFlag : 1;
  1915. bool opaqueFlag : 1;
  1916. bool ignoresMouseClicksFlag : 1;
  1917. bool allowChildMouseClicksFlag : 1;
  1918. bool wantsKeyboardFocusFlag : 1;
  1919. bool isFocusContainerFlag : 1;
  1920. bool isKeyboardFocusContainerFlag : 1;
  1921. bool childKeyboardFocusedFlag : 1;
  1922. bool dontFocusOnMouseClickFlag : 1;
  1923. bool hasFocusOutlineFlag : 1;
  1924. bool alwaysOnTopFlag : 1;
  1925. bool bufferToImageFlag : 1;
  1926. bool bringToFrontOnClickFlag : 1;
  1927. bool repaintOnMouseActivityFlag : 1;
  1928. bool isDisabledFlag : 1;
  1929. bool dontClipGraphicsFlag : 1;
  1930. bool mouseDownWasBlocked : 1;
  1931. bool isMoveCallbackPending : 1;
  1932. bool isResizeCallbackPending : 1;
  1933. bool viewportIgnoreDragFlag : 1;
  1934. bool accessibilityIgnoredFlag : 1;
  1935. bool cachedMouseInsideComponent : 1;
  1936. #if JUCE_DEBUG
  1937. bool isInsidePaintCall : 1;
  1938. #endif
  1939. };
  1940. union
  1941. {
  1942. uint32 componentFlags;
  1943. ComponentFlags flags;
  1944. };
  1945. uint8 componentTransparency = 0;
  1946. //==============================================================================
  1947. void internalMouseEnter (MouseInputSource, Point<float>, Time);
  1948. void internalMouseExit (MouseInputSource, Point<float>, Time);
  1949. void internalMouseDown (MouseInputSource, const PointerState&, Time);
  1950. void internalMouseUp (MouseInputSource, const PointerState&, Time, const ModifierKeys oldModifiers);
  1951. void internalMouseDrag (MouseInputSource, const PointerState&, Time);
  1952. void internalMouseMove (MouseInputSource, Point<float>, Time);
  1953. void internalMouseWheel (MouseInputSource, Point<float>, Time, const MouseWheelDetails&);
  1954. void internalMagnifyGesture (MouseInputSource, Point<float>, Time, float);
  1955. void internalBroughtToFront();
  1956. void internalKeyboardFocusGain (FocusChangeType, const WeakReference<Component>&);
  1957. void internalKeyboardFocusGain (FocusChangeType);
  1958. void internalKeyboardFocusLoss (FocusChangeType);
  1959. void internalChildKeyboardFocusChange (FocusChangeType, const WeakReference<Component>&);
  1960. void internalModalInputAttempt();
  1961. void internalModifierKeysChanged();
  1962. void internalChildrenChanged();
  1963. void internalHierarchyChanged();
  1964. void internalRepaint (Rectangle<int>);
  1965. void internalRepaintUnchecked (Rectangle<int>, bool);
  1966. Component* removeChildComponent (int index, bool sendParentEvents, bool sendChildEvents);
  1967. void reorderChildInternal (int sourceIndex, int destIndex);
  1968. void paintComponentAndChildren (Graphics&);
  1969. void paintWithinParentContext (Graphics&);
  1970. void sendMovedResizedMessages (bool wasMoved, bool wasResized);
  1971. void sendMovedResizedMessagesIfPending();
  1972. void repaintParent();
  1973. void sendFakeMouseMove() const;
  1974. void takeKeyboardFocus (FocusChangeType);
  1975. void grabKeyboardFocusInternal (FocusChangeType, bool canTryParent);
  1976. void giveAwayKeyboardFocusInternal (bool sendFocusLossEvent);
  1977. void sendEnablementChangeMessage();
  1978. void sendVisibilityChangeMessage();
  1979. struct ComponentHelpers;
  1980. friend struct ComponentHelpers;
  1981. /* Components aren't allowed to have copy constructors, as this would mess up parent hierarchies.
  1982. You might need to give your subclasses a private dummy constructor to avoid compiler warnings.
  1983. */
  1984. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Component)
  1985. protected:
  1986. //==============================================================================
  1987. /** @internal */
  1988. virtual ComponentPeer* createNewPeer (int styleFlags, void* nativeWindowToAttachTo);
  1989. /** @internal */
  1990. static std::unique_ptr<AccessibilityHandler> createIgnoredAccessibilityHandler (Component&);
  1991. #endif
  1992. };
  1993. } // namespace juce