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