| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2023 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include <gui/layer_tooltip.h> |
| 14 | |
| 15 | using namespace BoCA; |
| 16 | |
| 17 | freac::LayerTooltip::LayerTooltip(const Track &iTrack) : Layer() |
| 18 | { |
| 19 | artwork = NIL; |
| 20 | text = NIL; |
| 21 | |
| 22 | UpdateFromTrack(iTrack); |
| 23 | } |
| 24 | |
| 25 | freac::LayerTooltip::~LayerTooltip() |
| 26 | { |
| 27 | if (artwork != NIL) DeleteObject(artwork); |
| 28 | if (text != NIL) DeleteObject(text); |
| 29 | } |
| 30 | |
| 31 | Int freac::LayerTooltip::Show() |
| 32 | { |
| 33 | /* Create and add artwork thumbnail. |
| 34 | */ |
| 35 | if (track.pictures.Length() > 0 && artwork == NIL) |
| 36 | { |
| 37 | Bitmap bitmap = track.pictures.GetFirst().GetBitmap(); |
| 38 | |
| 39 | /* Scale bitmap to max 40x40 pixels. |
| 40 | */ |
| 41 | Size bmpSize = bitmap.GetSize(); |
| 42 | Bitmap bmpScaled; |
| 43 | |
| 44 | if (float(bmpSize.cx) / 40 >= float(bmpSize.cy) / 40 && bmpSize.cx > 40) bmpScaled = bitmap.Scale(bmpSize * (float(40) / bmpSize.cx)); |
| 45 | else if (float(bmpSize.cy) / 40 >= float(bmpSize.cx) / 40 && bmpSize.cy > 40) bmpScaled = bitmap.Scale(bmpSize * (float(40) / bmpSize.cy)); |
| 46 | |
| 47 | /* Create and add image widget. |
| 48 | */ |
| 49 | artwork = new Image(bmpScaled, Point(4, 4), Size(40, 40)); |
| 50 | |
| 51 | Add(artwork); |
| 52 | } |
| 53 | |
| 54 | /* Create and add tooltip text. |
| 55 | */ |
| 56 | if (text == NIL) |
| 57 | { |
| 58 | text = new Text(GetTrackInfo(track), Point(3 + (track.pictures.Length() > 0 ? 44 : 0), 3)); |
| 59 | |
| 60 | Font font = text->GetFont(); |
| 61 | |
| 62 | font.SetColor(Setup::TooltipTextColor); |
| 63 | |
| 64 | text->SetFont(font); |
| 65 | |
| 66 | Add(text); |
| 67 | |
| 68 | /* Set tooltip size. |
| 69 | */ |
| 70 | SetSize(Size(text->GetUnscaledTextWidth() + 7 + (track.pictures.Length() > 0 ? 44 : 0), text->GetUnscaledTextHeight() + 7)); |
| 71 | } |
| 72 | |
| 73 | return Layer::Show(); |
| 74 | } |
| 75 | |
| 76 | Void freac::LayerTooltip::UpdateFromTrack(const Track &nTrack) |
| 77 | { |
| 78 | /* Clear artwork thumbnail if different. |
| 79 | */ |
| 80 | if (artwork != NIL && nTrack.pictures.GetFirst() != track.pictures.GetFirst()) |
| 81 | { |
| 82 | DeleteObject(artwork); |
| 83 | |
| 84 | artwork = NIL; |
| 85 | } |
| 86 | |
| 87 | /* Create and add tooltip text. |
| 88 | */ |
| 89 | if (text != NIL) |
| 90 | { |
| 91 | DeleteObject(text); |
| 92 | |
| 93 | text = NIL; |
| 94 | } |
| 95 | |
| 96 | /* Update track info. |
| 97 | */ |
| 98 | track = nTrack; |
| 99 | } |
| 100 | |
| 101 | String freac::LayerTooltip::GetTrackInfo(const Track &track) |
| 102 | { |
| 103 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 104 | |
| 105 | i18n->SetContext("Joblist"); |
| 106 | |
| 107 | const Format &format = track.GetFormat(); |
| 108 | const Info &info = track.GetInfo(); |
| 109 | |
| 110 | String tooltip = String(i18n->AddColon(i18n->TranslateString("File"))).Append(" ").Append(track.fileName.Contains("://") ? track.fileName : File(track.fileName).GetFileName()).Append("\n"). |
| 111 | Append(i18n->AddColon(i18n->TranslateString("Size"))).Append(" ").Append(i18n->TranslateString("%1 bytes", "Technical").Replace("%1", track.GetFileSizeString())).Append("\n"). |
| 112 | Append(i18n->AddColon(i18n->TranslateString("Artist"))).Append(" ").Append(info.artist.Length() > 0 ? info.artist : i18n->TranslateString("unknown artist")).Append("\n"). |
| 113 | Append(i18n->AddColon(i18n->TranslateString("Title"))).Append(" ").Append(info.title.Length() > 0 ? info.title : i18n->TranslateString("unknown title")).Append("\n"). |
| 114 | Append(track.length > 0 || track.approxLength > 0 ? i18n->AddColon(i18n->TranslateString("Length")).Append(" ").Append(i18n->TranslateString("%1 min", "Technical").Replace("%1", track.GetLengthString())).Append("\n") : String()). |
| 115 | Append(track.length > 0 ? i18n->AddColon(i18n->TranslateString("Number of samples")).Append(" ").Append(S::I18n::Number::GetLocalizedNumberString(track.length)).Append("\n") : String()). |
| 116 | Append(i18n->AddColon(i18n->TranslateString("Sampling rate"))).Append(" ").Append(i18n->TranslateString("%1 Hz", "Technical").Replace("%1", S::I18n::Number::GetLocalizedNumberString(format.rate))).Append("\n"). |
| 117 | Append(i18n->AddColon(i18n->TranslateString("Sample resolution"))).Append(" ").Append(i18n->TranslateString("%1 bit", "Technical").Replace("%1", String::FromInt(format.bits))).Append("\n"). |
| 118 | Append(i18n->AddColon(i18n->TranslateString("Channels"))).Append(" ").Append(format.channels > 2 ? (format.channels != 4 && format.channels != 5 && format.channels <= 8 ? String::FromInt(format.channels - 1).Append(".1") : String::FromInt(format.channels)) : (format.channels == 1 ? i18n->TranslateString("Mono") : i18n->TranslateString("Stereo"))); |
| 119 | |
| 120 | if (format.rate > 0) |
| 121 | { |
| 122 | static wchar_t sign[2] = { 0x2248, 0 }; |
| 123 | |
| 124 | if (track.length > 0) tooltip.Append("\n").Append(i18n->AddColon(i18n->TranslateString("Bitrate"))).Append(" ").Append(i18n->TranslateString("%1 kbps", "Technical").Replace("%1", String::FromInt((Int) Math::Round(Float(track.fileSize) / (Float(track.length) / format.rate) * 8.0 / 1000.0)))); |
| 125 | else if (track.approxLength > 0) tooltip.Append("\n").Append(i18n->AddColon(i18n->TranslateString("Bitrate"))).Append(" ").Append(sign).Append(" ").Append(i18n->TranslateString("%1 kbps", "Technical").Replace("%1", String::FromInt((Int) Math::Round(Float(track.fileSize) / (Float(track.approxLength) / format.rate) * 8.0 / 1000.0)))); |
| 126 | } |
| 127 | |
| 128 | return tooltip; |
| 129 | } |