1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/srchctlg.cpp
3 // Purpose: implements wxSearchCtrl as a composite control
4 // Author: Vince Harron
7 // Copyright: Vince Harron
8 // Licence: 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 #define LIGHT_STEP 160
55 // ----------------------------------------------------------------------------
56 // wxSearchTextCtrl: text control used by search control
57 // ----------------------------------------------------------------------------
59 class wxSearchTextCtrl
: public wxTextCtrl
62 wxSearchTextCtrl(wxSearchCtrl
*search
, const wxString
& value
, int style
)
63 : wxTextCtrl(search
, wxID_ANY
, value
, wxDefaultPosition
, wxDefaultSize
,
64 (style
& ~wxBORDER_MASK
) | wxNO_BORDER
)
70 // remove the default minsize, the searchctrl will have one instead
71 SetSizeHints(wxDefaultCoord
,wxDefaultCoord
);
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
)
79 wxTextCtrl::DoSetValue(value
, flags
);
82 bool DoLoadFile(const wxString
& file
, int fileType
)
84 return wxTextCtrl::DoLoadFile(file
, fileType
);
87 bool DoSaveFile(const wxString
& file
, int fileType
)
89 return wxTextCtrl::DoSaveFile(file
, fileType
);
93 void OnText(wxCommandEvent
& eventText
)
95 wxCommandEvent
event(eventText
);
96 event
.SetEventObject(m_search
);
97 event
.SetId(m_search
->GetId());
99 m_search
->GetEventHandler()->ProcessEvent(event
);
102 void OnTextUrl(wxTextUrlEvent
& eventText
)
104 // copy constructor is disabled for some reason?
105 //wxTextUrlEvent event(eventText);
106 wxTextUrlEvent
event(
108 eventText
.GetMouseEvent(),
109 eventText
.GetURLStart(),
110 eventText
.GetURLEnd()
112 event
.SetEventObject(m_search
);
114 m_search
->GetEventHandler()->ProcessEvent(event
);
118 // We increase the text control height to be the same as for the controls
119 // with border as this is what we actually need here because even though
120 // this control itself is borderless, it's inside wxSearchCtrl which does
121 // have the border and so should have the same height as the normal text
122 // entries with border.
124 // This is a bit ugly and it would arguably be better to use whatever size
125 // the base class version returns and just centre the text vertically in
126 // the search control but I failed to modify the code in LayoutControls()
127 // to do this easily and as there is much in that code I don't understand
128 // (notably what is the logic for buttons sizing?) I prefer to not touch it
130 virtual wxSize
DoGetBestSize() const
132 const long flags
= GetWindowStyleFlag();
133 wxSearchTextCtrl
* const self
= const_cast<wxSearchTextCtrl
*>(this);
135 self
->SetWindowStyleFlag((flags
& ~wxBORDER_MASK
) | wxBORDER_DEFAULT
);
136 const wxSize size
= wxTextCtrl::DoGetBestSize();
137 self
->SetWindowStyleFlag(flags
);
144 wxSearchCtrl
* m_search
;
146 DECLARE_EVENT_TABLE()
149 BEGIN_EVENT_TABLE(wxSearchTextCtrl
, wxTextCtrl
)
150 EVT_TEXT(wxID_ANY
, wxSearchTextCtrl::OnText
)
151 EVT_TEXT_ENTER(wxID_ANY
, wxSearchTextCtrl::OnText
)
152 EVT_TEXT_URL(wxID_ANY
, wxSearchTextCtrl::OnTextUrl
)
153 EVT_TEXT_MAXLEN(wxID_ANY
, wxSearchTextCtrl::OnText
)
156 // ----------------------------------------------------------------------------
157 // wxSearchButton: search button used by search control
158 // ----------------------------------------------------------------------------
160 class wxSearchButton
: public wxControl
163 wxSearchButton(wxSearchCtrl
*search
, int eventType
, const wxBitmap
& bmp
)
164 : wxControl(search
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxNO_BORDER
),
166 m_eventType(eventType
),
170 void SetBitmapLabel(const wxBitmap
& label
) { m_bmp
= label
; }
172 // The buttons in wxSearchCtrl shouldn't accept focus from keyboard because
173 // this would interfere with the usual TAB processing: the user expects
174 // that pressing TAB in the search control should switch focus to the next
175 // control and not give it to the button inside the same control. Besides,
176 // the search button can be already activated by pressing "Enter" so there
177 // is really no reason for it to be able to get focus from keyboard.
178 virtual bool AcceptsFocusFromKeyboard() const { return false; }
181 wxSize
DoGetBestSize() const
183 return wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight());
186 void OnLeftUp(wxMouseEvent
&)
188 wxCommandEvent
event(m_eventType
, m_search
->GetId());
189 event
.SetEventObject(m_search
);
191 if ( m_eventType
== wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
)
193 // it's convenient to have the string to search for directly in the
194 // event instead of having to retrieve it from the control in the
195 // event handler code later, so provide it here
196 event
.SetString(m_search
->GetValue());
199 GetEventHandler()->ProcessEvent(event
);
201 m_search
->SetFocus();
204 if ( m_eventType
== wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
)
206 // this happens automatically, just like on Mac OS X
207 m_search
->PopupSearchMenu();
209 #endif // wxUSE_MENUS
212 void OnPaint(wxPaintEvent
&)
215 dc
.DrawBitmap(m_bmp
, 0,0, true);
220 wxSearchCtrl
*m_search
;
221 wxEventType m_eventType
;
224 DECLARE_EVENT_TABLE()
227 BEGIN_EVENT_TABLE(wxSearchButton
, wxControl
)
228 EVT_LEFT_UP(wxSearchButton::OnLeftUp
)
229 EVT_PAINT(wxSearchButton::OnPaint
)
232 BEGIN_EVENT_TABLE(wxSearchCtrl
, wxSearchCtrlBase
)
233 EVT_SEARCHCTRL_SEARCH_BTN(wxID_ANY
, wxSearchCtrl::OnSearchButton
)
234 EVT_SET_FOCUS(wxSearchCtrl::OnSetFocus
)
235 EVT_SIZE(wxSearchCtrl::OnSize
)
238 IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl
, wxSearchCtrlBase
)
240 // ============================================================================
242 // ============================================================================
244 // ----------------------------------------------------------------------------
245 // wxSearchCtrl creation
246 // ----------------------------------------------------------------------------
251 wxSearchCtrl::wxSearchCtrl()
256 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
257 const wxString
& value
,
261 const wxValidator
& validator
,
262 const wxString
& name
)
266 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
269 void wxSearchCtrl::Init()
272 m_searchButton
= NULL
;
273 m_cancelButton
= NULL
;
276 #endif // wxUSE_MENUS
278 m_searchButtonVisible
= true;
279 m_cancelButtonVisible
= false;
281 m_searchBitmapUser
= false;
282 m_cancelBitmapUser
= false;
284 m_searchMenuBitmapUser
= false;
285 #endif // wxUSE_MENUS
288 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
289 const wxString
& value
,
293 const wxValidator
& validator
,
294 const wxString
& name
)
296 // force border style for more native appearance
297 style
&= ~wxBORDER_MASK
;
299 style
|= wxBORDER_SUNKEN
;
300 #elif defined(__WXMSW__)
301 // Don't set the style explicitly, let GetDefaultBorder() work it out, unless
302 // we will get a sunken border (e.g. on Windows 200) in which case we must
303 // override with a simple border.
304 if (GetDefaultBorder() == wxBORDER_SUNKEN
)
305 style
|= wxBORDER_SIMPLE
;
307 style
|= wxBORDER_SIMPLE
;
309 if ( !wxSearchCtrlBaseBaseClass::Create(parent
, id
, pos
, size
,
310 style
, validator
, name
) )
315 m_text
= new wxSearchTextCtrl(this, value
, style
);
317 m_searchButton
= new wxSearchButton(this,
318 wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
,
320 m_cancelButton
= new wxSearchButton(this,
321 wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN
,
324 SetForegroundColour( m_text
->GetForegroundColour() );
325 SetBackgroundColour( m_text
->GetBackgroundColour() );
329 SetInitialSize(size
);
334 wxSearchCtrl::~wxSearchCtrl()
337 delete m_searchButton
;
338 delete m_cancelButton
;
341 #endif // wxUSE_MENUS
345 // search control specific interfaces
348 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
350 if ( menu
== m_menu
)
355 bool hadMenu
= (m_menu
!= NULL
);
359 if ( m_menu
&& !hadMenu
)
361 m_searchButton
->SetBitmapLabel(m_searchMenuBitmap
);
362 m_searchButton
->Refresh();
364 else if ( !m_menu
&& hadMenu
)
366 m_searchButton
->SetBitmapLabel(m_searchBitmap
);
367 if ( m_searchButtonVisible
)
369 m_searchButton
->Refresh();
372 wxRect rect
= GetRect();
373 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
376 wxMenu
* wxSearchCtrl::GetMenu()
381 #endif // wxUSE_MENUS
383 void wxSearchCtrl::ShowSearchButton( bool show
)
385 if ( m_searchButtonVisible
== show
)
390 m_searchButtonVisible
= show
;
391 if ( m_searchButtonVisible
)
396 wxRect rect
= GetRect();
397 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
400 bool wxSearchCtrl::IsSearchButtonVisible() const
402 return m_searchButtonVisible
;
406 void wxSearchCtrl::ShowCancelButton( bool show
)
408 if ( m_cancelButtonVisible
== show
)
413 m_cancelButtonVisible
= show
;
415 wxRect rect
= GetRect();
416 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
419 bool wxSearchCtrl::IsCancelButtonVisible() const
421 return m_cancelButtonVisible
;
424 void wxSearchCtrl::SetDescriptiveText(const wxString
& text
)
426 m_text
->SetHint(text
);
429 wxString
wxSearchCtrl::GetDescriptiveText() const
431 return m_text
->GetHint();
434 // ----------------------------------------------------------------------------
436 // ----------------------------------------------------------------------------
438 wxSize
wxSearchCtrl::DoGetBestSize() const
440 wxSize sizeText
= m_text
->GetBestSize();
441 wxSize
sizeSearch(0,0);
442 wxSize
sizeCancel(0,0);
443 int searchMargin
= 0;
444 int cancelMargin
= 0;
445 if ( m_searchButtonVisible
|| HasMenu() )
447 sizeSearch
= m_searchButton
->GetBestSize();
448 searchMargin
= MARGIN
;
450 if ( m_cancelButtonVisible
)
452 sizeCancel
= m_cancelButton
->GetBestSize();
453 cancelMargin
= MARGIN
;
456 int horizontalBorder
= 1 + ( sizeText
.y
- sizeText
.y
* 14 / 21 ) / 2;
458 // buttons are square and equal to the height of the text control
459 int height
= sizeText
.y
;
460 return wxSize(sizeSearch
.x
+ searchMargin
+ sizeText
.x
+ cancelMargin
+ sizeCancel
.x
+ 2*horizontalBorder
,
464 void wxSearchCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
466 wxSearchCtrlBase::DoMoveWindow(x
, y
, width
, height
);
468 LayoutControls(0, 0, width
, height
);
471 void wxSearchCtrl::LayoutControls(int x
, int y
, int width
, int height
)
476 wxSize sizeText
= m_text
->GetBestSize();
477 // make room for the search menu & clear button
478 int horizontalBorder
= ( sizeText
.y
- sizeText
.y
* 14 / 21 ) / 2;
479 x
+= horizontalBorder
;
481 width
-= horizontalBorder
*2;
483 if (width
< 0) width
= 0;
484 if (height
< 0) height
= 0;
486 wxSize
sizeSearch(0,0);
487 wxSize
sizeCancel(0,0);
488 int searchMargin
= 0;
489 int cancelMargin
= 0;
490 if ( m_searchButtonVisible
|| HasMenu() )
492 sizeSearch
= m_searchButton
->GetBestSize();
493 searchMargin
= MARGIN
;
495 if ( m_cancelButtonVisible
)
497 sizeCancel
= m_cancelButton
->GetBestSize();
498 cancelMargin
= MARGIN
;
500 m_searchButton
->Show( m_searchButtonVisible
|| HasMenu() );
501 m_cancelButton
->Show( m_cancelButtonVisible
);
503 if ( sizeSearch
.x
+ sizeCancel
.x
> width
)
505 sizeSearch
.x
= width
/2;
506 sizeCancel
.x
= width
/2;
510 wxCoord textWidth
= width
- sizeSearch
.x
- sizeCancel
.x
- searchMargin
- cancelMargin
- 1;
511 if (textWidth
< 0) textWidth
= 0;
513 // position the subcontrols inside the client area
515 m_searchButton
->SetSize(x
, y
+ ICON_OFFSET
- 1, sizeSearch
.x
, height
);
516 m_text
->SetSize( x
+ sizeSearch
.x
+ searchMargin
,
517 y
+ ICON_OFFSET
- BORDER
,
520 m_cancelButton
->SetSize(x
+ sizeSearch
.x
+ searchMargin
+ textWidth
+ cancelMargin
,
521 y
+ ICON_OFFSET
- 1, sizeCancel
.x
, height
);
524 wxWindowList
wxSearchCtrl::GetCompositeWindowParts() const
527 parts
.push_back(m_text
);
528 parts
.push_back(m_searchButton
);
529 parts
.push_back(m_cancelButton
);
536 wxString
wxSearchCtrl::DoGetValue() const
538 return m_text
->GetValue();
540 wxString
wxSearchCtrl::GetRange(long from
, long to
) const
542 return m_text
->GetRange(from
, to
);
545 int wxSearchCtrl::GetLineLength(long lineNo
) const
547 return m_text
->GetLineLength(lineNo
);
549 wxString
wxSearchCtrl::GetLineText(long lineNo
) const
551 return m_text
->GetLineText(lineNo
);
553 int wxSearchCtrl::GetNumberOfLines() const
555 return m_text
->GetNumberOfLines();
558 bool wxSearchCtrl::IsModified() const
560 return m_text
->IsModified();
562 bool wxSearchCtrl::IsEditable() const
564 return m_text
->IsEditable();
567 // more readable flag testing methods
568 bool wxSearchCtrl::IsSingleLine() const
570 return m_text
->IsSingleLine();
572 bool wxSearchCtrl::IsMultiLine() const
574 return m_text
->IsMultiLine();
577 // If the return values from and to are the same, there is no selection.
578 void wxSearchCtrl::GetSelection(long* from
, long* to
) const
580 m_text
->GetSelection(from
, to
);
583 wxString
wxSearchCtrl::GetStringSelection() const
585 return m_text
->GetStringSelection();
592 void wxSearchCtrl::Clear()
596 void wxSearchCtrl::Replace(long from
, long to
, const wxString
& value
)
598 m_text
->Replace(from
, to
, value
);
600 void wxSearchCtrl::Remove(long from
, long to
)
602 m_text
->Remove(from
, to
);
605 // load/save the controls contents from/to the file
606 bool wxSearchCtrl::LoadFile(const wxString
& file
)
608 return m_text
->LoadFile(file
);
610 bool wxSearchCtrl::SaveFile(const wxString
& file
)
612 return m_text
->SaveFile(file
);
615 // sets/clears the dirty flag
616 void wxSearchCtrl::MarkDirty()
620 void wxSearchCtrl::DiscardEdits()
622 m_text
->DiscardEdits();
625 // set the max number of characters which may be entered in a single line
627 void wxSearchCtrl::SetMaxLength(unsigned long len
)
629 m_text
->SetMaxLength(len
);
632 // writing text inserts it at the current position, appending always
633 // inserts it at the end
634 void wxSearchCtrl::WriteText(const wxString
& text
)
636 m_text
->WriteText(text
);
638 void wxSearchCtrl::AppendText(const wxString
& text
)
640 m_text
->AppendText(text
);
643 // insert the character which would have resulted from this key event,
644 // return true if anything has been inserted
645 bool wxSearchCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
647 return m_text
->EmulateKeyPress(event
);
650 // text control under some platforms supports the text styles: these
651 // methods allow to apply the given text style to the given selection or to
652 // set/get the style which will be used for all appended text
653 bool wxSearchCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
655 return m_text
->SetStyle(start
, end
, style
);
657 bool wxSearchCtrl::GetStyle(long position
, wxTextAttr
& style
)
659 return m_text
->GetStyle(position
, style
);
661 bool wxSearchCtrl::SetDefaultStyle(const wxTextAttr
& style
)
663 return m_text
->SetDefaultStyle(style
);
665 const wxTextAttr
& wxSearchCtrl::GetDefaultStyle() const
667 return m_text
->GetDefaultStyle();
670 // translate between the position (which is just an index in the text ctrl
671 // considering all its contents as a single strings) and (x, y) coordinates
672 // which represent column and line.
673 long wxSearchCtrl::XYToPosition(long x
, long y
) const
675 return m_text
->XYToPosition(x
, y
);
677 bool wxSearchCtrl::PositionToXY(long pos
, long *x
, long *y
) const
679 return m_text
->PositionToXY(pos
, x
, y
);
682 void wxSearchCtrl::ShowPosition(long pos
)
684 m_text
->ShowPosition(pos
);
687 // find the character at position given in pixels
689 // NB: pt is in device coords (not adjusted for the client area origin nor
691 wxTextCtrlHitTestResult
wxSearchCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
693 return m_text
->HitTest(pt
, pos
);
695 wxTextCtrlHitTestResult
wxSearchCtrl::HitTest(const wxPoint
& pt
,
697 wxTextCoord
*row
) const
699 return m_text
->HitTest(pt
, col
, row
);
702 // Clipboard operations
703 void wxSearchCtrl::Copy()
707 void wxSearchCtrl::Cut()
711 void wxSearchCtrl::Paste()
716 bool wxSearchCtrl::CanCopy() const
718 return m_text
->CanCopy();
720 bool wxSearchCtrl::CanCut() const
722 return m_text
->CanCut();
724 bool wxSearchCtrl::CanPaste() const
726 return m_text
->CanPaste();
730 void wxSearchCtrl::Undo()
734 void wxSearchCtrl::Redo()
739 bool wxSearchCtrl::CanUndo() const
741 return m_text
->CanUndo();
743 bool wxSearchCtrl::CanRedo() const
745 return m_text
->CanRedo();
749 void wxSearchCtrl::SetInsertionPoint(long pos
)
751 m_text
->SetInsertionPoint(pos
);
753 void wxSearchCtrl::SetInsertionPointEnd()
755 m_text
->SetInsertionPointEnd();
757 long wxSearchCtrl::GetInsertionPoint() const
759 return m_text
->GetInsertionPoint();
761 long wxSearchCtrl::GetLastPosition() const
763 return m_text
->GetLastPosition();
766 void wxSearchCtrl::SetSelection(long from
, long to
)
768 m_text
->SetSelection(from
, to
);
770 void wxSearchCtrl::SelectAll()
775 void wxSearchCtrl::SetEditable(bool editable
)
777 m_text
->SetEditable(editable
);
780 bool wxSearchCtrl::SetFont(const wxFont
& font
)
782 if ( !wxSearchCtrlBase::SetFont(font
) )
785 // Recreate the bitmaps as their size may have changed.
791 bool wxSearchCtrl::SetBackgroundColour(const wxColour
& colour
)
793 if ( !wxSearchCtrlBase::SetBackgroundColour(colour
) )
796 // When the background changes, re-render the bitmaps so that the correct
797 // colour shows in their "transparent" area.
803 // search control generic only
804 void wxSearchCtrl::SetSearchBitmap( const wxBitmap
& bitmap
)
806 m_searchBitmap
= bitmap
;
807 m_searchBitmapUser
= bitmap
.IsOk();
808 if ( m_searchBitmapUser
)
810 if ( m_searchButton
&& !HasMenu() )
812 m_searchButton
->SetBitmapLabel( m_searchBitmap
);
817 // the user bitmap was just cleared, generate one
824 void wxSearchCtrl::SetSearchMenuBitmap( const wxBitmap
& bitmap
)
826 m_searchMenuBitmap
= bitmap
;
827 m_searchMenuBitmapUser
= bitmap
.IsOk();
828 if ( m_searchMenuBitmapUser
)
830 if ( m_searchButton
&& m_menu
)
832 m_searchButton
->SetBitmapLabel( m_searchMenuBitmap
);
837 // the user bitmap was just cleared, generate one
842 #endif // wxUSE_MENUS
844 void wxSearchCtrl::SetCancelBitmap( const wxBitmap
& bitmap
)
846 m_cancelBitmap
= bitmap
;
847 m_cancelBitmapUser
= bitmap
.IsOk();
848 if ( m_cancelBitmapUser
)
850 if ( m_cancelButton
)
852 m_cancelButton
->SetBitmapLabel( m_cancelBitmap
);
857 // the user bitmap was just cleared, generate one
864 // override streambuf method
865 #if wxHAS_TEXT_WINDOW_STREAM
867 #endif // wxHAS_TEXT_WINDOW_STREAM
869 // stream-like insertion operators: these are always available, whether we
870 // were, or not, compiled with streambuf support
871 wxTextCtrl
& operator<<(const wxString
& s
);
872 wxTextCtrl
& operator<<(int i
);
873 wxTextCtrl
& operator<<(long i
);
874 wxTextCtrl
& operator<<(float f
);
875 wxTextCtrl
& operator<<(double d
);
876 wxTextCtrl
& operator<<(const wxChar c
);
879 void wxSearchCtrl::DoSetValue(const wxString
& value
, int flags
)
881 m_text
->DoSetValue(value
, flags
);
884 bool wxSearchCtrl::DoLoadFile(const wxString
& file
, int fileType
)
886 return m_text
->DoLoadFile(file
, fileType
);
889 bool wxSearchCtrl::DoSaveFile(const wxString
& file
, int fileType
)
891 return m_text
->DoSaveFile(file
, fileType
);
894 // do the window-specific processing after processing the update event
895 void wxSearchCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
897 wxSearchCtrlBase::DoUpdateWindowUI(event
);
900 bool wxSearchCtrl::ShouldInheritColours() const
905 // icons are rendered at 3-8 times larger than necessary and downscaled for
907 static int GetMultiplier()
910 // speed up bitmap generation by using a small bitmap
913 int depth
= ::wxDisplayDepth();
923 wxBitmap
wxSearchCtrl::RenderSearchBitmap( int x
, int y
, bool renderDrop
)
925 wxColour bg
= GetBackgroundColour();
926 wxColour fg
= GetForegroundColour().ChangeLightness(LIGHT_STEP
-20);
928 //===============================================================================
929 // begin drawing code
930 //===============================================================================
933 // force width:height ratio
945 // glass 11x11, top left corner
946 // handle (9,9)-(13,13)
947 // drop (13,16)-(19,6)-(16,9)
949 int multiplier
= GetMultiplier();
950 int penWidth
= multiplier
* 2;
952 penWidth
= penWidth
* x
/ 20;
954 wxBitmap
bitmap( multiplier
*x
, multiplier
*y
);
956 mem
.SelectObject(bitmap
);
959 mem
.SetBrush( wxBrush(bg
) );
960 mem
.SetPen( wxPen(bg
) );
961 mem
.DrawRectangle(0,0,bitmap
.GetWidth(),bitmap
.GetHeight());
964 mem
.SetBrush( wxBrush(fg
) );
965 mem
.SetPen( wxPen(fg
) );
966 int glassBase
= 5 * x
/ 20;
967 int glassFactor
= 2*glassBase
+ 1;
968 int radius
= multiplier
*glassFactor
/2;
969 mem
.DrawCircle(radius
,radius
,radius
);
970 mem
.SetBrush( wxBrush(bg
) );
971 mem
.SetPen( wxPen(bg
) );
972 mem
.DrawCircle(radius
,radius
,radius
-penWidth
);
975 int lineStart
= radius
+ (radius
-penWidth
/2) * 707 / 1000; // 707 / 1000 = 0.707 = 1/sqrt(2);
977 mem
.SetPen( wxPen(fg
) );
978 mem
.SetBrush( wxBrush(fg
) );
979 int handleCornerShift
= penWidth
* 707 / 1000 / 2; // 707 / 1000 = 0.707 = 1/sqrt(2);
980 handleCornerShift
= WXMAX( handleCornerShift
, 1 );
981 int handleBase
= 4 * x
/ 20;
982 int handleLength
= 2*handleBase
+1;
983 wxPoint handlePolygon
[] =
985 wxPoint(-handleCornerShift
,+handleCornerShift
),
986 wxPoint(+handleCornerShift
,-handleCornerShift
),
987 wxPoint(multiplier
*handleLength
/2+handleCornerShift
,multiplier
*handleLength
/2-handleCornerShift
),
988 wxPoint(multiplier
*handleLength
/2-handleCornerShift
,multiplier
*handleLength
/2+handleCornerShift
),
990 mem
.DrawPolygon(WXSIZEOF(handlePolygon
),handlePolygon
,lineStart
,lineStart
);
992 // draw drop triangle
993 int triangleX
= 13 * x
/ 20;
994 int triangleY
= 5 * x
/ 20;
995 int triangleBase
= 3 * x
/ 20;
996 int triangleFactor
= triangleBase
*2+1;
999 wxPoint dropPolygon
[] =
1001 wxPoint(multiplier
*0,multiplier
*0), // triangle left
1002 wxPoint(multiplier
*triangleFactor
-1,multiplier
*0), // triangle right
1003 wxPoint(multiplier
*triangleFactor
/2,multiplier
*triangleFactor
/2), // triangle bottom
1005 mem
.DrawPolygon(WXSIZEOF(dropPolygon
),dropPolygon
,multiplier
*triangleX
,multiplier
*triangleY
);
1007 mem
.SelectObject(wxNullBitmap
);
1009 //===============================================================================
1011 //===============================================================================
1013 if ( multiplier
!= 1 )
1015 wxImage image
= bitmap
.ConvertToImage();
1017 bitmap
= wxBitmap( image
);
1021 // Trim the edge where the arrow would have gone
1022 bitmap
= bitmap
.GetSubBitmap(wxRect(0,0, y
,y
));
1028 wxBitmap
wxSearchCtrl::RenderCancelBitmap( int x
, int y
)
1030 wxColour bg
= GetBackgroundColour();
1031 wxColour fg
= GetForegroundColour().ChangeLightness(LIGHT_STEP
);
1033 //===============================================================================
1034 // begin drawing code
1035 //===============================================================================
1052 // cross line starts (4,4)-(10,10)
1053 // drop (13,16)-(19,6)-(16,9)
1055 int multiplier
= GetMultiplier();
1057 int penWidth
= multiplier
* x
/ 14;
1059 wxBitmap
bitmap( multiplier
*x
, multiplier
*y
);
1061 mem
.SelectObject(bitmap
);
1064 mem
.SetBrush( wxBrush(bg
) );
1065 mem
.SetPen( wxPen(bg
) );
1066 mem
.DrawRectangle(0,0,bitmap
.GetWidth(),bitmap
.GetHeight());
1069 mem
.SetBrush( wxBrush(fg
) );
1070 mem
.SetPen( wxPen(fg
) );
1071 int radius
= multiplier
*x
/2;
1072 mem
.DrawCircle(radius
,radius
,radius
);
1075 int lineStartBase
= 4 * x
/ 14;
1076 int lineLength
= x
- 2*lineStartBase
;
1078 mem
.SetPen( wxPen(bg
) );
1079 mem
.SetBrush( wxBrush(bg
) );
1080 int handleCornerShift
= penWidth
/2;
1081 handleCornerShift
= WXMAX( handleCornerShift
, 1 );
1082 wxPoint handlePolygon
[] =
1084 wxPoint(-handleCornerShift
,+handleCornerShift
),
1085 wxPoint(+handleCornerShift
,-handleCornerShift
),
1086 wxPoint(multiplier
*lineLength
+handleCornerShift
,multiplier
*lineLength
-handleCornerShift
),
1087 wxPoint(multiplier
*lineLength
-handleCornerShift
,multiplier
*lineLength
+handleCornerShift
),
1089 mem
.DrawPolygon(WXSIZEOF(handlePolygon
),handlePolygon
,multiplier
*lineStartBase
,multiplier
*lineStartBase
);
1090 wxPoint handlePolygon2
[] =
1092 wxPoint(+handleCornerShift
,+handleCornerShift
),
1093 wxPoint(-handleCornerShift
,-handleCornerShift
),
1094 wxPoint(multiplier
*lineLength
-handleCornerShift
,-multiplier
*lineLength
-handleCornerShift
),
1095 wxPoint(multiplier
*lineLength
+handleCornerShift
,-multiplier
*lineLength
+handleCornerShift
),
1097 mem
.DrawPolygon(WXSIZEOF(handlePolygon2
),handlePolygon2
,multiplier
*lineStartBase
,multiplier
*(x
-lineStartBase
));
1099 //===============================================================================
1101 //===============================================================================
1103 if ( multiplier
!= 1 )
1105 wxImage image
= bitmap
.ConvertToImage();
1107 bitmap
= wxBitmap( image
);
1113 void wxSearchCtrl::RecalcBitmaps()
1119 wxSize sizeText
= m_text
->GetBestSize();
1121 int bitmapHeight
= sizeText
.y
- 2 * ICON_MARGIN
;
1122 int bitmapWidth
= sizeText
.y
* 20 / 14;
1124 if ( !m_searchBitmapUser
)
1127 !m_searchBitmap
.IsOk() ||
1128 m_searchBitmap
.GetHeight() != bitmapHeight
||
1129 m_searchBitmap
.GetWidth() != bitmapWidth
1132 m_searchBitmap
= RenderSearchBitmap(bitmapWidth
,bitmapHeight
,false);
1135 m_searchButton
->SetBitmapLabel(m_searchBitmap
);
1138 // else this bitmap was set by user, don't alter
1142 if ( !m_searchMenuBitmapUser
)
1145 !m_searchMenuBitmap
.IsOk() ||
1146 m_searchMenuBitmap
.GetHeight() != bitmapHeight
||
1147 m_searchMenuBitmap
.GetWidth() != bitmapWidth
1150 m_searchMenuBitmap
= RenderSearchBitmap(bitmapWidth
,bitmapHeight
,true);
1153 m_searchButton
->SetBitmapLabel(m_searchMenuBitmap
);
1156 // else this bitmap was set by user, don't alter
1158 #endif // wxUSE_MENUS
1160 if ( !m_cancelBitmapUser
)
1163 !m_cancelBitmap
.IsOk() ||
1164 m_cancelBitmap
.GetHeight() != bitmapHeight
||
1165 m_cancelBitmap
.GetWidth() != bitmapHeight
1168 m_cancelBitmap
= RenderCancelBitmap(bitmapHeight
-BORDER
-1,bitmapHeight
-BORDER
-1); // square
1169 m_cancelButton
->SetBitmapLabel(m_cancelBitmap
);
1171 // else this bitmap was set by user, don't alter
1175 void wxSearchCtrl::OnSearchButton( wxCommandEvent
& event
)
1180 void wxSearchCtrl::OnSetFocus( wxFocusEvent
& /*event*/ )
1188 void wxSearchCtrl::OnSize( wxSizeEvent
& WXUNUSED(event
) )
1191 GetSize(&width
, &height
);
1192 LayoutControls(0, 0, width
, height
);
1197 void wxSearchCtrl::PopupSearchMenu()
1201 wxSize size
= GetSize();
1202 PopupMenu( m_menu
, 0, size
.y
);
1206 #endif // wxUSE_MENUS
1208 #endif // !wxUSE_NATIVE_SEARCH_CONTROL
1210 #endif // wxUSE_SEARCHCTRL