Code::Blocks  SVN r11506
goto_file.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3  * http://www.gnu.org/licenses/gpl-3.0.html
4  *
5  * $Revision$
6  * $Id$
7  * $HeadURL$
8  */
9 
10 #include "sdk.h"
11 #include "goto_file.h"
12 
13 #ifndef WX_PRECOMP
14  //(*InternalHeadersPCH(GotoFile)
15  #include <wx/sizer.h>
16  #include <wx/listctrl.h>
17  #include <wx/string.h>
18  #include <wx/intl.h>
19  #include <wx/stattext.h>
20  #include <wx/textctrl.h>
21  //*)
22 
23  #include <algorithm>
24  #include <cmath>
25 
26  #include "cbexception.h"
27 #endif
28 //(*InternalHeaders(GotoFile)
29 //*)
30 
31 //(*IdInit(GotoFile)
32 const long GotoFile::ID_TEXTCTRL1 = wxNewId();
33 const long GotoFile::ID_RESULT_LIST = wxNewId();
34 //*)
35 
36 BEGIN_EVENT_TABLE(GotoFile,wxDialog)
37  //(*EventTable(GotoFile)
38  //*)
39 END_EVENT_TABLE()
40 
41 GotoFile::GotoFile(wxWindow* parent, IncrementalSelectIterator *iterator, const wxString &title,
42  const wxString &message) :
43  m_handler(this, iterator)
44 {
45  cbAssert(parent);
46 
47  BuildContent(parent, iterator, title, message);
48 }
49 
50 void GotoFile::BuildContent(wxWindow* parent, IncrementalSelectIterator *iterator, const wxString &title,
51  const wxString &message)
52 {
53  //(*Initialize(GotoFile)
54  wxStaticText* labelCtrl;
55 
58  labelCtrl = new wxStaticText(this, wxID_ANY, _("Some text"), wxDefaultPosition, wxDefaultSize, 0, _T("wxID_ANY"));
59  m_sizer->Add(labelCtrl, 0, wxTOP|wxLEFT|wxRIGHT|wxEXPAND, 5);
61  m_Text->SetFocus();
64  m_ResultList->SetMinSize(wxSize(500,300));
66  SetSizer(m_sizer);
67  m_sizer->Fit(this);
68  m_sizer->SetSizeHints(this);
69  //*)
70 
71  SetTitle(title);
72  labelCtrl->SetLabel(message);
73 
74  // Call this here to make sure the column widths are correctly calculated.
76 
77  const int columnWidth = iterator->GetColumnWidth(0);
78 
79  // Add first column
80  wxListItem column;
81  column.SetId(0);
82  column.SetText( _("Column") );
83  column.SetWidth(columnWidth);
84  m_ResultList->InsertColumn(0, column);
85  m_ResultList->SetIterator(iterator);
86 
87  // Call Fit to make sure all GetSize methods return correct values.
88  m_sizer->Fit(this);
89 
90  {
91  // Use GetItemRect to account for the spacing between rows. GetCharHeight is used just as
92  // precaution if GetItemRect fails.
93  wxRect itemRect;
94  if (!m_ResultList->GetItemRect(0, itemRect))
95  itemRect = wxRect();
96  const int charHeight = std::max(m_ResultList->GetCharHeight(), itemRect.GetHeight());
97  const int totalHeight = charHeight * m_ResultList->GetItemCount() + charHeight / 2;
98 
99  const wxSize minSize = m_ResultList->GetMinSize();
100  int minYCorrected = minSize.y;
101 
102  // Make the list taller if there are many items in it. This should make it a bit easier to find
103  // stuff. The height would be something like 50% of the display's client area height.
104  if (totalHeight > minSize.y)
105  {
106  const wxRect monitorRect = cbGetMonitorRectForWindow(parent);
107  const int monitorHeight = int(std::lround(monitorRect.GetHeight() * 0.5));
108  minYCorrected = std::max(minYCorrected, std::min(monitorHeight, totalHeight));
109  }
110 
111  // Resize the window to maximise visible items. Do this using SetSize instead of using
112  // SetMinSize to allow the user to make the window smaller if he/she wishes to do so.
113  const wxSize windowSize = GetSize();
114  // GetSize for the list control could return a window smaller than the minSize, but the
115  // window size could be accounting for list control's min size. This means that sizeDiff
116  // could be calculated larger than needed. Account for this using std::max. This seems to
117  // happen in wx2.8 builds.
118  const wxSize listSize(std::max(m_ResultList->GetSize().x, minSize.x),
119  std::max(m_ResultList->GetSize().y, minSize.y));
120  // This accounts for non-list UI elements present in the window.
121  const wxSize sizeDiff = windowSize - listSize;
122  SetSize(wxSize(std::max(columnWidth + sizeDiff.x, windowSize.x), minYCorrected + sizeDiff.y));
123  }
124 }
125 
127 {
128  m_handler.DeInit(this);
129 
130  //(*Destroy(GotoFile)
131  //*)
132 }
133 
135 {
136  return m_handler.GetSelection();
137 }
138 
140 {
141  wxSize sz = GetSize();
142  m_sizer->Add(control, 0, wxBOTTOM|wxLEFT|wxRIGHT|wxALIGN_RIGHT, 8);
143  Fit();
144  SetMinSize(GetSize());
145  SetSize(sz);
146 }
wxSize Fit(wxWindow *window)
#define wxMAXIMIZE_BOX
int wxNewId()
bool GetItemRect(long item, wxRect &rect, int code=wxLIST_RECT_BOUNDS) const
const wxValidator wxDefaultValidator
wxBoxSizer * m_sizer
Definition: goto_file.h:37
virtual int GetColumnWidth(int column) const
DLLIMPORT wxRect cbGetMonitorRectForWindow(wxWindow *window)
Definition: globals.cpp:1153
#define wxLC_REPORT
#define wxTE_PROCESS_ENTER
void SetWidth(int width)
#define _T(string)
#define wxHSCROLL
void Init(wxListCtrl *list, wxTextCtrl *text)
void SetId(long id)
wxTextCtrl * m_Text
Definition: goto_file.h:38
void SetIterator(IncrementalSelectIterator *iterator)
wxSizerItem * Add(wxWindow *window, const wxSizerFlags &flags)
const wxSize wxDefaultSize
const wxPoint wxDefaultPosition
static const long ID_TEXTCTRL1
Definition: goto_file.h:42
#define wxDEFAULT_DIALOG_STYLE
int GetItemCount() const
int GetHeight() const
wxString wxEmptyString
#define wxLC_SINGLE_SEL
const wxString & _(const wxString &string)
#define cbAssert(expr)
Definition: cbexception.h:48
void SetMinSize(wxPropertyGrid *grid)
#define wxLC_NO_HEADER
Class that implements a virtual list control that uses an IncrementalSelectIterator to populate the l...
~GotoFile() override
Definition: goto_file.cpp:126
#define wxCLOSE_BOX
long InsertColumn(long col, const wxListItem &info)
void SetSizeHints(wxWindow *window)
#define wxLC_VIRTUAL
static const long ID_RESULT_LIST
Definition: goto_file.h:43
void AddControlBelowList(wxControl *control)
Definition: goto_file.cpp:139
void SetText(const wxString &text)
void BuildContent(wxWindow *parent, IncrementalSelectIterator *iterator, const wxString &title, const wxString &message)
Definition: goto_file.cpp:50
#define wxRESIZE_BORDER
IncrementalListCtrl * m_ResultList
Definition: goto_file.h:36
#define wxVSCROLL
IncrementalSelectHandler m_handler
Definition: goto_file.h:32
int GetSelection()
Definition: goto_file.cpp:134