BitmapMenuItem.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. BitmapListItem.cpp: A BMenuItem with an optional picture
  3. Written by DarkWyrm <darkwyrm@earthlink.net>, Copyright 2007
  4. Released under the MIT license.
  5. */
  6. #include "BitmapMenuItem.h"
  7. #include <Bitmap.h>
  8. BitmapMenuItem::BitmapMenuItem(const char* label, BMessage* msg,
  9. BBitmap* bitmap, char shortcut, uint32 modifiers, bool ownership)
  10. :
  11. BMenuItem(label, msg, shortcut, modifiers),
  12. fBitmap(bitmap),
  13. fOwnership(ownership)
  14. {
  15. }
  16. BitmapMenuItem::BitmapMenuItem(BMessage* data)
  17. :
  18. BMenuItem(data)
  19. {
  20. fBitmap = (BBitmap*) BBitmap::Instantiate(data);
  21. }
  22. BitmapMenuItem::~BitmapMenuItem(void)
  23. {
  24. if (fOwnership == true)
  25. delete fBitmap;
  26. }
  27. status_t
  28. BitmapMenuItem::Archive(BMessage* data, bool deep) const
  29. {
  30. status_t status = BMenuItem::Archive(data, deep);
  31. if (status == B_OK && fBitmap)
  32. status = fBitmap->Archive(data, deep);
  33. if (status == B_OK)
  34. status = data->AddString("class", "BitmapMenuItem");
  35. return status;
  36. }
  37. void
  38. BitmapMenuItem::GetContentSize(float* width, float* height)
  39. {
  40. float w,h;
  41. BMenuItem::GetContentSize(&w,&h);
  42. if (fBitmap)
  43. w += (fBitmap->Bounds().Width() *
  44. (Frame().Height() / fBitmap->Bounds().Height())) + 20;
  45. if (width)
  46. *width = w;
  47. if (height)
  48. *height = h;
  49. }
  50. void
  51. BitmapMenuItem::DrawContent(void)
  52. {
  53. if (!Label() && !fBitmap)
  54. return;
  55. float width, height;
  56. GetContentSize(&width, &height);
  57. BRect drawrect(Frame());
  58. drawrect.bottom--;
  59. drawrect.top++;
  60. drawrect.left = ContentLocation().x;
  61. if (fBitmap) {
  62. // Scale the fBitmap down to completely fit within the field's height
  63. if (fBitmap->Bounds().Height() > drawrect.Height()) {
  64. drawrect.right = drawrect.left +
  65. (fBitmap->Bounds().Width() *
  66. (Frame().Height() / fBitmap->Bounds().Height()));
  67. } else {
  68. drawrect.right = drawrect.left + fBitmap->Bounds().Width();
  69. }
  70. } else {
  71. drawrect.right = drawrect.left - 5;
  72. }
  73. BPoint stringpoint(ContentLocation());
  74. stringpoint.x = drawrect.right + 10;
  75. Menu()->MovePenTo(stringpoint);
  76. BMenuItem::DrawContent();
  77. if (fBitmap) {
  78. Menu()->PushState();
  79. Menu()->SetDrawingMode(B_OP_ALPHA);
  80. Menu()->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
  81. Menu()->DrawBitmap(fBitmap, fBitmap->Bounds(), drawrect, B_FILTER_BITMAP_BILINEAR);
  82. Menu()->PopState();
  83. }
  84. }
  85. void
  86. BitmapMenuItem::SetBitmap(BBitmap *bitmap)
  87. {
  88. if (fOwnership == true)
  89. delete fBitmap;
  90. fBitmap = bitmap;
  91. }