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