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