IconMenu.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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): Rene Gollent
  20. * Alan Ellis <alan@cgsoftware.org>
  21. */
  22. //------------------------------------------------------------------------------
  23. // IconMenu.cpp
  24. //------------------------------------------------------------------------------
  25. // A menu item implementation that displays an icon as its label.
  26. //
  27. // IconMenu implementation Copyright (C) 1998 Tyler Riti <fizzboy@mail.utexas.edu>
  28. // Based on code Copyright (C) 1997 Jens Kilian
  29. // This code is free to use in any way so long as the credits above remain intact.
  30. // This code carries no warranties or guarantees of any kind. Use at your own risk.
  31. //------------------------------------------------------------------------------
  32. //------------------------------------------------------------------------------
  33. // I N C L U D E S
  34. //------------------------------------------------------------------------------
  35. #include <storage/AppFileInfo.h>
  36. #include <interface/Bitmap.h>
  37. #include <app/Application.h>
  38. #include <app/Roster.h>
  39. #include "IconMenu.h"
  40. //------------------------------------------------------------------------------
  41. // I M P L E M E N T A T I O N
  42. //------------------------------------------------------------------------------
  43. #ifdef __HAIKU__
  44. const color_space kIconColorSpace = B_RGBA32;
  45. #else
  46. const color_space kIconColorSpace = B_CMAP8;
  47. #endif
  48. TIconMenu::TIconMenu(BBitmap* icon, BMenu* menu) :
  49. BMenuItem(menu),
  50. bounds(),
  51. iconLabel(NULL)
  52. {
  53. if (icon) {
  54. bounds = icon->Bounds();
  55. iconLabel = new BBitmap(bounds, kIconColorSpace);
  56. iconLabel->SetBits(icon->Bits(), icon->BitsLength(), 0, kIconColorSpace);
  57. }
  58. }
  59. TIconMenu::TIconMenu(BMenu* menu) :
  60. BMenuItem(menu),
  61. bounds(0.0, 0.0, 15.0, 15.0),
  62. iconLabel(NULL)
  63. {
  64. app_info info;
  65. if (be_app->GetAppInfo(&info) == B_NO_ERROR) {
  66. BFile appFile(&(info.ref), O_RDONLY);
  67. BAppFileInfo appFileInfo(&appFile);
  68. iconLabel = new BBitmap(bounds, kIconColorSpace);
  69. if (appFileInfo.GetIcon(iconLabel, B_MINI_ICON) != B_NO_ERROR) {
  70. delete iconLabel;
  71. iconLabel = NULL;
  72. }
  73. }
  74. }
  75. TIconMenu::~TIconMenu()
  76. {
  77. delete iconLabel;
  78. iconLabel = NULL;
  79. }
  80. void TIconMenu::GetContentSize(float* width, float* height)
  81. {
  82. if (iconLabel) {
  83. *width = bounds.Width();
  84. *height = bounds.Height();
  85. }
  86. else
  87. BMenuItem::GetContentSize(width, height);
  88. }
  89. void TIconMenu::DrawContent()
  90. {
  91. if (iconLabel) {
  92. Menu()->SetDrawingMode(B_OP_OVER);
  93. float width, height;
  94. Menu()->GetPreferredSize(&width, &height);
  95. BRect destBounds = bounds;
  96. destBounds.OffsetBy(8.0, ((height - bounds.Height()) * 0.5) - 1);
  97. // Scaling the icon is left as an exercise for the reader :)
  98. Menu()->DrawBitmap(iconLabel, bounds, destBounds);
  99. }
  100. else
  101. BMenuItem::DrawContent();
  102. }
  103. //-------------------------------------------------------------- IconMenu.cpp --