pen.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: pen.h
  3. // Purpose: interface of wxPen* classes
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. The possible styles for a wxPen.
  9. Note that hatched pen styles are not supported by X11-based ports,
  10. including wxGTK.
  11. */
  12. enum wxPenStyle
  13. {
  14. wxPENSTYLE_INVALID = -1,
  15. wxPENSTYLE_SOLID,
  16. /**< Solid style. */
  17. wxPENSTYLE_DOT,
  18. /**< Dotted style. */
  19. wxPENSTYLE_LONG_DASH,
  20. /**< Long dashed style. */
  21. wxPENSTYLE_SHORT_DASH,
  22. /**< Short dashed style. */
  23. wxPENSTYLE_DOT_DASH,
  24. /**< Dot and dash style. */
  25. wxPENSTYLE_USER_DASH,
  26. /**< Use the user dashes: see wxPen::SetDashes. */
  27. wxPENSTYLE_TRANSPARENT,
  28. /**< No pen is used. */
  29. wxPENSTYLE_STIPPLE_MASK_OPAQUE,
  30. /**< @todo WHAT's this? */
  31. wxPENSTYLE_STIPPLE_MASK,
  32. /**< @todo WHAT's this? */
  33. wxPENSTYLE_STIPPLE,
  34. /**< Use the stipple bitmap. */
  35. wxPENSTYLE_BDIAGONAL_HATCH,
  36. /**< Backward diagonal hatch. */
  37. wxPENSTYLE_CROSSDIAG_HATCH,
  38. /**< Cross-diagonal hatch. */
  39. wxPENSTYLE_FDIAGONAL_HATCH,
  40. /**< Forward diagonal hatch. */
  41. wxPENSTYLE_CROSS_HATCH,
  42. /**< Cross hatch. */
  43. wxPENSTYLE_HORIZONTAL_HATCH,
  44. /**< Horizontal hatch. */
  45. wxPENSTYLE_VERTICAL_HATCH,
  46. /**< Vertical hatch. */
  47. wxPENSTYLE_FIRST_HATCH,
  48. /**< First of the hatch styles (inclusive). */
  49. wxPENSTYLE_LAST_HATCH
  50. /**< Last of the hatch styles (inclusive). */
  51. };
  52. /**
  53. The possible join values of a wxPen.
  54. @todo use wxPENJOIN_ prefix
  55. */
  56. enum wxPenJoin
  57. {
  58. wxJOIN_INVALID = -1,
  59. wxJOIN_BEVEL = 120,
  60. wxJOIN_MITER,
  61. wxJOIN_ROUND,
  62. };
  63. /**
  64. The possible cap values of a wxPen.
  65. @todo use wxPENCAP_ prefix
  66. */
  67. enum wxPenCap
  68. {
  69. wxCAP_INVALID = -1,
  70. wxCAP_ROUND = 130,
  71. wxCAP_PROJECTING,
  72. wxCAP_BUTT
  73. };
  74. /**
  75. @class wxPen
  76. A pen is a drawing tool for drawing outlines. It is used for drawing
  77. lines and painting the outline of rectangles, ellipses, etc.
  78. It has a colour, a width and a style.
  79. @note On a monochrome display, wxWidgets shows all non-white pens as black.
  80. Do not initialize objects on the stack before the program commences,
  81. since other required structures may not have been set up yet.
  82. Instead, define global pointers to objects and create them in wxApp::OnInit()
  83. or when required.
  84. An application may wish to dynamically create pens with different characteristics,
  85. and there is the consequent danger that a large number of duplicate pens will
  86. be created. Therefore an application may wish to get a pointer to a pen by using
  87. the global list of pens ::wxThePenList, and calling the member function
  88. wxPenList::FindOrCreatePen().
  89. See wxPenList for more info.
  90. This class uses @ref overview_refcount "reference counting and copy-on-write" internally
  91. so that assignments between two instances of this class are very cheap.
  92. You can therefore use actual objects instead of pointers without efficiency problems.
  93. If an instance of this class is changed it will create its own data internally
  94. so that other instances, which previously shared the data using the reference
  95. counting, are not affected.
  96. @library{wxcore}
  97. @category{gdi}
  98. @stdobjects
  99. @li ::wxNullPen
  100. @li ::wxBLACK_DASHED_PEN
  101. @li ::wxBLACK_PEN
  102. @li ::wxBLUE_PEN
  103. @li ::wxCYAN_PEN
  104. @li ::wxGREEN_PEN
  105. @li ::wxYELLOW_PEN
  106. @li ::wxGREY_PEN
  107. @li ::wxLIGHT_GREY_PEN
  108. @li ::wxMEDIUM_GREY_PEN
  109. @li ::wxRED_PEN
  110. @li ::wxTRANSPARENT_PEN
  111. @li ::wxWHITE_PEN
  112. @see wxPenList, wxDC, wxDC::SetPen()
  113. */
  114. class wxPen : public wxGDIObject
  115. {
  116. public:
  117. /**
  118. Default constructor. The pen will be uninitialised, and IsOk() will return @false.
  119. */
  120. wxPen();
  121. /**
  122. Constructs a pen from a colour object, pen width and style.
  123. @param colour
  124. A colour object.
  125. @param width
  126. Pen width. Under Windows, the pen width cannot be greater than 1 if
  127. the style is @c wxPENSTYLE_DOT, @c wxPENSTYLE_LONG_DASH, @c wxPENSTYLE_SHORT_DASH,
  128. @c wxPENSTYLE_DOT_DASH, or @c wxPENSTYLE_USER_DASH.
  129. @param style
  130. The style may be one of the ::wxPenStyle values.
  131. @remarks Different versions of Windows and different versions of other
  132. platforms support very different subsets of the styles above
  133. - there is no similarity even between Windows95 and Windows98 -
  134. so handle with care.
  135. @see SetStyle(), SetColour(), SetWidth()
  136. */
  137. wxPen(const wxColour& colour, int width = 1, wxPenStyle style = wxPENSTYLE_SOLID);
  138. /**
  139. Constructs a stippled pen from a stipple bitmap and a width.
  140. @param width
  141. Pen width. Under Windows, the pen width cannot be greater than 1 if
  142. the style is @c wxPENSTYLE_DOT, @c wxPENSTYLE_LONG_DASH, @c wxPENSTYLE_SHORT_DASH,
  143. @c wxPENSTYLE_DOT_DASH, or @c wxPENSTYLE_USER_DASH.
  144. @param stipple
  145. A stipple bitmap.
  146. @onlyfor{wxmsw,wxosx}
  147. @see SetWidth(), SetStipple()
  148. */
  149. wxPen(const wxBitmap& stipple, int width);
  150. /**
  151. Copy constructor, uses @ref overview_refcount.
  152. @param pen
  153. A pointer or reference to a pen to copy.
  154. */
  155. wxPen(const wxPen& pen);
  156. /**
  157. Destructor.
  158. @see @ref overview_refcount_destruct "reference-counted object destruction"
  159. @remarks Although all remaining pens are deleted when the application
  160. exits, the application should try to clean up all pens
  161. itself. This is because wxWidgets cannot know if a
  162. pointer to the pen object is stored in an application
  163. data structure, and there is a risk of double deletion.
  164. */
  165. virtual ~wxPen();
  166. /**
  167. Returns the pen cap style, which may be one of @c wxCAP_ROUND,
  168. @c wxCAP_PROJECTING and @c wxCAP_BUTT.
  169. The default is @c wxCAP_ROUND.
  170. @see SetCap()
  171. */
  172. virtual wxPenCap GetCap() const;
  173. /**
  174. Returns a reference to the pen colour.
  175. @see SetColour()
  176. */
  177. virtual wxColour GetColour() const;
  178. /**
  179. Gets an array of dashes (defined as @c char in X, @c DWORD under Windows).
  180. @a dashes is a pointer to the internal array. Do not deallocate or store this
  181. pointer.
  182. @return The number of dashes associated with this pen.
  183. @see SetDashes()
  184. */
  185. virtual int GetDashes(wxDash** dashes) const;
  186. /**
  187. Returns the pen join style, which may be one of @c wxJOIN_BEVEL,
  188. @c wxJOIN_ROUND and @c wxJOIN_MITER.
  189. The default is @c wxJOIN_ROUND.
  190. @see SetJoin()
  191. */
  192. virtual wxPenJoin GetJoin() const;
  193. /**
  194. Gets a pointer to the stipple bitmap.
  195. @see SetStipple()
  196. */
  197. virtual wxBitmap* GetStipple() const;
  198. /**
  199. Returns the pen style.
  200. @see wxPen(), SetStyle()
  201. */
  202. virtual wxPenStyle GetStyle() const;
  203. /**
  204. Returns the pen width.
  205. @see SetWidth()
  206. */
  207. virtual int GetWidth() const;
  208. /**
  209. Returns @true if the pen is initialised.
  210. Notice that an uninitialized pen object can't be queried for any pen
  211. properties and all calls to the accessor methods on it will result in
  212. an assert failure.
  213. */
  214. virtual bool IsOk() const;
  215. /**
  216. Returns @true if the pen is a valid non-transparent pen.
  217. This method returns @true if the pen object is initialized and has a
  218. non-transparent style. Notice that this should be used instead of
  219. simply testing whether GetStyle() returns a style different from
  220. wxPENSTYLE_TRANSPARENT if the pen may be invalid as GetStyle() would
  221. assert in this case.
  222. @see IsTransparent()
  223. @since 2.9.2.
  224. */
  225. bool IsNonTransparent() const;
  226. /**
  227. Returns @true if the pen is transparent.
  228. A transparent pen is simply a pen with wxPENSTYLE_TRANSPARENT style.
  229. Notice that this function works even for non-initialized pens (for
  230. which it returns @false) unlike tests of the form <code>GetStyle() ==
  231. wxPENSTYLE_TRANSPARENT</code> which would assert if the pen is invalid.
  232. @see IsNonTransparent()
  233. @since 2.9.2.
  234. */
  235. bool IsTransparent() const;
  236. /**
  237. Sets the pen cap style, which may be one of @c wxCAP_ROUND, @c wxCAP_PROJECTING
  238. and @c wxCAP_BUTT. The default is @c wxCAP_ROUND.
  239. @see GetCap()
  240. */
  241. virtual void SetCap(wxPenCap capStyle);
  242. //@{
  243. /**
  244. The pen's colour is changed to the given colour.
  245. @see GetColour()
  246. */
  247. virtual void SetColour(wxColour& colour);
  248. virtual void SetColour(unsigned char red, unsigned char green, unsigned char blue);
  249. //@}
  250. /**
  251. Associates an array of dash values (defined as @c char in X, @c DWORD under
  252. Windows) with the pen.
  253. The array is not deallocated by wxPen, but neither must it be deallocated by
  254. the calling application until the pen is deleted or this function is called
  255. with a @NULL array.
  256. @see GetDashes()
  257. */
  258. virtual void SetDashes(int n, const wxDash* dash);
  259. /**
  260. Sets the pen join style, which may be one of @c wxJOIN_BEVEL, @c wxJOIN_ROUND
  261. and @c wxJOIN_MITER.
  262. The default is @c wxJOIN_ROUND.
  263. @see GetJoin()
  264. */
  265. virtual void SetJoin(wxPenJoin join_style);
  266. /**
  267. Sets the bitmap for stippling.
  268. @see GetStipple()
  269. */
  270. virtual void SetStipple(const wxBitmap& stipple);
  271. /**
  272. Set the pen style.
  273. @see wxPen()
  274. */
  275. virtual void SetStyle(wxPenStyle style);
  276. /**
  277. Sets the pen width.
  278. @see GetWidth()
  279. */
  280. virtual void SetWidth(int width);
  281. /**
  282. Inequality operator.
  283. See @ref overview_refcount_equality "reference-counted object comparison" for
  284. more info.
  285. */
  286. bool operator!=(const wxPen& pen) const;
  287. /**
  288. Assignment operator, using @ref overview_refcount.
  289. */
  290. wxPen& operator=(const wxPen& pen);
  291. /**
  292. Equality operator.
  293. See @ref overview_refcount_equality "reference-counted object comparison" for
  294. more info.
  295. */
  296. bool operator==(const wxPen& pen) const;
  297. };
  298. /**
  299. An empty pen.
  300. wxPen::IsOk() always returns @false for this object.
  301. */
  302. wxPen wxNullPen;
  303. /**
  304. Red pen.
  305. Except for the color it has all standard attributes
  306. (1-pixel width, @c wxPENSTYLE_SOLID and @c wxCAP_ROUND styles, etc...).
  307. */
  308. wxPen* wxRED_PEN;
  309. /**
  310. Blue pen.
  311. Except for the color it has all standard attributes
  312. (1-pixel width, @c wxPENSTYLE_SOLID and @c wxCAP_ROUND styles, etc...).
  313. */
  314. wxPen* wxBLUE_PEN;
  315. /**
  316. Cyan pen.
  317. Except for the color it has all standard attributes
  318. (1-pixel width, @c wxPENSTYLE_SOLID and @c wxCAP_ROUND styles, etc...).
  319. */
  320. wxPen* wxCYAN_PEN;
  321. /**
  322. Green pen.
  323. Except for the color it has all standard attributes
  324. (1-pixel width, @c wxPENSTYLE_SOLID and @c wxCAP_ROUND styles, etc...).
  325. */
  326. wxPen* wxGREEN_PEN;
  327. /**
  328. Yellow pen.
  329. Except for the color it has all standard attributes
  330. (1-pixel width, @c wxPENSTYLE_SOLID and @c wxCAP_ROUND styles, etc...).
  331. */
  332. wxPen* wxYELLOW_PEN;
  333. /**
  334. Black pen.
  335. Except for the color it has all standard attributes
  336. (1-pixel width, @c wxPENSTYLE_SOLID and @c wxCAP_ROUND styles, etc...).
  337. */
  338. wxPen* wxBLACK_PEN;
  339. /**
  340. White pen.
  341. Except for the color it has all standard attributes
  342. (1-pixel width, @c wxPENSTYLE_SOLID and @c wxCAP_ROUND styles, etc...).
  343. */
  344. wxPen* wxWHITE_PEN;
  345. /**
  346. Transparent pen.
  347. Except for the color it has all standard attributes
  348. (1-pixel width, @c wxPENSTYLE_SOLID and @c wxCAP_ROUND styles, etc...).
  349. */
  350. wxPen* wxTRANSPARENT_PEN;
  351. /**
  352. Black dashed pen.
  353. Except for the color and for the @c wxPENSTYLE_SHORT_DASH it has all standard attributes
  354. (1-pixel width, @c wxCAP_ROUND style, etc...).
  355. */
  356. wxPen* wxBLACK_DASHED_PEN;
  357. /**
  358. Grey pen.
  359. Except for the color it has all standard attributes
  360. (1-pixel width, @c wxPENSTYLE_SOLID and @c wxCAP_ROUND styles, etc...).
  361. */
  362. wxPen* wxGREY_PEN;
  363. /**
  364. Medium-grey pen.
  365. Except for the color it has all standard attributes
  366. (1-pixel width, @c wxPENSTYLE_SOLID and @c wxCAP_ROUND styles, etc...).
  367. */
  368. wxPen* wxMEDIUM_GREY_PEN;
  369. /**
  370. Light-grey pen.
  371. Except for the color it has all standard attributes
  372. (1-pixel width, @c wxPENSTYLE_SOLID and @c wxCAP_ROUND styles, etc...).
  373. */
  374. wxPen* wxLIGHT_GREY_PEN;
  375. /**
  376. @class wxPenList
  377. There is only one instance of this class: ::wxThePenList.
  378. Use this object to search for a previously created pen of the desired
  379. type and create it if not already found. In some windowing systems,
  380. the pen may be a scarce resource, so it can pay to reuse old
  381. resources if possible. When an application finishes, all pens will
  382. be deleted and their resources freed, eliminating the possibility of
  383. 'memory leaks'. However, it is best not to rely on this automatic
  384. cleanup because it can lead to double deletion in some circumstances.
  385. There are two mechanisms in recent versions of wxWidgets which make the
  386. pen list less useful than it once was. Under Windows, scarce resources
  387. are cleaned up internally if they are not being used. Also, a referencing
  388. counting mechanism applied to all GDI objects means that some sharing
  389. of underlying resources is possible. You don't have to keep track of pointers,
  390. working out when it is safe delete a pen, because the referencing counting does
  391. it for you. For example, you can set a pen in a device context, and then
  392. immediately delete the pen you passed, because the pen is 'copied'.
  393. So you may find it easier to ignore the pen list, and instead create
  394. and copy pens as you see fit. If your Windows resource meter suggests
  395. your application is using too many resources, you can resort to using
  396. GDI lists to share objects explicitly.
  397. The only compelling use for the pen list is for wxWidgets to keep
  398. track of pens in order to clean them up on exit. It is also kept for
  399. backward compatibility with earlier versions of wxWidgets.
  400. @library{wxcore}
  401. @category{gdi}
  402. @stdobjects
  403. ::wxThePenList
  404. @see wxPen
  405. */
  406. class wxPenList
  407. {
  408. public:
  409. /**
  410. Constructor. The application should not construct its own pen list:
  411. use the object pointer ::wxThePenList.
  412. */
  413. wxPenList();
  414. /**
  415. Finds a pen with the specified attributes and returns it, else creates a
  416. new pen, adds it to the pen list, and returns it.
  417. @param colour
  418. Colour object.
  419. @param width
  420. Width of pen.
  421. @param style
  422. Pen style. See ::wxPenStyle for a list of styles.
  423. */
  424. wxPen* FindOrCreatePen(const wxColour& colour,
  425. int width = 1,
  426. wxPenStyle style = wxPENSTYLE_SOLID);
  427. };
  428. /**
  429. The global list of wxPen objects ready to be re-used (for better performances).
  430. */
  431. wxPenList* wxThePenList;