| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2021 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 <dialogs/format.h> |
| 14 | |
| 15 | #include <config.h> |
| 16 | |
| 17 | freac::DialogSelectFormat::DialogSelectFormat(const Array<BoCA::Format> &formats) |
| 18 | { |
| 19 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 20 | |
| 21 | i18n->SetContext("Dialogs::Format"); |
| 22 | |
| 23 | Rect workArea = S::System::Screen::GetActiveScreenWorkArea(); |
| 24 | |
| 25 | mainWnd = new Window(i18n->TranslateString("Select sample format"), workArea.GetPosition() + Point((workArea.GetSize().cx - 565) / 2, (workArea.GetSize().cy - 375) / 2), Size(450, 250)); |
| 26 | mainWnd->SetMinimumSize(Size(400, 220)); |
| 27 | mainWnd->SetRightToLeft(i18n->IsActiveLanguageRightToLeft()); |
| 28 | mainWnd->GetMainLayer()->onChangeSize.Connect(&DialogSelectFormat::OnChangeSize, this); |
| 29 | |
| 30 | mainWnd_titlebar = new Titlebar(TB_CLOSEBUTTON); |
| 31 | divbar = new Divider(39, OR_HORZ | OR_BOTTOM); |
| 32 | |
| 33 | button_confirm = new Button(i18n->TranslateString("Select"), Point(87, 29), Size()); |
| 34 | button_confirm->onAction.Connect(&DialogSelectFormat::OnConfirm, this); |
| 35 | button_confirm->SetOrientation(OR_LOWERRIGHT); |
| 36 | button_confirm->SetWidth(Math::Max(80, button_confirm->GetUnscaledTextWidth() + 14)); |
| 37 | button_confirm->SetX(7 + button_confirm->GetWidth()); |
| 38 | |
| 39 | button_cancel = new Button(i18n->TranslateString("Cancel"), Point(175, 29), Size()); |
| 40 | button_cancel->onAction.Connect(&DialogSelectFormat::OnCancel, this); |
| 41 | button_cancel->SetOrientation(OR_LOWERRIGHT); |
| 42 | button_cancel->SetX(95 + button_confirm->GetWidth()); |
| 43 | |
| 44 | Config *freacConfig = Config::Get(); |
| 45 | |
| 46 | text_formats = new Text(i18n->TranslateString("The selected tracks have different sample formats. Please select\nthe desired sample format of the output file:"), Point(7, 5)); |
| 47 | list_formats = new ListBox(Point(7, 12 + text_formats->GetUnscaledTextHeight()), Size(378, 90)); |
| 48 | |
| 49 | /* Add formats and find minimum number of channels in list. |
| 50 | */ |
| 51 | Int minChannels = 32768; |
| 52 | |
| 53 | foreach (const BoCA::Format &format, formats) |
| 54 | { |
| 55 | String formatString = String(i18n->TranslateString("%1 Hz", "Technical").Replace("%1", S::I18n::Number::GetLocalizedNumberString(format.rate))).Append(", "). |
| 56 | Append(i18n->TranslateString("%1 bit", "Technical").Replace("%1", String::FromInt(format.bits))).Append(format.fp ? String(" ").Append(i18n->TranslateString("float")) : String()).Append(", "). |
| 57 | 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"))); |
| 58 | |
| 59 | if (format.channels < minChannels) minChannels = format.channels; |
| 60 | |
| 61 | list_formats->AddEntry(formatString); |
| 62 | } |
| 63 | |
| 64 | /* Deactivate formats that would require upmixing and select first entry. |
| 65 | */ |
| 66 | Bool first = True; |
| 67 | |
| 68 | foreach (const BoCA::Format &format, formats) |
| 69 | { |
| 70 | if (format.channels > 2 && format.channels > minChannels) |
| 71 | { |
| 72 | list_formats->GetNthEntry(foreachindex)->Deactivate(); |
| 73 | |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | if (first) list_formats->SelectNthEntry(foreachindex); |
| 78 | |
| 79 | first = False; |
| 80 | } |
| 81 | |
| 82 | Add(mainWnd); |
| 83 | |
| 84 | mainWnd->Add(button_confirm); |
| 85 | mainWnd->Add(button_cancel); |
| 86 | |
| 87 | mainWnd->Add(text_formats); |
| 88 | mainWnd->Add(list_formats); |
| 89 | |
| 90 | mainWnd->Add(mainWnd_titlebar); |
| 91 | mainWnd->Add(divbar); |
| 92 | |
| 93 | mainWnd->SetFlags(WF_MODAL); |
| 94 | mainWnd->SetIcon(ImageLoader::Load(String(freacConfig->resourcesPath).Append("icons/freac.png"))); |
| 95 | } |
| 96 | |
| 97 | freac::DialogSelectFormat::~DialogSelectFormat() |
| 98 | { |
| 99 | DeleteObject(mainWnd_titlebar); |
| 100 | DeleteObject(mainWnd); |
| 101 | DeleteObject(divbar); |
| 102 | |
| 103 | DeleteObject(text_formats); |
| 104 | DeleteObject(list_formats); |
| 105 | |
| 106 | DeleteObject(button_confirm); |
| 107 | DeleteObject(button_cancel); |
| 108 | } |
| 109 | |
| 110 | const Error &freac::DialogSelectFormat::ShowDialog() |
| 111 | { |
| 112 | error = Error(); |
| 113 | |
| 114 | mainWnd->WaitUntilClosed(); |
| 115 | |
| 116 | return error; |
| 117 | } |
| 118 | |
| 119 | Int freac::DialogSelectFormat::GetSelectedEntryNumber() const |
| 120 | { |
| 121 | return list_formats->GetSelectedEntryNumber(); |
| 122 | } |
| 123 | |
| 124 | Void freac::DialogSelectFormat::OnChangeSize(const Size &nSize) |
| 125 | { |
| 126 | Rect clientRect = Rect(mainWnd->GetMainLayer()->GetPosition(), mainWnd->GetMainLayer()->GetSize()); |
| 127 | Size clientSize = Size(clientRect.right - clientRect.left, clientRect.bottom - clientRect.top); |
| 128 | |
| 129 | list_formats->SetSize(clientSize - Size(14, 59 + text_formats->GetUnscaledTextHeight())); |
| 130 | } |
| 131 | |
| 132 | Void freac::DialogSelectFormat::OnConfirm() |
| 133 | { |
| 134 | error = Success(); |
| 135 | |
| 136 | mainWnd->Close(); |
| 137 | } |
| 138 | |
| 139 | Void freac::DialogSelectFormat::OnCancel() |
| 140 | { |
| 141 | mainWnd->Close(); |
| 142 | } |