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 // Ensure that our best size is recomputed using our overridden
75 virtual wxWindow
* GetMainWindowOfCompositeControl()
80 // provide access to the base class protected methods to wxSearchCtrl which
81 // needs to forward to them
82 void DoSetValue(const wxString
& value
, int flags
)
84 wxTextCtrl::DoSetValue(value
, flags
);
87 bool DoLoadFile(const wxString
& file
, int fileType
)
89 return wxTextCtrl::DoLoadFile(file
, fileType
);
92 bool DoSaveFile(const wxString
& file
, int fileType
)
94 return wxTextCtrl::DoSaveFile(file
, fileType
);
98 void OnText(wxCommandEvent
& eventText
)
100 wxCommandEvent
event(eventText
);
101 event
.SetEventObject(m_search
);
102 event
.SetId(m_search
->GetId());
104 m_search
->GetEventHandler()->ProcessEvent(event
);
107 void OnTextUrl(wxTextUrlEvent
& eventText
)
109 // copy constructor is disabled for some reason?
110 //wxTextUrlEvent event(eventText);
111 wxTextUrlEvent
event(
113 eventText
.GetMouseEvent(),
114 eventText
.GetURLStart(),
115 eventText
.GetURLEnd()
117 event
.SetEventObject(m_search
);
119 m_search
->GetEventHandler()->ProcessEvent(event
);
123 // We increase the text control height to be the same as for the controls
124 // with border as this is what we actually need here because even though
125 // this control itself is borderless, it's inside wxSearchCtrl which does
126 // have the border and so should have the same height as the normal text
127 // entries with border.
129 // This is a bit ugly and it would arguably be better to use whatever size
130 // the base class version returns and just centre the text vertically in
131 // the search control but I failed to modify the code in LayoutControls()
132 // to do this easily and as there is much in that code I don't understand
133 // (notably what is the logic for buttons sizing?) I prefer to not touch it
135 virtual wxSize
DoGetBestSize() const
137 const long flags
= GetWindowStyleFlag();
138 wxSearchTextCtrl
* const self
= const_cast<wxSearchTextCtrl
*>(this);
140 self
->SetWindowStyleFlag((flags
& ~wxBORDER_MASK
) | wxBORDER_DEFAULT
);
141 const wxSize size
= wxTextCtrl::DoGetBestSize();
142 self
->SetWindowStyleFlag(flags
);
149 wxSearchCtrl
* m_search
;
151 DECLARE_EVENT_TABLE()
154 BEGIN_EVENT_TABLE(wxSearchTextCtrl
, wxTextCtrl
)
155 EVT_TEXT(wxID_ANY
, wxSearchTextCtrl::OnText
)
156 EVT_TEXT_ENTER(wxID_ANY
, wxSearchTextCtrl::OnText
)
157 EVT_TEXT_URL(wxID_ANY
, wxSearchTextCtrl::OnTextUrl
)
158 EVT_TEXT_MAXLEN(wxID_ANY
, wxSearchTextCtrl::OnText
)
161 // ----------------------------------------------------------------------------
162 // wxSearchButton: search button used by search control
163 // ----------------------------------------------------------------------------
165 class wxSearchButton
: public wxControl
168 wxSearchButton(wxSearchCtrl
*search
, int eventType
, const wxBitmap
& bmp
)
169 : wxControl(search
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxNO_BORDER
),
171 m_eventType(eventType
),
175 void SetBitmapLabel(const wxBitmap
& label
)
178 InvalidateBestSize();
181 // The buttons in wxSearchCtrl shouldn't accept focus from keyboard because
182 // this would interfere with the usual TAB processing: the user expects
183 // that pressing TAB in the search control should switch focus to the next
184 // control and not give it to the button inside the same control. Besides,
185 // the search button can be already activated by pressing "Enter" so there
186 // is really no reason for it to be able to get focus from keyboard.
187 virtual bool AcceptsFocusFromKeyboard() const { return false; }
189 virtual wxWindow
* GetMainWindowOfCompositeControl()
195 wxSize
DoGetBestSize() const
197 return wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight());
200 void OnLeftUp(wxMouseEvent
&)
202 wxCommandEvent
event(m_eventType
, m_search
->GetId());
203 event
.SetEventObject(m_search
);
205 if ( m_eventType
== wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
)
207 // it's convenient to have the string to search for directly in the
208 // event instead of having to retrieve it from the control in the
209 // event handler code later, so provide it here
210 event
.SetString(m_search
->GetValue());
213 GetEventHandler()->ProcessEvent(event
);
215 m_search
->SetFocus();
218 if ( m_eventType
== wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
)
220 // this happens automatically, just like on Mac OS X
221 m_search
->PopupSearchMenu();
223 #endif // wxUSE_MENUS
226 void OnPaint(wxPaintEvent
&)
229 dc
.DrawBitmap(m_bmp
, 0,0, true);
234 wxSearchCtrl
*m_search
;
235 wxEventType m_eventType
;
238 DECLARE_EVENT_TABLE()
241 BEGIN_EVENT_TABLE(wxSearchButton
, wxControl
)
242 EVT_LEFT_UP(wxSearchButton::OnLeftUp
)
243 EVT_PAINT(wxSearchButton::OnPaint
)
246 BEGIN_EVENT_TABLE(wxSearchCtrl
, wxSearchCtrlBase
)
247 EVT_SEARCHCTRL_CANCEL_BTN(wxID_ANY
, wxSearchCtrl::OnCancelButton
)
248 EVT_SET_FOCUS(wxSearchCtrl::OnSetFocus
)
249 EVT_SIZE(wxSearchCtrl::OnSize
)
252 IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl
, wxSearchCtrlBase
)
254 // ============================================================================
256 // ============================================================================
258 // ----------------------------------------------------------------------------
259 // wxSearchCtrl creation
260 // ----------------------------------------------------------------------------
265 wxSearchCtrl::wxSearchCtrl()
270 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
271 const wxString
& value
,
275 const wxValidator
& validator
,
276 const wxString
& name
)
280 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
283 void wxSearchCtrl::Init()
286 m_searchButton
= NULL
;
287 m_cancelButton
= NULL
;
290 #endif // wxUSE_MENUS
292 m_searchButtonVisible
= true;
293 m_cancelButtonVisible
= false;
295 m_searchBitmapUser
= false;
296 m_cancelBitmapUser
= false;
298 m_searchMenuBitmapUser
= false;
299 #endif // wxUSE_MENUS
302 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
303 const wxString
& value
,
307 const wxValidator
& validator
,
308 const wxString
& name
)
310 // force border style for more native appearance
311 style
&= ~wxBORDER_MASK
;
313 style
|= wxBORDER_SUNKEN
;
314 #elif defined(__WXMSW__)
315 // Don't set the style explicitly, let GetDefaultBorder() work it out, unless
316 // we will get a sunken border (e.g. on Windows 200) in which case we must
317 // override with a simple border.
318 if (GetDefaultBorder() == wxBORDER_SUNKEN
)
319 style
|= wxBORDER_SIMPLE
;
321 style
|= wxBORDER_SIMPLE
;
323 if ( !wxSearchCtrlBaseBaseClass::Create(parent
, id
, pos
, size
,
324 style
, validator
, name
) )
329 m_text
= new wxSearchTextCtrl(this, value
, style
);
331 m_searchButton
= new wxSearchButton(this,
332 wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
,
334 m_cancelButton
= new wxSearchButton(this,
335 wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN
,
338 SetBackgroundColour( m_text
->GetBackgroundColour() );
342 SetInitialSize(size
);
347 wxSearchCtrl::~wxSearchCtrl()
350 delete m_searchButton
;
351 delete m_cancelButton
;
354 #endif // wxUSE_MENUS
358 // search control specific interfaces
361 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
363 if ( menu
== m_menu
)
368 bool hadMenu
= (m_menu
!= NULL
);
372 if ( m_menu
&& !hadMenu
)
374 m_searchButton
->SetBitmapLabel(m_searchMenuBitmap
);
375 m_searchButton
->Refresh();
377 else if ( !m_menu
&& hadMenu
)
379 m_searchButton
->SetBitmapLabel(m_searchBitmap
);
380 if ( m_searchButtonVisible
)
382 m_searchButton
->Refresh();
385 wxRect rect
= GetRect();
386 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
389 wxMenu
* wxSearchCtrl::GetMenu()
394 #endif // wxUSE_MENUS
396 void wxSearchCtrl::ShowSearchButton( bool show
)
398 if ( m_searchButtonVisible
== show
)
403 m_searchButtonVisible
= show
;
404 if ( m_searchButtonVisible
)
409 wxRect rect
= GetRect();
410 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
413 bool wxSearchCtrl::IsSearchButtonVisible() const
415 return m_searchButtonVisible
;
419 void wxSearchCtrl::ShowCancelButton( bool show
)
421 if ( m_cancelButtonVisible
== show
)
426 m_cancelButtonVisible
= show
;
428 wxRect rect
= GetRect();
429 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
432 bool wxSearchCtrl::IsCancelButtonVisible() const
434 return m_cancelButtonVisible
;
437 void wxSearchCtrl::SetDescriptiveText(const wxString
& text
)
439 m_text
->SetHint(text
);
442 wxString
wxSearchCtrl::GetDescriptiveText() const
444 return m_text
->GetHint();
447 // ----------------------------------------------------------------------------
449 // ----------------------------------------------------------------------------
451 wxSize
wxSearchCtrl::DoGetBestSize() const
453 wxSize sizeText
= m_text
->GetBestSize();
454 wxSize
sizeSearch(0,0);
455 wxSize
sizeCancel(0,0);
456 int searchMargin
= 0;
457 int cancelMargin
= 0;
458 if ( m_searchButtonVisible
|| HasMenu() )
460 sizeSearch
= m_searchButton
->GetBestSize();
461 searchMargin
= MARGIN
;
463 if ( m_cancelButtonVisible
)
465 sizeCancel
= m_cancelButton
->GetBestSize();
466 cancelMargin
= MARGIN
;
469 int horizontalBorder
= 1 + ( sizeText
.y
- sizeText
.y
* 14 / 21 ) / 2;
471 // buttons are square and equal to the height of the text control
472 int height
= sizeText
.y
;
473 return wxSize(sizeSearch
.x
+ searchMargin
+ sizeText
.x
+ cancelMargin
+ sizeCancel
.x
+ 2*horizontalBorder
,
477 void wxSearchCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
479 wxSearchCtrlBase::DoMoveWindow(x
, y
, width
, height
);
481 LayoutControls(0, 0, width
, height
);
484 void wxSearchCtrl::LayoutControls(int x
, int y
, int width
, int height
)
489 wxSize sizeText
= m_text
->GetBestSize();
490 // make room for the search menu & clear button
491 int horizontalBorder
= ( sizeText
.y
- sizeText
.y
* 14 / 21 ) / 2;
492 x
+= horizontalBorder
;
494 width
-= horizontalBorder
*2;
496 if (width
< 0) width
= 0;
497 if (height
< 0) height
= 0;
499 wxSize
sizeSearch(0,0);
500 wxSize
sizeCancel(0,0);
501 int searchMargin
= 0;
502 int cancelMargin
= 0;
503 if ( m_searchButtonVisible
|| HasMenu() )
505 sizeSearch
= m_searchButton
->GetBestSize();
506 searchMargin
= MARGIN
;
508 if ( m_cancelButtonVisible
)
510 sizeCancel
= m_cancelButton
->GetBestSize();
511 cancelMargin
= MARGIN
;
513 m_searchButton
->Show( m_searchButtonVisible
|| HasMenu() );
514 m_cancelButton
->Show( m_cancelButtonVisible
);
516 if ( sizeSearch
.x
+ sizeCancel
.x
> width
)
518 sizeSearch
.x
= width
/2;
519 sizeCancel
.x
= width
/2;
523 wxCoord textWidth
= width
- sizeSearch
.x
- sizeCancel
.x
- searchMargin
- cancelMargin
- 1;
524 if (textWidth
< 0) textWidth
= 0;
526 // position the subcontrols inside the client area
528 m_searchButton
->SetSize(x
, y
+ ICON_OFFSET
- 1, sizeSearch
.x
, height
);
529 m_text
->SetSize( x
+ sizeSearch
.x
+ searchMargin
,
530 y
+ ICON_OFFSET
- BORDER
,
533 m_cancelButton
->SetSize(x
+ sizeSearch
.x
+ searchMargin
+ textWidth
+ cancelMargin
,
534 y
+ ICON_OFFSET
- 1, sizeCancel
.x
, height
);
537 wxWindowList
wxSearchCtrl::GetCompositeWindowParts() const
540 parts
.push_back(m_text
);
541 parts
.push_back(m_searchButton
);
542 parts
.push_back(m_cancelButton
);
549 wxString
wxSearchCtrl::DoGetValue() const
551 return m_text
->GetValue();
553 wxString
wxSearchCtrl::GetRange(long from
, long to
) const
555 return m_text
->GetRange(from
, to
);
558 int wxSearchCtrl::GetLineLength(long lineNo
) const
560 return m_text
->GetLineLength(lineNo
);
562 wxString
wxSearchCtrl::GetLineText(long lineNo
) const
564 return m_text
->GetLineText(lineNo
);
566 int wxSearchCtrl::GetNumberOfLines() const
568 return m_text
->GetNumberOfLines();
571 bool wxSearchCtrl::IsModified() const
573 return m_text
->IsModified();
575 bool wxSearchCtrl::IsEditable() const
577 return m_text
->IsEditable();
580 // more readable flag testing methods
581 bool wxSearchCtrl::IsSingleLine() const
583 return m_text
->IsSingleLine();
585 bool wxSearchCtrl::IsMultiLine() const
587 return m_text
->IsMultiLine();
590 // If the return values from and to are the same, there is no selection.
591 void wxSearchCtrl::GetSelection(long* from
, long* to
) const
593 m_text
->GetSelection(from
, to
);
596 wxString
wxSearchCtrl::GetStringSelection() const
598 return m_text
->GetStringSelection();
605 void wxSearchCtrl::Clear()
609 void wxSearchCtrl::Replace(long from
, long to
, const wxString
& value
)
611 m_text
->Replace(from
, to
, value
);
613 void wxSearchCtrl::Remove(long from
, long to
)
615 m_text
->Remove(from
, to
);
618 // load/save the controls contents from/to the file
619 bool wxSearchCtrl::LoadFile(const wxString
& file
)
621 return m_text
->LoadFile(file
);
623 bool wxSearchCtrl::SaveFile(const wxString
& file
)
625 return m_text
->SaveFile(file
);
628 // sets/clears the dirty flag
629 void wxSearchCtrl::MarkDirty()
633 void wxSearchCtrl::DiscardEdits()
635 m_text
->DiscardEdits();
638 // set the max number of characters which may be entered in a single line
640 void wxSearchCtrl::SetMaxLength(unsigned long len
)
642 m_text
->SetMaxLength(len
);
645 // writing text inserts it at the current position, appending always
646 // inserts it at the end
647 void wxSearchCtrl::WriteText(const wxString
& text
)
649 m_text
->WriteText(text
);
651 void wxSearchCtrl::AppendText(const wxString
& text
)
653 m_text
->AppendText(text
);
656 // insert the character which would have resulted from this key event,
657 // return true if anything has been inserted
658 bool wxSearchCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
660 return m_text
->EmulateKeyPress(event
);
663 // text control under some platforms supports the text styles: these
664 // methods allow to apply the given text style to the given selection or to
665 // set/get the style which will be used for all appended text
666 bool wxSearchCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
668 return m_text
->SetStyle(start
, end
, style
);
670 bool wxSearchCtrl::GetStyle(long position
, wxTextAttr
& style
)
672 return m_text
->GetStyle(position
, style
);
674 bool wxSearchCtrl::SetDefaultStyle(const wxTextAttr
& style
)
676 return m_text
->SetDefaultStyle(style
);
678 const wxTextAttr
& wxSearchCtrl::GetDefaultStyle() const
680 return m_text
->GetDefaultStyle();
683 // translate between the position (which is just an index in the text ctrl
684 // considering all its contents as a single strings) and (x, y) coordinates
685 // which represent column and line.
686 long wxSearchCtrl::XYToPosition(long x
, long y
) const
688 return m_text
->XYToPosition(x
, y
);
690 bool wxSearchCtrl::PositionToXY(long pos
, long *x
, long *y
) const
692 return m_text
->PositionToXY(pos
, x
, y
);
695 void wxSearchCtrl::ShowPosition(long pos
)
697 m_text
->ShowPosition(pos
);
700 // find the character at position given in pixels
702 // NB: pt is in device coords (not adjusted for the client area origin nor
704 wxTextCtrlHitTestResult
wxSearchCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
706 return m_text
->HitTest(pt
, pos
);
708 wxTextCtrlHitTestResult
wxSearchCtrl::HitTest(const wxPoint
& pt
,
710 wxTextCoord
*row
) const
712 return m_text
->HitTest(pt
, col
, row
);
715 // Clipboard operations
716 void wxSearchCtrl::Copy()
720 void wxSearchCtrl::Cut()
724 void wxSearchCtrl::Paste()
729 bool wxSearchCtrl::CanCopy() const
731 return m_text
->CanCopy();
733 bool wxSearchCtrl::CanCut() const
735 return m_text
->CanCut();
737 bool wxSearchCtrl::CanPaste() const
739 return m_text
->CanPaste();
743 void wxSearchCtrl::Undo()
747 void wxSearchCtrl::Redo()
752 bool wxSearchCtrl::CanUndo() const
754 return m_text
->CanUndo();
756 bool wxSearchCtrl::CanRedo() const
758 return m_text
->CanRedo();
762 void wxSearchCtrl::SetInsertionPoint(long pos
)
764 m_text
->SetInsertionPoint(pos
);
766 void wxSearchCtrl::SetInsertionPointEnd()
768 m_text
->SetInsertionPointEnd();
770 long wxSearchCtrl::GetInsertionPoint() const
772 return m_text
->GetInsertionPoint();
774 long wxSearchCtrl::GetLastPosition() const
776 return m_text
->GetLastPosition();
779 void wxSearchCtrl::SetSelection(long from
, long to
)
781 m_text
->SetSelection(from
, to
);
783 void wxSearchCtrl::SelectAll()
788 void wxSearchCtrl::SetEditable(bool editable
)
790 m_text
->SetEditable(editable
);
793 bool wxSearchCtrl::SetFont(const wxFont
& font
)
795 if ( !wxSearchCtrlBase::SetFont(font
) )
798 // Recreate the bitmaps as their size may have changed.
804 bool wxSearchCtrl::SetBackgroundColour(const wxColour
& colour
)
806 if ( !wxSearchCtrlBase::SetBackgroundColour(colour
) )
809 // When the background changes, re-render the bitmaps so that the correct
810 // colour shows in their "transparent" area.
816 // search control generic only
817 void wxSearchCtrl::SetSearchBitmap( const wxBitmap
& bitmap
)
819 m_searchBitmap
= bitmap
;
820 m_searchBitmapUser
= bitmap
.IsOk();
821 if ( m_searchBitmapUser
)
823 if ( m_searchButton
&& !HasMenu() )
825 m_searchButton
->SetBitmapLabel( m_searchBitmap
);
830 // the user bitmap was just cleared, generate one
837 void wxSearchCtrl::SetSearchMenuBitmap( const wxBitmap
& bitmap
)
839 m_searchMenuBitmap
= bitmap
;
840 m_searchMenuBitmapUser
= bitmap
.IsOk();
841 if ( m_searchMenuBitmapUser
)
843 if ( m_searchButton
&& m_menu
)
845 m_searchButton
->SetBitmapLabel( m_searchMenuBitmap
);
850 // the user bitmap was just cleared, generate one
855 #endif // wxUSE_MENUS
857 void wxSearchCtrl::SetCancelBitmap( const wxBitmap
& bitmap
)
859 m_cancelBitmap
= bitmap
;
860 m_cancelBitmapUser
= bitmap
.IsOk();
861 if ( m_cancelBitmapUser
)
863 if ( m_cancelButton
)
865 m_cancelButton
->SetBitmapLabel( m_cancelBitmap
);
870 // the user bitmap was just cleared, generate one
877 // override streambuf method
878 #if wxHAS_TEXT_WINDOW_STREAM
880 #endif // wxHAS_TEXT_WINDOW_STREAM
882 // stream-like insertion operators: these are always available, whether we
883 // were, or not, compiled with streambuf support
884 wxTextCtrl
& operator<<(const wxString
& s
);
885 wxTextCtrl
& operator<<(int i
);
886 wxTextCtrl
& operator<<(long i
);
887 wxTextCtrl
& operator<<(float f
);
888 wxTextCtrl
& operator<<(double d
);
889 wxTextCtrl
& operator<<(const wxChar c
);
892 void wxSearchCtrl::DoSetValue(const wxString
& value
, int flags
)
894 m_text
->DoSetValue(value
, flags
);
897 bool wxSearchCtrl::DoLoadFile(const wxString
& file
, int fileType
)
899 return m_text
->DoLoadFile(file
, fileType
);
902 bool wxSearchCtrl::DoSaveFile(const wxString
& file
, int fileType
)
904 return m_text
->DoSaveFile(file
, fileType
);
907 // do the window-specific processing after processing the update event
908 void wxSearchCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
910 wxSearchCtrlBase::DoUpdateWindowUI(event
);
913 bool wxSearchCtrl::ShouldInheritColours() const
918 // icons are rendered at 3-8 times larger than necessary and downscaled for
920 static int GetMultiplier()
923 // speed up bitmap generation by using a small bitmap
926 int depth
= ::wxDisplayDepth();
936 wxBitmap
wxSearchCtrl::RenderSearchBitmap( int x
, int y
, bool renderDrop
)
938 wxColour bg
= GetBackgroundColour();
939 wxColour fg
= GetForegroundColour().ChangeLightness(LIGHT_STEP
-20);
941 //===============================================================================
942 // begin drawing code
943 //===============================================================================
946 // force width:height ratio
958 // glass 11x11, top left corner
959 // handle (9,9)-(13,13)
960 // drop (13,16)-(19,6)-(16,9)
962 int multiplier
= GetMultiplier();
963 int penWidth
= multiplier
* 2;
965 penWidth
= penWidth
* x
/ 20;
967 wxBitmap
bitmap( multiplier
*x
, multiplier
*y
);
969 mem
.SelectObject(bitmap
);
972 mem
.SetBrush( wxBrush(bg
) );
973 mem
.SetPen( wxPen(bg
) );
974 mem
.DrawRectangle(0,0,bitmap
.GetWidth(),bitmap
.GetHeight());
977 mem
.SetBrush( wxBrush(fg
) );
978 mem
.SetPen( wxPen(fg
) );
979 int glassBase
= 5 * x
/ 20;
980 int glassFactor
= 2*glassBase
+ 1;
981 int radius
= multiplier
*glassFactor
/2;
982 mem
.DrawCircle(radius
,radius
,radius
);
983 mem
.SetBrush( wxBrush(bg
) );
984 mem
.SetPen( wxPen(bg
) );
985 mem
.DrawCircle(radius
,radius
,radius
-penWidth
);
988 int lineStart
= radius
+ (radius
-penWidth
/2) * 707 / 1000; // 707 / 1000 = 0.707 = 1/sqrt(2);
990 mem
.SetPen( wxPen(fg
) );
991 mem
.SetBrush( wxBrush(fg
) );
992 int handleCornerShift
= penWidth
* 707 / 1000 / 2; // 707 / 1000 = 0.707 = 1/sqrt(2);
993 handleCornerShift
= WXMAX( handleCornerShift
, 1 );
994 int handleBase
= 4 * x
/ 20;
995 int handleLength
= 2*handleBase
+1;
996 wxPoint handlePolygon
[] =
998 wxPoint(-handleCornerShift
,+handleCornerShift
),
999 wxPoint(+handleCornerShift
,-handleCornerShift
),
1000 wxPoint(multiplier
*handleLength
/2+handleCornerShift
,multiplier
*handleLength
/2-handleCornerShift
),
1001 wxPoint(multiplier
*handleLength
/2-handleCornerShift
,multiplier
*handleLength
/2+handleCornerShift
),
1003 mem
.DrawPolygon(WXSIZEOF(handlePolygon
),handlePolygon
,lineStart
,lineStart
);
1005 // draw drop triangle
1006 int triangleX
= 13 * x
/ 20;
1007 int triangleY
= 5 * x
/ 20;
1008 int triangleBase
= 3 * x
/ 20;
1009 int triangleFactor
= triangleBase
*2+1;
1012 wxPoint dropPolygon
[] =
1014 wxPoint(multiplier
*0,multiplier
*0), // triangle left
1015 wxPoint(multiplier
*triangleFactor
-1,multiplier
*0), // triangle right
1016 wxPoint(multiplier
*triangleFactor
/2,multiplier
*triangleFactor
/2), // triangle bottom
1018 mem
.DrawPolygon(WXSIZEOF(dropPolygon
),dropPolygon
,multiplier
*triangleX
,multiplier
*triangleY
);
1020 mem
.SelectObject(wxNullBitmap
);
1022 //===============================================================================
1024 //===============================================================================
1026 if ( multiplier
!= 1 )
1028 wxImage image
= bitmap
.ConvertToImage();
1030 bitmap
= wxBitmap( image
);
1034 // Trim the edge where the arrow would have gone
1035 bitmap
= bitmap
.GetSubBitmap(wxRect(0,0, y
,y
));
1041 wxBitmap
wxSearchCtrl::RenderCancelBitmap( int x
, int y
)
1043 wxColour bg
= GetBackgroundColour();
1044 wxColour fg
= GetForegroundColour().ChangeLightness(LIGHT_STEP
);
1046 //===============================================================================
1047 // begin drawing code
1048 //===============================================================================
1065 // cross line starts (4,4)-(10,10)
1066 // drop (13,16)-(19,6)-(16,9)
1068 int multiplier
= GetMultiplier();
1070 int penWidth
= multiplier
* x
/ 14;
1072 wxBitmap
bitmap( multiplier
*x
, multiplier
*y
);
1074 mem
.SelectObject(bitmap
);
1077 mem
.SetBrush( wxBrush(bg
) );
1078 mem
.SetPen( wxPen(bg
) );
1079 mem
.DrawRectangle(0,0,bitmap
.GetWidth(),bitmap
.GetHeight());
1082 mem
.SetBrush( wxBrush(fg
) );
1083 mem
.SetPen( wxPen(fg
) );
1084 int radius
= multiplier
*x
/2;
1085 mem
.DrawCircle(radius
,radius
,radius
);
1088 int lineStartBase
= 4 * x
/ 14;
1089 int lineLength
= x
- 2*lineStartBase
;
1091 mem
.SetPen( wxPen(bg
) );
1092 mem
.SetBrush( wxBrush(bg
) );
1093 int handleCornerShift
= penWidth
/2;
1094 handleCornerShift
= WXMAX( handleCornerShift
, 1 );
1095 wxPoint handlePolygon
[] =
1097 wxPoint(-handleCornerShift
,+handleCornerShift
),
1098 wxPoint(+handleCornerShift
,-handleCornerShift
),
1099 wxPoint(multiplier
*lineLength
+handleCornerShift
,multiplier
*lineLength
-handleCornerShift
),
1100 wxPoint(multiplier
*lineLength
-handleCornerShift
,multiplier
*lineLength
+handleCornerShift
),
1102 mem
.DrawPolygon(WXSIZEOF(handlePolygon
),handlePolygon
,multiplier
*lineStartBase
,multiplier
*lineStartBase
);
1103 wxPoint handlePolygon2
[] =
1105 wxPoint(+handleCornerShift
,+handleCornerShift
),
1106 wxPoint(-handleCornerShift
,-handleCornerShift
),
1107 wxPoint(multiplier
*lineLength
-handleCornerShift
,-multiplier
*lineLength
-handleCornerShift
),
1108 wxPoint(multiplier
*lineLength
+handleCornerShift
,-multiplier
*lineLength
+handleCornerShift
),
1110 mem
.DrawPolygon(WXSIZEOF(handlePolygon2
),handlePolygon2
,multiplier
*lineStartBase
,multiplier
*(x
-lineStartBase
));
1112 //===============================================================================
1114 //===============================================================================
1116 if ( multiplier
!= 1 )
1118 wxImage image
= bitmap
.ConvertToImage();
1120 bitmap
= wxBitmap( image
);
1126 void wxSearchCtrl::RecalcBitmaps()
1132 wxSize sizeText
= m_text
->GetBestSize();
1134 int bitmapHeight
= sizeText
.y
- 2 * ICON_MARGIN
;
1135 int bitmapWidth
= sizeText
.y
* 20 / 14;
1137 if ( !m_searchBitmapUser
)
1140 !m_searchBitmap
.IsOk() ||
1141 m_searchBitmap
.GetHeight() != bitmapHeight
||
1142 m_searchBitmap
.GetWidth() != bitmapWidth
1145 m_searchBitmap
= RenderSearchBitmap(bitmapWidth
,bitmapHeight
,false);
1148 m_searchButton
->SetBitmapLabel(m_searchBitmap
);
1151 // else this bitmap was set by user, don't alter
1155 if ( !m_searchMenuBitmapUser
)
1158 !m_searchMenuBitmap
.IsOk() ||
1159 m_searchMenuBitmap
.GetHeight() != bitmapHeight
||
1160 m_searchMenuBitmap
.GetWidth() != bitmapWidth
1163 m_searchMenuBitmap
= RenderSearchBitmap(bitmapWidth
,bitmapHeight
,true);
1166 m_searchButton
->SetBitmapLabel(m_searchMenuBitmap
);
1169 // else this bitmap was set by user, don't alter
1171 #endif // wxUSE_MENUS
1173 if ( !m_cancelBitmapUser
)
1176 !m_cancelBitmap
.IsOk() ||
1177 m_cancelBitmap
.GetHeight() != bitmapHeight
||
1178 m_cancelBitmap
.GetWidth() != bitmapHeight
1181 m_cancelBitmap
= RenderCancelBitmap(bitmapHeight
-BORDER
-1,bitmapHeight
-BORDER
-1); // square
1182 m_cancelButton
->SetBitmapLabel(m_cancelBitmap
);
1184 // else this bitmap was set by user, don't alter
1188 void wxSearchCtrl::OnCancelButton( wxCommandEvent
& event
)
1194 void wxSearchCtrl::OnSetFocus( wxFocusEvent
& /*event*/ )
1202 void wxSearchCtrl::OnSize( wxSizeEvent
& WXUNUSED(event
) )
1205 GetSize(&width
, &height
);
1206 LayoutControls(0, 0, width
, height
);
1211 void wxSearchCtrl::PopupSearchMenu()
1215 wxSize size
= GetSize();
1216 PopupMenu( m_menu
, 0, size
.y
);
1220 #endif // wxUSE_MENUS
1222 #endif // !wxUSE_NATIVE_SEARCH_CONTROL
1224 #endif // wxUSE_SEARCHCTRL