Code::Blocks  SVN r11506
recentitemslist.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3  * http://www.gnu.org/licenses/gpl-3.0.html
4  *
5  * $Revision: 9263 $
6  * $Id: recentitemslist.cpp 9263 2013-08-17 09:20:28Z mortenmacfly $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/src/recentitemslist.cpp $
8  */
9 
10 
11 #include <sdk.h>
12 
13 #ifndef CB_PRECOMP
14  #include <wx/menu.h>
15  #include "configmanager.h"
16  #include "editormanager.h"
17 #endif
18 
19 #include <wx/docview.h>
20 
21 #include "recentitemslist.h"
22 
23 #include "annoyingdialog.h"
24 #include "startherepage.h"
25 
26 RecentItemsList::RecentItemsList(const wxString &menuName, const wxString &configPath, int menuID, int firstMenuItemID) :
27  m_list(nullptr),
28  m_menuName(menuName),
29  m_configPath(configPath),
30  m_menuID(menuID),
31  m_firstMenuItemID(firstMenuItemID)
32 {
33 }
34 
36 {
37  wxString filename = FileName;
38 #ifdef __WXMSW__
39  // for windows, look for case-insensitive matches
40  // if found, don't add it
41  wxString low = filename.Lower();
42  for (size_t i = 0; i < m_list->GetCount(); ++i)
43  {
44  if (low == m_list->GetHistoryFile(i).Lower())
45  { // it exists, set filename to the existing name, so it can become
46  // the most recent one
47  filename = m_list->GetHistoryFile(i);
48  break;
49  }
50  }
51 #endif
52 
53  m_list->AddFileToHistory(filename);
54 
55  // because we append "clear history" menu to the end of the list,
56  // each time we must add a history item we have to:
57  // a) remove "Clear history" (Biplab#1: Don't remove or you'll loose icon)
58  // b) clear the menu (Biplab#1: except the last item)
59  // c) fill it with the history items (Biplab#1: by inserting them)
60  // and d) append "Clear history"... (Biplab#1: Not needed, item has not been removed)
61  wxMenu* recentFiles = GetMenu();
62  if (recentFiles)
63  {
64  ClearMenu(recentFiles);
65  BuildMenu(recentFiles);
66  }
68 }
69 
71 {
72  return (m_list && id < m_list->GetCount()) ? m_list->GetHistoryFile(id) : wxString(wxEmptyString);
73 }
74 
75 
76 void RecentItemsList::AskToRemoveFileFromHistory(size_t id, bool cannot_open)
77 {
78  if (!m_list || id >= m_list->GetCount())
79  return;
80 
81  wxString question(_("Do you want to remove it from the recent files list?"));
82  wxString query(wxEmptyString);
83  if (cannot_open)
84  {
85  query << _("The file cannot be opened (probably it's not available anymore).")
86  << _T("\n") << question;
87  }
88  else
89  query << question;
90 
91  AnnoyingDialog dialog(_("Remove file from list"), query, wxART_QUESTION);
92  PlaceWindow(&dialog);
93  if (dialog.ShowModal() == AnnoyingDialog::rtYES)
94  {
96  wxMenu* recentFiles = GetMenu();
97  if (recentFiles)
98  {
99  ClearMenu(recentFiles);
100  BuildMenu(recentFiles);
101  }
103  }
104 }
105 
107 {
108  while (m_list->GetCount())
111 
112  Initialize();
114 }
115 
117 {
119 
121 
122  wxMenu* recentFiles = GetMenu();
123  if (recentFiles)
124  {
126  for (int i = (int)files.GetCount() - 1; i >= 0; --i)
127  {
128  if (wxFileExists(files[i]))
129  m_list->AddFileToHistory(files[i]);
130  }
131  BuildMenu(recentFiles);
132  }
133 }
134 
135 
137 {
138  if (m_list)
139  {
140  wxArrayString files;
141  for (unsigned int i = 0; i < m_list->GetCount(); ++i)
142  files.Add(m_list->GetHistoryFile(i));
143  Manager::Get()->GetConfigManager(_T("app"))->Write(m_configPath, files);
144 
145  wxMenu* recentFiles = GetMenu();
146  if (recentFiles)
147  {
149  ClearMenu(recentFiles);
150  else
151  m_list->RemoveMenu(recentFiles);
152  }
153  delete m_list;
154  m_list = nullptr;
155  }
156 }
157 
159 {
160  if (m_list->GetCount() > 0)
161  {
162  menu->InsertSeparator(0);
163  for (size_t i = 0; i < m_list->GetCount(); ++i)
164  {
165  const wxString &name = wxString::Format(_T("&%lu "), static_cast<unsigned long>(i + 1))
166  + m_list->GetHistoryFile(i);
167  menu->Insert(menu->GetMenuItemCount() - 2, m_firstMenuItemID + i, name);
168  }
169  }
170 }
171 
173 {
174  while (menu->GetMenuItemCount() > 1)
175  menu->Delete(menu->GetMenuItems()[0]);
176 }
177 
179 {
180  wxMenuBar *mbar = Manager::Get()->GetAppFrame()->GetMenuBar();
181  if (!mbar)
182  return nullptr;
183  int pos = mbar->FindMenu(m_menuName);
184  if (pos == wxNOT_FOUND)
185  return nullptr;
186  wxMenu *menu = mbar->GetMenu(pos);
187  wxMenu *recentFiles;
188  menu->FindItem(m_menuID, &recentFiles);
189  return recentFiles;
190 }
191 
193 {
194  // update start here page
196  if (sh)
197  ((StartHerePage*)sh)->Reload();
198 }
void ClearMenu(wxMenu *menu)
ConfigManager * GetConfigManager(const wxString &name_space) const
Definition: manager.cpp:474
static Manager * Get()
Use Manager::Get() to get a pointer to its instance Manager::Get() is guaranteed to never return an i...
Definition: manager.cpp:182
wxMenuItem * InsertSeparator(size_t pos)
wxString Lower() const
virtual size_t GetCount() const
static bool IsAppShuttingDown()
Definition: manager.cpp:333
bool wxFileExists(const wxString &filename)
void BuildMenu(wxMenu *menu)
#define _T(string)
wxString m_configPath
virtual wxString GetHistoryFile(size_t index) const
#define wxNOT_FOUND
size_t GetMenuItemCount() const
wxArrayString ReadArrayString(const wxString &name)
EditorManager * GetEditorManager() const
Definition: manager.cpp:434
void Write(const wxString &name, const wxString &value, bool ignoreEmpty=false)
wxArtID wxART_QUESTION
null_pointer_t nullptr
Definition: nullptr.cpp:16
wxFrame * GetAppFrame() const
Definition: manager.cpp:419
RecentItemsList(const wxString &menuName, const wxString &configPath, int menuID, int firstMenuItemID)
Base class that all "editors" should inherit from.
Definition: editorbase.h:30
bool Delete(int id)
wxMenu * GetMenu(size_t menuIndex) const
wxString wxEmptyString
const wxString g_StartHereTitle
const wxString & _(const wxString &string)
int FindMenu(const wxString &title) const
EditorBase * GetEditor(int index)
virtual int FindItem(const wxString &itemString) const
void DeleteSubPath(const wxString &strPath)
DLLIMPORT void PlaceWindow(wxTopLevelWindow *w, cbPlaceDialogMode mode=pdlBest, bool enforce=false)
Definition: globals.cpp:1177
void AskToRemoveFileFromHistory(size_t id, bool cannot_open=true)
virtual void RemoveMenu(wxMenu *menu)
int ShowModal() override
size_t Add(const wxString &str, size_t copies=1)
size_t GetCount() const
wxString GetHistoryFile(size_t id) const
void AddToHistory(const wxString &FileName)
wxFileHistory * m_list
wxMenuItemList & GetMenuItems()
static wxString Format(const wxString &format,...)
Dialog that contains a "Don&#39;t annoy me" checkbox.
virtual void AddFileToHistory(const wxString &filename)
wxMenuItem * Insert(size_t pos, wxMenuItem *menuItem)
virtual void RemoveFileFromHistory(size_t i)