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