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 void OnText(wxCommandEvent
& eventText
)
140 wxCommandEvent
event(eventText
);
141 event
.SetEventObject(m_search
);
142 event
.SetId(m_search
->GetId());
144 m_search
->GetEventHandler()->ProcessEvent(event
);
147 void OnTextUrl(wxTextUrlEvent
& eventText
)
149 // copy constructor is disabled for some reason?
150 //wxTextUrlEvent event(eventText);
151 wxTextUrlEvent
event(
153 eventText
.GetMouseEvent(),
154 eventText
.GetURLStart(),
155 eventText
.GetURLEnd()
157 event
.SetEventObject(m_search
);
159 m_search
->GetEventHandler()->ProcessEvent(event
);
162 void OnIdle(wxIdleEvent
& WXUNUSED(event
))
164 if ( IsEmpty() && !(wxWindow::FindFocus() == this) )
166 ChangeValue(m_descriptiveText
);
167 SetInsertionPoint(0);
168 SetForegroundColour(wxStepColour(m_defaultFG
, LIGHT_STEP
));
172 void OnFocus(wxFocusEvent
& event
)
175 if ( GetValue() == m_descriptiveText
)
177 ChangeValue(wxEmptyString
);
178 SetForegroundColour(m_defaultFG
);
183 wxSearchCtrl
* m_search
;
184 wxString m_descriptiveText
;
185 wxColour m_defaultFG
;
187 DECLARE_EVENT_TABLE()
190 BEGIN_EVENT_TABLE(wxSearchTextCtrl
, wxTextCtrl
)
191 EVT_TEXT(wxID_ANY
, wxSearchTextCtrl::OnText
)
192 EVT_TEXT_ENTER(wxID_ANY
, wxSearchTextCtrl::OnText
)
193 EVT_TEXT_URL(wxID_ANY
, wxSearchTextCtrl::OnTextUrl
)
194 EVT_TEXT_MAXLEN(wxID_ANY
, wxSearchTextCtrl::OnText
)
195 EVT_IDLE(wxSearchTextCtrl::OnIdle
)
196 EVT_SET_FOCUS(wxSearchTextCtrl::OnFocus
)
199 // ----------------------------------------------------------------------------
200 // wxSearchButton: search button used by search control
201 // ----------------------------------------------------------------------------
203 class wxSearchButton
: public wxControl
206 wxSearchButton(wxSearchCtrl
*search
, int eventType
, const wxBitmap
& bmp
)
207 : wxControl(search
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxNO_BORDER
),
209 m_eventType(eventType
),
213 void SetBitmapLabel(const wxBitmap
& label
) { m_bmp
= label
; }
217 wxSize
DoGetBestSize() const
219 return wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight());
222 void OnLeftUp(wxMouseEvent
&)
224 wxCommandEvent
event(m_eventType
, m_search
->GetId());
225 event
.SetEventObject(m_search
);
227 GetEventHandler()->ProcessEvent(event
);
229 m_search
->SetFocus();
232 if ( m_eventType
== wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
)
234 // this happens automatically, just like on Mac OS X
235 m_search
->PopupSearchMenu();
237 #endif // wxUSE_MENUS
240 void OnPaint(wxPaintEvent
&)
243 dc
.DrawBitmap(m_bmp
, 0,0, true);
248 wxSearchCtrl
*m_search
;
249 wxEventType m_eventType
;
252 DECLARE_EVENT_TABLE()
255 BEGIN_EVENT_TABLE(wxSearchButton
, wxControl
)
256 EVT_LEFT_UP(wxSearchButton::OnLeftUp
)
257 EVT_PAINT(wxSearchButton::OnPaint
)
260 BEGIN_EVENT_TABLE(wxSearchCtrl
, wxSearchCtrlBase
)
261 EVT_SEARCHCTRL_SEARCH_BTN(wxID_ANY
, wxSearchCtrl::OnSearchButton
)
262 EVT_SET_FOCUS(wxSearchCtrl::OnSetFocus
)
263 EVT_SIZE(wxSearchCtrl::OnSize
)
266 IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl
, wxSearchCtrlBase
)
268 // ============================================================================
270 // ============================================================================
272 // ----------------------------------------------------------------------------
273 // wxSearchCtrl creation
274 // ----------------------------------------------------------------------------
279 wxSearchCtrl::wxSearchCtrl()
284 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
285 const wxString
& value
,
289 const wxValidator
& validator
,
290 const wxString
& name
)
294 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
297 void wxSearchCtrl::Init()
300 m_searchButton
= NULL
;
301 m_cancelButton
= NULL
;
304 #endif // wxUSE_MENUS
306 m_searchButtonVisible
= true;
307 m_cancelButtonVisible
= false;
309 m_searchBitmapUser
= false;
310 m_cancelBitmapUser
= false;
312 m_searchMenuBitmapUser
= false;
313 #endif // wxUSE_MENUS
316 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
317 const wxString
& value
,
321 const wxValidator
& validator
,
322 const wxString
& name
)
324 // force border style for more native appearance
325 style
&= ~wxBORDER_MASK
;
327 style
|= wxBORDER_SUNKEN
;
328 #elif defined(__WXMSW__)
329 // Don't set the style explicitly, let GetDefaultBorder() work it out, unless
330 // we will get a sunken border (e.g. on Windows 200) in which case we must
331 // override with a simple border.
332 if (GetDefaultBorder() == wxBORDER_SUNKEN
)
333 style
|= wxBORDER_SIMPLE
;
335 style
|= wxBORDER_SIMPLE
;
337 if ( !wxTextCtrlBase::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
342 m_text
= new wxSearchTextCtrl(this, value
, style
& ~wxBORDER_MASK
);
343 m_text
->SetDescriptiveText(_("Search"));
345 wxSize sizeText
= m_text
->GetBestSize();
347 m_searchButton
= new wxSearchButton(this,
348 wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
,
350 m_cancelButton
= new wxSearchButton(this,
351 wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN
,
354 SetForegroundColour( m_text
->GetForegroundColour() );
355 m_searchButton
->SetForegroundColour( m_text
->GetForegroundColour() );
356 m_cancelButton
->SetForegroundColour( m_text
->GetForegroundColour() );
358 SetBackgroundColour( m_text
->GetBackgroundColour() );
359 m_searchButton
->SetBackgroundColour( m_text
->GetBackgroundColour() );
360 m_cancelButton
->SetBackgroundColour( m_text
->GetBackgroundColour() );
364 SetInitialSize(size
);
369 wxSearchCtrl::~wxSearchCtrl()
372 delete m_searchButton
;
373 delete m_cancelButton
;
376 #endif // wxUSE_MENUS
380 // search control specific interfaces
383 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
385 if ( menu
== m_menu
)
390 bool hadMenu
= (m_menu
!= NULL
);
394 if ( m_menu
&& !hadMenu
)
396 m_searchButton
->SetBitmapLabel(m_searchMenuBitmap
);
397 m_searchButton
->Refresh();
399 else if ( !m_menu
&& hadMenu
)
401 m_searchButton
->SetBitmapLabel(m_searchBitmap
);
402 if ( m_searchButtonVisible
)
404 m_searchButton
->Refresh();
407 wxRect rect
= GetRect();
408 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
411 wxMenu
* wxSearchCtrl::GetMenu()
416 #endif // wxUSE_MENUS
418 void wxSearchCtrl::ShowSearchButton( bool show
)
420 if ( m_searchButtonVisible
== show
)
425 m_searchButtonVisible
= show
;
426 if ( m_searchButtonVisible
)
431 wxRect rect
= GetRect();
432 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
435 bool wxSearchCtrl::IsSearchButtonVisible() const
437 return m_searchButtonVisible
;
441 void wxSearchCtrl::ShowCancelButton( bool show
)
443 if ( m_cancelButtonVisible
== show
)
448 m_cancelButtonVisible
= show
;
450 wxRect rect
= GetRect();
451 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
454 bool wxSearchCtrl::IsCancelButtonVisible() const
456 return m_cancelButtonVisible
;
459 void wxSearchCtrl::SetDescriptiveText(const wxString
& text
)
461 m_text
->SetDescriptiveText(text
);
464 wxString
wxSearchCtrl::GetDescriptiveText() const
466 return m_text
->GetDescriptiveText();
469 // ----------------------------------------------------------------------------
471 // ----------------------------------------------------------------------------
473 wxSize
wxSearchCtrl::DoGetBestSize() const
475 wxSize sizeText
= m_text
->GetBestSize();
476 wxSize
sizeSearch(0,0);
477 wxSize
sizeCancel(0,0);
478 int searchMargin
= 0;
479 int cancelMargin
= 0;
480 if ( m_searchButtonVisible
|| HasMenu() )
482 sizeSearch
= m_searchButton
->GetBestSize();
483 searchMargin
= MARGIN
;
485 if ( m_cancelButtonVisible
)
487 sizeCancel
= m_cancelButton
->GetBestSize();
488 cancelMargin
= MARGIN
;
491 int horizontalBorder
= 1 + ( sizeText
.y
- sizeText
.y
* 14 / 21 ) / 2;
493 // buttons are square and equal to the height of the text control
494 int height
= sizeText
.y
;
495 return wxSize(sizeSearch
.x
+ searchMargin
+ sizeText
.x
+ cancelMargin
+ sizeCancel
.x
+ 2*horizontalBorder
,
499 void wxSearchCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
501 wxSearchCtrlBase::DoMoveWindow(x
, y
, width
, height
);
503 LayoutControls(0, 0, width
, height
);
506 void wxSearchCtrl::LayoutControls(int x
, int y
, int width
, int height
)
511 wxSize sizeText
= m_text
->GetBestSize();
512 // make room for the search menu & clear button
513 int horizontalBorder
= ( sizeText
.y
- sizeText
.y
* 14 / 21 ) / 2;
514 x
+= horizontalBorder
;
516 width
-= horizontalBorder
*2;
519 wxSize
sizeSearch(0,0);
520 wxSize
sizeCancel(0,0);
521 int searchMargin
= 0;
522 int cancelMargin
= 0;
523 if ( m_searchButtonVisible
|| HasMenu() )
525 sizeSearch
= m_searchButton
->GetBestSize();
526 searchMargin
= MARGIN
;
528 if ( m_cancelButtonVisible
)
530 sizeCancel
= m_cancelButton
->GetBestSize();
531 cancelMargin
= MARGIN
;
533 m_searchButton
->Show( m_searchButtonVisible
|| HasMenu() );
534 m_cancelButton
->Show( m_cancelButtonVisible
);
536 if ( sizeSearch
.x
+ sizeCancel
.x
> width
)
538 sizeSearch
.x
= width
/2;
539 sizeCancel
.x
= width
/2;
543 wxCoord textWidth
= width
- sizeSearch
.x
- sizeCancel
.x
- searchMargin
- cancelMargin
- 1;
545 // position the subcontrols inside the client area
547 m_searchButton
->SetSize(x
, y
+ ICON_OFFSET
- 1, sizeSearch
.x
, height
);
548 m_text
->SetSize( x
+ sizeSearch
.x
+ searchMargin
,
549 y
+ ICON_OFFSET
- BORDER
,
552 m_cancelButton
->SetSize(x
+ sizeSearch
.x
+ searchMargin
+ textWidth
+ cancelMargin
,
553 y
+ ICON_OFFSET
- 1, sizeCancel
.x
, height
);
560 wxString
wxSearchCtrl::GetValue() const
562 wxString value
= m_text
->GetValue();
563 if (value
== m_text
->GetDescriptiveText())
564 return wxEmptyString
;
568 void wxSearchCtrl::SetValue(const wxString
& value
)
570 m_text
->SetValue(value
);
573 wxString
wxSearchCtrl::GetRange(long from
, long to
) const
575 return m_text
->GetRange(from
, to
);
578 int wxSearchCtrl::GetLineLength(long lineNo
) const
580 return m_text
->GetLineLength(lineNo
);
582 wxString
wxSearchCtrl::GetLineText(long lineNo
) const
584 return m_text
->GetLineText(lineNo
);
586 int wxSearchCtrl::GetNumberOfLines() const
588 return m_text
->GetNumberOfLines();
591 bool wxSearchCtrl::IsModified() const
593 return m_text
->IsModified();
595 bool wxSearchCtrl::IsEditable() const
597 return m_text
->IsEditable();
600 // more readable flag testing methods
601 bool wxSearchCtrl::IsSingleLine() const
603 return m_text
->IsSingleLine();
605 bool wxSearchCtrl::IsMultiLine() const
607 return m_text
->IsMultiLine();
610 // If the return values from and to are the same, there is no selection.
611 void wxSearchCtrl::GetSelection(long* from
, long* to
) const
613 m_text
->GetSelection(from
, to
);
616 wxString
wxSearchCtrl::GetStringSelection() const
618 return m_text
->GetStringSelection();
625 void wxSearchCtrl::Clear()
629 void wxSearchCtrl::Replace(long from
, long to
, const wxString
& value
)
631 m_text
->Replace(from
, to
, value
);
633 void wxSearchCtrl::Remove(long from
, long to
)
635 m_text
->Remove(from
, to
);
638 // load/save the controls contents from/to the file
639 bool wxSearchCtrl::LoadFile(const wxString
& file
)
641 return m_text
->LoadFile(file
);
643 bool wxSearchCtrl::SaveFile(const wxString
& file
)
645 return m_text
->SaveFile(file
);
648 // sets/clears the dirty flag
649 void wxSearchCtrl::MarkDirty()
653 void wxSearchCtrl::DiscardEdits()
655 m_text
->DiscardEdits();
658 // set the max number of characters which may be entered in a single line
660 void wxSearchCtrl::SetMaxLength(unsigned long len
)
662 m_text
->SetMaxLength(len
);
665 // writing text inserts it at the current position, appending always
666 // inserts it at the end
667 void wxSearchCtrl::WriteText(const wxString
& text
)
669 m_text
->WriteText(text
);
671 void wxSearchCtrl::AppendText(const wxString
& text
)
673 m_text
->AppendText(text
);
676 // insert the character which would have resulted from this key event,
677 // return true if anything has been inserted
678 bool wxSearchCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
680 return m_text
->EmulateKeyPress(event
);
683 // text control under some platforms supports the text styles: these
684 // methods allow to apply the given text style to the given selection or to
685 // set/get the style which will be used for all appended text
686 bool wxSearchCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
688 return m_text
->SetStyle(start
, end
, style
);
690 bool wxSearchCtrl::GetStyle(long position
, wxTextAttr
& style
)
692 return m_text
->GetStyle(position
, style
);
694 bool wxSearchCtrl::SetDefaultStyle(const wxTextAttr
& style
)
696 return m_text
->SetDefaultStyle(style
);
698 const wxTextAttr
& wxSearchCtrl::GetDefaultStyle() const
700 return m_text
->GetDefaultStyle();
703 // translate between the position (which is just an index in the text ctrl
704 // considering all its contents as a single strings) and (x, y) coordinates
705 // which represent column and line.
706 long wxSearchCtrl::XYToPosition(long x
, long y
) const
708 return m_text
->XYToPosition(x
, y
);
710 bool wxSearchCtrl::PositionToXY(long pos
, long *x
, long *y
) const
712 return m_text
->PositionToXY(pos
, x
, y
);
715 void wxSearchCtrl::ShowPosition(long pos
)
717 m_text
->ShowPosition(pos
);
720 // find the character at position given in pixels
722 // NB: pt is in device coords (not adjusted for the client area origin nor
724 wxTextCtrlHitTestResult
wxSearchCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
726 return m_text
->HitTest(pt
, pos
);
728 wxTextCtrlHitTestResult
wxSearchCtrl::HitTest(const wxPoint
& pt
,
730 wxTextCoord
*row
) const
732 return m_text
->HitTest(pt
, col
, row
);
735 // Clipboard operations
736 void wxSearchCtrl::Copy()
740 void wxSearchCtrl::Cut()
744 void wxSearchCtrl::Paste()
749 bool wxSearchCtrl::CanCopy() const
751 return m_text
->CanCopy();
753 bool wxSearchCtrl::CanCut() const
755 return m_text
->CanCut();
757 bool wxSearchCtrl::CanPaste() const
759 return m_text
->CanPaste();
763 void wxSearchCtrl::Undo()
767 void wxSearchCtrl::Redo()
772 bool wxSearchCtrl::CanUndo() const
774 return m_text
->CanUndo();
776 bool wxSearchCtrl::CanRedo() const
778 return m_text
->CanRedo();
782 void wxSearchCtrl::SetInsertionPoint(long pos
)
784 m_text
->SetInsertionPoint(pos
);
786 void wxSearchCtrl::SetInsertionPointEnd()
788 m_text
->SetInsertionPointEnd();
790 long wxSearchCtrl::GetInsertionPoint() const
792 return m_text
->GetInsertionPoint();
794 wxTextPos
wxSearchCtrl::GetLastPosition() const
796 return m_text
->GetLastPosition();
799 void wxSearchCtrl::SetSelection(long from
, long to
)
801 m_text
->SetSelection(from
, to
);
803 void wxSearchCtrl::SelectAll()
808 void wxSearchCtrl::SetEditable(bool editable
)
810 m_text
->SetEditable(editable
);
813 bool wxSearchCtrl::SetFont(const wxFont
& font
)
815 bool result
= wxSearchCtrlBase::SetFont(font
);
816 if ( result
&& m_text
)
818 result
= m_text
->SetFont(font
);
824 // search control generic only
825 void wxSearchCtrl::SetSearchBitmap( const wxBitmap
& bitmap
)
827 m_searchBitmap
= bitmap
;
828 m_searchBitmapUser
= bitmap
.Ok();
829 if ( m_searchBitmapUser
)
831 if ( m_searchButton
&& !HasMenu() )
833 m_searchButton
->SetBitmapLabel( m_searchBitmap
);
838 // the user bitmap was just cleared, generate one
845 void wxSearchCtrl::SetSearchMenuBitmap( const wxBitmap
& bitmap
)
847 m_searchMenuBitmap
= bitmap
;
848 m_searchMenuBitmapUser
= bitmap
.Ok();
849 if ( m_searchMenuBitmapUser
)
851 if ( m_searchButton
&& m_menu
)
853 m_searchButton
->SetBitmapLabel( m_searchMenuBitmap
);
858 // the user bitmap was just cleared, generate one
863 #endif // wxUSE_MENUS
865 void wxSearchCtrl::SetCancelBitmap( const wxBitmap
& bitmap
)
867 m_cancelBitmap
= bitmap
;
868 m_cancelBitmapUser
= bitmap
.Ok();
869 if ( m_cancelBitmapUser
)
871 if ( m_cancelButton
)
873 m_cancelButton
->SetBitmapLabel( m_cancelBitmap
);
878 // the user bitmap was just cleared, generate one
885 // override streambuf method
886 #if wxHAS_TEXT_WINDOW_STREAM
888 #endif // wxHAS_TEXT_WINDOW_STREAM
890 // stream-like insertion operators: these are always available, whether we
891 // were, or not, compiled with streambuf support
892 wxTextCtrl
& operator<<(const wxString
& s
);
893 wxTextCtrl
& operator<<(int i
);
894 wxTextCtrl
& operator<<(long i
);
895 wxTextCtrl
& operator<<(float f
);
896 wxTextCtrl
& operator<<(double d
);
897 wxTextCtrl
& operator<<(const wxChar c
);
900 void wxSearchCtrl::DoSetValue(const wxString
& value
, int flags
)
902 m_text
->ChangeValue( value
);
903 if ( flags
& SetValue_SendEvent
)
904 SendTextUpdatedEvent();
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
= wxStepColour(GetForegroundColour(), 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
= wxStepColour(GetForegroundColour(), 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
.Ok() ||
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
.Ok() ||
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
.Ok() ||
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::OnSearchButton( wxCommandEvent
& event
)
1193 void wxSearchCtrl::OnSetFocus( wxFocusEvent
& /*event*/ )
1201 void wxSearchCtrl::OnSize( wxSizeEvent
& WXUNUSED(event
) )
1204 GetSize(&width
, &height
);
1205 LayoutControls(0, 0, width
, height
);
1210 void wxSearchCtrl::PopupSearchMenu()
1214 wxSize size
= GetSize();
1215 PopupMenu( m_menu
, 0, size
.y
);
1219 #endif // wxUSE_MENUS
1221 #endif // !wxUSE_NATIVE_SEARCH_CONTROL
1223 #endif // wxUSE_SEARCHCTRL