1 /* 2 * Copyright 2010-2011, Haiku, Inc. All Rights Reserved. 3 * Copyright 2008-2009, Pier Luigi Fiorini. All Rights Reserved. 4 * Copyright 2004-2008, Michael Davidson. All Rights Reserved. 5 * Copyright 2004-2007, Mikael Eiman. All Rights Reserved. 6 * Distributed under the terms of the MIT License. 7 * 8 * Authors: 9 * Michael Davidson, slaad@bong.com.au 10 * Mikael Eiman, mikael@eiman.tv 11 * Pier Luigi Fiorini, pierluigi.fiorini@gmail.com 12 * Stephan Aßmus <superstippi@gmx.de> 13 * Adrien Destugues <pulkomandy@pulkomandy.ath.cx> 14 */ 15 16 17 #include "NotificationView.h" 18 19 20 #include <Bitmap.h> 21 #include <ControlLook.h> 22 #include <GroupLayout.h> 23 #include <LayoutUtils.h> 24 #include <MessageRunner.h> 25 #include <Messenger.h> 26 #include <Notification.h> 27 #include <Path.h> 28 #include <PropertyInfo.h> 29 #include <Roster.h> 30 #include <StatusBar.h> 31 32 #include <Notifications.h> 33 34 #include "AppGroupView.h" 35 #include "NotificationWindow.h" 36 37 38 static const int kIconStripeWidth = 32; 39 40 property_info message_prop_list[] = { 41 { "type", {B_GET_PROPERTY, B_SET_PROPERTY, 0}, 42 {B_DIRECT_SPECIFIER, 0}, "get the notification type"}, 43 { "app", {B_GET_PROPERTY, B_SET_PROPERTY, 0}, 44 {B_DIRECT_SPECIFIER, 0}, "get notification's app"}, 45 { "title", {B_GET_PROPERTY, B_SET_PROPERTY, 0}, 46 {B_DIRECT_SPECIFIER, 0}, "get notification's title"}, 47 { "content", {B_GET_PROPERTY, B_SET_PROPERTY, 0}, 48 {B_DIRECT_SPECIFIER, 0}, "get notification's contents"}, 49 { "icon", {B_GET_PROPERTY, 0}, 50 {B_DIRECT_SPECIFIER, 0}, "get icon as an archived bitmap"}, 51 { "progress", {B_GET_PROPERTY, B_SET_PROPERTY, 0}, 52 {B_DIRECT_SPECIFIER, 0}, "get the progress (between 0.0 and 1.0)"}, 53 { NULL } 54 }; 55 56 57 NotificationView::NotificationView(NotificationWindow* win, 58 BNotification* notification, bigtime_t timeout) 59 : 60 BView("NotificationView", B_WILL_DRAW), 61 fParent(win), 62 fNotification(notification), 63 fTimeout(timeout), 64 fRunner(NULL), 65 fBitmap(NULL), 66 fCloseClicked(false) 67 { 68 if (fNotification->Icon() != NULL) 69 fBitmap = new BBitmap(fNotification->Icon()); 70 71 if (fTimeout <= 0) 72 fTimeout = fParent->Timeout() * 1000000; 73 74 BGroupLayout* layout = new BGroupLayout(B_VERTICAL); 75 SetLayout(layout); 76 77 SetViewUIColor(B_PANEL_BACKGROUND_COLOR); 78 SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 79 80 switch (fNotification->Type()) { 81 case B_IMPORTANT_NOTIFICATION: 82 fStripeColor = ui_color(B_CONTROL_HIGHLIGHT_COLOR); 83 break; 84 case B_ERROR_NOTIFICATION: 85 fStripeColor = ui_color(B_FAILURE_COLOR); 86 break; 87 case B_PROGRESS_NOTIFICATION: 88 { 89 BStatusBar* progress = new BStatusBar("progress"); 90 progress->SetBarHeight(12.0f); 91 progress->SetMaxValue(1.0f); 92 progress->Update(fNotification->Progress()); 93 94 BString label = ""; 95 label << (int)(fNotification->Progress() * 100) << " %"; 96 progress->SetTrailingText(label); 97 98 layout->AddView(progress); 99 } 100 // fall through. 101 case B_INFORMATION_NOTIFICATION: 102 fStripeColor = tint_color(ui_color(B_PANEL_BACKGROUND_COLOR), 103 B_DARKEN_1_TINT); 104 break; 105 } 106 } 107 108 109 NotificationView::~NotificationView() 110 { 111 delete fRunner; 112 delete fBitmap; 113 delete fNotification; 114 115 LineInfoList::iterator lIt; 116 for (lIt = fLines.begin(); lIt != fLines.end(); lIt++) 117 delete (*lIt); 118 } 119 120 121 void 122 NotificationView::AttachedToWindow() 123 { 124 SetText(); 125 126 BMessage msg(kRemoveView); 127 msg.AddPointer("view", this); 128 129 fRunner = new BMessageRunner(BMessenger(Parent()), &msg, fTimeout, 1); 130 } 131 132 133 void 134 NotificationView::MessageReceived(BMessage* msg) 135 { 136 switch (msg->what) { 137 case B_GET_PROPERTY: 138 { 139 BMessage specifier; 140 const char* property; 141 BMessage reply(B_REPLY); 142 bool msgOkay = true; 143 144 if (msg->FindMessage("specifiers", 0, &specifier) != B_OK) 145 msgOkay = false; 146 if (specifier.FindString("property", &property) != B_OK) 147 msgOkay = false; 148 149 if (msgOkay) { 150 if (strcmp(property, "type") == 0) 151 reply.AddInt32("result", fNotification->Type()); 152 153 if (strcmp(property, "group") == 0) 154 reply.AddString("result", fNotification->Group()); 155 156 if (strcmp(property, "title") == 0) 157 reply.AddString("result", fNotification->Title()); 158 159 if (strcmp(property, "content") == 0) 160 reply.AddString("result", fNotification->Content()); 161 162 if (strcmp(property, "progress") == 0) 163 reply.AddFloat("result", fNotification->Progress()); 164 165 if ((strcmp(property, "icon") == 0) && fBitmap) { 166 BMessage archive; 167 if (fBitmap->Archive(&archive) == B_OK) 168 reply.AddMessage("result", &archive); 169 } 170 171 reply.AddInt32("error", B_OK); 172 } else { 173 reply.what = B_MESSAGE_NOT_UNDERSTOOD; 174 reply.AddInt32("error", B_ERROR); 175 } 176 177 msg->SendReply(&reply); 178 break; 179 } 180 case B_SET_PROPERTY: 181 { 182 BMessage specifier; 183 const char* property; 184 BMessage reply(B_REPLY); 185 bool msgOkay = true; 186 187 if (msg->FindMessage("specifiers", 0, &specifier) != B_OK) 188 msgOkay = false; 189 if (specifier.FindString("property", &property) != B_OK) 190 msgOkay = false; 191 192 if (msgOkay) { 193 const char* value = NULL; 194 195 if (strcmp(property, "group") == 0) 196 if (msg->FindString("data", &value) == B_OK) 197 fNotification->SetGroup(value); 198 199 if (strcmp(property, "title") == 0) 200 if (msg->FindString("data", &value) == B_OK) 201 fNotification->SetTitle(value); 202 203 if (strcmp(property, "content") == 0) 204 if (msg->FindString("data", &value) == B_OK) 205 fNotification->SetContent(value); 206 207 if (strcmp(property, "icon") == 0) { 208 BMessage archive; 209 if (msg->FindMessage("data", &archive) == B_OK) { 210 delete fBitmap; 211 fBitmap = new BBitmap(&archive); 212 } 213 } 214 215 SetText(); 216 Invalidate(); 217 218 reply.AddInt32("error", B_OK); 219 } else { 220 reply.what = B_MESSAGE_NOT_UNDERSTOOD; 221 reply.AddInt32("error", B_ERROR); 222 } 223 224 msg->SendReply(&reply); 225 break; 226 } 227 default: 228 BView::MessageReceived(msg); 229 } 230 } 231 232 233 void 234 NotificationView::Draw(BRect updateRect) 235 { 236 BRect progRect; 237 238 SetDrawingMode(B_OP_ALPHA); 239 SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY); 240 241 // Icon size 242 float iconSize = (float)fParent->IconSize(); 243 244 BRect stripeRect = Bounds(); 245 stripeRect.right = kIconStripeWidth; 246 SetHighColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR), 247 B_DARKEN_1_TINT)); 248 FillRect(stripeRect); 249 250 SetHighColor(fStripeColor); 251 stripeRect.right = 2; 252 FillRect(stripeRect); 253 254 SetHighColor(ui_color(B_PANEL_TEXT_COLOR)); 255 // Rectangle for icon and overlay icon 256 BRect iconRect(0, 0, 0, 0); 257 258 // Draw icon 259 if (fBitmap) { 260 float ix = 18; 261 float iy = (Bounds().Height() - iconSize) / 4.0; 262 // Icon is vertically centered in view 263 264 if (fNotification->Type() == B_PROGRESS_NOTIFICATION) 265 { 266 // Move icon up by half progress bar height if it's present 267 iy -= (progRect.Height() + kEdgePadding); 268 } 269 270 iconRect.Set(ix, iy, ix + iconSize - 1.0, iy + iconSize - 1.0); 271 DrawBitmapAsync(fBitmap, fBitmap->Bounds(), iconRect); 272 } 273 274 // Draw content 275 LineInfoList::iterator lIt; 276 for (lIt = fLines.begin(); lIt != fLines.end(); lIt++) { 277 LineInfo *l = (*lIt); 278 279 SetFont(&l->font); 280 // Truncate the string. We have already line-wrapped the text but if 281 // there is a very long 'word' we can only truncate it. 282 TruncateString(&(l->text), B_TRUNCATE_END, 283 Bounds().Width() - l->location.x); 284 DrawString(l->text.String(), l->text.Length(), l->location); 285 } 286 287 AppGroupView* groupView = dynamic_cast<AppGroupView*>(Parent()); 288 if (groupView != NULL && groupView->ChildrenCount() > 1) 289 _DrawCloseButton(updateRect); 290 291 SetHighColor(tint_color(ViewColor(), B_DARKEN_1_TINT)); 292 BPoint left(Bounds().left, Bounds().top); 293 BPoint right(Bounds().right, Bounds().top); 294 StrokeLine(left, right); 295 296 Sync(); 297 } 298 299 300 void 301 NotificationView::_DrawCloseButton(const BRect& updateRect) 302 { 303 PushState(); 304 BRect closeRect = Bounds(); 305 306 closeRect.InsetBy(3 * kEdgePadding, 3 * kEdgePadding); 307 closeRect.left = closeRect.right - kCloseSize; 308 closeRect.bottom = closeRect.top + kCloseSize; 309 310 rgb_color base = ui_color(B_PANEL_BACKGROUND_COLOR); 311 float tint = B_DARKEN_2_TINT; 312 313 if (fCloseClicked) { 314 BRect buttonRect(closeRect.InsetByCopy(-4, -4)); 315 be_control_look->DrawButtonFrame(this, buttonRect, updateRect, 316 base, base, 317 BControlLook::B_ACTIVATED | BControlLook::B_BLEND_FRAME); 318 be_control_look->DrawButtonBackground(this, buttonRect, updateRect, 319 base, BControlLook::B_ACTIVATED); 320 tint *= 1.2; 321 closeRect.OffsetBy(1, 1); 322 } 323 324 base = tint_color(base, tint); 325 SetHighColor(base); 326 SetPenSize(2); 327 StrokeLine(closeRect.LeftTop(), closeRect.RightBottom()); 328 StrokeLine(closeRect.LeftBottom(), closeRect.RightTop()); 329 PopState(); 330 } 331 332 333 void 334 NotificationView::MouseDown(BPoint point) 335 { 336 int32 buttons; 337 Window()->CurrentMessage()->FindInt32("buttons", &buttons); 338 339 switch (buttons) { 340 case B_PRIMARY_MOUSE_BUTTON: 341 { 342 BRect closeRect = Bounds().InsetByCopy(2,2); 343 closeRect.left = closeRect.right - kCloseSize; 344 closeRect.bottom = closeRect.top + kCloseSize; 345 346 if (!closeRect.Contains(point)) { 347 entry_ref launchRef; 348 BString launchString; 349 BMessage argMsg(B_ARGV_RECEIVED); 350 BMessage refMsg(B_REFS_RECEIVED); 351 entry_ref appRef; 352 bool useArgv = false; 353 BList messages; 354 entry_ref ref; 355 356 if (fNotification->OnClickApp() != NULL 357 && be_roster->FindApp(fNotification->OnClickApp(), &appRef) 358 == B_OK) { 359 useArgv = true; 360 } 361 362 if (fNotification->OnClickFile() != NULL 363 && be_roster->FindApp( 364 (entry_ref*)fNotification->OnClickFile(), &appRef) 365 == B_OK) { 366 useArgv = true; 367 } 368 369 for (int32 i = 0; i < fNotification->CountOnClickRefs(); i++) 370 refMsg.AddRef("refs", fNotification->OnClickRefAt(i)); 371 messages.AddItem((void*)&refMsg); 372 373 if (useArgv) { 374 int32 argc = fNotification->CountOnClickArgs() + 1; 375 BString arg; 376 377 BPath p(&appRef); 378 argMsg.AddString("argv", p.Path()); 379 380 argMsg.AddInt32("argc", argc); 381 382 for (int32 i = 0; i < argc - 1; i++) { 383 argMsg.AddString("argv", 384 fNotification->OnClickArgAt(i)); 385 } 386 387 messages.AddItem((void*)&argMsg); 388 } 389 390 if (fNotification->OnClickApp() != NULL) 391 be_roster->Launch(fNotification->OnClickApp(), &messages); 392 else 393 be_roster->Launch(fNotification->OnClickFile(), &messages); 394 } else { 395 fCloseClicked = true; 396 } 397 398 // Remove the info view after a click 399 BMessage remove_msg(kRemoveView); 400 remove_msg.AddPointer("view", this); 401 402 BMessenger msgr(Parent()); 403 msgr.SendMessage(&remove_msg); 404 break; 405 } 406 } 407 } 408 409 410 BHandler* 411 NotificationView::ResolveSpecifier(BMessage* msg, int32 index, BMessage* spec, 412 int32 form, const char* prop) 413 { 414 BPropertyInfo prop_info(message_prop_list); 415 if (prop_info.FindMatch(msg, index, spec, form, prop) >= 0) { 416 msg->PopSpecifier(); 417 return this; 418 } 419 420 return BView::ResolveSpecifier(msg, index, spec, form, prop); 421 } 422 423 424 status_t 425 NotificationView::GetSupportedSuites(BMessage* msg) 426 { 427 msg->AddString("suites", "suite/x-vnd.Haiku-notification_server"); 428 BPropertyInfo prop_info(message_prop_list); 429 msg->AddFlat("messages", &prop_info); 430 return BView::GetSupportedSuites(msg); 431 } 432 433 434 void 435 NotificationView::SetText(float newMaxWidth) 436 { 437 if (newMaxWidth < 0 && Parent()) 438 newMaxWidth = Parent()->Bounds().IntegerWidth(); 439 if (newMaxWidth <= 0) 440 newMaxWidth = kDefaultWidth; 441 442 // Delete old lines 443 LineInfoList::iterator lIt; 444 for (lIt = fLines.begin(); lIt != fLines.end(); lIt++) 445 delete (*lIt); 446 fLines.clear(); 447 448 float iconRight = kIconStripeWidth; 449 if (fBitmap != NULL) 450 iconRight += fParent->IconSize(); 451 else 452 iconRight += 32; 453 454 font_height fh; 455 be_bold_font->GetHeight(&fh); 456 float fontHeight = ceilf(fh.leading) + ceilf(fh.descent) 457 + ceilf(fh.ascent); 458 float y = 2 * fontHeight; 459 460 // Title 461 LineInfo* titleLine = new LineInfo; 462 titleLine->text = fNotification->Title(); 463 titleLine->font = *be_bold_font; 464 465 titleLine->location = BPoint(iconRight + kEdgePadding, y); 466 467 fLines.push_front(titleLine); 468 y += fontHeight; 469 470 // Rest of text is rendered with be_plain_font. 471 be_plain_font->GetHeight(&fh); 472 fontHeight = ceilf(fh.leading) + ceilf(fh.descent) 473 + ceilf(fh.ascent); 474 475 // Split text into chunks between certain characters and compose the lines. 476 const char kSeparatorCharacters[] = " \n-\\"; 477 BString textBuffer = fNotification->Content(); 478 textBuffer.ReplaceAll("\t", " "); 479 const char* chunkStart = textBuffer.String(); 480 float maxWidth = newMaxWidth - kEdgePadding - iconRight; 481 LineInfo* line = NULL; 482 ssize_t length = textBuffer.Length(); 483 while (chunkStart - textBuffer.String() < length) { 484 size_t chunkLength = strcspn(chunkStart, kSeparatorCharacters) + 1; 485 486 // Start a new line if we didn't start one before 487 BString tempText; 488 if (line != NULL) 489 tempText.SetTo(line->text); 490 tempText.Append(chunkStart, chunkLength); 491 492 if (line == NULL || chunkStart[0] == '\n' 493 || StringWidth(tempText) > maxWidth) { 494 line = new LineInfo; 495 line->font = *be_plain_font; 496 line->location = BPoint(iconRight + kEdgePadding, y); 497 498 fLines.push_front(line); 499 y += fontHeight; 500 501 // Skip the eventual new-line character at the beginning of this chunk 502 if (chunkStart[0] == '\n') { 503 chunkStart++; 504 chunkLength--; 505 } 506 507 // Skip more new-line characters and move the line further down 508 while (chunkStart[0] == '\n') { 509 chunkStart++; 510 chunkLength--; 511 line->location.y += fontHeight; 512 y += fontHeight; 513 } 514 515 // Strip space at beginning of a new line 516 while (chunkStart[0] == ' ') { 517 chunkLength--; 518 chunkStart++; 519 } 520 } 521 522 if (chunkStart[0] == '\0') 523 break; 524 525 // Append the chunk to the current line, which was either a new 526 // line or the one from the previous iteration 527 line->text.Append(chunkStart, chunkLength); 528 529 chunkStart += chunkLength; 530 } 531 532 fHeight = y + (kEdgePadding * 2); 533 534 // Make sure icon fits 535 if (fBitmap != NULL) { 536 float minHeight = fBitmap->Bounds().Height() + 2 * kEdgePadding; 537 538 if (fHeight < minHeight) 539 fHeight = minHeight; 540 } 541 542 // Make sure the progress bar is below the text, and the window is big 543 // enough. 544 static_cast<BGroupLayout*>(GetLayout())->SetInsets(kIconStripeWidth + 8, 545 fHeight, 8, 8); 546 547 _CalculateSize(); 548 } 549 550 551 const char* 552 NotificationView::MessageID() const 553 { 554 return fNotification->MessageID(); 555 } 556 557 558 void 559 NotificationView::_CalculateSize() 560 { 561 float height = fHeight; 562 563 if (fNotification->Type() == B_PROGRESS_NOTIFICATION) { 564 font_height fh; 565 be_plain_font->GetHeight(&fh); 566 float fontHeight = fh.ascent + fh.descent + fh.leading; 567 height += 9 + (kSmallPadding * 2) + (kEdgePadding * 1) 568 + fontHeight * 2; 569 } 570 571 SetExplicitMinSize(BSize(0, height)); 572 SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, height)); 573 } 574