File/dir dialog styles and other changes (patch 1488371):
[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 // log wxComboCtrl events
82 void OnComboBoxUpdate( wxCommandEvent& event );
83
84 protected:
85 wxTextCtrl* m_logWin;
86 wxLog* m_logOld;
87
88 private:
89 // any class wishing to process wxWidgets events must use this macro
90 DECLARE_EVENT_TABLE()
91 };
92
93 // ----------------------------------------------------------------------------
94 // constants
95 // ----------------------------------------------------------------------------
96
97 // IDs for the controls and the menu commands
98 enum
99 {
100 // menu items
101 ComboControl_Quit = wxID_EXIT,
102
103 // it is important for the id corresponding to the "About" command to have
104 // this standard value as otherwise it won't be handled properly under Mac
105 // (where it is special and put into the "Apple" menu)
106 ComboControl_About = wxID_ABOUT
107 };
108
109 // ----------------------------------------------------------------------------
110 // event tables and other macros for wxWidgets
111 // ----------------------------------------------------------------------------
112
113 // the event tables connect the wxWidgets events with the functions (event
114 // handlers) which process them. It can be also done at run-time, but for the
115 // simple menu events like this the static method is much simpler.
116 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
117 EVT_TEXT(wxID_ANY,MyFrame::OnComboBoxUpdate)
118 EVT_COMBOBOX(wxID_ANY,MyFrame::OnComboBoxUpdate)
119
120 EVT_MENU(ComboControl_Quit, MyFrame::OnQuit)
121 EVT_MENU(ComboControl_About, MyFrame::OnAbout)
122 END_EVENT_TABLE()
123
124 // Create a new application object: this macro will allow wxWidgets to create
125 // the application object during program execution (it's better than using a
126 // static object for many reasons) and also implements the accessor function
127 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
128 // not wxApp)
129 IMPLEMENT_APP(MyApp)
130
131 // ============================================================================
132 // implementation
133 // ============================================================================
134
135 // ----------------------------------------------------------------------------
136 // the application class
137 // ----------------------------------------------------------------------------
138
139 // 'Main program' equivalent: the program execution "starts" here
140 bool MyApp::OnInit()
141 {
142 // create the main application window
143 MyFrame *frame = new MyFrame(_T("wxComboCtrl Sample"));
144
145 // and show it (the frames, unlike simple controls, are not shown when
146 // created initially)
147 frame->Show(true);
148
149 // success: wxApp::OnRun() will be called which will enter the main message
150 // loop and the application will run. If we returned false here, the
151 // application would exit immediately.
152 return true;
153 }
154
155
156 // ----------------------------------------------------------------------------
157 // wxListView Custom popup interface
158 // ----------------------------------------------------------------------------
159
160 #include <wx/listctrl.h>
161
162 class ListViewComboPopup : public wxListView, public wxComboPopup
163 {
164 public:
165
166 /*
167 ListViewComboPopup(wxComboCtrlBase* combo)
168 : wxListView(), wxComboPopup(combo)
169 {
170 m_value = -1;
171 m_itemHere = -1; // hot item in list
172 }
173 */
174 virtual void Init()
175 {
176 m_value = -1;
177 m_itemHere = -1; // hot item in list
178 }
179
180 virtual bool Create( wxWindow* parent )
181 {
182 return wxListView::Create(parent,1,
183 wxPoint(0,0),wxDefaultSize,
184 wxLC_LIST|wxLC_SINGLE_SEL|
185 wxLC_SORT_ASCENDING|wxSIMPLE_BORDER);
186 }
187
188 virtual wxWindow *GetControl() { return this; }
189
190 virtual void SetStringValue( const wxString& s )
191 {
192 int n = wxListView::FindItem(-1,s);
193 if ( n >= 0 && n < GetItemCount() )
194 wxListView::Select(n);
195 }
196
197 virtual wxString GetStringValue() const
198 {
199 if ( m_value >= 0 )
200 return wxListView::GetItemText(m_value);
201 return wxEmptyString;
202 }
203
204 //
205 // Popup event handlers
206 //
207
208 // Mouse hot-tracking
209 void OnMouseMove(wxMouseEvent& event)
210 {
211 // Move selection to cursor if it is inside the popup
212
213 int resFlags;
214 int itemHere = HitTest(event.GetPosition(),resFlags);
215 if ( itemHere >= 0 )
216 {
217 wxListView::Select(itemHere,true);
218 m_itemHere = itemHere;
219 }
220 event.Skip();
221 }
222
223 // On mouse left, set the value and close the popup
224 void OnMouseClick(wxMouseEvent& WXUNUSED(event))
225 {
226 m_value = m_itemHere;
227 // TODO: Send event
228 Dismiss();
229 }
230
231 //
232 // Utilies for item manipulation
233 //
234
235 void AddSelection( const wxString& selstr )
236 {
237 wxListView::InsertItem(GetItemCount(),selstr);
238 }
239
240 protected:
241
242 int m_value; // current item index
243 int m_itemHere; // hot item in popup
244
245 private:
246 DECLARE_EVENT_TABLE()
247 };
248
249 BEGIN_EVENT_TABLE(ListViewComboPopup, wxListView)
250 EVT_MOTION(ListViewComboPopup::OnMouseMove)
251 // NOTE: Left down event is used instead of left up right now
252 // since MSW wxListCtrl doesn't seem to emit left ups
253 // consistently.
254 EVT_LEFT_DOWN(ListViewComboPopup::OnMouseClick)
255 END_EVENT_TABLE()
256
257
258 // ----------------------------------------------------------------------------
259 // wxTreeCtrl Custom popup interface
260 // ----------------------------------------------------------------------------
261
262 #include <wx/treectrl.h>
263
264 class TreeCtrlComboPopup : public wxTreeCtrl, public wxComboPopup
265 {
266 public:
267
268 /*
269 TreeCtrlComboPopup(wxComboCtrlBase* combo)
270 : wxTreeCtrl(), wxComboPopup(combo)
271 {
272 }
273 */
274
275 virtual void Init()
276 {
277 }
278
279 virtual bool Create( wxWindow* parent )
280 {
281 return wxTreeCtrl::Create(parent,1,
282 wxPoint(0,0),wxDefaultSize,
283 wxTR_HIDE_ROOT|wxTR_HAS_BUTTONS|
284 wxTR_SINGLE|wxTR_LINES_AT_ROOT|
285 wxSIMPLE_BORDER);
286 }
287
288 virtual void OnShow()
289 {
290 // make sure selected item is visible
291 if ( m_value.IsOk() )
292 EnsureVisible(m_value);
293 }
294
295 virtual wxSize GetAdjustedSize( int minWidth,
296 int WXUNUSED(prefHeight),
297 int maxHeight )
298 {
299 return wxSize(wxMax(300,minWidth),wxMin(250,maxHeight));
300 }
301
302 virtual wxWindow *GetControl() { return this; }
303
304 // Needed by SetStringValue
305 wxTreeItemId FindItemByText( wxTreeItemId parent, const wxString& text )
306 {
307 wxTreeItemIdValue cookie;
308 wxTreeItemId child = GetFirstChild(parent,cookie);
309 while ( child.IsOk() )
310 {
311 if ( GetItemText(child) == text )
312 {
313 return child;
314 }
315 if ( ItemHasChildren(child) )
316 {
317 wxTreeItemId found = FindItemByText(child,text);
318 if ( found.IsOk() )
319 return found;
320 }
321 child = GetNextChild(parent,cookie);
322 }
323 return wxTreeItemId();
324 }
325
326 virtual void SetStringValue( const wxString& s )
327 {
328 wxTreeItemId root = GetRootItem();
329 if ( !root.IsOk() )
330 return;
331
332 wxTreeItemId found = FindItemByText(root,s);
333 if ( found.IsOk() )
334 {
335 m_value = m_itemHere = found;
336 wxTreeCtrl::SelectItem(found);
337 }
338 }
339
340 virtual wxString GetStringValue() const
341 {
342 if ( m_value.IsOk() )
343 return wxTreeCtrl::GetItemText(m_value);
344 return wxEmptyString;
345 }
346
347 //
348 // Popup event handlers
349 //
350
351 // Mouse hot-tracking
352 void OnMouseMove(wxMouseEvent& event)
353 {
354 int resFlags;
355 wxTreeItemId itemHere = HitTest(event.GetPosition(),resFlags);
356 if ( itemHere.IsOk() && (resFlags & wxTREE_HITTEST_ONITEMLABEL) )
357 {
358 wxTreeCtrl::SelectItem(itemHere,true);
359 m_itemHere = itemHere;
360 }
361 event.Skip();
362 }
363
364 // On mouse left, set the value and close the popup
365 void OnMouseClick(wxMouseEvent& event)
366 {
367 int resFlags;
368 wxTreeItemId itemHere = HitTest(event.GetPosition(),resFlags);
369 if ( itemHere.IsOk() && (resFlags & wxTREE_HITTEST_ONITEMLABEL) )
370 {
371 m_itemHere = itemHere;
372 m_value = itemHere;
373 Dismiss();
374 // TODO: Send event
375 }
376 event.Skip();
377 }
378
379 protected:
380
381 wxTreeItemId m_value; // current item index
382 wxTreeItemId m_itemHere; // hot item in popup
383
384 private:
385 DECLARE_EVENT_TABLE()
386 };
387
388 BEGIN_EVENT_TABLE(TreeCtrlComboPopup, wxTreeCtrl)
389 EVT_MOTION(TreeCtrlComboPopup::OnMouseMove)
390 // NOTE: Left down event is used instead of left up right now
391 // since MSW wxTreeCtrl doesn't seem to emit left ups
392 // consistently.
393 EVT_LEFT_DOWN(TreeCtrlComboPopup::OnMouseClick)
394 END_EVENT_TABLE()
395
396 // ----------------------------------------------------------------------------
397 // wxOwnerDrawnComboBox with custom paint list items
398 // ----------------------------------------------------------------------------
399 /*
400 class wxPenStyleComboBox : public wxOwnerDrawnComboBox
401 {
402 public:
403 virtual bool OnDrawListItem( wxDC& dc, const wxRect& rect, int item, int flags )
404 {
405 wxRect r(rect);
406 r.Deflate(3);
407 r.height -= 2;
408
409 int pen_style = wxSOLID;
410 if ( item == 1 )
411 pen_style = wxTRANSPARENT;
412 else if ( item == 2 )
413 pen_style = wxDOT;
414 else if ( item == 3 )
415 pen_style = wxLONG_DASH;
416 else if ( item == 4 )
417 pen_style = wxSHORT_DASH;
418 else if ( item == 5 )
419 pen_style = wxDOT_DASH;
420 else if ( item == 6 )
421 pen_style = wxBDIAGONAL_HATCH;
422 else if ( item == 7 )
423 pen_style = wxCROSSDIAG_HATCH;
424 else if ( item == 8 )
425 pen_style = wxFDIAGONAL_HATCH;
426 else if ( item == 9 )
427 pen_style = wxCROSS_HATCH;
428 else if ( item == 10 )
429 pen_style = wxHORIZONTAL_HATCH;
430 else if ( item == 11 )
431 pen_style = wxVERTICAL_HATCH;
432
433 wxPen pen( dc.GetTextForeground(), 3, pen_style );
434
435 // Get text colour as pen colour
436 dc.SetPen ( pen );
437
438 if ( !(flags & wxCP_PAINTING_CONTROL) )
439 {
440 dc.DrawText(GetString( item ),
441 r.x + 3,
442 (r.y + 0) + ( (r.height/2) - dc.GetCharHeight() )/2
443 );
444
445 dc.DrawLine( r.x+5, r.y+((r.height/4)*3), r.x+r.width - 5, r.y+((r.height/4)*3) );
446 }
447 else
448 {
449 dc.DrawLine( r.x+5, r.y+r.height/2, r.x+r.width - 5, r.y+r.height/2 );
450 }
451
452 return true;
453 }
454
455 virtual wxCoord OnMeasureListItem( int WXUNUSED(item) )
456 {
457 return 24;
458 }
459
460 virtual wxCoord OnMeasureListItemWidth( int WXUNUSED(item) )
461 {
462 return -1; // default - will be measured from text width
463 }
464
465 };
466 */
467
468 class wxPenStylePopup : public wxVListBoxComboPopup
469 {
470 public:
471 virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const
472 {
473 if ( item == wxNOT_FOUND )
474 return;
475
476 wxRect r(rect);
477 r.Deflate(3);
478 r.height -= 2;
479
480 int pen_style = wxSOLID;
481 if ( item == 1 )
482 pen_style = wxTRANSPARENT;
483 else if ( item == 2 )
484 pen_style = wxDOT;
485 else if ( item == 3 )
486 pen_style = wxLONG_DASH;
487 else if ( item == 4 )
488 pen_style = wxSHORT_DASH;
489 else if ( item == 5 )
490 pen_style = wxDOT_DASH;
491 else if ( item == 6 )
492 pen_style = wxBDIAGONAL_HATCH;
493 else if ( item == 7 )
494 pen_style = wxCROSSDIAG_HATCH;
495 else if ( item == 8 )
496 pen_style = wxFDIAGONAL_HATCH;
497 else if ( item == 9 )
498 pen_style = wxCROSS_HATCH;
499 else if ( item == 10 )
500 pen_style = wxHORIZONTAL_HATCH;
501 else if ( item == 11 )
502 pen_style = wxVERTICAL_HATCH;
503
504 wxPen pen( dc.GetTextForeground(), 3, pen_style );
505
506 // Get text colour as pen colour
507 dc.SetPen ( pen );
508
509 if ( !(flags & wxCP_PAINTING_CONTROL) )
510 {
511 dc.DrawText(GetString( item ),
512 r.x + 3,
513 (r.y + 0) + ( (r.height/2) - dc.GetCharHeight() )/2
514 );
515
516 dc.DrawLine( r.x+5, r.y+((r.height/4)*3), r.x+r.width - 5, r.y+((r.height/4)*3) );
517 }
518 else
519 {
520 dc.DrawLine( r.x+5, r.y+r.height/2, r.x+r.width - 5, r.y+r.height/2 );
521 }
522 }
523
524 virtual wxCoord OnMeasureItem( size_t WXUNUSED(item) ) const
525 {
526 return 24;
527 }
528
529 virtual wxCoord OnMeasureItemWidth( size_t WXUNUSED(item) ) const
530 {
531 return -1; // default - will be measured from text width
532 }
533
534 };
535
536 // ----------------------------------------------------------------------------
537 // wxComboCtrl with entirely custom button action (opens file dialog)
538 // ----------------------------------------------------------------------------
539
540 class wxFileSelectorCombo : public wxComboCtrl
541 {
542 public:
543 wxFileSelectorCombo() : wxComboCtrl() { Init(); }
544
545 wxFileSelectorCombo(wxWindow *parent,
546 wxWindowID id = wxID_ANY,
547 const wxString& value = wxEmptyString,
548 const wxPoint& pos = wxDefaultPosition,
549 const wxSize& size = wxDefaultSize,
550 long style = 0,
551 const wxValidator& validator = wxDefaultValidator,
552 const wxString& name = wxComboBoxNameStr)
553 : wxComboCtrl()
554 {
555 Init();
556 Create(parent,id,value,
557 pos,size,
558 // Style flag wxCC_STD_BUTTON makes the button
559 // behave more like a standard push button.
560 style | wxCC_STD_BUTTON,
561 validator,name);
562
563 //
564 // Prepare custom button bitmap (just '...' text)
565 wxMemoryDC dc;
566 wxBitmap bmp(12,16);
567 dc.SelectObject(bmp);
568
569 // Draw transparent background
570 wxColour magic(255,0,255);
571 wxBrush magicBrush(magic);
572 dc.SetBrush( magicBrush );
573 dc.SetPen( *wxTRANSPARENT_PEN );
574 dc.DrawRectangle(0,0,bmp.GetWidth(),bmp.GetHeight());
575
576 // Draw text
577 wxString str = wxT("...");
578 int w,h;
579 dc.GetTextExtent(str, &w, &h, 0, 0);
580 dc.DrawText(str, (bmp.GetWidth()-w)/2, (bmp.GetHeight()-h)/2);
581
582 dc.SelectObject( wxNullBitmap );
583
584 // Finalize transparency with a mask
585 wxMask *mask = new wxMask( bmp, magic );
586 bmp.SetMask( mask );
587
588 SetButtonBitmaps(bmp,true);
589 }
590
591 virtual void OnButtonClick()
592 {
593 // Show standard wxFileDialog on button click
594
595 wxFileDialog dlg(this,
596 wxT("Choose File"),
597 wxEmptyString,
598 GetValue(),
599 wxT("All files (*.*)|*.*"),
600 wxFD_OPEN);
601
602 if ( dlg.ShowModal() == wxID_OK )
603 {
604 SetValue(dlg.GetPath());
605 }
606 }
607
608 private:
609 void Init()
610 {
611 // Initialize member variables here
612 }
613 };
614
615 // ----------------------------------------------------------------------------
616 // main frame
617 // ----------------------------------------------------------------------------
618
619 // frame constructor
620 MyFrame::MyFrame(const wxString& title)
621 : wxFrame(NULL, wxID_ANY, title)
622 {
623 wxBoxSizer* topSizer;
624 wxBoxSizer* topRowSizer;
625 wxBoxSizer* colSizer;
626 wxBoxSizer* rowSizer;
627 wxStaticBoxSizer* groupSizer;
628
629 // set the frame icon
630 SetIcon(wxICON(sample));
631
632 #if wxUSE_MENUS
633 // create a menu bar
634 wxMenu *fileMenu = new wxMenu;
635
636 // the "About" item should be in the help menu
637 wxMenu *helpMenu = new wxMenu;
638 helpMenu->Append(ComboControl_About, _T("&About...\tF1"), _T("Show about dialog"));
639
640 fileMenu->Append(ComboControl_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
641
642 // now append the freshly created menu to the menu bar...
643 wxMenuBar *menuBar = new wxMenuBar();
644 menuBar->Append(fileMenu, _T("&File"));
645 menuBar->Append(helpMenu, _T("&Help"));
646
647 // ... and attach this menu bar to the frame
648 SetMenuBar(menuBar);
649 #endif // wxUSE_MENUS
650
651 wxPanel* panel = new wxPanel(this);
652
653 // Prepare log window right away since it shows EVT_TEXTs
654 m_logWin = new wxTextCtrl( panel, 105, wxEmptyString, wxDefaultPosition,
655 wxSize(-1,125), wxTE_MULTILINE|wxFULL_REPAINT_ON_RESIZE );
656 m_logWin->SetEditable(false);
657 wxLogTextCtrl* logger = new wxLogTextCtrl( m_logWin );
658 m_logOld = logger->SetActiveTarget( logger );
659 logger->SetTimestamp( NULL );
660
661
662 topSizer = new wxBoxSizer( wxVERTICAL );
663
664 topRowSizer = new wxBoxSizer( wxHORIZONTAL );
665
666 colSizer = new wxBoxSizer( wxVERTICAL );
667
668
669 // Make sure GetFeatures is implemented
670 int features = wxComboCtrl::GetFeatures();
671 wxLogDebug(wxT("wxComboCtrl features: 0x%X (all features: 0x%X)"),
672 features,wxComboCtrlFeatures::All);
673
674
675 wxComboBox* cb;
676 wxComboCtrl* cc;
677 wxGenericComboControl* gcc;
678 wxOwnerDrawnComboBox* odc;
679
680 // Create common strings array
681 wxArrayString arrItems;
682 arrItems.Add( wxT("Solid") );
683 arrItems.Add( wxT("Transparent") );
684 arrItems.Add( wxT("Dot") );
685 arrItems.Add( wxT("Long Dash") );
686 arrItems.Add( wxT("Short Dash") );
687 arrItems.Add( wxT("Dot Dash") );
688 arrItems.Add( wxT("Backward Diagonal Hatch") );
689 arrItems.Add( wxT("Cross-diagonal Hatch") );
690 arrItems.Add( wxT("Forward Diagonal Hatch") );
691 arrItems.Add( wxT("Cross Hatch") );
692 arrItems.Add( wxT("Horizontal Hatch") );
693 arrItems.Add( wxT("Vertical Hatch") );
694
695 int border = 4;
696
697 //
698 // Show some wxOwnerDrawComboBoxes for comparison
699 //
700 rowSizer = new wxBoxSizer(wxHORIZONTAL);
701
702 groupSizer = new wxStaticBoxSizer(new wxStaticBox(panel,wxID_ANY,wxT(" wxOwnerDrawnComboBox ")),
703 wxVERTICAL);
704
705 groupSizer->Add( new wxStaticText(panel,wxID_ANY,wxT("Writable, sorted:")), 0,
706 wxALIGN_CENTER_VERTICAL|wxRIGHT|wxEXPAND, border );
707
708 odc = new wxOwnerDrawnComboBox(panel,wxID_ANY,wxEmptyString,
709 wxDefaultPosition, wxDefaultSize,
710 arrItems,
711 wxCB_SORT // wxNO_BORDER|wxCB_READONLY
712 );
713
714 odc->Append(wxT("H - Appended Item")); // test sorting in append
715
716 odc->SetValue(wxT("Dot Dash"));
717
718 groupSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
719
720 //
721 // Readonly ODComboBox
722 groupSizer->Add( new wxStaticText(panel,wxID_ANY,wxT("Read-only:")), 0,
723 wxALIGN_CENTER_VERTICAL|wxRIGHT, border );
724
725 odc = new wxOwnerDrawnComboBox(panel,wxID_ANY,wxEmptyString,
726 wxDefaultPosition, wxDefaultSize,
727 arrItems,
728 wxCB_SORT|wxCB_READONLY // wxNO_BORDER|wxCB_READONLY
729 );
730
731 odc->SetValue(wxT("Dot Dash"));
732 odc->SetText(wxT("Dot Dash (Testing SetText)"));
733
734 groupSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
735
736 //
737 // Disabled ODComboBox
738 groupSizer->Add( new wxStaticText(panel,wxID_ANY,wxT("Disabled:")), 0,
739 wxALIGN_CENTER_VERTICAL|wxRIGHT, border );
740
741 odc = new wxOwnerDrawnComboBox(panel,wxID_ANY,wxEmptyString,
742 wxDefaultPosition, wxDefaultSize,
743 arrItems,
744 wxCB_SORT|wxCB_READONLY // wxNO_BORDER|wxCB_READONLY
745 );
746
747 odc->SetValue(wxT("Dot Dash"));
748 odc->Enable(false);
749
750 groupSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
751
752 rowSizer->Add( groupSizer, 1, wxEXPAND|wxALL, border );
753
754
755 groupSizer = new wxStaticBoxSizer(new wxStaticBox(panel,wxID_ANY,wxT(" wxComboBox ")),
756 wxVERTICAL);
757
758 //
759 // wxComboBox
760 //
761 groupSizer->Add( new wxStaticText(panel,wxID_ANY,wxT("Writable, sorted:")), 0,
762 wxALIGN_CENTER_VERTICAL|wxRIGHT|wxEXPAND, border );
763
764 cb = new wxComboBox(panel,wxID_ANY,wxEmptyString,
765 wxDefaultPosition, wxDefaultSize,
766 arrItems,
767 wxCB_SORT // wxNO_BORDER|wxCB_READONLY
768 );
769
770 cb->Append(wxT("H - Appended Item")); // test sorting in append
771
772 cb->SetValue(wxT("Dot Dash"));
773
774 groupSizer->Add( cb, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
775
776 //
777 // Readonly wxComboBox
778 groupSizer->Add( new wxStaticText(panel,wxID_ANY,wxT("Read-only:")), 0,
779 wxALIGN_CENTER_VERTICAL|wxRIGHT, border );
780
781 cb = new wxComboBox(panel,wxID_ANY,wxEmptyString,
782 wxDefaultPosition, wxDefaultSize,
783 arrItems,
784 wxCB_SORT|wxCB_READONLY // wxNO_BORDER|wxCB_READONLY
785 );
786
787 cb->SetValue(wxT("Dot Dash"));
788
789 groupSizer->Add( cb, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
790
791 //
792 // Disabled wxComboBox
793 groupSizer->Add( new wxStaticText(panel,wxID_ANY,wxT("Disabled:")), 0,
794 wxALIGN_CENTER_VERTICAL|wxRIGHT, border );
795
796 cb = new wxComboBox(panel,wxID_ANY,wxEmptyString,
797 wxDefaultPosition, wxDefaultSize,
798 arrItems,
799 wxCB_SORT|wxCB_READONLY // wxNO_BORDER|wxCB_READONLY
800 );
801
802 cb->SetValue(wxT("Dot Dash"));
803 cb->Enable(false);
804
805 groupSizer->Add( cb, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
806
807 rowSizer->Add( groupSizer, 1, wxEXPAND|wxALL, border );
808
809
810 colSizer->Add( rowSizer, 1, wxEXPAND|wxALL, border );
811
812
813 //
814 // Pen selector ODComboBox with application painted items
815 //
816 rowSizer = new wxBoxSizer ( wxHORIZONTAL );
817 rowSizer->Add( new wxStaticText(panel,wxID_ANY,
818 wxT("OwnerDrawnComboBox with Custom Paint Items and Button Placing:")), 1,
819 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
820 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
821
822 rowSizer = new wxBoxSizer ( wxHORIZONTAL );
823
824 // When defining derivative class for callbacks, we need
825 // to use two-stage creation (or redefine the common wx
826 // constructor).
827 odc = new wxOwnerDrawnComboBox(panel,wxID_ANY,wxEmptyString,
828 wxDefaultPosition, wxDefaultSize,
829 arrItems,
830 wxCB_READONLY //wxNO_BORDER | wxCB_READONLY
831 );
832
833 odc->SetPopupControl( new wxPenStylePopup() );
834
835 //m_odc->SetCustomPaintWidth( 60 );
836 odc->SetSelection(0);
837 odc->SetButtonPosition(-2, // width adjustment
838 -6, // height adjustment
839 wxLEFT, // side
840 2 // horizontal spacing
841 );
842
843 rowSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 4 );
844 rowSizer->AddStretchSpacer(1);
845 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
846
847
848 //
849 // List View wxComboCtrl
850 //
851
852 rowSizer = new wxBoxSizer ( wxHORIZONTAL );
853 rowSizer->Add( new wxStaticText(panel,wxID_ANY,wxT("List View wxComboCtrl:")), 1,
854 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
855 rowSizer->Add( new wxStaticText(panel,wxID_ANY,wxT("Tree Ctrl wxGenericComboControl:")), 1,
856 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
857 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
858
859 rowSizer = new wxBoxSizer ( wxHORIZONTAL );
860 cc = new wxComboCtrl(panel,2,wxEmptyString,
861 wxDefaultPosition, wxDefaultSize);
862
863 cc->SetPopupMinWidth(300);
864
865 ListViewComboPopup* iface = new ListViewComboPopup();
866 cc->SetPopupControl(iface);
867
868 iface->AddSelection( wxT("Cabbage") );
869 iface->AddSelection( wxT("Potato") );
870 iface->AddSelection( wxT("Onion") );
871 iface->AddSelection( wxT("Carrot") );
872 iface->AddSelection( wxT("Cauliflower") );
873 iface->AddSelection( wxT("Bean") );
874 iface->AddSelection( wxT("Raddish") );
875 iface->AddSelection( wxT("Banana") );
876 iface->AddSelection( wxT("Apple") );
877 iface->AddSelection( wxT("Orange") );
878 iface->AddSelection( wxT("Kiwi") );
879 iface->AddSelection( wxT("Strawberry") );
880 iface->AddSelection( wxT("Cucumber") );
881 iface->AddSelection( wxT("Blackberry") );
882 iface->AddSelection( wxT("Melon") );
883 iface->AddSelection( wxT("Cherry") );
884 iface->AddSelection( wxT("Pea") );
885 iface->AddSelection( wxT("Pear") );
886
887 rowSizer->Add( cc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
888
889
890 //
891 // Tree Ctrl wxComboCtrl
892 //
893
894 // Note that we test that wxGenericComboControl works
895 gcc = new wxGenericComboControl(panel,wxID_ANY,wxEmptyString,
896 wxDefaultPosition, wxDefaultSize);
897
898 // Set popup interface right away, otherwise some of the calls
899 // below may fail
900 TreeCtrlComboPopup* tcPopup = new TreeCtrlComboPopup();
901 gcc->SetPopupControl(tcPopup);
902
903 // Add items using wxTreeCtrl methods directly
904 wxTreeItemId rootId = tcPopup->AddRoot(wxT("<hidden_root>"));
905
906 wxTreeItemId groupId;
907
908 groupId = tcPopup->AppendItem(rootId,wxT("Controls"));
909 tcPopup->AppendItem(groupId,wxT("wxButton"));
910 tcPopup->AppendItem(groupId,wxT("wxCheckBox"));
911 tcPopup->AppendItem(groupId,wxT("wxListCtrl"));
912 tcPopup->AppendItem(groupId,wxT("wxStaticBox"));
913 tcPopup->AppendItem(groupId,wxT("wxStaticText"));
914 tcPopup->AppendItem(groupId,wxT("wxTextCtrl"));
915 tcPopup->AppendItem(groupId,wxT("wxTreeCtrl"));
916 groupId = tcPopup->AppendItem(rootId,wxT("Dialogs"));
917 tcPopup->AppendItem(groupId,wxT("wxDirDialog"));
918 tcPopup->AppendItem(groupId,wxT("wxFileDialog"));
919 tcPopup->AppendItem(groupId,wxT("wxWizard"));
920
921 gcc->SetValue(wxT("wxStaticBox"));
922
923 // Move button to left - it makes more sense for a tree ctrl
924 gcc->SetButtonPosition(0, // width adjustment
925 0, // height adjustment
926 wxLEFT, // side
927 0 // horizontal spacing
928 );
929
930 rowSizer->Add( gcc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
931
932 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
933
934 #if wxUSE_IMAGE
935 wxInitAllImageHandlers();
936
937 //
938 // Custom Dropbutton Bitmaps
939 // (second one uses blank button background)
940 //
941 rowSizer = new wxBoxSizer ( wxHORIZONTAL );
942 rowSizer->Add( new wxStaticText(panel,wxID_ANY,
943 wxT("OwnerDrawnComboBox with simple dropbutton graphics:")), 1,
944 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
945
946 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
947
948 rowSizer = new wxBoxSizer ( wxHORIZONTAL );
949
950 odc = new wxOwnerDrawnComboBox(panel,wxID_ANY,wxEmptyString,
951 wxDefaultPosition, wxDefaultSize,
952 arrItems,
953 (long)0 // wxCB_SORT // wxNO_BORDER | wxCB_READONLY
954 );
955
956 wxOwnerDrawnComboBox* odc2;
957 odc2 = new wxOwnerDrawnComboBox(panel,wxID_ANY,wxEmptyString,
958 wxDefaultPosition, wxDefaultSize,
959 arrItems,
960 (long)0 // wxCB_SORT // wxNO_BORDER | wxCB_READONLY
961 );
962
963 // Load images from disk
964 wxImage imgNormal(wxT("dropbutn.png"));
965 wxImage imgPressed(wxT("dropbutp.png"));
966 wxImage imgHover(wxT("dropbuth.png"));
967
968 if ( imgNormal.Ok() && imgPressed.Ok() && imgHover.Ok() )
969 {
970 wxBitmap bmpNormal(imgNormal);
971 wxBitmap bmpPressed(imgPressed);
972 wxBitmap bmpHover(imgHover);
973 odc->SetButtonBitmaps(bmpNormal,false,bmpPressed,bmpHover);
974 odc2->SetButtonBitmaps(bmpNormal,true,bmpPressed,bmpHover);
975 }
976 else
977 wxLogError(wxT("Dropbutton images not found"));
978
979 //odc2->SetButtonPosition(0, // width adjustment
980 // 0, // height adjustment
981 // wxLEFT, // side
982 // 0 // horizontal spacing
983 // );
984
985 rowSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 4 );
986 rowSizer->Add( odc2, 1, wxALIGN_CENTER_VERTICAL|wxALL, 4 );
987 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
988 #endif
989
990
991 //
992 // wxComboCtrl with totally custom button action (open file dialog)
993 //
994 rowSizer = new wxBoxSizer ( wxHORIZONTAL );
995 rowSizer->Add( new wxStaticText(panel,wxID_ANY,
996 wxT("wxComboCtrl with custom button action:")), 1,
997 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
998
999
1000 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
1001
1002 rowSizer = new wxBoxSizer ( wxHORIZONTAL );
1003 wxFileSelectorCombo* fsc;
1004
1005 fsc = new wxFileSelectorCombo(panel,wxID_ANY,wxEmptyString,
1006 wxDefaultPosition, wxDefaultSize,
1007 (long)0
1008 );
1009
1010 rowSizer->Add( fsc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 4 );
1011 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
1012
1013
1014 topRowSizer->Add( colSizer, 1, wxALL, 2 );
1015
1016 topRowSizer->Add( m_logWin, 1, wxEXPAND|wxALL, 5 );
1017 topSizer->Add( topRowSizer, 1, wxEXPAND );
1018
1019 panel->SetSizer( topSizer );
1020 topSizer->SetSizeHints( panel );
1021
1022 SetSize(740,480);
1023 Centre();
1024 }
1025
1026 MyFrame::~MyFrame()
1027 {
1028 delete wxLog::SetActiveTarget(m_logOld);
1029 }
1030
1031 // event handlers
1032
1033 void MyFrame::OnComboBoxUpdate( wxCommandEvent& event )
1034 {
1035 // Don't show messages for the log output window (it'll crash)
1036 if ( event.GetId() == 105 )
1037 return;
1038
1039 if ( event.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED )
1040 wxLogDebug(wxT("EVT_COMBOBOX(id=%i,selection=%i)"),event.GetId(),event.GetSelection());
1041 else if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED )
1042 wxLogDebug(wxT("EVT_TEXT(id=%i,string=\"%s\")"),event.GetId(),event.GetString().c_str());
1043 }
1044
1045 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
1046 {
1047 // true is to force the frame to close
1048 Close(true);
1049 }
1050
1051 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
1052 {
1053 wxMessageBox(wxString::Format(
1054 _T("Welcome to %s!\n")
1055 _T("\n")
1056 _T("This is the wxWidgets wxComboCtrl sample\n")
1057 _T("running under %s."),
1058 wxVERSION_STRING,
1059 wxGetOsDescription().c_str()
1060 ),
1061 _T("About wxComboCtrl sample"),
1062 wxOK | wxICON_INFORMATION,
1063 this);
1064 }