1 /***************************************************************
2  * Name:      DirectoryParamsPanel
3  *
4  * Purpose:   This class is a panel that allows the user to
5  *            set the directory search parameters (dir,
6  *            extensions...).
7  *            It is used in the ThreadSearchView and the
8  *            ThreadSearchConfPanel.
9  *            It does nothing but forwarding events to the
10  *            parent window.
11  *
12  * Author:    Jerome ANTOINE
13  * Created:   2007-10-08
14  * Copyright: Jerome ANTOINE
15  * License:   GPL
16  **************************************************************/
17 
18 #include "sdk.h"
19 #ifndef CB_PRECOMP
20     #include <wx/button.h>
21     #include <wx/checkbox.h>
22     #include <wx/combobox.h>
23     #include <wx/dirdlg.h>
24     #include <wx/sizer.h>
25     #include <wx/stattext.h>
26 #endif
27 
28 #include "DirectoryParamsPanel.h"
29 #include "ThreadSearchControlIds.h"
30 #include "ThreadSearchFindData.h"
31 
32 
33 namespace
34 {
35 
36 // Max number of items in search history combo box
37 const unsigned int MAX_NB_SEARCH_ITEMS = 20;
38 
AddItemToCombo(wxComboBox * combo,const wxString & str)39 inline void AddItemToCombo(wxComboBox *combo, const wxString &str)
40 {
41     int index = combo->FindString(str);
42 
43     // Removes item if already in combos box
44     if (index != wxNOT_FOUND)
45         combo->Delete(index);
46 
47     // Removes last item if max nb item is reached
48     if (combo->GetCount() >= MAX_NB_SEARCH_ITEMS)
49         combo->Delete(combo->GetCount()-1);
50 
51     // Adds it to combos
52     combo->Insert(str, 0);
53     combo->SetSelection(0);
54 }
55 
56 } // anonymouse namespace
57 
DirectoryParamsPanel(ThreadSearchFindData * findData,wxWindow * parent,int id,const wxPoint & pos,const wxSize & size,long WXUNUSED (style))58 DirectoryParamsPanel::DirectoryParamsPanel(ThreadSearchFindData *findData, wxWindow* parent, int id, const wxPoint& pos,
59                                            const wxSize& size, long WXUNUSED(style)):
60     wxPanel(parent, id, pos, size, wxTAB_TRAVERSAL),
61     m_pFindData(findData)
62 {
63     const wxString choices[] = {};
64 
65     // begin wxGlade: DirectoryParamsPanel::DirectoryParamsPanel
66     m_pSearchDirPath = new wxComboBox(this, controlIDs.Get(ControlIDs::idSearchDirPath), wxEmptyString,
67                                       wxDefaultPosition, wxDefaultSize, 0, choices, wxCB_DROPDOWN|wxTE_PROCESS_ENTER);
68     m_pBtnSelectDir = new wxButton(this, controlIDs.Get(ControlIDs::idBtnDirSelectClick), _("..."));
69     m_pChkSearchDirRecursively = new wxCheckBox(this, controlIDs.Get(ControlIDs::idChkSearchDirRecurse), _("Recurse"));
70     m_pChkSearchDirHiddenFiles = new wxCheckBox(this, controlIDs.Get(ControlIDs::idChkSearchDirHidden), _("Hidden"));
71     m_pMask = new wxComboBox(this, controlIDs.Get(ControlIDs::idSearchMask), findData->GetSearchMask(),
72                              wxDefaultPosition, wxDefaultSize, 0, choices, wxCB_DROPDOWN|wxTE_PROCESS_ENTER);
73 
74     set_properties();
75     do_layout();
76     // end wxGlade
77 }
78 
79 
80 BEGIN_EVENT_TABLE(DirectoryParamsPanel, wxPanel)
81     // begin wxGlade: DirectoryParamsPanel::event_table
82     EVT_TEXT_ENTER(controlIDs.Get(ControlIDs::idSearchDirPath), DirectoryParamsPanel::OnSearchDirTextEvent)
83     EVT_TEXT(controlIDs.Get(ControlIDs::idSearchDirPath), DirectoryParamsPanel::OnSearchDirTextEvent)
84     EVT_COMBOBOX(controlIDs.Get(ControlIDs::idSearchDirPath), DirectoryParamsPanel::OnSearchDirTextEvent)
85     EVT_BUTTON(controlIDs.Get(ControlIDs::idBtnDirSelectClick), DirectoryParamsPanel::OnBtnDirSelectClick)
86     EVT_CHECKBOX(controlIDs.Get(ControlIDs::idChkSearchDirRecurse), DirectoryParamsPanel::OnChkSearchDirRecurse)
87     EVT_CHECKBOX(controlIDs.Get(ControlIDs::idChkSearchDirHidden), DirectoryParamsPanel::OnChkSearchDirHidden)
88     EVT_TEXT_ENTER(controlIDs.Get(ControlIDs::idSearchMask), DirectoryParamsPanel::OnSearchMaskTextEvent)
89     EVT_TEXT(controlIDs.Get(ControlIDs::idSearchMask), DirectoryParamsPanel::OnSearchMaskTextEvent)
90     EVT_COMBOBOX(controlIDs.Get(ControlIDs::idSearchMask), DirectoryParamsPanel::OnSearchMaskTextEvent)
91     // end wxGlade
92 END_EVENT_TABLE();
93 
94 
OnSearchDirTextEvent(wxCommandEvent & event)95 void DirectoryParamsPanel::OnSearchDirTextEvent(wxCommandEvent &event)
96 {
97     m_pFindData->SetSearchPath(event.GetString());
98     event.Skip();
99 }
100 
OnSearchMaskTextEvent(wxCommandEvent & event)101 void DirectoryParamsPanel::OnSearchMaskTextEvent(wxCommandEvent &event)
102 {
103     m_pFindData->SetSearchMask(event.GetString());
104     event.Skip();
105 }
106 
107 
OnBtnDirSelectClick(wxCommandEvent & event)108 void DirectoryParamsPanel::OnBtnDirSelectClick(wxCommandEvent &event)
109 {
110     wxString dir = m_pSearchDirPath->GetValue();
111     if (dir.empty())
112         dir = wxGetCwd();
113     wxDirDialog DlgDir(this, _("Select directory"), dir);
114     if ( DlgDir.ShowModal() == wxID_OK )
115     {
116         m_pSearchDirPath->SetValue(DlgDir.GetPath());
117         m_pFindData->SetSearchPath(DlgDir.GetPath());
118     }
119 
120     event.Skip();
121 }
122 
123 
OnChkSearchDirRecurse(wxCommandEvent & event)124 void DirectoryParamsPanel::OnChkSearchDirRecurse(wxCommandEvent &event)
125 {
126     m_pFindData->SetRecursiveSearch(event.IsChecked());
127     event.Skip();
128 }
OnChkSearchDirHidden(wxCommandEvent & event)129 void DirectoryParamsPanel::OnChkSearchDirHidden(wxCommandEvent &event)
130 {
131     m_pFindData->SetHiddenSearch(event.IsChecked());
132     event.Skip();
133 }
134 
135 
136 // wxGlade: add DirectoryParamsPanel event handlers
137 
138 
set_properties()139 void DirectoryParamsPanel::set_properties()
140 {
141     // begin wxGlade: DirectoryParamsPanel::set_properties
142     m_pSearchDirPath->SetToolTip(_("Directory to search in files"));
143     m_pBtnSelectDir->SetToolTip(_("Browse for directory to search in"));
144     m_pChkSearchDirRecursively->SetToolTip(_("Search in directory files recursively"));
145     m_pChkSearchDirRecursively->SetValue(1);
146     m_pChkSearchDirHiddenFiles->SetToolTip(_("Search in directory hidden files"));
147     m_pChkSearchDirHiddenFiles->SetValue(1);
148     m_pMask->SetToolTip(wxT("*.cpp;*.c;*.h"));
149     // end wxGlade
150 }
151 
152 
do_layout()153 void DirectoryParamsPanel::do_layout()
154 {
155 #if wxCHECK_VERSION(3, 0, 0)
156     #define wxADJUST_MINSIZE 0
157 #endif
158     // begin wxGlade: DirectoryParamsPanel::do_layout
159     wxBoxSizer* SizerTop = new wxBoxSizer(wxHORIZONTAL);
160     SizerTop->Add(m_pSearchDirPath, 2, wxLEFT|wxRIGHT|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 4);
161     SizerTop->Add(m_pBtnSelectDir, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 4);
162     SizerTop->Add(m_pChkSearchDirRecursively, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 4);
163     SizerTop->Add(m_pChkSearchDirHiddenFiles, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 4);
164     SizerTop->Add(m_pMask, 1, wxLEFT|wxRIGHT|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 4);
165     wxStaticText* m_pStatTxtMask = new wxStaticText(this, -1, _("mask"));
166     SizerTop->Add(m_pStatTxtMask, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 4);
167     SetAutoLayout(true);
168     SetSizer(SizerTop);
169     SizerTop->Fit(this);
170     SizerTop->SetSizeHints(this);
171     // end wxGlade
172 }
173 
174 // Getters
GetSearchDirPath() const175 wxString DirectoryParamsPanel::GetSearchDirPath()        const {return m_pSearchDirPath->GetValue();}
GetSearchDirRecursively() const176 bool     DirectoryParamsPanel::GetSearchDirRecursively() const {return m_pChkSearchDirRecursively->IsChecked();}
GetSearchDirHidden() const177 bool     DirectoryParamsPanel::GetSearchDirHidden()      const {return m_pChkSearchDirHiddenFiles->IsChecked();}
GetSearchMask() const178 wxString DirectoryParamsPanel::GetSearchMask()           const {return m_pMask->GetValue();}
179 
180 // Setters
SetSearchDirPath(const wxString & sDirPath)181 void     DirectoryParamsPanel::SetSearchDirPath(const wxString& sDirPath) {m_pSearchDirPath->SetValue(sDirPath);}
SetSearchDirRecursively(bool bRecurse)182 void     DirectoryParamsPanel::SetSearchDirRecursively(bool bRecurse)     {m_pChkSearchDirRecursively->SetValue(bRecurse);}
SetSearchDirHidden(bool bSearchHidden)183 void     DirectoryParamsPanel::SetSearchDirHidden(bool bSearchHidden)     {m_pChkSearchDirHiddenFiles->SetValue(bSearchHidden);}
SetSearchMask(const wxString & sMask)184 void     DirectoryParamsPanel::SetSearchMask(const wxString& sMask)       {m_pMask->SetValue(sMask);}
185 
SetSearchHistory(const wxArrayString & searchDirs,const wxArrayString & searchMasks)186 void DirectoryParamsPanel::SetSearchHistory(const wxArrayString& searchDirs, const wxArrayString& searchMasks)
187 {
188     for (wxArrayString::const_iterator it = searchDirs.begin(); it != searchDirs.end(); ++it)
189     {
190         if (!it->empty())
191             m_pSearchDirPath->Append(*it);
192     }
193     for (wxArrayString::const_iterator it = searchMasks.begin(); it != searchMasks.end(); ++it)
194     {
195         if (!it->empty())
196             m_pMask->Append(*it);
197     }
198 }
199 
GetSearchDirsHistory() const200 wxArrayString DirectoryParamsPanel::GetSearchDirsHistory() const
201 {
202     return m_pSearchDirPath->GetStrings();
203 }
204 
GetSearchMasksHistory() const205 wxArrayString DirectoryParamsPanel::GetSearchMasksHistory() const
206 {
207     return m_pMask->GetStrings();
208 }
209 
AddExpressionToCombos(const wxString & path,const wxString & mask)210 void DirectoryParamsPanel::AddExpressionToCombos(const wxString& path, const wxString& mask)
211 {
212     AddItemToCombo(m_pSearchDirPath, path);
213     AddItemToCombo(m_pMask, mask);
214 }
215