1 /* 2 * Copyright 2019-2020, Andrew Lindesay <apl@lindesay.co.nz>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 #include "LanguageMenuUtils.h" 6 7 #include <string.h> 8 9 #include <Application.h> 10 #include <MenuItem.h> 11 #include <Messenger.h> 12 13 #include "AppUtils.h" 14 #include "HaikuDepotConstants.h" 15 #include "Logger.h" 16 17 18 /*! This method will add the supplied languages to the menu. It will 19 add first the popular languages, followed by a separator and then 20 other less popular languages. 21 */ 22 23 /* static */ void 24 LanguageMenuUtils::AddLanguagesToMenu( 25 const LanguageModel* languagesModel, BMenu* menu) 26 { 27 if (languagesModel->CountSupportedLanguages() == 0) 28 HDINFO("there are no languages defined"); 29 30 int32 addedPopular = LanguageMenuUtils::_AddLanguagesToMenu( 31 languagesModel, menu, true); 32 33 if (addedPopular > 0) 34 menu->AddSeparatorItem(); 35 36 int32 addedNonPopular = LanguageMenuUtils::_AddLanguagesToMenu( 37 languagesModel, menu, false); 38 39 HDDEBUG("did add %" B_PRId32 " popular languages and %" B_PRId32 40 " non-popular languages to a menu", addedPopular, 41 addedNonPopular); 42 } 43 44 45 /* static */ void 46 LanguageMenuUtils::MarkLanguageInMenu( 47 const BString& languageCode, BMenu* menu) { 48 AppUtils::MarkItemWithCodeInMenuOrFirst(languageCode, menu); 49 } 50 51 52 /* static */ void 53 LanguageMenuUtils::_AddLanguageToMenu( 54 const BString& code, const BString& name, BMenu* menu) 55 { 56 BMessage* message = new BMessage(MSG_LANGUAGE_SELECTED); 57 message->AddString("code", code); 58 BMenuItem* item = new BMenuItem(name, message); 59 menu->AddItem(item); 60 } 61 62 63 /* static */ void 64 LanguageMenuUtils::_AddLanguageToMenu( 65 const LanguageRef& language, BMenu* menu) 66 { 67 BString name; 68 if (language->GetName(name) != B_OK || name.IsEmpty()) 69 name.SetTo("???"); 70 LanguageMenuUtils::_AddLanguageToMenu(language->Code(), name, menu); 71 } 72 73 74 /* static */ int32 75 LanguageMenuUtils::_AddLanguagesToMenu(const LanguageModel* languageModel, 76 BMenu* menu, bool isPopular) 77 { 78 int32 count = 0; 79 80 for (int32 i = 0; i < languageModel->CountSupportedLanguages(); i++) { 81 const LanguageRef language = languageModel->SupportedLanguageAt(i); 82 83 if (language->IsPopular() == isPopular) { 84 LanguageMenuUtils::_AddLanguageToMenu(language, menu); 85 count++; 86 } 87 } 88 89 return count; 90 } 91