]> git.saurik.com Git - wxWidgets.git/blame - src/generic/srchctlg.cpp
No changes, just improve wxScrolled::EnableScrolling() documentation.
[wxWidgets.git] / src / generic / srchctlg.cpp
CommitLineData
3f7f284d
RD
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/srchctlg.cpp
3// Purpose: implements wxSearchCtrl as a composite control
4// Author: Vince Harron
3f7f284d 5// Created: 2006-02-19
8bc333d7 6// RCS-ID: $Id$
3f7f284d 7// Copyright: Vince Harron
526954c5 8// Licence: wxWindows licence
3f7f284d
RD
9///////////////////////////////////////////////////////////////////////////////
10
3f7f284d
RD
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
09d93215
PC
18#if wxUSE_SEARCHCTRL
19
20#include "wx/srchctrl.h"
21
3f7f284d
RD
22#ifndef WX_PRECOMP
23 #include "wx/button.h"
38f74dff
CE
24 #include "wx/dcclient.h"
25 #include "wx/menu.h"
26 #include "wx/dcmemory.h"
3f7f284d
RD
27#endif //WX_PRECOMP
28
5b43c75c 29#if !wxUSE_NATIVE_SEARCH_CONTROL
3f7f284d
RD
30
31#include "wx/image.h"
32
09d93215 33#define WXMAX(a,b) ((a)>(b)?(a):(b))
3f7f284d
RD
34
35// ----------------------------------------------------------------------------
36// constants
37// ----------------------------------------------------------------------------
38
39// the margin between the text control and the search/cancel buttons
40static const wxCoord MARGIN = 2;
41
42// border around all controls to compensate for wxSIMPLE_BORDER
43#if defined(__WXMSW__)
44static const wxCoord BORDER = 0;
45static const wxCoord ICON_MARGIN = 2;
46static const wxCoord ICON_OFFSET = 2;
47#else
48static const wxCoord BORDER = 2;
49static const wxCoord ICON_MARGIN = 0;
50static const wxCoord ICON_OFFSET = 0;
51#endif
52
6646ca90
RD
53#define LIGHT_STEP 160
54
3f7f284d
RD
55// ----------------------------------------------------------------------------
56// wxSearchTextCtrl: text control used by search control
57// ----------------------------------------------------------------------------
58
59class wxSearchTextCtrl : public wxTextCtrl
60{
61public:
62 wxSearchTextCtrl(wxSearchCtrl *search, const wxString& value, int style)
63 : wxTextCtrl(search, wxID_ANY, value, wxDefaultPosition, wxDefaultSize,
7ecc7eb6 64 (style & ~wxBORDER_MASK) | wxNO_BORDER)
3f7f284d
RD
65 {
66 m_search = search;
7ecc7eb6
VZ
67
68 SetHint(_("Search"));
88c23b64 69
241708ca
VZ
70 // Ensure that our best size is recomputed using our overridden
71 // DoGetBestSize().
72 InvalidateBestSize();
3f7f284d
RD
73 }
74
0ec1179b
VZ
75
76 // provide access to the base class protected methods to wxSearchCtrl which
77 // needs to forward to them
8366ab05
VZ
78 void DoSetValue(const wxString& value, int flags)
79 {
bdb7278a 80 wxTextCtrl::DoSetValue(value, flags);
8366ab05
VZ
81 }
82
83 bool DoLoadFile(const wxString& file, int fileType)
84 {
bdb7278a 85 return wxTextCtrl::DoLoadFile(file, fileType);
8366ab05
VZ
86 }
87
88 bool DoSaveFile(const wxString& file, int fileType)
89 {
bdb7278a 90 return wxTextCtrl::DoSaveFile(file, fileType);
8366ab05 91 }
0ec1179b 92
3f7f284d
RD
93protected:
94 void OnText(wxCommandEvent& eventText)
95 {
96 wxCommandEvent event(eventText);
97 event.SetEventObject(m_search);
98 event.SetId(m_search->GetId());
99
100 m_search->GetEventHandler()->ProcessEvent(event);
101 }
102
103 void OnTextUrl(wxTextUrlEvent& eventText)
104 {
105 // copy constructor is disabled for some reason?
106 //wxTextUrlEvent event(eventText);
107 wxTextUrlEvent event(
8bc333d7 108 m_search->GetId(),
3f7f284d
RD
109 eventText.GetMouseEvent(),
110 eventText.GetURLStart(),
111 eventText.GetURLEnd()
112 );
113 event.SetEventObject(m_search);
114
115 m_search->GetEventHandler()->ProcessEvent(event);
116 }
117
a166dbba
VZ
118#ifdef __WXMSW__
119 // We increase the text control height to be the same as for the controls
120 // with border as this is what we actually need here because even though
121 // this control itself is borderless, it's inside wxSearchCtrl which does
122 // have the border and so should have the same height as the normal text
123 // entries with border.
124 //
125 // This is a bit ugly and it would arguably be better to use whatever size
126 // the base class version returns and just centre the text vertically in
127 // the search control but I failed to modify the code in LayoutControls()
128 // to do this easily and as there is much in that code I don't understand
129 // (notably what is the logic for buttons sizing?) I prefer to not touch it
130 // at all.
131 virtual wxSize DoGetBestSize() const
132 {
133 const long flags = GetWindowStyleFlag();
134 wxSearchTextCtrl* const self = const_cast<wxSearchTextCtrl*>(this);
135
136 self->SetWindowStyleFlag((flags & ~wxBORDER_MASK) | wxBORDER_DEFAULT);
137 const wxSize size = wxTextCtrl::DoGetBestSize();
138 self->SetWindowStyleFlag(flags);
139
140 return size;
141 }
142#endif // __WXMSW__
143
3f7f284d
RD
144private:
145 wxSearchCtrl* m_search;
88c23b64 146
3f7f284d
RD
147 DECLARE_EVENT_TABLE()
148};
149
150BEGIN_EVENT_TABLE(wxSearchTextCtrl, wxTextCtrl)
151 EVT_TEXT(wxID_ANY, wxSearchTextCtrl::OnText)
152 EVT_TEXT_ENTER(wxID_ANY, wxSearchTextCtrl::OnText)
153 EVT_TEXT_URL(wxID_ANY, wxSearchTextCtrl::OnTextUrl)
154 EVT_TEXT_MAXLEN(wxID_ANY, wxSearchTextCtrl::OnText)
155END_EVENT_TABLE()
156
157// ----------------------------------------------------------------------------
158// wxSearchButton: search button used by search control
159// ----------------------------------------------------------------------------
160
161class wxSearchButton : public wxControl
162{
163public:
164 wxSearchButton(wxSearchCtrl *search, int eventType, const wxBitmap& bmp)
165 : wxControl(search, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNO_BORDER),
166 m_search(search),
167 m_eventType(eventType),
168 m_bmp(bmp)
169 { }
170
5d83eee7
VZ
171 void SetBitmapLabel(const wxBitmap& label)
172 {
173 m_bmp = label;
174 InvalidateBestSize();
175 }
3f7f284d 176
7e0a200b
VZ
177 // The buttons in wxSearchCtrl shouldn't accept focus from keyboard because
178 // this would interfere with the usual TAB processing: the user expects
179 // that pressing TAB in the search control should switch focus to the next
180 // control and not give it to the button inside the same control. Besides,
181 // the search button can be already activated by pressing "Enter" so there
182 // is really no reason for it to be able to get focus from keyboard.
183 virtual bool AcceptsFocusFromKeyboard() const { return false; }
8bc333d7 184
3f7f284d
RD
185protected:
186 wxSize DoGetBestSize() const
187 {
188 return wxSize(m_bmp.GetWidth(), m_bmp.GetHeight());
189 }
8bc333d7 190
3f7f284d
RD
191 void OnLeftUp(wxMouseEvent&)
192 {
193 wxCommandEvent event(m_eventType, m_search->GetId());
194 event.SetEventObject(m_search);
195
ac63bc40
VZ
196 if ( m_eventType == wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN )
197 {
198 // it's convenient to have the string to search for directly in the
199 // event instead of having to retrieve it from the control in the
200 // event handler code later, so provide it here
201 event.SetString(m_search->GetValue());
202 }
203
3f7f284d
RD
204 GetEventHandler()->ProcessEvent(event);
205
206 m_search->SetFocus();
207
cfddeb10 208#if wxUSE_MENUS
c60122bf 209 if ( m_eventType == wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN )
3f7f284d
RD
210 {
211 // this happens automatically, just like on Mac OS X
212 m_search->PopupSearchMenu();
213 }
cfddeb10 214#endif // wxUSE_MENUS
3f7f284d
RD
215 }
216
217 void OnPaint(wxPaintEvent&)
218 {
219 wxPaintDC dc(this);
220 dc.DrawBitmap(m_bmp, 0,0, true);
221 }
222
8bc333d7 223
3f7f284d
RD
224private:
225 wxSearchCtrl *m_search;
226 wxEventType m_eventType;
227 wxBitmap m_bmp;
228
229 DECLARE_EVENT_TABLE()
230};
231
6f011faa 232BEGIN_EVENT_TABLE(wxSearchButton, wxControl)
3f7f284d
RD
233 EVT_LEFT_UP(wxSearchButton::OnLeftUp)
234 EVT_PAINT(wxSearchButton::OnPaint)
235END_EVENT_TABLE()
236
237BEGIN_EVENT_TABLE(wxSearchCtrl, wxSearchCtrlBase)
e5eff438 238 EVT_SEARCHCTRL_CANCEL_BTN(wxID_ANY, wxSearchCtrl::OnCancelButton)
3f7f284d 239 EVT_SET_FOCUS(wxSearchCtrl::OnSetFocus)
af1f44f1 240 EVT_SIZE(wxSearchCtrl::OnSize)
3f7f284d
RD
241END_EVENT_TABLE()
242
243IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl, wxSearchCtrlBase)
244
245// ============================================================================
246// implementation
247// ============================================================================
248
249// ----------------------------------------------------------------------------
250// wxSearchCtrl creation
251// ----------------------------------------------------------------------------
252
253// creation
254// --------
255
256wxSearchCtrl::wxSearchCtrl()
8bc333d7 257{
3f7f284d
RD
258 Init();
259}
260
261wxSearchCtrl::wxSearchCtrl(wxWindow *parent, wxWindowID id,
262 const wxString& value,
263 const wxPoint& pos,
264 const wxSize& size,
265 long style,
266 const wxValidator& validator,
267 const wxString& name)
268{
269 Init();
270
271 Create(parent, id, value, pos, size, style, validator, name);
272}
273
274void wxSearchCtrl::Init()
275{
cfddeb10
VZ
276 m_text = NULL;
277 m_searchButton = NULL;
278 m_cancelButton = NULL;
279#if wxUSE_MENUS
280 m_menu = NULL;
281#endif // wxUSE_MENUS
3f7f284d
RD
282
283 m_searchButtonVisible = true;
284 m_cancelButtonVisible = false;
285
3f7f284d
RD
286 m_searchBitmapUser = false;
287 m_cancelBitmapUser = false;
cfddeb10
VZ
288#if wxUSE_MENUS
289 m_searchMenuBitmapUser = false;
290#endif // wxUSE_MENUS
3f7f284d
RD
291}
292
293bool wxSearchCtrl::Create(wxWindow *parent, wxWindowID id,
294 const wxString& value,
295 const wxPoint& pos,
296 const wxSize& size,
297 long style,
298 const wxValidator& validator,
299 const wxString& name)
300{
88c23b64
VZ
301 // force border style for more native appearance
302 style &= ~wxBORDER_MASK;
10eeb875 303#ifdef __WXGTK__
88c23b64 304 style |= wxBORDER_SUNKEN;
a047aff2
JS
305#elif defined(__WXMSW__)
306 // Don't set the style explicitly, let GetDefaultBorder() work it out, unless
307 // we will get a sunken border (e.g. on Windows 200) in which case we must
308 // override with a simple border.
309 if (GetDefaultBorder() == wxBORDER_SUNKEN)
310 style |= wxBORDER_SIMPLE;
10eeb875 311#else
88c23b64 312 style |= wxBORDER_SIMPLE;
10eeb875 313#endif
0ec1179b
VZ
314 if ( !wxSearchCtrlBaseBaseClass::Create(parent, id, pos, size,
315 style, validator, name) )
3f7f284d
RD
316 {
317 return false;
318 }
319
7ecc7eb6 320 m_text = new wxSearchTextCtrl(this, value, style);
88c23b64 321
88c23b64
VZ
322 m_searchButton = new wxSearchButton(this,
323 wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN,
324 m_searchBitmap);
325 m_cancelButton = new wxSearchButton(this,
326 wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN,
327 m_cancelBitmap);
3f7f284d 328
3f7f284d 329 SetBackgroundColour( m_text->GetBackgroundColour() );
3f7f284d
RD
330
331 RecalcBitmaps();
332
333 SetInitialSize(size);
334 Move(pos);
335 return true;
336}
337
338wxSearchCtrl::~wxSearchCtrl()
339{
340 delete m_text;
341 delete m_searchButton;
342 delete m_cancelButton;
cfddeb10 343#if wxUSE_MENUS
3f7f284d 344 delete m_menu;
cfddeb10 345#endif // wxUSE_MENUS
3f7f284d
RD
346}
347
348
349// search control specific interfaces
cfddeb10
VZ
350#if wxUSE_MENUS
351
3f7f284d
RD
352void wxSearchCtrl::SetMenu( wxMenu* menu )
353{
354 if ( menu == m_menu )
355 {
356 // no change
357 return;
358 }
09d93215 359 bool hadMenu = (m_menu != NULL);
3f7f284d 360 delete m_menu;
3f7f284d
RD
361 m_menu = menu;
362
363 if ( m_menu && !hadMenu )
364 {
365 m_searchButton->SetBitmapLabel(m_searchMenuBitmap);
366 m_searchButton->Refresh();
3f7f284d
RD
367 }
368 else if ( !m_menu && hadMenu )
369 {
370 m_searchButton->SetBitmapLabel(m_searchBitmap);
371 if ( m_searchButtonVisible )
372 {
373 m_searchButton->Refresh();
374 }
3f7f284d 375 }
6646ca90 376 wxRect rect = GetRect();
88c23b64 377 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
3f7f284d
RD
378}
379
380wxMenu* wxSearchCtrl::GetMenu()
381{
382 return m_menu;
383}
384
cfddeb10
VZ
385#endif // wxUSE_MENUS
386
ec184e32 387void wxSearchCtrl::ShowSearchButton( bool show )
3f7f284d
RD
388{
389 if ( m_searchButtonVisible == show )
390 {
391 // no change
392 return;
393 }
394 m_searchButtonVisible = show;
395 if ( m_searchButtonVisible )
396 {
397 RecalcBitmaps();
398 }
399
400 wxRect rect = GetRect();
401 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
402}
403
ec184e32 404bool wxSearchCtrl::IsSearchButtonVisible() const
3f7f284d
RD
405{
406 return m_searchButtonVisible;
407}
408
409
ec184e32 410void wxSearchCtrl::ShowCancelButton( bool show )
3f7f284d
RD
411{
412 if ( m_cancelButtonVisible == show )
413 {
414 // no change
415 return;
416 }
417 m_cancelButtonVisible = show;
418
419 wxRect rect = GetRect();
420 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
421}
422
ec184e32 423bool wxSearchCtrl::IsCancelButtonVisible() const
3f7f284d
RD
424{
425 return m_cancelButtonVisible;
426}
427
6646ca90
RD
428void wxSearchCtrl::SetDescriptiveText(const wxString& text)
429{
7ecc7eb6 430 m_text->SetHint(text);
6646ca90
RD
431}
432
433wxString wxSearchCtrl::GetDescriptiveText() const
434{
7ecc7eb6 435 return m_text->GetHint();
6646ca90 436}
3f7f284d
RD
437
438// ----------------------------------------------------------------------------
439// geometry
440// ----------------------------------------------------------------------------
441
442wxSize wxSearchCtrl::DoGetBestSize() const
443{
444 wxSize sizeText = m_text->GetBestSize();
445 wxSize sizeSearch(0,0);
446 wxSize sizeCancel(0,0);
447 int searchMargin = 0;
448 int cancelMargin = 0;
cfddeb10 449 if ( m_searchButtonVisible || HasMenu() )
3f7f284d
RD
450 {
451 sizeSearch = m_searchButton->GetBestSize();
452 searchMargin = MARGIN;
453 }
454 if ( m_cancelButtonVisible )
455 {
456 sizeCancel = m_cancelButton->GetBestSize();
457 cancelMargin = MARGIN;
458 }
459
460 int horizontalBorder = 1 + ( sizeText.y - sizeText.y * 14 / 21 ) / 2;
461
462 // buttons are square and equal to the height of the text control
463 int height = sizeText.y;
464 return wxSize(sizeSearch.x + searchMargin + sizeText.x + cancelMargin + sizeCancel.x + 2*horizontalBorder,
465 height + 2*BORDER);
466}
467
468void wxSearchCtrl::DoMoveWindow(int x, int y, int width, int height)
469{
470 wxSearchCtrlBase::DoMoveWindow(x, y, width, height);
471
472 LayoutControls(0, 0, width, height);
473}
474
475void wxSearchCtrl::LayoutControls(int x, int y, int width, int height)
476{
af1f44f1
RD
477 if ( !m_text )
478 return;
88c23b64 479
3f7f284d
RD
480 wxSize sizeText = m_text->GetBestSize();
481 // make room for the search menu & clear button
6646ca90 482 int horizontalBorder = ( sizeText.y - sizeText.y * 14 / 21 ) / 2;
3f7f284d
RD
483 x += horizontalBorder;
484 y += BORDER;
485 width -= horizontalBorder*2;
486 height -= BORDER*2;
d3ad3974
PC
487 if (width < 0) width = 0;
488 if (height < 0) height = 0;
3f7f284d
RD
489
490 wxSize sizeSearch(0,0);
491 wxSize sizeCancel(0,0);
492 int searchMargin = 0;
493 int cancelMargin = 0;
cfddeb10 494 if ( m_searchButtonVisible || HasMenu() )
3f7f284d
RD
495 {
496 sizeSearch = m_searchButton->GetBestSize();
497 searchMargin = MARGIN;
498 }
499 if ( m_cancelButtonVisible )
500 {
501 sizeCancel = m_cancelButton->GetBestSize();
502 cancelMargin = MARGIN;
503 }
cfddeb10 504 m_searchButton->Show( m_searchButtonVisible || HasMenu() );
3f7f284d
RD
505 m_cancelButton->Show( m_cancelButtonVisible );
506
507 if ( sizeSearch.x + sizeCancel.x > width )
508 {
509 sizeSearch.x = width/2;
510 sizeCancel.x = width/2;
511 searchMargin = 0;
512 cancelMargin = 0;
513 }
e6d0599d 514 wxCoord textWidth = width - sizeSearch.x - sizeCancel.x - searchMargin - cancelMargin - 1;
d3ad3974 515 if (textWidth < 0) textWidth = 0;
3f7f284d
RD
516
517 // position the subcontrols inside the client area
518
6646ca90 519 m_searchButton->SetSize(x, y + ICON_OFFSET - 1, sizeSearch.x, height);
88c23b64 520 m_text->SetSize( x + sizeSearch.x + searchMargin,
10eeb875 521 y + ICON_OFFSET - BORDER,
88c23b64 522 textWidth,
10eeb875 523 height);
3f7f284d 524 m_cancelButton->SetSize(x + sizeSearch.x + searchMargin + textWidth + cancelMargin,
6646ca90 525 y + ICON_OFFSET - 1, sizeCancel.x, height);
3f7f284d
RD
526}
527
35368572
VZ
528wxWindowList wxSearchCtrl::GetCompositeWindowParts() const
529{
530 wxWindowList parts;
531 parts.push_back(m_text);
532 parts.push_back(m_searchButton);
533 parts.push_back(m_cancelButton);
534 return parts;
535}
3f7f284d
RD
536
537// accessors
538// ---------
539
135b23b2 540wxString wxSearchCtrl::DoGetValue() const
3f7f284d 541{
7ecc7eb6 542 return m_text->GetValue();
3f7f284d 543}
3f7f284d
RD
544wxString wxSearchCtrl::GetRange(long from, long to) const
545{
546 return m_text->GetRange(from, to);
547}
548
549int wxSearchCtrl::GetLineLength(long lineNo) const
550{
551 return m_text->GetLineLength(lineNo);
552}
553wxString wxSearchCtrl::GetLineText(long lineNo) const
554{
555 return m_text->GetLineText(lineNo);
556}
557int wxSearchCtrl::GetNumberOfLines() const
558{
559 return m_text->GetNumberOfLines();
560}
561
562bool wxSearchCtrl::IsModified() const
563{
564 return m_text->IsModified();
565}
566bool wxSearchCtrl::IsEditable() const
567{
568 return m_text->IsEditable();
569}
570
571// more readable flag testing methods
572bool wxSearchCtrl::IsSingleLine() const
573{
574 return m_text->IsSingleLine();
575}
576bool wxSearchCtrl::IsMultiLine() const
577{
578 return m_text->IsMultiLine();
579}
580
581// If the return values from and to are the same, there is no selection.
582void wxSearchCtrl::GetSelection(long* from, long* to) const
583{
584 m_text->GetSelection(from, to);
585}
586
587wxString wxSearchCtrl::GetStringSelection() const
588{
589 return m_text->GetStringSelection();
590}
591
592// operations
593// ----------
594
595// editing
596void wxSearchCtrl::Clear()
597{
598 m_text->Clear();
599}
600void wxSearchCtrl::Replace(long from, long to, const wxString& value)
601{
602 m_text->Replace(from, to, value);
603}
604void wxSearchCtrl::Remove(long from, long to)
605{
606 m_text->Remove(from, to);
607}
608
609// load/save the controls contents from/to the file
610bool wxSearchCtrl::LoadFile(const wxString& file)
611{
612 return m_text->LoadFile(file);
613}
614bool wxSearchCtrl::SaveFile(const wxString& file)
615{
616 return m_text->SaveFile(file);
617}
618
619// sets/clears the dirty flag
620void wxSearchCtrl::MarkDirty()
621{
622 m_text->MarkDirty();
623}
624void wxSearchCtrl::DiscardEdits()
625{
626 m_text->DiscardEdits();
627}
628
629// set the max number of characters which may be entered in a single line
630// text control
631void wxSearchCtrl::SetMaxLength(unsigned long len)
632{
633 m_text->SetMaxLength(len);
634}
635
636// writing text inserts it at the current position, appending always
637// inserts it at the end
638void wxSearchCtrl::WriteText(const wxString& text)
639{
640 m_text->WriteText(text);
641}
642void wxSearchCtrl::AppendText(const wxString& text)
643{
644 m_text->AppendText(text);
645}
646
647// insert the character which would have resulted from this key event,
648// return true if anything has been inserted
649bool wxSearchCtrl::EmulateKeyPress(const wxKeyEvent& event)
650{
651 return m_text->EmulateKeyPress(event);
652}
653
654// text control under some platforms supports the text styles: these
655// methods allow to apply the given text style to the given selection or to
656// set/get the style which will be used for all appended text
657bool wxSearchCtrl::SetStyle(long start, long end, const wxTextAttr& style)
658{
659 return m_text->SetStyle(start, end, style);
660}
661bool wxSearchCtrl::GetStyle(long position, wxTextAttr& style)
662{
663 return m_text->GetStyle(position, style);
664}
665bool wxSearchCtrl::SetDefaultStyle(const wxTextAttr& style)
666{
667 return m_text->SetDefaultStyle(style);
668}
669const wxTextAttr& wxSearchCtrl::GetDefaultStyle() const
670{
671 return m_text->GetDefaultStyle();
672}
673
674// translate between the position (which is just an index in the text ctrl
675// considering all its contents as a single strings) and (x, y) coordinates
676// which represent column and line.
677long wxSearchCtrl::XYToPosition(long x, long y) const
678{
679 return m_text->XYToPosition(x, y);
680}
681bool wxSearchCtrl::PositionToXY(long pos, long *x, long *y) const
682{
683 return m_text->PositionToXY(pos, x, y);
684}
685
686void wxSearchCtrl::ShowPosition(long pos)
687{
688 m_text->ShowPosition(pos);
689}
690
691// find the character at position given in pixels
692//
693// NB: pt is in device coords (not adjusted for the client area origin nor
694// scrolling)
695wxTextCtrlHitTestResult wxSearchCtrl::HitTest(const wxPoint& pt, long *pos) const
696{
697 return m_text->HitTest(pt, pos);
698}
699wxTextCtrlHitTestResult wxSearchCtrl::HitTest(const wxPoint& pt,
700 wxTextCoord *col,
701 wxTextCoord *row) const
702{
703 return m_text->HitTest(pt, col, row);
704}
705
706// Clipboard operations
707void wxSearchCtrl::Copy()
708{
709 m_text->Copy();
710}
711void wxSearchCtrl::Cut()
712{
713 m_text->Cut();
714}
715void wxSearchCtrl::Paste()
716{
717 m_text->Paste();
718}
719
720bool wxSearchCtrl::CanCopy() const
721{
722 return m_text->CanCopy();
723}
724bool wxSearchCtrl::CanCut() const
725{
726 return m_text->CanCut();
727}
728bool wxSearchCtrl::CanPaste() const
729{
730 return m_text->CanPaste();
731}
732
733// Undo/redo
734void wxSearchCtrl::Undo()
735{
736 m_text->Undo();
737}
738void wxSearchCtrl::Redo()
739{
740 m_text->Redo();
741}
742
743bool wxSearchCtrl::CanUndo() const
744{
745 return m_text->CanUndo();
746}
747bool wxSearchCtrl::CanRedo() const
748{
749 return m_text->CanRedo();
750}
751
752// Insertion point
753void wxSearchCtrl::SetInsertionPoint(long pos)
754{
755 m_text->SetInsertionPoint(pos);
756}
757void wxSearchCtrl::SetInsertionPointEnd()
758{
759 m_text->SetInsertionPointEnd();
760}
761long wxSearchCtrl::GetInsertionPoint() const
762{
763 return m_text->GetInsertionPoint();
764}
0b6a49c2 765long wxSearchCtrl::GetLastPosition() const
3f7f284d
RD
766{
767 return m_text->GetLastPosition();
768}
769
770void wxSearchCtrl::SetSelection(long from, long to)
771{
772 m_text->SetSelection(from, to);
773}
774void wxSearchCtrl::SelectAll()
775{
776 m_text->SelectAll();
777}
778
779void wxSearchCtrl::SetEditable(bool editable)
780{
781 m_text->SetEditable(editable);
782}
783
784bool wxSearchCtrl::SetFont(const wxFont& font)
785{
35368572
VZ
786 if ( !wxSearchCtrlBase::SetFont(font) )
787 return false;
788
789 // Recreate the bitmaps as their size may have changed.
3f7f284d 790 RecalcBitmaps();
35368572
VZ
791
792 return true;
793}
794
795bool wxSearchCtrl::SetBackgroundColour(const wxColour& colour)
796{
797 if ( !wxSearchCtrlBase::SetBackgroundColour(colour) )
798 return false;
799
800 // When the background changes, re-render the bitmaps so that the correct
801 // colour shows in their "transparent" area.
802 RecalcBitmaps();
803
804 return true;
3f7f284d
RD
805}
806
807// search control generic only
808void wxSearchCtrl::SetSearchBitmap( const wxBitmap& bitmap )
809{
810 m_searchBitmap = bitmap;
a1b806b9 811 m_searchBitmapUser = bitmap.IsOk();
3f7f284d
RD
812 if ( m_searchBitmapUser )
813 {
cfddeb10 814 if ( m_searchButton && !HasMenu() )
3f7f284d
RD
815 {
816 m_searchButton->SetBitmapLabel( m_searchBitmap );
817 }
818 }
819 else
820 {
821 // the user bitmap was just cleared, generate one
822 RecalcBitmaps();
823 }
824}
825
cfddeb10
VZ
826#if wxUSE_MENUS
827
3f7f284d
RD
828void wxSearchCtrl::SetSearchMenuBitmap( const wxBitmap& bitmap )
829{
830 m_searchMenuBitmap = bitmap;
a1b806b9 831 m_searchMenuBitmapUser = bitmap.IsOk();
3f7f284d
RD
832 if ( m_searchMenuBitmapUser )
833 {
834 if ( m_searchButton && m_menu )
835 {
836 m_searchButton->SetBitmapLabel( m_searchMenuBitmap );
837 }
838 }
839 else
840 {
841 // the user bitmap was just cleared, generate one
842 RecalcBitmaps();
843 }
844}
845
cfddeb10
VZ
846#endif // wxUSE_MENUS
847
3f7f284d
RD
848void wxSearchCtrl::SetCancelBitmap( const wxBitmap& bitmap )
849{
850 m_cancelBitmap = bitmap;
a1b806b9 851 m_cancelBitmapUser = bitmap.IsOk();
3f7f284d
RD
852 if ( m_cancelBitmapUser )
853 {
854 if ( m_cancelButton )
855 {
856 m_cancelButton->SetBitmapLabel( m_cancelBitmap );
857 }
858 }
859 else
860 {
861 // the user bitmap was just cleared, generate one
862 RecalcBitmaps();
863 }
864}
865
866#if 0
867
868// override streambuf method
869#if wxHAS_TEXT_WINDOW_STREAM
870int overflow(int i);
871#endif // wxHAS_TEXT_WINDOW_STREAM
872
873// stream-like insertion operators: these are always available, whether we
874// were, or not, compiled with streambuf support
875wxTextCtrl& operator<<(const wxString& s);
876wxTextCtrl& operator<<(int i);
877wxTextCtrl& operator<<(long i);
878wxTextCtrl& operator<<(float f);
879wxTextCtrl& operator<<(double d);
880wxTextCtrl& operator<<(const wxChar c);
881#endif
882
bdb7278a
VZ
883void wxSearchCtrl::DoSetValue(const wxString& value, int flags)
884{
885 m_text->DoSetValue(value, flags);
886}
887
888bool wxSearchCtrl::DoLoadFile(const wxString& file, int fileType)
889{
890 return m_text->DoLoadFile(file, fileType);
891}
892
893bool wxSearchCtrl::DoSaveFile(const wxString& file, int fileType)
894{
895 return m_text->DoSaveFile(file, fileType);
896}
897
3f7f284d
RD
898// do the window-specific processing after processing the update event
899void wxSearchCtrl::DoUpdateWindowUI(wxUpdateUIEvent& event)
900{
901 wxSearchCtrlBase::DoUpdateWindowUI(event);
902}
903
904bool wxSearchCtrl::ShouldInheritColours() const
905{
906 return true;
907}
908
909// icons are rendered at 3-8 times larger than necessary and downscaled for
910// antialiasing
911static int GetMultiplier()
912{
913#ifdef __WXWINCE__
914 // speed up bitmap generation by using a small bitmap
915 return 3;
916#else
917 int depth = ::wxDisplayDepth();
918
919 if ( depth >= 24 )
920 {
921 return 8;
922 }
923 return 6;
924#endif
925}
926
927wxBitmap wxSearchCtrl::RenderSearchBitmap( int x, int y, bool renderDrop )
928{
929 wxColour bg = GetBackgroundColour();
c892ae14 930 wxColour fg = GetForegroundColour().ChangeLightness(LIGHT_STEP-20);
3f7f284d
RD
931
932 //===============================================================================
933 // begin drawing code
934 //===============================================================================
935 // image stats
936
937 // force width:height ratio
938 if ( 14*x > y*20 )
939 {
940 // x is too big
941 x = y*20/14;
942 }
943 else
944 {
945 // y is too big
946 y = x*14/20;
947 }
948
949 // glass 11x11, top left corner
950 // handle (9,9)-(13,13)
951 // drop (13,16)-(19,6)-(16,9)
952
953 int multiplier = GetMultiplier();
954 int penWidth = multiplier * 2;
955
956 penWidth = penWidth * x / 20;
957
958 wxBitmap bitmap( multiplier*x, multiplier*y );
959 wxMemoryDC mem;
960 mem.SelectObject(bitmap);
961
962 // clear background
963 mem.SetBrush( wxBrush(bg) );
964 mem.SetPen( wxPen(bg) );
965 mem.DrawRectangle(0,0,bitmap.GetWidth(),bitmap.GetHeight());
966
967 // draw drop glass
968 mem.SetBrush( wxBrush(fg) );
969 mem.SetPen( wxPen(fg) );
970 int glassBase = 5 * x / 20;
971 int glassFactor = 2*glassBase + 1;
972 int radius = multiplier*glassFactor/2;
973 mem.DrawCircle(radius,radius,radius);
974 mem.SetBrush( wxBrush(bg) );
975 mem.SetPen( wxPen(bg) );
976 mem.DrawCircle(radius,radius,radius-penWidth);
977
978 // draw handle
979 int lineStart = radius + (radius-penWidth/2) * 707 / 1000; // 707 / 1000 = 0.707 = 1/sqrt(2);
980
981 mem.SetPen( wxPen(fg) );
982 mem.SetBrush( wxBrush(fg) );
983 int handleCornerShift = penWidth * 707 / 1000 / 2; // 707 / 1000 = 0.707 = 1/sqrt(2);
984 handleCornerShift = WXMAX( handleCornerShift, 1 );
985 int handleBase = 4 * x / 20;
986 int handleLength = 2*handleBase+1;
987 wxPoint handlePolygon[] =
988 {
989 wxPoint(-handleCornerShift,+handleCornerShift),
990 wxPoint(+handleCornerShift,-handleCornerShift),
991 wxPoint(multiplier*handleLength/2+handleCornerShift,multiplier*handleLength/2-handleCornerShift),
992 wxPoint(multiplier*handleLength/2-handleCornerShift,multiplier*handleLength/2+handleCornerShift),
993 };
994 mem.DrawPolygon(WXSIZEOF(handlePolygon),handlePolygon,lineStart,lineStart);
995
996 // draw drop triangle
997 int triangleX = 13 * x / 20;
998 int triangleY = 5 * x / 20;
999 int triangleBase = 3 * x / 20;
1000 int triangleFactor = triangleBase*2+1;
1001 if ( renderDrop )
1002 {
1003 wxPoint dropPolygon[] =
1004 {
1005 wxPoint(multiplier*0,multiplier*0), // triangle left
1006 wxPoint(multiplier*triangleFactor-1,multiplier*0), // triangle right
1007 wxPoint(multiplier*triangleFactor/2,multiplier*triangleFactor/2), // triangle bottom
1008 };
1009 mem.DrawPolygon(WXSIZEOF(dropPolygon),dropPolygon,multiplier*triangleX,multiplier*triangleY);
1010 }
6646ca90 1011 mem.SelectObject(wxNullBitmap);
88c23b64 1012
3f7f284d
RD
1013 //===============================================================================
1014 // end drawing code
1015 //===============================================================================
1016
1017 if ( multiplier != 1 )
1018 {
1019 wxImage image = bitmap.ConvertToImage();
1020 image.Rescale(x,y);
1021 bitmap = wxBitmap( image );
1022 }
6646ca90
RD
1023 if ( !renderDrop )
1024 {
1025 // Trim the edge where the arrow would have gone
1026 bitmap = bitmap.GetSubBitmap(wxRect(0,0, y,y));
1027 }
3f7f284d
RD
1028
1029 return bitmap;
1030}
1031
1032wxBitmap wxSearchCtrl::RenderCancelBitmap( int x, int y )
1033{
1034 wxColour bg = GetBackgroundColour();
c892ae14 1035 wxColour fg = GetForegroundColour().ChangeLightness(LIGHT_STEP);
3f7f284d
RD
1036
1037 //===============================================================================
1038 // begin drawing code
1039 //===============================================================================
1040 // image stats
1041
1042 // total size 14x14
1043 // force 1:1 ratio
1044 if ( x > y )
1045 {
1046 // x is too big
1047 x = y;
1048 }
1049 else
1050 {
1051 // y is too big
1052 y = x;
1053 }
1054
1055 // 14x14 circle
1056 // cross line starts (4,4)-(10,10)
1057 // drop (13,16)-(19,6)-(16,9)
1058
1059 int multiplier = GetMultiplier();
1060
1061 int penWidth = multiplier * x / 14;
1062
1063 wxBitmap bitmap( multiplier*x, multiplier*y );
1064 wxMemoryDC mem;
1065 mem.SelectObject(bitmap);
1066
1067 // clear background
1068 mem.SetBrush( wxBrush(bg) );
1069 mem.SetPen( wxPen(bg) );
1070 mem.DrawRectangle(0,0,bitmap.GetWidth(),bitmap.GetHeight());
1071
1072 // draw drop glass
1073 mem.SetBrush( wxBrush(fg) );
1074 mem.SetPen( wxPen(fg) );
1075 int radius = multiplier*x/2;
1076 mem.DrawCircle(radius,radius,radius);
1077
1078 // draw cross
1079 int lineStartBase = 4 * x / 14;
1080 int lineLength = x - 2*lineStartBase;
1081
1082 mem.SetPen( wxPen(bg) );
1083 mem.SetBrush( wxBrush(bg) );
1084 int handleCornerShift = penWidth/2;
1085 handleCornerShift = WXMAX( handleCornerShift, 1 );
1086 wxPoint handlePolygon[] =
1087 {
1088 wxPoint(-handleCornerShift,+handleCornerShift),
1089 wxPoint(+handleCornerShift,-handleCornerShift),
1090 wxPoint(multiplier*lineLength+handleCornerShift,multiplier*lineLength-handleCornerShift),
1091 wxPoint(multiplier*lineLength-handleCornerShift,multiplier*lineLength+handleCornerShift),
1092 };
1093 mem.DrawPolygon(WXSIZEOF(handlePolygon),handlePolygon,multiplier*lineStartBase,multiplier*lineStartBase);
1094 wxPoint handlePolygon2[] =
1095 {
1096 wxPoint(+handleCornerShift,+handleCornerShift),
1097 wxPoint(-handleCornerShift,-handleCornerShift),
1098 wxPoint(multiplier*lineLength-handleCornerShift,-multiplier*lineLength-handleCornerShift),
1099 wxPoint(multiplier*lineLength+handleCornerShift,-multiplier*lineLength+handleCornerShift),
1100 };
1101 mem.DrawPolygon(WXSIZEOF(handlePolygon2),handlePolygon2,multiplier*lineStartBase,multiplier*(x-lineStartBase));
1102
1103 //===============================================================================
1104 // end drawing code
1105 //===============================================================================
1106
1107 if ( multiplier != 1 )
1108 {
1109 wxImage image = bitmap.ConvertToImage();
1110 image.Rescale(x,y);
1111 bitmap = wxBitmap( image );
1112 }
1113
1114 return bitmap;
1115}
1116
1117void wxSearchCtrl::RecalcBitmaps()
1118{
1119 if ( !m_text )
1120 {
1121 return;
1122 }
1123 wxSize sizeText = m_text->GetBestSize();
1124
1125 int bitmapHeight = sizeText.y - 2 * ICON_MARGIN;
1126 int bitmapWidth = sizeText.y * 20 / 14;
1127
1128 if ( !m_searchBitmapUser )
1129 {
8bc333d7 1130 if (
a1b806b9 1131 !m_searchBitmap.IsOk() ||
8bc333d7 1132 m_searchBitmap.GetHeight() != bitmapHeight ||
3f7f284d
RD
1133 m_searchBitmap.GetWidth() != bitmapWidth
1134 )
1135 {
1136 m_searchBitmap = RenderSearchBitmap(bitmapWidth,bitmapHeight,false);
cfddeb10 1137 if ( !HasMenu() )
3f7f284d
RD
1138 {
1139 m_searchButton->SetBitmapLabel(m_searchBitmap);
1140 }
1141 }
1142 // else this bitmap was set by user, don't alter
1143 }
1144
cfddeb10 1145#if wxUSE_MENUS
3f7f284d
RD
1146 if ( !m_searchMenuBitmapUser )
1147 {
8bc333d7 1148 if (
a1b806b9 1149 !m_searchMenuBitmap.IsOk() ||
8bc333d7 1150 m_searchMenuBitmap.GetHeight() != bitmapHeight ||
3f7f284d
RD
1151 m_searchMenuBitmap.GetWidth() != bitmapWidth
1152 )
1153 {
1154 m_searchMenuBitmap = RenderSearchBitmap(bitmapWidth,bitmapHeight,true);
1155 if ( m_menu )
1156 {
1157 m_searchButton->SetBitmapLabel(m_searchMenuBitmap);
1158 }
1159 }
1160 // else this bitmap was set by user, don't alter
1161 }
cfddeb10 1162#endif // wxUSE_MENUS
3f7f284d
RD
1163
1164 if ( !m_cancelBitmapUser )
1165 {
8bc333d7 1166 if (
a1b806b9 1167 !m_cancelBitmap.IsOk() ||
8bc333d7 1168 m_cancelBitmap.GetHeight() != bitmapHeight ||
3f7f284d
RD
1169 m_cancelBitmap.GetWidth() != bitmapHeight
1170 )
1171 {
e6d0599d 1172 m_cancelBitmap = RenderCancelBitmap(bitmapHeight-BORDER-1,bitmapHeight-BORDER-1); // square
3f7f284d
RD
1173 m_cancelButton->SetBitmapLabel(m_cancelBitmap);
1174 }
1175 // else this bitmap was set by user, don't alter
1176 }
1177}
1178
639dbb6d 1179void wxSearchCtrl::OnCancelButton( wxCommandEvent& event )
3f7f284d 1180{
e5eff438 1181 m_text->Clear();
639dbb6d 1182 event.Skip();
3f7f284d
RD
1183}
1184
1185void wxSearchCtrl::OnSetFocus( wxFocusEvent& /*event*/ )
1186{
1187 if ( m_text )
1188 {
1189 m_text->SetFocus();
1190 }
1191}
1192
8ebf2465 1193void wxSearchCtrl::OnSize( wxSizeEvent& WXUNUSED(event) )
af1f44f1
RD
1194{
1195 int width, height;
1196 GetSize(&width, &height);
1197 LayoutControls(0, 0, width, height);
1198}
1199
cfddeb10
VZ
1200#if wxUSE_MENUS
1201
3f7f284d
RD
1202void wxSearchCtrl::PopupSearchMenu()
1203{
1204 if ( m_menu )
1205 {
1206 wxSize size = GetSize();
1207 PopupMenu( m_menu, 0, size.y );
1208 }
1209}
1210
cfddeb10
VZ
1211#endif // wxUSE_MENUS
1212
5b43c75c 1213#endif // !wxUSE_NATIVE_SEARCH_CONTROL
3f7f284d
RD
1214
1215#endif // wxUSE_SEARCHCTRL