1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/srchctlg.cpp
3 // Purpose: implements wxSearchCtrl as a composite control
4 // Author: Vince Harron
7 // Copyright: Vince Harron
8 // License: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/srchctrl.h"
23 #include "wx/button.h"
24 #include "wx/dcclient.h"
26 #include "wx/dcmemory.h"
29 #if !wxUSE_NATIVE_SEARCH_CONTROL
33 #define WXMAX(a,b) ((a)>(b)?(a):(b))
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // the margin between the text control and the search/cancel buttons
40 static const wxCoord MARGIN
= 2;
42 // border around all controls to compensate for wxSIMPLE_BORDER
43 #if defined(__WXMSW__)
44 static const wxCoord BORDER
= 0;
45 static const wxCoord ICON_MARGIN
= 2;
46 static const wxCoord ICON_OFFSET
= 2;
48 static const wxCoord BORDER
= 2;
49 static const wxCoord ICON_MARGIN
= 0;
50 static const wxCoord ICON_OFFSET
= 0;
53 // ----------------------------------------------------------------------------
54 // TODO: These functions or something like them should probably be made
55 // public. There are similar functions in src/aui/dockart.cpp...
57 static double wxBlendColour(double fg
, double bg
, double alpha
)
59 double result
= bg
+ (alpha
* (fg
- bg
));
67 static wxColor
wxStepColour(const wxColor
& c
, int ialpha
)
72 double r
= c
.Red(), g
= c
.Green(), b
= c
.Blue();
75 // ialpha is 0..200 where 0 is completely black
76 // and 200 is completely white and 100 is the same
77 // convert that to normal alpha 0.0 - 1.0
78 ialpha
= wxMin(ialpha
, 200);
79 ialpha
= wxMax(ialpha
, 0);
80 double alpha
= ((double)(ialpha
- 100.0))/100.0;
86 alpha
= 1.0 - alpha
; // 0 = transparent fg; 1 = opaque fg
92 alpha
= 1.0 + alpha
; // 0 = transparent fg; 1 = opaque fg
95 r
= wxBlendColour(r
, bg
, alpha
);
96 g
= wxBlendColour(g
, bg
, alpha
);
97 b
= wxBlendColour(b
, bg
, alpha
);
99 return wxColour((unsigned char)r
, (unsigned char)g
, (unsigned char)b
);
102 #define LIGHT_STEP 160
104 // ----------------------------------------------------------------------------
105 // wxSearchTextCtrl: text control used by search control
106 // ----------------------------------------------------------------------------
108 class wxSearchTextCtrl
: public wxTextCtrl
111 wxSearchTextCtrl(wxSearchCtrl
*search
, const wxString
& value
, int style
)
112 : wxTextCtrl(search
, wxID_ANY
, value
, wxDefaultPosition
, wxDefaultSize
,
116 m_defaultFG
= GetForegroundColour();
118 // remove the default minsize, the searchctrl will have one instead
119 SetSizeHints(wxDefaultCoord
,wxDefaultCoord
);
122 void SetDescriptiveText(const wxString
& text
)
124 if ( GetValue() == m_descriptiveText
)
126 ChangeValue(wxEmptyString
);
129 m_descriptiveText
= text
;
132 wxString
GetDescriptiveText() const
134 return m_descriptiveText
;
138 // provide access to the base class protected methods to wxSearchCtrl which
139 // needs to forward to them
140 using wxTextCtrl::DoSetValue
;
141 using wxTextCtrl::DoLoadFile
;
142 using wxTextCtrl::DoSaveFile
;
145 void OnText(wxCommandEvent
& eventText
)
147 wxCommandEvent
event(eventText
);
148 event
.SetEventObject(m_search
);
149 event
.SetId(m_search
->GetId());
151 m_search
->GetEventHandler()->ProcessEvent(event
);
154 void OnTextUrl(wxTextUrlEvent
& eventText
)
156 // copy constructor is disabled for some reason?
157 //wxTextUrlEvent event(eventText);
158 wxTextUrlEvent
event(
160 eventText
.GetMouseEvent(),
161 eventText
.GetURLStart(),
162 eventText
.GetURLEnd()
164 event
.SetEventObject(m_search
);
166 m_search
->GetEventHandler()->ProcessEvent(event
);
169 void OnIdle(wxIdleEvent
& WXUNUSED(event
))
171 if ( IsEmpty() && !(wxWindow::FindFocus() == this) )
173 ChangeValue(m_descriptiveText
);
174 SetInsertionPoint(0);
175 SetForegroundColour(wxStepColour(m_defaultFG
, LIGHT_STEP
));
179 void OnFocus(wxFocusEvent
& event
)
182 if ( GetValue() == m_descriptiveText
)
184 ChangeValue(wxEmptyString
);
185 SetForegroundColour(m_defaultFG
);
190 wxSearchCtrl
* m_search
;
191 wxString m_descriptiveText
;
192 wxColour m_defaultFG
;
194 DECLARE_EVENT_TABLE()
197 BEGIN_EVENT_TABLE(wxSearchTextCtrl
, wxTextCtrl
)
198 EVT_TEXT(wxID_ANY
, wxSearchTextCtrl::OnText
)
199 EVT_TEXT_ENTER(wxID_ANY
, wxSearchTextCtrl::OnText
)
200 EVT_TEXT_URL(wxID_ANY
, wxSearchTextCtrl::OnTextUrl
)
201 EVT_TEXT_MAXLEN(wxID_ANY
, wxSearchTextCtrl::OnText
)
202 EVT_IDLE(wxSearchTextCtrl::OnIdle
)
203 EVT_SET_FOCUS(wxSearchTextCtrl::OnFocus
)
206 // ----------------------------------------------------------------------------
207 // wxSearchButton: search button used by search control
208 // ----------------------------------------------------------------------------
210 class wxSearchButton
: public wxControl
213 wxSearchButton(wxSearchCtrl
*search
, int eventType
, const wxBitmap
& bmp
)
214 : wxControl(search
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxNO_BORDER
),
216 m_eventType(eventType
),
220 void SetBitmapLabel(const wxBitmap
& label
) { m_bmp
= label
; }
224 wxSize
DoGetBestSize() const
226 return wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight());
229 void OnLeftUp(wxMouseEvent
&)
231 wxCommandEvent
event(m_eventType
, m_search
->GetId());
232 event
.SetEventObject(m_search
);
234 GetEventHandler()->ProcessEvent(event
);
236 m_search
->SetFocus();
239 if ( m_eventType
== wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
)
241 // this happens automatically, just like on Mac OS X
242 m_search
->PopupSearchMenu();
244 #endif // wxUSE_MENUS
247 void OnPaint(wxPaintEvent
&)
250 dc
.DrawBitmap(m_bmp
, 0,0, true);
255 wxSearchCtrl
*m_search
;
256 wxEventType m_eventType
;
259 DECLARE_EVENT_TABLE()
262 BEGIN_EVENT_TABLE(wxSearchButton
, wxControl
)
263 EVT_LEFT_UP(wxSearchButton::OnLeftUp
)
264 EVT_PAINT(wxSearchButton::OnPaint
)
267 BEGIN_EVENT_TABLE(wxSearchCtrl
, wxSearchCtrlBase
)
268 EVT_SEARCHCTRL_SEARCH_BTN(wxID_ANY
, wxSearchCtrl::OnSearchButton
)
269 EVT_SET_FOCUS(wxSearchCtrl::OnSetFocus
)
270 EVT_SIZE(wxSearchCtrl::OnSize
)
273 IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl
, wxSearchCtrlBase
)
275 // ============================================================================
277 // ============================================================================
279 // ----------------------------------------------------------------------------
280 // wxSearchCtrl creation
281 // ----------------------------------------------------------------------------
286 wxSearchCtrl::wxSearchCtrl()
291 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
292 const wxString
& value
,
296 const wxValidator
& validator
,
297 const wxString
& name
)
301 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
304 void wxSearchCtrl::Init()
307 m_searchButton
= NULL
;
308 m_cancelButton
= NULL
;
311 #endif // wxUSE_MENUS
313 m_searchButtonVisible
= true;
314 m_cancelButtonVisible
= false;
316 m_searchBitmapUser
= false;
317 m_cancelBitmapUser
= false;
319 m_searchMenuBitmapUser
= false;
320 #endif // wxUSE_MENUS
323 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
324 const wxString
& value
,
328 const wxValidator
& validator
,
329 const wxString
& name
)
331 // force border style for more native appearance
332 style
&= ~wxBORDER_MASK
;
334 style
|= wxBORDER_SUNKEN
;
335 #elif defined(__WXMSW__)
336 // Don't set the style explicitly, let GetDefaultBorder() work it out, unless
337 // we will get a sunken border (e.g. on Windows 200) in which case we must
338 // override with a simple border.
339 if (GetDefaultBorder() == wxBORDER_SUNKEN
)
340 style
|= wxBORDER_SIMPLE
;
342 style
|= wxBORDER_SIMPLE
;
344 if ( !wxSearchCtrlBaseBaseClass::Create(parent
, id
, pos
, size
,
345 style
, validator
, name
) )
350 m_text
= new wxSearchTextCtrl(this, value
, style
& ~wxBORDER_MASK
);
351 m_text
->SetDescriptiveText(_("Search"));
353 m_searchButton
= new wxSearchButton(this,
354 wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
,
356 m_cancelButton
= new wxSearchButton(this,
357 wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN
,
360 SetForegroundColour( m_text
->GetForegroundColour() );
361 m_searchButton
->SetForegroundColour( m_text
->GetForegroundColour() );
362 m_cancelButton
->SetForegroundColour( m_text
->GetForegroundColour() );
364 SetBackgroundColour( m_text
->GetBackgroundColour() );
365 m_searchButton
->SetBackgroundColour( m_text
->GetBackgroundColour() );
366 m_cancelButton
->SetBackgroundColour( m_text
->GetBackgroundColour() );
370 SetInitialSize(size
);
375 wxSearchCtrl::~wxSearchCtrl()
378 delete m_searchButton
;
379 delete m_cancelButton
;
382 #endif // wxUSE_MENUS
386 // search control specific interfaces
389 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
391 if ( menu
== m_menu
)
396 bool hadMenu
= (m_menu
!= NULL
);
400 if ( m_menu
&& !hadMenu
)
402 m_searchButton
->SetBitmapLabel(m_searchMenuBitmap
);
403 m_searchButton
->Refresh();
405 else if ( !m_menu
&& hadMenu
)
407 m_searchButton
->SetBitmapLabel(m_searchBitmap
);
408 if ( m_searchButtonVisible
)
410 m_searchButton
->Refresh();
413 wxRect rect
= GetRect();
414 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
417 wxMenu
* wxSearchCtrl::GetMenu()
422 #endif // wxUSE_MENUS
424 void wxSearchCtrl::ShowSearchButton( bool show
)
426 if ( m_searchButtonVisible
== show
)
431 m_searchButtonVisible
= show
;
432 if ( m_searchButtonVisible
)
437 wxRect rect
= GetRect();
438 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
441 bool wxSearchCtrl::IsSearchButtonVisible() const
443 return m_searchButtonVisible
;
447 void wxSearchCtrl::ShowCancelButton( bool show
)
449 if ( m_cancelButtonVisible
== show
)
454 m_cancelButtonVisible
= show
;
456 wxRect rect
= GetRect();
457 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
460 bool wxSearchCtrl::IsCancelButtonVisible() const
462 return m_cancelButtonVisible
;
465 void wxSearchCtrl::SetDescriptiveText(const wxString
& text
)
467 m_text
->SetDescriptiveText(text
);
470 wxString
wxSearchCtrl::GetDescriptiveText() const
472 return m_text
->GetDescriptiveText();
475 // ----------------------------------------------------------------------------
477 // ----------------------------------------------------------------------------
479 wxSize
wxSearchCtrl::DoGetBestSize() const
481 wxSize sizeText
= m_text
->GetBestSize();
482 wxSize
sizeSearch(0,0);
483 wxSize
sizeCancel(0,0);
484 int searchMargin
= 0;
485 int cancelMargin
= 0;
486 if ( m_searchButtonVisible
|| HasMenu() )
488 sizeSearch
= m_searchButton
->GetBestSize();
489 searchMargin
= MARGIN
;
491 if ( m_cancelButtonVisible
)
493 sizeCancel
= m_cancelButton
->GetBestSize();
494 cancelMargin
= MARGIN
;
497 int horizontalBorder
= 1 + ( sizeText
.y
- sizeText
.y
* 14 / 21 ) / 2;
499 // buttons are square and equal to the height of the text control
500 int height
= sizeText
.y
;
501 return wxSize(sizeSearch
.x
+ searchMargin
+ sizeText
.x
+ cancelMargin
+ sizeCancel
.x
+ 2*horizontalBorder
,
505 void wxSearchCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
507 wxSearchCtrlBase::DoMoveWindow(x
, y
, width
, height
);
509 LayoutControls(0, 0, width
, height
);
512 void wxSearchCtrl::LayoutControls(int x
, int y
, int width
, int height
)
517 wxSize sizeText
= m_text
->GetBestSize();
518 // make room for the search menu & clear button
519 int horizontalBorder
= ( sizeText
.y
- sizeText
.y
* 14 / 21 ) / 2;
520 x
+= horizontalBorder
;
522 width
-= horizontalBorder
*2;
525 wxSize
sizeSearch(0,0);
526 wxSize
sizeCancel(0,0);
527 int searchMargin
= 0;
528 int cancelMargin
= 0;
529 if ( m_searchButtonVisible
|| HasMenu() )
531 sizeSearch
= m_searchButton
->GetBestSize();
532 searchMargin
= MARGIN
;
534 if ( m_cancelButtonVisible
)
536 sizeCancel
= m_cancelButton
->GetBestSize();
537 cancelMargin
= MARGIN
;
539 m_searchButton
->Show( m_searchButtonVisible
|| HasMenu() );
540 m_cancelButton
->Show( m_cancelButtonVisible
);
542 if ( sizeSearch
.x
+ sizeCancel
.x
> width
)
544 sizeSearch
.x
= width
/2;
545 sizeCancel
.x
= width
/2;
549 wxCoord textWidth
= width
- sizeSearch
.x
- sizeCancel
.x
- searchMargin
- cancelMargin
- 1;
551 // position the subcontrols inside the client area
553 m_searchButton
->SetSize(x
, y
+ ICON_OFFSET
- 1, sizeSearch
.x
, height
);
554 m_text
->SetSize( x
+ sizeSearch
.x
+ searchMargin
,
555 y
+ ICON_OFFSET
- BORDER
,
558 m_cancelButton
->SetSize(x
+ sizeSearch
.x
+ searchMargin
+ textWidth
+ cancelMargin
,
559 y
+ ICON_OFFSET
- 1, sizeCancel
.x
, height
);
566 wxString
wxSearchCtrl::GetValue() const
568 wxString value
= m_text
->GetValue();
569 if (value
== m_text
->GetDescriptiveText())
570 return wxEmptyString
;
574 void wxSearchCtrl::SetValue(const wxString
& value
)
576 m_text
->SetValue(value
);
579 wxString
wxSearchCtrl::GetRange(long from
, long to
) const
581 return m_text
->GetRange(from
, to
);
584 int wxSearchCtrl::GetLineLength(long lineNo
) const
586 return m_text
->GetLineLength(lineNo
);
588 wxString
wxSearchCtrl::GetLineText(long lineNo
) const
590 return m_text
->GetLineText(lineNo
);
592 int wxSearchCtrl::GetNumberOfLines() const
594 return m_text
->GetNumberOfLines();
597 bool wxSearchCtrl::IsModified() const
599 return m_text
->IsModified();
601 bool wxSearchCtrl::IsEditable() const
603 return m_text
->IsEditable();
606 // more readable flag testing methods
607 bool wxSearchCtrl::IsSingleLine() const
609 return m_text
->IsSingleLine();
611 bool wxSearchCtrl::IsMultiLine() const
613 return m_text
->IsMultiLine();
616 // If the return values from and to are the same, there is no selection.
617 void wxSearchCtrl::GetSelection(long* from
, long* to
) const
619 m_text
->GetSelection(from
, to
);
622 wxString
wxSearchCtrl::GetStringSelection() const
624 return m_text
->GetStringSelection();
631 void wxSearchCtrl::Clear()
635 void wxSearchCtrl::Replace(long from
, long to
, const wxString
& value
)
637 m_text
->Replace(from
, to
, value
);
639 void wxSearchCtrl::Remove(long from
, long to
)
641 m_text
->Remove(from
, to
);
644 // load/save the controls contents from/to the file
645 bool wxSearchCtrl::LoadFile(const wxString
& file
)
647 return m_text
->LoadFile(file
);
649 bool wxSearchCtrl::SaveFile(const wxString
& file
)
651 return m_text
->SaveFile(file
);
654 // sets/clears the dirty flag
655 void wxSearchCtrl::MarkDirty()
659 void wxSearchCtrl::DiscardEdits()
661 m_text
->DiscardEdits();
664 // set the max number of characters which may be entered in a single line
666 void wxSearchCtrl::SetMaxLength(unsigned long len
)
668 m_text
->SetMaxLength(len
);
671 // writing text inserts it at the current position, appending always
672 // inserts it at the end
673 void wxSearchCtrl::WriteText(const wxString
& text
)
675 m_text
->WriteText(text
);
677 void wxSearchCtrl::AppendText(const wxString
& text
)
679 m_text
->AppendText(text
);
682 // insert the character which would have resulted from this key event,
683 // return true if anything has been inserted
684 bool wxSearchCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
686 return m_text
->EmulateKeyPress(event
);
689 // text control under some platforms supports the text styles: these
690 // methods allow to apply the given text style to the given selection or to
691 // set/get the style which will be used for all appended text
692 bool wxSearchCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
694 return m_text
->SetStyle(start
, end
, style
);
696 bool wxSearchCtrl::GetStyle(long position
, wxTextAttr
& style
)
698 return m_text
->GetStyle(position
, style
);
700 bool wxSearchCtrl::SetDefaultStyle(const wxTextAttr
& style
)
702 return m_text
->SetDefaultStyle(style
);
704 const wxTextAttr
& wxSearchCtrl::GetDefaultStyle() const
706 return m_text
->GetDefaultStyle();
709 // translate between the position (which is just an index in the text ctrl
710 // considering all its contents as a single strings) and (x, y) coordinates
711 // which represent column and line.
712 long wxSearchCtrl::XYToPosition(long x
, long y
) const
714 return m_text
->XYToPosition(x
, y
);
716 bool wxSearchCtrl::PositionToXY(long pos
, long *x
, long *y
) const
718 return m_text
->PositionToXY(pos
, x
, y
);
721 void wxSearchCtrl::ShowPosition(long pos
)
723 m_text
->ShowPosition(pos
);
726 // find the character at position given in pixels
728 // NB: pt is in device coords (not adjusted for the client area origin nor
730 wxTextCtrlHitTestResult
wxSearchCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
732 return m_text
->HitTest(pt
, pos
);
734 wxTextCtrlHitTestResult
wxSearchCtrl::HitTest(const wxPoint
& pt
,
736 wxTextCoord
*row
) const
738 return m_text
->HitTest(pt
, col
, row
);
741 // Clipboard operations
742 void wxSearchCtrl::Copy()
746 void wxSearchCtrl::Cut()
750 void wxSearchCtrl::Paste()
755 bool wxSearchCtrl::CanCopy() const
757 return m_text
->CanCopy();
759 bool wxSearchCtrl::CanCut() const
761 return m_text
->CanCut();
763 bool wxSearchCtrl::CanPaste() const
765 return m_text
->CanPaste();
769 void wxSearchCtrl::Undo()
773 void wxSearchCtrl::Redo()
778 bool wxSearchCtrl::CanUndo() const
780 return m_text
->CanUndo();
782 bool wxSearchCtrl::CanRedo() const
784 return m_text
->CanRedo();
788 void wxSearchCtrl::SetInsertionPoint(long pos
)
790 m_text
->SetInsertionPoint(pos
);
792 void wxSearchCtrl::SetInsertionPointEnd()
794 m_text
->SetInsertionPointEnd();
796 long wxSearchCtrl::GetInsertionPoint() const
798 return m_text
->GetInsertionPoint();
800 wxTextPos
wxSearchCtrl::GetLastPosition() const
802 return m_text
->GetLastPosition();
805 void wxSearchCtrl::SetSelection(long from
, long to
)
807 m_text
->SetSelection(from
, to
);
809 void wxSearchCtrl::SelectAll()
814 void wxSearchCtrl::SetEditable(bool editable
)
816 m_text
->SetEditable(editable
);
819 bool wxSearchCtrl::SetFont(const wxFont
& font
)
821 bool result
= wxSearchCtrlBase::SetFont(font
);
822 if ( result
&& m_text
)
824 result
= m_text
->SetFont(font
);
830 // search control generic only
831 void wxSearchCtrl::SetSearchBitmap( const wxBitmap
& bitmap
)
833 m_searchBitmap
= bitmap
;
834 m_searchBitmapUser
= bitmap
.Ok();
835 if ( m_searchBitmapUser
)
837 if ( m_searchButton
&& !HasMenu() )
839 m_searchButton
->SetBitmapLabel( m_searchBitmap
);
844 // the user bitmap was just cleared, generate one
851 void wxSearchCtrl::SetSearchMenuBitmap( const wxBitmap
& bitmap
)
853 m_searchMenuBitmap
= bitmap
;
854 m_searchMenuBitmapUser
= bitmap
.Ok();
855 if ( m_searchMenuBitmapUser
)
857 if ( m_searchButton
&& m_menu
)
859 m_searchButton
->SetBitmapLabel( m_searchMenuBitmap
);
864 // the user bitmap was just cleared, generate one
869 #endif // wxUSE_MENUS
871 void wxSearchCtrl::SetCancelBitmap( const wxBitmap
& bitmap
)
873 m_cancelBitmap
= bitmap
;
874 m_cancelBitmapUser
= bitmap
.Ok();
875 if ( m_cancelBitmapUser
)
877 if ( m_cancelButton
)
879 m_cancelButton
->SetBitmapLabel( m_cancelBitmap
);
884 // the user bitmap was just cleared, generate one
891 // override streambuf method
892 #if wxHAS_TEXT_WINDOW_STREAM
894 #endif // wxHAS_TEXT_WINDOW_STREAM
896 // stream-like insertion operators: these are always available, whether we
897 // were, or not, compiled with streambuf support
898 wxTextCtrl
& operator<<(const wxString
& s
);
899 wxTextCtrl
& operator<<(int i
);
900 wxTextCtrl
& operator<<(long i
);
901 wxTextCtrl
& operator<<(float f
);
902 wxTextCtrl
& operator<<(double d
);
903 wxTextCtrl
& operator<<(const wxChar c
);
906 void wxSearchCtrl::DoSetValue(const wxString
& value
, int flags
)
908 m_text
->DoSetValue(value
, flags
);
911 bool wxSearchCtrl::DoLoadFile(const wxString
& file
, int fileType
)
913 return m_text
->DoLoadFile(file
, fileType
);
916 bool wxSearchCtrl::DoSaveFile(const wxString
& file
, int fileType
)
918 return m_text
->DoSaveFile(file
, fileType
);
921 // do the window-specific processing after processing the update event
922 void wxSearchCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
924 wxSearchCtrlBase::DoUpdateWindowUI(event
);
927 bool wxSearchCtrl::ShouldInheritColours() const
932 // icons are rendered at 3-8 times larger than necessary and downscaled for
934 static int GetMultiplier()
937 // speed up bitmap generation by using a small bitmap
940 int depth
= ::wxDisplayDepth();
950 wxBitmap
wxSearchCtrl::RenderSearchBitmap( int x
, int y
, bool renderDrop
)
952 wxColour bg
= GetBackgroundColour();
953 wxColour fg
= wxStepColour(GetForegroundColour(), LIGHT_STEP
-20);
955 //===============================================================================
956 // begin drawing code
957 //===============================================================================
960 // force width:height ratio
972 // glass 11x11, top left corner
973 // handle (9,9)-(13,13)
974 // drop (13,16)-(19,6)-(16,9)
976 int multiplier
= GetMultiplier();
977 int penWidth
= multiplier
* 2;
979 penWidth
= penWidth
* x
/ 20;
981 wxBitmap
bitmap( multiplier
*x
, multiplier
*y
);
983 mem
.SelectObject(bitmap
);
986 mem
.SetBrush( wxBrush(bg
) );
987 mem
.SetPen( wxPen(bg
) );
988 mem
.DrawRectangle(0,0,bitmap
.GetWidth(),bitmap
.GetHeight());
991 mem
.SetBrush( wxBrush(fg
) );
992 mem
.SetPen( wxPen(fg
) );
993 int glassBase
= 5 * x
/ 20;
994 int glassFactor
= 2*glassBase
+ 1;
995 int radius
= multiplier
*glassFactor
/2;
996 mem
.DrawCircle(radius
,radius
,radius
);
997 mem
.SetBrush( wxBrush(bg
) );
998 mem
.SetPen( wxPen(bg
) );
999 mem
.DrawCircle(radius
,radius
,radius
-penWidth
);
1002 int lineStart
= radius
+ (radius
-penWidth
/2) * 707 / 1000; // 707 / 1000 = 0.707 = 1/sqrt(2);
1004 mem
.SetPen( wxPen(fg
) );
1005 mem
.SetBrush( wxBrush(fg
) );
1006 int handleCornerShift
= penWidth
* 707 / 1000 / 2; // 707 / 1000 = 0.707 = 1/sqrt(2);
1007 handleCornerShift
= WXMAX( handleCornerShift
, 1 );
1008 int handleBase
= 4 * x
/ 20;
1009 int handleLength
= 2*handleBase
+1;
1010 wxPoint handlePolygon
[] =
1012 wxPoint(-handleCornerShift
,+handleCornerShift
),
1013 wxPoint(+handleCornerShift
,-handleCornerShift
),
1014 wxPoint(multiplier
*handleLength
/2+handleCornerShift
,multiplier
*handleLength
/2-handleCornerShift
),
1015 wxPoint(multiplier
*handleLength
/2-handleCornerShift
,multiplier
*handleLength
/2+handleCornerShift
),
1017 mem
.DrawPolygon(WXSIZEOF(handlePolygon
),handlePolygon
,lineStart
,lineStart
);
1019 // draw drop triangle
1020 int triangleX
= 13 * x
/ 20;
1021 int triangleY
= 5 * x
/ 20;
1022 int triangleBase
= 3 * x
/ 20;
1023 int triangleFactor
= triangleBase
*2+1;
1026 wxPoint dropPolygon
[] =
1028 wxPoint(multiplier
*0,multiplier
*0), // triangle left
1029 wxPoint(multiplier
*triangleFactor
-1,multiplier
*0), // triangle right
1030 wxPoint(multiplier
*triangleFactor
/2,multiplier
*triangleFactor
/2), // triangle bottom
1032 mem
.DrawPolygon(WXSIZEOF(dropPolygon
),dropPolygon
,multiplier
*triangleX
,multiplier
*triangleY
);
1034 mem
.SelectObject(wxNullBitmap
);
1036 //===============================================================================
1038 //===============================================================================
1040 if ( multiplier
!= 1 )
1042 wxImage image
= bitmap
.ConvertToImage();
1044 bitmap
= wxBitmap( image
);
1048 // Trim the edge where the arrow would have gone
1049 bitmap
= bitmap
.GetSubBitmap(wxRect(0,0, y
,y
));
1055 wxBitmap
wxSearchCtrl::RenderCancelBitmap( int x
, int y
)
1057 wxColour bg
= GetBackgroundColour();
1058 wxColour fg
= wxStepColour(GetForegroundColour(), LIGHT_STEP
);
1060 //===============================================================================
1061 // begin drawing code
1062 //===============================================================================
1079 // cross line starts (4,4)-(10,10)
1080 // drop (13,16)-(19,6)-(16,9)
1082 int multiplier
= GetMultiplier();
1084 int penWidth
= multiplier
* x
/ 14;
1086 wxBitmap
bitmap( multiplier
*x
, multiplier
*y
);
1088 mem
.SelectObject(bitmap
);
1091 mem
.SetBrush( wxBrush(bg
) );
1092 mem
.SetPen( wxPen(bg
) );
1093 mem
.DrawRectangle(0,0,bitmap
.GetWidth(),bitmap
.GetHeight());
1096 mem
.SetBrush( wxBrush(fg
) );
1097 mem
.SetPen( wxPen(fg
) );
1098 int radius
= multiplier
*x
/2;
1099 mem
.DrawCircle(radius
,radius
,radius
);
1102 int lineStartBase
= 4 * x
/ 14;
1103 int lineLength
= x
- 2*lineStartBase
;
1105 mem
.SetPen( wxPen(bg
) );
1106 mem
.SetBrush( wxBrush(bg
) );
1107 int handleCornerShift
= penWidth
/2;
1108 handleCornerShift
= WXMAX( handleCornerShift
, 1 );
1109 wxPoint handlePolygon
[] =
1111 wxPoint(-handleCornerShift
,+handleCornerShift
),
1112 wxPoint(+handleCornerShift
,-handleCornerShift
),
1113 wxPoint(multiplier
*lineLength
+handleCornerShift
,multiplier
*lineLength
-handleCornerShift
),
1114 wxPoint(multiplier
*lineLength
-handleCornerShift
,multiplier
*lineLength
+handleCornerShift
),
1116 mem
.DrawPolygon(WXSIZEOF(handlePolygon
),handlePolygon
,multiplier
*lineStartBase
,multiplier
*lineStartBase
);
1117 wxPoint handlePolygon2
[] =
1119 wxPoint(+handleCornerShift
,+handleCornerShift
),
1120 wxPoint(-handleCornerShift
,-handleCornerShift
),
1121 wxPoint(multiplier
*lineLength
-handleCornerShift
,-multiplier
*lineLength
-handleCornerShift
),
1122 wxPoint(multiplier
*lineLength
+handleCornerShift
,-multiplier
*lineLength
+handleCornerShift
),
1124 mem
.DrawPolygon(WXSIZEOF(handlePolygon2
),handlePolygon2
,multiplier
*lineStartBase
,multiplier
*(x
-lineStartBase
));
1126 //===============================================================================
1128 //===============================================================================
1130 if ( multiplier
!= 1 )
1132 wxImage image
= bitmap
.ConvertToImage();
1134 bitmap
= wxBitmap( image
);
1140 void wxSearchCtrl::RecalcBitmaps()
1146 wxSize sizeText
= m_text
->GetBestSize();
1148 int bitmapHeight
= sizeText
.y
- 2 * ICON_MARGIN
;
1149 int bitmapWidth
= sizeText
.y
* 20 / 14;
1151 if ( !m_searchBitmapUser
)
1154 !m_searchBitmap
.Ok() ||
1155 m_searchBitmap
.GetHeight() != bitmapHeight
||
1156 m_searchBitmap
.GetWidth() != bitmapWidth
1159 m_searchBitmap
= RenderSearchBitmap(bitmapWidth
,bitmapHeight
,false);
1162 m_searchButton
->SetBitmapLabel(m_searchBitmap
);
1165 // else this bitmap was set by user, don't alter
1169 if ( !m_searchMenuBitmapUser
)
1172 !m_searchMenuBitmap
.Ok() ||
1173 m_searchMenuBitmap
.GetHeight() != bitmapHeight
||
1174 m_searchMenuBitmap
.GetWidth() != bitmapWidth
1177 m_searchMenuBitmap
= RenderSearchBitmap(bitmapWidth
,bitmapHeight
,true);
1180 m_searchButton
->SetBitmapLabel(m_searchMenuBitmap
);
1183 // else this bitmap was set by user, don't alter
1185 #endif // wxUSE_MENUS
1187 if ( !m_cancelBitmapUser
)
1190 !m_cancelBitmap
.Ok() ||
1191 m_cancelBitmap
.GetHeight() != bitmapHeight
||
1192 m_cancelBitmap
.GetWidth() != bitmapHeight
1195 m_cancelBitmap
= RenderCancelBitmap(bitmapHeight
-BORDER
-1,bitmapHeight
-BORDER
-1); // square
1196 m_cancelButton
->SetBitmapLabel(m_cancelBitmap
);
1198 // else this bitmap was set by user, don't alter
1202 void wxSearchCtrl::OnSearchButton( wxCommandEvent
& event
)
1207 void wxSearchCtrl::OnSetFocus( wxFocusEvent
& /*event*/ )
1215 void wxSearchCtrl::OnSize( wxSizeEvent
& WXUNUSED(event
) )
1218 GetSize(&width
, &height
);
1219 LayoutControls(0, 0, width
, height
);
1224 void wxSearchCtrl::PopupSearchMenu()
1228 wxSize size
= GetSize();
1229 PopupMenu( m_menu
, 0, size
.y
);
1233 #endif // wxUSE_MENUS
1235 #endif // !wxUSE_NATIVE_SEARCH_CONTROL
1237 #endif // wxUSE_SEARCHCTRL