void Message::Draw(BView *view, int32 frame) { if (view == NULL || view->Window() == NULL || !view->Window()->IsLocked()) return; BScreen screen(view->Window()); if (!screen.IsValid()) return; // Double-buffered drawing BBitmap buffer(view->Bounds(), screen.ColorSpace(), true); if (buffer.InitCheck() != B_OK) return; BView offscreen(view->Bounds(), NULL, 0, 0); buffer.AddChild(&offscreen); buffer.Lock(); // Set up the colors rgb_color base_color = {(uint8)(rand() % 25), (uint8)(rand() % 25), (uint8)(rand() % 25)}; offscreen.SetHighColor(base_color); offscreen.SetLowColor(tint_color(base_color, 0.815F)); offscreen.FillRect(offscreen.Bounds(), kCheckered); rgb_color colors[8] = { tint_color(base_color, B_LIGHTEN_1_TINT), tint_color(base_color, 0.795F), tint_color(base_color, 0.851F), tint_color(base_color, 0.926F), tint_color(base_color, 1.05F), tint_color(base_color, B_DARKEN_1_TINT), tint_color(base_color, B_DARKEN_2_TINT), tint_color(base_color, B_DARKEN_3_TINT), }; offscreen.SetDrawingMode(B_OP_OVER); // Set the basic font parameters, including random font family BFont font; offscreen.GetFont(&font); font.SetFace(B_BOLD_FACE); font.SetFamilyAndStyle(*(fFontFamilies.ItemAt(rand() % fFontFamilies.CountItems())), NULL); offscreen.SetFont(&font); // Get the message BString *message = get_message(); BString *origMessage = new BString(); message->CopyInto(*origMessage, 0, message->Length()); // Replace newlines and tabs with spaces message->ReplaceSet("\n\t", ' '); int height = (int) offscreen.Bounds().Height(); int width = (int) offscreen.Bounds().Width(); // From 14 to 22 iterations int32 iterations = (rand() % 8) + 14; for (int32 i = 0; i < iterations; i++) { // Randomly set font size and shear BFont font; offscreen.GetFont(&font); float fontSize = ((rand() % 320) + 42) * fScaleFactor; font.SetSize(fontSize); // Set the shear off 90 about 1/2 of the time if (rand() % 2 == 1) font.SetShear((float) ((rand() % 135) + (rand() % 45))); else font.SetShear(90.0); offscreen.SetFont(&font); // Randomly set drawing location int x = (rand() % width) - (rand() % width/((rand() % 8)+1)); int y = rand() % height; // Draw new text offscreen.SetHighColor(colors[rand() % 8]); int strLength = message->Length(); // See how wide this string is with the current font float strWidth = offscreen.StringWidth(message->String()); int drawingLength = (int) (strLength * (width / strWidth)); int start = 0; if (drawingLength >= strLength) drawingLength = strLength; else start = rand() % (strLength - drawingLength); char *toDraw = new char[drawingLength+1]; strncpy(toDraw, message->String()+start, drawingLength); toDraw[drawingLength] = 0; offscreen.DrawString(toDraw, BPoint(x, y)); delete[] toDraw; } // Now draw the full message in a nice translucent box, but only // if this isn't preview mode if (!fPreview) { BFont font(be_fixed_font); font.SetSize(14.0); offscreen.SetFont(&font); font_height fontHeight; font.GetHeight(&fontHeight); float lineHeight = fontHeight.ascent + fontHeight.descent + fontHeight.leading; BStringList lines; int longestLine = 0; int32 count = get_lines(origMessage, lines, &longestLine); float stringWidth = font.StringWidth(lines.StringAt(longestLine).String()); BRect box(0, 0, stringWidth + 20, (lineHeight * count) + 20); box.OffsetTo((width - box.Width()) / 2, height - box.Height() - 40); offscreen.SetDrawingMode(B_OP_ALPHA); base_color.alpha = 128; offscreen.SetHighColor(base_color); offscreen.FillRoundRect(box, 8, 8); offscreen.SetHighColor(205, 205, 205); BPoint start = box.LeftTop(); start.x += 10; start.y += 10 + fontHeight.ascent + fontHeight.leading; for (int i = 0; i < count; i++) { offscreen.DrawString(lines.StringAt(i).String(), start); start.y += lineHeight; } } delete origMessage; delete message; offscreen.Sync(); buffer.Unlock(); view->DrawBitmap(&buffer); buffer.RemoveChild(&offscreen); }
status_t CDDBDaemon::_WriteCDData(dev_t device, QueryResponseData* diskData, ReadResponseData* readResponse) { // Rename volume. BVolume volume(device); status_t result; status_t error = B_OK; BString name = diskData->artist << " - " << diskData->title; name.ReplaceSet("/", " "); if ((result = volume.SetName(name.String())) != B_OK) { printf("Can't set volume name.\n"); return result; } // Rename tracks and add relevant Audio attributes. BDirectory cddaRoot; volume.GetRootDirectory(&cddaRoot); BEntry entry; int index = 0; while (cddaRoot.GetNextEntry(&entry) == B_OK) { TrackData* data = (TrackData*)((readResponse->tracks).ItemAt(index)); // Update name. name = data->title; name.ReplaceSet("/", " "); if ((result = entry.Rename(name.String())) != B_OK) { printf("Failed renaming entry at index %d to \"%s\".\n", index, name.String()); error = result; // User can benefit from continuing through all tracks. // Report error later. } // Add relevant attributes. We consider an error here as non-fatal. BNode node(&entry); node.WriteAttr("Audio:Title", B_STRING_TYPE, 0, (data->title).String(), (data->title).Length()); node.WriteAttr("Audio:Album", B_STRING_TYPE, 0, (readResponse->title).String(), (readResponse->title).Length()); node.WriteAttr("Audio:Genre", B_STRING_TYPE, 0, (readResponse->genre).String(), (readResponse->genre).Length()); node.WriteAttr("Audio:Year", B_INT32_TYPE, 0, &(readResponse->year), sizeof(int32)); if (data->artist == "") { node.WriteAttr("Audio:Artist", B_STRING_TYPE, 0, (readResponse->artist).String(), (readResponse->artist).Length()); } else { node.WriteAttr("Audio:Artist", B_STRING_TYPE, 0, (data->artist).String(), (data->artist).Length()); } index++; } return error; }
status_t CDDBLookup::_WriteCDData(dev_t device, const QueryResponseData& diskData, const ReadResponseData& readResponse) { // Rename volume. BVolume volume(device); status_t error = B_OK; BString name = diskData.artist; name += " - "; name += diskData.title; name.ReplaceSet("/", " "); status_t result = volume.SetName(name.String()); if (result != B_OK) { printf("Can't set volume name.\n"); return result; } // Rename tracks and add relevant Audio attributes. BDirectory cddaRoot; volume.GetRootDirectory(&cddaRoot); BEntry entry; int index = 0; while (cddaRoot.GetNextEntry(&entry) == B_OK) { TrackData* track = readResponse.tracks.ItemAt(index); // Update name. int trackNum = index + 1; // index=0 is actually Track 1 name.SetToFormat("%02d %s.wav", trackNum, track->title.String()); name.ReplaceSet("/", " "); result = entry.Rename(name.String()); if (result != B_OK) { fprintf(stderr, "%s: Failed renaming entry at index %d to " "\"%s\".\n", kProgramName, index, name.String()); error = result; // User can benefit from continuing through all tracks. // Report error later. } // Add relevant attributes. We consider an error here as non-fatal. BNode node(&entry); node.WriteAttrString("Media:Title", &track->title); node.WriteAttrString("Audio:Album", &readResponse.title); if (readResponse.genre.Length() != 0) node.WriteAttrString("Media:Genre", &readResponse.genre); if (readResponse.year != 0) { node.WriteAttr("Media:Year", B_INT32_TYPE, 0, &readResponse.year, sizeof(int32)); } if (track->artist == "") node.WriteAttrString("Audio:Artist", &readResponse.artist); else node.WriteAttrString("Audio:Artist", &track->artist); index++; } return error; }