]> git.saurik.com Git - wxWidgets.git/blame - src/generic/srchctlg.cpp
Disable wxUSE_ENH_METAFILE for wxGTK builds.
[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,
7ecc7eb6 64 (style & ~wxBORDER_MASK) | wxNO_BORDER)
3f7f284d
RD
65 {
66 m_search = search;
7ecc7eb6
VZ
67
68 SetHint(_("Search"));
88c23b64 69
241708ca
VZ
70 // Ensure that our best size is recomputed using our overridden
71 // DoGetBestSize().
72 InvalidateBestSize();
3f7f284d
RD
73 }
74
09ca8913
VZ
75 virtual wxWindow* GetMainWindowOfCompositeControl()
76 {
77 return m_search;
78 }
0ec1179b
VZ
79
80 // provide access to the base class protected methods to wxSearchCtrl which
81 // needs to forward to them
8366ab05
VZ
82 void DoSetValue(const wxString& value, int flags)
83 {
bdb7278a 84 wxTextCtrl::DoSetValue(value, flags);
8366ab05
VZ
85 }
86
87 bool DoLoadFile(const wxString& file, int fileType)
88 {
bdb7278a 89 return wxTextCtrl::DoLoadFile(file, fileType);
8366ab05
VZ
90 }
91
92 bool DoSaveFile(const wxString& file, int fileType)
93 {
bdb7278a 94 return wxTextCtrl::DoSaveFile(file, fileType);
8366ab05 95 }
0ec1179b 96
3f7f284d
RD
97protected:
98 void OnText(wxCommandEvent& eventText)
99 {
100 wxCommandEvent event(eventText);
101 event.SetEventObject(m_search);
102 event.SetId(m_search->GetId());
103
104 m_search->GetEventHandler()->ProcessEvent(event);
105 }
106
107 void OnTextUrl(wxTextUrlEvent& eventText)
108 {
109 // copy constructor is disabled for some reason?
110 //wxTextUrlEvent event(eventText);
111 wxTextUrlEvent event(
8bc333d7 112 m_search->GetId(),
3f7f284d
RD
113 eventText.GetMouseEvent(),
114 eventText.GetURLStart(),
115 eventText.GetURLEnd()
116 );
117 event.SetEventObject(m_search);
118
119 m_search->GetEventHandler()->ProcessEvent(event);
120 }
121
a166dbba
VZ
122#ifdef __WXMSW__
123 // We increase the text control height to be the same as for the controls
124 // with border as this is what we actually need here because even though
125 // this control itself is borderless, it's inside wxSearchCtrl which does
126 // have the border and so should have the same height as the normal text
127 // entries with border.
128 //
129 // This is a bit ugly and it would arguably be better to use whatever size
130 // the base class version returns and just centre the text vertically in
131 // the search control but I failed to modify the code in LayoutControls()
132 // to do this easily and as there is much in that code I don't understand
133 // (notably what is the logic for buttons sizing?) I prefer to not touch it
134 // at all.
135 virtual wxSize DoGetBestSize() const
136 {
137 const long flags = GetWindowStyleFlag();
138 wxSearchTextCtrl* const self = const_cast<wxSearchTextCtrl*>(this);
139
140 self->SetWindowStyleFlag((flags & ~wxBORDER_MASK) | wxBORDER_DEFAULT);
141 const wxSize size = wxTextCtrl::DoGetBestSize();
142 self->SetWindowStyleFlag(flags);
143
144 return size;
145 }
146#endif // __WXMSW__
147
3f7f284d
RD
148private:
149 wxSearchCtrl* m_search;
88c23b64 150
3f7f284d
RD
151 DECLARE_EVENT_TABLE()
152};
153
154BEGIN_EVENT_TABLE(wxSearchTextCtrl, wxTextCtrl)
155 EVT_TEXT(wxID_ANY, wxSearchTextCtrl::OnText)
156 EVT_TEXT_ENTER(wxID_ANY, wxSearchTextCtrl::OnText)
157 EVT_TEXT_URL(wxID_ANY, wxSearchTextCtrl::OnTextUrl)
158 EVT_TEXT_MAXLEN(wxID_ANY, wxSearchTextCtrl::OnText)
159END_EVENT_TABLE()
160
161// ----------------------------------------------------------------------------
162// wxSearchButton: search button used by search control
163// ----------------------------------------------------------------------------
164
165class wxSearchButton : public wxControl
166{
167public:
168 wxSearchButton(wxSearchCtrl *search, int eventType, const wxBitmap& bmp)
169 : wxControl(search, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNO_BORDER),
170 m_search(search),
171 m_eventType(eventType),
172 m_bmp(bmp)
173 { }
174
5d83eee7
VZ
175 void SetBitmapLabel(const wxBitmap& label)
176 {
177 m_bmp = label;
178 InvalidateBestSize();
179 }
3f7f284d 180
7e0a200b
VZ
181 // The buttons in wxSearchCtrl shouldn't accept focus from keyboard because
182 // this would interfere with the usual TAB processing: the user expects
183 // that pressing TAB in the search control should switch focus to the next
184 // control and not give it to the button inside the same control. Besides,
185 // the search button can be already activated by pressing "Enter" so there
186 // is really no reason for it to be able to get focus from keyboard.
187 virtual bool AcceptsFocusFromKeyboard() const { return false; }
8bc333d7 188
09ca8913
VZ
189 virtual wxWindow* GetMainWindowOfCompositeControl()
190 {
191 return m_search;
192 }
193
3f7f284d
RD
194protected:
195 wxSize DoGetBestSize() const
196 {
197 return wxSize(m_bmp.GetWidth(), m_bmp.GetHeight());
198 }
8bc333d7 199
3f7f284d
RD
200 void OnLeftUp(wxMouseEvent&)
201 {
202 wxCommandEvent event(m_eventType, m_search->GetId());
203 event.SetEventObject(m_search);
204
ce7fe42e 205 if ( m_eventType == wxEVT_SEARCHCTRL_SEARCH_BTN )
ac63bc40
VZ
206 {
207 // it's convenient to have the string to search for directly in the
208 // event instead of having to retrieve it from the control in the
209 // event handler code later, so provide it here
210 event.SetString(m_search->GetValue());
211 }
212
3f7f284d
RD
213 GetEventHandler()->ProcessEvent(event);
214
215 m_search->SetFocus();
216
cfddeb10 217#if wxUSE_MENUS
ce7fe42e 218 if ( m_eventType == wxEVT_SEARCHCTRL_SEARCH_BTN )
3f7f284d
RD
219 {
220 // this happens automatically, just like on Mac OS X
221 m_search->PopupSearchMenu();
222 }
cfddeb10 223#endif // wxUSE_MENUS
3f7f284d
RD
224 }
225
226 void OnPaint(wxPaintEvent&)
227 {
228 wxPaintDC dc(this);
229 dc.DrawBitmap(m_bmp, 0,0, true);
230 }
231
8bc333d7 232
3f7f284d
RD
233private:
234 wxSearchCtrl *m_search;
235 wxEventType m_eventType;
236 wxBitmap m_bmp;
237
238 DECLARE_EVENT_TABLE()
239};
240
6f011faa 241BEGIN_EVENT_TABLE(wxSearchButton, wxControl)
3f7f284d
RD
242 EVT_LEFT_UP(wxSearchButton::OnLeftUp)
243 EVT_PAINT(wxSearchButton::OnPaint)
244END_EVENT_TABLE()
245
246BEGIN_EVENT_TABLE(wxSearchCtrl, wxSearchCtrlBase)
e5eff438 247 EVT_SEARCHCTRL_CANCEL_BTN(wxID_ANY, wxSearchCtrl::OnCancelButton)
3f7f284d 248 EVT_SET_FOCUS(wxSearchCtrl::OnSetFocus)
af1f44f1 249 EVT_SIZE(wxSearchCtrl::OnSize)
3f7f284d
RD
250END_EVENT_TABLE()
251
252IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl, wxSearchCtrlBase)
253
254// ============================================================================
255// implementation
256// ============================================================================
257
258// ----------------------------------------------------------------------------
259// wxSearchCtrl creation
260// ----------------------------------------------------------------------------
261
262// creation
263// --------
264
265wxSearchCtrl::wxSearchCtrl()
8bc333d7 266{
3f7f284d
RD
267 Init();
268}
269
270wxSearchCtrl::wxSearchCtrl(wxWindow *parent, wxWindowID id,
271 const wxString& value,
272 const wxPoint& pos,
273 const wxSize& size,
274 long style,
275 const wxValidator& validator,
276 const wxString& name)
277{
278 Init();
279
280 Create(parent, id, value, pos, size, style, validator, name);
281}
282
283void wxSearchCtrl::Init()
284{
cfddeb10
VZ
285 m_text = NULL;
286 m_searchButton = NULL;
287 m_cancelButton = NULL;
288#if wxUSE_MENUS
289 m_menu = NULL;
290#endif // wxUSE_MENUS
3f7f284d
RD
291
292 m_searchButtonVisible = true;
293 m_cancelButtonVisible = false;
294
3f7f284d
RD
295 m_searchBitmapUser = false;
296 m_cancelBitmapUser = false;
cfddeb10
VZ
297#if wxUSE_MENUS
298 m_searchMenuBitmapUser = false;
299#endif // wxUSE_MENUS
3f7f284d
RD
300}
301
302bool wxSearchCtrl::Create(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{
88c23b64
VZ
310 // force border style for more native appearance
311 style &= ~wxBORDER_MASK;
10eeb875 312#ifdef __WXGTK__
88c23b64 313 style |= wxBORDER_SUNKEN;
a047aff2
JS
314#elif defined(__WXMSW__)
315 // Don't set the style explicitly, let GetDefaultBorder() work it out, unless
316 // we will get a sunken border (e.g. on Windows 200) in which case we must
317 // override with a simple border.
318 if (GetDefaultBorder() == wxBORDER_SUNKEN)
319 style |= wxBORDER_SIMPLE;
10eeb875 320#else
88c23b64 321 style |= wxBORDER_SIMPLE;
10eeb875 322#endif
0ec1179b
VZ
323 if ( !wxSearchCtrlBaseBaseClass::Create(parent, id, pos, size,
324 style, validator, name) )
3f7f284d
RD
325 {
326 return false;
327 }
328
7ecc7eb6 329 m_text = new wxSearchTextCtrl(this, value, style);
88c23b64 330
88c23b64 331 m_searchButton = new wxSearchButton(this,
ce7fe42e 332 wxEVT_SEARCHCTRL_SEARCH_BTN,
88c23b64
VZ
333 m_searchBitmap);
334 m_cancelButton = new wxSearchButton(this,
ce7fe42e 335 wxEVT_SEARCHCTRL_CANCEL_BTN,
88c23b64 336 m_cancelBitmap);
3f7f284d 337
3f7f284d 338 SetBackgroundColour( m_text->GetBackgroundColour() );
3f7f284d
RD
339
340 RecalcBitmaps();
341
342 SetInitialSize(size);
343 Move(pos);
344 return true;
345}
346
347wxSearchCtrl::~wxSearchCtrl()
348{
349 delete m_text;
350 delete m_searchButton;
351 delete m_cancelButton;
cfddeb10 352#if wxUSE_MENUS
3f7f284d 353 delete m_menu;
cfddeb10 354#endif // wxUSE_MENUS
3f7f284d
RD
355}
356
357
358// search control specific interfaces
cfddeb10
VZ
359#if wxUSE_MENUS
360
3f7f284d
RD
361void wxSearchCtrl::SetMenu( wxMenu* menu )
362{
363 if ( menu == m_menu )
364 {
365 // no change
366 return;
367 }
09d93215 368 bool hadMenu = (m_menu != NULL);
3f7f284d 369 delete m_menu;
3f7f284d
RD
370 m_menu = menu;
371
372 if ( m_menu && !hadMenu )
373 {
374 m_searchButton->SetBitmapLabel(m_searchMenuBitmap);
375 m_searchButton->Refresh();
3f7f284d
RD
376 }
377 else if ( !m_menu && hadMenu )
378 {
379 m_searchButton->SetBitmapLabel(m_searchBitmap);
380 if ( m_searchButtonVisible )
381 {
382 m_searchButton->Refresh();
383 }
3f7f284d 384 }
6646ca90 385 wxRect rect = GetRect();
88c23b64 386 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
3f7f284d
RD
387}
388
389wxMenu* wxSearchCtrl::GetMenu()
390{
391 return m_menu;
392}
393
cfddeb10
VZ
394#endif // wxUSE_MENUS
395
ec184e32 396void wxSearchCtrl::ShowSearchButton( bool show )
3f7f284d
RD
397{
398 if ( m_searchButtonVisible == show )
399 {
400 // no change
401 return;
402 }
403 m_searchButtonVisible = show;
404 if ( m_searchButtonVisible )
405 {
406 RecalcBitmaps();
407 }
408
409 wxRect rect = GetRect();
410 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
411}
412
ec184e32 413bool wxSearchCtrl::IsSearchButtonVisible() const
3f7f284d
RD
414{
415 return m_searchButtonVisible;
416}
417
418
ec184e32 419void wxSearchCtrl::ShowCancelButton( bool show )
3f7f284d
RD
420{
421 if ( m_cancelButtonVisible == show )
422 {
423 // no change
424 return;
425 }
426 m_cancelButtonVisible = show;
427
428 wxRect rect = GetRect();
429 LayoutControls(0, 0, rect.GetWidth(), rect.GetHeight());
430}
431
ec184e32 432bool wxSearchCtrl::IsCancelButtonVisible() const
3f7f284d
RD
433{
434 return m_cancelButtonVisible;
435}
436
6646ca90
RD
437void wxSearchCtrl::SetDescriptiveText(const wxString& text)
438{
7ecc7eb6 439 m_text->SetHint(text);
6646ca90
RD
440}
441
442wxString wxSearchCtrl::GetDescriptiveText() const
443{
7ecc7eb6 444 return m_text->GetHint();
6646ca90 445}
3f7f284d
RD
446
447// ----------------------------------------------------------------------------
448// geometry
449// ----------------------------------------------------------------------------
450
451wxSize wxSearchCtrl::DoGetBestSize() const
452{
453 wxSize sizeText = m_text->GetBestSize();
454 wxSize sizeSearch(0,0);
455 wxSize sizeCancel(0,0);
456 int searchMargin = 0;
457 int cancelMargin = 0;
cfddeb10 458 if ( m_searchButtonVisible || HasMenu() )
3f7f284d
RD
459 {
460 sizeSearch = m_searchButton->GetBestSize();
461 searchMargin = MARGIN;
462 }
463 if ( m_cancelButtonVisible )
464 {
465 sizeCancel = m_cancelButton->GetBestSize();
466 cancelMargin = MARGIN;
467 }
468
469 int horizontalBorder = 1 + ( sizeText.y - sizeText.y * 14 / 21 ) / 2;
470
471 // buttons are square and equal to the height of the text control
472 int height = sizeText.y;
473 return wxSize(sizeSearch.x + searchMargin + sizeText.x + cancelMargin + sizeCancel.x + 2*horizontalBorder,
474 height + 2*BORDER);
475}
476
477void wxSearchCtrl::DoMoveWindow(int x, int y, int width, int height)
478{
479 wxSearchCtrlBase::DoMoveWindow(x, y, width, height);
480
481 LayoutControls(0, 0, width, height);
482}
483
484void wxSearchCtrl::LayoutControls(int x, int y, int width, int height)
485{
af1f44f1
RD
486 if ( !m_text )
487 return;
88c23b64 488
3f7f284d
RD
489 wxSize sizeText = m_text->GetBestSize();
490 // make room for the search menu & clear button
6646ca90 491 int horizontalBorder = ( sizeText.y - sizeText.y * 14 / 21 ) / 2;
3f7f284d
RD
492 x += horizontalBorder;
493 y += BORDER;
494 width -= horizontalBorder*2;
495 height -= BORDER*2;
d3ad3974
PC
496 if (width < 0) width = 0;
497 if (height < 0) height = 0;
3f7f284d
RD
498
499 wxSize sizeSearch(0,0);
500 wxSize sizeCancel(0,0);
501 int searchMargin = 0;
502 int cancelMargin = 0;
cfddeb10 503 if ( m_searchButtonVisible || HasMenu() )
3f7f284d
RD
504 {
505 sizeSearch = m_searchButton->GetBestSize();
506 searchMargin = MARGIN;
507 }
508 if ( m_cancelButtonVisible )
509 {
510 sizeCancel = m_cancelButton->GetBestSize();
511 cancelMargin = MARGIN;
512 }
cfddeb10 513 m_searchButton->Show( m_searchButtonVisible || HasMenu() );
3f7f284d
RD
514 m_cancelButton->Show( m_cancelButtonVisible );
515
516 if ( sizeSearch.x + sizeCancel.x > width )
517 {
518 sizeSearch.x = width/2;
519 sizeCancel.x = width/2;
520 searchMargin = 0;
521 cancelMargin = 0;
522 }
e6d0599d 523 wxCoord textWidth = width - sizeSearch.x - sizeCancel.x - searchMargin - cancelMargin - 1;
d3ad3974 524 if (textWidth < 0) textWidth = 0;
3f7f284d
RD
525
526 // position the subcontrols inside the client area
527
6646ca90 528 m_searchButton->SetSize(x, y + ICON_OFFSET - 1, sizeSearch.x, height);
88c23b64 529 m_text->SetSize( x + sizeSearch.x + searchMargin,
10eeb875 530 y + ICON_OFFSET - BORDER,
88c23b64 531 textWidth,
10eeb875 532 height);
3f7f284d 533 m_cancelButton->SetSize(x + sizeSearch.x + searchMargin + textWidth + cancelMargin,
6646ca90 534 y + ICON_OFFSET - 1, sizeCancel.x, height);
3f7f284d
RD
535}
536
35368572
VZ
537wxWindowList wxSearchCtrl::GetCompositeWindowParts() const
538{
539 wxWindowList parts;
540 parts.push_back(m_text);
541 parts.push_back(m_searchButton);
542 parts.push_back(m_cancelButton);
543 return parts;
544}
3f7f284d
RD
545
546// accessors
547// ---------
548
135b23b2 549wxString wxSearchCtrl::DoGetValue() const
3f7f284d 550{
7ecc7eb6 551 return m_text->GetValue();
3f7f284d 552}
3f7f284d
RD
553wxString wxSearchCtrl::GetRange(long from, long to) const
554{
555 return m_text->GetRange(from, to);
556}
557
558int wxSearchCtrl::GetLineLength(long lineNo) const
559{
560 return m_text->GetLineLength(lineNo);
561}
562wxString wxSearchCtrl::GetLineText(long lineNo) const
563{
564 return m_text->GetLineText(lineNo);
565}
566int wxSearchCtrl::GetNumberOfLines() const
567{
568 return m_text->GetNumberOfLines();
569}
570
571bool wxSearchCtrl::IsModified() const
572{
573 return m_text->IsModified();
574}
575bool wxSearchCtrl::IsEditable() const
576{
577 return m_text->IsEditable();
578}
579
580// more readable flag testing methods
581bool wxSearchCtrl::IsSingleLine() const
582{
583 return m_text->IsSingleLine();
584}
585bool wxSearchCtrl::IsMultiLine() const
586{
587 return m_text->IsMultiLine();
588}
589
590// If the return values from and to are the same, there is no selection.
591void wxSearchCtrl::GetSelection(long* from, long* to) const
592{
593 m_text->GetSelection(from, to);
594}
595
596wxString wxSearchCtrl::GetStringSelection() const
597{
598 return m_text->GetStringSelection();
599}
600
601// operations
602// ----------
603
604// editing
605void wxSearchCtrl::Clear()
606{
607 m_text->Clear();
608}
609void wxSearchCtrl::Replace(long from, long to, const wxString& value)
610{
611 m_text->Replace(from, to, value);
612}
613void wxSearchCtrl::Remove(long from, long to)
614{
615 m_text->Remove(from, to);
616}
617
618// load/save the controls contents from/to the file
619bool wxSearchCtrl::LoadFile(const wxString& file)
620{
621 return m_text->LoadFile(file);
622}
623bool wxSearchCtrl::SaveFile(const wxString& file)
624{
625 return m_text->SaveFile(file);
626}
627
628// sets/clears the dirty flag
629void wxSearchCtrl::MarkDirty()
630{
631 m_text->MarkDirty();
632}
633void wxSearchCtrl::DiscardEdits()
634{
635 m_text->DiscardEdits();
636}
637
638// set the max number of characters which may be entered in a single line
639// text control
640void wxSearchCtrl::SetMaxLength(unsigned long len)
641{
642 m_text->SetMaxLength(len);
643}
644
645// writing text inserts it at the current position, appending always
646// inserts it at the end
647void wxSearchCtrl::WriteText(const wxString& text)
648{
649 m_text->WriteText(text);
650}
651void wxSearchCtrl::AppendText(const wxString& text)
652{
653 m_text->AppendText(text);
654}
655
656// insert the character which would have resulted from this key event,
657// return true if anything has been inserted
658bool wxSearchCtrl::EmulateKeyPress(const wxKeyEvent& event)
659{
660 return m_text->EmulateKeyPress(event);
661}
662
663// text control under some platforms supports the text styles: these
664// methods allow to apply the given text style to the given selection or to
665// set/get the style which will be used for all appended text
666bool wxSearchCtrl::SetStyle(long start, long end, const wxTextAttr& style)
667{
668 return m_text->SetStyle(start, end, style);
669}
670bool wxSearchCtrl::GetStyle(long position, wxTextAttr& style)
671{
672 return m_text->GetStyle(position, style);
673}
674bool wxSearchCtrl::SetDefaultStyle(const wxTextAttr& style)
675{
676 return m_text->SetDefaultStyle(style);
677}
678const wxTextAttr& wxSearchCtrl::GetDefaultStyle() const
679{
680 return m_text->GetDefaultStyle();
681}
682
683// translate between the position (which is just an index in the text ctrl
684// considering all its contents as a single strings) and (x, y) coordinates
685// which represent column and line.
686long wxSearchCtrl::XYToPosition(long x, long y) const
687{
688 return m_text->XYToPosition(x, y);
689}
690bool wxSearchCtrl::PositionToXY(long pos, long *x, long *y) const
691{
692 return m_text->PositionToXY(pos, x, y);
693}
694
695void wxSearchCtrl::ShowPosition(long pos)
696{
697 m_text->ShowPosition(pos);
698}
699
700// find the character at position given in pixels
701//
702// NB: pt is in device coords (not adjusted for the client area origin nor
703// scrolling)
704wxTextCtrlHitTestResult wxSearchCtrl::HitTest(const wxPoint& pt, long *pos) const
705{
706 return m_text->HitTest(pt, pos);
707}
708wxTextCtrlHitTestResult wxSearchCtrl::HitTest(const wxPoint& pt,
709 wxTextCoord *col,
710 wxTextCoord *row) const
711{
712 return m_text->HitTest(pt, col, row);
713}
714
715// Clipboard operations
716void wxSearchCtrl::Copy()
717{
718 m_text->Copy();
719}
720void wxSearchCtrl::Cut()
721{
722 m_text->Cut();
723}
724void wxSearchCtrl::Paste()
725{
726 m_text->Paste();
727}
728
729bool wxSearchCtrl::CanCopy() const
730{
731 return m_text->CanCopy();
732}
733bool wxSearchCtrl::CanCut() const
734{
735 return m_text->CanCut();
736}
737bool wxSearchCtrl::CanPaste() const
738{
739 return m_text->CanPaste();
740}
741
742// Undo/redo
743void wxSearchCtrl::Undo()
744{
745 m_text->Undo();
746}
747void wxSearchCtrl::Redo()
748{
749 m_text->Redo();
750}
751
752bool wxSearchCtrl::CanUndo() const
753{
754 return m_text->CanUndo();
755}
756bool wxSearchCtrl::CanRedo() const
757{
758 return m_text->CanRedo();
759}
760
761// Insertion point
762void wxSearchCtrl::SetInsertionPoint(long pos)
763{
764 m_text->SetInsertionPoint(pos);
765}
766void wxSearchCtrl::SetInsertionPointEnd()
767{
768 m_text->SetInsertionPointEnd();
769}
770long wxSearchCtrl::GetInsertionPoint() const
771{
772 return m_text->GetInsertionPoint();
773}
0b6a49c2 774long wxSearchCtrl::GetLastPosition() const
3f7f284d
RD
775{
776 return m_text->GetLastPosition();
777}
778
779void wxSearchCtrl::SetSelection(long from, long to)
780{
781 m_text->SetSelection(from, to);
782}
783void wxSearchCtrl::SelectAll()
784{
785 m_text->SelectAll();
786}
787
788void wxSearchCtrl::SetEditable(bool editable)
789{
790 m_text->SetEditable(editable);
791}
792
793bool wxSearchCtrl::SetFont(const wxFont& font)
794{
35368572
VZ
795 if ( !wxSearchCtrlBase::SetFont(font) )
796 return false;
797
798 // Recreate the bitmaps as their size may have changed.
3f7f284d 799 RecalcBitmaps();
35368572
VZ
800
801 return true;
802}
803
804bool wxSearchCtrl::SetBackgroundColour(const wxColour& colour)
805{
806 if ( !wxSearchCtrlBase::SetBackgroundColour(colour) )
807 return false;
808
809 // When the background changes, re-render the bitmaps so that the correct
810 // colour shows in their "transparent" area.
811 RecalcBitmaps();
812
813 return true;
3f7f284d
RD
814}
815
816// search control generic only
817void wxSearchCtrl::SetSearchBitmap( const wxBitmap& bitmap )
818{
819 m_searchBitmap = bitmap;
a1b806b9 820 m_searchBitmapUser = bitmap.IsOk();
3f7f284d
RD
821 if ( m_searchBitmapUser )
822 {
cfddeb10 823 if ( m_searchButton && !HasMenu() )
3f7f284d
RD
824 {
825 m_searchButton->SetBitmapLabel( m_searchBitmap );
826 }
827 }
828 else
829 {
830 // the user bitmap was just cleared, generate one
831 RecalcBitmaps();
832 }
833}
834
cfddeb10
VZ
835#if wxUSE_MENUS
836
3f7f284d
RD
837void wxSearchCtrl::SetSearchMenuBitmap( const wxBitmap& bitmap )
838{
839 m_searchMenuBitmap = bitmap;
a1b806b9 840 m_searchMenuBitmapUser = bitmap.IsOk();
3f7f284d
RD
841 if ( m_searchMenuBitmapUser )
842 {
843 if ( m_searchButton && m_menu )
844 {
845 m_searchButton->SetBitmapLabel( m_searchMenuBitmap );
846 }
847 }
848 else
849 {
850 // the user bitmap was just cleared, generate one
851 RecalcBitmaps();
852 }
853}
854
cfddeb10
VZ
855#endif // wxUSE_MENUS
856
3f7f284d
RD
857void wxSearchCtrl::SetCancelBitmap( const wxBitmap& bitmap )
858{
859 m_cancelBitmap = bitmap;
a1b806b9 860 m_cancelBitmapUser = bitmap.IsOk();
3f7f284d
RD
861 if ( m_cancelBitmapUser )
862 {
863 if ( m_cancelButton )
864 {
865 m_cancelButton->SetBitmapLabel( m_cancelBitmap );
866 }
867 }
868 else
869 {
870 // the user bitmap was just cleared, generate one
871 RecalcBitmaps();
872 }
873}
874
875#if 0
876
877// override streambuf method
878#if wxHAS_TEXT_WINDOW_STREAM
879int overflow(int i);
880#endif // wxHAS_TEXT_WINDOW_STREAM
881
882// stream-like insertion operators: these are always available, whether we
883// were, or not, compiled with streambuf support
884wxTextCtrl& operator<<(const wxString& s);
885wxTextCtrl& operator<<(int i);
886wxTextCtrl& operator<<(long i);
887wxTextCtrl& operator<<(float f);
888wxTextCtrl& operator<<(double d);
889wxTextCtrl& operator<<(const wxChar c);
890#endif
891
bdb7278a
VZ
892void wxSearchCtrl::DoSetValue(const wxString& value, int flags)
893{
894 m_text->DoSetValue(value, flags);
895}
896
897bool wxSearchCtrl::DoLoadFile(const wxString& file, int fileType)
898{
899 return m_text->DoLoadFile(file, fileType);
900}
901
902bool wxSearchCtrl::DoSaveFile(const wxString& file, int fileType)
903{
904 return m_text->DoSaveFile(file, fileType);
905}
906
3f7f284d
RD
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();
c892ae14 939 wxColour fg = GetForegroundColour().ChangeLightness(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();
c892ae14 1044 wxColour fg = GetForegroundColour().ChangeLightness(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 (
a1b806b9 1140 !m_searchBitmap.IsOk() ||
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 (
a1b806b9 1158 !m_searchMenuBitmap.IsOk() ||
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 (
a1b806b9 1176 !m_cancelBitmap.IsOk() ||
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
639dbb6d 1188void wxSearchCtrl::OnCancelButton( wxCommandEvent& event )
3f7f284d 1189{
e5eff438 1190 m_text->Clear();
639dbb6d 1191 event.Skip();
3f7f284d
RD
1192}
1193
1194void wxSearchCtrl::OnSetFocus( wxFocusEvent& /*event*/ )
1195{
1196 if ( m_text )
1197 {
1198 m_text->SetFocus();
1199 }
1200}
1201
8ebf2465 1202void wxSearchCtrl::OnSize( wxSizeEvent& WXUNUSED(event) )
af1f44f1
RD
1203{
1204 int width, height;
1205 GetSize(&width, &height);
1206 LayoutControls(0, 0, width, height);
1207}
1208
cfddeb10
VZ
1209#if wxUSE_MENUS
1210
3f7f284d
RD
1211void wxSearchCtrl::PopupSearchMenu()
1212{
1213 if ( m_menu )
1214 {
1215 wxSize size = GetSize();
1216 PopupMenu( m_menu, 0, size.y );
1217 }
1218}
1219
cfddeb10
VZ
1220#endif // wxUSE_MENUS
1221
5b43c75c 1222#endif // !wxUSE_NATIVE_SEARCH_CONTROL
3f7f284d
RD
1223
1224#endif // wxUSE_SEARCHCTRL