// XABContactMenu.cpp - XABContactMenu widget
//
// (c) 2012-2016 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"
XABContactMenu::XABContactMenu(XABContactMenuType MenuTypeIn){
// Setup the XABContactMenu control.
MenuType = MenuTypeIn;
// Setup the menu items.
AppendMenuItem(wxT("opencontact"), _("Open Contact..."), wxT(""), ID_CONTACTMENU_OPEN, wxITEM_NORMAL);
AppendMenuItem(wxT("sep1"), wxT(""), wxT(""), ID_CONTACTMENU_SEPARATOR, wxITEM_SEPARATOR);
if (MenuType == XABCONTACTMENU_MAIN){
// Setup up the add contact option if in the main window.
AppendMenuItem(wxT("newcontact"), _("New Contact..."), wxT(""), ID_CONTACTMENU_NEW, wxITEM_NORMAL);
}
AppendMenuItem(wxT("editcontact"), _("Edit Contact..."), wxT(""), 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..."), wxT(""), 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..."), wxT(""), ID_CONTACTMENU_DELETE, wxITEM_NORMAL);
AppendMenuItem(wxT("sep2"), wxT(""), wxT(""), ID_CONTACTMENU_SEPARATOR, wxITEM_SEPARATOR);
AppendMenuItem(wxT("refreshab"), _("Refresh Address Book"), wxT(""), ID_CONTACTMENU_REFRESHAB, wxITEM_NORMAL);
}
}
XABContactMenu::~XABContactMenu(){
// Destory the XABContactMenu object.
// 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,
XABContactMenuID ItemID,
wxItemKind ItemType){
if (ItemType == wxITEM_SEPARATOR){
this->AppendSeparator();
return;
}
// Append a menu item to the XABContactMenu control.
wxMenuItem *menuitem = this->Append(ItemID, MenuName, MenuDescription);
this->Connect(menuitem->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(XABContactMenu::ProcessMenuItemClick));
MenuItems.insert(std::make_pair(ObjectName.ToStdString(), menuitem));
}
void XABContactMenu::SetupPointersSearch(wxWindow* WindowPtrIn,
wxListCtrl* ContactListCtrlIn,
bool SearchModeOnlyIn){
// Setup the pointers when being used in the search window.
WindowPtr = WindowPtrIn;
ContactListCtrl = ContactListCtrlIn;
SearchModeOnly = SearchModeOnlyIn;
}
void XABContactMenu::SetupPointers(wxWindow* WindowPtrIn,
wxListCtrl* ContactListCtrlIn,
bool EnableAccountSettingsIn){
// Setup the pointers.
WindowPtr = WindowPtrIn;
ContactListCtrl = ContactListCtrlIn;
EnableAccountSettings = EnableAccountSettingsIn;
}
wxMenu* XABContactMenu::MenuPointer(){
// Process the menu pointer.
// 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){
// Process an action when a menu item in the XABContactMenu
// control is selected.
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;
}
}
}