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