Make all AccountsMenu items BitmapMenuItems
Now all account menus will have the pretty protocol icons to go with them. :) The icons are scaled to text-size so that they might be displayed in a corresponding MenuButton, or maybe BMenuField at some point, and put into the ImageCache. The menu items do not have ownership of the bitmaps.
This commit is contained in:
parent
71917d4a4d
commit
47ca0f4169
|
@ -8,9 +8,10 @@
|
||||||
#include "AccountsMenu.h"
|
#include "AccountsMenu.h"
|
||||||
|
|
||||||
|
|
||||||
AccountMenuItem::AccountMenuItem(const char* label, BMessage* msg)
|
AccountMenuItem::AccountMenuItem(const char* label, BMessage* msg,
|
||||||
|
BBitmap* icon)
|
||||||
:
|
:
|
||||||
BMenuItem(label, msg)
|
BitmapMenuItem(label, msg, icon, 0, 0, false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,10 +7,12 @@
|
||||||
|
|
||||||
#include <MenuItem.h>
|
#include <MenuItem.h>
|
||||||
|
|
||||||
|
#include <libinterface/BitmapMenuItem.h>
|
||||||
|
|
||||||
class AccountMenuItem : public BMenuItem {
|
|
||||||
|
class AccountMenuItem : public BitmapMenuItem {
|
||||||
public:
|
public:
|
||||||
AccountMenuItem(const char* label, BMessage* msg);
|
AccountMenuItem(const char* label, BMessage* msg, BBitmap* icon = NULL);
|
||||||
|
|
||||||
virtual void SetMarked(bool mark);
|
virtual void SetMarked(bool mark);
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,10 +5,15 @@
|
||||||
|
|
||||||
#include "AccountsMenu.h"
|
#include "AccountsMenu.h"
|
||||||
|
|
||||||
|
#include <Bitmap.h>
|
||||||
#include <Catalog.h>
|
#include <Catalog.h>
|
||||||
#include <MenuItem.h>
|
#include <MenuItem.h>
|
||||||
|
|
||||||
|
#include <libinterface/BitmapMenuItem.h>
|
||||||
|
#include <libinterface/BitmapUtils.h>
|
||||||
|
|
||||||
#include "AccountMenuItem.h"
|
#include "AccountMenuItem.h"
|
||||||
|
#include "ImageCache.h"
|
||||||
#include "MainWindow.h"
|
#include "MainWindow.h"
|
||||||
#include "Server.h"
|
#include "Server.h"
|
||||||
#include "TheApp.h"
|
#include "TheApp.h"
|
||||||
|
@ -64,26 +69,56 @@ AccountsMenu::SetDefaultSelection(BMenuItem* item)
|
||||||
void
|
void
|
||||||
AccountsMenu::_PopulateMenu()
|
AccountsMenu::_PopulateMenu()
|
||||||
{
|
{
|
||||||
if (CountItems() > 0)
|
BFont font;
|
||||||
RemoveItems(0, CountItems(), true);
|
GetFont(&font);
|
||||||
|
|
||||||
if (fAllMessage != NULL)
|
// Add 'all' item if missing
|
||||||
AddItem(new BMenuItem(B_TRANSLATE("All"), new BMessage(*fAllMessage)));
|
if (fAllMessage != NULL && FindItem(B_TRANSLATE("All")) == NULL) {
|
||||||
|
BBitmap* icon = _EnsureAsteriskIcon();
|
||||||
|
AddItem(new BitmapMenuItem(B_TRANSLATE("All"), new BMessage(*fAllMessage),
|
||||||
|
icon, 0, 0, false));
|
||||||
|
}
|
||||||
|
|
||||||
Server* server = ((TheApp*)be_app)->GetMainWindow()->GetServer();
|
Server* server = ((TheApp*)be_app)->GetMainWindow()->GetServer();
|
||||||
AccountInstances accounts = server->GetActiveAccounts();
|
AccountInstances accounts = server->GetActiveAccounts();
|
||||||
|
|
||||||
|
// Add protocol item if not already in menu
|
||||||
for (int i = 0; i < accounts.CountItems(); i++) {
|
for (int i = 0; i < accounts.CountItems(); i++) {
|
||||||
|
int64 instance = accounts.ValueAt(i);
|
||||||
BString label = accounts.KeyAt(i).String();
|
BString label = accounts.KeyAt(i).String();
|
||||||
if (label.CountChars() > 15) {
|
if (label.CountChars() > 15) {
|
||||||
label.RemoveChars(16, label.CountChars() - 16);
|
label.RemoveChars(16, label.CountChars() - 16);
|
||||||
label << B_UTF8_ELLIPSIS;
|
label << B_UTF8_ELLIPSIS;
|
||||||
}
|
}
|
||||||
AddItem(new AccountMenuItem(label.String(), new BMessage(fAccountMessage)));
|
|
||||||
|
if (FindItem(label.String()) != NULL)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
ProtocolLooper* looper = server->GetProtocolLooper(instance);
|
||||||
|
BBitmap* icon = _EnsureProtocolIcon(label.String(), looper);
|
||||||
|
|
||||||
|
BMessage* message = new BMessage(fAccountMessage);
|
||||||
|
message->AddInt64("instance", instance);
|
||||||
|
AddItem(new AccountMenuItem(label.String(), message, icon));
|
||||||
}
|
}
|
||||||
|
|
||||||
int32 selection = fDefaultSelection;
|
int32 selection = fDefaultSelection;
|
||||||
|
|
||||||
|
// If an account has been disabled since last population… get ridda it
|
||||||
|
if ((fAllMessage != NULL && CountItems() - 1 > accounts.CountItems())
|
||||||
|
|| (fAllMessage == NULL && CountItems() > accounts.CountItems()))
|
||||||
|
for (int i = 0; i < CountItems(); i++) {
|
||||||
|
bool found = false;
|
||||||
|
int64 instance = ItemAt(i)->Message()->GetInt64("instance", 0);
|
||||||
|
for (int j = 0; j < accounts.CountItems(); j++)
|
||||||
|
if (accounts.ValueAt(j) == instance)
|
||||||
|
found = true;
|
||||||
|
|
||||||
|
if (found == false)
|
||||||
|
RemoveItem(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply last/default selection
|
||||||
if (fAllMessage == NULL && selection < CountItems() && selection >= 0)
|
if (fAllMessage == NULL && selection < CountItems() && selection >= 0)
|
||||||
ItemAt(selection)->SetMarked(true);
|
ItemAt(selection)->SetMarked(true);
|
||||||
else if (CountItems() > 0)
|
else if (CountItems() > 0)
|
||||||
|
@ -91,3 +126,35 @@ AccountsMenu::_PopulateMenu()
|
||||||
else
|
else
|
||||||
SetEnabled(false);
|
SetEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BBitmap*
|
||||||
|
AccountsMenu::_EnsureProtocolIcon(const char* label, ProtocolLooper* looper)
|
||||||
|
{
|
||||||
|
BFont font;
|
||||||
|
GetFont(&font);
|
||||||
|
BBitmap* icon = ImageCache::Get()->GetImage(label);
|
||||||
|
|
||||||
|
if (icon == NULL && looper != NULL && looper->Protocol()->Icon() != NULL) {
|
||||||
|
BBitmap* unscaled = looper->Protocol()->Icon();
|
||||||
|
icon = RescaleBitmap(unscaled, font.Size(), font.Size());
|
||||||
|
ImageCache::Get()->AddImage(label, icon);
|
||||||
|
}
|
||||||
|
return icon;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BBitmap*
|
||||||
|
AccountsMenu::_EnsureAsteriskIcon()
|
||||||
|
{
|
||||||
|
BFont font;
|
||||||
|
GetFont(&font);
|
||||||
|
BBitmap* icon = ImageCache::Get()->GetImage("kAsteriskScaled");
|
||||||
|
|
||||||
|
if (icon == NULL) {
|
||||||
|
BBitmap* unscaled = ImageCache::Get()->GetImage("kAsteriskIcon");
|
||||||
|
icon = RescaleBitmap(unscaled, font.Size(), font.Size());
|
||||||
|
ImageCache::Get()->AddImage("kAsteriskScaled", icon);
|
||||||
|
}
|
||||||
|
return icon;
|
||||||
|
}
|
||||||
|
|
|
@ -9,6 +9,8 @@
|
||||||
|
|
||||||
#include "Observer.h"
|
#include "Observer.h"
|
||||||
|
|
||||||
|
class ProtocolLooper;
|
||||||
|
|
||||||
|
|
||||||
class AccountsMenu : public BMenu, public Observer {
|
class AccountsMenu : public BMenu, public Observer {
|
||||||
public:
|
public:
|
||||||
|
@ -23,6 +25,11 @@ public:
|
||||||
private:
|
private:
|
||||||
void _PopulateMenu();
|
void _PopulateMenu();
|
||||||
|
|
||||||
|
BBitmap* _EnsureProtocolIcon(const char* label,
|
||||||
|
ProtocolLooper* looper);
|
||||||
|
BBitmap* _EnsureAsteriskIcon();
|
||||||
|
|
||||||
|
|
||||||
BMessage fAccountMessage;
|
BMessage fAccountMessage;
|
||||||
BMessage* fAllMessage;
|
BMessage* fAllMessage;
|
||||||
static int32 fDefaultSelection;
|
static int32 fDefaultSelection;
|
||||||
|
|
Ŝarĝante…
Reference in New Issue