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