Code::Blocks  SVN r11506
startherepage.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: 11399 $
6  * $Id: startherepage.cpp 11399 2018-05-08 21:54:03Z fuscated $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/src/startherepage.cpp $
8  */
9 
10 #include <sdk.h>
11 
12 #ifndef CB_PRECOMP
13  #include <wx/dataobj.h>
14  #include <wx/intl.h>
15  #include <wx/utils.h>
16  #include <wx/sizer.h>
17  #include <wx/settings.h>
18 
19  #include <manager.h>
20  #include <logmanager.h>
21  #include <projectmanager.h>
22  #include <templatemanager.h>
23  #include <pluginmanager.h>
24  #include <editormanager.h>
25  #include <configmanager.h>
26 #endif
27 
28 #include "startherepage.h"
29 #include "main.h"
30 #include "appglobals.h"
31 #include "cbcolourmanager.h"
32 #include "recentitemslist.h"
33 
34 #include <wx/clipbrd.h>
35 #include <wx/docview.h>
36 #include <wx/wxhtml.h>
37 
38 const wxString g_StartHereTitle = _("Start here");
39 int idWin = wxNewId();
40 
41 class MyHtmlWin : public wxHtmlWindow
42 {
43  public:
44  MyHtmlWin(StartHerePage* parent, int id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxHW_SCROLLBAR_AUTO)
45  : wxHtmlWindow(parent, id, pos, size, style),
46  m_pOwner(parent)
47  {
48  }
49 
50  void OnLinkClicked(const wxHtmlLinkInfo& link) override
51  {
52  if (m_pOwner)
53  {
54  if (!m_pOwner->LinkClicked(link))
56  }
57  }
58  private:
60 };
61 
62 BEGIN_EVENT_TABLE(StartHerePage, EditorBase)
63 END_EVENT_TABLE()
64 
65 namespace
66 {
67 
68 wxString wrapText(const wxString &text, const wxString &textColour)
69 {
70  return wxT("<font color=\"") + textColour + wxT("\">") + text + wxT("</font>");
71 }
72 
73 void ReplaceRecentProjectFiles(wxString &buf, const wxFileHistory &projects, const wxFileHistory &files,
74  const wxString &linkColour, const wxString &textColour)
75 {
76  // replace history vars
77  wxString links;
78 
79  links << _T("<table>\n<tr><td colspan=\"2\"><b>");
80  links << wrapText(_("Recent projects"), textColour);
81  links << _T("</b></td></tr>\n");
82  if (projects.GetCount())
83  {
84  for (size_t i = 0; i < projects.GetCount(); ++i)
85  {
86  links << _T("<tr><td width=\"50\"><img alt=\"\" width=\"20\" src=\"blank.png\" />");
87  links << wxString::Format(_T("<a href=\"CB_CMD_DELETE_HISTORY_PROJECT_%lu\"><img alt=\"\" src=\"trash_16x16.png\" /></a>"),
88  static_cast<unsigned long>(i + 1));
89  links << _T("<img alt=\"\" width=\"10\" src=\"blank.png\" /></td><td width=\"10\">");
90  links << _T("<a href=\"CB_CMD_OPEN_HISTORY_PROJECT_") << (i + 1) << _T("\">")
91  << _T("<font color=\"") << linkColour << _T("\">") << projects.GetHistoryFile(i)
92  << _T("</font></a>");
93  links << _T("</td></tr>\n");
94  }
95  }
96  else
97  {
98  links << _T("<tr><td style=\"width:2em;\"></td><td>&nbsp;&nbsp;&nbsp;&nbsp;");
99  links << wrapText(_("No recent projects"), textColour);
100  links << _T("</td></tr>\n");
101  }
102 
103  links << _T("</table>\n<table>\n<tr><td colspan=\"2\"><b>");
104  links << wrapText(_("Recent files"), textColour);
105  links <<_T("</b></td></tr>\n");
106  if (files.GetCount())
107  {
108  for (size_t i = 0; i < files.GetCount(); ++i)
109  {
110  links << _T("<tr><td width=\"50\"><img alt=\"\" width=\"20\" src=\"blank.png\" />");
111  links << wxString::Format(_T("<a href=\"CB_CMD_DELETE_HISTORY_FILE_%lu\"><img alt=\"\" src=\"trash_16x16.png\" /></a>"),
112  static_cast<unsigned long>(i + 1));
113  links << _T("<img alt=\"\" width=\"10\" src=\"blank.png\" /></td><td width=\"10\">");
114  links << _T("<a href=\"CB_CMD_OPEN_HISTORY_FILE_") << (i + 1) << _T("\">")
115  << _T("<font color=\"") << linkColour << _T("\">") << files.GetHistoryFile(i) << _T("</font></a>");
116  links << _T("</td></tr>\n");
117  }
118  }
119  else
120  {
121  links << _T("<tr><td style=\"width:2em;\"></td><td>&nbsp;&nbsp;&nbsp;&nbsp;");
122  links << wrapText(_("No recent files"), textColour);
123  links << _T("</td></tr>\n");
124  }
125 
126  links << _T("</table>\n");
127 
128 
129  // update page
130  buf.Replace(_T("CB_VAR_RECENT_FILES_AND_PROJECTS"), links);
131  buf.Replace(_T("CB_TXT_NEW_PROJECT"), _("Create a new project"));
132  buf.Replace(_T("CB_TXT_OPEN_PROJECT"), _("Open an existing project"));
133  buf.Replace(_T("CB_TXT_VISIT_FORUMS"), _("Visit the Code::Blocks forums"));
134  buf.Replace(_T("CB_TXT_BUG_FEATURE"), _("Report a bug or request a new feature"));
135  buf.Replace(_T("CB_TXT_TIP_OF_THE_DAY"), _("Tip of the Day"));
136 }
137 
138 } // anonymous namespace
139 
141  const RecentItemsList &files, wxWindow* parent)
142  : EditorBase(parent, g_StartHereTitle),
143  m_pOwner(owner),
144  m_projects(projects),
145  m_files(files)
146 {
147  RegisterColours();
148 
149  //ctor
150  wxBoxSizer* bs = new wxBoxSizer(wxVERTICAL);
151 
153 
154  // avoid gtk-critical because of sizes less than -1 (can happen with wxAuiNotebook/cbAuiNotebook)
155  wxSize size = GetSize();
156  size.x = std::max(size.x, -1);
157  size.y = std::max(size.y, -1);
158 
159  m_pWin = new MyHtmlWin(this, idWin, wxPoint(0,0), size);
160 
161  // set default font sizes based on system default font size
162 
163  /* NOTE (mandrav#1#): wxWidgets documentation on wxHtmlWindow::SetFonts(),
164  states that the sizes array accepts values from -2 to +4.
165  My tests (under linux at least) have showed that it actually
166  expects real point sizes. */
167 
169  int sizes[7] = {};
170  for (int i = 0; i < 7; ++i)
171  sizes[i] = systemFont.GetPointSize();
172  m_pWin->SetFonts(wxEmptyString, wxEmptyString, &sizes[0]);
173 
174  // must load the page this way because if we don't the image can't be found...
175  m_pWin->LoadPage(resPath + _T("/start_here.zip#zip:start_here.html"));
176 
177  // alternate way to read the file so we can perform some search and replace
178  // the C::B image referenced in the default start page can be found now
179  // because we used LoadPage() above...
180  wxString buf;
181  wxFileSystem* fs = new wxFileSystem;
182  wxFSFile* f = fs->OpenFile(resPath + _T("/start_here.zip#zip:start_here.html"));
183  if (f)
184  {
185  wxInputStream* is = f->GetStream();
186  char tmp[1024] = {};
187  while (!is->Eof() && is->CanRead())
188  {
189  memset(tmp, 0, sizeof(tmp));
190  is->Read(tmp, sizeof(tmp) - 1);
191  buf << cbC2U((const char*)tmp);
192  }
193  delete f;
194  }
195  else
196  buf = _("<html><body><h1>Welcome to Code::Blocks!</h1><br>The default start page seems to be missing...</body></html>");
197  delete fs;
198 
199  #if defined(_LP64) || defined(_WIN64)
200  const int bit_type = 64;
201  #else
202  const int bit_type = 32;
203  #endif
204 
205  #ifdef __GNUC__
206  revInfo.Printf(_T("%s (%s) gcc %d.%d.%d %s/%s - %d bit"),
208  __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__, appglobals::AppPlatform.c_str(), appglobals::AppWXAnsiUnicode.c_str(), bit_type);
209  #else
210  revInfo.Printf(_T("%s (%s) %s/%s"),
213  #endif
214  // perform var substitution
215  buf.Replace(_T("CB_VAR_REVISION_INFO"), revInfo);
216  buf.Replace(_T("CB_VAR_VERSION_VERB"), appglobals::AppActualVersionVerb);
217  buf.Replace(_T("CB_VAR_VERSION"), appglobals::AppActualVersion);
218  buf.Replace(_T("CB_SAFE_MODE"), PluginManager::GetSafeMode() ? _("SAFE MODE") : _T(""));
219 
220  m_OriginalPageContent = buf; // keep a copy of original for Reload()
221  Reload();
222 
223  bs->Add(m_pWin, 1, wxEXPAND);
224  SetSizer(bs);
225  SetAutoLayout(true);
226 }
227 
229 {
230  //dtor
231  //m_pWin->Destroy();
232 }
233 
235 {
236  static bool inited = false;
237  if (inited)
238  return;
239  inited = true;
240 
242  cm->RegisterColour(_("Start here page"), _("Background colour"), wxT("start_here_background"),
244  cm->RegisterColour(_("Start here page"), _("Link colour"), wxT("start_here_link"), *wxBLUE);
245  cm->RegisterColour(_("Start here page"), _("Text colour"), wxT("start_here_text"),
247 }
248 
250 {
251  // Called every time something in the history changes.
253 
255  const wxString &backgroundColour = cm->GetColour(wxT("start_here_background")).GetAsString(wxC2S_HTML_SYNTAX);
256  const wxString &linkColour = cm->GetColour(wxT("start_here_link")).GetAsString(wxC2S_HTML_SYNTAX);
257  const wxString &textColour = cm->GetColour(wxT("start_here_text")).GetAsString(wxC2S_HTML_SYNTAX);
258 
259  ReplaceRecentProjectFiles(buf, *m_projects.GetFileHistory(), *m_files.GetFileHistory(), linkColour, textColour);
260 
261  buf.Replace(wxT("CB_BODY_BGCOLOUR"), backgroundColour);
262  buf.Replace(wxT("CB_LINK_COLOUR"), linkColour);
263  buf.Replace(wxT("CB_TEXT_COLOUR"), textColour);
264 
265  int x, y;
266  m_pWin->GetViewStart(&x, &y);
267  m_pWin->SetPage(buf);
268  m_pWin->Scroll(x, y);
269 }
270 
272 {
273  //If it's already loading something, stop here
274  if (Manager::Get()->GetProjectManager()->IsLoading())
275  return true;
276  if (!m_pOwner)
277  return true;
278 
279  wxString href = link.GetHref();
280  if (href.StartsWith(_T("CB_CMD_")))
281  {
282  wxCommandEvent evt(wxEVT_COMMAND_MENU_SELECTED, idStartHerePageLink);
283  evt.SetString(link.GetHref());
284  wxPostEvent(m_pOwner, evt);
285  return true;
286  }
287 
288  if ( href.IsSameAs(_T("http://www.codeblocks.org/"))
289  || href.StartsWith(_T("https://sourceforge.net/p/codeblocks/tickets")) )
290  {
292  if (wxTheClipboard->Open())
293  {
294  wxTheClipboard->SetData(data);
295  wxTheClipboard->Close();
296  }
297  }
298 
299  if(href.IsSameAs(_T("rev")))
300  {
302  if (wxTheClipboard->Open())
303  {
304  wxTheClipboard->SetData(data);
305  wxTheClipboard->Close();
306  }
307  return true;
308  }
309 
310  return false;
311 }
static wxString GetSvnDate()
static bool GetSafeMode()
const wxString AppPlatform
Definition: appglobals.cpp:43
int wxNewId()
void RegisterColours()
static wxFont GetFont(wxSystemFont index)
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
virtual size_t GetCount() const
void RegisterColour(const wxString &category, const wxString &name, const wxString &id, const wxColour &defaultColour)
const RecentItemsList & m_files
Definition: startherepage.h:38
const wxString AppActualVersionVerb
Definition: appglobals.cpp:26
const wxString bit_type
Definition: appglobals.cpp:63
int idWin
#define _T(string)
MyHtmlWin(StartHerePage *parent, int id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxHW_SCROLLBAR_AUTO)
#define wxTheClipboard
wxString m_OriginalPageContent
Definition: startherepage.h:36
virtual wxString GetHistoryFile(size_t index) const
#define wxT(string)
wxFSFile * OpenFile(const wxString &location, int flags=wxFS_READ)
const wxFileHistory * GetFileHistory() const
void wxPostEvent(wxEvtHandler *dest, const wxEvent &event)
const wxString AppActualVersion
Definition: appglobals.cpp:27
wxColour GetColour(const wxString &id) const
DLLIMPORT wxString cbC2U(const char *str)
Return str as a proper unicode-compatible string.
Definition: globals.cpp:733
wxSizerItem * Add(wxWindow *window, const wxSizerFlags &flags)
size_t Replace(const wxString &strOld, const wxString &strNew, bool replaceAll=true)
const wxString AppWXAnsiUnicode
Definition: appglobals.cpp:58
bool IsSameAs(const wxString &s, bool caseSensitive=true) const
Base class that all "editors" should inherit from.
Definition: editorbase.h:30
static wxColour GetColour(wxSystemColour index)
wxString revInfo
Definition: startherepage.h:21
wxString wxEmptyString
const wxString & _(const wxString &string)
ColourManager * GetColourManager() const
Definition: manager.cpp:489
StartHerePage(wxEvtHandler *owner, const RecentItemsList &projects, const RecentItemsList &files, wxWindow *parent)
StartHerePage * m_pOwner
wxHtmlWindow * m_pWin
Definition: startherepage.h:35
bool LinkClicked(const wxHtmlLinkInfo &link)
wxColour * wxBLUE
void OnLinkClicked(const wxHtmlLinkInfo &link) override
virtual int GetPointSize() const
static wxString ReadDataPath()
bool StartsWith(const wxString &prefix, wxString *rest=NULL) const
wxInputStream * GetStream() const
virtual ~StartHerePage()
bool wxLaunchDefaultBrowser(const wxString &url, int flags=0)
wxEvtHandler * m_pOwner
Definition: startherepage.h:34
int Printf(const wxString &pszFormat,...)
virtual wxString GetAsString(long flags=wxC2S_NAME|wxC2S_CSS_SYNTAX) const
static wxString Format(const wxString &format,...)
const RecentItemsList & m_projects
Definition: startherepage.h:37