customcombo.cpp 5.7 KB

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