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