From 062e273d4389bd652ec40eefac2487a7f1ee02db Mon Sep 17 00:00:00 2001 From: Humdinger Date: Thu, 25 Jul 2024 14:34:55 +0200 Subject: [PATCH] Add context menus to Transaction and Accounts list * 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. --- Makefile | 2 +- ...ccountListItem.cpp => AccountListView.cpp} | 99 ++++++++++++++++++- src/{AccountListItem.h => AccountListView.h} | 27 +++++ src/MainWindow.h | 1 + src/RegisterView.cpp | 7 +- src/RegisterView.h | 5 +- src/TransactionView.cpp | 90 ++++++++++++++++- src/TransactionView.h | 30 +++++- src/TransferWindow.h | 2 +- 9 files changed, 252 insertions(+), 11 deletions(-) rename src/{AccountListItem.cpp => AccountListView.cpp} (50%) rename src/{AccountListItem.h => AccountListView.h} (50%) diff --git a/Makefile b/Makefile index 02f2763..2bae402 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/src/AccountListItem.cpp b/src/AccountListView.cpp similarity index 50% rename from src/AccountListItem.cpp rename to src/AccountListView.cpp index 4e8c9c6..82f7fba 100644 --- a/src/AccountListItem.cpp +++ b/src/AccountListView.cpp @@ -1,12 +1,15 @@ -#include "AccountListItem.h" +#include "AccountListView.h" #include #include #include #include #include + #include "Account.h" #include "CBLocale.h" +#include "MainWindow.h" #include "Preferences.h" +#include "RegisterView.h" #include "TransactionLayout.h" @@ -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(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); +} diff --git a/src/AccountListItem.h b/src/AccountListView.h similarity index 50% rename from src/AccountListItem.h rename to src/AccountListView.h index 1555895..9ffe6dc 100644 --- a/src/AccountListItem.h +++ b/src/AccountListView.h @@ -2,9 +2,36 @@ #define ACCOUNTLISTITEM_H #include +#include +#include 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); diff --git a/src/MainWindow.h b/src/MainWindow.h index 1520448..508e281 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -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, diff --git a/src/RegisterView.cpp b/src/RegisterView.cpp index db1eded..94e0058 100644 --- a/src/RegisterView.cpp +++ b/src/RegisterView.cpp @@ -7,7 +7,7 @@ #include #include "Account.h" -#include "AccountListItem.h" +#include "AccountListView.h" #include "CheckView.h" #include "Database.h" #include "MainWindow.h" @@ -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)); @@ -219,4 +219,5 @@ RegisterView::GetAccountViewWidth() width = (namewidth > width) ? namewidth : width; } return width; -} \ No newline at end of file +} + diff --git a/src/RegisterView.h b/src/RegisterView.h index 604f218..d8f0d7b 100644 --- a/src/RegisterView.h +++ b/src/RegisterView.h @@ -4,6 +4,8 @@ #include #include #include + +#include "AccountListView.h" #include "Notifier.h" #include "TransactionView.h" @@ -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); @@ -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; diff --git a/src/TransactionView.cpp b/src/TransactionView.cpp index d42b65f..16a99a1 100644 --- a/src/TransactionView.cpp +++ b/src/TransactionView.cpp @@ -1,25 +1,27 @@ #include "TransactionView.h" #include #include +#include #include #include #include #include #include #include + #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); @@ -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); +} diff --git a/src/TransactionView.h b/src/TransactionView.h index e8f7c3a..7b8759b 100644 --- a/src/TransactionView.h +++ b/src/TransactionView.h @@ -2,10 +2,11 @@ #define TRANSACTIONVIEW_H #include +#include #include #include #include -#include + #include "Account.h" #include "Notifier.h" #include "TransactionData.h" @@ -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); @@ -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* fItemList; Transaction* fCurrent; BStringView *fCategoryLabel, *fMemoLabel; diff --git a/src/TransferWindow.h b/src/TransferWindow.h index 83ff882..0cf673b 100644 --- a/src/TransferWindow.h +++ b/src/TransferWindow.h @@ -9,7 +9,7 @@ #include #include #include "Account.h" -#include "AccountListItem.h" +#include "AccountListView.h" #include "Fixed.h" #define M_CREATE_TRANSFER 'crtn'