1 /* 2 * Copyright 2019, 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 "HaikuDepotConstants.h" 14 #include "Logger.h" 15 16 17 /*! This method will add the supplied languages to the menu. It will 18 add first the popular languages, followed by a separator and then 19 other less popular languages. 20 */ 21 22 /* static */ void 23 LanguageMenuUtils::AddLanguagesToMenu( 24 const LanguageList& languages, BMenu* menu) 25 { 26 if (languages.IsEmpty() && Logger::IsInfoEnabled()) 27 printf("there are no languages defined\n"); 28 29 int32 addedPopular = LanguageMenuUtils::_AddLanguagesToMenu( 30 languages, menu, true); 31 32 if (addedPopular > 0) 33 menu->AddSeparatorItem(); 34 35 int32 addedNonPopular = LanguageMenuUtils::_AddLanguagesToMenu( 36 languages, menu, false); 37 38 if (Logger::IsDebugEnabled()) { 39 printf("did add %" B_PRId32 " popular languages and %" B_PRId32 40 " non-popular languages to a menu\n", addedPopular, 41 addedNonPopular); 42 } 43 } 44 45 46 /* static */ void 47 LanguageMenuUtils::MarkLanguageInMenu( 48 const BString& languageCode, BMenu* menu) { 49 if (menu->CountItems() == 0) { 50 debugger("menu contains no items; not able to set the " 51 "language"); 52 return; 53 } 54 55 int32 index = LanguageMenuUtils::_IndexOfLanguageInMenu( 56 languageCode, menu); 57 58 if (index == -1) { 59 printf("unable to find the language [%s] in the menu\n", 60 languageCode.String()); 61 menu->ItemAt(0)->SetMarked(true); 62 } 63 else 64 menu->ItemAt(index)->SetMarked(true); 65 } 66 67 68 /* static */ void 69 LanguageMenuUtils::_AddLanguageToMenu( 70 const BString& code, const BString& name, BMenu* menu) 71 { 72 BMessage* message = new BMessage(MSG_LANGUAGE_SELECTED); 73 message->AddString("code", code); 74 BMenuItem* item = new BMenuItem(name, message); 75 menu->AddItem(item); 76 } 77 78 79 /* static */ void 80 LanguageMenuUtils::_AddLanguageToMenu( 81 const LanguageRef& language, BMenu* menu) 82 { 83 BString name; 84 if (language->GetName(name) != B_OK || name.IsEmpty()) 85 name.SetTo("???"); 86 LanguageMenuUtils::_AddLanguageToMenu(language->Code(), name, menu); 87 } 88 89 90 /* static */ int32 91 LanguageMenuUtils::_AddLanguagesToMenu(const LanguageList& languages, 92 BMenu* menu, bool isPopular) 93 { 94 int32 count = 0; 95 96 for (int32 i = 0; i < languages.CountItems(); i++) { 97 const LanguageRef language = languages.ItemAtFast(i); 98 99 if (language->IsPopular() == isPopular) { 100 LanguageMenuUtils::_AddLanguageToMenu(language, menu); 101 count++; 102 } 103 } 104 105 return count; 106 } 107 108 109 /* static */ status_t 110 LanguageMenuUtils::_GetLanguageAtIndexInMenu(BMenu* menu, int32 index, 111 BString* result) 112 { 113 BMessage *itemMessage = menu->ItemAt(index)->Message(); 114 115 if (itemMessage == NULL) 116 return B_ERROR; 117 118 return itemMessage->FindString("code", result); 119 } 120 121 122 /* static */ int32 123 LanguageMenuUtils::_IndexOfLanguageInMenu( 124 const BString& languageCode, BMenu* menu) 125 { 126 BString itemLanguageCode; 127 for (int32 i = 0; i < menu->CountItems(); i++) { 128 if (_GetLanguageAtIndexInMenu( 129 menu, i, &itemLanguageCode) == B_OK) { 130 if (itemLanguageCode == languageCode) { 131 return i; 132 } 133 } 134 } 135 136 return -1; 137 }