Actually sort the translator menu used in ShowImage.
There were two problems with the last commit: * the list needed to be outside of the top-level loop. * BList was just broken for sorting translation_format pointers. I fixed this by moving the loop outside and converting the translation_formats to translator_info, which has the translator_id, since that is needed to create the menu item, and would otherwise be unavailable outside the loop. I tried to get this working with BList, but the sorting was completely broken, and converting to BObjectList made the code much, much better and worked great. Screw BList and casting, hurray templated BObjectList. Really fixes #6782.
This commit is contained in:
@@ -77,8 +77,10 @@ public:
|
|||||||
BTranslatorRoster* roster = NULL);
|
BTranslatorRoster* roster = NULL);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static int CompareTranslationFormatByName(const void* format1,
|
static translator_info* _BuildTranslatorInfo(const translator_id id,
|
||||||
const void* format2);
|
const translation_format* format);
|
||||||
|
static int _CompareTranslatorInfoByName(const translator_info* info1,
|
||||||
|
const translator_info* info2);
|
||||||
|
|
||||||
static color_space sBitmapSpace;
|
static color_space sBitmapSpace;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -17,9 +17,9 @@
|
|||||||
#include <CharacterSetRoster.h>
|
#include <CharacterSetRoster.h>
|
||||||
#include <Entry.h>
|
#include <Entry.h>
|
||||||
#include <File.h>
|
#include <File.h>
|
||||||
#include <List.h>
|
|
||||||
#include <MenuItem.h>
|
#include <MenuItem.h>
|
||||||
#include <NodeInfo.h>
|
#include <NodeInfo.h>
|
||||||
|
#include <ObjectList.h>
|
||||||
#include <Path.h>
|
#include <Path.h>
|
||||||
#include <Resources.h>
|
#include <Resources.h>
|
||||||
#include <Roster.h>
|
#include <Roster.h>
|
||||||
@@ -894,6 +894,8 @@ BTranslationUtils::AddTranslationItems(BMenu *intoMenu, uint32 fromType,
|
|||||||
if (err < B_OK)
|
if (err < B_OK)
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
|
BObjectList<translator_info> infoList;
|
||||||
|
|
||||||
for (int tix = 0; tix < count; tix++) {
|
for (int tix = 0; tix < count; tix++) {
|
||||||
const translation_format *formats = NULL;
|
const translation_format *formats = NULL;
|
||||||
int32 numFormats = 0;
|
int32 numFormats = 0;
|
||||||
@@ -911,34 +913,35 @@ BTranslationUtils::AddTranslationItems(BMenu *intoMenu, uint32 fromType,
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Get supported output formats
|
// Get supported output formats
|
||||||
BList formatList;
|
|
||||||
err = roster->GetOutputFormats(ids[tix], &formats, &numFormats);
|
err = roster->GetOutputFormats(ids[tix], &formats, &numFormats);
|
||||||
if (err == B_OK) {
|
if (err == B_OK) {
|
||||||
for (int oix = 0; oix < numFormats; oix++) {
|
for (int oix = 0; oix < numFormats; oix++) {
|
||||||
if (formats[oix].type != fromType) {
|
if (formats[oix].type != fromType) {
|
||||||
formatList.AddItem(const_cast<translation_format*>(
|
infoList.AddItem(_BuildTranslatorInfo(ids[tix],
|
||||||
&formats[oix]));
|
const_cast<translation_format*>(&formats[oix])));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort alphabetically by name
|
// Sort alphabetically by name
|
||||||
formatList.SortItems(&CompareTranslationFormatByName);
|
infoList.SortItems(&_CompareTranslatorInfoByName);
|
||||||
|
|
||||||
// Now add the menu items
|
// Now add the menu items
|
||||||
for (int i = 0; i < formatList.CountItems(); i++) {
|
for (int i = 0; i < infoList.CountItems(); i++) {
|
||||||
translation_format* format = static_cast<translation_format*>(
|
translator_info* info = infoList.ItemAt(i);
|
||||||
formatList.ItemAt(i));
|
|
||||||
|
|
||||||
BMessage *itemmsg;
|
BMessage *itemmsg;
|
||||||
if (kModel)
|
if (kModel)
|
||||||
itemmsg = new BMessage(*kModel);
|
itemmsg = new BMessage(*kModel);
|
||||||
else
|
else
|
||||||
itemmsg = new BMessage(B_TRANSLATION_MENU);
|
itemmsg = new BMessage(B_TRANSLATION_MENU);
|
||||||
itemmsg->AddInt32(kTranslatorIdName, ids[tix]);
|
itemmsg->AddInt32(kTranslatorIdName, info->translator);
|
||||||
itemmsg->AddInt32(kTranslatorTypeName, format->type);
|
itemmsg->AddInt32(kTranslatorTypeName, info->type);
|
||||||
intoMenu->AddItem(new BMenuItem(format->name, itemmsg));
|
intoMenu->AddItem(new BMenuItem(info->name, itemmsg));
|
||||||
}
|
|
||||||
|
// Delete object created in _BuildTranslatorInfo
|
||||||
|
delete info;
|
||||||
}
|
}
|
||||||
|
|
||||||
delete[] ids;
|
delete[] ids;
|
||||||
@@ -946,9 +949,26 @@ BTranslationUtils::AddTranslationItems(BMenu *intoMenu, uint32 fromType,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
translator_info*
|
||||||
BTranslationUtils::CompareTranslationFormatByName(const void* format1, const void* format2)
|
BTranslationUtils::_BuildTranslatorInfo(const translator_id id, const translation_format* format)
|
||||||
{
|
{
|
||||||
return strcasecmp(static_cast<const translation_format*>(format1)->name,
|
// Caller must delete
|
||||||
static_cast<const translation_format*>(format2)->name);
|
translator_info* info = new translator_info;
|
||||||
|
|
||||||
|
info->translator = id;
|
||||||
|
info->type = format->type;
|
||||||
|
info->group = format->group;
|
||||||
|
info->quality = format->quality;
|
||||||
|
info->capability = format->capability;
|
||||||
|
strlcpy(info->name, format->name, sizeof(info->name));
|
||||||
|
strlcpy(info->MIME, format->MIME, sizeof(info->MIME));
|
||||||
|
|
||||||
|
return info;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
BTranslationUtils::_CompareTranslatorInfoByName(const translator_info* info1, const translator_info* info2)
|
||||||
|
{
|
||||||
|
return strcasecmp(info1->name, info2->name);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user