Chat-O-Matic/application/views/StatusView.cpp

149 lines
3.1 KiB
C++
Raw Normal View History

/*
* Copyright 2009, Pier Luigi Fiorini. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
*/
2021-05-19 16:12:19 -05:00
#include "StatusView.h"
#include <Bitmap.h>
2021-05-29 15:47:54 -05:00
#include <LayoutBuilder.h>
#include <Message.h>
#include <MenuField.h>
#include <MenuItem.h>
#include <PopUpMenu.h>
#include <StringView.h>
#include <libinterface/BitmapMenuItem.h>
#include <libinterface/BitmapUtils.h>
2021-05-19 16:12:19 -05:00
#include <libinterface/BitmapView.h>
#include "AccountManager.h"
#include "ImageCache.h"
#include "NicknameTextControl.h"
#include "StatusMenuItem.h"
2021-06-20 12:44:20 -05:00
#include "Utils.h"
2021-05-19 16:12:19 -05:00
const int32 kSetNickname = 'stnk';
StatusView::StatusView(const char* name)
:
BView(name, B_WILL_DRAW)
{
// Nick name
fNickname = new NicknameTextControl("Nickname",
new BMessage(kSetNickname));
// Status menu
fStatusMenu = new BPopUpMenu("-");
// Add status menu items
2021-06-20 12:44:20 -05:00
int32 s = STATUS_ONLINE;
while (s >= STATUS_ONLINE && s < STATUS_STATUSES) {
StatusMenuItem* item = new StatusMenuItem(UserStatusToString(
(UserStatus)s), (UserStatus)s);
fStatusMenu->AddItem(item);
/*// Add items for custom messages
2021-06-20 12:44:20 -05:00
if (s == STATUS_ONLINE || s == STATUS_DO_NOT_DISTURB) {
item = new StatusMenuItem("Custom...", (UserStatus)s, true);
fStatusMenu->AddItem(item);
fStatusMenu->AddItem(new BSeparatorItem());
}*/
// Mark offline status by default
2021-06-20 12:44:20 -05:00
if (s == STATUS_OFFLINE)
item->SetMarked(true);
s++;
}
// Menu field
BMenuField* statusField = new BMenuField("StatusField", NULL,
fStatusMenu);
// Icon
fAvatar = new BitmapView("AvatarIcon");
fAvatar->SetExplicitMaxSize(BSize(50, 50));
fAvatar->SetExplicitPreferredSize(BSize(50, 50));
fAvatar->SetBitmap(ImageCache::Get()->GetImage("kPersonIcon"));
// Set layout
2021-05-29 15:47:54 -05:00
BLayoutBuilder::Group<>(this, B_HORIZONTAL)
.AddGroup(B_VERTICAL)
.Add(statusField)
.AddGroup(B_HORIZONTAL)
.Add(fNickname)
.Add(fAvatar)
.End()
.End()
2021-05-29 15:47:54 -05:00
.End();
}
void
StatusView::AttachedToWindow()
{
//fNickname->SetTarget(this);
fStatusMenu->SetTargetForItems(this);
}
void
StatusView::MessageReceived(BMessage* msg)
{
switch (msg->what) {
case kSetNickname:
{
AccountManager* accountManager = AccountManager::Get();
accountManager->SetNickname(fNickname->Text());
break;
}
case kSetStatus:
{
int32 status;
if (msg->FindInt32("status", &status) != B_OK)
return;
AccountManager* accountManager = AccountManager::Get();
2021-06-20 12:44:20 -05:00
accountManager->SetStatus((UserStatus)status, "");
break;
}
default:
BView::MessageReceived(msg);
}
}
void
StatusView::SetName(BString name)
{
fNickname->SetText(name.String());
}
void
2021-06-20 12:44:20 -05:00
StatusView::SetStatus(UserStatus status)
{
for (int32 i = 0; i < fStatusMenu->CountItems(); i++) {
StatusMenuItem* item
= reinterpret_cast<StatusMenuItem*>(fStatusMenu->ItemAt(i));
if (item && item->Status() == status && !item->IsCustom())
item->SetMarked(true);
}
}
void
2010-06-22 14:02:42 -05:00
StatusView::SetAvatarIcon(const BBitmap* bitmap)
{
// We don't want the default avatar to override a real one
if (bitmap != ImageCache::Get()->GetImage("kPersonIcon"))
fAvatar->SetBitmap(bitmap);
}