Skip to content

Commit

Permalink
Add context menus to Transaction and Accounts list
Browse files Browse the repository at this point in the history
* Create classes AccountList and TransactionList to be able to implement
  MouseDown() to show popup menus.

* Re-use B_TRANSLATION_CONTEXT "MainWindow" to avoid duplicated translation
  work for the same menu item labels.

* Rename AccountListItem.h|.cpp to AccountListView.h|cpp to host all the
  code for List, Context, Item.
  • Loading branch information
humdingerb committed Jul 25, 2024
1 parent c9cf0f4 commit 062e273
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ APP_MIME_SIG = application/x-vnd.wgp-CapitalBe
# Also note that spaces in folder names do not work well with this Makefile.
SRCS = \
src/Account.cpp \
src/AccountListItem.cpp \
src/AccountListView.cpp \
src/AccountSettingsWindow.cpp \
src/App.cpp \
src/AutoTextControl.cpp \
Expand Down
99 changes: 98 additions & 1 deletion src/AccountListItem.cpp → src/AccountListView.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include "AccountListItem.h"
#include "AccountListView.h"
#include <Catalog.h>
#include <Font.h>
#include <ListView.h>
#include <String.h>
#include <View.h>

#include "Account.h"
#include "CBLocale.h"
#include "MainWindow.h"
#include "Preferences.h"
#include "RegisterView.h"
#include "TransactionLayout.h"


Expand Down Expand Up @@ -84,3 +87,97 @@ AccountListItem::Update(BView* owner, const BFont* finfo)
// value based on the height of be_plain_font, which we are also using here
SetHeight(TRowHeight() * 2);
}


#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "MainWindow"

AccountList::AccountList(void)
:
BListView("AccountList"),
fShowingPopUpMenu(false)
{
}

AccountList::~AccountList(void)
{
}

void
AccountList::MessageReceived(BMessage* message)
{
switch (message->what) {
case M_CONTEXT_CLOSE:
{
fShowingPopUpMenu = false;
break;
}
default:
{
BListView::MessageReceived(message);
break;
}
}
}

void
AccountList::MouseDown(BPoint position)
{
uint32 buttons = 0;
if (Window() != NULL && Window()->CurrentMessage() != NULL)
buttons = Window()->CurrentMessage()->FindInt32("buttons");

if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0) {
Select(IndexOf(position));
ShowPopUpMenu(position);
return;
}

BView::MouseDown(position);
}

#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "MainWindow"

void
AccountList::ShowPopUpMenu(BPoint position)
{
if (fShowingPopUpMenu || IsEmpty() || IndexOf(position) < 0)
return;

AccountContext* menu = new AccountContext("PopUpMenu", this);

menu->AddItem(new BMenuItem(
B_TRANSLATE("Reconcile" B_UTF8_ELLIPSIS), new BMessage(M_SHOW_RECONCILE_WINDOW), 'R'));
menu->AddSeparatorItem();
menu->AddItem(
new BMenuItem(B_TRANSLATE("New" B_UTF8_ELLIPSIS), new BMessage(M_SHOW_NEW_ACCOUNT), 'N'));
menu->AddItem(
new BMenuItem(B_TRANSLATE("Delete" B_UTF8_ELLIPSIS), new BMessage(M_DELETE_ACCOUNT)));

AccountListItem* item = dynamic_cast<AccountListItem*>(ItemAt(IndexOf(position)));
Account* acc = item->GetAccount();
BString label = acc->IsClosed() ? B_TRANSLATE("Reopen") : B_TRANSLATE("Close");
menu->AddItem(new BMenuItem(label, new BMessage(M_CLOSE_ACCOUNT)));
menu->AddSeparatorItem();
menu->AddItem(new BMenuItem(
B_TRANSLATE("Settings" B_UTF8_ELLIPSIS), new BMessage(M_SHOW_ACCOUNT_SETTINGS)));

menu->SetTargetForItems(Window());
menu->Go(ConvertToScreen(position), true, true, true);
fShowingPopUpMenu = true;
}


AccountContext::~AccountContext(void)
{
fTarget.SendMessage(M_CONTEXT_CLOSE);
}

AccountContext::AccountContext(const char* name, BMessenger target)
:
BPopUpMenu(name, false, false),
fTarget(target)
{
SetAsyncAutoDestruct(true);
}
27 changes: 27 additions & 0 deletions src/AccountListItem.h → src/AccountListView.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,36 @@
#define ACCOUNTLISTITEM_H

#include <ListItem.h>
#include <ListView.h>
#include <PopUpMenu.h>

class Account;


class AccountList : public BListView {
public:
AccountList(void);
~AccountList(void);

virtual void MessageReceived(BMessage* message);
void MouseDown(BPoint position);

private:
void ShowPopUpMenu(BPoint screen);
bool fShowingPopUpMenu;
};


class AccountContext : public BPopUpMenu {
public:
AccountContext(const char* name, BMessenger target);
virtual ~AccountContext(void);

private:
BMessenger fTarget;
};


class AccountListItem : public BListItem {
public:
AccountListItem(Account* acc);
Expand Down
1 change: 1 addition & 0 deletions src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum {
M_DELETE_TRANSACTION,
M_SCHEDULE_TRANSACTION,
M_ENTER_TRANSFER,
M_CONTEXT_CLOSE,

M_SHOW_ABOUT = 'msha',
M_SHOW_REPORTS_WINDOW,
Expand Down
7 changes: 4 additions & 3 deletions src/RegisterView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <StringView.h>

#include "Account.h"
#include "AccountListItem.h"
#include "AccountListView.h"
#include "CheckView.h"
#include "Database.h"
#include "MainWindow.h"
Expand All @@ -34,7 +34,7 @@ RegisterView::RegisterView(const char* name, int32 flags)
accountLabel->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));

// fAccountView = new DragListView(r,"accountview");
fAccountView = new BListView("accountview", B_SINGLE_SELECTION_LIST);
fAccountView = new AccountList();
fAccountView->SetSelectionMessage(new BMessage(M_SELECT_ACCOUNT));
fAccountView->SetInvocationMessage(new BMessage(M_SHOW_ACCOUNT_SETTINGS));
fAccountView->SetExplicitSize(BSize(GetAccountViewWidth(), B_SIZE_UNSET));
Expand Down Expand Up @@ -219,4 +219,5 @@ RegisterView::GetAccountViewWidth()
width = (namewidth > width) ? namewidth : width;
}
return width;
}
}

5 changes: 4 additions & 1 deletion src/RegisterView.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <Box.h>
#include <ListView.h>
#include <View.h>

#include "AccountListView.h"
#include "Notifier.h"
#include "TransactionView.h"

Expand All @@ -17,6 +19,7 @@ class RegisterView : public BView, public Observer {
~RegisterView(void);
void MessageReceived(BMessage* msg);
void AttachedToWindow(void);

void HandleNotify(const uint64& value, const BMessage* msg);
void SelectAccount(const int32& index);

Expand All @@ -32,7 +35,7 @@ class RegisterView : public BView, public Observer {
float GetAccountViewWidth(void);

CheckView* fCheckView;
BListView* fAccountView;
AccountList* fAccountView;
BScrollView* fAccountScroller;
TransactionView* fTransactionView;
BBox* fTrackBox;
Expand Down
90 changes: 88 additions & 2 deletions src/TransactionView.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
#include "TransactionView.h"
#include <Bitmap.h>
#include <Box.h>
#include <Catalog.h>
#include <LayoutBuilder.h>
#include <ScrollBar.h>
#include <StringView.h>
#include <TranslationUtils.h>
#include <stdio.h>
#include <stdlib.h>

#include "Database.h"
#include "MainWindow.h"
#include "TransactionItem.h"
#include "TransactionLayout.h"


TransactionView::TransactionView()
: BView("transactionview", B_WILL_DRAW | B_SUBPIXEL_PRECISE | B_FRAME_EVENTS),
fCurrent(NULL)
{
InitTransactionItemLayout(this);

fListView = new BListView("RegisterList", B_SINGLE_SELECTION_LIST,
B_WILL_DRAW | B_NAVIGABLE | B_FULL_UPDATE_ON_RESIZE);
fListView = new TransactionList();
fListView->SetSelectionMessage(new BMessage(M_TRANSACTION_SELECTED));
fListView->SetInvocationMessage(new BMessage(M_TRANSACTION_INVOKED));
fScrollView = new BScrollView("scrollregister", fListView, 0, false, true);
Expand Down Expand Up @@ -422,3 +424,87 @@ TransactionView::FindIndexForDate(const time_t& time, const char* payee)
}
return fListView->CountItems();
}


TransactionList::TransactionList(void)
:
BListView("TransactionList"),
fShowingPopUpMenu(false)
{
}

TransactionList::~TransactionList(void)
{
}

void
TransactionList::MessageReceived(BMessage* message)
{
switch (message->what) {
case M_CONTEXT_CLOSE:
{
fShowingPopUpMenu = false;
break;
}
default:
{
BListView::MessageReceived(message);
break;
}
}
}

void
TransactionList::MouseDown(BPoint position)
{
uint32 buttons = 0;
if (Window() != NULL && Window()->CurrentMessage() != NULL)
buttons = Window()->CurrentMessage()->FindInt32("buttons");

if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0) {
Select(IndexOf(position));
ShowPopUpMenu(position);
return;
}

BView::MouseDown(position);
}

#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "MainWindow"

void
TransactionList::ShowPopUpMenu(BPoint position)
{
if (fShowingPopUpMenu || IsEmpty())
return;

TransactionContext* menu = new TransactionContext("PopUpMenu", this);

menu->AddItem(
new BMenuItem(B_TRANSLATE("Edit" B_UTF8_ELLIPSIS), new BMessage(M_EDIT_TRANSACTION), 'E'));
menu->AddItem(
new BMenuItem(B_TRANSLATE("Schedule this transaction" B_UTF8_ELLIPSIS),
new BMessage(M_SCHEDULE_TRANSACTION)));
menu->AddSeparatorItem();
menu->AddItem(
new BMenuItem(B_TRANSLATE("Delete"), new BMessage(M_DELETE_TRANSACTION)));

menu->SetTargetForItems(Window());
menu->Go(ConvertToScreen(position), true, true, true);
fShowingPopUpMenu = true;
}


TransactionContext::~TransactionContext(void)
{
fTarget.SendMessage(M_CONTEXT_CLOSE);
}

TransactionContext::TransactionContext(const char* name, BMessenger target)
:
BPopUpMenu(name, false, false),
fTarget(target)
{
SetAsyncAutoDestruct(true);
}
30 changes: 28 additions & 2 deletions src/TransactionView.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#define TRANSACTIONVIEW_H

#include <ListView.h>
#include <PopUpMenu.h>
#include <ScrollView.h>
#include <StringView.h>
#include <View.h>
#include <set>

#include "Account.h"
#include "Notifier.h"
#include "TransactionData.h"
Expand All @@ -15,6 +16,31 @@

class TransactionItem;


class TransactionList : public BListView {
public:
TransactionList(void);
~TransactionList(void);

virtual void MessageReceived(BMessage* message);
void MouseDown(BPoint position);

private:
void ShowPopUpMenu(BPoint screen);
bool fShowingPopUpMenu;
};


class TransactionContext : public BPopUpMenu {
public:
TransactionContext(const char* name, BMessenger target);
virtual ~TransactionContext();

private:
BMessenger fTarget;
};


class TransactionView : public BView, public Observer {
public:
TransactionView(void);
Expand Down Expand Up @@ -42,7 +68,7 @@ class TransactionView : public BView, public Observer {
int32 FindItemForID(const uint32& id);
int32 FindIndexForDate(const time_t& time, const char* payee);

BListView* fListView;
TransactionList* fListView;
BObjectList<TransactionItem>* fItemList;
Transaction* fCurrent;
BStringView *fCategoryLabel, *fMemoLabel;
Expand Down
Loading

0 comments on commit 062e273

Please sign in to comment.