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