1 // XABContactMenu.cpp - XABContactMenu widget
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include "XABContactMenu.h"
20 #include "../frmMain.h"
21 #include "../search/frmSearch.h"
23 XABContactMenu::XABContactMenu(XABContactMenuType MenuTypeIn){
25 // Setup the XABContactMenu control.
27 MenuType = MenuTypeIn;
29 // Setup the menu items.
31 AppendMenuItem(wxT("opencontact"), _("Open Contact..."), _(""), ID_CONTACTMENU_OPEN, wxITEM_NORMAL);
32 AppendMenuItem(wxT("sep1"), _(""), _(""), ID_CONTACTMENU_SEPARATOR, wxITEM_SEPARATOR);
34 if (MenuType == XABCONTACTMENU_MAIN){
36 // Setup up the add contact option if in the main window.
38 AppendMenuItem(wxT("newcontact"), _("New Contact..."), _(""), ID_CONTACTMENU_NEW, wxITEM_NORMAL);
42 AppendMenuItem(wxT("editcontact"), _("Edit Contact..."), _(""), ID_CONTACTMENU_EDIT, wxITEM_NORMAL);
44 if (MenuType == XABCONTACTMENU_SEARCH){
46 // Setup up the reveal contact option if in the search window.
48 AppendMenuItem(wxT("revealcontact"), _("Reveal Contact..."), _(""), ID_CONTACTMENU_REVEAL, wxITEM_NORMAL);
52 if (MenuType == XABCONTACTMENU_MAIN){
54 // Setup the delete and refresh contact options if in the main window.
56 AppendMenuItem(wxT("deletecontact"), _("Delete Contact..."), _(""), ID_CONTACTMENU_DELETE, wxITEM_NORMAL);
57 AppendMenuItem(wxT("sep2"), _(""), _(""), ID_CONTACTMENU_SEPARATOR, wxITEM_SEPARATOR);
58 AppendMenuItem(wxT("refreshab"), _("Refresh Address Book"), _(""), ID_CONTACTMENU_REFRESHAB, wxITEM_NORMAL);
64 XABContactMenu::~XABContactMenu(){
66 // Destory the XABContactMenu object.
68 // Delete the menu items.
70 for (std::map<std::string, wxMenuItem*>::iterator MenuItemIter = MenuItems.begin();
71 MenuItemIter != MenuItems.end(); ++MenuItemIter){
73 // Delete the wxMenuItem object.
75 this->Disconnect(MenuItemIter->second->GetId(), wxEVT_COMMAND_MENU_SELECTED,
76 wxCommandEventHandler(XABContactMenu::ProcessMenuItemClick) );
77 delete(MenuItemIter->second);
78 MenuItemIter->second = NULL;
86 void XABContactMenu::AppendMenuItem(wxString ObjectName,
88 wxString MenuDescription,
89 XABContactMenuID ItemID,
92 if (ItemType == wxITEM_SEPARATOR){
93 this->AppendSeparator();
97 // Append a menu item to the XABContactMenu control.
99 wxMenuItem *menuitem = this->Append(ItemID, MenuName, MenuDescription);
100 this->Connect(menuitem->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(XABContactMenu::ProcessMenuItemClick));
101 MenuItems.insert(std::make_pair(ObjectName.ToStdString(), menuitem));
105 void XABContactMenu::SetupPointersSearch(wxWindow* WindowPtrIn,
106 wxListCtrl* ContactListCtrlIn,
107 bool SearchModeOnlyIn){
109 // Setup the pointers when being used in the search window.
111 WindowPtr = WindowPtrIn;
112 ContactListCtrl = ContactListCtrlIn;
113 SearchModeOnly = SearchModeOnlyIn;
117 void XABContactMenu::SetupPointers(wxWindow* WindowPtrIn,
118 wxListCtrl* ContactListCtrlIn,
119 bool EnableAccountSettingsIn){
121 // Setup the pointers.
123 WindowPtr = WindowPtrIn;
124 ContactListCtrl = ContactListCtrlIn;
125 EnableAccountSettings = EnableAccountSettingsIn;
129 wxMenu* XABContactMenu::MenuPointer(){
131 // Process the menu pointer.
133 // Check for the following before passing the pointer:
134 // If an account has been selected.
135 // - Disable Refresh Address Book if not.
136 // If one or more contacts have been selected.
137 // - Disable New, Edit, Delete and Reveal contact as needed.
139 bool ContactSelected = FALSE;
141 int ContactCount = 0;
145 ItemIndex = ContactListCtrl->GetNextItem(ItemIndex,
147 wxLIST_STATE_SELECTED);
149 if (ItemIndex == -1){
155 ContactSelected = TRUE;
160 if (MenuType == XABCONTACTMENU_MAIN){
162 std::map<std::string, wxMenuItem*>::iterator MenuItemIter = MenuItems.find("refreshab");
164 if (EnableAccountSettings == FALSE){
165 MenuItemIter->second->Enable(FALSE);
166 MenuItemIter = MenuItems.find("newcontact");
167 MenuItemIter->second->Enable(FALSE);
170 MenuItemIter = MenuItems.find("opencontact");
171 MenuItemIter->second->Enable(ContactSelected);
172 MenuItemIter = MenuItems.find("editcontact");
173 MenuItemIter->second->Enable(ContactSelected);
174 MenuItemIter = MenuItems.find("deletecontact");
175 MenuItemIter->second->Enable(ContactSelected);
177 } else if (MenuType == XABCONTACTMENU_SEARCH){
179 std::map<std::string, wxMenuItem*>::iterator MenuItemIter = MenuItems.find("opencontact");
180 MenuItemIter->second->Enable(ContactSelected);
182 if (SearchModeOnly == TRUE){
184 MenuItemIter = MenuItems.find("editcontact");
185 MenuItemIter->second->Enable(FALSE);
186 MenuItemIter = MenuItems.find("revealcontact");
187 MenuItemIter->second->Enable(FALSE);
191 MenuItemIter = MenuItems.find("editcontact");
192 MenuItemIter->second->Enable(ContactSelected);
193 MenuItemIter = MenuItems.find("revealcontact");
194 MenuItemIter->second->Enable(ContactSelected);
204 void XABContactMenu::ProcessMenuItemClick( wxCommandEvent& event){
206 // Process an action when a menu item in the XABContactMenu
207 // control is selected.
209 int ItemID = event.GetId();
211 if (MenuType == XABCONTACTMENU_MAIN){
215 case ID_CONTACTMENU_OPEN:
217 wxCommandEvent rcevent(CE_OPENCONTACTLIST);
218 wxPostEvent(WindowPtr, rcevent);
221 case ID_CONTACTMENU_NEW:
223 wxCommandEvent rcevent(CE_NEWCONTACT);
224 wxPostEvent(WindowPtr, rcevent);
227 case ID_CONTACTMENU_EDIT:
229 wxCommandEvent rcevent(CE_EDITCONTACT);
230 wxPostEvent(WindowPtr, rcevent);
233 case ID_CONTACTMENU_DELETE:
235 wxCommandEvent rcevent(CE_DELETECONTACT);
236 wxPostEvent(WindowPtr, rcevent);
239 case ID_CONTACTMENU_REFRESHAB:
241 wxCommandEvent rcevent(REFRESHADDRESSBOOK);
242 wxPostEvent(WindowPtr, rcevent);
248 } else if (MenuType == XABCONTACTMENU_SEARCH){
252 case ID_CONTACTMENU_OPEN:
254 wxCommandEvent rcevent(SE_OPENCONTACT);
255 wxPostEvent(WindowPtr, rcevent);
258 case ID_CONTACTMENU_EDIT:
260 wxCommandEvent rcevent(SE_EDITCONTACT);
261 wxPostEvent(WindowPtr, rcevent);
264 case ID_CONTACTMENU_REVEAL:
266 wxCommandEvent rcevent(SE_REVEALCONTACT);
267 wxPostEvent(WindowPtr, rcevent);