BitmapView.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright 2009-2010, Pier Luigi Fiorini. All rights reserved.
  3. * Distributed under the terms of the MIT License.
  4. *
  5. * Authors:
  6. * Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
  7. */
  8. #include <new>
  9. #include <Bitmap.h>
  10. #include <LayoutUtils.h>
  11. #include <TranslationUtils.h>
  12. #include "BitmapView.h"
  13. const float kMinWidth = 32.0f;
  14. const float kMinHeight = 32.0f;
  15. BitmapView::BitmapView(const char* name, uint32 flags)
  16. :
  17. BView(name, flags | B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
  18. fBitmap(NULL),
  19. fIsSquare(false),
  20. fWidth(kMinWidth),
  21. fHeight(kMinHeight)
  22. {
  23. }
  24. BitmapView::~BitmapView()
  25. {
  26. delete fBitmap;
  27. fBitmap = NULL;
  28. }
  29. void
  30. BitmapView::AttachedToWindow()
  31. {
  32. // Set view color to parent's view color
  33. if (Parent() != NULL)
  34. SetViewColor(Parent()->ViewColor());
  35. else
  36. SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
  37. }
  38. status_t
  39. BitmapView::InitCheck()
  40. {
  41. if (fBitmap != NULL)
  42. return B_OK;
  43. return B_ERROR;
  44. }
  45. BBitmap*
  46. BitmapView::Bitmap() const
  47. {
  48. return fBitmap;
  49. }
  50. status_t
  51. BitmapView::SetBitmap(const char* filename)
  52. {
  53. delete fBitmap;
  54. fBitmap = BTranslationUtils::GetBitmap(filename);
  55. if (fBitmap == NULL)
  56. return B_ERROR;
  57. return B_OK;
  58. }
  59. status_t
  60. BitmapView::SetBitmap(const BBitmap* bitmap)
  61. {
  62. delete fBitmap;
  63. fBitmap = NULL;
  64. if (bitmap != NULL) {
  65. fBitmap = new(std::nothrow) BBitmap(bitmap);
  66. if (fBitmap == NULL)
  67. return B_NO_MEMORY;
  68. if (fBitmap->InitCheck() != B_OK)
  69. return fBitmap->InitCheck();
  70. fWidth = fBitmap->Bounds().Width();
  71. fHeight = fBitmap->Bounds().Height();
  72. Invalidate();
  73. }
  74. return B_OK;
  75. }
  76. void
  77. BitmapView::SetSquare(bool isSquare)
  78. {
  79. fIsSquare = isSquare;
  80. }
  81. BSize
  82. BitmapView::MinSize()
  83. {
  84. return BLayoutUtils::ComposeSize(ExplicitMinSize(),
  85. BSize(kMinWidth, kMinHeight));
  86. }
  87. BSize
  88. BitmapView::MaxSize()
  89. {
  90. return BLayoutUtils::ComposeSize(ExplicitMaxSize(),
  91. BSize(fWidth, fHeight));
  92. }
  93. BSize
  94. BitmapView::PreferredSize()
  95. {
  96. return BLayoutUtils::ComposeSize(ExplicitPreferredSize(),
  97. BSize(fWidth, fHeight));
  98. }
  99. void
  100. BitmapView::Draw(BRect frame)
  101. {
  102. if (fBitmap == NULL)
  103. return;
  104. SetDrawingMode(B_OP_ALPHA);
  105. SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
  106. DrawBitmap(fBitmap, fBitmap->Bounds(),
  107. _ViewBounds(), B_FILTER_BITMAP_BILINEAR);
  108. }
  109. BRect
  110. BitmapView::_ViewBounds()
  111. {
  112. BRect bounds = Bounds();
  113. if (fIsSquare == false || bounds.Height() == bounds.Width())
  114. return bounds;
  115. BPoint lt = bounds.LeftTop();
  116. BPoint rb = bounds.RightBottom();
  117. float diff = 0.0;
  118. if (bounds.Height() > bounds.Width()) {
  119. diff = bounds.Height() - bounds.Width();
  120. lt -= BPoint(0.0, diff / 2);
  121. rb += BPoint(0.0, diff / 2);
  122. }
  123. else if (bounds.Width() > bounds.Height()) {
  124. diff = bounds.Height() - bounds.Width();
  125. lt -= BPoint(diff / 2, 0.0);
  126. rb += BPoint(diff / 2, 0.0);
  127. }
  128. return BRect(lt, rb);
  129. }