1 /////////////////////////////////////////////////////////////////////////////
2 // Name: customcombo.cpp
3 // Purpose: Implement some custom wxComboCtrls
4 // Author: Utensil Candel (UtensilCandel@@gmail.com)
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
7
8 // For compilers that support precompilation, includes "wx/wx.h".
9 #include "wx/wxprec.h"
10
11
12 // for all others, include the necessary headers
13 #ifndef WX_PRECOMP
14 #include "wx/wx.h"
15 #endif
16
17 #if !wxUSE_COMBOCTRL
18 #error "Please set wxUSE_COMBOCTRL to 1 and rebuild the library."
19 #endif
20
21 #include "customcombo.h"
22
23
wxBEGIN_EVENT_TABLE(ListViewComboPopup,wxListView)24 wxBEGIN_EVENT_TABLE(ListViewComboPopup, wxListView)
25 EVT_MOTION(ListViewComboPopup::OnMouseMove)
26 // NOTE: Left down event is used instead of left up right now
27 // since MSW wxListCtrl doesn't seem to emit left ups
28 // consistently.
29 EVT_LEFT_DOWN(ListViewComboPopup::OnMouseClick)
30 wxEND_EVENT_TABLE()
31
32 wxBEGIN_EVENT_TABLE(TreeCtrlComboPopup, wxTreeCtrl)
33 EVT_MOTION(TreeCtrlComboPopup::OnMouseMove)
34 // NOTE: Left down event is used instead of left up right now
35 // since MSW wxTreeCtrl doesn't seem to emit left ups
36 // consistently.
37 EVT_LEFT_DOWN(TreeCtrlComboPopup::OnMouseClick)
38 wxEND_EVENT_TABLE()
39
40
41
42 // ----------------------------------------------------------------------------
43 // PenStyleComboBox
44 // ----------------------------------------------------------------------------
45
46 void PenStyleComboBox::OnDrawItem( wxDC& dc,
47 const wxRect& rect,
48 int item,
49 int flags ) const
50 {
51 if ( item == wxNOT_FOUND )
52 return;
53
54 wxRect r(rect);
55 r.Deflate(3);
56 r.height -= 2;
57
58 wxPenStyle penStyle = wxPENSTYLE_SOLID;
59 // if ( item == 1 )
60 // penStyle = wxPENSTYLE_TRANSPARENT;
61 // else if ( item == 2 )
62 // penStyle = wxPENSTYLE_DOT;
63 // else if ( item == 3 )
64 // penStyle = wxPENSTYLE_LONG_DASH;
65 // else if ( item == 4 )
66 // penStyle = wxPENSTYLE_SHORT_DASH;
67 if ( item == 0 )
68 penStyle = wxPENSTYLE_DOT_DASH;
69 else if ( item == 1 )
70 penStyle = wxPENSTYLE_BDIAGONAL_HATCH;
71 else if ( item == 2 )
72 penStyle = wxPENSTYLE_CROSSDIAG_HATCH;
73 // else if ( item == 8 )
74 // penStyle = wxPENSTYLE_FDIAGONAL_HATCH;
75 // else if ( item == 9 )
76 // penStyle = wxPENSTYLE_CROSS_HATCH;
77 // else if ( item == 10 )
78 // penStyle = wxPENSTYLE_HORIZONTAL_HATCH;
79 // else if ( item == 11 )
80 // penStyle = wxPENSTYLE_VERTICAL_HATCH;
81
82 wxPen pen( dc.GetTextForeground(), 3, penStyle );
83
84 // Get text colour as pen colour
85 dc.SetPen( pen );
86
87 if ( !(flags & wxODCB_PAINTING_CONTROL) )
88 {
89 dc.DrawText(GetString( item ),
90 r.x + 3,
91 (r.y + 0) + ( (r.height/2) - dc.GetCharHeight() )/2
92 );
93
94 dc.DrawLine( r.x+5, r.y+((r.height/4)*3), r.x+r.width - 5, r.y+((r.height/4)*3) );
95 }
96 else
97 {
98 dc.DrawLine( r.x+5, r.y+r.height/2, r.x+r.width - 5, r.y+r.height/2 );
99 }
100 }
101
OnDrawBackground(wxDC & dc,const wxRect & rect,int item,int flags) const102 void PenStyleComboBox::OnDrawBackground( wxDC& dc, const wxRect& rect,
103 int item, int flags ) const
104 {
105 // If item is selected or even, or we are painting the
106 // combo control itself, use the default rendering.
107 if ( (flags & (wxODCB_PAINTING_CONTROL|wxODCB_PAINTING_SELECTED)) ||
108 (item & 1) == 0 )
109 {
110 wxOwnerDrawnComboBox::OnDrawBackground(dc,rect,item,flags);
111 return;
112 }
113
114 // Otherwise, draw every other background with different colour.
115 wxColour bgCol(240,240,250);
116 dc.SetBrush(wxBrush(bgCol));
117 dc.SetPen(wxPen(bgCol));
118 dc.DrawRectangle(rect);
119 }
120
OnMeasureItem(size_t item) const121 inline wxCoord PenStyleComboBox::OnMeasureItem( size_t item ) const
122 {
123 // Simply demonstrate the ability to have variable-height items
124 if ( item & 1 )
125 return 36;
126 else
127 return 24;
128 }
129
OnMeasureItemWidth(size_t WXUNUSED (item)) const130 inline wxCoord PenStyleComboBox::OnMeasureItemWidth( size_t WXUNUSED(item) ) const
131 {
132 return -1; // default - will be measured from text width
133 }
134
CreateSample(wxWindow * parent)135 PenStyleComboBox * PenStyleComboBox::CreateSample(wxWindow* parent)
136 {
137 PenStyleComboBox* odc;
138
139 // Common list of items for all dialogs.
140 wxArrayString arrItems;
141
142 // Create common strings array
143 // arrItems.Add( wxT("Solid") );
144 // arrItems.Add( wxT("Transparent") );
145 // arrItems.Add( wxT("Dot") );
146 // arrItems.Add( wxT("Long Dash") );
147 // arrItems.Add( wxT("Short Dash") );
148 // Comment the following since we don't need too long a drop list
149 arrItems.Add( wxT("Dot Dash") );
150 arrItems.Add( wxT("Backward Diagonal Hatch") );
151 arrItems.Add( wxT("Cross-diagonal Hatch") );
152 // arrItems.Add( wxT("Forward Diagonal Hatch") );
153 // arrItems.Add( wxT("Cross Hatch") );
154 // arrItems.Add( wxT("Horizontal Hatch") );
155 // arrItems.Add( wxT("Vertical Hatch") );
156
157 // When defining derivative class for callbacks, we need
158 // to use two-stage creation (or redefine the common wx
159 // constructor).
160 odc = new PenStyleComboBox();
161 odc->Create(parent,wxID_ANY,wxEmptyString,
162 wxDefaultPosition, wxDefaultSize,
163 arrItems,
164 wxCB_READONLY //wxNO_BORDER | wxCB_READONLY
165 );
166
167
168 odc->SetSelection(0);
169
170 // Load images from disk
171 wxImage imgNormal(wxT("bitmaps/dropbutn.png"));
172 wxImage imgPressed(wxT("bitmaps/dropbutp.png"));
173 wxImage imgHover(wxT("bitmaps/dropbuth.png"));
174
175 if ( imgNormal.IsOk() && imgPressed.IsOk() && imgHover.IsOk() )
176 {
177 wxBitmap bmpNormal(imgNormal);
178 wxBitmap bmpPressed(imgPressed);
179 wxBitmap bmpHover(imgHover);
180 odc->SetButtonBitmaps(bmpNormal,false,bmpPressed,bmpHover);
181 }
182 else
183 wxLogError(wxT("Dropbutton images not found"));
184
185 return odc;
186 }
187
188