1 /***************************************************************
2  * Name:      ThreadSearchView
3  * Purpose:   This class implements the panel that is added to
4  *            Code::Blocks Message notebook or layout.
5  *            It runs the search worker thread and receives
6  *            ThreadSearchEvent from it to update the list log.
7  * Author:    Jerome ANTOINE
8  * Created:   2007-10-08
9  * Copyright: Jerome ANTOINE
10  * License:   GPL
11  **************************************************************/
12 
13 #ifndef THREAD_SEARCH_VIEW_H
14 #define THREAD_SEARCH_VIEW_H
15 
16 #include <wx/string.h>
17 #include <wx/thread.h>
18 #include <wx/timer.h>
19 #include <wx/dynarray.h>
20 #include <wx/datetime.h>
21 #include <wx/panel.h>
22 #include <wx/arrstr.h>
23 
24 #include "editormanager.h"
25 #include "cbproject.h"
26 #include "projectmanager.h"
27 
28 #include "ThreadSearchLoggerBase.h"
29 
30 class wxToolBar;
31 class wxStaticBox;
32 class wxComboBox;
33 class wxButton;
34 class wxStaticText;
35 class cbStyledTextCtrl;
36 class wxPanel;
37 class wxMouseEvent;
38 class wxCommandEvent;
39 class wxSplitterEvent;
40 class wxScintillaEvent;
41 class wxContextMenuEvent;
42 class wxSplitterWindow;
43 class wxStaticBoxSizer;
44 class wxBoxSizer;
45 class wxBitmap;
46 class wxBitmapButton;
47 class wxStaticLine;
48 class wxImageList;
49 
50 class ThreadSearch;
51 class ThreadSearchEvent;
52 class ThreadSearchThread;
53 class ThreadSearchFindData;
54 class SearchInPanel;
55 class DirectoryParamsPanel;
56 
57 
58 class ThreadSearchView: public wxPanel {
59 
60     friend class ThreadSearch;
61 public:
62     // begin wxGlade: ThreadSearchView::ids
63     // end wxGlade
64 
65     /** Constructor. */
66     ThreadSearchView(ThreadSearch& threadSearchPlugin);
67 
68     /** Destructor. */
69     ~ThreadSearchView();
70 
71     /** Runs the worker thread search.
72       * @param aFindData : structure containing all search parameters :
73       *                    text, whole word, case, directory...
74       */
75     void ThreadedSearch(const ThreadSearchFindData& aFindData);
76 
77     /** No comment
78       * @return true if a search is running, false otherwise
79       */
80     bool IsSearchRunning();
81 
82     /** This function is called from ThreadSearchThread::OnExit.
83       * This must not be called directly.
84       */
85     void OnThreadExit();
86 
87     /** Makes instance update its graphical widgets.
88       * Should be called by ThreadSearch instance after m_ThreadSearchPlugin modification
89       */
90     void Update();
91 
92     /** Loads file in code preview and makes line visible.
93       * @param file : file path
94       * @param line : line to display in the preview editor
95       * @return true if success
96       */
97     bool UpdatePreview(const wxString& file, long line);
98 
99     /** Sets/gets the splitter window sash position. */
100     void SetSashPosition(int position, const bool redraw = true);
101     int  GetSashPosition() const;
102 
103     /** Sets/gets the search history */
104     void          SetSearchHistory(const wxArrayString& searchPatterns, const wxArrayString& searchDirs,
105                                    const wxArrayString& searchMasks);
106     wxArrayString GetSearchHistory() const;
107     wxArrayString GetSearchDirsHistory() const;
108     wxArrayString GetSearchMasksHistory() const;
109 
110     /** SetLoggerType
111       * Sets the logger type. If value is different from current one,
112       * m_pLogger is destroyed and rebuilt on the fly.
113       * @param lgrType : type of logger, can be a wxListCtrl or a wxTreeCtrl
114       */
115     void SetLoggerType(ThreadSearchLoggerBase::eLoggerTypes lgrType);
116 
117     /** PostThreadSearchEvent
118       * @param event : event sent by the worker thread (ThreadSearchThread)
119       * Thread safe mechanism. Clone the worker thread event to the mutex protected events array.
120       */
121     void PostThreadSearchEvent(const ThreadSearchEvent& event);
122 
123     /** SetToolBar
124       * C::B plugins manager provides a toolbar instance to ThreadSearch instance
125       * during init. This instance is referenced in ThreadSearchView by m_pToolBar.
126       */
SetToolBar(wxToolBar * pToolBar)127     void SetToolBar(wxToolBar* pToolBar) {m_pToolBar = pToolBar;}
128 
129     /** Set the proper image depending if there are any enabled options in the popup menu.
130       */
131     void UpdateOptionsButtonImage(const ThreadSearchFindData &findData);
132 
133     /** This method shows/hide the search graphical controls.
134       * @param show : show = true/hide = false toolbar
135       */
136     void ShowSearchControls(bool show);
137 
138     /** This method applies splitter settings
139       * @param showCodePreview : show = true/hide = false preview editor (=~ splitted or not)
140       * @param splitterMode : wxSPLIT_HORIZONTAL or wxSPLIT_VERTICAL
141       */
142     void ApplySplitterSettings(bool showCodePreview, long splitterMode);
143 
144     void FocusSearchCombo(const wxString &searchWord);
145 private:
146     // begin wxGlade: ThreadSearchView::methods
147     void set_properties();
148     void do_layout();
149     // end wxGlade
150 
151     /** ClearThreadSearchEventsArray
152       * Removes all events sent by the worker thread and stored in the array.
153       * Thread safe method.
154       @return true if clear successful. Not successful if Mutex is not caught.
155       */
156     bool ClearThreadSearchEventsArray();
157 
158     /** StopThread
159       * Called to stop the thread and manage all required operations.
160       * @return true if successful.
161       */
162     bool StopThread();
163 
164     // BEGIN Duplicated from cbeditor.cpp to apply folding options
165     void SetMarkerStyle(int marker, int markerType, wxColor fore, wxColor back);
166     void UnderlineFoldedLines(bool underline);
167     void SetFoldingIndicator(int id);
168     // END Duplicated from cbeditor.cpp to apply folding options
169 
170     ThreadSearchThread* m_pFindThread;             // Worker thread pointer. Must be allocated on the heap.
171     ThreadSearch&       m_ThreadSearchPlugin;      // Thread search plugin reference. 'Subject' in the observer pattern.
172     wxString            m_PreviewFilePath;         // File currently previewed path. Used to avoid reloading files.
173     wxDateTime          m_PreviewFileDate;         // File currently previewed modification time. Used to avoid reloading files.
174 
175     wxMutex             m_MutexSearchEventsArray;  // Mutex used for multi thread access to m_ThreadSearchEventsArray
176     wxArrayPtrVoid      m_ThreadSearchEventsArray; // Used to store events sent by the worker thread.
177     wxTimer             m_Timer;                   // Used for events update
178     long                m_StoppingThread;          // Used not to do more than one thread termination.
179 
180 protected:
181     // Graphical widgets managed by wxGlade
182     // begin wxGlade: ThreadSearchView::attributes
183     wxStaticBox* m_pSizerSearchDirItems_staticbox;
184     wxComboBox* m_pCboSearchExpr;
185     wxBitmapButton* m_pBtnSearch;
186     wxBitmapButton* m_pBtnOptions;
187     wxStaticLine* m_pStaticLine1;
188     wxStaticText* m_pStaTxtSearchIn;
189     SearchInPanel* m_pPnlSearchIn;
190     wxStaticLine* m_pStaticLine2;
191     wxBitmapButton* m_pBtnShowDirItems;
192     DirectoryParamsPanel* m_pPnlDirParams;
193     cbStyledTextCtrl* m_pSearchPreview;
194     wxPanel* m_pPnlPreview;
195     ThreadSearchLoggerBase* m_pLogger;
196     wxPanel* m_pPnlListLog;
197     wxSplitterWindow* m_pSplitter;
198     // end wxGlade
199     wxStaticBoxSizer* m_pSizerSearchDirItems;
200     wxBoxSizer*       m_pSizerSearchItems;
201     wxToolBar*        m_pToolBar;
202 
203     DECLARE_EVENT_TABLE()
204 
205     /** AddExpressionToSearchCombos
206       * Inserts expression to search combo in first position.
207       * If expression is already listed, it is removed before insertion.
208       * Used to keep the search history in the combo box.
209       * @param expression : searched text.
210       * @param path : searched path.
211       * @param mask : searched mask.
212       */
213     void AddExpressionToSearchCombos(const wxString& expression, const wxString& path, const wxString& mask);
214 
215     /// enum defining the possible labels to update Search buttons (view and toolbars)
216     /// Undefined can be used to skip label update
217     enum eSearchButtonLabel {
218         search,
219         cancel,
220         skip
221     };
222 
223     /** UpdateSearchButtons
224       * Updates wxButtons and wxBitmap buttons according to new label.
225       * @param enable : true : enable; false : disable
226       * @param label  : see eSearchButtonLabel declaration
227       */
228     void UpdateSearchButtons(bool enable, eSearchButtonLabel label = skip);
229 
230     /** Method used to enable/disable the graphical widgets related
231       * to search parameters. Disabled when thread is running.
232       * @param enable : true : enable; false : disable
233       */
234     void EnableControls(bool enable);
235 
236 public:
237     void OnThreadSearchErrorEvent(const ThreadSearchEvent& event);
238     void OnCboSearchExprEnter(wxCommandEvent &event); // wxGlade: <event_handler>
239     void OnBtnSearchClick(wxCommandEvent &event); // wxGlade: <event_handler>
240     void OnBtnOptionsClick(wxCommandEvent &event); // wxGlade: <event_handler>
241     void OnBtnShowDirItemsClick(wxCommandEvent &event); // wxGlade: <event_handler>
242     void OnSplitterDoubleClick(wxSplitterEvent &event); // wxGlade: <event_handler>
243 
244     void OnShowOptionsDialog(wxCommandEvent &event);
245     void OnQuickOptions(wxCommandEvent &event);
246     void OnQuickOptionsUpdateUI(wxUpdateUIEvent &event);
247 
248     void OnMarginClick(wxScintillaEvent& event);
249     void OnContextMenu(wxContextMenuEvent& event);
250     void OnLoggerClick      (const wxString& file, long line);  // Called by ThreadSearchLoggerBase derived instance
251                                                                 // when user clicks on a search result
252     void OnLoggerDoubleClick(const wxString& file, long line);  // Called by ThreadSearchLoggerBase derived instance
253                                                                 // when user double clicks on a search result
254 
255     void OnBtnSearchOpenFiles(wxCommandEvent &event);
256     void OnBtnSearchTargetFiles(wxCommandEvent &event);
257     void OnBtnSearchProjectFiles(wxCommandEvent &event);
258     void OnBtnSearchWorkspaceFiles(wxCommandEvent &event);
259     void OnBtnSearchDirectoryFiles(wxCommandEvent &event);
260 
261     void OnTmrListCtrlUpdate(wxTimerEvent& event);
262 }; // wxGlade: end class
263 
264 
265 #endif // THREAD_SEARCH_VIEW_H
266