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