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
76 // provide access to the base class protected methods to wxSearchCtrl which
77 // needs to forward to them
78 void DoSetValue(const wxString
& value
, int flags
)
80 wxTextCtrl::DoSetValue(value
, flags
);
83 bool DoLoadFile(const wxString
& file
, int fileType
)
85 return wxTextCtrl::DoLoadFile(file
, fileType
);
88 bool DoSaveFile(const wxString
& file
, int fileType
)
90 return wxTextCtrl::DoSaveFile(file
, fileType
);
94 void OnText(wxCommandEvent
& eventText
)
96 wxCommandEvent
event(eventText
);
97 event
.SetEventObject(m_search
);
98 event
.SetId(m_search
->GetId());
100 m_search
->GetEventHandler()->ProcessEvent(event
);
103 void OnTextUrl(wxTextUrlEvent
& eventText
)
105 // copy constructor is disabled for some reason?
106 //wxTextUrlEvent event(eventText);
107 wxTextUrlEvent
event(
109 eventText
.GetMouseEvent(),
110 eventText
.GetURLStart(),
111 eventText
.GetURLEnd()
113 event
.SetEventObject(m_search
);
115 m_search
->GetEventHandler()->ProcessEvent(event
);
119 // We increase the text control height to be the same as for the controls
120 // with border as this is what we actually need here because even though
121 // this control itself is borderless, it's inside wxSearchCtrl which does
122 // have the border and so should have the same height as the normal text
123 // entries with border.
125 // This is a bit ugly and it would arguably be better to use whatever size
126 // the base class version returns and just centre the text vertically in
127 // the search control but I failed to modify the code in LayoutControls()
128 // to do this easily and as there is much in that code I don't understand
129 // (notably what is the logic for buttons sizing?) I prefer to not touch it
131 virtual wxSize
DoGetBestSize() const
133 const long flags
= GetWindowStyleFlag();
134 wxSearchTextCtrl
* const self
= const_cast<wxSearchTextCtrl
*>(this);
136 self
->SetWindowStyleFlag((flags
& ~wxBORDER_MASK
) | wxBORDER_DEFAULT
);
137 const wxSize size
= wxTextCtrl::DoGetBestSize();
138 self
->SetWindowStyleFlag(flags
);
145 wxSearchCtrl
* m_search
;
147 DECLARE_EVENT_TABLE()
150 BEGIN_EVENT_TABLE(wxSearchTextCtrl
, wxTextCtrl
)
151 EVT_TEXT(wxID_ANY
, wxSearchTextCtrl::OnText
)
152 EVT_TEXT_ENTER(wxID_ANY
, wxSearchTextCtrl::OnText
)
153 EVT_TEXT_URL(wxID_ANY
, wxSearchTextCtrl::OnTextUrl
)
154 EVT_TEXT_MAXLEN(wxID_ANY
, wxSearchTextCtrl::OnText
)
157 // ----------------------------------------------------------------------------
158 // wxSearchButton: search button used by search control
159 // ----------------------------------------------------------------------------
161 class wxSearchButton
: public wxControl
164 wxSearchButton(wxSearchCtrl
*search
, int eventType
, const wxBitmap
& bmp
)
165 : wxControl(search
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxNO_BORDER
),
167 m_eventType(eventType
),
171 void SetBitmapLabel(const wxBitmap
& label
)
174 InvalidateBestSize();
177 // The buttons in wxSearchCtrl shouldn't accept focus from keyboard because
178 // this would interfere with the usual TAB processing: the user expects
179 // that pressing TAB in the search control should switch focus to the next
180 // control and not give it to the button inside the same control. Besides,
181 // the search button can be already activated by pressing "Enter" so there
182 // is really no reason for it to be able to get focus from keyboard.
183 virtual bool AcceptsFocusFromKeyboard() const { return false; }
186 wxSize
DoGetBestSize() const
188 return wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight());
191 void OnLeftUp(wxMouseEvent
&)
193 wxCommandEvent
event(m_eventType
, m_search
->GetId());
194 event
.SetEventObject(m_search
);
196 if ( m_eventType
== wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
)
198 // it's convenient to have the string to search for directly in the
199 // event instead of having to retrieve it from the control in the
200 // event handler code later, so provide it here
201 event
.SetString(m_search
->GetValue());
204 GetEventHandler()->ProcessEvent(event
);
206 m_search
->SetFocus();
209 if ( m_eventType
== wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
)
211 // this happens automatically, just like on Mac OS X
212 m_search
->PopupSearchMenu();
214 #endif // wxUSE_MENUS
217 void OnPaint(wxPaintEvent
&)
220 dc
.DrawBitmap(m_bmp
, 0,0, true);
225 wxSearchCtrl
*m_search
;
226 wxEventType m_eventType
;
229 DECLARE_EVENT_TABLE()
232 BEGIN_EVENT_TABLE(wxSearchButton
, wxControl
)
233 EVT_LEFT_UP(wxSearchButton::OnLeftUp
)
234 EVT_PAINT(wxSearchButton::OnPaint
)
237 BEGIN_EVENT_TABLE(wxSearchCtrl
, wxSearchCtrlBase
)
238 EVT_SEARCHCTRL_CANCEL_BTN(wxID_ANY
, wxSearchCtrl::OnCancelButton
)
239 EVT_SET_FOCUS(wxSearchCtrl::OnSetFocus
)
240 EVT_SIZE(wxSearchCtrl::OnSize
)
243 IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl
, wxSearchCtrlBase
)
245 // ============================================================================
247 // ============================================================================
249 // ----------------------------------------------------------------------------
250 // wxSearchCtrl creation
251 // ----------------------------------------------------------------------------
256 wxSearchCtrl::wxSearchCtrl()
261 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
262 const wxString
& value
,
266 const wxValidator
& validator
,
267 const wxString
& name
)
271 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
274 void wxSearchCtrl::Init()
277 m_searchButton
= NULL
;
278 m_cancelButton
= NULL
;
281 #endif // wxUSE_MENUS
283 m_searchButtonVisible
= true;
284 m_cancelButtonVisible
= false;
286 m_searchBitmapUser
= false;
287 m_cancelBitmapUser
= false;
289 m_searchMenuBitmapUser
= false;
290 #endif // wxUSE_MENUS
293 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
294 const wxString
& value
,
298 const wxValidator
& validator
,
299 const wxString
& name
)
301 // force border style for more native appearance
302 style
&= ~wxBORDER_MASK
;
304 style
|= wxBORDER_SUNKEN
;
305 #elif defined(__WXMSW__)
306 // Don't set the style explicitly, let GetDefaultBorder() work it out, unless
307 // we will get a sunken border (e.g. on Windows 200) in which case we must
308 // override with a simple border.
309 if (GetDefaultBorder() == wxBORDER_SUNKEN
)
310 style
|= wxBORDER_SIMPLE
;
312 style
|= wxBORDER_SIMPLE
;
314 if ( !wxSearchCtrlBaseBaseClass::Create(parent
, id
, pos
, size
,
315 style
, validator
, name
) )
320 m_text
= new wxSearchTextCtrl(this, value
, style
);
322 m_searchButton
= new wxSearchButton(this,
323 wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
,
325 m_cancelButton
= new wxSearchButton(this,
326 wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN
,
329 SetBackgroundColour( m_text
->GetBackgroundColour() );
333 SetInitialSize(size
);
338 wxSearchCtrl::~wxSearchCtrl()
341 delete m_searchButton
;
342 delete m_cancelButton
;
345 #endif // wxUSE_MENUS
349 // search control specific interfaces
352 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
354 if ( menu
== m_menu
)
359 bool hadMenu
= (m_menu
!= NULL
);
363 if ( m_menu
&& !hadMenu
)
365 m_searchButton
->SetBitmapLabel(m_searchMenuBitmap
);
366 m_searchButton
->Refresh();
368 else if ( !m_menu
&& hadMenu
)
370 m_searchButton
->SetBitmapLabel(m_searchBitmap
);
371 if ( m_searchButtonVisible
)
373 m_searchButton
->Refresh();
376 wxRect rect
= GetRect();
377 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
380 wxMenu
* wxSearchCtrl::GetMenu()
385 #endif // wxUSE_MENUS
387 void wxSearchCtrl::ShowSearchButton( bool show
)
389 if ( m_searchButtonVisible
== show
)
394 m_searchButtonVisible
= show
;
395 if ( m_searchButtonVisible
)
400 wxRect rect
= GetRect();
401 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
404 bool wxSearchCtrl::IsSearchButtonVisible() const
406 return m_searchButtonVisible
;
410 void wxSearchCtrl::ShowCancelButton( bool show
)
412 if ( m_cancelButtonVisible
== show
)
417 m_cancelButtonVisible
= show
;
419 wxRect rect
= GetRect();
420 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
423 bool wxSearchCtrl::IsCancelButtonVisible() const
425 return m_cancelButtonVisible
;
428 void wxSearchCtrl::SetDescriptiveText(const wxString
& text
)
430 m_text
->SetHint(text
);
433 wxString
wxSearchCtrl::GetDescriptiveText() const
435 return m_text
->GetHint();
438 // ----------------------------------------------------------------------------
440 // ----------------------------------------------------------------------------
442 wxSize
wxSearchCtrl::DoGetBestSize() const
444 wxSize sizeText
= m_text
->GetBestSize();
445 wxSize
sizeSearch(0,0);
446 wxSize
sizeCancel(0,0);
447 int searchMargin
= 0;
448 int cancelMargin
= 0;
449 if ( m_searchButtonVisible
|| HasMenu() )
451 sizeSearch
= m_searchButton
->GetBestSize();
452 searchMargin
= MARGIN
;
454 if ( m_cancelButtonVisible
)
456 sizeCancel
= m_cancelButton
->GetBestSize();
457 cancelMargin
= MARGIN
;
460 int horizontalBorder
= 1 + ( sizeText
.y
- sizeText
.y
* 14 / 21 ) / 2;
462 // buttons are square and equal to the height of the text control
463 int height
= sizeText
.y
;
464 return wxSize(sizeSearch
.x
+ searchMargin
+ sizeText
.x
+ cancelMargin
+ sizeCancel
.x
+ 2*horizontalBorder
,
468 void wxSearchCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
470 wxSearchCtrlBase::DoMoveWindow(x
, y
, width
, height
);
472 LayoutControls(0, 0, width
, height
);
475 void wxSearchCtrl::LayoutControls(int x
, int y
, int width
, int height
)
480 wxSize sizeText
= m_text
->GetBestSize();
481 // make room for the search menu & clear button
482 int horizontalBorder
= ( sizeText
.y
- sizeText
.y
* 14 / 21 ) / 2;
483 x
+= horizontalBorder
;
485 width
-= horizontalBorder
*2;
487 if (width
< 0) width
= 0;
488 if (height
< 0) height
= 0;
490 wxSize
sizeSearch(0,0);
491 wxSize
sizeCancel(0,0);
492 int searchMargin
= 0;
493 int cancelMargin
= 0;
494 if ( m_searchButtonVisible
|| HasMenu() )
496 sizeSearch
= m_searchButton
->GetBestSize();
497 searchMargin
= MARGIN
;
499 if ( m_cancelButtonVisible
)
501 sizeCancel
= m_cancelButton
->GetBestSize();
502 cancelMargin
= MARGIN
;
504 m_searchButton
->Show( m_searchButtonVisible
|| HasMenu() );
505 m_cancelButton
->Show( m_cancelButtonVisible
);
507 if ( sizeSearch
.x
+ sizeCancel
.x
> width
)
509 sizeSearch
.x
= width
/2;
510 sizeCancel
.x
= width
/2;
514 wxCoord textWidth
= width
- sizeSearch
.x
- sizeCancel
.x
- searchMargin
- cancelMargin
- 1;
515 if (textWidth
< 0) textWidth
= 0;
517 // position the subcontrols inside the client area
519 m_searchButton
->SetSize(x
, y
+ ICON_OFFSET
- 1, sizeSearch
.x
, height
);
520 m_text
->SetSize( x
+ sizeSearch
.x
+ searchMargin
,
521 y
+ ICON_OFFSET
- BORDER
,
524 m_cancelButton
->SetSize(x
+ sizeSearch
.x
+ searchMargin
+ textWidth
+ cancelMargin
,
525 y
+ ICON_OFFSET
- 1, sizeCancel
.x
, height
);
528 wxWindowList
wxSearchCtrl::GetCompositeWindowParts() const
531 parts
.push_back(m_text
);
532 parts
.push_back(m_searchButton
);
533 parts
.push_back(m_cancelButton
);
540 wxString
wxSearchCtrl::DoGetValue() const
542 return m_text
->GetValue();
544 wxString
wxSearchCtrl::GetRange(long from
, long to
) const
546 return m_text
->GetRange(from
, to
);
549 int wxSearchCtrl::GetLineLength(long lineNo
) const
551 return m_text
->GetLineLength(lineNo
);
553 wxString
wxSearchCtrl::GetLineText(long lineNo
) const
555 return m_text
->GetLineText(lineNo
);
557 int wxSearchCtrl::GetNumberOfLines() const
559 return m_text
->GetNumberOfLines();
562 bool wxSearchCtrl::IsModified() const
564 return m_text
->IsModified();
566 bool wxSearchCtrl::IsEditable() const
568 return m_text
->IsEditable();
571 // more readable flag testing methods
572 bool wxSearchCtrl::IsSingleLine() const
574 return m_text
->IsSingleLine();
576 bool wxSearchCtrl::IsMultiLine() const
578 return m_text
->IsMultiLine();
581 // If the return values from and to are the same, there is no selection.
582 void wxSearchCtrl::GetSelection(long* from
, long* to
) const
584 m_text
->GetSelection(from
, to
);
587 wxString
wxSearchCtrl::GetStringSelection() const
589 return m_text
->GetStringSelection();
596 void wxSearchCtrl::Clear()
600 void wxSearchCtrl::Replace(long from
, long to
, const wxString
& value
)
602 m_text
->Replace(from
, to
, value
);
604 void wxSearchCtrl::Remove(long from
, long to
)
606 m_text
->Remove(from
, to
);
609 // load/save the controls contents from/to the file
610 bool wxSearchCtrl::LoadFile(const wxString
& file
)
612 return m_text
->LoadFile(file
);
614 bool wxSearchCtrl::SaveFile(const wxString
& file
)
616 return m_text
->SaveFile(file
);
619 // sets/clears the dirty flag
620 void wxSearchCtrl::MarkDirty()
624 void wxSearchCtrl::DiscardEdits()
626 m_text
->DiscardEdits();
629 // set the max number of characters which may be entered in a single line
631 void wxSearchCtrl::SetMaxLength(unsigned long len
)
633 m_text
->SetMaxLength(len
);
636 // writing text inserts it at the current position, appending always
637 // inserts it at the end
638 void wxSearchCtrl::WriteText(const wxString
& text
)
640 m_text
->WriteText(text
);
642 void wxSearchCtrl::AppendText(const wxString
& text
)
644 m_text
->AppendText(text
);
647 // insert the character which would have resulted from this key event,
648 // return true if anything has been inserted
649 bool wxSearchCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
651 return m_text
->EmulateKeyPress(event
);
654 // text control under some platforms supports the text styles: these
655 // methods allow to apply the given text style to the given selection or to
656 // set/get the style which will be used for all appended text
657 bool wxSearchCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
659 return m_text
->SetStyle(start
, end
, style
);
661 bool wxSearchCtrl::GetStyle(long position
, wxTextAttr
& style
)
663 return m_text
->GetStyle(position
, style
);
665 bool wxSearchCtrl::SetDefaultStyle(const wxTextAttr
& style
)
667 return m_text
->SetDefaultStyle(style
);
669 const wxTextAttr
& wxSearchCtrl::GetDefaultStyle() const
671 return m_text
->GetDefaultStyle();
674 // translate between the position (which is just an index in the text ctrl
675 // considering all its contents as a single strings) and (x, y) coordinates
676 // which represent column and line.
677 long wxSearchCtrl::XYToPosition(long x
, long y
) const
679 return m_text
->XYToPosition(x
, y
);
681 bool wxSearchCtrl::PositionToXY(long pos
, long *x
, long *y
) const
683 return m_text
->PositionToXY(pos
, x
, y
);
686 void wxSearchCtrl::ShowPosition(long pos
)
688 m_text
->ShowPosition(pos
);
691 // find the character at position given in pixels
693 // NB: pt is in device coords (not adjusted for the client area origin nor
695 wxTextCtrlHitTestResult
wxSearchCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
697 return m_text
->HitTest(pt
, pos
);
699 wxTextCtrlHitTestResult
wxSearchCtrl::HitTest(const wxPoint
& pt
,
701 wxTextCoord
*row
) const
703 return m_text
->HitTest(pt
, col
, row
);
706 // Clipboard operations
707 void wxSearchCtrl::Copy()
711 void wxSearchCtrl::Cut()
715 void wxSearchCtrl::Paste()
720 bool wxSearchCtrl::CanCopy() const
722 return m_text
->CanCopy();
724 bool wxSearchCtrl::CanCut() const
726 return m_text
->CanCut();
728 bool wxSearchCtrl::CanPaste() const
730 return m_text
->CanPaste();
734 void wxSearchCtrl::Undo()
738 void wxSearchCtrl::Redo()
743 bool wxSearchCtrl::CanUndo() const
745 return m_text
->CanUndo();
747 bool wxSearchCtrl::CanRedo() const
749 return m_text
->CanRedo();
753 void wxSearchCtrl::SetInsertionPoint(long pos
)
755 m_text
->SetInsertionPoint(pos
);
757 void wxSearchCtrl::SetInsertionPointEnd()
759 m_text
->SetInsertionPointEnd();
761 long wxSearchCtrl::GetInsertionPoint() const
763 return m_text
->GetInsertionPoint();
765 long wxSearchCtrl::GetLastPosition() const
767 return m_text
->GetLastPosition();
770 void wxSearchCtrl::SetSelection(long from
, long to
)
772 m_text
->SetSelection(from
, to
);
774 void wxSearchCtrl::SelectAll()
779 void wxSearchCtrl::SetEditable(bool editable
)
781 m_text
->SetEditable(editable
);
784 bool wxSearchCtrl::SetFont(const wxFont
& font
)
786 if ( !wxSearchCtrlBase::SetFont(font
) )
789 // Recreate the bitmaps as their size may have changed.
795 bool wxSearchCtrl::SetBackgroundColour(const wxColour
& colour
)
797 if ( !wxSearchCtrlBase::SetBackgroundColour(colour
) )
800 // When the background changes, re-render the bitmaps so that the correct
801 // colour shows in their "transparent" area.
807 // search control generic only
808 void wxSearchCtrl::SetSearchBitmap( const wxBitmap
& bitmap
)
810 m_searchBitmap
= bitmap
;
811 m_searchBitmapUser
= bitmap
.IsOk();
812 if ( m_searchBitmapUser
)
814 if ( m_searchButton
&& !HasMenu() )
816 m_searchButton
->SetBitmapLabel( m_searchBitmap
);
821 // the user bitmap was just cleared, generate one
828 void wxSearchCtrl::SetSearchMenuBitmap( const wxBitmap
& bitmap
)
830 m_searchMenuBitmap
= bitmap
;
831 m_searchMenuBitmapUser
= bitmap
.IsOk();
832 if ( m_searchMenuBitmapUser
)
834 if ( m_searchButton
&& m_menu
)
836 m_searchButton
->SetBitmapLabel( m_searchMenuBitmap
);
841 // the user bitmap was just cleared, generate one
846 #endif // wxUSE_MENUS
848 void wxSearchCtrl::SetCancelBitmap( const wxBitmap
& bitmap
)
850 m_cancelBitmap
= bitmap
;
851 m_cancelBitmapUser
= bitmap
.IsOk();
852 if ( m_cancelBitmapUser
)
854 if ( m_cancelButton
)
856 m_cancelButton
->SetBitmapLabel( m_cancelBitmap
);
861 // the user bitmap was just cleared, generate one
868 // override streambuf method
869 #if wxHAS_TEXT_WINDOW_STREAM
871 #endif // wxHAS_TEXT_WINDOW_STREAM
873 // stream-like insertion operators: these are always available, whether we
874 // were, or not, compiled with streambuf support
875 wxTextCtrl
& operator<<(const wxString
& s
);
876 wxTextCtrl
& operator<<(int i
);
877 wxTextCtrl
& operator<<(long i
);
878 wxTextCtrl
& operator<<(float f
);
879 wxTextCtrl
& operator<<(double d
);
880 wxTextCtrl
& operator<<(const wxChar c
);
883 void wxSearchCtrl::DoSetValue(const wxString
& value
, int flags
)
885 m_text
->DoSetValue(value
, flags
);
888 bool wxSearchCtrl::DoLoadFile(const wxString
& file
, int fileType
)
890 return m_text
->DoLoadFile(file
, fileType
);
893 bool wxSearchCtrl::DoSaveFile(const wxString
& file
, int fileType
)
895 return m_text
->DoSaveFile(file
, fileType
);
898 // do the window-specific processing after processing the update event
899 void wxSearchCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
901 wxSearchCtrlBase::DoUpdateWindowUI(event
);
904 bool wxSearchCtrl::ShouldInheritColours() const
909 // icons are rendered at 3-8 times larger than necessary and downscaled for
911 static int GetMultiplier()
914 // speed up bitmap generation by using a small bitmap
917 int depth
= ::wxDisplayDepth();
927 wxBitmap
wxSearchCtrl::RenderSearchBitmap( int x
, int y
, bool renderDrop
)
929 wxColour bg
= GetBackgroundColour();
930 wxColour fg
= GetForegroundColour().ChangeLightness(LIGHT_STEP
-20);
932 //===============================================================================
933 // begin drawing code
934 //===============================================================================
937 // force width:height ratio
949 // glass 11x11, top left corner
950 // handle (9,9)-(13,13)
951 // drop (13,16)-(19,6)-(16,9)
953 int multiplier
= GetMultiplier();
954 int penWidth
= multiplier
* 2;
956 penWidth
= penWidth
* x
/ 20;
958 wxBitmap
bitmap( multiplier
*x
, multiplier
*y
);
960 mem
.SelectObject(bitmap
);
963 mem
.SetBrush( wxBrush(bg
) );
964 mem
.SetPen( wxPen(bg
) );
965 mem
.DrawRectangle(0,0,bitmap
.GetWidth(),bitmap
.GetHeight());
968 mem
.SetBrush( wxBrush(fg
) );
969 mem
.SetPen( wxPen(fg
) );
970 int glassBase
= 5 * x
/ 20;
971 int glassFactor
= 2*glassBase
+ 1;
972 int radius
= multiplier
*glassFactor
/2;
973 mem
.DrawCircle(radius
,radius
,radius
);
974 mem
.SetBrush( wxBrush(bg
) );
975 mem
.SetPen( wxPen(bg
) );
976 mem
.DrawCircle(radius
,radius
,radius
-penWidth
);
979 int lineStart
= radius
+ (radius
-penWidth
/2) * 707 / 1000; // 707 / 1000 = 0.707 = 1/sqrt(2);
981 mem
.SetPen( wxPen(fg
) );
982 mem
.SetBrush( wxBrush(fg
) );
983 int handleCornerShift
= penWidth
* 707 / 1000 / 2; // 707 / 1000 = 0.707 = 1/sqrt(2);
984 handleCornerShift
= WXMAX( handleCornerShift
, 1 );
985 int handleBase
= 4 * x
/ 20;
986 int handleLength
= 2*handleBase
+1;
987 wxPoint handlePolygon
[] =
989 wxPoint(-handleCornerShift
,+handleCornerShift
),
990 wxPoint(+handleCornerShift
,-handleCornerShift
),
991 wxPoint(multiplier
*handleLength
/2+handleCornerShift
,multiplier
*handleLength
/2-handleCornerShift
),
992 wxPoint(multiplier
*handleLength
/2-handleCornerShift
,multiplier
*handleLength
/2+handleCornerShift
),
994 mem
.DrawPolygon(WXSIZEOF(handlePolygon
),handlePolygon
,lineStart
,lineStart
);
996 // draw drop triangle
997 int triangleX
= 13 * x
/ 20;
998 int triangleY
= 5 * x
/ 20;
999 int triangleBase
= 3 * x
/ 20;
1000 int triangleFactor
= triangleBase
*2+1;
1003 wxPoint dropPolygon
[] =
1005 wxPoint(multiplier
*0,multiplier
*0), // triangle left
1006 wxPoint(multiplier
*triangleFactor
-1,multiplier
*0), // triangle right
1007 wxPoint(multiplier
*triangleFactor
/2,multiplier
*triangleFactor
/2), // triangle bottom
1009 mem
.DrawPolygon(WXSIZEOF(dropPolygon
),dropPolygon
,multiplier
*triangleX
,multiplier
*triangleY
);
1011 mem
.SelectObject(wxNullBitmap
);
1013 //===============================================================================
1015 //===============================================================================
1017 if ( multiplier
!= 1 )
1019 wxImage image
= bitmap
.ConvertToImage();
1021 bitmap
= wxBitmap( image
);
1025 // Trim the edge where the arrow would have gone
1026 bitmap
= bitmap
.GetSubBitmap(wxRect(0,0, y
,y
));
1032 wxBitmap
wxSearchCtrl::RenderCancelBitmap( int x
, int y
)
1034 wxColour bg
= GetBackgroundColour();
1035 wxColour fg
= GetForegroundColour().ChangeLightness(LIGHT_STEP
);
1037 //===============================================================================
1038 // begin drawing code
1039 //===============================================================================
1056 // cross line starts (4,4)-(10,10)
1057 // drop (13,16)-(19,6)-(16,9)
1059 int multiplier
= GetMultiplier();
1061 int penWidth
= multiplier
* x
/ 14;
1063 wxBitmap
bitmap( multiplier
*x
, multiplier
*y
);
1065 mem
.SelectObject(bitmap
);
1068 mem
.SetBrush( wxBrush(bg
) );
1069 mem
.SetPen( wxPen(bg
) );
1070 mem
.DrawRectangle(0,0,bitmap
.GetWidth(),bitmap
.GetHeight());
1073 mem
.SetBrush( wxBrush(fg
) );
1074 mem
.SetPen( wxPen(fg
) );
1075 int radius
= multiplier
*x
/2;
1076 mem
.DrawCircle(radius
,radius
,radius
);
1079 int lineStartBase
= 4 * x
/ 14;
1080 int lineLength
= x
- 2*lineStartBase
;
1082 mem
.SetPen( wxPen(bg
) );
1083 mem
.SetBrush( wxBrush(bg
) );
1084 int handleCornerShift
= penWidth
/2;
1085 handleCornerShift
= WXMAX( handleCornerShift
, 1 );
1086 wxPoint handlePolygon
[] =
1088 wxPoint(-handleCornerShift
,+handleCornerShift
),
1089 wxPoint(+handleCornerShift
,-handleCornerShift
),
1090 wxPoint(multiplier
*lineLength
+handleCornerShift
,multiplier
*lineLength
-handleCornerShift
),
1091 wxPoint(multiplier
*lineLength
-handleCornerShift
,multiplier
*lineLength
+handleCornerShift
),
1093 mem
.DrawPolygon(WXSIZEOF(handlePolygon
),handlePolygon
,multiplier
*lineStartBase
,multiplier
*lineStartBase
);
1094 wxPoint handlePolygon2
[] =
1096 wxPoint(+handleCornerShift
,+handleCornerShift
),
1097 wxPoint(-handleCornerShift
,-handleCornerShift
),
1098 wxPoint(multiplier
*lineLength
-handleCornerShift
,-multiplier
*lineLength
-handleCornerShift
),
1099 wxPoint(multiplier
*lineLength
+handleCornerShift
,-multiplier
*lineLength
+handleCornerShift
),
1101 mem
.DrawPolygon(WXSIZEOF(handlePolygon2
),handlePolygon2
,multiplier
*lineStartBase
,multiplier
*(x
-lineStartBase
));
1103 //===============================================================================
1105 //===============================================================================
1107 if ( multiplier
!= 1 )
1109 wxImage image
= bitmap
.ConvertToImage();
1111 bitmap
= wxBitmap( image
);
1117 void wxSearchCtrl::RecalcBitmaps()
1123 wxSize sizeText
= m_text
->GetBestSize();
1125 int bitmapHeight
= sizeText
.y
- 2 * ICON_MARGIN
;
1126 int bitmapWidth
= sizeText
.y
* 20 / 14;
1128 if ( !m_searchBitmapUser
)
1131 !m_searchBitmap
.IsOk() ||
1132 m_searchBitmap
.GetHeight() != bitmapHeight
||
1133 m_searchBitmap
.GetWidth() != bitmapWidth
1136 m_searchBitmap
= RenderSearchBitmap(bitmapWidth
,bitmapHeight
,false);
1139 m_searchButton
->SetBitmapLabel(m_searchBitmap
);
1142 // else this bitmap was set by user, don't alter
1146 if ( !m_searchMenuBitmapUser
)
1149 !m_searchMenuBitmap
.IsOk() ||
1150 m_searchMenuBitmap
.GetHeight() != bitmapHeight
||
1151 m_searchMenuBitmap
.GetWidth() != bitmapWidth
1154 m_searchMenuBitmap
= RenderSearchBitmap(bitmapWidth
,bitmapHeight
,true);
1157 m_searchButton
->SetBitmapLabel(m_searchMenuBitmap
);
1160 // else this bitmap was set by user, don't alter
1162 #endif // wxUSE_MENUS
1164 if ( !m_cancelBitmapUser
)
1167 !m_cancelBitmap
.IsOk() ||
1168 m_cancelBitmap
.GetHeight() != bitmapHeight
||
1169 m_cancelBitmap
.GetWidth() != bitmapHeight
1172 m_cancelBitmap
= RenderCancelBitmap(bitmapHeight
-BORDER
-1,bitmapHeight
-BORDER
-1); // square
1173 m_cancelButton
->SetBitmapLabel(m_cancelBitmap
);
1175 // else this bitmap was set by user, don't alter
1179 void wxSearchCtrl::OnCancelButton( wxCommandEvent
& WXUNUSED(event
) )
1184 void wxSearchCtrl::OnSetFocus( wxFocusEvent
& /*event*/ )
1192 void wxSearchCtrl::OnSize( wxSizeEvent
& WXUNUSED(event
) )
1195 GetSize(&width
, &height
);
1196 LayoutControls(0, 0, width
, height
);
1201 void wxSearchCtrl::PopupSearchMenu()
1205 wxSize size
= GetSize();
1206 PopupMenu( m_menu
, 0, size
.y
);
1210 #endif // wxUSE_MENUS
1212 #endif // !wxUSE_NATIVE_SEARCH_CONTROL
1214 #endif // wxUSE_SEARCHCTRL