1 /*
2  *  This file is part of Poedit (http://poedit.net)
3  *
4  *  Copyright (C) 2014-2015 Vaclav Slavik
5  *
6  *  Permission is hereby granted, free of charge, to any person obtaining a
7  *  copy of this software and associated documentation files (the "Software"),
8  *  to deal in the Software without restriction, including without limitation
9  *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  *  and/or sell copies of the Software, and to permit persons to whom the
11  *  Software is furnished to do so, subject to the following conditions:
12  *
13  *  The above copyright notice and this permission notice shall be included in
14  *  all copies or substantial portions of the Software.
15  *
16  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  *  DEALINGS IN THE SOFTWARE.
23  *
24  */
25 
26 #ifndef Poedit_sidebar_h
27 #define Poedit_sidebar_h
28 
29 #include <cstdint>
30 #include <memory>
31 #include <vector>
32 
33 #include <wx/event.h>
34 #include <wx/panel.h>
35 
36 #include "catalog.h"
37 #include "language.h"
38 #include "tm/suggestions.h"
39 
40 class WXDLLIMPEXP_FWD_CORE wxMenu;
41 class WXDLLIMPEXP_FWD_CORE wxMenuItem;
42 class WXDLLIMPEXP_FWD_CORE wxSizer;
43 class WXDLLIMPEXP_FWD_CORE wxStaticText;
44 class WXDLLIMPEXP_FWD_CORE wxStaticBitmap;
45 
46 class ExplanationLabel;
47 
48 struct Suggestion;
49 class SuggestionsProvider;
50 class SuggestionWidget;
51 class Sidebar;
52 
53 
54 /// Implements part of the sidebar.
55 class SidebarBlock : public std::enable_shared_from_this<SidebarBlock>
56 {
57 public:
~SidebarBlock()58     virtual ~SidebarBlock() {}
59 
GetSizer()60     wxSizer *GetSizer() const { return m_sizer; }
61 
62     virtual void Show(bool show);
63 
64     void SetItem(const CatalogItemPtr& item);
65 
66     virtual bool ShouldShowForItem(const CatalogItemPtr& item) const = 0;
67 
68     virtual void Update(const CatalogItemPtr& item) = 0;
69 
IsGrowable()70     virtual bool IsGrowable() const { return false; }
71 
72 protected:
73     enum Flags
74     {
75         NoUpperMargin = 1
76     };
77 
78     SidebarBlock(Sidebar *parent, const wxString& label, int flags = 0);
79 
80     Sidebar *m_parent;
81     wxSizer *m_headerSizer;
82     wxSizer *m_innerSizer;
83 
84 private:
85     wxSizer *m_sizer;
86 };
87 
88 
89 wxDECLARE_EVENT(EVT_SUGGESTION_SELECTED, wxCommandEvent);
90 
91 /// Sidebar block implementation translation suggestions
92 class SuggestionsSidebarBlock : public SidebarBlock
93 {
94 public:
95     SuggestionsSidebarBlock(Sidebar *parent, wxMenu *menu);
96     ~SuggestionsSidebarBlock();
97 
98     void Show(bool show) override;
IsGrowable()99     bool IsGrowable() const override { return true; }
100     bool ShouldShowForItem(const CatalogItemPtr& item) const override;
101     void Update(const CatalogItemPtr& item) override;
102 
103 protected:
104     // How many entries can have shortcuts?
105     static const int SUGGESTIONS_MENU_ENTRIES = 9;
106 
107     virtual void UpdateVisibility();
108 
109     virtual wxBitmap GetIconForSuggestion(const Suggestion& s) const;
110     virtual wxString GetTooltipForSuggestion(const Suggestion& s) const;
111 
112     void ClearMessage();
113     void SetMessage(const wxString& icon, const wxString& text);
114 
115     virtual void ReportError(SuggestionsBackend *backend, std::exception_ptr e);
116     virtual void ClearSuggestions();
117     virtual void UpdateSuggestions(const SuggestionsList& hits);
118     virtual void OnQueriesFinished();
119 
120     virtual void BuildSuggestionsMenu(int count = SUGGESTIONS_MENU_ENTRIES);
121     virtual void UpdateSuggestionsMenu();
122     virtual void ClearSuggestionsMenu();
123 
124     virtual void QueryAllProviders(const CatalogItemPtr& item);
125     void QueryProvider(SuggestionsBackend& backend, const CatalogItemPtr& item, uint64_t queryId);
126 
127 protected:
128     std::unique_ptr<SuggestionsProvider> m_provider;
129 
130     wxMenu *m_suggestionsMenu;
131 
132     wxSizer *m_msgSizer;
133     bool m_msgPresent;
134     wxStaticBitmap *m_msgIcon;
135     ExplanationLabel *m_msgText;
136     wxStaticText *m_iGotNothing;
137 
138     wxSizer *m_suggestionsSizer;
139     // Additional sizer for derived classes, shown below suggestions
140     wxSizer *m_extrasSizer;
141 
142     SuggestionsList m_suggestions;
143     std::vector<SuggestionWidget*> m_suggestionsWidgets;
144     std::vector<wxMenuItem*> m_suggestionMenuItems;
145     int m_pendingQueries;
146     uint64_t m_latestQueryId;
147 };
148 
149 /**
150     Control showing Poedit's assistance sidebar.
151 
152     Contains TM suggestions, comments and possibly other auxiliary stuff.
153  */
154 class Sidebar : public wxPanel
155 {
156 public:
157     Sidebar(wxWindow *parent, wxMenu *suggestionsMenu);
158     ~Sidebar();
159 
160     /// Update selected item, if there's a single one. May be nullptr.
161     void SetSelectedItem(const CatalogPtr& catalog, const CatalogItemPtr& item);
162 
163     /// Tell the sidebar there's multiple selection.
164     void SetMultipleSelection();
165 
166     /// Returns currently selected item
GetSelectedItem()167     CatalogItemPtr GetSelectedItem() const { return m_selectedItem; }
168     Language GetCurrentSourceLanguage() const;
169     Language GetCurrentLanguage() const;
170     bool FileHasCapability(Catalog::Cap cap) const;
171 
172     /// Refreshes displayed content
173     void RefreshContent();
174 
175     /// Call when catalog changes/is invalidated
ResetCatalog()176     void ResetCatalog() { SetSelectedItem(nullptr, nullptr); }
177 
178     /// Set max height of the upper (not input-aligned) part.
179     void SetUpperHeight(int size);
180 
181 protected:
182     void DoEnable(bool enable) override;
183 
184 private:
185     enum BlockPos { Top, Bottom };
186     void AddBlock(SidebarBlock *block, BlockPos pos);
187 
188     void OnPaint(wxPaintEvent&);
189 
190 private:
191     CatalogPtr m_catalog;
192     CatalogItemPtr m_selectedItem;
193 
194     std::vector<std::shared_ptr<SidebarBlock>> m_blocks;
195 
196     wxSizer *m_blocksSizer;
197     wxSizer *m_topBlocksSizer, *m_bottomBlocksSizer;
198 };
199 
200 #endif // Poedit_sidebar_h
201