Chat-O-Matic/protocols/purple/PurpleDialog.cpp

109 lines
2.6 KiB
C++
Raw Permalink Normal View History

/*
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "PurpleDialog.h"
#include <libpurple/purple.h>
#include <Button.h>
#include <LayoutBuilder.h>
#include <StringView.h>
#include <TextView.h>
2021-07-18 21:51:38 -05:00
#include "PurpleApp.h"
PurpleDialog::PurpleDialog(const char* title, const char* primary,
const char* secondary, PurpleAccount* account, va_list actions,
size_t action_count, void* user_data)
:
2021-07-18 21:51:38 -05:00
BWindow(BRect(BPoint(-1000, -1000), BSize(300, 250)), _tr(title),
B_FLOATING_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
B_AUTO_UPDATE_SIZE_LIMITS),
fUserData(user_data)
{
CenterOnScreen();
_ParseActions(actions, action_count, PURPLE_REQUEST_ACTION);
_InitActionInterface(primary, secondary);
}
void
PurpleDialog::MessageReceived(BMessage* msg)
{
switch (msg->what)
{
case ACTION_BUTTON:
{
int32 id;
if (msg->FindInt32("index", &id) != B_OK) break;
PurpleRequestActionCb cb = fActions.ItemAt(id)->callback.action;
cb(fUserData, fActions.ItemAt(id)->index);
Quit();
break;
}
default:
BWindow::MessageReceived(msg);
}
}
void
PurpleDialog::_InitActionInterface(const char* label, const char* desc)
{
2021-07-18 21:51:38 -05:00
BStringView* primaryLabel = new BStringView("primaryText", _tr(label));
primaryLabel->SetExplicitAlignment(
BAlignment(B_ALIGN_CENTER, B_ALIGN_TOP));
BTextView* secondaryLabel = new BTextView("secondaryText");
secondaryLabel->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
secondaryLabel->MakeEditable(false);
secondaryLabel->SetWordWrap(true);
2021-07-18 21:51:38 -05:00
secondaryLabel->SetText(_tr(desc));
// Init buttons view
BView* buttonsView = new BView("actionButtons", 0);
BLayoutBuilder::Group<>(buttonsView, B_HORIZONTAL);
for (int i = 0; i < fActions.CountItems(); i++) {
RequestAction* action = fActions.ItemAt(i);
BMessage* msg = new BMessage(ACTION_BUTTON);
msg->AddInt32("index", action->index);
BButton* button = new BButton(action->name.String(), msg);
buttonsView->AddChild(button);
}
// Main layout
BLayoutBuilder::Group<>(this, B_VERTICAL)
.SetInsets(B_USE_DEFAULT_SPACING)
.Add(primaryLabel)
.Add(secondaryLabel)
.Add(buttonsView)
.AddGlue()
.End();
}
void
PurpleDialog::_ParseActions(va_list actions, int32 count,
PurpleRequestType type)
{
for (int i = 0; i < count; i++) {
RequestAction* action = new RequestAction;
action->name = va_arg(actions, const char*);
action->index = i;
action->type = type;
switch (type) {
case PURPLE_REQUEST_ACTION:
action->callback.action = va_arg(actions, PurpleRequestActionCb);
break;
}
fActions.AddItem(action);
}
}