Fix generic wxSearchCtrl best size calculation.
[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 SetForegroundColour( m_text->GetForegroundColour() );
326 SetBackgroundColour( m_text->GetBackgroundColour() );
327
328 RecalcBitmaps();
329
330 SetInitialSize(size);
331 Move(pos);
332 return true;
333 }
334
335 wxSearchCtrl::~wxSearchCtrl()
336 {
337 delete m_text;
338 delete m_searchButton;
339 delete m_cancelButton;
340 #if wxUSE_MENUS
341 delete m_menu;
342 #endif // wxUSE_MENUS
343 }
344
345
346 // search control specific interfaces
347 #if wxUSE_MENUS
348
349 void wxSearchCtrl::SetMenu( wxMenu* menu )
350 {
351 if ( menu == m_menu )
352 {
353 // no change
354 return;
355 }
356 bool hadMenu = (m_menu != NULL);
357 delete m_menu;
358 m_menu = menu;
359
360 if ( m_menu && !hadMenu )
361 {
362 m_searchButton->SetBitmapLabel(m_searchMenuBitmap);
363 m_searchButton->Refresh();
364 }
365 else if ( !m_menu && hadMenu )
366 {
367 m_searchButton->SetBitmapLabel(m_searchBitmap);
368 if ( m_searchButtonVisible )
369 {
370 m_searchButton->Refresh();
371 }
372 }
373 wxRect rect = GetRect();
374 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
375 }
376
377 wxMenu* wxSearchCtrl::GetMenu()
378 {
379 return m_menu;
380 }
381
382 #endif // wxUSE_MENUS
383
384 void wxSearchCtrl::ShowSearchButton( bool show )
385 {
386 if ( m_searchButtonVisible == show )
387 {
388 // no change
389 return;
390 }
391 m_searchButtonVisible = show;
392 if ( m_searchButtonVisible )
393 {
394 RecalcBitmaps();
395 }
396
397 wxRect rect = GetRect();
398 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
399 }
400
401 bool wxSearchCtrl::IsSearchButtonVisible() const
402 {
403 return m_searchButtonVisible;
404 }
405
406
407 void wxSearchCtrl::ShowCancelButton( bool show )
408 {
409 if ( m_cancelButtonVisible == show )
410 {
411 // no change
412 return;
413 }
414 m_cancelButtonVisible = show;
415
416 wxRect rect = GetRect();
417 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
418 }
419
420 bool wxSearchCtrl::IsCancelButtonVisible() const
421 {
422 return m_cancelButtonVisible;
423 }
424
425 void wxSearchCtrl::SetDescriptiveText(const wxString& text)
426 {
427 m_text->SetHint(text);
428 }
429
430 wxString wxSearchCtrl::GetDescriptiveText() const
431 {
432 return m_text->GetHint();
433 }
434
435 // ----------------------------------------------------------------------------
436 // geometry
437 // ----------------------------------------------------------------------------
438
439 wxSize wxSearchCtrl::DoGetBestSize() const
440 {
441 wxSize sizeText = m_text->GetBestSize();
442 wxSize sizeSearch(0,0);
443 wxSize sizeCancel(0,0);
444 int searchMargin = 0;
445 int cancelMargin = 0;
446 if ( m_searchButtonVisible || HasMenu() )
447 {
448 sizeSearch = m_searchButton->GetBestSize();
449 searchMargin = MARGIN;
450 }
451 if ( m_cancelButtonVisible )
452 {
453 sizeCancel = m_cancelButton->GetBestSize();
454 cancelMargin = MARGIN;
455 }
456
457 int horizontalBorder = 1 + ( sizeText.y - sizeText.y * 14 / 21 ) / 2;
458
459 // buttons are square and equal to the height of the text control
460 int height = sizeText.y;
461 return wxSize(sizeSearch.x + searchMargin + sizeText.x + cancelMargin + sizeCancel.x + 2*horizontalBorder,
462 height + 2*BORDER);
463 }
464
465 void wxSearchCtrl::DoMoveWindow(int x, int y, int width, int height)
466 {
467 wxSearchCtrlBase::DoMoveWindow(x, y, width, height);
468
469 LayoutControls(0, 0, width, height);
470 }
471
472 void wxSearchCtrl::LayoutControls(int x, int y, int width, int height)
473 {
474 if ( !m_text )
475 return;
476
477 wxSize sizeText = m_text->GetBestSize();
478 // make room for the search menu & clear button
479 int horizontalBorder = ( sizeText.y - sizeText.y * 14 / 21 ) / 2;
480 x += horizontalBorder;
481 y += BORDER;
482 width -= horizontalBorder*2;
483 height -= BORDER*2;
484 if (width < 0) width = 0;
485 if (height < 0) height = 0;
486
487 wxSize sizeSearch(0,0);
488 wxSize sizeCancel(0,0);
489 int searchMargin = 0;
490 int cancelMargin = 0;
491 if ( m_searchButtonVisible || HasMenu() )
492 {
493 sizeSearch = m_searchButton->GetBestSize();
494 searchMargin = MARGIN;
495 }
496 if ( m_cancelButtonVisible )
497 {
498 sizeCancel = m_cancelButton->GetBestSize();
499 cancelMargin = MARGIN;
500 }
501 m_searchButton->Show( m_searchButtonVisible || HasMenu() );
502 m_cancelButton->Show( m_cancelButtonVisible );
503
504 if ( sizeSearch.x + sizeCancel.x > width )
505 {
506 sizeSearch.x = width/2;
507 sizeCancel.x = width/2;
508 searchMargin = 0;
509 cancelMargin = 0;
510 }
511 wxCoord textWidth = width - sizeSearch.x - sizeCancel.x - searchMargin - cancelMargin - 1;
512 if (textWidth < 0) textWidth = 0;
513
514 // position the subcontrols inside the client area
515
516 m_searchButton->SetSize(x, y + ICON_OFFSET - 1, sizeSearch.x, height);
517 m_text->SetSize( x + sizeSearch.x + searchMargin,
518 y + ICON_OFFSET - BORDER,
519 textWidth,
520 height);
521 m_cancelButton->SetSize(x + sizeSearch.x + searchMargin + textWidth + cancelMargin,
522 y + ICON_OFFSET - 1, sizeCancel.x, height);
523 }
524
525 wxWindowList wxSearchCtrl::GetCompositeWindowParts() const
526 {
527 wxWindowList parts;
528 parts.push_back(m_text);
529 parts.push_back(m_searchButton);
530 parts.push_back(m_cancelButton);
531 return parts;
532 }
533
534 // accessors
535 // ---------
536
537 wxString wxSearchCtrl::DoGetValue() const
538 {
539 return m_text->GetValue();
540 }
541 wxString wxSearchCtrl::GetRange(long from, long to) const
542 {
543 return m_text->GetRange(from, to);
544 }
545
546 int wxSearchCtrl::GetLineLength(long lineNo) const
547 {
548 return m_text->GetLineLength(lineNo);
549 }
550 wxString wxSearchCtrl::GetLineText(long lineNo) const
551 {
552 return m_text->GetLineText(lineNo);
553 }
554 int wxSearchCtrl::GetNumberOfLines() const
555 {
556 return m_text->GetNumberOfLines();
557 }
558
559 bool wxSearchCtrl::IsModified() const
560 {
561 return m_text->IsModified();
562 }
563 bool wxSearchCtrl::IsEditable() const
564 {
565 return m_text->IsEditable();
566 }
567
568 // more readable flag testing methods
569 bool wxSearchCtrl::IsSingleLine() const
570 {
571 return m_text->IsSingleLine();
572 }
573 bool wxSearchCtrl::IsMultiLine() const
574 {
575 return m_text->IsMultiLine();
576 }
577
578 // If the return values from and to are the same, there is no selection.
579 void wxSearchCtrl::GetSelection(long* from, long* to) const
580 {
581 m_text->GetSelection(from, to);
582 }
583
584 wxString wxSearchCtrl::GetStringSelection() const
585 {
586 return m_text->GetStringSelection();
587 }
588
589 // operations
590 // ----------
591
592 // editing
593 void wxSearchCtrl::Clear()
594 {
595 m_text->Clear();
596 }
597 void wxSearchCtrl::Replace(long from, long to, const wxString& value)
598 {
599 m_text->Replace(from, to, value);
600 }
601 void wxSearchCtrl::Remove(long from, long to)
602 {
603 m_text->Remove(from, to);
604 }
605
606 // load/save the controls contents from/to the file
607 bool wxSearchCtrl::LoadFile(const wxString& file)
608 {
609 return m_text->LoadFile(file);
610 }
611 bool wxSearchCtrl::SaveFile(const wxString& file)
612 {
613 return m_text->SaveFile(file);
614 }
615
616 // sets/clears the dirty flag
617 void wxSearchCtrl::MarkDirty()
618 {
619 m_text->MarkDirty();
620 }
621 void wxSearchCtrl::DiscardEdits()
622 {
623 m_text->DiscardEdits();
624 }
625
626 // set the max number of characters which may be entered in a single line
627 // text control
628 void wxSearchCtrl::SetMaxLength(unsigned long len)
629 {
630 m_text->SetMaxLength(len);
631 }
632
633 // writing text inserts it at the current position, appending always
634 // inserts it at the end
635 void wxSearchCtrl::WriteText(const wxString& text)
636 {
637 m_text->WriteText(text);
638 }
639 void wxSearchCtrl::AppendText(const wxString& text)
640 {
641 m_text->AppendText(text);
642 }
643
644 // insert the character which would have resulted from this key event,
645 // return true if anything has been inserted
646 bool wxSearchCtrl::EmulateKeyPress(const wxKeyEvent& event)
647 {
648 return m_text->EmulateKeyPress(event);
649 }
650
651 // text control under some platforms supports the text styles: these
652 // methods allow to apply the given text style to the given selection or to
653 // set/get the style which will be used for all appended text
654 bool wxSearchCtrl::SetStyle(long start, long end, const wxTextAttr& style)
655 {
656 return m_text->SetStyle(start, end, style);
657 }
658 bool wxSearchCtrl::GetStyle(long position, wxTextAttr& style)
659 {
660 return m_text->GetStyle(position, style);
661 }
662 bool wxSearchCtrl::SetDefaultStyle(const wxTextAttr& style)
663 {
664 return m_text->SetDefaultStyle(style);
665 }
666 const wxTextAttr& wxSearchCtrl::GetDefaultStyle() const
667 {
668 return m_text->GetDefaultStyle();
669 }
670
671 // translate between the position (which is just an index in the text ctrl
672 // considering all its contents as a single strings) and (x, y) coordinates
673 // which represent column and line.
674 long wxSearchCtrl::XYToPosition(long x, long y) const
675 {
676 return m_text->XYToPosition(x, y);
677 }
678 bool wxSearchCtrl::PositionToXY(long pos, long *x, long *y) const
679 {
680 return m_text->PositionToXY(pos, x, y);
681 }
682
683 void wxSearchCtrl::ShowPosition(long pos)
684 {
685 m_text->ShowPosition(pos);
686 }
687
688 // find the character at position given in pixels
689 //
690 // NB: pt is in device coords (not adjusted for the client area origin nor
691 // scrolling)
692 wxTextCtrlHitTestResult wxSearchCtrl::HitTest(const wxPoint& pt, long *pos) const
693 {
694 return m_text->HitTest(pt, pos);
695 }
696 wxTextCtrlHitTestResult wxSearchCtrl::HitTest(const wxPoint& pt,
697 wxTextCoord *col,
698 wxTextCoord *row) const
699 {
700 return m_text->HitTest(pt, col, row);
701 }
702
703 // Clipboard operations
704 void wxSearchCtrl::Copy()
705 {
706 m_text->Copy();
707 }
708 void wxSearchCtrl::Cut()
709 {
710 m_text->Cut();
711 }
712 void wxSearchCtrl::Paste()
713 {
714 m_text->Paste();
715 }
716
717 bool wxSearchCtrl::CanCopy() const
718 {
719 return m_text->CanCopy();
720 }
721 bool wxSearchCtrl::CanCut() const
722 {
723 return m_text->CanCut();
724 }
725 bool wxSearchCtrl::CanPaste() const
726 {
727 return m_text->CanPaste();
728 }
729
730 // Undo/redo
731 void wxSearchCtrl::Undo()
732 {
733 m_text->Undo();
734 }
735 void wxSearchCtrl::Redo()
736 {
737 m_text->Redo();
738 }
739
740 bool wxSearchCtrl::CanUndo() const
741 {
742 return m_text->CanUndo();
743 }
744 bool wxSearchCtrl::CanRedo() const
745 {
746 return m_text->CanRedo();
747 }
748
749 // Insertion point
750 void wxSearchCtrl::SetInsertionPoint(long pos)
751 {
752 m_text->SetInsertionPoint(pos);
753 }
754 void wxSearchCtrl::SetInsertionPointEnd()
755 {
756 m_text->SetInsertionPointEnd();
757 }
758 long wxSearchCtrl::GetInsertionPoint() const
759 {
760 return m_text->GetInsertionPoint();
761 }
762 long wxSearchCtrl::GetLastPosition() const
763 {
764 return m_text->GetLastPosition();
765 }
766
767 void wxSearchCtrl::SetSelection(long from, long to)
768 {
769 m_text->SetSelection(from, to);
770 }
771 void wxSearchCtrl::SelectAll()
772 {
773 m_text->SelectAll();
774 }
775
776 void wxSearchCtrl::SetEditable(bool editable)
777 {
778 m_text->SetEditable(editable);
779 }
780
781 bool wxSearchCtrl::SetFont(const wxFont& font)
782 {
783 if ( !wxSearchCtrlBase::SetFont(font) )
784 return false;
785
786 // Recreate the bitmaps as their size may have changed.
787 RecalcBitmaps();
788
789 return true;
790 }
791
792 bool wxSearchCtrl::SetBackgroundColour(const wxColour& colour)
793 {
794 if ( !wxSearchCtrlBase::SetBackgroundColour(colour) )
795 return false;
796
797 // When the background changes, re-render the bitmaps so that the correct
798 // colour shows in their "transparent" area.
799 RecalcBitmaps();
800
801 return true;
802 }
803
804 // search control generic only
805 void wxSearchCtrl::SetSearchBitmap( const wxBitmap& bitmap )
806 {
807 m_searchBitmap = bitmap;
808 m_searchBitmapUser = bitmap.IsOk();
809 if ( m_searchBitmapUser )
810 {
811 if ( m_searchButton && !HasMenu() )
812 {
813 m_searchButton->SetBitmapLabel( m_searchBitmap );
814 }
815 }
816 else
817 {
818 // the user bitmap was just cleared, generate one
819 RecalcBitmaps();
820 }
821 }
822
823 #if wxUSE_MENUS
824
825 void wxSearchCtrl::SetSearchMenuBitmap( const wxBitmap& bitmap )
826 {
827 m_searchMenuBitmap = bitmap;
828 m_searchMenuBitmapUser = bitmap.IsOk();
829 if ( m_searchMenuBitmapUser )
830 {
831 if ( m_searchButton && m_menu )
832 {
833 m_searchButton->SetBitmapLabel( m_searchMenuBitmap );
834 }
835 }
836 else
837 {
838 // the user bitmap was just cleared, generate one
839 RecalcBitmaps();
840 }
841 }
842
843 #endif // wxUSE_MENUS
844
845 void wxSearchCtrl::SetCancelBitmap( const wxBitmap& bitmap )
846 {
847 m_cancelBitmap = bitmap;
848 m_cancelBitmapUser = bitmap.IsOk();
849 if ( m_cancelBitmapUser )
850 {
851 if ( m_cancelButton )
852 {
853 m_cancelButton->SetBitmapLabel( m_cancelBitmap );
854 }
855 }
856 else
857 {
858 // the user bitmap was just cleared, generate one
859 RecalcBitmaps();
860 }
861 }
862
863 #if 0
864
865 // override streambuf method
866 #if wxHAS_TEXT_WINDOW_STREAM
867 int overflow(int i);
868 #endif // wxHAS_TEXT_WINDOW_STREAM
869
870 // stream-like insertion operators: these are always available, whether we
871 // were, or not, compiled with streambuf support
872 wxTextCtrl& operator<<(const wxString& s);
873 wxTextCtrl& operator<<(int i);
874 wxTextCtrl& operator<<(long i);
875 wxTextCtrl& operator<<(float f);
876 wxTextCtrl& operator<<(double d);
877 wxTextCtrl& operator<<(const wxChar c);
878 #endif
879
880 void wxSearchCtrl::DoSetValue(const wxString& value, int flags)
881 {
882 m_text->DoSetValue(value, flags);
883 }
884
885 bool wxSearchCtrl::DoLoadFile(const wxString& file, int fileType)
886 {
887 return m_text->DoLoadFile(file, fileType);
888 }
889
890 bool wxSearchCtrl::DoSaveFile(const wxString& file, int fileType)
891 {
892 return m_text->DoSaveFile(file, fileType);
893 }
894
895 // do the window-specific processing after processing the update event
896 void wxSearchCtrl::DoUpdateWindowUI(wxUpdateUIEvent& event)
897 {
898 wxSearchCtrlBase::DoUpdateWindowUI(event);
899 }
900
901 bool wxSearchCtrl::ShouldInheritColours() const
902 {
903 return true;
904 }
905
906 // icons are rendered at 3-8 times larger than necessary and downscaled for
907 // antialiasing
908 static int GetMultiplier()
909 {
910 #ifdef __WXWINCE__
911 // speed up bitmap generation by using a small bitmap
912 return 3;
913 #else
914 int depth = ::wxDisplayDepth();
915
916 if ( depth >= 24 )
917 {
918 return 8;
919 }
920 return 6;
921 #endif
922 }
923
924 wxBitmap wxSearchCtrl::RenderSearchBitmap( int x, int y, bool renderDrop )
925 {
926 wxColour bg = GetBackgroundColour();
927 wxColour fg = GetForegroundColour().ChangeLightness(LIGHT_STEP-20);
928
929 //===============================================================================
930 // begin drawing code
931 //===============================================================================
932 // image stats
933
934 // force width:height ratio
935 if ( 14*x > y*20 )
936 {
937 // x is too big
938 x = y*20/14;
939 }
940 else
941 {
942 // y is too big
943 y = x*14/20;
944 }
945
946 // glass 11x11, top left corner
947 // handle (9,9)-(13,13)
948 // drop (13,16)-(19,6)-(16,9)
949
950 int multiplier = GetMultiplier();
951 int penWidth = multiplier * 2;
952
953 penWidth = penWidth * x / 20;
954
955 wxBitmap bitmap( multiplier*x, multiplier*y );
956 wxMemoryDC mem;
957 mem.SelectObject(bitmap);
958
959 // clear background
960 mem.SetBrush( wxBrush(bg) );
961 mem.SetPen( wxPen(bg) );
962 mem.DrawRectangle(0,0,bitmap.GetWidth(),bitmap.GetHeight());
963
964 // draw drop glass
965 mem.SetBrush( wxBrush(fg) );
966 mem.SetPen( wxPen(fg) );
967 int glassBase = 5 * x / 20;
968 int glassFactor = 2*glassBase + 1;
969 int radius = multiplier*glassFactor/2;
970 mem.DrawCircle(radius,radius,radius);
971 mem.SetBrush( wxBrush(bg) );
972 mem.SetPen( wxPen(bg) );
973 mem.DrawCircle(radius,radius,radius-penWidth);
974
975 // draw handle
976 int lineStart = radius + (radius-penWidth/2) * 707 / 1000; // 707 / 1000 = 0.707 = 1/sqrt(2);
977
978 mem.SetPen( wxPen(fg) );
979 mem.SetBrush( wxBrush(fg) );
980 int handleCornerShift = penWidth * 707 / 1000 / 2; // 707 / 1000 = 0.707 = 1/sqrt(2);
981 handleCornerShift = WXMAX( handleCornerShift, 1 );
982 int handleBase = 4 * x / 20;
983 int handleLength = 2*handleBase+1;
984 wxPoint handlePolygon[] =
985 {
986 wxPoint(-handleCornerShift,+handleCornerShift),
987 wxPoint(+handleCornerShift,-handleCornerShift),
988 wxPoint(multiplier*handleLength/2+handleCornerShift,multiplier*handleLength/2-handleCornerShift),
989 wxPoint(multiplier*handleLength/2-handleCornerShift,multiplier*handleLength/2+handleCornerShift),
990 };
991 mem.DrawPolygon(WXSIZEOF(handlePolygon),handlePolygon,lineStart,lineStart);
992
993 // draw drop triangle
994 int triangleX = 13 * x / 20;
995 int triangleY = 5 * x / 20;
996 int triangleBase = 3 * x / 20;
997 int triangleFactor = triangleBase*2+1;
998 if ( renderDrop )
999 {
1000 wxPoint dropPolygon[] =
1001 {
1002 wxPoint(multiplier*0,multiplier*0), // triangle left
1003 wxPoint(multiplier*triangleFactor-1,multiplier*0), // triangle right
1004 wxPoint(multiplier*triangleFactor/2,multiplier*triangleFactor/2), // triangle bottom
1005 };
1006 mem.DrawPolygon(WXSIZEOF(dropPolygon),dropPolygon,multiplier*triangleX,multiplier*triangleY);
1007 }
1008 mem.SelectObject(wxNullBitmap);
1009
1010 //===============================================================================
1011 // end drawing code
1012 //===============================================================================
1013
1014 if ( multiplier != 1 )
1015 {
1016 wxImage image = bitmap.ConvertToImage();
1017 image.Rescale(x,y);
1018 bitmap = wxBitmap( image );
1019 }
1020 if ( !renderDrop )
1021 {
1022 // Trim the edge where the arrow would have gone
1023 bitmap = bitmap.GetSubBitmap(wxRect(0,0, y,y));
1024 }
1025
1026 return bitmap;
1027 }
1028
1029 wxBitmap wxSearchCtrl::RenderCancelBitmap( int x, int y )
1030 {
1031 wxColour bg = GetBackgroundColour();
1032 wxColour fg = GetForegroundColour().ChangeLightness(LIGHT_STEP);
1033
1034 //===============================================================================
1035 // begin drawing code
1036 //===============================================================================
1037 // image stats
1038
1039 // total size 14x14
1040 // force 1:1 ratio
1041 if ( x > y )
1042 {
1043 // x is too big
1044 x = y;
1045 }
1046 else
1047 {
1048 // y is too big
1049 y = x;
1050 }
1051
1052 // 14x14 circle
1053 // cross line starts (4,4)-(10,10)
1054 // drop (13,16)-(19,6)-(16,9)
1055
1056 int multiplier = GetMultiplier();
1057
1058 int penWidth = multiplier * x / 14;
1059
1060 wxBitmap bitmap( multiplier*x, multiplier*y );
1061 wxMemoryDC mem;
1062 mem.SelectObject(bitmap);
1063
1064 // clear background
1065 mem.SetBrush( wxBrush(bg) );
1066 mem.SetPen( wxPen(bg) );
1067 mem.DrawRectangle(0,0,bitmap.GetWidth(),bitmap.GetHeight());
1068
1069 // draw drop glass
1070 mem.SetBrush( wxBrush(fg) );
1071 mem.SetPen( wxPen(fg) );
1072 int radius = multiplier*x/2;
1073 mem.DrawCircle(radius,radius,radius);
1074
1075 // draw cross
1076 int lineStartBase = 4 * x / 14;
1077 int lineLength = x - 2*lineStartBase;
1078
1079 mem.SetPen( wxPen(bg) );
1080 mem.SetBrush( wxBrush(bg) );
1081 int handleCornerShift = penWidth/2;
1082 handleCornerShift = WXMAX( handleCornerShift, 1 );
1083 wxPoint handlePolygon[] =
1084 {
1085 wxPoint(-handleCornerShift,+handleCornerShift),
1086 wxPoint(+handleCornerShift,-handleCornerShift),
1087 wxPoint(multiplier*lineLength+handleCornerShift,multiplier*lineLength-handleCornerShift),
1088 wxPoint(multiplier*lineLength-handleCornerShift,multiplier*lineLength+handleCornerShift),
1089 };
1090 mem.DrawPolygon(WXSIZEOF(handlePolygon),handlePolygon,multiplier*lineStartBase,multiplier*lineStartBase);
1091 wxPoint handlePolygon2[] =
1092 {
1093 wxPoint(+handleCornerShift,+handleCornerShift),
1094 wxPoint(-handleCornerShift,-handleCornerShift),
1095 wxPoint(multiplier*lineLength-handleCornerShift,-multiplier*lineLength-handleCornerShift),
1096 wxPoint(multiplier*lineLength+handleCornerShift,-multiplier*lineLength+handleCornerShift),
1097 };
1098 mem.DrawPolygon(WXSIZEOF(handlePolygon2),handlePolygon2,multiplier*lineStartBase,multiplier*(x-lineStartBase));
1099
1100 //===============================================================================
1101 // end drawing code
1102 //===============================================================================
1103
1104 if ( multiplier != 1 )
1105 {
1106 wxImage image = bitmap.ConvertToImage();
1107 image.Rescale(x,y);
1108 bitmap = wxBitmap( image );
1109 }
1110
1111 return bitmap;
1112 }
1113
1114 void wxSearchCtrl::RecalcBitmaps()
1115 {
1116 if ( !m_text )
1117 {
1118 return;
1119 }
1120 wxSize sizeText = m_text->GetBestSize();
1121
1122 int bitmapHeight = sizeText.y - 2 * ICON_MARGIN;
1123 int bitmapWidth = sizeText.y * 20 / 14;
1124
1125 if ( !m_searchBitmapUser )
1126 {
1127 if (
1128 !m_searchBitmap.IsOk() ||
1129 m_searchBitmap.GetHeight() != bitmapHeight ||
1130 m_searchBitmap.GetWidth() != bitmapWidth
1131 )
1132 {
1133 m_searchBitmap = RenderSearchBitmap(bitmapWidth,bitmapHeight,false);
1134 if ( !HasMenu() )
1135 {
1136 m_searchButton->SetBitmapLabel(m_searchBitmap);
1137 }
1138 }
1139 // else this bitmap was set by user, don't alter
1140 }
1141
1142 #if wxUSE_MENUS
1143 if ( !m_searchMenuBitmapUser )
1144 {
1145 if (
1146 !m_searchMenuBitmap.IsOk() ||
1147 m_searchMenuBitmap.GetHeight() != bitmapHeight ||
1148 m_searchMenuBitmap.GetWidth() != bitmapWidth
1149 )
1150 {
1151 m_searchMenuBitmap = RenderSearchBitmap(bitmapWidth,bitmapHeight,true);
1152 if ( m_menu )
1153 {
1154 m_searchButton->SetBitmapLabel(m_searchMenuBitmap);
1155 }
1156 }
1157 // else this bitmap was set by user, don't alter
1158 }
1159 #endif // wxUSE_MENUS
1160
1161 if ( !m_cancelBitmapUser )
1162 {
1163 if (
1164 !m_cancelBitmap.IsOk() ||
1165 m_cancelBitmap.GetHeight() != bitmapHeight ||
1166 m_cancelBitmap.GetWidth() != bitmapHeight
1167 )
1168 {
1169 m_cancelBitmap = RenderCancelBitmap(bitmapHeight-BORDER-1,bitmapHeight-BORDER-1); // square
1170 m_cancelButton->SetBitmapLabel(m_cancelBitmap);
1171 }
1172 // else this bitmap was set by user, don't alter
1173 }
1174 }
1175
1176 void wxSearchCtrl::OnSearchButton( wxCommandEvent& event )
1177 {
1178 event.Skip();
1179 }
1180
1181 void wxSearchCtrl::OnSetFocus( wxFocusEvent& /*event*/ )
1182 {
1183 if ( m_text )
1184 {
1185 m_text->SetFocus();
1186 }
1187 }
1188
1189 void wxSearchCtrl::OnSize( wxSizeEvent& WXUNUSED(event) )
1190 {
1191 int width, height;
1192 GetSize(&width, &height);
1193 LayoutControls(0, 0, width, height);
1194 }
1195
1196 #if wxUSE_MENUS
1197
1198 void wxSearchCtrl::PopupSearchMenu()
1199 {
1200 if ( m_menu )
1201 {
1202 wxSize size = GetSize();
1203 PopupMenu( m_menu, 0, size.y );
1204 }
1205 }
1206
1207 #endif // wxUSE_MENUS
1208
1209 #endif // !wxUSE_NATIVE_SEARCH_CONTROL
1210
1211 #endif // wxUSE_SEARCHCTRL