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 111KB

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