Controller.h 4.45 KB
Newer Older
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
/* Copyright 2015 the SumatraPDF project authors (see AUTHORS file).
   License: GPLv3 */

class Controller;
class ChmModel;
class DisplayModel;
class EbookController;
struct EbookFormattingData;

class ControllerCallback {
public:
    virtual ~ControllerCallback() { }
    // tell the UI to show the pageNo as current page (which also syncs
    // the toc with the curent page). Needed for when a page change happens
    // indirectly or is initiated from within the model
    virtual void PageNoChanged(Controller *ctrl, int pageNo) = 0;
    // tell the UI to open the linked document or URL
    virtual void GotoLink(PageDestination *dest) = 0;
    // DisplayModel //
    virtual void Repaint() = 0;
    virtual void UpdateScrollbars(SizeI canvas) = 0;
    virtual void RequestRendering(int pageNo) = 0;
    virtual void CleanUp(DisplayModel *dm) = 0;
    virtual void RenderThumbnail(DisplayModel *dm, SizeI size, const std::function<void(RenderedBitmap*)>&) = 0;
    // ChmModel //
    // tell the UI to move focus back to the main window
    // (if always == false, then focus is only moved if it's inside
    // an HtmlWindow and thus outside the reach of the main UI)
    virtual void FocusFrame(bool always) = 0;
    // tell the UI to let the user save the provided data to a file
    virtual void SaveDownload(const WCHAR *url, const unsigned char *data, size_t len) = 0;
    // EbookController //
    virtual void HandleLayoutedPages(EbookController *ctrl, EbookFormattingData *data) = 0;
    virtual void RequestDelayedLayout(int delay) = 0;
};

class Controller {
protected:
    ControllerCallback *cb;

public:
    explicit Controller(ControllerCallback *cb) : cb(cb) { CrashIf(!cb); }
    virtual ~Controller() { }

    // meta data
    virtual const WCHAR *FilePath() const = 0;
    virtual const WCHAR *DefaultFileExt() const = 0;
    virtual int PageCount() const = 0;
    virtual WCHAR *GetProperty(DocumentProperty prop) = 0;

    // page navigation (stateful)
    virtual int CurrentPageNo() const = 0;
    virtual void GoToPage(int pageNo, bool addNavPoint) = 0;
    virtual bool CanNavigate(int dir) const = 0;
    virtual void Navigate(int dir) = 0;

    // view settings
    virtual void SetDisplayMode(DisplayMode mode, bool keepContinuous=false) = 0;
    virtual DisplayMode GetDisplayMode() const = 0;
    virtual void SetPresentationMode(bool enable) = 0;
    virtual void SetZoomVirtual(float zoom, PointI *fixPt=nullptr) = 0;
    virtual float GetZoomVirtual(bool absolute=false) const = 0;
    virtual float GetNextZoomStep(float towards) const = 0;
    virtual void SetViewPortSize(SizeI size) = 0;

    // table of contents
    virtual bool HasTocTree() const = 0;
    virtual DocTocItem *GetTocTree() = 0;
    virtual void ScrollToLink(PageDestination *dest) = 0;
    virtual PageDestination *GetNamedDest(const WCHAR *name) = 0;

    // state export
    virtual void UpdateDisplayState(DisplayState *ds) = 0;
    // asynchronously calls saveThumbnail (fails silently)
    virtual void CreateThumbnail(SizeI size, const std::function<void(RenderedBitmap *)>& saveThumbnail) = 0;

    // page labels (optional)
    virtual bool HasPageLabels() const { return false; }
    virtual WCHAR *GetPageLabel(int pageNo) const { return str::Format(L"%d", pageNo); }
    virtual int GetPageByLabel(const WCHAR *label) const { return _wtoi(label); }

    // common shortcuts
    virtual bool ValidPageNo(int pageNo) const {
        return 1 <= pageNo && pageNo <= PageCount();
    }
    virtual bool GoToNextPage() {
        if (CurrentPageNo() == PageCount())
            return false;
        GoToPage(CurrentPageNo() + 1, false);
        return true;
    }
    virtual bool GoToPrevPage(bool toBottom=false) {
        UNUSED(toBottom);
        if (CurrentPageNo() == 1)
            return false;
        GoToPage(CurrentPageNo() - 1, false);
        return true;
    }
    virtual bool GoToFirstPage() {
        if (CurrentPageNo() == 1)
            return false;
        GoToPage(1, true);
        return true;
    }
    virtual bool GoToLastPage() {
        if (CurrentPageNo() == PageCount())
            return false;
        GoToPage(PageCount(), true);
        return true;
    }

    // for quick type determination and type-safe casting
    virtual DisplayModel *AsFixed() { return nullptr; }
    virtual ChmModel *AsChm() { return nullptr; }
    virtual EbookController *AsEbook() { return nullptr; }
};