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