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