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 // provide access to the base class protected methods to wxSearchCtrl which
139 // needs to forward to them
140 void DoSetValue(const wxString
& value
, int flags
)
142 wxTextCtrl::DoSetValue(value
, flags
);
145 bool DoLoadFile(const wxString
& file
, int fileType
)
147 return wxTextCtrl::DoLoadFile(file
, fileType
);
150 bool DoSaveFile(const wxString
& file
, int fileType
)
152 return wxTextCtrl::DoSaveFile(file
, fileType
);
156 void OnText(wxCommandEvent
& eventText
)
158 wxCommandEvent
event(eventText
);
159 event
.SetEventObject(m_search
);
160 event
.SetId(m_search
->GetId());
162 m_search
->GetEventHandler()->ProcessEvent(event
);
165 void OnTextUrl(wxTextUrlEvent
& eventText
)
167 // copy constructor is disabled for some reason?
168 //wxTextUrlEvent event(eventText);
169 wxTextUrlEvent
event(
171 eventText
.GetMouseEvent(),
172 eventText
.GetURLStart(),
173 eventText
.GetURLEnd()
175 event
.SetEventObject(m_search
);
177 m_search
->GetEventHandler()->ProcessEvent(event
);
180 void OnIdle(wxIdleEvent
& WXUNUSED(event
))
182 if ( IsEmpty() && !(wxWindow::FindFocus() == this) )
184 ChangeValue(m_descriptiveText
);
185 SetInsertionPoint(0);
186 SetForegroundColour(wxStepColour(m_defaultFG
, LIGHT_STEP
));
190 void OnFocus(wxFocusEvent
& event
)
193 if ( GetValue() == m_descriptiveText
)
195 ChangeValue(wxEmptyString
);
196 SetForegroundColour(m_defaultFG
);
201 wxSearchCtrl
* m_search
;
202 wxString m_descriptiveText
;
203 wxColour m_defaultFG
;
205 DECLARE_EVENT_TABLE()
208 BEGIN_EVENT_TABLE(wxSearchTextCtrl
, wxTextCtrl
)
209 EVT_TEXT(wxID_ANY
, wxSearchTextCtrl::OnText
)
210 EVT_TEXT_ENTER(wxID_ANY
, wxSearchTextCtrl::OnText
)
211 EVT_TEXT_URL(wxID_ANY
, wxSearchTextCtrl::OnTextUrl
)
212 EVT_TEXT_MAXLEN(wxID_ANY
, wxSearchTextCtrl::OnText
)
213 EVT_IDLE(wxSearchTextCtrl::OnIdle
)
214 EVT_SET_FOCUS(wxSearchTextCtrl::OnFocus
)
217 // ----------------------------------------------------------------------------
218 // wxSearchButton: search button used by search control
219 // ----------------------------------------------------------------------------
221 class wxSearchButton
: public wxControl
224 wxSearchButton(wxSearchCtrl
*search
, int eventType
, const wxBitmap
& bmp
)
225 : wxControl(search
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxNO_BORDER
),
227 m_eventType(eventType
),
231 void SetBitmapLabel(const wxBitmap
& label
) { m_bmp
= label
; }
235 wxSize
DoGetBestSize() const
237 return wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight());
240 void OnLeftUp(wxMouseEvent
&)
242 wxCommandEvent
event(m_eventType
, m_search
->GetId());
243 event
.SetEventObject(m_search
);
245 GetEventHandler()->ProcessEvent(event
);
247 m_search
->SetFocus();
250 if ( m_eventType
== wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
)
252 // this happens automatically, just like on Mac OS X
253 m_search
->PopupSearchMenu();
255 #endif // wxUSE_MENUS
258 void OnPaint(wxPaintEvent
&)
261 dc
.DrawBitmap(m_bmp
, 0,0, true);
266 wxSearchCtrl
*m_search
;
267 wxEventType m_eventType
;
270 DECLARE_EVENT_TABLE()
273 BEGIN_EVENT_TABLE(wxSearchButton
, wxControl
)
274 EVT_LEFT_UP(wxSearchButton::OnLeftUp
)
275 EVT_PAINT(wxSearchButton::OnPaint
)
278 BEGIN_EVENT_TABLE(wxSearchCtrl
, wxSearchCtrlBase
)
279 EVT_SEARCHCTRL_SEARCH_BTN(wxID_ANY
, wxSearchCtrl::OnSearchButton
)
280 EVT_SET_FOCUS(wxSearchCtrl::OnSetFocus
)
281 EVT_SIZE(wxSearchCtrl::OnSize
)
284 IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl
, wxSearchCtrlBase
)
286 // ============================================================================
288 // ============================================================================
290 // ----------------------------------------------------------------------------
291 // wxSearchCtrl creation
292 // ----------------------------------------------------------------------------
297 wxSearchCtrl::wxSearchCtrl()
302 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
303 const wxString
& value
,
307 const wxValidator
& validator
,
308 const wxString
& name
)
312 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
315 void wxSearchCtrl::Init()
318 m_searchButton
= NULL
;
319 m_cancelButton
= NULL
;
322 #endif // wxUSE_MENUS
324 m_searchButtonVisible
= true;
325 m_cancelButtonVisible
= false;
327 m_searchBitmapUser
= false;
328 m_cancelBitmapUser
= false;
330 m_searchMenuBitmapUser
= false;
331 #endif // wxUSE_MENUS
334 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
335 const wxString
& value
,
339 const wxValidator
& validator
,
340 const wxString
& name
)
342 // force border style for more native appearance
343 style
&= ~wxBORDER_MASK
;
345 style
|= wxBORDER_SUNKEN
;
346 #elif defined(__WXMSW__)
347 // Don't set the style explicitly, let GetDefaultBorder() work it out, unless
348 // we will get a sunken border (e.g. on Windows 200) in which case we must
349 // override with a simple border.
350 if (GetDefaultBorder() == wxBORDER_SUNKEN
)
351 style
|= wxBORDER_SIMPLE
;
353 style
|= wxBORDER_SIMPLE
;
355 if ( !wxSearchCtrlBaseBaseClass::Create(parent
, id
, pos
, size
,
356 style
, validator
, name
) )
361 m_text
= new wxSearchTextCtrl(this, value
, style
& ~wxBORDER_MASK
);
362 m_text
->SetDescriptiveText(_("Search"));
364 m_searchButton
= new wxSearchButton(this,
365 wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
,
367 m_cancelButton
= new wxSearchButton(this,
368 wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN
,
371 SetForegroundColour( m_text
->GetForegroundColour() );
372 m_searchButton
->SetForegroundColour( m_text
->GetForegroundColour() );
373 m_cancelButton
->SetForegroundColour( m_text
->GetForegroundColour() );
375 SetBackgroundColour( m_text
->GetBackgroundColour() );
376 m_searchButton
->SetBackgroundColour( m_text
->GetBackgroundColour() );
377 m_cancelButton
->SetBackgroundColour( m_text
->GetBackgroundColour() );
381 SetInitialSize(size
);
386 wxSearchCtrl::~wxSearchCtrl()
389 delete m_searchButton
;
390 delete m_cancelButton
;
393 #endif // wxUSE_MENUS
397 // search control specific interfaces
400 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
402 if ( menu
== m_menu
)
407 bool hadMenu
= (m_menu
!= NULL
);
411 if ( m_menu
&& !hadMenu
)
413 m_searchButton
->SetBitmapLabel(m_searchMenuBitmap
);
414 m_searchButton
->Refresh();
416 else if ( !m_menu
&& hadMenu
)
418 m_searchButton
->SetBitmapLabel(m_searchBitmap
);
419 if ( m_searchButtonVisible
)
421 m_searchButton
->Refresh();
424 wxRect rect
= GetRect();
425 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
428 wxMenu
* wxSearchCtrl::GetMenu()
433 #endif // wxUSE_MENUS
435 void wxSearchCtrl::ShowSearchButton( bool show
)
437 if ( m_searchButtonVisible
== show
)
442 m_searchButtonVisible
= show
;
443 if ( m_searchButtonVisible
)
448 wxRect rect
= GetRect();
449 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
452 bool wxSearchCtrl::IsSearchButtonVisible() const
454 return m_searchButtonVisible
;
458 void wxSearchCtrl::ShowCancelButton( bool show
)
460 if ( m_cancelButtonVisible
== show
)
465 m_cancelButtonVisible
= show
;
467 wxRect rect
= GetRect();
468 LayoutControls(0, 0, rect
.GetWidth(), rect
.GetHeight());
471 bool wxSearchCtrl::IsCancelButtonVisible() const
473 return m_cancelButtonVisible
;
476 void wxSearchCtrl::SetDescriptiveText(const wxString
& text
)
478 m_text
->SetDescriptiveText(text
);
481 wxString
wxSearchCtrl::GetDescriptiveText() const
483 return m_text
->GetDescriptiveText();
486 // ----------------------------------------------------------------------------
488 // ----------------------------------------------------------------------------
490 wxSize
wxSearchCtrl::DoGetBestSize() const
492 wxSize sizeText
= m_text
->GetBestSize();
493 wxSize
sizeSearch(0,0);
494 wxSize
sizeCancel(0,0);
495 int searchMargin
= 0;
496 int cancelMargin
= 0;
497 if ( m_searchButtonVisible
|| HasMenu() )
499 sizeSearch
= m_searchButton
->GetBestSize();
500 searchMargin
= MARGIN
;
502 if ( m_cancelButtonVisible
)
504 sizeCancel
= m_cancelButton
->GetBestSize();
505 cancelMargin
= MARGIN
;
508 int horizontalBorder
= 1 + ( sizeText
.y
- sizeText
.y
* 14 / 21 ) / 2;
510 // buttons are square and equal to the height of the text control
511 int height
= sizeText
.y
;
512 return wxSize(sizeSearch
.x
+ searchMargin
+ sizeText
.x
+ cancelMargin
+ sizeCancel
.x
+ 2*horizontalBorder
,
516 void wxSearchCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
518 wxSearchCtrlBase::DoMoveWindow(x
, y
, width
, height
);
520 LayoutControls(0, 0, width
, height
);
523 void wxSearchCtrl::LayoutControls(int x
, int y
, int width
, int height
)
528 wxSize sizeText
= m_text
->GetBestSize();
529 // make room for the search menu & clear button
530 int horizontalBorder
= ( sizeText
.y
- sizeText
.y
* 14 / 21 ) / 2;
531 x
+= horizontalBorder
;
533 width
-= horizontalBorder
*2;
535 if (width
< 0) width
= 0;
536 if (height
< 0) height
= 0;
538 wxSize
sizeSearch(0,0);
539 wxSize
sizeCancel(0,0);
540 int searchMargin
= 0;
541 int cancelMargin
= 0;
542 if ( m_searchButtonVisible
|| HasMenu() )
544 sizeSearch
= m_searchButton
->GetBestSize();
545 searchMargin
= MARGIN
;
547 if ( m_cancelButtonVisible
)
549 sizeCancel
= m_cancelButton
->GetBestSize();
550 cancelMargin
= MARGIN
;
552 m_searchButton
->Show( m_searchButtonVisible
|| HasMenu() );
553 m_cancelButton
->Show( m_cancelButtonVisible
);
555 if ( sizeSearch
.x
+ sizeCancel
.x
> width
)
557 sizeSearch
.x
= width
/2;
558 sizeCancel
.x
= width
/2;
562 wxCoord textWidth
= width
- sizeSearch
.x
- sizeCancel
.x
- searchMargin
- cancelMargin
- 1;
563 if (textWidth
< 0) textWidth
= 0;
565 // position the subcontrols inside the client area
567 m_searchButton
->SetSize(x
, y
+ ICON_OFFSET
- 1, sizeSearch
.x
, height
);
568 m_text
->SetSize( x
+ sizeSearch
.x
+ searchMargin
,
569 y
+ ICON_OFFSET
- BORDER
,
572 m_cancelButton
->SetSize(x
+ sizeSearch
.x
+ searchMargin
+ textWidth
+ cancelMargin
,
573 y
+ ICON_OFFSET
- 1, sizeCancel
.x
, height
);
580 wxString
wxSearchCtrl::GetValue() const
582 wxString value
= m_text
->GetValue();
583 if (value
== m_text
->GetDescriptiveText())
584 return wxEmptyString
;
588 void wxSearchCtrl::SetValue(const wxString
& value
)
590 m_text
->SetValue(value
);
593 wxString
wxSearchCtrl::GetRange(long from
, long to
) const
595 return m_text
->GetRange(from
, to
);
598 int wxSearchCtrl::GetLineLength(long lineNo
) const
600 return m_text
->GetLineLength(lineNo
);
602 wxString
wxSearchCtrl::GetLineText(long lineNo
) const
604 return m_text
->GetLineText(lineNo
);
606 int wxSearchCtrl::GetNumberOfLines() const
608 return m_text
->GetNumberOfLines();
611 bool wxSearchCtrl::IsModified() const
613 return m_text
->IsModified();
615 bool wxSearchCtrl::IsEditable() const
617 return m_text
->IsEditable();
620 // more readable flag testing methods
621 bool wxSearchCtrl::IsSingleLine() const
623 return m_text
->IsSingleLine();
625 bool wxSearchCtrl::IsMultiLine() const
627 return m_text
->IsMultiLine();
630 // If the return values from and to are the same, there is no selection.
631 void wxSearchCtrl::GetSelection(long* from
, long* to
) const
633 m_text
->GetSelection(from
, to
);
636 wxString
wxSearchCtrl::GetStringSelection() const
638 return m_text
->GetStringSelection();
645 void wxSearchCtrl::Clear()
649 void wxSearchCtrl::Replace(long from
, long to
, const wxString
& value
)
651 m_text
->Replace(from
, to
, value
);
653 void wxSearchCtrl::Remove(long from
, long to
)
655 m_text
->Remove(from
, to
);
658 // load/save the controls contents from/to the file
659 bool wxSearchCtrl::LoadFile(const wxString
& file
)
661 return m_text
->LoadFile(file
);
663 bool wxSearchCtrl::SaveFile(const wxString
& file
)
665 return m_text
->SaveFile(file
);
668 // sets/clears the dirty flag
669 void wxSearchCtrl::MarkDirty()
673 void wxSearchCtrl::DiscardEdits()
675 m_text
->DiscardEdits();
678 // set the max number of characters which may be entered in a single line
680 void wxSearchCtrl::SetMaxLength(unsigned long len
)
682 m_text
->SetMaxLength(len
);
685 // writing text inserts it at the current position, appending always
686 // inserts it at the end
687 void wxSearchCtrl::WriteText(const wxString
& text
)
689 m_text
->WriteText(text
);
691 void wxSearchCtrl::AppendText(const wxString
& text
)
693 m_text
->AppendText(text
);
696 // insert the character which would have resulted from this key event,
697 // return true if anything has been inserted
698 bool wxSearchCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
700 return m_text
->EmulateKeyPress(event
);
703 // text control under some platforms supports the text styles: these
704 // methods allow to apply the given text style to the given selection or to
705 // set/get the style which will be used for all appended text
706 bool wxSearchCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
708 return m_text
->SetStyle(start
, end
, style
);
710 bool wxSearchCtrl::GetStyle(long position
, wxTextAttr
& style
)
712 return m_text
->GetStyle(position
, style
);
714 bool wxSearchCtrl::SetDefaultStyle(const wxTextAttr
& style
)
716 return m_text
->SetDefaultStyle(style
);
718 const wxTextAttr
& wxSearchCtrl::GetDefaultStyle() const
720 return m_text
->GetDefaultStyle();
723 // translate between the position (which is just an index in the text ctrl
724 // considering all its contents as a single strings) and (x, y) coordinates
725 // which represent column and line.
726 long wxSearchCtrl::XYToPosition(long x
, long y
) const
728 return m_text
->XYToPosition(x
, y
);
730 bool wxSearchCtrl::PositionToXY(long pos
, long *x
, long *y
) const
732 return m_text
->PositionToXY(pos
, x
, y
);
735 void wxSearchCtrl::ShowPosition(long pos
)
737 m_text
->ShowPosition(pos
);
740 // find the character at position given in pixels
742 // NB: pt is in device coords (not adjusted for the client area origin nor
744 wxTextCtrlHitTestResult
wxSearchCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
746 return m_text
->HitTest(pt
, pos
);
748 wxTextCtrlHitTestResult
wxSearchCtrl::HitTest(const wxPoint
& pt
,
750 wxTextCoord
*row
) const
752 return m_text
->HitTest(pt
, col
, row
);
755 // Clipboard operations
756 void wxSearchCtrl::Copy()
760 void wxSearchCtrl::Cut()
764 void wxSearchCtrl::Paste()
769 bool wxSearchCtrl::CanCopy() const
771 return m_text
->CanCopy();
773 bool wxSearchCtrl::CanCut() const
775 return m_text
->CanCut();
777 bool wxSearchCtrl::CanPaste() const
779 return m_text
->CanPaste();
783 void wxSearchCtrl::Undo()
787 void wxSearchCtrl::Redo()
792 bool wxSearchCtrl::CanUndo() const
794 return m_text
->CanUndo();
796 bool wxSearchCtrl::CanRedo() const
798 return m_text
->CanRedo();
802 void wxSearchCtrl::SetInsertionPoint(long pos
)
804 m_text
->SetInsertionPoint(pos
);
806 void wxSearchCtrl::SetInsertionPointEnd()
808 m_text
->SetInsertionPointEnd();
810 long wxSearchCtrl::GetInsertionPoint() const
812 return m_text
->GetInsertionPoint();
814 long wxSearchCtrl::GetLastPosition() const
816 return m_text
->GetLastPosition();
819 void wxSearchCtrl::SetSelection(long from
, long to
)
821 m_text
->SetSelection(from
, to
);
823 void wxSearchCtrl::SelectAll()
828 void wxSearchCtrl::SetEditable(bool editable
)
830 m_text
->SetEditable(editable
);
833 bool wxSearchCtrl::SetFont(const wxFont
& font
)
835 bool result
= wxSearchCtrlBase::SetFont(font
);
836 if ( result
&& m_text
)
838 result
= m_text
->SetFont(font
);
844 // search control generic only
845 void wxSearchCtrl::SetSearchBitmap( const wxBitmap
& bitmap
)
847 m_searchBitmap
= bitmap
;
848 m_searchBitmapUser
= bitmap
.Ok();
849 if ( m_searchBitmapUser
)
851 if ( m_searchButton
&& !HasMenu() )
853 m_searchButton
->SetBitmapLabel( m_searchBitmap
);
858 // the user bitmap was just cleared, generate one
865 void wxSearchCtrl::SetSearchMenuBitmap( const wxBitmap
& bitmap
)
867 m_searchMenuBitmap
= bitmap
;
868 m_searchMenuBitmapUser
= bitmap
.Ok();
869 if ( m_searchMenuBitmapUser
)
871 if ( m_searchButton
&& m_menu
)
873 m_searchButton
->SetBitmapLabel( m_searchMenuBitmap
);
878 // the user bitmap was just cleared, generate one
883 #endif // wxUSE_MENUS
885 void wxSearchCtrl::SetCancelBitmap( const wxBitmap
& bitmap
)
887 m_cancelBitmap
= bitmap
;
888 m_cancelBitmapUser
= bitmap
.Ok();
889 if ( m_cancelBitmapUser
)
891 if ( m_cancelButton
)
893 m_cancelButton
->SetBitmapLabel( m_cancelBitmap
);
898 // the user bitmap was just cleared, generate one
905 // override streambuf method
906 #if wxHAS_TEXT_WINDOW_STREAM
908 #endif // wxHAS_TEXT_WINDOW_STREAM
910 // stream-like insertion operators: these are always available, whether we
911 // were, or not, compiled with streambuf support
912 wxTextCtrl
& operator<<(const wxString
& s
);
913 wxTextCtrl
& operator<<(int i
);
914 wxTextCtrl
& operator<<(long i
);
915 wxTextCtrl
& operator<<(float f
);
916 wxTextCtrl
& operator<<(double d
);
917 wxTextCtrl
& operator<<(const wxChar c
);
920 void wxSearchCtrl::DoSetValue(const wxString
& value
, int flags
)
922 m_text
->DoSetValue(value
, flags
);
925 bool wxSearchCtrl::DoLoadFile(const wxString
& file
, int fileType
)
927 return m_text
->DoLoadFile(file
, fileType
);
930 bool wxSearchCtrl::DoSaveFile(const wxString
& file
, int fileType
)
932 return m_text
->DoSaveFile(file
, fileType
);
935 // do the window-specific processing after processing the update event
936 void wxSearchCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
938 wxSearchCtrlBase::DoUpdateWindowUI(event
);
941 bool wxSearchCtrl::ShouldInheritColours() const
946 // icons are rendered at 3-8 times larger than necessary and downscaled for
948 static int GetMultiplier()
951 // speed up bitmap generation by using a small bitmap
954 int depth
= ::wxDisplayDepth();
964 wxBitmap
wxSearchCtrl::RenderSearchBitmap( int x
, int y
, bool renderDrop
)
966 wxColour bg
= GetBackgroundColour();
967 wxColour fg
= wxStepColour(GetForegroundColour(), LIGHT_STEP
-20);
969 //===============================================================================
970 // begin drawing code
971 //===============================================================================
974 // force width:height ratio
986 // glass 11x11, top left corner
987 // handle (9,9)-(13,13)
988 // drop (13,16)-(19,6)-(16,9)
990 int multiplier
= GetMultiplier();
991 int penWidth
= multiplier
* 2;
993 penWidth
= penWidth
* x
/ 20;
995 wxBitmap
bitmap( multiplier
*x
, multiplier
*y
);
997 mem
.SelectObject(bitmap
);
1000 mem
.SetBrush( wxBrush(bg
) );
1001 mem
.SetPen( wxPen(bg
) );
1002 mem
.DrawRectangle(0,0,bitmap
.GetWidth(),bitmap
.GetHeight());
1005 mem
.SetBrush( wxBrush(fg
) );
1006 mem
.SetPen( wxPen(fg
) );
1007 int glassBase
= 5 * x
/ 20;
1008 int glassFactor
= 2*glassBase
+ 1;
1009 int radius
= multiplier
*glassFactor
/2;
1010 mem
.DrawCircle(radius
,radius
,radius
);
1011 mem
.SetBrush( wxBrush(bg
) );
1012 mem
.SetPen( wxPen(bg
) );
1013 mem
.DrawCircle(radius
,radius
,radius
-penWidth
);
1016 int lineStart
= radius
+ (radius
-penWidth
/2) * 707 / 1000; // 707 / 1000 = 0.707 = 1/sqrt(2);
1018 mem
.SetPen( wxPen(fg
) );
1019 mem
.SetBrush( wxBrush(fg
) );
1020 int handleCornerShift
= penWidth
* 707 / 1000 / 2; // 707 / 1000 = 0.707 = 1/sqrt(2);
1021 handleCornerShift
= WXMAX( handleCornerShift
, 1 );
1022 int handleBase
= 4 * x
/ 20;
1023 int handleLength
= 2*handleBase
+1;
1024 wxPoint handlePolygon
[] =
1026 wxPoint(-handleCornerShift
,+handleCornerShift
),
1027 wxPoint(+handleCornerShift
,-handleCornerShift
),
1028 wxPoint(multiplier
*handleLength
/2+handleCornerShift
,multiplier
*handleLength
/2-handleCornerShift
),
1029 wxPoint(multiplier
*handleLength
/2-handleCornerShift
,multiplier
*handleLength
/2+handleCornerShift
),
1031 mem
.DrawPolygon(WXSIZEOF(handlePolygon
),handlePolygon
,lineStart
,lineStart
);
1033 // draw drop triangle
1034 int triangleX
= 13 * x
/ 20;
1035 int triangleY
= 5 * x
/ 20;
1036 int triangleBase
= 3 * x
/ 20;
1037 int triangleFactor
= triangleBase
*2+1;
1040 wxPoint dropPolygon
[] =
1042 wxPoint(multiplier
*0,multiplier
*0), // triangle left
1043 wxPoint(multiplier
*triangleFactor
-1,multiplier
*0), // triangle right
1044 wxPoint(multiplier
*triangleFactor
/2,multiplier
*triangleFactor
/2), // triangle bottom
1046 mem
.DrawPolygon(WXSIZEOF(dropPolygon
),dropPolygon
,multiplier
*triangleX
,multiplier
*triangleY
);
1048 mem
.SelectObject(wxNullBitmap
);
1050 //===============================================================================
1052 //===============================================================================
1054 if ( multiplier
!= 1 )
1056 wxImage image
= bitmap
.ConvertToImage();
1058 bitmap
= wxBitmap( image
);
1062 // Trim the edge where the arrow would have gone
1063 bitmap
= bitmap
.GetSubBitmap(wxRect(0,0, y
,y
));
1069 wxBitmap
wxSearchCtrl::RenderCancelBitmap( int x
, int y
)
1071 wxColour bg
= GetBackgroundColour();
1072 wxColour fg
= wxStepColour(GetForegroundColour(), LIGHT_STEP
);
1074 //===============================================================================
1075 // begin drawing code
1076 //===============================================================================
1093 // cross line starts (4,4)-(10,10)
1094 // drop (13,16)-(19,6)-(16,9)
1096 int multiplier
= GetMultiplier();
1098 int penWidth
= multiplier
* x
/ 14;
1100 wxBitmap
bitmap( multiplier
*x
, multiplier
*y
);
1102 mem
.SelectObject(bitmap
);
1105 mem
.SetBrush( wxBrush(bg
) );
1106 mem
.SetPen( wxPen(bg
) );
1107 mem
.DrawRectangle(0,0,bitmap
.GetWidth(),bitmap
.GetHeight());
1110 mem
.SetBrush( wxBrush(fg
) );
1111 mem
.SetPen( wxPen(fg
) );
1112 int radius
= multiplier
*x
/2;
1113 mem
.DrawCircle(radius
,radius
,radius
);
1116 int lineStartBase
= 4 * x
/ 14;
1117 int lineLength
= x
- 2*lineStartBase
;
1119 mem
.SetPen( wxPen(bg
) );
1120 mem
.SetBrush( wxBrush(bg
) );
1121 int handleCornerShift
= penWidth
/2;
1122 handleCornerShift
= WXMAX( handleCornerShift
, 1 );
1123 wxPoint handlePolygon
[] =
1125 wxPoint(-handleCornerShift
,+handleCornerShift
),
1126 wxPoint(+handleCornerShift
,-handleCornerShift
),
1127 wxPoint(multiplier
*lineLength
+handleCornerShift
,multiplier
*lineLength
-handleCornerShift
),
1128 wxPoint(multiplier
*lineLength
-handleCornerShift
,multiplier
*lineLength
+handleCornerShift
),
1130 mem
.DrawPolygon(WXSIZEOF(handlePolygon
),handlePolygon
,multiplier
*lineStartBase
,multiplier
*lineStartBase
);
1131 wxPoint handlePolygon2
[] =
1133 wxPoint(+handleCornerShift
,+handleCornerShift
),
1134 wxPoint(-handleCornerShift
,-handleCornerShift
),
1135 wxPoint(multiplier
*lineLength
-handleCornerShift
,-multiplier
*lineLength
-handleCornerShift
),
1136 wxPoint(multiplier
*lineLength
+handleCornerShift
,-multiplier
*lineLength
+handleCornerShift
),
1138 mem
.DrawPolygon(WXSIZEOF(handlePolygon2
),handlePolygon2
,multiplier
*lineStartBase
,multiplier
*(x
-lineStartBase
));
1140 //===============================================================================
1142 //===============================================================================
1144 if ( multiplier
!= 1 )
1146 wxImage image
= bitmap
.ConvertToImage();
1148 bitmap
= wxBitmap( image
);
1154 void wxSearchCtrl::RecalcBitmaps()
1160 wxSize sizeText
= m_text
->GetBestSize();
1162 int bitmapHeight
= sizeText
.y
- 2 * ICON_MARGIN
;
1163 int bitmapWidth
= sizeText
.y
* 20 / 14;
1165 if ( !m_searchBitmapUser
)
1168 !m_searchBitmap
.Ok() ||
1169 m_searchBitmap
.GetHeight() != bitmapHeight
||
1170 m_searchBitmap
.GetWidth() != bitmapWidth
1173 m_searchBitmap
= RenderSearchBitmap(bitmapWidth
,bitmapHeight
,false);
1176 m_searchButton
->SetBitmapLabel(m_searchBitmap
);
1179 // else this bitmap was set by user, don't alter
1183 if ( !m_searchMenuBitmapUser
)
1186 !m_searchMenuBitmap
.Ok() ||
1187 m_searchMenuBitmap
.GetHeight() != bitmapHeight
||
1188 m_searchMenuBitmap
.GetWidth() != bitmapWidth
1191 m_searchMenuBitmap
= RenderSearchBitmap(bitmapWidth
,bitmapHeight
,true);
1194 m_searchButton
->SetBitmapLabel(m_searchMenuBitmap
);
1197 // else this bitmap was set by user, don't alter
1199 #endif // wxUSE_MENUS
1201 if ( !m_cancelBitmapUser
)
1204 !m_cancelBitmap
.Ok() ||
1205 m_cancelBitmap
.GetHeight() != bitmapHeight
||
1206 m_cancelBitmap
.GetWidth() != bitmapHeight
1209 m_cancelBitmap
= RenderCancelBitmap(bitmapHeight
-BORDER
-1,bitmapHeight
-BORDER
-1); // square
1210 m_cancelButton
->SetBitmapLabel(m_cancelBitmap
);
1212 // else this bitmap was set by user, don't alter
1216 void wxSearchCtrl::OnSearchButton( wxCommandEvent
& event
)
1221 void wxSearchCtrl::OnSetFocus( wxFocusEvent
& /*event*/ )
1229 void wxSearchCtrl::OnSize( wxSizeEvent
& WXUNUSED(event
) )
1232 GetSize(&width
, &height
);
1233 LayoutControls(0, 0, width
, height
);
1238 void wxSearchCtrl::PopupSearchMenu()
1242 wxSize size
= GetSize();
1243 PopupMenu( m_menu
, 0, size
.y
);
1247 #endif // wxUSE_MENUS
1249 #endif // !wxUSE_NATIVE_SEARCH_CONTROL
1251 #endif // wxUSE_SEARCHCTRL