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