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 m_searchButton
= new wxSearchButton(this,
346 wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
,
348 m_cancelButton
= new wxSearchButton(this,
349 wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN
,
352 SetForegroundColour( m_text
->GetForegroundColour() );
353 m_searchButton
->SetForegroundColour( m_text
->GetForegroundColour() );
354 m_cancelButton
->SetForegroundColour( m_text
->GetForegroundColour() );
356 SetBackgroundColour( m_text
->GetBackgroundColour() );
357 m_searchButton
->SetBackgroundColour( m_text
->GetBackgroundColour() );
358 m_cancelButton
->SetBackgroundColour( m_text
->GetBackgroundColour() );
362 SetInitialSize(size
);
367 wxSearchCtrl::~wxSearchCtrl()
370 delete m_searchButton
;
371 delete m_cancelButton
;
374 #endif // wxUSE_MENUS
378 // search control specific interfaces
381 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
383 if ( menu
== m_menu
)
388 bool hadMenu
= (m_menu
!= NULL
);
392 if ( m_menu
&& !hadMenu
)
394 m_searchButton
->SetBitmapLabel(m_searchMenuBitmap
);
395 m_searchButton
->Refresh();
397 else if ( !m_menu
&& hadMenu
)
399 m_searchButton
->SetBitmapLabel(m_searchBitmap
);
400 if ( m_searchButtonVisible
)
402 m_searchButton
->Refresh();
405 wxRect rect
= GetRect();
406 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
409 wxMenu
* wxSearchCtrl::GetMenu()
414 #endif // wxUSE_MENUS
416 void wxSearchCtrl::ShowSearchButton( bool show
)
418 if ( m_searchButtonVisible
== show
)
423 m_searchButtonVisible
= show
;
424 if ( m_searchButtonVisible
)
429 wxRect rect
= GetRect();
430 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
433 bool wxSearchCtrl::IsSearchButtonVisible() const
435 return m_searchButtonVisible
;
439 void wxSearchCtrl::ShowCancelButton( bool show
)
441 if ( m_cancelButtonVisible
== show
)
446 m_cancelButtonVisible
= show
;
448 wxRect rect
= GetRect();
449 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
452 bool wxSearchCtrl::IsCancelButtonVisible() const
454 return m_cancelButtonVisible
;
457 void wxSearchCtrl::SetDescriptiveText(const wxString
& text
)
459 m_text
->SetDescriptiveText(text
);
462 wxString
wxSearchCtrl::GetDescriptiveText() const
464 return m_text
->GetDescriptiveText();
467 // ----------------------------------------------------------------------------
469 // ----------------------------------------------------------------------------
471 wxSize
wxSearchCtrl::DoGetBestSize() const
473 wxSize sizeText
= m_text
->GetBestSize();
474 wxSize
sizeSearch(0,0);
475 wxSize
sizeCancel(0,0);
476 int searchMargin
= 0;
477 int cancelMargin
= 0;
478 if ( m_searchButtonVisible
|| HasMenu() )
480 sizeSearch
= m_searchButton
->GetBestSize();
481 searchMargin
= MARGIN
;
483 if ( m_cancelButtonVisible
)
485 sizeCancel
= m_cancelButton
->GetBestSize();
486 cancelMargin
= MARGIN
;
489 int horizontalBorder
= 1 + ( sizeText
.y
- sizeText
.y
* 14 / 21 ) / 2;
491 // buttons are square and equal to the height of the text control
492 int height
= sizeText
.y
;
493 return wxSize(sizeSearch
.x
+ searchMargin
+ sizeText
.x
+ cancelMargin
+ sizeCancel
.x
+ 2*horizontalBorder
,
497 void wxSearchCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
499 wxSearchCtrlBase::DoMoveWindow(x
, y
, width
, height
);
501 LayoutControls(0, 0, width
, height
);
504 void wxSearchCtrl::LayoutControls(int x
, int y
, int width
, int height
)
509 wxSize sizeText
= m_text
->GetBestSize();
510 // make room for the search menu & clear button
511 int horizontalBorder
= ( sizeText
.y
- sizeText
.y
* 14 / 21 ) / 2;
512 x
+= horizontalBorder
;
514 width
-= horizontalBorder
*2;
517 wxSize
sizeSearch(0,0);
518 wxSize
sizeCancel(0,0);
519 int searchMargin
= 0;
520 int cancelMargin
= 0;
521 if ( m_searchButtonVisible
|| HasMenu() )
523 sizeSearch
= m_searchButton
->GetBestSize();
524 searchMargin
= MARGIN
;
526 if ( m_cancelButtonVisible
)
528 sizeCancel
= m_cancelButton
->GetBestSize();
529 cancelMargin
= MARGIN
;
531 m_searchButton
->Show( m_searchButtonVisible
|| HasMenu() );
532 m_cancelButton
->Show( m_cancelButtonVisible
);
534 if ( sizeSearch
.x
+ sizeCancel
.x
> width
)
536 sizeSearch
.x
= width
/2;
537 sizeCancel
.x
= width
/2;
541 wxCoord textWidth
= width
- sizeSearch
.x
- sizeCancel
.x
- searchMargin
- cancelMargin
- 1;
543 // position the subcontrols inside the client area
545 m_searchButton
->SetSize(x
, y
+ ICON_OFFSET
- 1, sizeSearch
.x
, height
);
546 m_text
->SetSize( x
+ sizeSearch
.x
+ searchMargin
,
547 y
+ ICON_OFFSET
- BORDER
,
550 m_cancelButton
->SetSize(x
+ sizeSearch
.x
+ searchMargin
+ textWidth
+ cancelMargin
,
551 y
+ ICON_OFFSET
- 1, sizeCancel
.x
, height
);
558 wxString
wxSearchCtrl::GetValue() const
560 wxString value
= m_text
->GetValue();
561 if (value
== m_text
->GetDescriptiveText())
562 return wxEmptyString
;
566 void wxSearchCtrl::SetValue(const wxString
& value
)
568 m_text
->SetValue(value
);
571 wxString
wxSearchCtrl::GetRange(long from
, long to
) const
573 return m_text
->GetRange(from
, to
);
576 int wxSearchCtrl::GetLineLength(long lineNo
) const
578 return m_text
->GetLineLength(lineNo
);
580 wxString
wxSearchCtrl::GetLineText(long lineNo
) const
582 return m_text
->GetLineText(lineNo
);
584 int wxSearchCtrl::GetNumberOfLines() const
586 return m_text
->GetNumberOfLines();
589 bool wxSearchCtrl::IsModified() const
591 return m_text
->IsModified();
593 bool wxSearchCtrl::IsEditable() const
595 return m_text
->IsEditable();
598 // more readable flag testing methods
599 bool wxSearchCtrl::IsSingleLine() const
601 return m_text
->IsSingleLine();
603 bool wxSearchCtrl::IsMultiLine() const
605 return m_text
->IsMultiLine();
608 // If the return values from and to are the same, there is no selection.
609 void wxSearchCtrl::GetSelection(long* from
, long* to
) const
611 m_text
->GetSelection(from
, to
);
614 wxString
wxSearchCtrl::GetStringSelection() const
616 return m_text
->GetStringSelection();
623 void wxSearchCtrl::Clear()
627 void wxSearchCtrl::Replace(long from
, long to
, const wxString
& value
)
629 m_text
->Replace(from
, to
, value
);
631 void wxSearchCtrl::Remove(long from
, long to
)
633 m_text
->Remove(from
, to
);
636 // load/save the controls contents from/to the file
637 bool wxSearchCtrl::LoadFile(const wxString
& file
)
639 return m_text
->LoadFile(file
);
641 bool wxSearchCtrl::SaveFile(const wxString
& file
)
643 return m_text
->SaveFile(file
);
646 // sets/clears the dirty flag
647 void wxSearchCtrl::MarkDirty()
651 void wxSearchCtrl::DiscardEdits()
653 m_text
->DiscardEdits();
656 // set the max number of characters which may be entered in a single line
658 void wxSearchCtrl::SetMaxLength(unsigned long len
)
660 m_text
->SetMaxLength(len
);
663 // writing text inserts it at the current position, appending always
664 // inserts it at the end
665 void wxSearchCtrl::WriteText(const wxString
& text
)
667 m_text
->WriteText(text
);
669 void wxSearchCtrl::AppendText(const wxString
& text
)
671 m_text
->AppendText(text
);
674 // insert the character which would have resulted from this key event,
675 // return true if anything has been inserted
676 bool wxSearchCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
678 return m_text
->EmulateKeyPress(event
);
681 // text control under some platforms supports the text styles: these
682 // methods allow to apply the given text style to the given selection or to
683 // set/get the style which will be used for all appended text
684 bool wxSearchCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
686 return m_text
->SetStyle(start
, end
, style
);
688 bool wxSearchCtrl::GetStyle(long position
, wxTextAttr
& style
)
690 return m_text
->GetStyle(position
, style
);
692 bool wxSearchCtrl::SetDefaultStyle(const wxTextAttr
& style
)
694 return m_text
->SetDefaultStyle(style
);
696 const wxTextAttr
& wxSearchCtrl::GetDefaultStyle() const
698 return m_text
->GetDefaultStyle();
701 // translate between the position (which is just an index in the text ctrl
702 // considering all its contents as a single strings) and (x, y) coordinates
703 // which represent column and line.
704 long wxSearchCtrl::XYToPosition(long x
, long y
) const
706 return m_text
->XYToPosition(x
, y
);
708 bool wxSearchCtrl::PositionToXY(long pos
, long *x
, long *y
) const
710 return m_text
->PositionToXY(pos
, x
, y
);
713 void wxSearchCtrl::ShowPosition(long pos
)
715 m_text
->ShowPosition(pos
);
718 // find the character at position given in pixels
720 // NB: pt is in device coords (not adjusted for the client area origin nor
722 wxTextCtrlHitTestResult
wxSearchCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
724 return m_text
->HitTest(pt
, pos
);
726 wxTextCtrlHitTestResult
wxSearchCtrl::HitTest(const wxPoint
& pt
,
728 wxTextCoord
*row
) const
730 return m_text
->HitTest(pt
, col
, row
);
733 // Clipboard operations
734 void wxSearchCtrl::Copy()
738 void wxSearchCtrl::Cut()
742 void wxSearchCtrl::Paste()
747 bool wxSearchCtrl::CanCopy() const
749 return m_text
->CanCopy();
751 bool wxSearchCtrl::CanCut() const
753 return m_text
->CanCut();
755 bool wxSearchCtrl::CanPaste() const
757 return m_text
->CanPaste();
761 void wxSearchCtrl::Undo()
765 void wxSearchCtrl::Redo()
770 bool wxSearchCtrl::CanUndo() const
772 return m_text
->CanUndo();
774 bool wxSearchCtrl::CanRedo() const
776 return m_text
->CanRedo();
780 void wxSearchCtrl::SetInsertionPoint(long pos
)
782 m_text
->SetInsertionPoint(pos
);
784 void wxSearchCtrl::SetInsertionPointEnd()
786 m_text
->SetInsertionPointEnd();
788 long wxSearchCtrl::GetInsertionPoint() const
790 return m_text
->GetInsertionPoint();
792 wxTextPos
wxSearchCtrl::GetLastPosition() const
794 return m_text
->GetLastPosition();
797 void wxSearchCtrl::SetSelection(long from
, long to
)
799 m_text
->SetSelection(from
, to
);
801 void wxSearchCtrl::SelectAll()
806 void wxSearchCtrl::SetEditable(bool editable
)
808 m_text
->SetEditable(editable
);
811 bool wxSearchCtrl::SetFont(const wxFont
& font
)
813 bool result
= wxSearchCtrlBase::SetFont(font
);
814 if ( result
&& m_text
)
816 result
= m_text
->SetFont(font
);
822 // search control generic only
823 void wxSearchCtrl::SetSearchBitmap( const wxBitmap
& bitmap
)
825 m_searchBitmap
= bitmap
;
826 m_searchBitmapUser
= bitmap
.Ok();
827 if ( m_searchBitmapUser
)
829 if ( m_searchButton
&& !HasMenu() )
831 m_searchButton
->SetBitmapLabel( m_searchBitmap
);
836 // the user bitmap was just cleared, generate one
843 void wxSearchCtrl::SetSearchMenuBitmap( const wxBitmap
& bitmap
)
845 m_searchMenuBitmap
= bitmap
;
846 m_searchMenuBitmapUser
= bitmap
.Ok();
847 if ( m_searchMenuBitmapUser
)
849 if ( m_searchButton
&& m_menu
)
851 m_searchButton
->SetBitmapLabel( m_searchMenuBitmap
);
856 // the user bitmap was just cleared, generate one
861 #endif // wxUSE_MENUS
863 void wxSearchCtrl::SetCancelBitmap( const wxBitmap
& bitmap
)
865 m_cancelBitmap
= bitmap
;
866 m_cancelBitmapUser
= bitmap
.Ok();
867 if ( m_cancelBitmapUser
)
869 if ( m_cancelButton
)
871 m_cancelButton
->SetBitmapLabel( m_cancelBitmap
);
876 // the user bitmap was just cleared, generate one
883 // override streambuf method
884 #if wxHAS_TEXT_WINDOW_STREAM
886 #endif // wxHAS_TEXT_WINDOW_STREAM
888 // stream-like insertion operators: these are always available, whether we
889 // were, or not, compiled with streambuf support
890 wxTextCtrl
& operator<<(const wxString
& s
);
891 wxTextCtrl
& operator<<(int i
);
892 wxTextCtrl
& operator<<(long i
);
893 wxTextCtrl
& operator<<(float f
);
894 wxTextCtrl
& operator<<(double d
);
895 wxTextCtrl
& operator<<(const wxChar c
);
898 void wxSearchCtrl::DoSetValue(const wxString
& value
, int flags
)
900 m_text
->ChangeValue( value
);
901 if ( flags
& SetValue_SendEvent
)
902 SendTextUpdatedEvent();
905 // do the window-specific processing after processing the update event
906 void wxSearchCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
908 wxSearchCtrlBase::DoUpdateWindowUI(event
);
911 bool wxSearchCtrl::ShouldInheritColours() const
916 // icons are rendered at 3-8 times larger than necessary and downscaled for
918 static int GetMultiplier()
921 // speed up bitmap generation by using a small bitmap
924 int depth
= ::wxDisplayDepth();
934 wxBitmap
wxSearchCtrl::RenderSearchBitmap( int x
, int y
, bool renderDrop
)
936 wxColour bg
= GetBackgroundColour();
937 wxColour fg
= wxStepColour(GetForegroundColour(), LIGHT_STEP
-20);
939 //===============================================================================
940 // begin drawing code
941 //===============================================================================
944 // force width:height ratio
956 // glass 11x11, top left corner
957 // handle (9,9)-(13,13)
958 // drop (13,16)-(19,6)-(16,9)
960 int multiplier
= GetMultiplier();
961 int penWidth
= multiplier
* 2;
963 penWidth
= penWidth
* x
/ 20;
965 wxBitmap
bitmap( multiplier
*x
, multiplier
*y
);
967 mem
.SelectObject(bitmap
);
970 mem
.SetBrush( wxBrush(bg
) );
971 mem
.SetPen( wxPen(bg
) );
972 mem
.DrawRectangle(0,0,bitmap
.GetWidth(),bitmap
.GetHeight());
975 mem
.SetBrush( wxBrush(fg
) );
976 mem
.SetPen( wxPen(fg
) );
977 int glassBase
= 5 * x
/ 20;
978 int glassFactor
= 2*glassBase
+ 1;
979 int radius
= multiplier
*glassFactor
/2;
980 mem
.DrawCircle(radius
,radius
,radius
);
981 mem
.SetBrush( wxBrush(bg
) );
982 mem
.SetPen( wxPen(bg
) );
983 mem
.DrawCircle(radius
,radius
,radius
-penWidth
);
986 int lineStart
= radius
+ (radius
-penWidth
/2) * 707 / 1000; // 707 / 1000 = 0.707 = 1/sqrt(2);
988 mem
.SetPen( wxPen(fg
) );
989 mem
.SetBrush( wxBrush(fg
) );
990 int handleCornerShift
= penWidth
* 707 / 1000 / 2; // 707 / 1000 = 0.707 = 1/sqrt(2);
991 handleCornerShift
= WXMAX( handleCornerShift
, 1 );
992 int handleBase
= 4 * x
/ 20;
993 int handleLength
= 2*handleBase
+1;
994 wxPoint handlePolygon
[] =
996 wxPoint(-handleCornerShift
,+handleCornerShift
),
997 wxPoint(+handleCornerShift
,-handleCornerShift
),
998 wxPoint(multiplier
*handleLength
/2+handleCornerShift
,multiplier
*handleLength
/2-handleCornerShift
),
999 wxPoint(multiplier
*handleLength
/2-handleCornerShift
,multiplier
*handleLength
/2+handleCornerShift
),
1001 mem
.DrawPolygon(WXSIZEOF(handlePolygon
),handlePolygon
,lineStart
,lineStart
);
1003 // draw drop triangle
1004 int triangleX
= 13 * x
/ 20;
1005 int triangleY
= 5 * x
/ 20;
1006 int triangleBase
= 3 * x
/ 20;
1007 int triangleFactor
= triangleBase
*2+1;
1010 wxPoint dropPolygon
[] =
1012 wxPoint(multiplier
*0,multiplier
*0), // triangle left
1013 wxPoint(multiplier
*triangleFactor
-1,multiplier
*0), // triangle right
1014 wxPoint(multiplier
*triangleFactor
/2,multiplier
*triangleFactor
/2), // triangle bottom
1016 mem
.DrawPolygon(WXSIZEOF(dropPolygon
),dropPolygon
,multiplier
*triangleX
,multiplier
*triangleY
);
1018 mem
.SelectObject(wxNullBitmap
);
1020 //===============================================================================
1022 //===============================================================================
1024 if ( multiplier
!= 1 )
1026 wxImage image
= bitmap
.ConvertToImage();
1028 bitmap
= wxBitmap( image
);
1032 // Trim the edge where the arrow would have gone
1033 bitmap
= bitmap
.GetSubBitmap(wxRect(0,0, y
,y
));
1039 wxBitmap
wxSearchCtrl::RenderCancelBitmap( int x
, int y
)
1041 wxColour bg
= GetBackgroundColour();
1042 wxColour fg
= wxStepColour(GetForegroundColour(), LIGHT_STEP
);
1044 //===============================================================================
1045 // begin drawing code
1046 //===============================================================================
1063 // cross line starts (4,4)-(10,10)
1064 // drop (13,16)-(19,6)-(16,9)
1066 int multiplier
= GetMultiplier();
1068 int penWidth
= multiplier
* x
/ 14;
1070 wxBitmap
bitmap( multiplier
*x
, multiplier
*y
);
1072 mem
.SelectObject(bitmap
);
1075 mem
.SetBrush( wxBrush(bg
) );
1076 mem
.SetPen( wxPen(bg
) );
1077 mem
.DrawRectangle(0,0,bitmap
.GetWidth(),bitmap
.GetHeight());
1080 mem
.SetBrush( wxBrush(fg
) );
1081 mem
.SetPen( wxPen(fg
) );
1082 int radius
= multiplier
*x
/2;
1083 mem
.DrawCircle(radius
,radius
,radius
);
1086 int lineStartBase
= 4 * x
/ 14;
1087 int lineLength
= x
- 2*lineStartBase
;
1089 mem
.SetPen( wxPen(bg
) );
1090 mem
.SetBrush( wxBrush(bg
) );
1091 int handleCornerShift
= penWidth
/2;
1092 handleCornerShift
= WXMAX( handleCornerShift
, 1 );
1093 wxPoint handlePolygon
[] =
1095 wxPoint(-handleCornerShift
,+handleCornerShift
),
1096 wxPoint(+handleCornerShift
,-handleCornerShift
),
1097 wxPoint(multiplier
*lineLength
+handleCornerShift
,multiplier
*lineLength
-handleCornerShift
),
1098 wxPoint(multiplier
*lineLength
-handleCornerShift
,multiplier
*lineLength
+handleCornerShift
),
1100 mem
.DrawPolygon(WXSIZEOF(handlePolygon
),handlePolygon
,multiplier
*lineStartBase
,multiplier
*lineStartBase
);
1101 wxPoint handlePolygon2
[] =
1103 wxPoint(+handleCornerShift
,+handleCornerShift
),
1104 wxPoint(-handleCornerShift
,-handleCornerShift
),
1105 wxPoint(multiplier
*lineLength
-handleCornerShift
,-multiplier
*lineLength
-handleCornerShift
),
1106 wxPoint(multiplier
*lineLength
+handleCornerShift
,-multiplier
*lineLength
+handleCornerShift
),
1108 mem
.DrawPolygon(WXSIZEOF(handlePolygon2
),handlePolygon2
,multiplier
*lineStartBase
,multiplier
*(x
-lineStartBase
));
1110 //===============================================================================
1112 //===============================================================================
1114 if ( multiplier
!= 1 )
1116 wxImage image
= bitmap
.ConvertToImage();
1118 bitmap
= wxBitmap( image
);
1124 void wxSearchCtrl::RecalcBitmaps()
1130 wxSize sizeText
= m_text
->GetBestSize();
1132 int bitmapHeight
= sizeText
.y
- 2 * ICON_MARGIN
;
1133 int bitmapWidth
= sizeText
.y
* 20 / 14;
1135 if ( !m_searchBitmapUser
)
1138 !m_searchBitmap
.Ok() ||
1139 m_searchBitmap
.GetHeight() != bitmapHeight
||
1140 m_searchBitmap
.GetWidth() != bitmapWidth
1143 m_searchBitmap
= RenderSearchBitmap(bitmapWidth
,bitmapHeight
,false);
1146 m_searchButton
->SetBitmapLabel(m_searchBitmap
);
1149 // else this bitmap was set by user, don't alter
1153 if ( !m_searchMenuBitmapUser
)
1156 !m_searchMenuBitmap
.Ok() ||
1157 m_searchMenuBitmap
.GetHeight() != bitmapHeight
||
1158 m_searchMenuBitmap
.GetWidth() != bitmapWidth
1161 m_searchMenuBitmap
= RenderSearchBitmap(bitmapWidth
,bitmapHeight
,true);
1164 m_searchButton
->SetBitmapLabel(m_searchMenuBitmap
);
1167 // else this bitmap was set by user, don't alter
1169 #endif // wxUSE_MENUS
1171 if ( !m_cancelBitmapUser
)
1174 !m_cancelBitmap
.Ok() ||
1175 m_cancelBitmap
.GetHeight() != bitmapHeight
||
1176 m_cancelBitmap
.GetWidth() != bitmapHeight
1179 m_cancelBitmap
= RenderCancelBitmap(bitmapHeight
-BORDER
-1,bitmapHeight
-BORDER
-1); // square
1180 m_cancelButton
->SetBitmapLabel(m_cancelBitmap
);
1182 // else this bitmap was set by user, don't alter
1186 void wxSearchCtrl::OnSearchButton( wxCommandEvent
& event
)
1191 void wxSearchCtrl::OnSetFocus( wxFocusEvent
& /*event*/ )
1199 void wxSearchCtrl::OnSize( wxSizeEvent
& WXUNUSED(event
) )
1202 GetSize(&width
, &height
);
1203 LayoutControls(0, 0, width
, height
);
1208 void wxSearchCtrl::PopupSearchMenu()
1212 wxSize size
= GetSize();
1213 PopupMenu( m_menu
, 0, size
.y
);
1217 #endif // wxUSE_MENUS
1219 #endif // !wxUSE_NATIVE_SEARCH_CONTROL
1221 #endif // wxUSE_SEARCHCTRL