StatusView.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * The contents of this file are subject to the Mozilla Public
  3. * License Version 1.1 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy of
  5. * the License at http://www.mozilla.org/MPL/
  6. *
  7. * Software distributed under the License is distributed on an "AS
  8. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9. * implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. * The Original Code is Vision.
  13. *
  14. * The Initial Developer of the Original Code is The Vision Team.
  15. * Portions created by The Vision Team are
  16. * Copyright (C) 1999, 2000, 2001 The Vision Team. All Rights
  17. * Reserved.
  18. *
  19. * Contributor(s): Wade Majors <wade@ezri.org>
  20. * Rene Gollent
  21. * Todd Lair
  22. * Andrew Bazan
  23. * Jamie Wilkinson
  24. */
  25. #ifdef __HAIKU__
  26. # include <ControlLook.h>
  27. #endif
  28. #include <MenuItem.h>
  29. #include <PopUpMenu.h>
  30. #include "StatusView.h"
  31. #include "URLCrunch.h"
  32. #include "Vision.h"
  33. StatusView::StatusView (BRect frame)
  34. : BView (
  35. BRect (frame.left,
  36. frame.bottom - STATUS_HEIGHT,
  37. frame.right,
  38. frame.bottom),
  39. "status",
  40. B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM,
  41. B_WILL_DRAW)
  42. {
  43. BFont font (be_plain_font);
  44. font_height fh;
  45. font.GetHeight (&fh);
  46. while (fh.ascent + fh.leading > STATUS_HEIGHT - 2)
  47. {
  48. font.SetSize (font.Size() - 2);
  49. font.GetHeight (&fh);
  50. }
  51. SetFont (&font);
  52. SetLowColor (ui_color (B_PANEL_BACKGROUND_COLOR));
  53. #ifdef __HAIKU__
  54. SetViewColor (B_TRANSPARENT_COLOR);
  55. #else
  56. SetViewColor (LowColor());
  57. #endif
  58. }
  59. StatusView::~StatusView (void)
  60. {
  61. while (!items.IsEmpty())
  62. {
  63. StatusItem *item ((StatusItem *)items.RemoveItem ((int32)0));
  64. delete item;
  65. }
  66. }
  67. void
  68. StatusView::AddItem (StatusItem *item, bool erase)
  69. {
  70. StatusItem *last ((StatusItem *)items.LastItem());
  71. float width (3.0);
  72. if (last)
  73. width = last->frame.right + 8.0;
  74. if (item->label.Length())
  75. width += StringWidth (item->label.String()) + 3;
  76. item->frame.top = 2.0;
  77. item->frame.bottom = Bounds().Height();
  78. item->frame.left = width;
  79. item->frame.right = width + StringWidth (item->value.String());
  80. if (erase)
  81. item->value = "";
  82. items.AddItem (item);
  83. }
  84. void
  85. StatusView::MouseDown(BPoint point)
  86. {
  87. StatusItem *item (NULL);
  88. for (int32 i = 0; i < items.CountItems(); i++)
  89. {
  90. item = (StatusItem *)items.ItemAt(i);
  91. if (item != NULL)
  92. {
  93. if (point.x >= item->frame.left && point.x < item->frame.right)
  94. {
  95. item->GeneratePopUp(ConvertToScreen(point), ConvertToScreen(item->frame));
  96. break;
  97. }
  98. }
  99. }
  100. BView::MouseDown(point);
  101. }
  102. void
  103. StatusView::Clear (void)
  104. {
  105. int32 i,
  106. all (items.CountItems());
  107. for (i = 0; i <= all; i++)
  108. {
  109. StatusItem *item ((StatusItem *)items.RemoveItem ((int32)0));
  110. delete item;
  111. }
  112. Invalidate();
  113. }
  114. StatusItem *
  115. StatusView::ItemAt (int32 which) const
  116. {
  117. return (StatusItem *)items.ItemAt (which);
  118. }
  119. void
  120. StatusView::SetItemValue (int32 which, const char *value, bool redraw)
  121. {
  122. StatusItem *item (ItemAt (which));
  123. StatusItem *nextitem;
  124. bool completeInvalidate (false);
  125. if (item)
  126. {
  127. item->value = value;
  128. if (item->frame.left + StringWidth(value) != item->frame.right)
  129. completeInvalidate = true;
  130. item->frame.right = item->frame.left + StringWidth(value);
  131. for (int32 i = which+1; (nextitem = ItemAt(i)) != NULL ; i++)
  132. {
  133. nextitem->frame.left = item->frame.right + 8.0 + StringWidth(nextitem->label.String());
  134. nextitem->frame.right = nextitem->frame.left + StringWidth(nextitem->value.String());
  135. item = nextitem;
  136. }
  137. if (redraw)
  138. {
  139. Invalidate(BRect (ItemAt (which)->frame.left, item->frame.top,
  140. (completeInvalidate) ? Bounds().right : item->frame.right, item->frame.bottom));
  141. }
  142. }
  143. }
  144. void
  145. StatusView::Draw (BRect update)
  146. {
  147. SetDrawingMode (B_OP_COPY);
  148. SetHighColor (tint_color(LowColor(), B_DARKEN_2_TINT));
  149. StrokeLine (BPoint (update.left, Bounds().top),
  150. BPoint (update.right, Bounds().top));
  151. #ifdef __HAIKU__
  152. BRect rect(Bounds());
  153. rect.top++;
  154. be_control_look->DrawMenuBarBackground(this, rect, update, LowColor());
  155. #else
  156. SetHighColor (255, 255, 255, 255);
  157. StrokeLine (BPoint (update.left, Bounds().top + 1),
  158. BPoint (update.right, Bounds().top + 1));
  159. #endif
  160. float width (5.0);
  161. font_height fh;
  162. GetFontHeight (&fh);
  163. SetDrawingMode (B_OP_OVER);
  164. SetHighColor (tint_color(LowColor(), 1.7));
  165. for (int32 i = 0; i < items.CountItems(); ++i)
  166. {
  167. if (i)
  168. {
  169. DrawSplit (width += 3);
  170. width += 5;
  171. }
  172. StatusItem *item (ItemAt (i));
  173. if (item->label.Length())
  174. {
  175. DrawString (item->label.String(),
  176. BPoint (width, fh.ascent + fh.leading + 2));
  177. }
  178. if (item->alignment == STATUS_ALIGN_RIGHT)
  179. width = item->frame.right - StringWidth (item->value.String());
  180. else
  181. width = item->frame.left;
  182. DrawString (item->value.String(),
  183. BPoint (width, fh.ascent + fh.leading + 2));
  184. width = item->frame.right;
  185. }
  186. }
  187. void
  188. StatusView::DrawSplit (float x)
  189. {
  190. BRect bounds (Bounds());
  191. PushState();
  192. #ifdef __HAIKU__
  193. SetDrawingMode (B_OP_ALPHA);
  194. SetHighColor (0, 0, 0, 40);
  195. StrokeLine (BPoint (x, bounds.top + 3.0),
  196. BPoint (x, bounds.bottom - 2.0));
  197. SetHighColor (255, 255, 255, 80);
  198. StrokeLine (BPoint (x + 1, bounds.top + 3.0),
  199. BPoint (x + 1, bounds.bottom - 2.0));
  200. #else
  201. SetDrawingMode (B_OP_COPY);
  202. SetHighColor (131, 131, 131, 255);
  203. StrokeLine (BPoint (x, bounds.top + 2.0),
  204. BPoint (x, bounds.bottom));
  205. SetHighColor (255, 255, 255, 255);
  206. StrokeLine (BPoint (x + 1, bounds.top + 2.0),
  207. BPoint (x + 1, bounds.bottom));
  208. #endif
  209. PopState();
  210. }
  211. StatusItem::StatusItem (
  212. const char *label_,
  213. const char *value_,
  214. int32 alignment_)
  215. {
  216. label = label_ ? label_ : "";
  217. value = value_ ? value_ : "";
  218. alignment = alignment_;
  219. }
  220. StatusItem::~StatusItem (void)
  221. {
  222. }
  223. void
  224. StatusItem::GeneratePopUp (BPoint point, BRect openrect)
  225. {
  226. BString str (value);
  227. str.Append(" ");
  228. BString url;
  229. URLCrunch crunch (str.String(), str.Length());
  230. BPopUpMenu *menu = new BPopUpMenu("URLs");
  231. BMessage msg (M_LOAD_URL);
  232. BMessage *allocmsg (NULL);
  233. BMenuItem *item (NULL);
  234. while (crunch.Crunch(&url) != B_ERROR)
  235. {
  236. allocmsg = new BMessage (msg);
  237. allocmsg->AddString("url", url.String());
  238. item = new BMenuItem(url.String(), allocmsg);
  239. menu->AddItem(item);
  240. allocmsg = NULL;
  241. }
  242. if (menu->CountItems() > 0)
  243. {
  244. menu->SetTargetForItems(be_app);
  245. menu->SetAsyncAutoDestruct(true);
  246. menu->Go(point, true, true, openrect, true);
  247. }
  248. else
  249. {
  250. delete menu;
  251. }
  252. }