RosterItem.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 2009, Andrea Anzani. All rights reserved.
  3. * Distributed under the terms of the MIT License.
  4. *
  5. * Authors:
  6. * Andrea Anzani, andrea.anzani@gmail.com
  7. * Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
  8. * Jaidyn Levesque, jadedctrl@teknik.io
  9. */
  10. #include <ListView.h>
  11. #include <libinterface/BitmapUtils.h>
  12. #include "AppResources.h"
  13. #include "Contact.h"
  14. #include "NotifyMessage.h"
  15. #include "RosterItem.h"
  16. #include "Utils.h"
  17. RosterItem::RosterItem(const char* name, Contact* contact)
  18. : BStringItem(name),
  19. fBitmap(contact->AvatarBitmap()),
  20. fStatus(contact->GetNotifyStatus()),
  21. fContact(contact),
  22. fVisible(true)
  23. {
  24. rgb_color highlightColor = ui_color(B_LIST_SELECTED_BACKGROUND_COLOR);
  25. rgb_color darkenHighlightColor = tint_color(highlightColor, B_DARKEN_1_TINT);
  26. fGradient.AddColor(highlightColor, 0);
  27. fGradient.AddColor(darkenHighlightColor, 255);
  28. }
  29. RosterItem::~RosterItem()
  30. {
  31. delete fBitmap;
  32. }
  33. void
  34. RosterItem::SetVisible(bool visible)
  35. {
  36. fVisible = visible;
  37. }
  38. void
  39. RosterItem::SetBitmap(BBitmap* bitmap)
  40. {
  41. fBitmap = bitmap;
  42. }
  43. void
  44. RosterItem::ObserveString(int32 what, BString str)
  45. {
  46. switch (what) {
  47. case STR_CONTACT_NAME:
  48. SetText(str);
  49. break;
  50. case STR_PERSONAL_STATUS:
  51. SetPersonalStatus(str);
  52. break;
  53. }
  54. }
  55. void
  56. RosterItem::ObservePointer(int32 what, void* ptr)
  57. {
  58. switch (what) {
  59. case PTR_AVATAR_BITMAP:
  60. SetBitmap((BBitmap*)ptr);
  61. break;
  62. }
  63. }
  64. void
  65. RosterItem::ObserveInteger(int32 what, int32 val)
  66. {
  67. switch (what) {
  68. case INT_CONTACT_STATUS:
  69. SetStatus((UserStatus)val);
  70. break;
  71. }
  72. }
  73. void
  74. RosterItem::DrawItem(BView* owner, BRect frame, bool complete)
  75. {
  76. if (Text() == NULL)
  77. return;
  78. rgb_color highlightColor = ui_color(B_CONTROL_HIGHLIGHT_COLOR);
  79. rgb_color highColor = owner->HighColor();
  80. rgb_color lowColor = owner->LowColor();
  81. float h = frame.Height();
  82. // Draw selection
  83. if (IsSelected()) {
  84. fGradient.SetStart(frame.LeftTop());
  85. fGradient.SetEnd(frame.LeftBottom());
  86. owner->SetLowColor(highlightColor);
  87. owner->FillRect(frame, fGradient);
  88. } else if (complete) {
  89. owner->SetHighColor(lowColor);
  90. owner->FillRect(frame);
  91. }
  92. // Draw contact status
  93. switch (fStatus) {
  94. case STATUS_ONLINE:
  95. owner->SetHighColor(APP_GREEN_COLOR);
  96. break;
  97. case STATUS_CUSTOM_STATUS:
  98. case STATUS_AWAY:
  99. owner->SetHighColor(APP_ORANGE_COLOR);
  100. break;
  101. case STATUS_DO_NOT_DISTURB:
  102. owner->SetHighColor(APP_RED_COLOR);
  103. break;
  104. case STATUS_INVISIBLE:
  105. case STATUS_OFFLINE:
  106. break;
  107. default:
  108. break;
  109. }
  110. owner->FillRect(BRect(frame.left,
  111. frame.top,
  112. frame.left + 5, frame.top + h - 1
  113. ));
  114. // Draw avatar icon
  115. if (fBitmap != NULL) {
  116. BRect rect(frame.left + 6, frame.top,
  117. frame.left + 42, frame.top + h);
  118. owner->SetDrawingMode(B_OP_ALPHA);
  119. owner->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
  120. owner->DrawBitmap(fBitmap, fBitmap->Bounds(),
  121. rect, B_FILTER_BITMAP_BILINEAR);
  122. }
  123. // Draw contact name
  124. owner->MovePenTo(frame.left + 48, frame.top + fBaselineOffset);
  125. owner->SetHighColor(ui_color(B_CONTROL_TEXT_COLOR));
  126. owner->DrawString(Text());
  127. // Draw contact status string
  128. owner->MovePenTo(frame.left + 48, frame.top + fBaselineOffset +
  129. fBaselineOffset + 3);
  130. owner->SetHighColor(TintColor(owner->HighColor(), 2));
  131. if (fPersonalStatus.Length() == 0)
  132. owner->DrawString(UserStatusToString(fStatus));
  133. else
  134. owner->DrawString(fPersonalStatus);
  135. // Draw separator between items
  136. owner->SetHighColor(tint_color(lowColor, B_DARKEN_2_TINT));
  137. owner->StrokeLine(BPoint(frame.left, frame.bottom),
  138. BPoint(frame.right, frame.bottom));
  139. // Draw protocol bitmpap
  140. BBitmap* protocolBitmap = fContact->ProtocolBitmap();
  141. if (protocolBitmap != NULL) {
  142. BRect rect(frame.right - 19, frame.top + 2,
  143. frame.right - 2, frame.top + 19 );;
  144. owner->SetDrawingMode(B_OP_ALPHA);
  145. owner->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
  146. owner->DrawBitmap(protocolBitmap, protocolBitmap->Bounds(),
  147. rect, B_FILTER_BITMAP_BILINEAR);
  148. }
  149. owner->SetHighColor(highColor);
  150. owner->SetLowColor(lowColor);
  151. }
  152. void
  153. RosterItem::Update(BView* owner, const BFont* font)
  154. {
  155. font_height fheight;
  156. font->GetHeight(&fheight);
  157. fBaselineOffset = 2 + ceilf(fheight.ascent + fheight.leading / 2);
  158. SetHeight((ceilf(fheight.ascent) + ceilf(fheight.descent) +
  159. ceilf(fheight.leading) + 4 ) * 2);
  160. }
  161. void
  162. RosterItem::SetStatus(UserStatus status)
  163. {
  164. if (fStatus != status)
  165. fStatus = status;
  166. }