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"
27 ID_CONTACTMENU_DELETE,
28 ID_CONTACTMENU_REFRESHAB,
32 XABContactMenu::XABContactMenu(int MenuTypeIn){
34 // Setup the XABContactMenu control.
36 MenuType = MenuTypeIn;
38 // Setup the menu items.
40 AppendMenuItem(wxT("opencontact"), _("Open Contact..."), _(""), ID_CONTACTMENU_OPEN, wxITEM_NORMAL);
41 AppendMenuItem(wxT("sep1"), _(""), _(""), wxID_ANY, wxITEM_SEPARATOR);
43 if (MenuType == XABCONTACTMENU_MAIN){
45 // Setup up the add contact option if in the main window.
47 AppendMenuItem(wxT("newcontact"), _("New Contact..."), _(""), ID_CONTACTMENU_NEW, wxITEM_NORMAL);
51 AppendMenuItem(wxT("editcontact"), _("Edit Contact..."), _(""), ID_CONTACTMENU_EDIT, wxITEM_NORMAL);
53 if (MenuType == XABCONTACTMENU_SEARCH){
55 // Setup up the reveal contact option if in the search window.
57 AppendMenuItem(wxT("revealcontact"), _("Reveal Contact..."), _(""), ID_CONTACTMENU_REVEAL, wxITEM_NORMAL);
61 if (MenuType == XABCONTACTMENU_MAIN){
63 // Setup the delete and refresh contact options if in the main window.
65 AppendMenuItem(wxT("deletecontact"), _("Delete Contact..."), _(""), ID_CONTACTMENU_DELETE, wxITEM_NORMAL);
66 AppendMenuItem(wxT("sep2"), _(""), _(""), wxID_ANY, wxITEM_SEPARATOR);
67 AppendMenuItem(wxT("refreshab"), _("Refresh Address Book"), _(""), ID_CONTACTMENU_REFRESHAB, wxITEM_NORMAL);
73 XABContactMenu::~XABContactMenu(){
75 // Destory the XABContactMenu object.
77 // Delete the menu items.
79 for (std::map<std::string, wxMenuItem*>::iterator MenuItemIter = MenuItems.begin();
80 MenuItemIter != MenuItems.end(); ++MenuItemIter){
82 // Delete the wxMenuItem object.
84 this->Disconnect(MenuItemIter->second->GetId(), wxEVT_COMMAND_MENU_SELECTED,
85 wxCommandEventHandler(XABContactMenu::ProcessMenuItemClick) );
86 delete(MenuItemIter->second);
87 MenuItemIter->second = NULL;
95 void XABContactMenu::AppendMenuItem(wxString ObjectName,
97 wxString MenuDescription,
101 // Append a menu item to the XABContactMenu control.
103 wxMenuItem *menuitem = new wxMenuItem(
112 this->Connect(menuitem->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(XABContactMenu::ProcessMenuItemClick));
113 this->Append(menuitem);
114 MenuItems.insert(std::make_pair(ObjectName.ToStdString(), menuitem));
118 void XABContactMenu::SetupPointersSearch(wxWindow* WindowPtrIn,
119 wxListCtrl* ContactListCtrlIn,
120 bool SearchModeOnlyIn){
122 // Setup the pointers when being used in the search window.
124 WindowPtr = WindowPtrIn;
125 ContactListCtrl = ContactListCtrlIn;
126 SearchModeOnly = SearchModeOnlyIn;
130 void XABContactMenu::SetupPointers(wxWindow* WindowPtrIn,
131 wxListCtrl* ContactListCtrlIn,
132 bool EnableAccountSettingsIn){
134 // Setup the pointers.
136 WindowPtr = WindowPtrIn;
137 ContactListCtrl = ContactListCtrlIn;
138 EnableAccountSettings = EnableAccountSettingsIn;
142 wxMenu* XABContactMenu::MenuPointer(){
144 // Process the menu pointer.
146 // Check for the following before passing the pointer:
147 // If an account has been selected.
148 // - Disable Refresh Address Book if not.
149 // If one or more contacts have been selected.
150 // - Disable New, Edit, Delete and Reveal contact as needed.
152 bool ContactSelected = FALSE;
154 int ContactCount = 0;
158 ItemIndex = ContactListCtrl->GetNextItem(ItemIndex,
160 wxLIST_STATE_SELECTED);
162 if (ItemIndex == -1){
168 ContactSelected = TRUE;
173 if (MenuType == XABCONTACTMENU_MAIN){
175 std::map<std::string, wxMenuItem*>::iterator MenuItemIter = MenuItems.find("refreshab");
177 if (EnableAccountSettings == FALSE){
178 MenuItemIter->second->Enable(FALSE);
179 MenuItemIter = MenuItems.find("newcontact");
180 MenuItemIter->second->Enable(FALSE);
183 MenuItemIter = MenuItems.find("opencontact");
184 MenuItemIter->second->Enable(ContactSelected);
185 MenuItemIter = MenuItems.find("editcontact");
186 MenuItemIter->second->Enable(ContactSelected);
187 MenuItemIter = MenuItems.find("deletecontact");
188 MenuItemIter->second->Enable(ContactSelected);
190 } else if (MenuType == XABCONTACTMENU_SEARCH){
192 std::map<std::string, wxMenuItem*>::iterator MenuItemIter = MenuItems.find("opencontact");
193 MenuItemIter->second->Enable(ContactSelected);
195 if (SearchModeOnly == TRUE){
197 MenuItemIter = MenuItems.find("editcontact");
198 MenuItemIter->second->Enable(FALSE);
199 MenuItemIter = MenuItems.find("revealcontact");
200 MenuItemIter->second->Enable(FALSE);
204 MenuItemIter = MenuItems.find("editcontact");
205 MenuItemIter->second->Enable(ContactSelected);
206 MenuItemIter = MenuItems.find("revealcontact");
207 MenuItemIter->second->Enable(ContactSelected);
217 void XABContactMenu::ProcessMenuItemClick( wxCommandEvent& event){
219 // Process an action when a menu item in the XABContactMenu
220 // control is selected.
222 int ItemID = event.GetId();
224 if (MenuType == XABCONTACTMENU_MAIN){
228 case ID_CONTACTMENU_OPEN:
230 wxCommandEvent rcevent(CE_OPENCONTACTLIST);
231 wxPostEvent(WindowPtr, rcevent);
234 case ID_CONTACTMENU_NEW:
236 wxCommandEvent rcevent(CE_NEWCONTACT);
237 wxPostEvent(WindowPtr, rcevent);
240 case ID_CONTACTMENU_EDIT:
242 wxCommandEvent rcevent(CE_EDITCONTACT);
243 wxPostEvent(WindowPtr, rcevent);
246 case ID_CONTACTMENU_DELETE:
248 wxCommandEvent rcevent(CE_DELETECONTACT);
249 wxPostEvent(WindowPtr, rcevent);
252 case ID_CONTACTMENU_REFRESHAB:
254 wxCommandEvent rcevent(REFRESHADDRESSBOOK);
255 wxPostEvent(WindowPtr, rcevent);
261 } else if (MenuType == XABCONTACTMENU_SEARCH){
265 case ID_CONTACTMENU_OPEN:
267 wxCommandEvent rcevent(SE_OPENCONTACT);
268 wxPostEvent(WindowPtr, rcevent);
271 case ID_CONTACTMENU_EDIT:
273 wxCommandEvent rcevent(SE_EDITCONTACT);
274 wxPostEvent(WindowPtr, rcevent);
277 case ID_CONTACTMENU_REVEAL:
279 wxCommandEvent rcevent(SE_REVEALCONTACT);
280 wxPostEvent(WindowPtr, rcevent);