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