]> git.saurik.com Git - wxWidgets.git/blob - src/generic/srchctlg.cpp
Corrected bug in in revision 47973
[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 #elif defined(__WXMSW__)
329 // Don't set the style explicitly, let GetDefaultBorder() work it out, unless
330 // we will get a sunken border (e.g. on Windows 200) in which case we must
331 // override with a simple border.
332 if (GetDefaultBorder() == wxBORDER_SUNKEN)
333 style |= wxBORDER_SIMPLE;
334 #else
335 style |= wxBORDER_SIMPLE;
336 #endif
337 if ( !wxTextCtrlBase::Create(parent, id, pos, size, style, validator, name) )
338 {
339 return false;
340 }
341
342 m_text = new wxSearchTextCtrl(this, value, style & ~wxBORDER_MASK);
343 m_text->SetDescriptiveText(_("Search"));
344
345 wxSize sizeText = m_text->GetBestSize();
346
347 m_searchButton = new wxSearchButton(this,
348 wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN,
349 m_searchBitmap);
350 m_cancelButton = new wxSearchButton(this,
351 wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN,
352 m_cancelBitmap);
353
354 SetForegroundColour( m_text->GetForegroundColour() );
355 m_searchButton->SetForegroundColour( m_text->GetForegroundColour() );
356 m_cancelButton->SetForegroundColour( m_text->GetForegroundColour() );
357
358 SetBackgroundColour( m_text->GetBackgroundColour() );
359 m_searchButton->SetBackgroundColour( m_text->GetBackgroundColour() );
360 m_cancelButton->SetBackgroundColour( m_text->GetBackgroundColour() );
361
362 RecalcBitmaps();
363
364 SetInitialSize(size);
365 Move(pos);
366 return true;
367 }
368
369 wxSearchCtrl::~wxSearchCtrl()
370 {
371 delete m_text;
372 delete m_searchButton;
373 delete m_cancelButton;
374 #if wxUSE_MENUS
375 delete m_menu;
376 #endif // wxUSE_MENUS
377 }
378
379
380 // search control specific interfaces
381 #if wxUSE_MENUS
382
383 void wxSearchCtrl::SetMenu( wxMenu* menu )
384 {
385 if ( menu == m_menu )
386 {
387 // no change
388 return;
389 }
390 bool hadMenu = (m_menu != NULL);
391 delete m_menu;
392 m_menu = menu;
393
394 if ( m_menu && !hadMenu )
395 {
396 m_searchButton->SetBitmapLabel(m_searchMenuBitmap);
397 m_searchButton->Refresh();
398 }
399 else if ( !m_menu && hadMenu )
400 {
401 m_searchButton->SetBitmapLabel(m_searchBitmap);
402 if ( m_searchButtonVisible )
403 {
404 m_searchButton->Refresh();
405 }
406 }
407 wxRect rect = GetRect();
408 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
409 }
410
411 wxMenu* wxSearchCtrl::GetMenu()
412 {
413 return m_menu;
414 }
415
416 #endif // wxUSE_MENUS
417
418 void wxSearchCtrl::ShowSearchButton( bool show )
419 {
420 if ( m_searchButtonVisible == show )
421 {
422 // no change
423 return;
424 }
425 m_searchButtonVisible = show;
426 if ( m_searchButtonVisible )
427 {
428 RecalcBitmaps();
429 }
430
431 wxRect rect = GetRect();
432 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
433 }
434
435 bool wxSearchCtrl::IsSearchButtonVisible() const
436 {
437 return m_searchButtonVisible;
438 }
439
440
441 void wxSearchCtrl::ShowCancelButton( bool show )
442 {
443 if ( m_cancelButtonVisible == show )
444 {
445 // no change
446 return;
447 }
448 m_cancelButtonVisible = show;
449
450 wxRect rect = GetRect();
451 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
452 }
453
454 bool wxSearchCtrl::IsCancelButtonVisible() const
455 {
456 return m_cancelButtonVisible;
457 }
458
459 void wxSearchCtrl::SetDescriptiveText(const wxString& text)
460 {
461 m_text->SetDescriptiveText(text);
462 }
463
464 wxString wxSearchCtrl::GetDescriptiveText() const
465 {
466 return m_text->GetDescriptiveText();
467 }
468
469 // ----------------------------------------------------------------------------
470 // geometry
471 // ----------------------------------------------------------------------------
472
473 wxSize wxSearchCtrl::DoGetBestSize() const
474 {
475 wxSize sizeText = m_text->GetBestSize();
476 wxSize sizeSearch(0,0);
477 wxSize sizeCancel(0,0);
478 int searchMargin = 0;
479 int cancelMargin = 0;
480 if ( m_searchButtonVisible || HasMenu() )
481 {
482 sizeSearch = m_searchButton->GetBestSize();
483 searchMargin = MARGIN;
484 }
485 if ( m_cancelButtonVisible )
486 {
487 sizeCancel = m_cancelButton->GetBestSize();
488 cancelMargin = MARGIN;
489 }
490
491 int horizontalBorder = 1 + ( sizeText.y - sizeText.y * 14 / 21 ) / 2;
492
493 // buttons are square and equal to the height of the text control
494 int height = sizeText.y;
495 return wxSize(sizeSearch.x + searchMargin + sizeText.x + cancelMargin + sizeCancel.x + 2*horizontalBorder,
496 height + 2*BORDER);
497 }
498
499 void wxSearchCtrl::DoMoveWindow(int x, int y, int width, int height)
500 {
501 wxSearchCtrlBase::DoMoveWindow(x, y, width, height);
502
503 LayoutControls(0, 0, width, height);
504 }
505
506 void wxSearchCtrl::LayoutControls(int x, int y, int width, int height)
507 {
508 if ( !m_text )
509 return;
510
511 wxSize sizeText = m_text->GetBestSize();
512 // make room for the search menu & clear button
513 int horizontalBorder = ( sizeText.y - sizeText.y * 14 / 21 ) / 2;
514 x += horizontalBorder;
515 y += BORDER;
516 width -= horizontalBorder*2;
517 height -= BORDER*2;
518
519 wxSize sizeSearch(0,0);
520 wxSize sizeCancel(0,0);
521 int searchMargin = 0;
522 int cancelMargin = 0;
523 if ( m_searchButtonVisible || HasMenu() )
524 {
525 sizeSearch = m_searchButton->GetBestSize();
526 searchMargin = MARGIN;
527 }
528 if ( m_cancelButtonVisible )
529 {
530 sizeCancel = m_cancelButton->GetBestSize();
531 cancelMargin = MARGIN;
532 }
533 m_searchButton->Show( m_searchButtonVisible || HasMenu() );
534 m_cancelButton->Show( m_cancelButtonVisible );
535
536 if ( sizeSearch.x + sizeCancel.x > width )
537 {
538 sizeSearch.x = width/2;
539 sizeCancel.x = width/2;
540 searchMargin = 0;
541 cancelMargin = 0;
542 }
543 wxCoord textWidth = width - sizeSearch.x - sizeCancel.x - searchMargin - cancelMargin - 1;
544
545 // position the subcontrols inside the client area
546
547 m_searchButton->SetSize(x, y + ICON_OFFSET - 1, sizeSearch.x, height);
548 m_text->SetSize( x + sizeSearch.x + searchMargin,
549 y + ICON_OFFSET - BORDER,
550 textWidth,
551 height);
552 m_cancelButton->SetSize(x + sizeSearch.x + searchMargin + textWidth + cancelMargin,
553 y + ICON_OFFSET - 1, sizeCancel.x, height);
554 }
555
556
557 // accessors
558 // ---------
559
560 wxString wxSearchCtrl::GetValue() const
561 {
562 wxString value = m_text->GetValue();
563 if (value == m_text->GetDescriptiveText())
564 return wxEmptyString;
565 else
566 return value;
567 }
568 void wxSearchCtrl::SetValue(const wxString& value)
569 {
570 m_text->SetValue(value);
571 }
572
573 wxString wxSearchCtrl::GetRange(long from, long to) const
574 {
575 return m_text->GetRange(from, to);
576 }
577
578 int wxSearchCtrl::GetLineLength(long lineNo) const
579 {
580 return m_text->GetLineLength(lineNo);
581 }
582 wxString wxSearchCtrl::GetLineText(long lineNo) const
583 {
584 return m_text->GetLineText(lineNo);
585 }
586 int wxSearchCtrl::GetNumberOfLines() const
587 {
588 return m_text->GetNumberOfLines();
589 }
590
591 bool wxSearchCtrl::IsModified() const
592 {
593 return m_text->IsModified();
594 }
595 bool wxSearchCtrl::IsEditable() const
596 {
597 return m_text->IsEditable();
598 }
599
600 // more readable flag testing methods
601 bool wxSearchCtrl::IsSingleLine() const
602 {
603 return m_text->IsSingleLine();
604 }
605 bool wxSearchCtrl::IsMultiLine() const
606 {
607 return m_text->IsMultiLine();
608 }
609
610 // If the return values from and to are the same, there is no selection.
611 void wxSearchCtrl::GetSelection(long* from, long* to) const
612 {
613 m_text->GetSelection(from, to);
614 }
615
616 wxString wxSearchCtrl::GetStringSelection() const
617 {
618 return m_text->GetStringSelection();
619 }
620
621 // operations
622 // ----------
623
624 // editing
625 void wxSearchCtrl::Clear()
626 {
627 m_text->Clear();
628 }
629 void wxSearchCtrl::Replace(long from, long to, const wxString& value)
630 {
631 m_text->Replace(from, to, value);
632 }
633 void wxSearchCtrl::Remove(long from, long to)
634 {
635 m_text->Remove(from, to);
636 }
637
638 // load/save the controls contents from/to the file
639 bool wxSearchCtrl::LoadFile(const wxString& file)
640 {
641 return m_text->LoadFile(file);
642 }
643 bool wxSearchCtrl::SaveFile(const wxString& file)
644 {
645 return m_text->SaveFile(file);
646 }
647
648 // sets/clears the dirty flag
649 void wxSearchCtrl::MarkDirty()
650 {
651 m_text->MarkDirty();
652 }
653 void wxSearchCtrl::DiscardEdits()
654 {
655 m_text->DiscardEdits();
656 }
657
658 // set the max number of characters which may be entered in a single line
659 // text control
660 void wxSearchCtrl::SetMaxLength(unsigned long len)
661 {
662 m_text->SetMaxLength(len);
663 }
664
665 // writing text inserts it at the current position, appending always
666 // inserts it at the end
667 void wxSearchCtrl::WriteText(const wxString& text)
668 {
669 m_text->WriteText(text);
670 }
671 void wxSearchCtrl::AppendText(const wxString& text)
672 {
673 m_text->AppendText(text);
674 }
675
676 // insert the character which would have resulted from this key event,
677 // return true if anything has been inserted
678 bool wxSearchCtrl::EmulateKeyPress(const wxKeyEvent& event)
679 {
680 return m_text->EmulateKeyPress(event);
681 }
682
683 // text control under some platforms supports the text styles: these
684 // methods allow to apply the given text style to the given selection or to
685 // set/get the style which will be used for all appended text
686 bool wxSearchCtrl::SetStyle(long start, long end, const wxTextAttr& style)
687 {
688 return m_text->SetStyle(start, end, style);
689 }
690 bool wxSearchCtrl::GetStyle(long position, wxTextAttr& style)
691 {
692 return m_text->GetStyle(position, style);
693 }
694 bool wxSearchCtrl::SetDefaultStyle(const wxTextAttr& style)
695 {
696 return m_text->SetDefaultStyle(style);
697 }
698 const wxTextAttr& wxSearchCtrl::GetDefaultStyle() const
699 {
700 return m_text->GetDefaultStyle();
701 }
702
703 // translate between the position (which is just an index in the text ctrl
704 // considering all its contents as a single strings) and (x, y) coordinates
705 // which represent column and line.
706 long wxSearchCtrl::XYToPosition(long x, long y) const
707 {
708 return m_text->XYToPosition(x, y);
709 }
710 bool wxSearchCtrl::PositionToXY(long pos, long *x, long *y) const
711 {
712 return m_text->PositionToXY(pos, x, y);
713 }
714
715 void wxSearchCtrl::ShowPosition(long pos)
716 {
717 m_text->ShowPosition(pos);
718 }
719
720 // find the character at position given in pixels
721 //
722 // NB: pt is in device coords (not adjusted for the client area origin nor
723 // scrolling)
724 wxTextCtrlHitTestResult wxSearchCtrl::HitTest(const wxPoint& pt, long *pos) const
725 {
726 return m_text->HitTest(pt, pos);
727 }
728 wxTextCtrlHitTestResult wxSearchCtrl::HitTest(const wxPoint& pt,
729 wxTextCoord *col,
730 wxTextCoord *row) const
731 {
732 return m_text->HitTest(pt, col, row);
733 }
734
735 // Clipboard operations
736 void wxSearchCtrl::Copy()
737 {
738 m_text->Copy();
739 }
740 void wxSearchCtrl::Cut()
741 {
742 m_text->Cut();
743 }
744 void wxSearchCtrl::Paste()
745 {
746 m_text->Paste();
747 }
748
749 bool wxSearchCtrl::CanCopy() const
750 {
751 return m_text->CanCopy();
752 }
753 bool wxSearchCtrl::CanCut() const
754 {
755 return m_text->CanCut();
756 }
757 bool wxSearchCtrl::CanPaste() const
758 {
759 return m_text->CanPaste();
760 }
761
762 // Undo/redo
763 void wxSearchCtrl::Undo()
764 {
765 m_text->Undo();
766 }
767 void wxSearchCtrl::Redo()
768 {
769 m_text->Redo();
770 }
771
772 bool wxSearchCtrl::CanUndo() const
773 {
774 return m_text->CanUndo();
775 }
776 bool wxSearchCtrl::CanRedo() const
777 {
778 return m_text->CanRedo();
779 }
780
781 // Insertion point
782 void wxSearchCtrl::SetInsertionPoint(long pos)
783 {
784 m_text->SetInsertionPoint(pos);
785 }
786 void wxSearchCtrl::SetInsertionPointEnd()
787 {
788 m_text->SetInsertionPointEnd();
789 }
790 long wxSearchCtrl::GetInsertionPoint() const
791 {
792 return m_text->GetInsertionPoint();
793 }
794 wxTextPos wxSearchCtrl::GetLastPosition() const
795 {
796 return m_text->GetLastPosition();
797 }
798
799 void wxSearchCtrl::SetSelection(long from, long to)
800 {
801 m_text->SetSelection(from, to);
802 }
803 void wxSearchCtrl::SelectAll()
804 {
805 m_text->SelectAll();
806 }
807
808 void wxSearchCtrl::SetEditable(bool editable)
809 {
810 m_text->SetEditable(editable);
811 }
812
813 bool wxSearchCtrl::SetFont(const wxFont& font)
814 {
815 bool result = wxSearchCtrlBase::SetFont(font);
816 if ( result && m_text )
817 {
818 result = m_text->SetFont(font);
819 }
820 RecalcBitmaps();
821 return result;
822 }
823
824 // search control generic only
825 void wxSearchCtrl::SetSearchBitmap( const wxBitmap& bitmap )
826 {
827 m_searchBitmap = bitmap;
828 m_searchBitmapUser = bitmap.Ok();
829 if ( m_searchBitmapUser )
830 {
831 if ( m_searchButton && !HasMenu() )
832 {
833 m_searchButton->SetBitmapLabel( m_searchBitmap );
834 }
835 }
836 else
837 {
838 // the user bitmap was just cleared, generate one
839 RecalcBitmaps();
840 }
841 }
842
843 #if wxUSE_MENUS
844
845 void wxSearchCtrl::SetSearchMenuBitmap( const wxBitmap& bitmap )
846 {
847 m_searchMenuBitmap = bitmap;
848 m_searchMenuBitmapUser = bitmap.Ok();
849 if ( m_searchMenuBitmapUser )
850 {
851 if ( m_searchButton && m_menu )
852 {
853 m_searchButton->SetBitmapLabel( m_searchMenuBitmap );
854 }
855 }
856 else
857 {
858 // the user bitmap was just cleared, generate one
859 RecalcBitmaps();
860 }
861 }
862
863 #endif // wxUSE_MENUS
864
865 void wxSearchCtrl::SetCancelBitmap( const wxBitmap& bitmap )
866 {
867 m_cancelBitmap = bitmap;
868 m_cancelBitmapUser = bitmap.Ok();
869 if ( m_cancelBitmapUser )
870 {
871 if ( m_cancelButton )
872 {
873 m_cancelButton->SetBitmapLabel( m_cancelBitmap );
874 }
875 }
876 else
877 {
878 // the user bitmap was just cleared, generate one
879 RecalcBitmaps();
880 }
881 }
882
883 #if 0
884
885 // override streambuf method
886 #if wxHAS_TEXT_WINDOW_STREAM
887 int overflow(int i);
888 #endif // wxHAS_TEXT_WINDOW_STREAM
889
890 // stream-like insertion operators: these are always available, whether we
891 // were, or not, compiled with streambuf support
892 wxTextCtrl& operator<<(const wxString& s);
893 wxTextCtrl& operator<<(int i);
894 wxTextCtrl& operator<<(long i);
895 wxTextCtrl& operator<<(float f);
896 wxTextCtrl& operator<<(double d);
897 wxTextCtrl& operator<<(const wxChar c);
898 #endif
899
900 void wxSearchCtrl::DoSetValue(const wxString& value, int flags)
901 {
902 m_text->ChangeValue( value );
903 if ( flags & SetValue_SendEvent )
904 SendTextUpdatedEvent();
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 = wxStepColour(GetForegroundColour(), 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 = wxStepColour(GetForegroundColour(), 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.Ok() ||
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.Ok() ||
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.Ok() ||
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::OnSearchButton( wxCommandEvent& event )
1189 {
1190 event.Skip();
1191 }
1192
1193 void wxSearchCtrl::OnSetFocus( wxFocusEvent& /*event*/ )
1194 {
1195 if ( m_text )
1196 {
1197 m_text->SetFocus();
1198 }
1199 }
1200
1201 void wxSearchCtrl::OnSize( wxSizeEvent& WXUNUSED(event) )
1202 {
1203 int width, height;
1204 GetSize(&width, &height);
1205 LayoutControls(0, 0, width, height);
1206 }
1207
1208 #if wxUSE_MENUS
1209
1210 void wxSearchCtrl::PopupSearchMenu()
1211 {
1212 if ( m_menu )
1213 {
1214 wxSize size = GetSize();
1215 PopupMenu( m_menu, 0, size.y );
1216 }
1217 }
1218
1219 #endif // wxUSE_MENUS
1220
1221 #endif // !wxUSE_NATIVE_SEARCH_CONTROL
1222
1223 #endif // wxUSE_SEARCHCTRL