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