1 /* 2 3 IconMenuItem.cpp 4 5 ProcessController 6 (c) 2000, Georges-Edouard Berenger, All Rights Reserved. 7 Copyright (C) 2004 beunited.org 8 9 This library is free software; you can redistribute it and/or 10 modify it under the terms of the GNU Lesser General Public 11 License as published by the Free Software Foundation; either 12 version 2.1 of the License, or (at your option) any later version. 13 14 This library is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 Lesser General Public License for more details. 18 19 You should have received a copy of the GNU Lesser General Public 20 License along with this library; if not, write to the Free Software 21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 23 */ 24 25 #include "IconMenuItem.h" 26 #include <Application.h> 27 #include <NodeInfo.h> 28 #include <Bitmap.h> 29 #include <Roster.h> 30 #include <parsedate.h> 31 32 // -------------------------------------------------------------- 33 34 IconMenuItem::IconMenuItem(BBitmap* icon, const char* title, 35 BMessage* msg, bool drawText, bool purge) 36 :BMenuItem(title, msg), fIcon(icon), fDrawText(drawText), fPurge(purge) 37 38 { 39 if (!fIcon) 40 DefaultIcon(NULL); 41 } 42 43 // -------------------------------------------------------------- 44 45 IconMenuItem::IconMenuItem(BBitmap* icon, BMenu* menu, bool drawText, bool purge) 46 :BMenuItem(menu), fIcon(icon), fDrawText(drawText), fPurge(purge) 47 48 { 49 if (!fIcon) 50 DefaultIcon(NULL); 51 } 52 53 // -------------------------------------------------------------- 54 55 IconMenuItem::IconMenuItem(const char* mime, const char* title, BMessage* msg, bool drawText) 56 :BMenuItem(title, msg), fIcon(NULL), fDrawText(drawText) 57 58 { 59 DefaultIcon(mime); 60 } 61 62 // -------------------------------------------------------------- 63 IconMenuItem::~IconMenuItem() 64 { 65 if (fPurge && fIcon) 66 delete fIcon; 67 } 68 69 // -------------------------------------------------------------- 70 void IconMenuItem::DrawContent() 71 { 72 BPoint loc; 73 74 DrawIcon(); 75 if (fDrawText) { 76 loc = ContentLocation(); 77 loc.x += 20; 78 Menu()->MovePenTo(loc); 79 BMenuItem::DrawContent(); 80 } 81 } 82 83 // -------------------------------------------------------------- 84 void IconMenuItem::Highlight(bool hilited) 85 { 86 BMenuItem::Highlight(hilited); 87 DrawIcon(); 88 } 89 90 // -------------------------------------------------------------- 91 void IconMenuItem::DrawIcon() 92 { 93 BPoint loc; 94 95 loc = ContentLocation(); 96 BRect frame = Frame(); 97 loc.y = frame.top + (frame.bottom - frame.top - 15) / 2; 98 Menu()->SetDrawingMode(B_OP_OVER); 99 if (fIcon) 100 Menu()->DrawBitmap(fIcon, loc); 101 } 102 103 // -------------------------------------------------------------- 104 void IconMenuItem::GetContentSize(float* width, float* height) 105 { 106 BMenuItem::GetContentSize(width, height); 107 int limit = IconMenuItem::MinHeight(); 108 if (*height < limit) 109 *height = limit; 110 if (fDrawText) 111 *width += 20; 112 else 113 *width = 16; 114 } 115 116 // -------------------------------------------------------------- 117 void IconMenuItem::DefaultIcon(const char* mime) 118 { 119 BRect r(0, 0, 15, 15); 120 fIcon = new BBitmap(r, B_COLOR_8_BIT); 121 if (mime) { 122 BMimeType mimeType(mime); 123 if (mimeType.GetIcon(fIcon, B_MINI_ICON)!=B_OK) 124 fDrawText = true; 125 } else { 126 app_info info; 127 be_app->GetAppInfo(&info); 128 if (BNodeInfo::GetTrackerIcon(&info.ref, fIcon, B_MINI_ICON)!=B_OK) 129 fDrawText = true; 130 } 131 fPurge = true; 132 } 133 134 // -------------------------------------------------------------- 135 int IconMenuItem::MinHeight() 136 { 137 static int minheight = -1; 138 if (minheight < 0) 139 minheight = before_dano() ? 16 : 17; 140 return minheight; 141 } 142 143 // -------------------------------------------------------------- 144 bool before_dano() 145 { 146 static int old_version = -1; 147 if (old_version < 0) 148 { 149 system_info sys_info; 150 get_system_info(&sys_info); 151 time_t kernelTime = parsedate(sys_info.kernel_build_date, time(NULL)); 152 struct tm * date = gmtime(&kernelTime); 153 old_version = (date->tm_year < 101 || date->tm_year == 101 && date->tm_mon < 10); 154 } 155 return old_version; 156 } 157