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