Don't use text control foreground colour for generic wxSearchCtrl itself.
[wxWidgets.git] / src / generic / srchctlg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/srchctlg.cpp
3 // Purpose: implements wxSearchCtrl as a composite control
4 // Author: Vince Harron
5 // Created: 2006-02-19
6 // RCS-ID: $Id$
7 // Copyright: Vince Harron
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_SEARCHCTRL
19
20 #include "wx/srchctrl.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/button.h"
24 #include "wx/dcclient.h"
25 #include "wx/menu.h"
26 #include "wx/dcmemory.h"
27 #endif //WX_PRECOMP
28
29 #if !wxUSE_NATIVE_SEARCH_CONTROL
30
31 #include "wx/image.h"
32
33 #define WXMAX(a,b) ((a)>(b)?(a):(b))
34
35 // ----------------------------------------------------------------------------
36 // constants
37 // ----------------------------------------------------------------------------
38
39 // the margin between the text control and the search/cancel buttons
40 static const wxCoord MARGIN = 2;
41
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;
47 #else
48 static const wxCoord BORDER = 2;
49 static const wxCoord ICON_MARGIN = 0;
50 static const wxCoord ICON_OFFSET = 0;
51 #endif
52
53 #define LIGHT_STEP 160
54
55 // ----------------------------------------------------------------------------
56 // wxSearchTextCtrl: text control used by search control
57 // ----------------------------------------------------------------------------
58
59 class wxSearchTextCtrl : public wxTextCtrl
60 {
61 public:
62 wxSearchTextCtrl(wxSearchCtrl *search, const wxString& value, int style)
63 : wxTextCtrl(search, wxID_ANY, value, wxDefaultPosition, wxDefaultSize,
64 (style & ~wxBORDER_MASK) | wxNO_BORDER)
65 {
66 m_search = search;
67
68 SetHint(_("Search"));
69
70 // Ensure that our best size is recomputed using our overridden
71 // DoGetBestSize().
72 InvalidateBestSize();
73 }
74
75
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)
79 {
80 wxTextCtrl::DoSetValue(value, flags);
81 }
82
83 bool DoLoadFile(const wxString& file, int fileType)
84 {
85 return wxTextCtrl::DoLoadFile(file, fileType);
86 }
87
88 bool DoSaveFile(const wxString& file, int fileType)
89 {
90 return wxTextCtrl::DoSaveFile(file, fileType);
91 }
92
93 protected:
94 void OnText(wxCommandEvent& eventText)
95 {
96 wxCommandEvent event(eventText);
97 event.SetEventObject(m_search);
98 event.SetId(m_search->GetId());
99
100 m_search->GetEventHandler()->ProcessEvent(event);
101 }
102
103 void OnTextUrl(wxTextUrlEvent& eventText)
104 {
105 // copy constructor is disabled for some reason?
106 //wxTextUrlEvent event(eventText);
107 wxTextUrlEvent event(
108 m_search->GetId(),
109 eventText.GetMouseEvent(),
110 eventText.GetURLStart(),
111 eventText.GetURLEnd()
112 );
113 event.SetEventObject(m_search);
114
115 m_search->GetEventHandler()->ProcessEvent(event);
116 }
117
118 #ifdef __WXMSW__
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.
124 //
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
130 // at all.
131 virtual wxSize DoGetBestSize() const
132 {
133 const long flags = GetWindowStyleFlag();
134 wxSearchTextCtrl* const self = const_cast<wxSearchTextCtrl*>(this);
135
136 self->SetWindowStyleFlag((flags & ~wxBORDER_MASK) | wxBORDER_DEFAULT);
137 const wxSize size = wxTextCtrl::DoGetBestSize();
138 self->SetWindowStyleFlag(flags);
139
140 return size;
141 }
142 #endif // __WXMSW__
143
144 private:
145 wxSearchCtrl* m_search;
146
147 DECLARE_EVENT_TABLE()
148 };
149
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)
155 END_EVENT_TABLE()
156
157 // ----------------------------------------------------------------------------
158 // wxSearchButton: search button used by search control
159 // ----------------------------------------------------------------------------
160
161 class wxSearchButton : public wxControl
162 {
163 public:
164 wxSearchButton(wxSearchCtrl *search, int eventType, const wxBitmap& bmp)
165 : wxControl(search, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNO_BORDER),
166 m_search(search),
167 m_eventType(eventType),
168 m_bmp(bmp)
169 { }
170
171 void SetBitmapLabel(const wxBitmap& label) { m_bmp = label; }
172
173 // The buttons in wxSearchCtrl shouldn't accept focus from keyboard because
174 // this would interfere with the usual TAB processing: the user expects
175 // that pressing TAB in the search control should switch focus to the next
176 // control and not give it to the button inside the same control. Besides,
177 // the search button can be already activated by pressing "Enter" so there
178 // is really no reason for it to be able to get focus from keyboard.
179 virtual bool AcceptsFocusFromKeyboard() const { return false; }
180
181 protected:
182 wxSize DoGetBestSize() const
183 {
184 return wxSize(m_bmp.GetWidth(), m_bmp.GetHeight());
185 }
186
187 void OnLeftUp(wxMouseEvent&)
188 {
189 wxCommandEvent event(m_eventType, m_search->GetId());
190 event.SetEventObject(m_search);
191
192 if ( m_eventType == wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN )
193 {
194 // it's convenient to have the string to search for directly in the
195 // event instead of having to retrieve it from the control in the
196 // event handler code later, so provide it here
197 event.SetString(m_search->GetValue());
198 }
199
200 GetEventHandler()->ProcessEvent(event);
201
202 m_search->SetFocus();
203
204 #if wxUSE_MENUS
205 if ( m_eventType == wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN )
206 {
207 // this happens automatically, just like on Mac OS X
208 m_search->PopupSearchMenu();
209 }
210 #endif // wxUSE_MENUS
211 }
212
213 void OnPaint(wxPaintEvent&)
214 {
215 wxPaintDC dc(this);
216 dc.DrawBitmap(m_bmp, 0,0, true);
217 }
218
219
220 private:
221 wxSearchCtrl *m_search;
222 wxEventType m_eventType;
223 wxBitmap m_bmp;
224
225 DECLARE_EVENT_TABLE()
226 };
227
228 BEGIN_EVENT_TABLE(wxSearchButton, wxControl)
229 EVT_LEFT_UP(wxSearchButton::OnLeftUp)
230 EVT_PAINT(wxSearchButton::OnPaint)
231 END_EVENT_TABLE()
232
233 BEGIN_EVENT_TABLE(wxSearchCtrl, wxSearchCtrlBase)
234 EVT_SEARCHCTRL_SEARCH_BTN(wxID_ANY, wxSearchCtrl::OnSearchButton)
235 EVT_SET_FOCUS(wxSearchCtrl::OnSetFocus)
236 EVT_SIZE(wxSearchCtrl::OnSize)
237 END_EVENT_TABLE()
238
239 IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl, wxSearchCtrlBase)
240
241 // ============================================================================
242 // implementation
243 // ============================================================================
244
245 // ----------------------------------------------------------------------------
246 // wxSearchCtrl creation
247 // ----------------------------------------------------------------------------
248
249 // creation
250 // --------
251
252 wxSearchCtrl::wxSearchCtrl()
253 {
254 Init();
255 }
256
257 wxSearchCtrl::wxSearchCtrl(wxWindow *parent, wxWindowID id,
258 const wxString& value,
259 const wxPoint& pos,
260 const wxSize& size,
261 long style,
262 const wxValidator& validator,
263 const wxString& name)
264 {
265 Init();
266
267 Create(parent, id, value, pos, size, style, validator, name);
268 }
269
270 void wxSearchCtrl::Init()
271 {
272 m_text = NULL;
273 m_searchButton = NULL;
274 m_cancelButton = NULL;
275 #if wxUSE_MENUS
276 m_menu = NULL;
277 #endif // wxUSE_MENUS
278
279 m_searchButtonVisible = true;
280 m_cancelButtonVisible = false;
281
282 m_searchBitmapUser = false;
283 m_cancelBitmapUser = false;
284 #if wxUSE_MENUS
285 m_searchMenuBitmapUser = false;
286 #endif // wxUSE_MENUS
287 }
288
289 bool wxSearchCtrl::Create(wxWindow *parent, wxWindowID id,
290 const wxString& value,
291 const wxPoint& pos,
292 const wxSize& size,
293 long style,
294 const wxValidator& validator,
295 const wxString& name)
296 {
297 // force border style for more native appearance
298 style &= ~wxBORDER_MASK;
299 #ifdef __WXGTK__
300 style |= wxBORDER_SUNKEN;
301 #elif defined(__WXMSW__)
302 // Don't set the style explicitly, let GetDefaultBorder() work it out, unless
303 // we will get a sunken border (e.g. on Windows 200) in which case we must
304 // override with a simple border.
305 if (GetDefaultBorder() == wxBORDER_SUNKEN)
306 style |= wxBORDER_SIMPLE;
307 #else
308 style |= wxBORDER_SIMPLE;
309 #endif
310 if ( !wxSearchCtrlBaseBaseClass::Create(parent, id, pos, size,
311 style, validator, name) )
312 {
313 return false;
314 }
315
316 m_text = new wxSearchTextCtrl(this, value, style);
317
318 m_searchButton = new wxSearchButton(this,
319 wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN,
320 m_searchBitmap);
321 m_cancelButton = new wxSearchButton(this,
322 wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN,
323 m_cancelBitmap);
324
325 SetBackgroundColour( m_text->GetBackgroundColour() );
326
327 RecalcBitmaps();
328
329 SetInitialSize(size);
330 Move(pos);
331 return true;
332 }
333
334 wxSearchCtrl::~wxSearchCtrl()
335 {
336 delete m_text;
337 delete m_searchButton;
338 delete m_cancelButton;
339 #if wxUSE_MENUS
340 delete m_menu;
341 #endif // wxUSE_MENUS
342 }
343
344
345 // search control specific interfaces
346 #if wxUSE_MENUS
347
348 void wxSearchCtrl::SetMenu( wxMenu* menu )
349 {
350 if ( menu == m_menu )
351 {
352 // no change
353 return;
354 }
355 bool hadMenu = (m_menu != NULL);
356 delete m_menu;
357 m_menu = menu;
358
359 if ( m_menu && !hadMenu )
360 {
361 m_searchButton->SetBitmapLabel(m_searchMenuBitmap);
362 m_searchButton->Refresh();
363 }
364 else if ( !m_menu && hadMenu )
365 {
366 m_searchButton->SetBitmapLabel(m_searchBitmap);
367 if ( m_searchButtonVisible )
368 {
369 m_searchButton->Refresh();
370 }
371 }
372 wxRect rect = GetRect();
373 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
374 }
375
376 wxMenu* wxSearchCtrl::GetMenu()
377 {
378 return m_menu;
379 }
380
381 #endif // wxUSE_MENUS
382
383 void wxSearchCtrl::ShowSearchButton( bool show )
384 {
385 if ( m_searchButtonVisible == show )
386 {
387 // no change
388 return;
389 }
390 m_searchButtonVisible = show;
391 if ( m_searchButtonVisible )
392 {
393 RecalcBitmaps();
394 }
395
396 wxRect rect = GetRect();
397 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
398 }
399
400 bool wxSearchCtrl::IsSearchButtonVisible() const
401 {
402 return m_searchButtonVisible;
403 }
404
405
406 void wxSearchCtrl::ShowCancelButton( bool show )
407 {
408 if ( m_cancelButtonVisible == show )
409 {
410 // no change
411 return;
412 }
413 m_cancelButtonVisible = show;
414
415 wxRect rect = GetRect();
416 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
417 }
418
419 bool wxSearchCtrl::IsCancelButtonVisible() const
420 {
421 return m_cancelButtonVisible;
422 }
423
424 void wxSearchCtrl::SetDescriptiveText(const wxString& text)
425 {
426 m_text->SetHint(text);
427 }
428
429 wxString wxSearchCtrl::GetDescriptiveText() const
430 {
431 return m_text->GetHint();
432 }
433
434 // ----------------------------------------------------------------------------
435 // geometry
436 // ----------------------------------------------------------------------------
437
438 wxSize wxSearchCtrl::DoGetBestSize() const
439 {
440 wxSize sizeText = m_text->GetBestSize();
441 wxSize sizeSearch(0,0);
442 wxSize sizeCancel(0,0);
443 int searchMargin = 0;
444 int cancelMargin = 0;
445 if ( m_searchButtonVisible || HasMenu() )
446 {
447 sizeSearch = m_searchButton->GetBestSize();
448 searchMargin = MARGIN;
449 }
450 if ( m_cancelButtonVisible )
451 {
452 sizeCancel = m_cancelButton->GetBestSize();
453 cancelMargin = MARGIN;
454 }
455
456 int horizontalBorder = 1 + ( sizeText.y - sizeText.y * 14 / 21 ) / 2;
457
458 // buttons are square and equal to the height of the text control
459 int height = sizeText.y;
460 return wxSize(sizeSearch.x + searchMargin + sizeText.x + cancelMargin + sizeCancel.x + 2*horizontalBorder,
461 height + 2*BORDER);
462 }
463
464 void wxSearchCtrl::DoMoveWindow(int x, int y, int width, int height)
465 {
466 wxSearchCtrlBase::DoMoveWindow(x, y, width, height);
467
468 LayoutControls(0, 0, width, height);
469 }
470
471 void wxSearchCtrl::LayoutControls(int x, int y, int width, int height)
472 {
473 if ( !m_text )
474 return;
475
476 wxSize sizeText = m_text->GetBestSize();
477 // make room for the search menu & clear button
478 int horizontalBorder = ( sizeText.y - sizeText.y * 14 / 21 ) / 2;
479 x += horizontalBorder;
480 y += BORDER;
481 width -= horizontalBorder*2;
482 height -= BORDER*2;
483 if (width < 0) width = 0;
484 if (height < 0) height = 0;
485
486 wxSize sizeSearch(0,0);
487 wxSize sizeCancel(0,0);
488 int searchMargin = 0;
489 int cancelMargin = 0;
490 if ( m_searchButtonVisible || HasMenu() )
491 {
492 sizeSearch = m_searchButton->GetBestSize();
493 searchMargin = MARGIN;
494 }
495 if ( m_cancelButtonVisible )
496 {
497 sizeCancel = m_cancelButton->GetBestSize();
498 cancelMargin = MARGIN;
499 }
500 m_searchButton->Show( m_searchButtonVisible || HasMenu() );
501 m_cancelButton->Show( m_cancelButtonVisible );
502
503 if ( sizeSearch.x + sizeCancel.x > width )
504 {
505 sizeSearch.x = width/2;
506 sizeCancel.x = width/2;
507 searchMargin = 0;
508 cancelMargin = 0;
509 }
510 wxCoord textWidth = width - sizeSearch.x - sizeCancel.x - searchMargin - cancelMargin - 1;
511 if (textWidth < 0) textWidth = 0;
512
513 // position the subcontrols inside the client area
514
515 m_searchButton->SetSize(x, y + ICON_OFFSET - 1, sizeSearch.x, height);
516 m_text->SetSize( x + sizeSearch.x + searchMargin,
517 y + ICON_OFFSET - BORDER,
518 textWidth,
519 height);
520 m_cancelButton->SetSize(x + sizeSearch.x + searchMargin + textWidth + cancelMargin,
521 y + ICON_OFFSET - 1, sizeCancel.x, height);
522 }
523
524 wxWindowList wxSearchCtrl::GetCompositeWindowParts() const
525 {
526 wxWindowList parts;
527 parts.push_back(m_text);
528 parts.push_back(m_searchButton);
529 parts.push_back(m_cancelButton);
530 return parts;
531 }
532
533 // accessors
534 // ---------
535
536 wxString wxSearchCtrl::DoGetValue() const
537 {
538 return m_text->GetValue();
539 }
540 wxString wxSearchCtrl::GetRange(long from, long to) const
541 {
542 return m_text->GetRange(from, to);
543 }
544
545 int wxSearchCtrl::GetLineLength(long lineNo) const
546 {
547 return m_text->GetLineLength(lineNo);
548 }
549 wxString wxSearchCtrl::GetLineText(long lineNo) const
550 {
551 return m_text->GetLineText(lineNo);
552 }
553 int wxSearchCtrl::GetNumberOfLines() const
554 {
555 return m_text->GetNumberOfLines();
556 }
557
558 bool wxSearchCtrl::IsModified() const
559 {
560 return m_text->IsModified();
561 }
562 bool wxSearchCtrl::IsEditable() const
563 {
564 return m_text->IsEditable();
565 }
566
567 // more readable flag testing methods
568 bool wxSearchCtrl::IsSingleLine() const
569 {
570 return m_text->IsSingleLine();
571 }
572 bool wxSearchCtrl::IsMultiLine() const
573 {
574 return m_text->IsMultiLine();
575 }
576
577 // If the return values from and to are the same, there is no selection.
578 void wxSearchCtrl::GetSelection(long* from, long* to) const
579 {
580 m_text->GetSelection(from, to);
581 }
582
583 wxString wxSearchCtrl::GetStringSelection() const
584 {
585 return m_text->GetStringSelection();
586 }
587
588 // operations
589 // ----------
590
591 // editing
592 void wxSearchCtrl::Clear()
593 {
594 m_text->Clear();
595 }
596 void wxSearchCtrl::Replace(long from, long to, const wxString& value)
597 {
598 m_text->Replace(from, to, value);
599 }
600 void wxSearchCtrl::Remove(long from, long to)
601 {
602 m_text->Remove(from, to);
603 }
604
605 // load/save the controls contents from/to the file
606 bool wxSearchCtrl::LoadFile(const wxString& file)
607 {
608 return m_text->LoadFile(file);
609 }
610 bool wxSearchCtrl::SaveFile(const wxString& file)
611 {
612 return m_text->SaveFile(file);
613 }
614
615 // sets/clears the dirty flag
616 void wxSearchCtrl::MarkDirty()
617 {
618 m_text->MarkDirty();
619 }
620 void wxSearchCtrl::DiscardEdits()
621 {
622 m_text->DiscardEdits();
623 }
624
625 // set the max number of characters which may be entered in a single line
626 // text control
627 void wxSearchCtrl::SetMaxLength(unsigned long len)
628 {
629 m_text->SetMaxLength(len);
630 }
631
632 // writing text inserts it at the current position, appending always
633 // inserts it at the end
634 void wxSearchCtrl::WriteText(const wxString& text)
635 {
636 m_text->WriteText(text);
637 }
638 void wxSearchCtrl::AppendText(const wxString& text)
639 {
640 m_text->AppendText(text);
641 }
642
643 // insert the character which would have resulted from this key event,
644 // return true if anything has been inserted
645 bool wxSearchCtrl::EmulateKeyPress(const wxKeyEvent& event)
646 {
647 return m_text->EmulateKeyPress(event);
648 }
649
650 // text control under some platforms supports the text styles: these
651 // methods allow to apply the given text style to the given selection or to
652 // set/get the style which will be used for all appended text
653 bool wxSearchCtrl::SetStyle(long start, long end, const wxTextAttr& style)
654 {
655 return m_text->SetStyle(start, end, style);
656 }
657 bool wxSearchCtrl::GetStyle(long position, wxTextAttr& style)
658 {
659 return m_text->GetStyle(position, style);
660 }
661 bool wxSearchCtrl::SetDefaultStyle(const wxTextAttr& style)
662 {
663 return m_text->SetDefaultStyle(style);
664 }
665 const wxTextAttr& wxSearchCtrl::GetDefaultStyle() const
666 {
667 return m_text->GetDefaultStyle();
668 }
669
670 // translate between the position (which is just an index in the text ctrl
671 // considering all its contents as a single strings) and (x, y) coordinates
672 // which represent column and line.
673 long wxSearchCtrl::XYToPosition(long x, long y) const
674 {
675 return m_text->XYToPosition(x, y);
676 }
677 bool wxSearchCtrl::PositionToXY(long pos, long *x, long *y) const
678 {
679 return m_text->PositionToXY(pos, x, y);
680 }
681
682 void wxSearchCtrl::ShowPosition(long pos)
683 {
684 m_text->ShowPosition(pos);
685 }
686
687 // find the character at position given in pixels
688 //
689 // NB: pt is in device coords (not adjusted for the client area origin nor
690 // scrolling)
691 wxTextCtrlHitTestResult wxSearchCtrl::HitTest(const wxPoint& pt, long *pos) const
692 {
693 return m_text->HitTest(pt, pos);
694 }
695 wxTextCtrlHitTestResult wxSearchCtrl::HitTest(const wxPoint& pt,
696 wxTextCoord *col,
697 wxTextCoord *row) const
698 {
699 return m_text->HitTest(pt, col, row);
700 }
701
702 // Clipboard operations
703 void wxSearchCtrl::Copy()
704 {
705 m_text->Copy();
706 }
707 void wxSearchCtrl::Cut()
708 {
709 m_text->Cut();
710 }
711 void wxSearchCtrl::Paste()
712 {
713 m_text->Paste();
714 }
715
716 bool wxSearchCtrl::CanCopy() const
717 {
718 return m_text->CanCopy();
719 }
720 bool wxSearchCtrl::CanCut() const
721 {
722 return m_text->CanCut();
723 }
724 bool wxSearchCtrl::CanPaste() const
725 {
726 return m_text->CanPaste();
727 }
728
729 // Undo/redo
730 void wxSearchCtrl::Undo()
731 {
732 m_text->Undo();
733 }
734 void wxSearchCtrl::Redo()
735 {
736 m_text->Redo();
737 }
738
739 bool wxSearchCtrl::CanUndo() const
740 {
741 return m_text->CanUndo();
742 }
743 bool wxSearchCtrl::CanRedo() const
744 {
745 return m_text->CanRedo();
746 }
747
748 // Insertion point
749 void wxSearchCtrl::SetInsertionPoint(long pos)
750 {
751 m_text->SetInsertionPoint(pos);
752 }
753 void wxSearchCtrl::SetInsertionPointEnd()
754 {
755 m_text->SetInsertionPointEnd();
756 }
757 long wxSearchCtrl::GetInsertionPoint() const
758 {
759 return m_text->GetInsertionPoint();
760 }
761 long wxSearchCtrl::GetLastPosition() const
762 {
763 return m_text->GetLastPosition();
764 }
765
766 void wxSearchCtrl::SetSelection(long from, long to)
767 {
768 m_text->SetSelection(from, to);
769 }
770 void wxSearchCtrl::SelectAll()
771 {
772 m_text->SelectAll();
773 }
774
775 void wxSearchCtrl::SetEditable(bool editable)
776 {
777 m_text->SetEditable(editable);
778 }
779
780 bool wxSearchCtrl::SetFont(const wxFont& font)
781 {
782 if ( !wxSearchCtrlBase::SetFont(font) )
783 return false;
784
785 // Recreate the bitmaps as their size may have changed.
786 RecalcBitmaps();
787
788 return true;
789 }
790
791 bool wxSearchCtrl::SetBackgroundColour(const wxColour& colour)
792 {
793 if ( !wxSearchCtrlBase::SetBackgroundColour(colour) )
794 return false;
795
796 // When the background changes, re-render the bitmaps so that the correct
797 // colour shows in their "transparent" area.
798 RecalcBitmaps();
799
800 return true;
801 }
802
803 // search control generic only
804 void wxSearchCtrl::SetSearchBitmap( const wxBitmap& bitmap )
805 {
806 m_searchBitmap = bitmap;
807 m_searchBitmapUser = bitmap.IsOk();
808 if ( m_searchBitmapUser )
809 {
810 if ( m_searchButton && !HasMenu() )
811 {
812 m_searchButton->SetBitmapLabel( m_searchBitmap );
813 }
814 }
815 else
816 {
817 // the user bitmap was just cleared, generate one
818 RecalcBitmaps();
819 }
820 }
821
822 #if wxUSE_MENUS
823
824 void wxSearchCtrl::SetSearchMenuBitmap( const wxBitmap& bitmap )
825 {
826 m_searchMenuBitmap = bitmap;
827 m_searchMenuBitmapUser = bitmap.IsOk();
828 if ( m_searchMenuBitmapUser )
829 {
830 if ( m_searchButton && m_menu )
831 {
832 m_searchButton->SetBitmapLabel( m_searchMenuBitmap );
833 }
834 }
835 else
836 {
837 // the user bitmap was just cleared, generate one
838 RecalcBitmaps();
839 }
840 }
841
842 #endif // wxUSE_MENUS
843
844 void wxSearchCtrl::SetCancelBitmap( const wxBitmap& bitmap )
845 {
846 m_cancelBitmap = bitmap;
847 m_cancelBitmapUser = bitmap.IsOk();
848 if ( m_cancelBitmapUser )
849 {
850 if ( m_cancelButton )
851 {
852 m_cancelButton->SetBitmapLabel( m_cancelBitmap );
853 }
854 }
855 else
856 {
857 // the user bitmap was just cleared, generate one
858 RecalcBitmaps();
859 }
860 }
861
862 #if 0
863
864 // override streambuf method
865 #if wxHAS_TEXT_WINDOW_STREAM
866 int overflow(int i);
867 #endif // wxHAS_TEXT_WINDOW_STREAM
868
869 // stream-like insertion operators: these are always available, whether we
870 // were, or not, compiled with streambuf support
871 wxTextCtrl& operator<<(const wxString& s);
872 wxTextCtrl& operator<<(int i);
873 wxTextCtrl& operator<<(long i);
874 wxTextCtrl& operator<<(float f);
875 wxTextCtrl& operator<<(double d);
876 wxTextCtrl& operator<<(const wxChar c);
877 #endif
878
879 void wxSearchCtrl::DoSetValue(const wxString& value, int flags)
880 {
881 m_text->DoSetValue(value, flags);
882 }
883
884 bool wxSearchCtrl::DoLoadFile(const wxString& file, int fileType)
885 {
886 return m_text->DoLoadFile(file, fileType);
887 }
888
889 bool wxSearchCtrl::DoSaveFile(const wxString& file, int fileType)
890 {
891 return m_text->DoSaveFile(file, fileType);
892 }
893
894 // do the window-specific processing after processing the update event
895 void wxSearchCtrl::DoUpdateWindowUI(wxUpdateUIEvent& event)
896 {
897 wxSearchCtrlBase::DoUpdateWindowUI(event);
898 }
899
900 bool wxSearchCtrl::ShouldInheritColours() const
901 {
902 return true;
903 }
904
905 // icons are rendered at 3-8 times larger than necessary and downscaled for
906 // antialiasing
907 static int GetMultiplier()
908 {
909 #ifdef __WXWINCE__
910 // speed up bitmap generation by using a small bitmap
911 return 3;
912 #else
913 int depth = ::wxDisplayDepth();
914
915 if ( depth >= 24 )
916 {
917 return 8;
918 }
919 return 6;
920 #endif
921 }
922
923 wxBitmap wxSearchCtrl::RenderSearchBitmap( int x, int y, bool renderDrop )
924 {
925 wxColour bg = GetBackgroundColour();
926 wxColour fg = GetForegroundColour().ChangeLightness(LIGHT_STEP-20);
927
928 //===============================================================================
929 // begin drawing code
930 //===============================================================================
931 // image stats
932
933 // force width:height ratio
934 if ( 14*x > y*20 )
935 {
936 // x is too big
937 x = y*20/14;
938 }
939 else
940 {
941 // y is too big
942 y = x*14/20;
943 }
944
945 // glass 11x11, top left corner
946 // handle (9,9)-(13,13)
947 // drop (13,16)-(19,6)-(16,9)
948
949 int multiplier = GetMultiplier();
950 int penWidth = multiplier * 2;
951
952 penWidth = penWidth * x / 20;
953
954 wxBitmap bitmap( multiplier*x, multiplier*y );
955 wxMemoryDC mem;
956 mem.SelectObject(bitmap);
957
958 // clear background
959 mem.SetBrush( wxBrush(bg) );
960 mem.SetPen( wxPen(bg) );
961 mem.DrawRectangle(0,0,bitmap.GetWidth(),bitmap.GetHeight());
962
963 // draw drop glass
964 mem.SetBrush( wxBrush(fg) );
965 mem.SetPen( wxPen(fg) );
966 int glassBase = 5 * x / 20;
967 int glassFactor = 2*glassBase + 1;
968 int radius = multiplier*glassFactor/2;
969 mem.DrawCircle(radius,radius,radius);
970 mem.SetBrush( wxBrush(bg) );
971 mem.SetPen( wxPen(bg) );
972 mem.DrawCircle(radius,radius,radius-penWidth);
973
974 // draw handle
975 int lineStart = radius + (radius-penWidth/2) * 707 / 1000; // 707 / 1000 = 0.707 = 1/sqrt(2);
976
977 mem.SetPen( wxPen(fg) );
978 mem.SetBrush( wxBrush(fg) );
979 int handleCornerShift = penWidth * 707 / 1000 / 2; // 707 / 1000 = 0.707 = 1/sqrt(2);
980 handleCornerShift = WXMAX( handleCornerShift, 1 );
981 int handleBase = 4 * x / 20;
982 int handleLength = 2*handleBase+1;
983 wxPoint handlePolygon[] =
984 {
985 wxPoint(-handleCornerShift,+handleCornerShift),
986 wxPoint(+handleCornerShift,-handleCornerShift),
987 wxPoint(multiplier*handleLength/2+handleCornerShift,multiplier*handleLength/2-handleCornerShift),
988 wxPoint(multiplier*handleLength/2-handleCornerShift,multiplier*handleLength/2+handleCornerShift),
989 };
990 mem.DrawPolygon(WXSIZEOF(handlePolygon),handlePolygon,lineStart,lineStart);
991
992 // draw drop triangle
993 int triangleX = 13 * x / 20;
994 int triangleY = 5 * x / 20;
995 int triangleBase = 3 * x / 20;
996 int triangleFactor = triangleBase*2+1;
997 if ( renderDrop )
998 {
999 wxPoint dropPolygon[] =
1000 {
1001 wxPoint(multiplier*0,multiplier*0), // triangle left
1002 wxPoint(multiplier*triangleFactor-1,multiplier*0), // triangle right
1003 wxPoint(multiplier*triangleFactor/2,multiplier*triangleFactor/2), // triangle bottom
1004 };
1005 mem.DrawPolygon(WXSIZEOF(dropPolygon),dropPolygon,multiplier*triangleX,multiplier*triangleY);
1006 }
1007 mem.SelectObject(wxNullBitmap);
1008
1009 //===============================================================================
1010 // end drawing code
1011 //===============================================================================
1012
1013 if ( multiplier != 1 )
1014 {
1015 wxImage image = bitmap.ConvertToImage();
1016 image.Rescale(x,y);
1017 bitmap = wxBitmap( image );
1018 }
1019 if ( !renderDrop )
1020 {
1021 // Trim the edge where the arrow would have gone
1022 bitmap = bitmap.GetSubBitmap(wxRect(0,0, y,y));
1023 }
1024
1025 return bitmap;
1026 }
1027
1028 wxBitmap wxSearchCtrl::RenderCancelBitmap( int x, int y )
1029 {
1030 wxColour bg = GetBackgroundColour();
1031 wxColour fg = GetForegroundColour().ChangeLightness(LIGHT_STEP);
1032
1033 //===============================================================================
1034 // begin drawing code
1035 //===============================================================================
1036 // image stats
1037
1038 // total size 14x14
1039 // force 1:1 ratio
1040 if ( x > y )
1041 {
1042 // x is too big
1043 x = y;
1044 }
1045 else
1046 {
1047 // y is too big
1048 y = x;
1049 }
1050
1051 // 14x14 circle
1052 // cross line starts (4,4)-(10,10)
1053 // drop (13,16)-(19,6)-(16,9)
1054
1055 int multiplier = GetMultiplier();
1056
1057 int penWidth = multiplier * x / 14;
1058
1059 wxBitmap bitmap( multiplier*x, multiplier*y );
1060 wxMemoryDC mem;
1061 mem.SelectObject(bitmap);
1062
1063 // clear background
1064 mem.SetBrush( wxBrush(bg) );
1065 mem.SetPen( wxPen(bg) );
1066 mem.DrawRectangle(0,0,bitmap.GetWidth(),bitmap.GetHeight());
1067
1068 // draw drop glass
1069 mem.SetBrush( wxBrush(fg) );
1070 mem.SetPen( wxPen(fg) );
1071 int radius = multiplier*x/2;
1072 mem.DrawCircle(radius,radius,radius);
1073
1074 // draw cross
1075 int lineStartBase = 4 * x / 14;
1076 int lineLength = x - 2*lineStartBase;
1077
1078 mem.SetPen( wxPen(bg) );
1079 mem.SetBrush( wxBrush(bg) );
1080 int handleCornerShift = penWidth/2;
1081 handleCornerShift = WXMAX( handleCornerShift, 1 );
1082 wxPoint handlePolygon[] =
1083 {
1084 wxPoint(-handleCornerShift,+handleCornerShift),
1085 wxPoint(+handleCornerShift,-handleCornerShift),
1086 wxPoint(multiplier*lineLength+handleCornerShift,multiplier*lineLength-handleCornerShift),
1087 wxPoint(multiplier*lineLength-handleCornerShift,multiplier*lineLength+handleCornerShift),
1088 };
1089 mem.DrawPolygon(WXSIZEOF(handlePolygon),handlePolygon,multiplier*lineStartBase,multiplier*lineStartBase);
1090 wxPoint handlePolygon2[] =
1091 {
1092 wxPoint(+handleCornerShift,+handleCornerShift),
1093 wxPoint(-handleCornerShift,-handleCornerShift),
1094 wxPoint(multiplier*lineLength-handleCornerShift,-multiplier*lineLength-handleCornerShift),
1095 wxPoint(multiplier*lineLength+handleCornerShift,-multiplier*lineLength+handleCornerShift),
1096 };
1097 mem.DrawPolygon(WXSIZEOF(handlePolygon2),handlePolygon2,multiplier*lineStartBase,multiplier*(x-lineStartBase));
1098
1099 //===============================================================================
1100 // end drawing code
1101 //===============================================================================
1102
1103 if ( multiplier != 1 )
1104 {
1105 wxImage image = bitmap.ConvertToImage();
1106 image.Rescale(x,y);
1107 bitmap = wxBitmap( image );
1108 }
1109
1110 return bitmap;
1111 }
1112
1113 void wxSearchCtrl::RecalcBitmaps()
1114 {
1115 if ( !m_text )
1116 {
1117 return;
1118 }
1119 wxSize sizeText = m_text->GetBestSize();
1120
1121 int bitmapHeight = sizeText.y - 2 * ICON_MARGIN;
1122 int bitmapWidth = sizeText.y * 20 / 14;
1123
1124 if ( !m_searchBitmapUser )
1125 {
1126 if (
1127 !m_searchBitmap.IsOk() ||
1128 m_searchBitmap.GetHeight() != bitmapHeight ||
1129 m_searchBitmap.GetWidth() != bitmapWidth
1130 )
1131 {
1132 m_searchBitmap = RenderSearchBitmap(bitmapWidth,bitmapHeight,false);
1133 if ( !HasMenu() )
1134 {
1135 m_searchButton->SetBitmapLabel(m_searchBitmap);
1136 }
1137 }
1138 // else this bitmap was set by user, don't alter
1139 }
1140
1141 #if wxUSE_MENUS
1142 if ( !m_searchMenuBitmapUser )
1143 {
1144 if (
1145 !m_searchMenuBitmap.IsOk() ||
1146 m_searchMenuBitmap.GetHeight() != bitmapHeight ||
1147 m_searchMenuBitmap.GetWidth() != bitmapWidth
1148 )
1149 {
1150 m_searchMenuBitmap = RenderSearchBitmap(bitmapWidth,bitmapHeight,true);
1151 if ( m_menu )
1152 {
1153 m_searchButton->SetBitmapLabel(m_searchMenuBitmap);
1154 }
1155 }
1156 // else this bitmap was set by user, don't alter
1157 }
1158 #endif // wxUSE_MENUS
1159
1160 if ( !m_cancelBitmapUser )
1161 {
1162 if (
1163 !m_cancelBitmap.IsOk() ||
1164 m_cancelBitmap.GetHeight() != bitmapHeight ||
1165 m_cancelBitmap.GetWidth() != bitmapHeight
1166 )
1167 {
1168 m_cancelBitmap = RenderCancelBitmap(bitmapHeight-BORDER-1,bitmapHeight-BORDER-1); // square
1169 m_cancelButton->SetBitmapLabel(m_cancelBitmap);
1170 }
1171 // else this bitmap was set by user, don't alter
1172 }
1173 }
1174
1175 void wxSearchCtrl::OnSearchButton( wxCommandEvent& event )
1176 {
1177 event.Skip();
1178 }
1179
1180 void wxSearchCtrl::OnSetFocus( wxFocusEvent& /*event*/ )
1181 {
1182 if ( m_text )
1183 {
1184 m_text->SetFocus();
1185 }
1186 }
1187
1188 void wxSearchCtrl::OnSize( wxSizeEvent& WXUNUSED(event) )
1189 {
1190 int width, height;
1191 GetSize(&width, &height);
1192 LayoutControls(0, 0, width, height);
1193 }
1194
1195 #if wxUSE_MENUS
1196
1197 void wxSearchCtrl::PopupSearchMenu()
1198 {
1199 if ( m_menu )
1200 {
1201 wxSize size = GetSize();
1202 PopupMenu( m_menu, 0, size.y );
1203 }
1204 }
1205
1206 #endif // wxUSE_MENUS
1207
1208 #endif // !wxUSE_NATIVE_SEARCH_CONTROL
1209
1210 #endif // wxUSE_SEARCHCTRL