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