]> git.saurik.com Git - wxWidgets.git/blame - src/generic/srchctlg.cpp
Added logic, API and docs for wxDataViewModel::HasDefaultCompare indicating a compare...
[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
c60122bf
KO
345 wxSize sizeText = m_text->GetBestSize();
346
88c23b64
VZ
347 m_searchButton = new wxSearchButton(this,
348 wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN,
349 m_searchBitmap);
350 m_cancelButton = new wxSearchButton(this,
351 wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN,
352 m_cancelBitmap);
3f7f284d
RD
353
354 SetForegroundColour( m_text->GetForegroundColour() );
355 m_searchButton->SetForegroundColour( m_text->GetForegroundColour() );
356 m_cancelButton->SetForegroundColour( m_text->GetForegroundColour() );
357
358 SetBackgroundColour( m_text->GetBackgroundColour() );
359 m_searchButton->SetBackgroundColour( m_text->GetBackgroundColour() );
360 m_cancelButton->SetBackgroundColour( m_text->GetBackgroundColour() );
361
362 RecalcBitmaps();
363
364 SetInitialSize(size);
365 Move(pos);
366 return true;
367}
368
369wxSearchCtrl::~wxSearchCtrl()
370{
371 delete m_text;
372 delete m_searchButton;
373 delete m_cancelButton;
cfddeb10 374#if wxUSE_MENUS
3f7f284d 375 delete m_menu;
cfddeb10 376#endif // wxUSE_MENUS
3f7f284d
RD
377}
378
379
380// search control specific interfaces
cfddeb10
VZ
381#if wxUSE_MENUS
382
3f7f284d
RD
383void wxSearchCtrl::SetMenu( wxMenu* menu )
384{
385 if ( menu == m_menu )
386 {
387 // no change
388 return;
389 }
09d93215 390 bool hadMenu = (m_menu != NULL);
3f7f284d 391 delete m_menu;
3f7f284d
RD
392 m_menu = menu;
393
394 if ( m_menu && !hadMenu )
395 {
396 m_searchButton->SetBitmapLabel(m_searchMenuBitmap);
397 m_searchButton->Refresh();
3f7f284d
RD
398 }
399 else if ( !m_menu && hadMenu )
400 {
401 m_searchButton->SetBitmapLabel(m_searchBitmap);
402 if ( m_searchButtonVisible )
403 {
404 m_searchButton->Refresh();
405 }
3f7f284d 406 }
6646ca90 407 wxRect rect = GetRect();
88c23b64 408 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
3f7f284d
RD
409}
410
411wxMenu* wxSearchCtrl::GetMenu()
412{
413 return m_menu;
414}
415
cfddeb10
VZ
416#endif // wxUSE_MENUS
417
ec184e32 418void wxSearchCtrl::ShowSearchButton( bool show )
3f7f284d
RD
419{
420 if ( m_searchButtonVisible == show )
421 {
422 // no change
423 return;
424 }
425 m_searchButtonVisible = show;
426 if ( m_searchButtonVisible )
427 {
428 RecalcBitmaps();
429 }
430
431 wxRect rect = GetRect();
432 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
433}
434
ec184e32 435bool wxSearchCtrl::IsSearchButtonVisible() const
3f7f284d
RD
436{
437 return m_searchButtonVisible;
438}
439
440
ec184e32 441void wxSearchCtrl::ShowCancelButton( bool show )
3f7f284d
RD
442{
443 if ( m_cancelButtonVisible == show )
444 {
445 // no change
446 return;
447 }
448 m_cancelButtonVisible = show;
449
450 wxRect rect = GetRect();
451 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
452}
453
ec184e32 454bool wxSearchCtrl::IsCancelButtonVisible() const
3f7f284d
RD
455{
456 return m_cancelButtonVisible;
457}
458
6646ca90
RD
459void wxSearchCtrl::SetDescriptiveText(const wxString& text)
460{
461 m_text->SetDescriptiveText(text);
462}
463
464wxString wxSearchCtrl::GetDescriptiveText() const
465{
466 return m_text->GetDescriptiveText();
467}
3f7f284d
RD
468
469// ----------------------------------------------------------------------------
470// geometry
471// ----------------------------------------------------------------------------
472
473wxSize wxSearchCtrl::DoGetBestSize() const
474{
475 wxSize sizeText = m_text->GetBestSize();
476 wxSize sizeSearch(0,0);
477 wxSize sizeCancel(0,0);
478 int searchMargin = 0;
479 int cancelMargin = 0;
cfddeb10 480 if ( m_searchButtonVisible || HasMenu() )
3f7f284d
RD
481 {
482 sizeSearch = m_searchButton->GetBestSize();
483 searchMargin = MARGIN;
484 }
485 if ( m_cancelButtonVisible )
486 {
487 sizeCancel = m_cancelButton->GetBestSize();
488 cancelMargin = MARGIN;
489 }
490
491 int horizontalBorder = 1 + ( sizeText.y - sizeText.y * 14 / 21 ) / 2;
492
493 // buttons are square and equal to the height of the text control
494 int height = sizeText.y;
495 return wxSize(sizeSearch.x + searchMargin + sizeText.x + cancelMargin + sizeCancel.x + 2*horizontalBorder,
496 height + 2*BORDER);
497}
498
499void wxSearchCtrl::DoMoveWindow(int x, int y, int width, int height)
500{
501 wxSearchCtrlBase::DoMoveWindow(x, y, width, height);
502
503 LayoutControls(0, 0, width, height);
504}
505
506void wxSearchCtrl::LayoutControls(int x, int y, int width, int height)
507{
af1f44f1
RD
508 if ( !m_text )
509 return;
88c23b64 510
3f7f284d
RD
511 wxSize sizeText = m_text->GetBestSize();
512 // make room for the search menu & clear button
6646ca90 513 int horizontalBorder = ( sizeText.y - sizeText.y * 14 / 21 ) / 2;
3f7f284d
RD
514 x += horizontalBorder;
515 y += BORDER;
516 width -= horizontalBorder*2;
517 height -= BORDER*2;
518
519 wxSize sizeSearch(0,0);
520 wxSize sizeCancel(0,0);
521 int searchMargin = 0;
522 int cancelMargin = 0;
cfddeb10 523 if ( m_searchButtonVisible || HasMenu() )
3f7f284d
RD
524 {
525 sizeSearch = m_searchButton->GetBestSize();
526 searchMargin = MARGIN;
527 }
528 if ( m_cancelButtonVisible )
529 {
530 sizeCancel = m_cancelButton->GetBestSize();
531 cancelMargin = MARGIN;
532 }
cfddeb10 533 m_searchButton->Show( m_searchButtonVisible || HasMenu() );
3f7f284d
RD
534 m_cancelButton->Show( m_cancelButtonVisible );
535
536 if ( sizeSearch.x + sizeCancel.x > width )
537 {
538 sizeSearch.x = width/2;
539 sizeCancel.x = width/2;
540 searchMargin = 0;
541 cancelMargin = 0;
542 }
e6d0599d 543 wxCoord textWidth = width - sizeSearch.x - sizeCancel.x - searchMargin - cancelMargin - 1;
3f7f284d
RD
544
545 // position the subcontrols inside the client area
546
6646ca90 547 m_searchButton->SetSize(x, y + ICON_OFFSET - 1, sizeSearch.x, height);
88c23b64 548 m_text->SetSize( x + sizeSearch.x + searchMargin,
10eeb875 549 y + ICON_OFFSET - BORDER,
88c23b64 550 textWidth,
10eeb875 551 height);
3f7f284d 552 m_cancelButton->SetSize(x + sizeSearch.x + searchMargin + textWidth + cancelMargin,
6646ca90 553 y + ICON_OFFSET - 1, sizeCancel.x, height);
3f7f284d
RD
554}
555
556
557// accessors
558// ---------
559
560wxString wxSearchCtrl::GetValue() const
561{
4aab3429
RD
562 wxString value = m_text->GetValue();
563 if (value == m_text->GetDescriptiveText())
564 return wxEmptyString;
565 else
566 return value;
3f7f284d
RD
567}
568void wxSearchCtrl::SetValue(const wxString& value)
569{
570 m_text->SetValue(value);
571}
572
573wxString wxSearchCtrl::GetRange(long from, long to) const
574{
575 return m_text->GetRange(from, to);
576}
577
578int wxSearchCtrl::GetLineLength(long lineNo) const
579{
580 return m_text->GetLineLength(lineNo);
581}
582wxString wxSearchCtrl::GetLineText(long lineNo) const
583{
584 return m_text->GetLineText(lineNo);
585}
586int wxSearchCtrl::GetNumberOfLines() const
587{
588 return m_text->GetNumberOfLines();
589}
590
591bool wxSearchCtrl::IsModified() const
592{
593 return m_text->IsModified();
594}
595bool wxSearchCtrl::IsEditable() const
596{
597 return m_text->IsEditable();
598}
599
600// more readable flag testing methods
601bool wxSearchCtrl::IsSingleLine() const
602{
603 return m_text->IsSingleLine();
604}
605bool wxSearchCtrl::IsMultiLine() const
606{
607 return m_text->IsMultiLine();
608}
609
610// If the return values from and to are the same, there is no selection.
611void wxSearchCtrl::GetSelection(long* from, long* to) const
612{
613 m_text->GetSelection(from, to);
614}
615
616wxString wxSearchCtrl::GetStringSelection() const
617{
618 return m_text->GetStringSelection();
619}
620
621// operations
622// ----------
623
624// editing
625void wxSearchCtrl::Clear()
626{
627 m_text->Clear();
628}
629void wxSearchCtrl::Replace(long from, long to, const wxString& value)
630{
631 m_text->Replace(from, to, value);
632}
633void wxSearchCtrl::Remove(long from, long to)
634{
635 m_text->Remove(from, to);
636}
637
638// load/save the controls contents from/to the file
639bool wxSearchCtrl::LoadFile(const wxString& file)
640{
641 return m_text->LoadFile(file);
642}
643bool wxSearchCtrl::SaveFile(const wxString& file)
644{
645 return m_text->SaveFile(file);
646}
647
648// sets/clears the dirty flag
649void wxSearchCtrl::MarkDirty()
650{
651 m_text->MarkDirty();
652}
653void wxSearchCtrl::DiscardEdits()
654{
655 m_text->DiscardEdits();
656}
657
658// set the max number of characters which may be entered in a single line
659// text control
660void wxSearchCtrl::SetMaxLength(unsigned long len)
661{
662 m_text->SetMaxLength(len);
663}
664
665// writing text inserts it at the current position, appending always
666// inserts it at the end
667void wxSearchCtrl::WriteText(const wxString& text)
668{
669 m_text->WriteText(text);
670}
671void wxSearchCtrl::AppendText(const wxString& text)
672{
673 m_text->AppendText(text);
674}
675
676// insert the character which would have resulted from this key event,
677// return true if anything has been inserted
678bool wxSearchCtrl::EmulateKeyPress(const wxKeyEvent& event)
679{
680 return m_text->EmulateKeyPress(event);
681}
682
683// text control under some platforms supports the text styles: these
684// methods allow to apply the given text style to the given selection or to
685// set/get the style which will be used for all appended text
686bool wxSearchCtrl::SetStyle(long start, long end, const wxTextAttr& style)
687{
688 return m_text->SetStyle(start, end, style);
689}
690bool wxSearchCtrl::GetStyle(long position, wxTextAttr& style)
691{
692 return m_text->GetStyle(position, style);
693}
694bool wxSearchCtrl::SetDefaultStyle(const wxTextAttr& style)
695{
696 return m_text->SetDefaultStyle(style);
697}
698const wxTextAttr& wxSearchCtrl::GetDefaultStyle() const
699{
700 return m_text->GetDefaultStyle();
701}
702
703// translate between the position (which is just an index in the text ctrl
704// considering all its contents as a single strings) and (x, y) coordinates
705// which represent column and line.
706long wxSearchCtrl::XYToPosition(long x, long y) const
707{
708 return m_text->XYToPosition(x, y);
709}
710bool wxSearchCtrl::PositionToXY(long pos, long *x, long *y) const
711{
712 return m_text->PositionToXY(pos, x, y);
713}
714
715void wxSearchCtrl::ShowPosition(long pos)
716{
717 m_text->ShowPosition(pos);
718}
719
720// find the character at position given in pixels
721//
722// NB: pt is in device coords (not adjusted for the client area origin nor
723// scrolling)
724wxTextCtrlHitTestResult wxSearchCtrl::HitTest(const wxPoint& pt, long *pos) const
725{
726 return m_text->HitTest(pt, pos);
727}
728wxTextCtrlHitTestResult wxSearchCtrl::HitTest(const wxPoint& pt,
729 wxTextCoord *col,
730 wxTextCoord *row) const
731{
732 return m_text->HitTest(pt, col, row);
733}
734
735// Clipboard operations
736void wxSearchCtrl::Copy()
737{
738 m_text->Copy();
739}
740void wxSearchCtrl::Cut()
741{
742 m_text->Cut();
743}
744void wxSearchCtrl::Paste()
745{
746 m_text->Paste();
747}
748
749bool wxSearchCtrl::CanCopy() const
750{
751 return m_text->CanCopy();
752}
753bool wxSearchCtrl::CanCut() const
754{
755 return m_text->CanCut();
756}
757bool wxSearchCtrl::CanPaste() const
758{
759 return m_text->CanPaste();
760}
761
762// Undo/redo
763void wxSearchCtrl::Undo()
764{
765 m_text->Undo();
766}
767void wxSearchCtrl::Redo()
768{
769 m_text->Redo();
770}
771
772bool wxSearchCtrl::CanUndo() const
773{
774 return m_text->CanUndo();
775}
776bool wxSearchCtrl::CanRedo() const
777{
778 return m_text->CanRedo();
779}
780
781// Insertion point
782void wxSearchCtrl::SetInsertionPoint(long pos)
783{
784 m_text->SetInsertionPoint(pos);
785}
786void wxSearchCtrl::SetInsertionPointEnd()
787{
788 m_text->SetInsertionPointEnd();
789}
790long wxSearchCtrl::GetInsertionPoint() const
791{
792 return m_text->GetInsertionPoint();
793}
794wxTextPos wxSearchCtrl::GetLastPosition() const
795{
796 return m_text->GetLastPosition();
797}
798
799void wxSearchCtrl::SetSelection(long from, long to)
800{
801 m_text->SetSelection(from, to);
802}
803void wxSearchCtrl::SelectAll()
804{
805 m_text->SelectAll();
806}
807
808void wxSearchCtrl::SetEditable(bool editable)
809{
810 m_text->SetEditable(editable);
811}
812
813bool wxSearchCtrl::SetFont(const wxFont& font)
814{
815 bool result = wxSearchCtrlBase::SetFont(font);
816 if ( result && m_text )
817 {
09d93215 818 result = m_text->SetFont(font);
3f7f284d
RD
819 }
820 RecalcBitmaps();
821 return result;
822}
823
824// search control generic only
825void wxSearchCtrl::SetSearchBitmap( const wxBitmap& bitmap )
826{
827 m_searchBitmap = bitmap;
828 m_searchBitmapUser = bitmap.Ok();
829 if ( m_searchBitmapUser )
830 {
cfddeb10 831 if ( m_searchButton && !HasMenu() )
3f7f284d
RD
832 {
833 m_searchButton->SetBitmapLabel( m_searchBitmap );
834 }
835 }
836 else
837 {
838 // the user bitmap was just cleared, generate one
839 RecalcBitmaps();
840 }
841}
842
cfddeb10
VZ
843#if wxUSE_MENUS
844
3f7f284d
RD
845void wxSearchCtrl::SetSearchMenuBitmap( const wxBitmap& bitmap )
846{
847 m_searchMenuBitmap = bitmap;
848 m_searchMenuBitmapUser = bitmap.Ok();
849 if ( m_searchMenuBitmapUser )
850 {
851 if ( m_searchButton && m_menu )
852 {
853 m_searchButton->SetBitmapLabel( m_searchMenuBitmap );
854 }
855 }
856 else
857 {
858 // the user bitmap was just cleared, generate one
859 RecalcBitmaps();
860 }
861}
862
cfddeb10
VZ
863#endif // wxUSE_MENUS
864
3f7f284d
RD
865void wxSearchCtrl::SetCancelBitmap( const wxBitmap& bitmap )
866{
867 m_cancelBitmap = bitmap;
868 m_cancelBitmapUser = bitmap.Ok();
869 if ( m_cancelBitmapUser )
870 {
871 if ( m_cancelButton )
872 {
873 m_cancelButton->SetBitmapLabel( m_cancelBitmap );
874 }
875 }
876 else
877 {
878 // the user bitmap was just cleared, generate one
879 RecalcBitmaps();
880 }
881}
882
883#if 0
884
885// override streambuf method
886#if wxHAS_TEXT_WINDOW_STREAM
887int overflow(int i);
888#endif // wxHAS_TEXT_WINDOW_STREAM
889
890// stream-like insertion operators: these are always available, whether we
891// were, or not, compiled with streambuf support
892wxTextCtrl& operator<<(const wxString& s);
893wxTextCtrl& operator<<(int i);
894wxTextCtrl& operator<<(long i);
895wxTextCtrl& operator<<(float f);
896wxTextCtrl& operator<<(double d);
897wxTextCtrl& operator<<(const wxChar c);
898#endif
899
900void wxSearchCtrl::DoSetValue(const wxString& value, int flags)
901{
902 m_text->ChangeValue( value );
903 if ( flags & SetValue_SendEvent )
904 SendTextUpdatedEvent();
905}
906
907// do the window-specific processing after processing the update event
908void wxSearchCtrl::DoUpdateWindowUI(wxUpdateUIEvent& event)
909{
910 wxSearchCtrlBase::DoUpdateWindowUI(event);
911}
912
913bool wxSearchCtrl::ShouldInheritColours() const
914{
915 return true;
916}
917
918// icons are rendered at 3-8 times larger than necessary and downscaled for
919// antialiasing
920static int GetMultiplier()
921{
922#ifdef __WXWINCE__
923 // speed up bitmap generation by using a small bitmap
924 return 3;
925#else
926 int depth = ::wxDisplayDepth();
927
928 if ( depth >= 24 )
929 {
930 return 8;
931 }
932 return 6;
933#endif
934}
935
936wxBitmap wxSearchCtrl::RenderSearchBitmap( int x, int y, bool renderDrop )
937{
938 wxColour bg = GetBackgroundColour();
6646ca90 939 wxColour fg = wxStepColour(GetForegroundColour(), LIGHT_STEP-20);
3f7f284d
RD
940
941 //===============================================================================
942 // begin drawing code
943 //===============================================================================
944 // image stats
945
946 // force width:height ratio
947 if ( 14*x > y*20 )
948 {
949 // x is too big
950 x = y*20/14;
951 }
952 else
953 {
954 // y is too big
955 y = x*14/20;
956 }
957
958 // glass 11x11, top left corner
959 // handle (9,9)-(13,13)
960 // drop (13,16)-(19,6)-(16,9)
961
962 int multiplier = GetMultiplier();
963 int penWidth = multiplier * 2;
964
965 penWidth = penWidth * x / 20;
966
967 wxBitmap bitmap( multiplier*x, multiplier*y );
968 wxMemoryDC mem;
969 mem.SelectObject(bitmap);
970
971 // clear background
972 mem.SetBrush( wxBrush(bg) );
973 mem.SetPen( wxPen(bg) );
974 mem.DrawRectangle(0,0,bitmap.GetWidth(),bitmap.GetHeight());
975
976 // draw drop glass
977 mem.SetBrush( wxBrush(fg) );
978 mem.SetPen( wxPen(fg) );
979 int glassBase = 5 * x / 20;
980 int glassFactor = 2*glassBase + 1;
981 int radius = multiplier*glassFactor/2;
982 mem.DrawCircle(radius,radius,radius);
983 mem.SetBrush( wxBrush(bg) );
984 mem.SetPen( wxPen(bg) );
985 mem.DrawCircle(radius,radius,radius-penWidth);
986
987 // draw handle
988 int lineStart = radius + (radius-penWidth/2) * 707 / 1000; // 707 / 1000 = 0.707 = 1/sqrt(2);
989
990 mem.SetPen( wxPen(fg) );
991 mem.SetBrush( wxBrush(fg) );
992 int handleCornerShift = penWidth * 707 / 1000 / 2; // 707 / 1000 = 0.707 = 1/sqrt(2);
993 handleCornerShift = WXMAX( handleCornerShift, 1 );
994 int handleBase = 4 * x / 20;
995 int handleLength = 2*handleBase+1;
996 wxPoint handlePolygon[] =
997 {
998 wxPoint(-handleCornerShift,+handleCornerShift),
999 wxPoint(+handleCornerShift,-handleCornerShift),
1000 wxPoint(multiplier*handleLength/2+handleCornerShift,multiplier*handleLength/2-handleCornerShift),
1001 wxPoint(multiplier*handleLength/2-handleCornerShift,multiplier*handleLength/2+handleCornerShift),
1002 };
1003 mem.DrawPolygon(WXSIZEOF(handlePolygon),handlePolygon,lineStart,lineStart);
1004
1005 // draw drop triangle
1006 int triangleX = 13 * x / 20;
1007 int triangleY = 5 * x / 20;
1008 int triangleBase = 3 * x / 20;
1009 int triangleFactor = triangleBase*2+1;
1010 if ( renderDrop )
1011 {
1012 wxPoint dropPolygon[] =
1013 {
1014 wxPoint(multiplier*0,multiplier*0), // triangle left
1015 wxPoint(multiplier*triangleFactor-1,multiplier*0), // triangle right
1016 wxPoint(multiplier*triangleFactor/2,multiplier*triangleFactor/2), // triangle bottom
1017 };
1018 mem.DrawPolygon(WXSIZEOF(dropPolygon),dropPolygon,multiplier*triangleX,multiplier*triangleY);
1019 }
6646ca90 1020 mem.SelectObject(wxNullBitmap);
88c23b64 1021
3f7f284d
RD
1022 //===============================================================================
1023 // end drawing code
1024 //===============================================================================
1025
1026 if ( multiplier != 1 )
1027 {
1028 wxImage image = bitmap.ConvertToImage();
1029 image.Rescale(x,y);
1030 bitmap = wxBitmap( image );
1031 }
6646ca90
RD
1032 if ( !renderDrop )
1033 {
1034 // Trim the edge where the arrow would have gone
1035 bitmap = bitmap.GetSubBitmap(wxRect(0,0, y,y));
1036 }
3f7f284d
RD
1037
1038 return bitmap;
1039}
1040
1041wxBitmap wxSearchCtrl::RenderCancelBitmap( int x, int y )
1042{
1043 wxColour bg = GetBackgroundColour();
6646ca90 1044 wxColour fg = wxStepColour(GetForegroundColour(), LIGHT_STEP);
3f7f284d
RD
1045
1046 //===============================================================================
1047 // begin drawing code
1048 //===============================================================================
1049 // image stats
1050
1051 // total size 14x14
1052 // force 1:1 ratio
1053 if ( x > y )
1054 {
1055 // x is too big
1056 x = y;
1057 }
1058 else
1059 {
1060 // y is too big
1061 y = x;
1062 }
1063
1064 // 14x14 circle
1065 // cross line starts (4,4)-(10,10)
1066 // drop (13,16)-(19,6)-(16,9)
1067
1068 int multiplier = GetMultiplier();
1069
1070 int penWidth = multiplier * x / 14;
1071
1072 wxBitmap bitmap( multiplier*x, multiplier*y );
1073 wxMemoryDC mem;
1074 mem.SelectObject(bitmap);
1075
1076 // clear background
1077 mem.SetBrush( wxBrush(bg) );
1078 mem.SetPen( wxPen(bg) );
1079 mem.DrawRectangle(0,0,bitmap.GetWidth(),bitmap.GetHeight());
1080
1081 // draw drop glass
1082 mem.SetBrush( wxBrush(fg) );
1083 mem.SetPen( wxPen(fg) );
1084 int radius = multiplier*x/2;
1085 mem.DrawCircle(radius,radius,radius);
1086
1087 // draw cross
1088 int lineStartBase = 4 * x / 14;
1089 int lineLength = x - 2*lineStartBase;
1090
1091 mem.SetPen( wxPen(bg) );
1092 mem.SetBrush( wxBrush(bg) );
1093 int handleCornerShift = penWidth/2;
1094 handleCornerShift = WXMAX( handleCornerShift, 1 );
1095 wxPoint handlePolygon[] =
1096 {
1097 wxPoint(-handleCornerShift,+handleCornerShift),
1098 wxPoint(+handleCornerShift,-handleCornerShift),
1099 wxPoint(multiplier*lineLength+handleCornerShift,multiplier*lineLength-handleCornerShift),
1100 wxPoint(multiplier*lineLength-handleCornerShift,multiplier*lineLength+handleCornerShift),
1101 };
1102 mem.DrawPolygon(WXSIZEOF(handlePolygon),handlePolygon,multiplier*lineStartBase,multiplier*lineStartBase);
1103 wxPoint handlePolygon2[] =
1104 {
1105 wxPoint(+handleCornerShift,+handleCornerShift),
1106 wxPoint(-handleCornerShift,-handleCornerShift),
1107 wxPoint(multiplier*lineLength-handleCornerShift,-multiplier*lineLength-handleCornerShift),
1108 wxPoint(multiplier*lineLength+handleCornerShift,-multiplier*lineLength+handleCornerShift),
1109 };
1110 mem.DrawPolygon(WXSIZEOF(handlePolygon2),handlePolygon2,multiplier*lineStartBase,multiplier*(x-lineStartBase));
1111
1112 //===============================================================================
1113 // end drawing code
1114 //===============================================================================
1115
1116 if ( multiplier != 1 )
1117 {
1118 wxImage image = bitmap.ConvertToImage();
1119 image.Rescale(x,y);
1120 bitmap = wxBitmap( image );
1121 }
1122
1123 return bitmap;
1124}
1125
1126void wxSearchCtrl::RecalcBitmaps()
1127{
1128 if ( !m_text )
1129 {
1130 return;
1131 }
1132 wxSize sizeText = m_text->GetBestSize();
1133
1134 int bitmapHeight = sizeText.y - 2 * ICON_MARGIN;
1135 int bitmapWidth = sizeText.y * 20 / 14;
1136
1137 if ( !m_searchBitmapUser )
1138 {
8bc333d7 1139 if (
3f7f284d 1140 !m_searchBitmap.Ok() ||
8bc333d7 1141 m_searchBitmap.GetHeight() != bitmapHeight ||
3f7f284d
RD
1142 m_searchBitmap.GetWidth() != bitmapWidth
1143 )
1144 {
1145 m_searchBitmap = RenderSearchBitmap(bitmapWidth,bitmapHeight,false);
cfddeb10 1146 if ( !HasMenu() )
3f7f284d
RD
1147 {
1148 m_searchButton->SetBitmapLabel(m_searchBitmap);
1149 }
1150 }
1151 // else this bitmap was set by user, don't alter
1152 }
1153
cfddeb10 1154#if wxUSE_MENUS
3f7f284d
RD
1155 if ( !m_searchMenuBitmapUser )
1156 {
8bc333d7 1157 if (
3f7f284d 1158 !m_searchMenuBitmap.Ok() ||
8bc333d7 1159 m_searchMenuBitmap.GetHeight() != bitmapHeight ||
3f7f284d
RD
1160 m_searchMenuBitmap.GetWidth() != bitmapWidth
1161 )
1162 {
1163 m_searchMenuBitmap = RenderSearchBitmap(bitmapWidth,bitmapHeight,true);
1164 if ( m_menu )
1165 {
1166 m_searchButton->SetBitmapLabel(m_searchMenuBitmap);
1167 }
1168 }
1169 // else this bitmap was set by user, don't alter
1170 }
cfddeb10 1171#endif // wxUSE_MENUS
3f7f284d
RD
1172
1173 if ( !m_cancelBitmapUser )
1174 {
8bc333d7 1175 if (
3f7f284d 1176 !m_cancelBitmap.Ok() ||
8bc333d7 1177 m_cancelBitmap.GetHeight() != bitmapHeight ||
3f7f284d
RD
1178 m_cancelBitmap.GetWidth() != bitmapHeight
1179 )
1180 {
e6d0599d 1181 m_cancelBitmap = RenderCancelBitmap(bitmapHeight-BORDER-1,bitmapHeight-BORDER-1); // square
3f7f284d
RD
1182 m_cancelButton->SetBitmapLabel(m_cancelBitmap);
1183 }
1184 // else this bitmap was set by user, don't alter
1185 }
1186}
1187
1188void wxSearchCtrl::OnSearchButton( wxCommandEvent& event )
1189{
1190 event.Skip();
1191}
1192
1193void wxSearchCtrl::OnSetFocus( wxFocusEvent& /*event*/ )
1194{
1195 if ( m_text )
1196 {
1197 m_text->SetFocus();
1198 }
1199}
1200
8ebf2465 1201void wxSearchCtrl::OnSize( wxSizeEvent& WXUNUSED(event) )
af1f44f1
RD
1202{
1203 int width, height;
1204 GetSize(&width, &height);
1205 LayoutControls(0, 0, width, height);
1206}
1207
cfddeb10
VZ
1208#if wxUSE_MENUS
1209
3f7f284d
RD
1210void wxSearchCtrl::PopupSearchMenu()
1211{
1212 if ( m_menu )
1213 {
1214 wxSize size = GetSize();
1215 PopupMenu( m_menu, 0, size.y );
1216 }
1217}
1218
cfddeb10
VZ
1219#endif // wxUSE_MENUS
1220
5b43c75c 1221#endif // !wxUSE_NATIVE_SEARCH_CONTROL
3f7f284d
RD
1222
1223#endif // wxUSE_SEARCHCTRL