From adfe152ee269d1d6153827ca38a174f217d2634d Mon Sep 17 00:00:00 2001 From: Ryan Leavengood Date: Mon, 6 Aug 2012 02:08:37 -0400 Subject: [PATCH] Sort the translation formats in AddTranslationItems by name. This is used by ShowImage and CodyCam to create a list of image formats which a file can be saved as. Tracker sorts the image MIME types used in the Find window by name, so this makes these Save As menus match that (minus the icons which I think are superfluous.) Fixes #6782. If the use of BList is no longer recommended, I welcome better suggestions for sorting which will work in both GCC2 and GCC4. But this works ;) --- headers/os/translation/TranslationUtils.h | 3 ++ src/kits/translation/TranslationUtils.cpp | 45 +++++++++++++++++------ 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/headers/os/translation/TranslationUtils.h b/headers/os/translation/TranslationUtils.h index f1a2480457..66c927af07 100644 --- a/headers/os/translation/TranslationUtils.h +++ b/headers/os/translation/TranslationUtils.h @@ -77,6 +77,9 @@ public: BTranslatorRoster* roster = NULL); private: + static int CompareTranslationFormatByName(const void* format1, + const void* format2); + static color_space sBitmapSpace; }; diff --git a/src/kits/translation/TranslationUtils.cpp b/src/kits/translation/TranslationUtils.cpp index dd8118c754..1d40796214 100644 --- a/src/kits/translation/TranslationUtils.cpp +++ b/src/kits/translation/TranslationUtils.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -834,6 +835,7 @@ BTranslationUtils::GetDefaultSettings(const char *kTranslatorName, return pMessage; } + // --------------------------------------------------------------- // AddTranslationItems // @@ -908,24 +910,45 @@ BTranslationUtils::AddTranslationItems(BMenu *intoMenu, uint32 fromType, if (!ok) continue; + // Get supported output formats + BList formatList; err = roster->GetOutputFormats(ids[tix], &formats, &numFormats); if (err == B_OK) { - for (int oix = 0; oix < numFormats; oix++) { - if (formats[oix].type != fromType) { - BMessage *itemmsg; - if (kModel) - itemmsg = new BMessage(*kModel); - else - itemmsg = new BMessage(B_TRANSLATION_MENU); - itemmsg->AddInt32(kTranslatorIdName, ids[tix]); - itemmsg->AddInt32(kTranslatorTypeName, formats[oix].type); - intoMenu->AddItem( - new BMenuItem(formats[oix].name, itemmsg)); + for (int oix = 0; oix < numFormats; oix++) { + if (formats[oix].type != fromType) { + formatList.AddItem(const_cast( + &formats[oix])); } } } + + // Sort alphabetically by name + formatList.SortItems(&CompareTranslationFormatByName); + + // Now add the menu items + for (int i = 0; i < formatList.CountItems(); i++) { + translation_format* format = static_cast( + formatList.ItemAt(i)); + + BMessage *itemmsg; + if (kModel) + itemmsg = new BMessage(*kModel); + else + itemmsg = new BMessage(B_TRANSLATION_MENU); + itemmsg->AddInt32(kTranslatorIdName, ids[tix]); + itemmsg->AddInt32(kTranslatorTypeName, format->type); + intoMenu->AddItem(new BMenuItem(format->name, itemmsg)); + } } delete[] ids; return B_OK; } + + +int +BTranslationUtils::CompareTranslationFormatByName(const void* format1, const void* format2) +{ + return strcasecmp(static_cast(format1)->name, + static_cast(format2)->name); +}