1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
//---------------------------------------------------------------------------------------
// LenMus Phonascus: The teacher of music
// Copyright (c) 2002-2012 LenMus project
//
// This program 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,
// either version 3 of the License, or (at your option) any later version.
//
// This program 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 this
// program. If not, see <http://www.gnu.org/licenses/>.
//
// For any comment, suggestion or feature request, please contact the manager of
// the project at cecilios@users.sourceforge.net
//
//---------------------------------------------------------------------------------------
#include "lenmus_dlg_books.h"
#include "lenmus_paths.h"
#include "lenmus_book_reader.h"
#include "lenmus_main_frame.h" //event identifiers
//wxWidgets
#include <wx/wxprec.h>
#include <wx/wx.h>
#include <wx/intl.h>
#include <wx/html/htmlwin.h>
#include <wx/gdicmn.h>
#include <wx/font.h>
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/string.h>
#include <wx/button.h>
#include <wx/sizer.h>
#include <wx/dialog.h>
#include <wx/dir.h>
#include <wx/filename.h>
namespace lenmus
{
//=======================================================================================
// BooksDlg implementation
//=======================================================================================
BEGIN_EVENT_TABLE(BooksDlg, wxDialog)
EVT_BUTTON(wxID_OK, BooksDlg::OnClose)
EVT_HTML_LINK_CLICKED(wxID_ANY, BooksDlg::OnLinkClicked)
END_EVENT_TABLE()
//---------------------------------------------------------------------------------------
BooksDlg::BooksDlg(wxWindow* pParent, ApplicationScope& appScope)
: wxDialog(pParent, wxID_ANY, _("Available music books"),
wxDefaultPosition, wxSize(600,400),
wxDEFAULT_DIALOG_STYLE)
, m_appScope(appScope)
, m_pParent(pParent)
, m_fullName(_T(""))
{
CreateControls();
load_available_books();
CentreOnScreen();
}
//---------------------------------------------------------------------------------------
BooksDlg::~BooksDlg()
{
}
//---------------------------------------------------------------------------------------
void BooksDlg::CreateControls()
{
//AWARE: Code created with wxFormBuilder and copied here.
//Modifications:
// - near line 178: add 'if' to hide Save button
this->SetSizeHints( wxDefaultSize, wxDefaultSize );
wxBoxSizer* pMainSizer;
pMainSizer = LENMUS_NEW wxBoxSizer( wxVERTICAL );
m_pHtmlWnd = LENMUS_NEW wxHtmlWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxHW_SCROLLBAR_AUTO );
pMainSizer->Add( m_pHtmlWnd, 1, wxALL|wxEXPAND, 5 );
wxBoxSizer* pButtonsSizer;
pButtonsSizer = LENMUS_NEW wxBoxSizer( wxHORIZONTAL );
m_pBtnAccept = LENMUS_NEW wxButton( this, wxID_OK, _("Close"), wxDefaultPosition, wxDefaultSize, 0 );
pButtonsSizer->Add( m_pBtnAccept, 0, wxALL, 5 );
pButtonsSizer->Add( 0, 0, 1, wxEXPAND, 5 );
pMainSizer->Add( pButtonsSizer, 0, wxALIGN_CENTER_HORIZONTAL, 5 );
this->SetSizer( pMainSizer );
this->Layout();
}
//---------------------------------------------------------------------------------------
void BooksDlg::OnClose(wxCommandEvent& WXUNUSED(event))
{
EndModal(wxID_CANCEL);
}
//---------------------------------------------------------------------------------------
void BooksDlg::load_available_books()
{
Paths* pPaths = m_appScope.get_paths();
wxString sPath = pPaths->GetBooksPath();
wxString sPattern = _T("*.lmb");
wxString sRead = _("Please read the <a-1>Study guide</a-1> for information about \
the best way to use LenMus Phonascus and the books.");
sRead.Replace(_T("<a-1>"), _T("<a href=\"lenmus#study-guide\">'"));
sRead.Replace(_T("</a-1>"), _T("'</a>"));
wxString sHeader = _T("<html><body>");
wxString sContent = sHeader +
_T("<p>") + sRead +
_T("</p>")
_T("<h3>") + _("Available books:") + _T("</h3><ul>");
wxLogMessage(_T("[BooksDlg::load_available_books] Scanning path <%s>"), sPath.c_str());
wxDir dir(sPath);
if ( !dir.IsOpened() )
{
wxMessageBox( wxString::Format(_("Error when trying to move to folder %s"),
sPath.c_str() ));
wxLogMessage(_T("[BooksDlg::load_available_books] Error when trying to move to folder %s"),
sPath.c_str() );
return;
}
//Scan the books folder and load all books found
wxString filename;
bool fFound = dir.GetFirst(&filename, sPattern, wxDIR_FILES);
while (fFound)
{
wxFileName oFilename(sPath, filename, wxPATH_NATIVE);
wxString name = get_book_name(oFilename);
wxString link = wxString::Format(_T("book-%s"), filename.c_str());
sContent += _T("<li><a href=\"lenmus#")
+ link
+ _T("\">")
+ name
+ _T("</a></li>");
fFound = dir.GetNext(&filename);
}
sContent += _T("</ul></body></html>");
m_pHtmlWnd->SetPage(sContent);
}
//---------------------------------------------------------------------------------------
wxString BooksDlg::get_book_name(wxFileName& oFilename)
{
//create a books collection containing only this book
BooksCollection books;
BookRecord *pRecord = books.add_book(oFilename);
////force to read the TOC file and get book title
//BookRecord *pRecord = books.add_book_toc(oFilename);
return pRecord->GetTitle();
}
//---------------------------------------------------------------------------------------
void BooksDlg::OnLinkClicked(wxHtmlLinkEvent& event)
{
const wxHtmlLinkInfo& link = event.GetLinkInfo();
wxString sLocation = link.GetHref();
//study guide
if (sLocation == _T("lenmus#study-guide"))
{
show_html_document(_T("study-guide.htm"));
return;
}
//verify if it is a LenMus command link
int iPos = sLocation.Find(wxT("lenmus#"));
if (iPos == wxNOT_FOUND)
{
// external link
::wxLaunchDefaultBrowser(sLocation);
}
else
{
wxString filename = sLocation.substr(12);
Paths* pPaths = m_appScope.get_paths();
wxString sPath = pPaths->GetBooksPath();
wxFileName oFile(sPath, filename, wxPATH_NATIVE);
if (!oFile.FileExists())
{
//try to use the english version
sPath = pPaths->GetLocaleRootPath();
oFile.AssignDir(sPath);
oFile.AppendDir(_T("en"));
oFile.AppendDir(_T("books"));
oFile.SetFullName(filename);
if (!oFile.FileExists())
{
wxMessageBox(_("Sorry: File not found!"));
wxLogMessage(_T("[BooksDlg::OnLinkClicked] File '%s' not found!"),
oFile.GetFullPath().c_str() );
return;
}
}
m_fullName = oFile.GetFullPath();
EndModal(wxID_OK);
}
}
//---------------------------------------------------------------------------------------
void BooksDlg::show_html_document(const wxString& sDocName)
{
Paths* pPaths = m_appScope.get_paths();
wxString sPath = pPaths->GetLocalePath();
wxFileName oFile(sPath, sDocName, wxPATH_NATIVE);
if (!oFile.FileExists())
{
//try to display the english version
sPath = pPaths->GetLocaleRootPath();
oFile.AssignDir(sPath);
oFile.AppendDir(_T("en"));
oFile.SetFullName(sDocName);
if (!oFile.FileExists())
{
wxMessageBox(_("Sorry: File not found!"));
wxLogMessage(_T("[BooksDlg::ShowHtmlDocument] File %s' not found!"),
oFile.GetFullPath().c_str() );
return;
}
}
::wxLaunchDefaultBrowser( oFile.GetFullPath() );
}
} // namespace lenmus
|