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