Advertisement
Guest User

BMapButton.cpp

a guest
Dec 8th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. /*
  2. Open Tracker License
  3.  
  4. Terms and Conditions
  5.  
  6. Copyright (c) 1991-2001, Be Incorporated. All rights reserved.
  7.  
  8. Permission is hereby granted, free of charge, to any person obtaining a copy of
  9. this software and associated documentation files (the "Software"), to deal in
  10. the Software without restriction, including without limitation the rights to
  11. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  12. of the Software, and to permit persons to whom the Software is furnished to do
  13. so, subject to the following conditions:
  14.  
  15. The above copyright notice and this permission notice applies to all licensees
  16. and shall be included in all copies or substantial portions of the Software.
  17.  
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  22. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
  23. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24.  
  25. Except as contained in this notice, the name of Be Incorporated shall not be
  26. used in advertising or otherwise to promote the sale, use or other dealings in
  27. this Software without prior written authorization from Be Incorporated.
  28.  
  29. BeMail(TM), Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
  30. of Be Incorporated in the United States and other countries. Other brand product
  31. names are registered trademarks or trademarks of their respective holders.
  32. All rights reserved.
  33. */
  34.  
  35.  
  36. #include "BmapButton.h"
  37.  
  38. #include <Application.h>
  39. #include <Autolock.h>
  40. #include <Bitmap.h>
  41. #include <ColorTools.h>
  42. #include <Resources.h>
  43. #include <IconUtils.h>
  44.  
  45. #include <stdlib.h>
  46.  
  47.  
  48. BList BmapButton::fBitmapCache;
  49. BLocker BmapButton::fBmCacheLock;
  50.  
  51.  
  52. struct BitmapItem {
  53. BBitmap* bm;
  54. int32 id;
  55. int32 openCount;
  56. };
  57.  
  58.  
  59. BmapButton::BmapButton(BRect frame,
  60. const char* name,
  61. const char* label,
  62. int32 enabledID,
  63. int32 disabledID,
  64. int32 rollID,
  65. int32 pressedID,
  66. bool showLabel,
  67. BMessage* message,
  68. uint32 resizeMask,
  69. uint32 flags) :
  70. BControl(frame, name, label, message, resizeMask, flags),
  71. fPressing(false),
  72. fIsInBounds(false),
  73. fShowLabel(showLabel),
  74. fActive(true),
  75. fIButtons(0)
  76. {
  77. fEnabledBM = RetrieveBitmap(enabledID);
  78. fDisabledBM = RetrieveBitmap(disabledID);
  79. fRollBM = RetrieveBitmap(rollID);
  80. fPressedBM = RetrieveBitmap(pressedID);
  81. }
  82.  
  83.  
  84. BmapButton::~BmapButton(void)
  85. {
  86. ReleaseBitmap(fEnabledBM);
  87. ReleaseBitmap(fDisabledBM);
  88. ReleaseBitmap(fRollBM);
  89. ReleaseBitmap(fPressedBM);
  90. }
  91.  
  92.  
  93. const BBitmap*
  94. BmapButton::RetrieveBitmap(int32 id)
  95. {
  96. // Lock access to the list
  97. BAutolock lock(fBmCacheLock);
  98. if (!lock.IsLocked())
  99. return NULL;
  100.  
  101. // Check for the bitmap in the cache first
  102. BitmapItem* item;
  103. for (int32 i=0; (item=(BitmapItem*)fBitmapCache.ItemAt(i)) != NULL; i++) {
  104. if (item->id == id) {
  105. item->openCount++;
  106. return item->bm;
  107. }
  108. }
  109.  
  110. // If it's not in the cache, try to load it
  111. BResources* res = BApplication::AppResources();
  112. if (!res) return NULL;
  113.  
  114. //size_t size = 0;
  115. m_Icon = new BBitmap (BRect (0, 0, 15, 15), B_RGBA32);
  116. size_t size;
  117. //const void* data = res->LoadResource('VICN', id, &size);
  118. id = 20; //temporary
  119. const void* data = res->LoadResource('VICN', id, &size); //LATEST
  120. if(data !=NULL) {
  121. BIconUtils::GetVectorIcon((const uint8*)data, size, m_Icon);
  122. }
  123. // if (!data) return NULL;
  124. BMemoryIO mio(data, size);
  125. BMessage arch;
  126. if (arch.Unflatten(&mio) != B_OK) return NULL;
  127.  
  128. BArchivable* obj = instantiate_object(&arch);
  129. BBitmap* bm = dynamic_cast<BBitmap*>(obj);
  130. if (!bm) {
  131. delete obj;
  132. return NULL;
  133. }
  134.  
  135. item = (BitmapItem*)malloc(sizeof(BitmapItem));
  136. item->bm = bm;
  137. item->id = id;
  138. item->openCount = 1;
  139. fBitmapCache.AddItem(item);
  140. return bm;
  141. }
  142.  
  143.  
  144. status_t
  145. BmapButton::ReleaseBitmap(const BBitmap* bm)
  146. {
  147. BAutolock lock(fBmCacheLock);
  148. if (!lock.IsLocked())
  149. return B_ERROR;
  150.  
  151. BitmapItem* item;
  152. for (int32 i = 0;; i++)
  153. {
  154. item = static_cast<BitmapItem*>(fBitmapCache.ItemAt(i));
  155. if (item == NULL)
  156. break;
  157.  
  158. if (item->bm == bm) {
  159. if (--item->openCount <= 0) {
  160. fBitmapCache.RemoveItem(i);
  161. delete item->bm;
  162. free(item);
  163. }
  164. return B_OK;
  165. }
  166. }
  167. return B_ERROR;
  168. }
  169.  
  170. #define F_SHOW_GEOMETRY 0
  171.  
  172. void
  173. BmapButton::Draw(BRect updateRect)
  174. {
  175. BRect bounds(Bounds());
  176. float labelHeight, labelWidth;
  177.  
  178. #if F_SHOW_GEOMETRY
  179. StrokeRect(bounds);
  180. #endif
  181.  
  182. // Draw Label
  183. if (fShowLabel) {
  184. font_height fheight;
  185.  
  186. BFont renderFont;
  187. renderFont = *be_plain_font;
  188. renderFont.GetHeight(&fheight);
  189. SetFont(&renderFont);
  190.  
  191. labelHeight = fheight.leading + fheight.ascent + fheight.descent + 1;
  192. labelWidth = renderFont.StringWidth(Label());
  193.  
  194. BRect textRect;
  195. textRect.left = (bounds.right - bounds.left - labelWidth + 1) / 2;
  196. textRect.right = textRect.left + labelWidth;
  197. textRect.bottom = bounds.bottom;
  198. textRect.top = textRect.bottom - fheight.descent - fheight.ascent - 1;
  199.  
  200. // Only draw if it's within the update rect
  201. if (updateRect.Intersects(textRect)) {
  202. float baseLine = textRect.bottom - fheight.descent;
  203.  
  204. if (IsFocus() && fActive)
  205. SetHighColor(0, 0, 255);
  206. else
  207. SetHighColor(ViewColor());
  208. StrokeLine(BPoint(textRect.left, baseLine),
  209. BPoint(textRect.right, baseLine));
  210.  
  211. if (IsEnabled())
  212. SetHighColor(0, 0, 0);
  213. else {
  214. const rgb_color black = {0, 0, 0, 255};
  215. SetHighColor(disable_color(black, ViewColor()));
  216. }
  217. MovePenTo(textRect.left, baseLine);
  218. DrawString(Label());
  219.  
  220. #if F_SHOW_GEOMETRY
  221. FrameRect(textRect);
  222. #endif
  223. }
  224. } else {
  225. labelHeight = 0;
  226. labelWidth = 0;
  227. }
  228.  
  229. // Draw Bitmap
  230.  
  231. // Select the bitmap to use
  232. const BBitmap* bm;
  233.  
  234. if (!IsEnabled())
  235. bm = fDisabledBM;
  236. else if (fPressing) {
  237. if (fIsInBounds)
  238. bm = fPressedBM;
  239. else
  240. bm = fRollBM;
  241. } else {
  242. if (fIsInBounds)
  243. bm = fRollBM;
  244. else
  245. bm = fEnabledBM;
  246. }
  247.  
  248. // Draw the bitmap
  249. if (bm) {
  250. fBitmapRect = bm->Bounds();
  251. fBitmapRect.OffsetTo(0, 0);
  252. fBitmapRect.OffsetBy((bounds.right - bounds.left - fBitmapRect.right
  253. - fBitmapRect.left) / 2,
  254. (bounds.bottom - bounds.top - labelHeight - fBitmapRect.bottom
  255. - fBitmapRect.top) / 2);
  256. // Update if within update rect
  257.  
  258. SetDrawingMode(B_OP_OVER);
  259.  
  260. if (updateRect.Intersects(fBitmapRect)) {
  261. DrawBitmap(bm, fBitmapRect);
  262. #if F_SHOW_GEOMETRY
  263. StrokeRect(fBitmapRect);
  264. #endif
  265. }
  266.  
  267. SetDrawingMode(B_OP_COPY);
  268. }
  269. }
  270.  
  271.  
  272. void
  273. BmapButton::GetPreferredSize(float* width, float* height)
  274. {
  275. BRect prefBounds;
  276.  
  277. if (fEnabledBM) {
  278. if (fShowLabel) {
  279. float labelHeight, labelWidth;
  280. font_height fheight;
  281. BRect bmBounds(fEnabledBM->Bounds());
  282. BFont renderFont;
  283. renderFont = *be_plain_font;
  284. renderFont.GetHeight(&fheight);
  285. SetFont(&renderFont);
  286.  
  287. labelHeight = fheight.leading + fheight.ascent + fheight.descent + 1;
  288. labelWidth = renderFont.StringWidth(Label());
  289. prefBounds.left = 0;
  290. prefBounds.top = 0;
  291. prefBounds.right = labelWidth > (bmBounds.right - bmBounds.left)
  292. ? labelWidth : (bmBounds.right - bmBounds.left);
  293. prefBounds.bottom = labelHeight + (bmBounds.bottom - bmBounds.top);
  294. } else
  295. prefBounds = fEnabledBM->Bounds();
  296. } else
  297. prefBounds = Bounds();
  298.  
  299. *width = prefBounds.IntegerWidth();
  300. *height = prefBounds.IntegerHeight();
  301. }
  302.  
  303.  
  304. void
  305. BmapButton::MouseMoved(BPoint where, uint32 code, const BMessage* msg)
  306. {
  307. // eliminate unused parameter warnings
  308. (void)where;
  309. (void)msg;
  310.  
  311. if (IsEnabled() && fActive) {
  312. switch(code) {
  313. case B_ENTERED_VIEW:
  314. fIsInBounds = true;
  315. Invalidate(fBitmapRect);
  316. break;
  317.  
  318. case B_EXITED_VIEW:
  319. fIsInBounds = false;
  320. Invalidate(fBitmapRect);
  321. break;
  322. }
  323. }
  324. }
  325.  
  326.  
  327. void
  328. BmapButton::MouseDown(BPoint point)
  329. {
  330. if (!IsEnabled())
  331. return;
  332. // Save Mouse State
  333. GetMouse(&point, &fButtons);
  334. fWhere = point;
  335.  
  336. if (fButtons & fIButtons) {
  337. BMessage copy(*Message());
  338. copy.AddPoint("where", ConvertToScreen(fWhere));
  339. copy.AddInt32("buttons", fButtons);
  340. Invoke(&copy);
  341. return;
  342. }
  343.  
  344. SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS | B_SUSPEND_VIEW_FOCUS | B_NO_POINTER_HISTORY);
  345. fPressing = true;
  346. Invalidate(fBitmapRect);
  347. }
  348.  
  349.  
  350. void
  351. BmapButton::MouseUp(BPoint where)
  352. {
  353. if (atomic_and(&fPressing, 0)) {
  354. SetMouseEventMask(0, 0);
  355. if (Bounds().Contains(where) && IsEnabled()) {
  356. BMessage copy(*Message());
  357. copy.AddPoint("where", ConvertToScreen(fWhere));
  358. copy.AddInt32("buttons", fButtons);
  359. Invoke(&copy);
  360. }
  361. Invalidate(fBitmapRect);
  362. }
  363. }
  364.  
  365.  
  366. void
  367. BmapButton::ShowLabel(bool show)
  368. {
  369. fShowLabel = show;
  370. }
  371.  
  372.  
  373. void
  374. BmapButton::WindowActivated(bool active)
  375. {
  376. fActive = active;
  377. if (IsFocus() || fIsInBounds) {
  378. fIsInBounds = false;
  379. Invalidate();
  380. }
  381. BControl::WindowActivated(active);
  382. }
  383.  
  384.  
  385. void
  386. BmapButton::InvokeOnButton(uint32 button)
  387. {
  388. fIButtons = button;
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement