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