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