~s-cecilio/lenmus/v5.3

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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//---------------------------------------------------------------------------------------
//    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
//
//---------------------------------------------------------------------------------------

//lenmus
#include "lenmus_document_frame.h"

#include "lenmus_canvas.h"
#include "lenmus_document_canvas.h"
#include "lenmus_string.h"
#include "lenmus_midi_server.h"
#include "lenmus_dyncontrol.h"
#include "lenmus_standard_header.h"
#include "lenmus_book_reader.h"
#include "lenmus_content_box.h"
#include "lenmus_content_box_ctrol.h"
#include "lenmus_zip_reader.h"

//lomse
#include <lomse_shapes.h>

//wxWidgets
#include <wx/filename.h>


//toc
#include <wx/textctrl.h>
#include <wx/notebook.h>
#include <wx/imaglist.h>
#include <wx/treectrl.h>
#include <wx/tokenzr.h>
#include <wx/wfstream.h>
#include <wx/html/htmlwin.h>
#include <wx/busyinfo.h>
#include <wx/progdlg.h>
#include <wx/toolbar.h>
#include <wx/fontenum.h>
#include <wx/stream.h>
#include <wx/filedlg.h>
#include <wx/artprov.h>
#include <wx/spinctrl.h>
#include <wx/dynarray.h>
#include <wx/choicdlg.h>
#include <wx/settings.h>



namespace lenmus
{
//--------------------------------------------------------------------------
// TextBookHelpTreeItemData (private)
//--------------------------------------------------------------------------

class TextBookHelpTreeItemData : public wxTreeItemData
{
    public:
        TextBookHelpTreeItemData(int id) : wxTreeItemData()
            { m_Id = id;}

        int m_Id;
};


//--------------------------------------------------------------------------
// TextBookHelpHashData (private)
//--------------------------------------------------------------------------

class TextBookHelpHashData : public wxObject
{
    public:
        TextBookHelpHashData(int index, long id) : wxObject()
            { m_Index = index; m_Id = id;}
        ~TextBookHelpHashData() {}

        int m_Index;
        long m_Id;
};


//---------------------------------------------------------------------------
// DocumentFrame::m_mergedIndex
//---------------------------------------------------------------------------

WX_DEFINE_ARRAY_PTR(const BookIndexItem*, wxHtmlHelpDataItemPtrArray);

struct TextBookHelpMergedIndexItem
{
    TextBookHelpMergedIndexItem *parent;
    wxString                   name;
    wxHtmlHelpDataItemPtrArray items;
};

WX_DECLARE_OBJARRAY(TextBookHelpMergedIndexItem, TextBookHelpMergedIndex);
#include <wx/arrimpl.cpp>
WX_DEFINE_OBJARRAY(TextBookHelpMergedIndex)

//void DocumentFrame::UpdateMergedIndex()
//{
//    // Updates "merged index" structure that combines indexes of all books
//    // into better searchable structure
//
//    delete m_mergedIndex;
//    m_mergedIndex = LENMUS_NEW TextBookHelpMergedIndex;
//    TextBookHelpMergedIndex& merged = *m_mergedIndex;
//
//    const BookIndexArray& items = m_pBookReader->GetIndexArray();
//    size_t len = items.size();
//
//    TextBookHelpMergedIndexItem *history[128] = {NULL};
//
//    for (size_t i = 0; i < len; i++)
//    {
//        const BookIndexItem* pItem = items[i];
//        wxASSERT_MSG( pItem->level < 128, _T("nested index entries too deep") );
//
//        if (history[pItem->level] &&
//            history[pItem->level]->items[0]->title == pItem->title)
//        {
//            // same index entry as previous one, update list of items
//            history[pItem->level]->items.Add(pItem);
//        }
//        else
//        {
//            // LENMUS_NEW index entry
//            TextBookHelpMergedIndexItem *mi = LENMUS_NEW TextBookHelpMergedIndexItem();
//            mi->name = pItem->GetIndentedName();
//            mi->items.Add(pItem);
//            mi->parent = (pItem->level == 0) ? NULL : history[pItem->level - 1];
//            history[pItem->level] = mi;
//            merged.Add(mi);
//        }
//    }
//}

//=======================================================================================
// DocumentLoader implementation
//=======================================================================================
DocumentLoader::DocumentLoader(ContentWindow* parent, ApplicationScope& appScope,
                               LomseDoorway& lomse)
    : m_pContentWindow(parent)
    , m_appScope(appScope)
    , m_lomse(lomse)
{
}

//---------------------------------------------------------------------------------------
CanvasInterface* DocumentLoader::create_canvas(const string& filename, int viewType)
{
    //use filename (without path) as page title
    wxFileName document( to_wx_string(filename) );
    bool fIsBook = (document.GetExt().Upper() == _T("LMB"));

    if (fIsBook)
    {
        DocumentFrame* pCanvas = LENMUS_NEW DocumentFrame(m_pContentWindow, m_appScope, m_lomse);
        m_pContentWindow->add_canvas(pCanvas, document.GetName());
        pCanvas->display_document(filename, viewType);
        return pCanvas;
    }
    else
    {
        DocumentCanvas* pCanvas = LENMUS_NEW DocumentCanvas(m_pContentWindow, m_appScope, m_lomse);
        m_pContentWindow->add_canvas(pCanvas, document.GetName());
        pCanvas->display_document(filename, viewType);
        return pCanvas;
    }
}


//=======================================================================================
// DocumentFrame implementation
//=======================================================================================

BEGIN_EVENT_TABLE(DocumentFrame, wxSplitterWindow)
    EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY, DocumentFrame::on_splitter_moved)
    LM_EVT_PAGE_REQUEST(DocumentFrame::on_page_change_requested)
END_EVENT_TABLE()

DocumentFrame::DocumentFrame(ContentWindow* parent, ApplicationScope& appScope,
                             LomseDoorway& lomse)
    : wxSplitterWindow(parent, wxNewId(), wxDefaultPosition,
                       wxDefaultSize, 0, _T("Canvas"))
    , CanvasInterface(parent)
    , m_appScope(appScope)
    , m_lomse(lomse)
    , m_left(NULL)
    , m_right(NULL)
    , m_pBookReader(NULL)
{
}

//---------------------------------------------------------------------------------------
DocumentFrame::~DocumentFrame()
{
    delete m_pBookReader;
    delete m_left;
    delete m_right;
}

////---------------------------------------------------------------------------------------
//void DocumentFrame::on_show_toc(wxCommandEvent& WXUNUSED(event) )
//{
//    if (IsSplit())
//        Unsplit();
//    m_left->Show(true);
//    m_right->Show(true);
//    SplitVertically(m_left, m_right, 100);
//}
//
////---------------------------------------------------------------------------------------
//void DocumentFrame::on_hide_toc(wxCommandEvent& WXUNUSED(event) )
//{
//    if (IsSplit())
//        Unsplit();
//}

//---------------------------------------------------------------------------------------
void DocumentFrame::on_page_change_requested(PageRequestEvent& event)
{
    wxString url = to_wx_string(event.get_url());
    change_to_page(url);
}

//---------------------------------------------------------------------------------------
void DocumentFrame::create_toc_pane()
{
    m_left = LENMUS_NEW BookContentBox(this, this, m_appScope, wxID_ANY, wxDefaultPosition,
                                wxDefaultSize, 0, _T("The TOC"));
    m_left->CreateContents(m_pBookReader);
}

//---------------------------------------------------------------------------------------
void DocumentFrame::create_content_pane(const string& filename)
{
    m_right = LENMUS_NEW DocumentWindow(this, m_appScope, m_lomse);
    load_page(filename);
}

//---------------------------------------------------------------------------------------
void DocumentFrame::create_content_pane(int iTocItem)
{
    m_right = LENMUS_NEW DocumentWindow(this, m_appScope, m_lomse);
    load_page(iTocItem);
}

//---------------------------------------------------------------------------------------
ImoScore* DocumentFrame::get_active_score()
{
    if (m_right)
        return m_right->get_active_score();
    else
        return NULL;
}

//---------------------------------------------------------------------------------------
Interactor* DocumentFrame::get_interactor()
{
    if (m_right)
        return m_right->get_interactor();
    else
        return NULL;
}

//---------------------------------------------------------------------------------------
void DocumentFrame::display_document(const string& filename, int viewType)
{
    wxFileName document( to_wx_string(filename) );
    bool fIsBook = (document.GetExt().Upper() == _T("LMB"));

    m_bookPath = document.GetFullPath();

   if (fIsBook)
    {
        //read book (.lmb)
        m_pBookReader = LENMUS_NEW BookReader();
        if (!m_pBookReader->AddBook(document))
        {
            //TODO better error handling
            wxMessageBox(wxString::Format(_("Failed adding book %s"),
                document.GetFullPath().c_str() ));
            return;
        }
        create_toc_pane();
        create_content_pane(0);

        m_left->Show(true);

        //split at 25%/75%
        wxSize size = this->GetClientSize();
        int leftPixels = size.GetWidth() / 4;
        SplitVertically(m_left, m_right, leftPixels);
    }
    else
    {
        //read page (.lms)
        create_content_pane(filename);
        Initialize(m_right);
    }
}

//---------------------------------------------------------------------------------------
void DocumentFrame::on_hyperlink_event(SpEventInfo pEvent)
{
    SpEventMouse pEv = boost::static_pointer_cast<EventMouse>(pEvent);
    ImoLink* pLink = static_cast<ImoLink*>( pEv->get_imo_object() );
    wxString url = to_wx_string( pLink->get_url() );

    wxString pagename;
    if (url.StartsWith(_T("#LenMusPage/"), &pagename))
        change_to_page(pagename);
    else
        ::wxLaunchDefaultBrowser(url);
}

//---------------------------------------------------------------------------------------
void DocumentFrame::change_to_page(wxString& pagename)
{
    wxString fullpath = m_pBookReader->FindPageByName(pagename);
    if (fullpath != _T(""))
    {
        load_page( to_std_string(fullpath) );
        m_right->Refresh(false /* don't erase background */);
    }
    else
    {
        wxString msg = wxString::Format(_T("Invalid link='%s'\nPage='%s' not found."),
                                        pagename.c_str(), fullpath.c_str() );
        wxMessageBox(msg);
    }
}

//---------------------------------------------------------------------------------------
void DocumentFrame::load_page(int iTocItem)
{
    wxLogMessage(_T("DocumentFrame::load_page (by toc item, item %d) %s"), iTocItem, GetLabel().c_str());
    wxString fullpath = get_path_for_toc_item(iTocItem);
    wxLogMessage(_T("[DocumentFrame::load_page] page: <%s>"), fullpath.c_str());
    load_page( to_std_string(fullpath) );
}

//---------------------------------------------------------------------------------------
void DocumentFrame::load_page(const string& filename)
{
    wxLogMessage(_T("DocumentFrame::load_page (by filename) %s. Filename='%s'"), GetLabel().c_str(), to_wx_string(filename).c_str());
    //Code commented out and replaced by following code because it causes a rare problem
    //when returning back from exercise 1 in L1_MusicReading_accidentals.lms
    //
    //int viewType = ViewFactory::k_view_vertical_book;
    // m_right->display_document(filename, viewType);

    if (!filename.empty())
    {
        LdpZipReader reader(filename);
        int viewType = ViewFactory::k_view_vertical_book;
        string title = "test";
        m_right->display_document(reader, viewType, title);
    }
}

//---------------------------------------------------------------------------------------
wxString DocumentFrame::get_path_for_toc_item(int iItem)
{
    const BookIndexArray& contents = m_pBookReader->GetContentsArray();
    if (!contents[iItem]->page.empty())
        return contents[iItem]->GetFullPath();
    else
        return wxEmptyString;
}

//---------------------------------------------------------------------------------------
void DocumentFrame::on_splitter_moved(wxSplitterEvent& WXUNUSED(event))
{
    wxLogMessage(_T("DocumentFrame::on_splitter_moved %s"), GetLabel().c_str());
    if (m_right)
        m_right->zoom_fit_width();
}


}   //namespace lenmus