// XABContactMenu.cpp - XABContactMenu widget
//
// (c) 2012-2015 Xestia Software Development.
//
// This file is part of Xestia Address Book.
//
// Xestia Address Book is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by the
// Free Software Foundation, version 3 of the license.
//
// Xestia Address Book is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with Xestia Address Book. If not, see
#include "XABContactMenu.h"
#include "../frmMain.h"
#include "../search/frmSearch.h"
enum {
ID_CONTACTMENU_OPEN,
ID_CONTACTMENU_NEW,
ID_CONTACTMENU_EDIT,
ID_CONTACTMENU_DELETE,
ID_CONTACTMENU_REFRESHAB,
ID_CONTACTMENU_REVEAL
};
XABContactMenu::XABContactMenu(int MenuTypeIn){
MenuType = MenuTypeIn;
// Setup the menu items.
AppendMenuItem(wxT("opencontact"), _("Open Contact..."), _(""), ID_CONTACTMENU_OPEN, wxITEM_NORMAL);
AppendMenuItem(wxT("sep1"), _(""), _(""), wxID_ANY, wxITEM_SEPARATOR);
if (MenuType == XABCONTACTMENU_MAIN){
// Setup up the add contact option if in the main window.
AppendMenuItem(wxT("newcontact"), _("New Contact..."), _(""), ID_CONTACTMENU_NEW, wxITEM_NORMAL);
}
AppendMenuItem(wxT("editcontact"), _("Edit Contact..."), _(""), ID_CONTACTMENU_EDIT, wxITEM_NORMAL);
if (MenuType == XABCONTACTMENU_SEARCH){
// Setup up the reveal contact option if in the search window.
AppendMenuItem(wxT("revealcontact"), _("Reveal Contact..."), _(""), ID_CONTACTMENU_REVEAL, wxITEM_NORMAL);
}
if (MenuType == XABCONTACTMENU_MAIN){
// Setup the delete and refresh contact options if in the main window.
AppendMenuItem(wxT("deletecontact"), _("Delete Contact..."), _(""), ID_CONTACTMENU_DELETE, wxITEM_NORMAL);
AppendMenuItem(wxT("sep2"), _(""), _(""), wxID_ANY, wxITEM_SEPARATOR);
AppendMenuItem(wxT("refreshab"), _("Refresh Address Book"), _(""), ID_CONTACTMENU_REFRESHAB, wxITEM_NORMAL);
}
}
XABContactMenu::~XABContactMenu(){
// Delete the menu items.
for (std::map::iterator MenuItemIter = MenuItems.begin();
MenuItemIter != MenuItems.end(); ++MenuItemIter){
// Delete the wxMenuItem object.
this->Disconnect(MenuItemIter->second->GetId(), wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(XABContactMenu::ProcessMenuItemClick) );
delete(MenuItemIter->second);
MenuItemIter->second = NULL;
}
MenuItems.clear();
}
void XABContactMenu::AppendMenuItem(wxString ObjectName,
wxString MenuName,
wxString MenuDescription,
int ItemID,
wxItemKind ItemType){
wxMenuItem *menuitem = new wxMenuItem(
NULL,
ItemID,
MenuName,
MenuDescription,
ItemType,
NULL
);
this->Connect(menuitem->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(XABContactMenu::ProcessMenuItemClick));
this->Append(menuitem);
MenuItems.insert(std::make_pair(ObjectName.ToStdString(), menuitem));
}
void XABContactMenu::SetupPointersSearch(wxWindow* WindowPtrIn,
wxListCtrl* ContactListCtrlIn,
bool SearchModeOnlyIn){
WindowPtr = WindowPtrIn;
ContactListCtrl = ContactListCtrlIn;
SearchModeOnly = SearchModeOnlyIn;
}
void XABContactMenu::SetupPointers(wxWindow* WindowPtrIn,
wxListCtrl* ContactListCtrlIn,
bool EnableAccountSettingsIn){
WindowPtr = WindowPtrIn;
ContactListCtrl = ContactListCtrlIn;
EnableAccountSettings = EnableAccountSettingsIn;
}
wxMenu* XABContactMenu::MenuPointer(){
// Check for the following before passing the pointer:
// If an account has been selected.
// - Disable Refresh Address Book if not.
// If one or more contacts have been selected.
// - Disable New, Edit, Delete and Reveal contact as needed.
bool ContactSelected = FALSE;
long ItemIndex = -1;
int ContactCount = 0;
for (;;){
ItemIndex = ContactListCtrl->GetNextItem(ItemIndex,
wxLIST_NEXT_ALL,
wxLIST_STATE_SELECTED);
if (ItemIndex == -1){
break;
}
ContactSelected = TRUE;
break;
}
if (MenuType == XABCONTACTMENU_MAIN){
std::map::iterator MenuItemIter = MenuItems.find("refreshab");
if (EnableAccountSettings == FALSE){
MenuItemIter->second->Enable(FALSE);
MenuItemIter = MenuItems.find("newcontact");
MenuItemIter->second->Enable(FALSE);
}
MenuItemIter = MenuItems.find("opencontact");
MenuItemIter->second->Enable(ContactSelected);
MenuItemIter = MenuItems.find("editcontact");
MenuItemIter->second->Enable(ContactSelected);
MenuItemIter = MenuItems.find("deletecontact");
MenuItemIter->second->Enable(ContactSelected);
} else if (MenuType == XABCONTACTMENU_SEARCH){
std::map::iterator MenuItemIter = MenuItems.find("opencontact");
MenuItemIter->second->Enable(ContactSelected);
if (SearchModeOnly == TRUE){
MenuItemIter = MenuItems.find("editcontact");
MenuItemIter->second->Enable(FALSE);
MenuItemIter = MenuItems.find("revealcontact");
MenuItemIter->second->Enable(FALSE);
} else {
MenuItemIter = MenuItems.find("editcontact");
MenuItemIter->second->Enable(ContactSelected);
MenuItemIter = MenuItems.find("revealcontact");
MenuItemIter->second->Enable(ContactSelected);
}
}
return this;
}
void XABContactMenu::ProcessMenuItemClick( wxCommandEvent& event){
int ItemID = event.GetId();
if (MenuType == XABCONTACTMENU_MAIN){
switch (ItemID){
case ID_CONTACTMENU_OPEN:
{
wxCommandEvent rcevent(CE_OPENCONTACTLIST);
wxPostEvent(WindowPtr, rcevent);
}
break;
case ID_CONTACTMENU_NEW:
{
wxCommandEvent rcevent(CE_NEWCONTACT);
wxPostEvent(WindowPtr, rcevent);
}
break;
case ID_CONTACTMENU_EDIT:
{
wxCommandEvent rcevent(CE_EDITCONTACT);
wxPostEvent(WindowPtr, rcevent);
}
break;
case ID_CONTACTMENU_DELETE:
{
wxCommandEvent rcevent(CE_DELETECONTACT);
wxPostEvent(WindowPtr, rcevent);
}
break;
case ID_CONTACTMENU_REFRESHAB:
{
wxCommandEvent rcevent(REFRESHADDRESSBOOK);
wxPostEvent(WindowPtr, rcevent);
}
break;
}
} else if (MenuType == XABCONTACTMENU_SEARCH){
switch (ItemID){
case ID_CONTACTMENU_OPEN:
{
wxCommandEvent rcevent(SE_OPENCONTACT);
wxPostEvent(WindowPtr, rcevent);
}
break;
case ID_CONTACTMENU_EDIT:
{
wxCommandEvent rcevent(SE_EDITCONTACT);
wxPostEvent(WindowPtr, rcevent);
}
break;
case ID_CONTACTMENU_REVEAL:
{
wxCommandEvent rcevent(SE_REVEALCONTACT);
wxPostEvent(WindowPtr, rcevent);
}
break;
}
}
}