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