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