1
0

buttwnd.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. #ifndef _BUTTWND_H
  2. #define _BUTTWND_H
  3. #include <wasabicfg.h>
  4. #include <bfc/common.h>
  5. #include <tataki/canvas/canvas.h>
  6. #include <tataki/bitmap/autobitmap.h>
  7. #include <api/wnd/wndclass/guiobjwnd.h>
  8. #include <tataki/color/skinclr.h>
  9. #include <api/wnd/accessible.h>
  10. #include <api/wnd/textalign.h>
  11. class api_region;
  12. #define DEFAULT_BUTTON_TEXT_SIZE 14
  13. /**
  14. Button Text Alignment
  15. Darkain: this was changed to use TextAlign
  16. */
  17. /*
  18. typedef enum {
  19. BUTTONJUSTIFY_LEFT,
  20. BUTTONJUSTIFY_CENTER
  21. } ButtonJustify;
  22. */
  23. #define DEFEREDCB_DOWN 0x450
  24. #define DEFEREDCB_UP 0x451
  25. #define BUTTONWND_PARENT GuiObjectWnd
  26. /**
  27. A fully skinnable button. Has images for normal, hilited, activated states.
  28. Plus images for a checked state. It may also be used to draw OS style buttons.
  29. See setBorderStyle() for more details.
  30. @short Button control.
  31. @author Nullsoft
  32. @ver 1.0
  33. @see ButtBar
  34. */
  35. class ButtonWnd : public BUTTONWND_PARENT {
  36. public:
  37. /**
  38. Sets defaults for ButtonWnd objects.
  39. @see ~ButtonWnd()
  40. @param button_text The button's caption.
  41. */
  42. ButtonWnd(const wchar_t *button_text=NULL);
  43. /**
  44. Deletes components of ButtonWnd.
  45. @see ButtonWnd()
  46. */
  47. virtual ~ButtonWnd();
  48. /**
  49. Paints the bitmap on canvas according
  50. to current options (centering, tiling, stretching, title).
  51. @ret 0 for failure, 1 for success
  52. @param canvas The canvas on which to paint.
  53. */
  54. virtual int onPaint(Canvas *canvas);
  55. /**
  56. Sets the bitmaps that will be used to render the button.
  57. This includes bitmaps for various button states. Also enables
  58. you to set the colorgroup (gammagroup) for the bitmaps.
  59. @ret 1
  60. @param _normal Bitmap for normal state.
  61. @param _pushed Bitmap for pushed state.
  62. @param _hilited Bitmap for hilited state.
  63. @param _activated Bitmap for activated state.
  64. @param colorgroup The colorgroup for the bitmaps (gammagroup).
  65. */
  66. int setBitmaps(const wchar_t *normal, const wchar_t *pushed=NULL, const wchar_t *hilited=NULL, const wchar_t *activated=NULL);
  67. SkinBitmap *getNormalBitmap();
  68. /**
  69. Sets the bitmaps that will be used to render the button.
  70. This includes bitmaps for various button states. Also enables
  71. you to set the colorgroup (gammagroup) for the bitmaps.
  72. @ret 1
  73. @param hInst The parent window's instance handle.
  74. @param _normal Bitmap for normal state.
  75. @param _pushed Bitmap for pushed state.
  76. @param _hilited Bitmap for hilited state.
  77. @param _activated Bitmap for activated state.
  78. @param colorgroup The colorgroup for the bitmaps (gammagroup).
  79. */
  80. int setBitmaps(OSMODULEHANDLE hInst, int normal, int pushed, int hilited, int activated, const wchar_t *colorgroup=NULL);
  81. /**
  82. Set the right bitmap to be used.
  83. @see setBitmaps()
  84. @ret 1
  85. @param bitmap The name of the bitmap to use.
  86. */
  87. int setRightBitmap(const wchar_t *bitmap);
  88. /**
  89. Center the bitmap?
  90. @see setBitmaps()
  91. @ret Normalized flag
  92. @param centerit A non zero value will center the bitmap.
  93. */
  94. int setBitmapCenter(int centerit);
  95. /**
  96. Sets base texture and causes rerendering.
  97. @see setBaseTexture()
  98. @param useit A non zero value will use the base texture.
  99. */
  100. void setUseBaseTexture(int useit);
  101. /**
  102. Sets bitmap for button, sets position for button, flags whether to tile the bitmap
  103. @see setUseBaseTexture()
  104. @param bmp Skin bitmap for button
  105. @param x Button position on x-coordinate
  106. @param yButton position on y-coordinate
  107. @param tile Flag
  108. */
  109. void setBaseTexture(SkinBitmap *bmp, int x, int y, int tile=0);
  110. /**
  111. Sets the colorgroup (gammagroup) for all the bitmaps associated with
  112. this button.
  113. @param _colorgroup The colorgroup for the bitmaps.
  114. */
  115. void setHInstanceColorGroup(const wchar_t *_colorgroup) { colorgroup = _colorgroup; }
  116. /**
  117. Writes given text to button in given size and triggers rendering.
  118. @see getButtonText()
  119. @assert Text string is not empty
  120. @ret 1
  121. @param text Label text
  122. @param size Size to render label text
  123. */
  124. int setButtonText(const wchar_t *text, int size=DEFAULT_BUTTON_TEXT_SIZE);
  125. /**
  126. Gets text from button.
  127. @see setButtonText()
  128. @ret Button text string
  129. */
  130. const wchar_t * getButtonText();
  131. /**
  132. Sets text to render at left, in center, or at right.
  133. @see setButtonText()
  134. @see getButtonText()
  135. @see ButtonJustify
  136. @param jus BUTTONJUSTIFY_LEFT, left justified; BUTTONJUSTIFY_CENTER, centered;
  137. */
  138. // void setTextJustification(ButtonJustify jus);
  139. void setTextAlign(TextAlign align);
  140. TextAlign getTextAlign() { return alignment; }
  141. /**
  142. Enables and disables wantfocus for the button. When disabled, the button can
  143. never receive focus.
  144. @param want !0, enable focus; 0, disable focus;
  145. */
  146. void setWantFocus(int want) { iwantfocus = !!want; }
  147. /**
  148. Return the wantfocus
  149. */
  150. virtual int wantFocus() const { return iwantfocus; }
  151. /**
  152. Event is triggered when the mouse leaves the button's region.
  153. Override this event to implement your own behavior.
  154. */
  155. virtual void onLeaveArea();
  156. virtual void onEnterArea();
  157. /**
  158. Gets width of button, allowing for length of text plus button margin, if any.
  159. @see getHeight()
  160. @ret Button width (in pixels).
  161. */
  162. int getWidth(); // our preferred width and height (from bitmaps)
  163. /**
  164. Gets height of button, allowing for height of text plus button margin, if any.
  165. @see getWidth()
  166. @ret Button height (in pixels).
  167. */
  168. int getHeight();
  169. /**
  170. Event is triggered when focus is given to the button.
  171. Override this event to implement your own behavior.
  172. @see onKillFocus()
  173. @ret 1
  174. */
  175. virtual int onGetFocus();
  176. /**
  177. Event is triggered when the button focus is lost.
  178. Override this event to implement your own behavior.
  179. @see onGetFocus()
  180. @ret 1
  181. */
  182. virtual int onKillFocus();
  183. /**
  184. Event is triggered when a key is pressed and the button
  185. has focus.
  186. @ret 1, if you handle the event;
  187. @param c The value of the key that was pressed.
  188. */
  189. virtual int onChar(unsigned int c);
  190. /**
  191. Saves new status and rerenders, if button enabled status changes.
  192. @see getEnabled()
  193. @see onEnable()
  194. @param _enabled 0, disabled; !0 enabled;
  195. */
  196. void enableButton(int enabled); // can be pushed
  197. /**
  198. Tells parent to handle left button click.
  199. @see onRightPush()
  200. @param x Mouse click x-coordinate
  201. @param y Mouse click y-coordinate
  202. */
  203. virtual void onLeftPush(int x, int y);
  204. /**
  205. Passes right mouse clicks to the parent.
  206. @see onLeftPush()
  207. @param x Mouse click x-coordinate
  208. @param y Mouse click y-coordinate
  209. */
  210. virtual void onRightPush(int x, int y);
  211. /**
  212. Passes left double click to parent.
  213. @see onRightDoubleClick()
  214. @param x Mouse click x-coordinate
  215. @param y Mouse click y-coordinate
  216. */
  217. virtual void onLeftDoubleClick(int x, int y);
  218. /**
  219. Passes right double click to parent
  220. @see onLeftDoubleClick()
  221. @param x Mouse click x-coordinate
  222. @param y Mouse click y-coordinate
  223. */
  224. virtual void onRightDoubleClick(int x, int y);
  225. /**
  226. Event is triggered when the button will be resized.
  227. Override this event to implement your own behavior.
  228. The default behavior is to cause a repaint.
  229. @ret 1
  230. */
  231. virtual int onResize();
  232. /**
  233. Sets the region pointed at after each mouse move.
  234. If the region has changed, it invalidate the region
  235. so that it will be updated on the screen.
  236. @ret Status from parent class
  237. @param x New x-coordinate of mouse cursor
  238. @param y New y-coordinate of mouse cursor
  239. */
  240. virtual int onMouseMove(int x, int y); // need to catch region changes
  241. /**
  242. Event is triggered when the button is enabled or disabled.
  243. Override this event to implement your own behavior.
  244. @see getEnabled()
  245. @ret 1
  246. @param is The enable state (nonzero is enabled).
  247. */
  248. virtual int onEnable(int is);
  249. /**
  250. Returns the value of the enabled flag.
  251. @see enableButton()
  252. @see onEnable()
  253. @ret enabled
  254. */
  255. virtual int getEnabled() const;
  256. /**
  257. Get the preferences for this button.
  258. This will enable you to read the suggested width and height
  259. for the button.
  260. @ret Width or height of the normal bitmap, as requested, or a property from the parent class.
  261. @param what SUGGESTED_W, will return the width; SUGGESTED_H, will return the height;
  262. */
  263. virtual int getPreferences(int what);
  264. /**
  265. Get the button state. This is the state caused by user interaction.
  266. @ret !0, pushed; 0, not pushed;
  267. */
  268. virtual int userDown() { return userdown; }
  269. /**
  270. */
  271. virtual int wantClicks() { return getEnabled(); }
  272. /**
  273. Set the bitmap to use when the button will be "checked".
  274. This enables you to have checked buttons and menu items.
  275. @see setChecked()
  276. @see getChecked()
  277. @param checkbm The name of the bitmap to use.
  278. */
  279. void setCheckBitmap(const wchar_t *checkbm);
  280. /**
  281. Set the checked state of the button.
  282. @param c <0, not checked; 0, none, >0 checked;
  283. */
  284. void setChecked(int c) { checked=c; }; // <0=nocheck, 0=none, >0=checked
  285. /**
  286. Get the checked state of the button.
  287. @ret <0, not checked; 0, none; >0 checked;
  288. */
  289. int getChecked() const { return checked; }
  290. /**
  291. Triggers rerendering in the opposite
  292. highlight state if the hilighting flag is changed.
  293. @see getHilite()
  294. @param h
  295. */
  296. void setHilite(int h);
  297. /**
  298. @see setHilite()
  299. @ret Is either highlighting flag set?
  300. */
  301. int getHilite();
  302. /**
  303. Simulate a button push. You can use this method to simulate
  304. menu pushing also.
  305. @see getPushed()
  306. @param p A nonzero value will simulate a push.
  307. */
  308. void setPushed(int p); // used by menus to simulate pushing
  309. /**
  310. Get the pushed state of a button.
  311. @see setPushed()
  312. @ret 0, not pushed; !0, pushed;
  313. */
  314. int getPushed() const; // used by menus to simulate pushing
  315. /**
  316. Sets the auto dim state. Autodim will dim the normal
  317. bitmap if no hilite bitmap is provided.
  318. @param ad !0, autodim on; 0, autodim off;
  319. */
  320. void setAutoDim(int ad) { autodim=!!ad; } // nonzero makes it dim if there's no hilite bitmap
  321. /**
  322. Get the autodim state.
  323. @see setAutoDim()
  324. @ret 0, autodim off; !0 autodim on;
  325. */
  326. int getAutoDim() const { return autodim; } // nonzero makes it dim if there's no hilite bitmap
  327. /**
  328. Set the active state of the button.
  329. @see getActivatedButton()
  330. @see setActivatedNoCallback()
  331. @param a !0, activate the button; 0, deactivate the button;
  332. */
  333. virtual void setActivatedButton(int a);
  334. /**
  335. Set the active state of the button, without generating a callback.
  336. This means that the onActivated event will not fire for this button.
  337. @see getActivatedButton()
  338. @see setActivatedButton()
  339. @param a !0, activate the button; 0, deactivate the button;
  340. */
  341. virtual void setActivatedNoCallback(int a);
  342. /**
  343. Get the active state of the button.
  344. @see setActivatedButton()
  345. @ret activated !0, active; 0, inactive;
  346. */
  347. virtual int getActivatedButton();
  348. /**
  349. Render borders around the button?
  350. @param b !0, borders; 0, no borders;
  351. */
  352. void setBorders(int b);
  353. /**
  354. Sets the border style for the button. This
  355. has no effect if no borders are being drawn.
  356. "button_normal" A normal button.
  357. "osbutton_normal" A normal OS button (if in Windows, will show a std win32 button).
  358. "osbutton_close" An OS close button.
  359. "osbutton_minimize" An OS minimize button.
  360. "osbutton_maximize" An OS maximize button.
  361. @see getBorderStyle()
  362. @param style The style of button you want.
  363. */
  364. void setBorderStyle(const wchar_t *style);
  365. /**
  366. Get the border style of the button (if there is one).
  367. If no border is drawn, this method always returns NULL.
  368. @see setBorderStyle()
  369. @ret The border style.
  370. */
  371. const wchar_t *getBorderStyle();
  372. /**
  373. Set the inactive alpha blending value. This is the alpha blending
  374. value that will be used for blending when the button does NOT have focus.
  375. @param a The alpha value, range is from 0 (fully transparent) to 255 (fully opaque).
  376. */
  377. void setInactiveAlpha(int a);
  378. /**
  379. Set the active alpha blending value. This is the alpha blending value
  380. that will be used for blending when the button HAS focus.
  381. @param a The alpha value, range is from 0 (fully transparent) to 255 (fully opaque).
  382. */
  383. void setActiveAlpha(int a);
  384. /**
  385. Sets the colors for various states of our button. This is
  386. done via element id's which are in the skin xml or registered
  387. as seperate xml.
  388. @param text Normal text color (window has focus but button is not active).
  389. @param hilite Hilited text color (button has focus).
  390. @param dimmed Dimmed text color (parent window doesn't even have focus).
  391. */
  392. void setColors(const wchar_t *text=L"studio.button.text", const wchar_t *hilite=L"studio.button.hiliteText", const wchar_t *dimmed=L"studio.button.dimmedText");
  393. /**
  394. Deletes the regions and resets them to NULL.
  395. @see reloadResources()
  396. */
  397. virtual void freeResources();
  398. /**
  399. Reinitializes regions for which there are bitmaps available.
  400. @see freeResources()
  401. */
  402. virtual void reloadResources();
  403. /**
  404. Event is triggered when the is being activated.
  405. Override this event to implement your own behavior.
  406. @see setActivatedButton()
  407. @ret 1
  408. @param active The button's state (nonzero is active).
  409. */
  410. virtual int onActivateButton(int active);
  411. /**
  412. Returns the current region of the button.
  413. @see api_region
  414. @ret The region of the button.
  415. */
  416. virtual api_region *getRegion();
  417. /**
  418. Set the modal return. This is what will be returned
  419. when the window is closed and the window is set to modal.
  420. @param r The return code you wish to set.
  421. */
  422. virtual void setModalRetCode(int r);
  423. /**
  424. Get the modal return code for the window.
  425. @ret The modal return code.
  426. */
  427. virtual int getModalRetCode() const;
  428. /**
  429. Event is triggered when the button is about to be initialized.
  430. Override this event to implement your own behavior.
  431. @ret 1
  432. */
  433. virtual int onInit();
  434. virtual int onDeferredCallback(intptr_t p1, intptr_t p2);
  435. virtual void setTextColor(const wchar_t *text);
  436. virtual void setTextHoverColor(const wchar_t *text);
  437. virtual void setTextDimmedColor(const wchar_t *text);
  438. virtual void checkState(POINT *pt=NULL);
  439. virtual void onCancelCapture();
  440. private:
  441. AutoSkinBitmap normalbmp, pushedbmp, hilitebmp, checkbmp, rightbmp, activatedbmp;
  442. SkinBitmap *base_texture;
  443. RegionI *normalrgn, *pushedrgn, *hirgn, *currgn, *activatedrgn;
  444. int textsize;
  445. TextAlign alignment;
  446. SkinColor color_text, color_hilite, color_dimmed;
  447. int retcode;
  448. StringW normalBmpStr, pushedBmpStr, hilitedBmpStr, activatedBmpStr;
  449. int folderstyle;
  450. int autodim;
  451. int userhilite;
  452. int userdown;
  453. int activated;
  454. int enabled;
  455. int borders;
  456. const wchar_t *borderstyle;
  457. int dsoNormal, dsoPushed, dsoDisabled;
  458. int iwantfocus;
  459. int center_bitmap;
  460. int use_base_texture;
  461. int checked;
  462. int xShift, yShift, tile_base_texture;
  463. int inactivealpha, activealpha;
  464. StringW colorgroup;
  465. int forcedown;
  466. };
  467. #endif