]> git.saurik.com Git - wxWidgets.git/blob - samples/combo/combo.cpp
fix (harmless)m ingw32 warning; use anonymous namespace instead of a bunch of static...
[wxWidgets.git] / samples / combo / combo.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: combo.cpp
3 // Purpose: wxComboCtrl sample
4 // Author: Jaakko Salli
5 // Modified by:
6 // Created: Apr-30-2006
7 // RCS-ID: $Id$
8 // Copyright: (c) Jaakko Salli
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 #if !wxUSE_COMBOCTRL
34 #error "Please set wxUSE_COMBOCTRL to 1 and rebuild the library."
35 #endif
36
37 #include "wx/image.h"
38
39 #include "wx/combo.h"
40 #include "wx/odcombo.h"
41
42 // ----------------------------------------------------------------------------
43 // resources
44 // ----------------------------------------------------------------------------
45
46 // the application icon (under Windows and OS/2 it is in resources and even
47 // though we could still include the XPM here it would be unused)
48 #if !defined(__WXMSW__) && !defined(__WXPM__)
49 #include "../sample.xpm"
50 #endif
51
52 // ----------------------------------------------------------------------------
53 // private classes
54 // ----------------------------------------------------------------------------
55
56 // Define a new application type, each program should derive a class from wxApp
57 class MyApp : public wxApp
58 {
59 public:
60 // override base class virtuals
61 // ----------------------------
62
63 // this one is called on application startup and is a good place for the app
64 // initialization (doing it here and not in the ctor allows to have an error
65 // return: if OnInit() returns false, the application terminates)
66 virtual bool OnInit();
67 };
68
69 // Define a new frame type: this is going to be our main frame
70 class MyFrame : public wxFrame
71 {
72 public:
73 // ctor and dtor
74 MyFrame(const wxString& title);
75 ~MyFrame();
76
77 // event handlers (these functions should _not_ be virtual)
78 void OnQuit(wxCommandEvent& event);
79 void OnAbout(wxCommandEvent& event);
80
81 void OnShowComparison( wxCommandEvent& event );
82
83 // log wxComboCtrl events
84 void OnComboBoxUpdate( wxCommandEvent& event );
85
86 void OnIdle( wxIdleEvent& event );
87
88
89 wxCheckBox* m_cbUseAnim;
90
91 protected:
92 wxTextCtrl* m_logWin;
93 wxLog* m_logOld;
94
95 // Common list of items for all dialogs.
96 wxArrayString m_arrItems;
97
98 private:
99 // any class wishing to process wxWidgets events must use this macro
100 DECLARE_EVENT_TABLE()
101 };
102
103 // ----------------------------------------------------------------------------
104 // constants
105 // ----------------------------------------------------------------------------
106
107 // IDs for the controls and the menu commands
108 enum
109 {
110 ComboControl_Compare = wxID_HIGHEST,
111
112 // menu items
113 ComboControl_Quit = wxID_EXIT,
114
115 // it is important for the id corresponding to the "About" command to have
116 // this standard value as otherwise it won't be handled properly under Mac
117 // (where it is special and put into the "Apple" menu)
118 ComboControl_About = wxID_ABOUT
119 };
120
121 // ----------------------------------------------------------------------------
122 // event tables and other macros for wxWidgets
123 // ----------------------------------------------------------------------------
124
125 // the event tables connect the wxWidgets events with the functions (event
126 // handlers) which process them. It can be also done at run-time, but for the
127 // simple menu events like this the static method is much simpler.
128 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
129 EVT_TEXT(wxID_ANY,MyFrame::OnComboBoxUpdate)
130 EVT_COMBOBOX(wxID_ANY,MyFrame::OnComboBoxUpdate)
131
132 EVT_MENU(ComboControl_Compare, MyFrame::OnShowComparison)
133 EVT_MENU(ComboControl_Quit, MyFrame::OnQuit)
134 EVT_MENU(ComboControl_About, MyFrame::OnAbout)
135
136 EVT_IDLE(MyFrame::OnIdle)
137 END_EVENT_TABLE()
138
139 // Create a new application object: this macro will allow wxWidgets to create
140 // the application object during program execution (it's better than using a
141 // static object for many reasons) and also implements the accessor function
142 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
143 // not wxApp)
144 IMPLEMENT_APP(MyApp)
145
146 // ============================================================================
147 // implementation
148 // ============================================================================
149
150 // ----------------------------------------------------------------------------
151 // the application class
152 // ----------------------------------------------------------------------------
153
154 // 'Main program' equivalent: the program execution "starts" here
155 bool MyApp::OnInit()
156 {
157 if ( !wxApp::OnInit() )
158 return false;
159
160 // create the main application window
161 MyFrame *frame = new MyFrame(_T("wxComboCtrl and wxOwnerDrawnComboBox Sample"));
162
163 // and show it (the frames, unlike simple controls, are not shown when
164 // created initially)
165 frame->Show(true);
166
167 // success: wxApp::OnRun() will be called which will enter the main message
168 // loop and the application will run. If we returned false here, the
169 // application would exit immediately.
170 return true;
171 }
172
173
174 // ----------------------------------------------------------------------------
175 // wxOwnerDrawnComboBox with custom paint list items
176 // ----------------------------------------------------------------------------
177
178 class wxPenStyleComboBox : public wxOwnerDrawnComboBox
179 {
180 public:
181 virtual void OnDrawItem( wxDC& dc,
182 const wxRect& rect,
183 int item,
184 int flags ) const
185 {
186 if ( item == wxNOT_FOUND )
187 return;
188
189 wxRect r(rect);
190 r.Deflate(3);
191 r.height -= 2;
192
193 int penStyle = wxSOLID;
194 if ( item == 1 )
195 penStyle = wxTRANSPARENT;
196 else if ( item == 2 )
197 penStyle = wxDOT;
198 else if ( item == 3 )
199 penStyle = wxLONG_DASH;
200 else if ( item == 4 )
201 penStyle = wxSHORT_DASH;
202 else if ( item == 5 )
203 penStyle = wxDOT_DASH;
204 else if ( item == 6 )
205 penStyle = wxBDIAGONAL_HATCH;
206 else if ( item == 7 )
207 penStyle = wxCROSSDIAG_HATCH;
208 else if ( item == 8 )
209 penStyle = wxFDIAGONAL_HATCH;
210 else if ( item == 9 )
211 penStyle = wxCROSS_HATCH;
212 else if ( item == 10 )
213 penStyle = wxHORIZONTAL_HATCH;
214 else if ( item == 11 )
215 penStyle = wxVERTICAL_HATCH;
216
217 wxPen pen( dc.GetTextForeground(), 3, penStyle );
218
219 // Get text colour as pen colour
220 dc.SetPen( pen );
221
222 if ( !(flags & wxODCB_PAINTING_CONTROL) )
223 {
224 dc.DrawText(GetString( item ),
225 r.x + 3,
226 (r.y + 0) + ( (r.height/2) - dc.GetCharHeight() )/2
227 );
228
229 dc.DrawLine( r.x+5, r.y+((r.height/4)*3), r.x+r.width - 5, r.y+((r.height/4)*3) );
230 }
231 else
232 {
233 dc.DrawLine( r.x+5, r.y+r.height/2, r.x+r.width - 5, r.y+r.height/2 );
234 }
235 }
236
237 virtual void OnDrawBackground( wxDC& dc, const wxRect& rect,
238 int item, int flags ) const
239 {
240
241 // If item is selected or even, or we are painting the
242 // combo control itself, use the default rendering.
243 if ( (flags & (wxODCB_PAINTING_CONTROL|wxODCB_PAINTING_SELECTED)) ||
244 (item & 1) == 0 )
245 {
246 wxOwnerDrawnComboBox::OnDrawBackground(dc,rect,item,flags);
247 return;
248 }
249
250 // Otherwise, draw every other background with different colour.
251 wxColour bgCol(240,240,250);
252 dc.SetBrush(wxBrush(bgCol));
253 dc.SetPen(wxPen(bgCol));
254 dc.DrawRectangle(rect);
255 }
256
257 virtual wxCoord OnMeasureItem( size_t item ) const
258 {
259 // Simply demonstrate the ability to have variable-height items
260 if ( item & 1 )
261 return 36;
262 else
263 return 24;
264 }
265
266 virtual wxCoord OnMeasureItemWidth( size_t WXUNUSED(item) ) const
267 {
268 return -1; // default - will be measured from text width
269 }
270
271 };
272
273
274 // ----------------------------------------------------------------------------
275 // wxListView Custom popup interface
276 // ----------------------------------------------------------------------------
277
278 #include <wx/listctrl.h>
279
280 class ListViewComboPopup : public wxListView, public wxComboPopup
281 {
282 public:
283
284 virtual void Init()
285 {
286 m_value = -1;
287 m_itemHere = -1; // hot item in list
288 }
289
290 virtual bool Create( wxWindow* parent )
291 {
292 return wxListView::Create(parent,1,
293 wxPoint(0,0),wxDefaultSize,
294 wxLC_LIST|wxLC_SINGLE_SEL|
295 wxLC_SORT_ASCENDING|wxSIMPLE_BORDER);
296 }
297
298 virtual wxWindow *GetControl() { return this; }
299
300 virtual void SetStringValue( const wxString& s )
301 {
302 int n = wxListView::FindItem(-1,s);
303 if ( n >= 0 && n < GetItemCount() )
304 wxListView::Select(n);
305 }
306
307 virtual wxString GetStringValue() const
308 {
309 if ( m_value >= 0 )
310 return wxListView::GetItemText(m_value);
311 return wxEmptyString;
312 }
313
314 //
315 // Popup event handlers
316 //
317
318 // Mouse hot-tracking
319 void OnMouseMove(wxMouseEvent& event)
320 {
321 // Move selection to cursor if it is inside the popup
322
323 int resFlags;
324 int itemHere = HitTest(event.GetPosition(),resFlags);
325 if ( itemHere >= 0 )
326 {
327 wxListView::Select(itemHere,true);
328 m_itemHere = itemHere;
329 }
330 event.Skip();
331 }
332
333 // On mouse left, set the value and close the popup
334 void OnMouseClick(wxMouseEvent& WXUNUSED(event))
335 {
336 m_value = m_itemHere;
337 // TODO: Send event
338 Dismiss();
339 }
340
341 //
342 // Utilies for item manipulation
343 //
344
345 void AddSelection( const wxString& selstr )
346 {
347 wxListView::InsertItem(GetItemCount(),selstr);
348 }
349
350 protected:
351
352 int m_value; // current item index
353 int m_itemHere; // hot item in popup
354
355 private:
356 DECLARE_EVENT_TABLE()
357 };
358
359 BEGIN_EVENT_TABLE(ListViewComboPopup, wxListView)
360 EVT_MOTION(ListViewComboPopup::OnMouseMove)
361 // NOTE: Left down event is used instead of left up right now
362 // since MSW wxListCtrl doesn't seem to emit left ups
363 // consistently.
364 EVT_LEFT_DOWN(ListViewComboPopup::OnMouseClick)
365 END_EVENT_TABLE()
366
367
368 // ----------------------------------------------------------------------------
369 // wxTreeCtrl Custom popup interface
370 // ----------------------------------------------------------------------------
371
372 #include <wx/treectrl.h>
373
374 class TreeCtrlComboPopup : public wxTreeCtrl, public wxComboPopup
375 {
376 public:
377
378 virtual void Init()
379 {
380 }
381
382 virtual bool Create( wxWindow* parent )
383 {
384 return wxTreeCtrl::Create(parent,1,
385 wxPoint(0,0),wxDefaultSize,
386 wxTR_DEFAULT_STYLE | wxTR_HIDE_ROOT | wxSIMPLE_BORDER );
387 }
388
389 virtual void OnShow()
390 {
391 // make sure selected item is visible
392 if ( m_value.IsOk() )
393 EnsureVisible(m_value);
394 }
395
396 virtual wxSize GetAdjustedSize( int minWidth,
397 int WXUNUSED(prefHeight),
398 int maxHeight )
399 {
400 return wxSize(wxMax(300,minWidth),wxMin(250,maxHeight));
401 }
402
403 virtual wxWindow *GetControl() { return this; }
404
405 // Needed by SetStringValue
406 wxTreeItemId FindItemByText( wxTreeItemId parent, const wxString& text )
407 {
408 wxTreeItemIdValue cookie;
409 wxTreeItemId child = GetFirstChild(parent,cookie);
410 while ( child.IsOk() )
411 {
412 if ( GetItemText(child) == text )
413 {
414 return child;
415 }
416 if ( ItemHasChildren(child) )
417 {
418 wxTreeItemId found = FindItemByText(child,text);
419 if ( found.IsOk() )
420 return found;
421 }
422 child = GetNextChild(parent,cookie);
423 }
424 return wxTreeItemId();
425 }
426
427 virtual void SetStringValue( const wxString& s )
428 {
429 wxTreeItemId root = GetRootItem();
430 if ( !root.IsOk() )
431 return;
432
433 wxTreeItemId found = FindItemByText(root,s);
434 if ( found.IsOk() )
435 {
436 m_value = m_itemHere = found;
437 wxTreeCtrl::SelectItem(found);
438 }
439 }
440
441 virtual wxString GetStringValue() const
442 {
443 if ( m_value.IsOk() )
444 return wxTreeCtrl::GetItemText(m_value);
445 return wxEmptyString;
446 }
447
448 //
449 // Popup event handlers
450 //
451
452 // Mouse hot-tracking
453 void OnMouseMove(wxMouseEvent& event)
454 {
455 int resFlags;
456 wxTreeItemId itemHere = HitTest(event.GetPosition(),resFlags);
457 if ( itemHere.IsOk() && (resFlags & wxTREE_HITTEST_ONITEMLABEL) )
458 {
459 wxTreeCtrl::SelectItem(itemHere,true);
460 m_itemHere = itemHere;
461 }
462 event.Skip();
463 }
464
465 // On mouse left, set the value and close the popup
466 void OnMouseClick(wxMouseEvent& event)
467 {
468 int resFlags;
469 wxTreeItemId itemHere = HitTest(event.GetPosition(),resFlags);
470 if ( itemHere.IsOk() && (resFlags & wxTREE_HITTEST_ONITEMLABEL) )
471 {
472 m_itemHere = itemHere;
473 m_value = itemHere;
474 Dismiss();
475 // TODO: Send event
476 }
477 event.Skip();
478 }
479
480 protected:
481
482 wxTreeItemId m_value; // current item index
483 wxTreeItemId m_itemHere; // hot item in popup
484
485 private:
486 DECLARE_EVENT_TABLE()
487 };
488
489 BEGIN_EVENT_TABLE(TreeCtrlComboPopup, wxTreeCtrl)
490 EVT_MOTION(TreeCtrlComboPopup::OnMouseMove)
491 // NOTE: Left down event is used instead of left up right now
492 // since MSW wxTreeCtrl doesn't seem to emit left ups
493 // consistently.
494 EVT_LEFT_DOWN(TreeCtrlComboPopup::OnMouseClick)
495 END_EVENT_TABLE()
496
497 // ----------------------------------------------------------------------------
498 // wxComboCtrl with custom popup animation. We use EVT_TIMER, which is quite
499 // safe, but requires much more can than doing it in a single function (ie.
500 // AnimateShow) and using combination of wxSleep and wxSafeYield.
501 // ----------------------------------------------------------------------------
502
503 #if wxUSE_TIMER
504
505 #define CUSTOM_COMBOBOX_ANIMATION_DURATION 200 // In milliseconds
506
507 #include "wx/timer.h"
508
509 class wxComboCtrlWithCustomPopupAnim : public wxComboCtrl
510 {
511 public:
512
513 virtual bool AnimateShow( const wxRect& rect, int flags )
514 {
515 MyFrame* myFrame = (MyFrame*) ::wxGetTopLevelParent(this);
516
517 if ( !myFrame->m_cbUseAnim->GetValue() )
518 return true;
519
520 m_animStart = ::wxGetLocalTimeMillis();
521 m_animRect = rect;
522 m_animFlags = flags;
523
524 wxScreenDC dc;
525
526 wxBitmap bitmap( rect.width, rect.height, -1 );
527 wxMemoryDC memdc( bitmap );
528 memdc.Blit( 0, 0, rect.width, rect.height, &dc, rect.x, rect.y );
529 memdc.SelectObject(wxNullBitmap);
530 m_animBackBitmap = bitmap;
531
532 m_animTimer.SetOwner( this, wxID_ANY );
533 m_animTimer.Start( 10, wxTIMER_CONTINUOUS );
534
535 OnTimerEvent(*((wxTimerEvent*)NULL)); // Event is never used, so we can give NULL
536 return false;
537 }
538
539 void OnTimerEvent( wxTimerEvent& WXUNUSED(event) )
540 {
541 bool stopTimer = false;
542
543 wxWindow* popup = GetPopupControl()->GetControl();
544 wxScreenDC dc;
545 const wxRect& rect = m_animRect;
546
547 // Popup was hidden before it was fully shown?
548 if ( IsPopupWindowState(Hidden) )
549 {
550 stopTimer = true;
551 }
552 else
553 {
554 wxLongLong t = ::wxGetLocalTimeMillis();
555
556 int pos = (int) (t-m_animStart).GetLo();
557 if ( pos < CUSTOM_COMBOBOX_ANIMATION_DURATION )
558 {
559 //
560 // Actual animation happens here
561 //
562 int width = rect.width;
563 int height = rect.height;
564
565 int center_x = rect.x + (width/2);
566 int center_y = rect.y + (height/2);
567
568 double d_height = (double) height;
569
570 dc.SetPen( *wxBLACK_PEN );
571 dc.SetBrush( *wxTRANSPARENT_BRUSH );
572
573 int w = (((pos*256)/CUSTOM_COMBOBOX_ANIMATION_DURATION)*width)/256;
574
575 double ratio = ((double)w / (double)width);
576 int h = (int)(d_height * ratio);
577 dc.DrawBitmap( m_animBackBitmap, rect.x, rect.y );
578 dc.DrawRectangle( center_x - w/2, center_y - h/2, w, h );
579 }
580 else
581 {
582 stopTimer = true;
583 }
584 }
585
586 if ( stopTimer )
587 {
588 dc.DrawBitmap( m_animBackBitmap, rect.x, rect.y );
589 popup->Move( 0, 0 );
590 m_animTimer.Stop();
591 DoShowPopup( m_animRect, m_animFlags );
592 }
593 }
594
595 protected:
596
597 // Popup animation related
598 wxLongLong m_animStart;
599 wxTimer m_animTimer;
600 wxRect m_animRect;
601 wxBitmap m_animBackBitmap;
602 int m_animFlags;
603
604 private:
605 DECLARE_EVENT_TABLE()
606 };
607
608 BEGIN_EVENT_TABLE(wxComboCtrlWithCustomPopupAnim, wxComboCtrl)
609 EVT_TIMER(wxID_ANY, wxComboCtrlWithCustomPopupAnim::OnTimerEvent)
610 END_EVENT_TABLE()
611
612 #else
613
614 #define wxComboCtrlWithCustomPopupAnim wxComboCtrl
615
616 #endif
617
618 // ----------------------------------------------------------------------------
619 // wxComboCtrl with entirely custom button action (opens file dialog)
620 // ----------------------------------------------------------------------------
621
622 class wxFileSelectorCombo : public wxComboCtrl
623 {
624 public:
625 wxFileSelectorCombo() : wxComboCtrl() { Init(); }
626
627 wxFileSelectorCombo(wxWindow *parent,
628 wxWindowID id = wxID_ANY,
629 const wxString& value = wxEmptyString,
630 const wxPoint& pos = wxDefaultPosition,
631 const wxSize& size = wxDefaultSize,
632 long style = 0,
633 const wxValidator& validator = wxDefaultValidator,
634 const wxString& name = wxComboBoxNameStr)
635 : wxComboCtrl()
636 {
637 Init();
638 Create(parent,id,value,
639 pos,size,
640 // Style flag wxCC_STD_BUTTON makes the button
641 // behave more like a standard push button.
642 style | wxCC_STD_BUTTON,
643 validator,name);
644
645 //
646 // Prepare custom button bitmap (just '...' text)
647 wxMemoryDC dc;
648 wxBitmap bmp(12,16);
649 dc.SelectObject(bmp);
650
651 // Draw transparent background
652 wxColour magic(255,0,255);
653 wxBrush magicBrush(magic);
654 dc.SetBrush( magicBrush );
655 dc.SetPen( *wxTRANSPARENT_PEN );
656 dc.DrawRectangle(0,0,bmp.GetWidth(),bmp.GetHeight());
657
658 // Draw text
659 wxString str = wxT("...");
660 int w,h;
661 dc.GetTextExtent(str, &w, &h, 0, 0);
662 dc.DrawText(str, (bmp.GetWidth()-w)/2, (bmp.GetHeight()-h)/2);
663
664 dc.SelectObject( wxNullBitmap );
665
666 // Finalize transparency with a mask
667 wxMask *mask = new wxMask( bmp, magic );
668 bmp.SetMask( mask );
669
670 SetButtonBitmaps(bmp,true);
671 }
672
673 virtual void OnButtonClick()
674 {
675 // Show standard wxFileDialog on button click
676
677 wxFileDialog dlg(this,
678 wxT("Choose File"),
679 wxEmptyString,
680 GetValue(),
681 wxT("All files (*.*)|*.*"),
682 wxFD_OPEN);
683
684 if ( dlg.ShowModal() == wxID_OK )
685 {
686 SetValue(dlg.GetPath());
687 }
688 }
689
690 // Implement empty DoSetPopupControl to prevent assertion failure.
691 virtual void DoSetPopupControl(wxComboPopup* WXUNUSED(popup))
692 {
693 }
694
695 private:
696 void Init()
697 {
698 // Initialize member variables here
699 }
700 };
701
702 // ----------------------------------------------------------------------------
703 // main frame
704 // ----------------------------------------------------------------------------
705
706 // frame constructor
707 MyFrame::MyFrame(const wxString& title)
708 : wxFrame(NULL, wxID_ANY, title)
709 {
710 wxBoxSizer* topSizer;
711 wxBoxSizer* topRowSizer;
712 wxBoxSizer* colSizer;
713 wxBoxSizer* rowSizer;
714
715 // set the frame icon
716 SetIcon(wxICON(sample));
717
718 #if wxUSE_MENUS
719 // create a menu bar
720 wxMenu *fileMenu = new wxMenu;
721
722 // the "About" item should be in the help menu
723 wxMenu *helpMenu = new wxMenu;
724 helpMenu->Append(ComboControl_About, _T("&About...\tF1"), _T("Show about dialog"));
725
726 fileMenu->Append(ComboControl_Compare, _T("&Compare against wxComboBox..."),
727 _T("Show some wxOwnerDrawnComboBoxes side-by-side with native wxComboBoxes."));
728 fileMenu->AppendSeparator();
729 fileMenu->Append(ComboControl_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
730
731 // now append the freshly created menu to the menu bar...
732 wxMenuBar *menuBar = new wxMenuBar();
733 menuBar->Append(fileMenu, _T("&File"));
734 menuBar->Append(helpMenu, _T("&Help"));
735
736 // ... and attach this menu bar to the frame
737 SetMenuBar(menuBar);
738 #endif // wxUSE_MENUS
739
740 wxPanel* panel = new wxPanel(this);
741
742 // Prepare log window right away since it shows EVT_TEXTs
743 m_logWin = new wxTextCtrl( panel, 105, wxEmptyString, wxDefaultPosition,
744 wxSize(-1,125), wxTE_MULTILINE|wxFULL_REPAINT_ON_RESIZE );
745 m_logWin->SetEditable(false);
746 wxLogTextCtrl* logger = new wxLogTextCtrl( m_logWin );
747 m_logOld = logger->SetActiveTarget( logger );
748 logger->DisableTimestamp();
749
750
751 topSizer = new wxBoxSizer( wxVERTICAL );
752
753 topRowSizer = new wxBoxSizer( wxHORIZONTAL );
754
755 colSizer = new wxBoxSizer( wxVERTICAL );
756
757
758 wxComboCtrl* cc;
759 wxGenericComboCtrl* gcc;
760 wxOwnerDrawnComboBox* odc;
761
762 // Create common strings array
763 m_arrItems.Add( wxT("Solid") );
764 m_arrItems.Add( wxT("Transparent") );
765 m_arrItems.Add( wxT("Dot") );
766 m_arrItems.Add( wxT("Long Dash") );
767 m_arrItems.Add( wxT("Short Dash") );
768 m_arrItems.Add( wxT("Dot Dash") );
769 m_arrItems.Add( wxT("Backward Diagonal Hatch") );
770 m_arrItems.Add( wxT("Cross-diagonal Hatch") );
771 m_arrItems.Add( wxT("Forward Diagonal Hatch") );
772 m_arrItems.Add( wxT("Cross Hatch") );
773 m_arrItems.Add( wxT("Horizontal Hatch") );
774 m_arrItems.Add( wxT("Vertical Hatch") );
775
776
777 //
778 // Create pen selector ODComboBox with owner-drawn items
779 //
780 rowSizer = new wxBoxSizer( wxHORIZONTAL );
781 rowSizer->Add( new wxStaticText(panel,wxID_ANY,
782 wxT("OwnerDrawnComboBox with owner-drawn items:")), 1,
783 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
784 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
785
786 rowSizer = new wxBoxSizer( wxHORIZONTAL );
787
788
789 // When defining derivative class for callbacks, we need
790 // to use two-stage creation (or redefine the common wx
791 // constructor).
792 odc = new wxPenStyleComboBox();
793 odc->Create(panel,wxID_ANY,wxEmptyString,
794 wxDefaultPosition, wxDefaultSize,
795 m_arrItems,
796 wxCB_READONLY //wxNO_BORDER | wxCB_READONLY
797 );
798
799
800 odc->SetSelection(0);
801
802 rowSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 4 );
803 rowSizer->AddStretchSpacer(1);
804 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
805
806
807
808 //
809 // Same but with changed button position
810 //
811 rowSizer = new wxBoxSizer( wxHORIZONTAL );
812 rowSizer->Add( new wxStaticText(panel,wxID_ANY,
813 wxT("OwnerDrawnComboBox with owner-drawn items and button on the left:")), 1,
814 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
815 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
816
817 rowSizer = new wxBoxSizer( wxHORIZONTAL );
818
819
820 // When defining derivative class for callbacks, we need
821 // to use two-stage creation (or redefine the common wx
822 // constructor).
823 odc = new wxPenStyleComboBox();
824 odc->Create(panel,wxID_ANY,wxEmptyString,
825 wxDefaultPosition, wxDefaultSize,
826 m_arrItems,
827 wxCB_READONLY //wxNO_BORDER | wxCB_READONLY
828 );
829
830
831 odc->SetSelection(0);
832
833 // Use button size that is slightly smaller than the default.
834 wxSize butSize = odc->GetButtonSize();
835 odc->SetButtonPosition(butSize.x - 2, // button width
836 butSize.y - 6, // button height
837 wxLEFT, // side
838 2 // horizontal spacing
839 );
840
841 rowSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 4 );
842 rowSizer->AddStretchSpacer(1);
843 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
844
845
846 //
847 // List View wxComboCtrl
848 //
849
850 rowSizer = new wxBoxSizer( wxHORIZONTAL );
851 rowSizer->Add( new wxStaticText(panel,wxID_ANY,wxT("List View wxComboCtrl:")), 1,
852 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
853 rowSizer->Add( new wxStaticText(panel,wxID_ANY,wxT("Tree Ctrl wxComboControl:")), 1,
854 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
855 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
856
857 rowSizer = new wxBoxSizer( wxHORIZONTAL );
858 cc = new wxComboCtrlWithCustomPopupAnim();
859 cc->Create(panel, wxID_ANY, wxEmptyString);
860
861 // Make sure we use popup that allows focusing the listview.
862 cc->UseAltPopupWindow();
863
864 cc->SetPopupMinWidth(300);
865
866 ListViewComboPopup* iface = new ListViewComboPopup();
867 cc->SetPopupControl(iface);
868
869 int i;
870 for ( i=0; i<100; i++ )
871 iface->AddSelection( wxString::Format(wxT("Item %02i"),i));
872
873 rowSizer->Add( cc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
874
875
876 //
877 // Tree Ctrl wxComboCtrl
878 //
879
880 // Note that we test that wxGenericComboCtrl works
881 gcc = new wxGenericComboCtrl(panel,wxID_ANY,wxEmptyString,
882 wxDefaultPosition, wxDefaultSize);
883
884 // Make sure we use popup that allows focusing the treectrl.
885 gcc->UseAltPopupWindow();
886
887 // Set popup interface right away, otherwise some of the calls
888 // below may fail
889 TreeCtrlComboPopup* tcPopup = new TreeCtrlComboPopup();
890 gcc->SetPopupControl(tcPopup);
891
892 // Add items using wxTreeCtrl methods directly
893 wxTreeItemId rootId = tcPopup->AddRoot(wxT("<hidden_root>"));
894
895 wxTreeItemId groupId;
896
897 for ( i=0; i<4; i++ )
898 {
899 groupId = tcPopup->AppendItem(rootId,
900 wxString::Format(wxT("Branch %02i"),i));
901
902 int n;
903 for ( n=0; n<25; n++ )
904 tcPopup->AppendItem(groupId,
905 wxString::Format(wxT("Subitem %02i"),(i*25)+n));
906 }
907
908 gcc->SetValue(wxT("Subitem 05"));
909
910 // Move button to left - it makes more sense for a tree ctrl
911 gcc->SetButtonPosition(-1, // button width
912 -1, // button height
913 wxLEFT, // side
914 0 // horizontal spacing
915 );
916
917 rowSizer->Add( gcc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
918
919 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
920
921 #if wxUSE_IMAGE
922 wxInitAllImageHandlers();
923
924 //
925 // Custom Dropbutton Bitmaps
926 // (second one uses blank button background)
927 //
928 rowSizer = new wxBoxSizer( wxHORIZONTAL );
929 rowSizer->Add( new wxStaticText(panel,wxID_ANY,
930 wxT("OwnerDrawnComboBox with simple dropbutton graphics:")), 1,
931 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
932
933 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
934
935 rowSizer = new wxBoxSizer( wxHORIZONTAL );
936
937 odc = new wxOwnerDrawnComboBox(panel,wxID_ANY,wxEmptyString,
938 wxDefaultPosition, wxDefaultSize,
939 m_arrItems,
940 (long)0 // wxCB_SORT // wxNO_BORDER | wxCB_READONLY
941 );
942
943 wxOwnerDrawnComboBox* odc2;
944 odc2 = new wxOwnerDrawnComboBox(panel,wxID_ANY,wxEmptyString,
945 wxDefaultPosition, wxDefaultSize,
946 m_arrItems,
947 (long)0 // wxCB_SORT // wxNO_BORDER | wxCB_READONLY
948 );
949
950 // Load images from disk
951 wxImage imgNormal(wxT("dropbutn.png"));
952 wxImage imgPressed(wxT("dropbutp.png"));
953 wxImage imgHover(wxT("dropbuth.png"));
954
955 if ( imgNormal.IsOk() && imgPressed.IsOk() && imgHover.IsOk() )
956 {
957 wxBitmap bmpNormal(imgNormal);
958 wxBitmap bmpPressed(imgPressed);
959 wxBitmap bmpHover(imgHover);
960 odc->SetButtonBitmaps(bmpNormal,false,bmpPressed,bmpHover);
961 odc2->SetButtonBitmaps(bmpNormal,true,bmpPressed,bmpHover);
962 }
963 else
964 wxLogError(wxT("Dropbutton images not found"));
965
966 //odc2->SetButtonPosition(0, // width adjustment
967 // 0, // height adjustment
968 // wxLEFT, // side
969 // 0 // horizontal spacing
970 // );
971
972 rowSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 4 );
973 rowSizer->Add( odc2, 1, wxALIGN_CENTER_VERTICAL|wxALL, 4 );
974 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
975 #endif
976
977
978 //
979 // wxComboCtrl with totally custom button action (open file dialog)
980 //
981 rowSizer = new wxBoxSizer( wxHORIZONTAL );
982 rowSizer->Add( new wxStaticText(panel,wxID_ANY,
983 wxT("wxComboCtrl with custom button action:")), 1,
984 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
985
986
987 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
988
989 rowSizer = new wxBoxSizer( wxHORIZONTAL );
990 wxFileSelectorCombo* fsc;
991
992 fsc = new wxFileSelectorCombo(panel,wxID_ANY,wxEmptyString,
993 wxDefaultPosition, wxDefaultSize,
994 (long)0
995 );
996
997 rowSizer->Add( fsc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 4 );
998 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
999
1000
1001 // Make sure GetFeatures is implemented
1002 wxComboCtrl::GetFeatures();
1003
1004
1005 topRowSizer->Add( colSizer, 1, wxALL, 2 );
1006
1007 colSizer = new wxBoxSizer( wxVERTICAL );
1008
1009 wxStaticBoxSizer* sbSizer = new wxStaticBoxSizer( new wxStaticBox(panel,
1010 wxID_ANY,
1011 wxT("Options")),
1012 wxVERTICAL );
1013
1014 m_cbUseAnim = new wxCheckBox(panel, wxID_ANY, wxT("Custom popup animation for ListView wxComboCtrl"));
1015 m_cbUseAnim->SetValue(true);
1016 sbSizer->Add( m_cbUseAnim, 0, wxALL, 3 );
1017
1018 colSizer->Add( sbSizer, 0, wxEXPAND|wxALL, 3 );
1019 colSizer->AddSpacer(8);
1020 colSizer->Add( new wxStaticText(panel, wxID_ANY, wxT("Log Messages:")), 0, wxTOP|wxLEFT, 3 );
1021 colSizer->Add( m_logWin, 1, wxEXPAND|wxALL, 3 );
1022
1023 topRowSizer->Add( colSizer, 1, wxEXPAND|wxALL, 2 );
1024 topSizer->Add( topRowSizer, 1, wxEXPAND );
1025
1026 panel->SetSizer( topSizer );
1027 topSizer->SetSizeHints( panel );
1028
1029 SetSize(740,400);
1030 Centre();
1031 }
1032
1033 // event handlers
1034
1035 void MyFrame::OnComboBoxUpdate( wxCommandEvent& event )
1036 {
1037 // Don't show messages for the log output window (it'll crash)
1038 if ( !event.GetEventObject()->IsKindOf(CLASSINFO(wxComboCtrl)) )
1039 return;
1040
1041 if ( event.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED )
1042 wxLogDebug(wxT("EVT_COMBOBOX(id=%i,selection=%i)"),event.GetId(),event.GetSelection());
1043 else if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED )
1044 wxLogDebug(wxT("EVT_TEXT(id=%i,string=\"%s\")"),event.GetId(),event.GetString().c_str());
1045 }
1046
1047 void MyFrame::OnShowComparison( wxCommandEvent& WXUNUSED(event) )
1048 {
1049 //
1050 // Show some wxOwnerDrawComboBoxes for comparison
1051 //
1052
1053 wxBoxSizer* colSizer;
1054 wxBoxSizer* rowSizer;
1055 wxStaticBoxSizer* groupSizer;
1056
1057 wxComboBox* cb;
1058 wxOwnerDrawnComboBox* odc;
1059
1060 const int border = 4;
1061
1062 wxDialog* dlg = new wxDialog(this,wxID_ANY,
1063 wxT("Compare against wxComboBox"),
1064 wxDefaultPosition,wxDefaultSize,
1065 wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER);
1066
1067 colSizer = new wxBoxSizer( wxVERTICAL );
1068
1069 rowSizer = new wxBoxSizer(wxHORIZONTAL);
1070
1071 groupSizer = new wxStaticBoxSizer(new wxStaticBox(dlg,wxID_ANY,wxT(" wxOwnerDrawnComboBox ")),
1072 wxVERTICAL);
1073
1074 groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Writable, sorted:")), 0,
1075 wxALIGN_CENTER_VERTICAL|wxRIGHT|wxEXPAND, border );
1076
1077 odc = new wxOwnerDrawnComboBox(dlg,wxID_ANY,wxEmptyString,
1078 wxDefaultPosition, wxDefaultSize,
1079 m_arrItems,
1080 wxCB_SORT // wxNO_BORDER|wxCB_READONLY
1081 );
1082
1083 odc->Append(wxT("H - Appended Item")); // test sorting in append
1084
1085 odc->SetValue(wxT("Dot Dash"));
1086
1087 groupSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
1088
1089 //
1090 // Readonly ODComboBox
1091 groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Read-only:")), 0,
1092 wxALIGN_CENTER_VERTICAL|wxRIGHT, border );
1093
1094 odc = new wxOwnerDrawnComboBox(dlg,wxID_ANY,wxEmptyString,
1095 wxDefaultPosition, wxDefaultSize,
1096 m_arrItems,
1097 wxCB_SORT|wxCB_READONLY // wxNO_BORDER|wxCB_READONLY
1098 );
1099
1100 odc->SetValue(wxT("Dot Dash"));
1101 odc->SetText(wxT("Dot Dash (Testing SetText)"));
1102
1103 groupSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
1104
1105 //
1106 // Disabled ODComboBox
1107 groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Disabled:")), 0,
1108 wxALIGN_CENTER_VERTICAL|wxRIGHT, border );
1109
1110 odc = new wxOwnerDrawnComboBox(dlg,wxID_ANY,wxEmptyString,
1111 wxDefaultPosition, wxDefaultSize,
1112 m_arrItems,
1113 wxCB_SORT|wxCB_READONLY // wxNO_BORDER|wxCB_READONLY
1114 );
1115
1116 odc->SetValue(wxT("Dot Dash"));
1117 odc->Enable(false);
1118
1119 groupSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
1120
1121 rowSizer->Add( groupSizer, 1, wxEXPAND|wxALL, border );
1122
1123
1124 groupSizer = new wxStaticBoxSizer(new wxStaticBox(dlg,wxID_ANY,wxT(" wxComboBox ")),
1125 wxVERTICAL);
1126
1127 //
1128 // wxComboBox
1129 //
1130 groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Writable, sorted:")), 0,
1131 wxALIGN_CENTER_VERTICAL|wxRIGHT|wxEXPAND, border );
1132
1133 cb = new wxComboBox(dlg,wxID_ANY,wxEmptyString,
1134 wxDefaultPosition, wxDefaultSize,
1135 m_arrItems,
1136 wxCB_SORT // wxNO_BORDER|wxCB_READONLY
1137 );
1138
1139 cb->Append(wxT("H - Appended Item")); // test sorting in append
1140
1141 cb->SetValue(wxT("Dot Dash"));
1142
1143 groupSizer->Add( cb, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
1144
1145 //
1146 // Readonly wxComboBox
1147 groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Read-only:")), 0,
1148 wxALIGN_CENTER_VERTICAL|wxRIGHT, border );
1149
1150 cb = new wxComboBox(dlg,wxID_ANY,wxEmptyString,
1151 wxDefaultPosition, wxDefaultSize,
1152 m_arrItems,
1153 wxCB_SORT|wxCB_READONLY // wxNO_BORDER|wxCB_READONLY
1154 );
1155
1156 cb->SetValue(wxT("Dot Dash"));
1157
1158 groupSizer->Add( cb, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
1159
1160 //
1161 // Disabled wxComboBox
1162 groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Disabled:")), 0,
1163 wxALIGN_CENTER_VERTICAL|wxRIGHT, border );
1164
1165 cb = new wxComboBox(dlg,wxID_ANY,wxEmptyString,
1166 wxDefaultPosition, wxDefaultSize,
1167 m_arrItems,
1168 wxCB_SORT|wxCB_READONLY // wxNO_BORDER|wxCB_READONLY
1169 );
1170
1171 cb->SetValue(wxT("Dot Dash"));
1172 cb->Enable(false);
1173
1174 groupSizer->Add( cb, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
1175
1176 rowSizer->Add( groupSizer, 1, wxEXPAND|wxALL, border );
1177
1178 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, border );
1179
1180 dlg->SetSizer( colSizer );
1181 colSizer->SetSizeHints( dlg );
1182
1183 dlg->SetSize(60,240);
1184 dlg->Centre();
1185 dlg->ShowModal();
1186 }
1187
1188 MyFrame::~MyFrame()
1189 {
1190 delete wxLog::SetActiveTarget(m_logOld);
1191 }
1192
1193 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
1194 {
1195 // true is to force the frame to close
1196 Close(true);
1197 }
1198
1199 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
1200 {
1201 wxMessageBox(wxString::Format(
1202 _T("Welcome to %s!\n")
1203 _T("\n")
1204 _T("This is the wxWidgets wxComboCtrl and wxOwnerDrawnComboBox sample\n")
1205 _T("running under %s."),
1206 wxVERSION_STRING,
1207 wxGetOsDescription().c_str()
1208 ),
1209 _T("About wxComboCtrl sample"),
1210 wxOK | wxICON_INFORMATION,
1211 this);
1212 }
1213
1214 void MyFrame::OnIdle(wxIdleEvent& event)
1215 {
1216 // This code is useful for debugging focus problems
1217 // (which are plentiful when dealing with popup windows).
1218 #if 0
1219 static wxWindow* lastFocus = (wxWindow*) NULL;
1220
1221 wxWindow* curFocus = ::wxWindow::FindFocus();
1222
1223 if ( curFocus != lastFocus )
1224 {
1225 const wxChar* className = wxT("<none>");
1226 if ( curFocus )
1227 className = curFocus->GetClassInfo()->GetClassName();
1228 lastFocus = curFocus;
1229 wxLogDebug( wxT("FOCUSED: %s %X"),
1230 className,
1231 (unsigned int)curFocus);
1232 }
1233 #endif
1234
1235 event.Skip();
1236 }