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