custclas.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //-----------------------------------------------------------------------------
  2. // Name: custclass.cpp
  3. // Purpose: XML resources sample: A custom class to insert into a XRC file
  4. // Author: Robert O'Connor (rob@medicalmnemonics.com), Vaclav Slavik
  5. // Copyright: (c) Robert O'Connor and Vaclav Slavik
  6. // Licence: wxWindows licence
  7. //-----------------------------------------------------------------------------
  8. //-----------------------------------------------------------------------------
  9. // Standard wxWidgets headers
  10. //-----------------------------------------------------------------------------
  11. // For compilers that support precompilation, includes "wx/wx.h".
  12. #include "wx/wxprec.h"
  13. #ifdef __BORLANDC__
  14. #pragma hdrstop
  15. #endif
  16. // For all others, include the necessary headers (this file is usually all you
  17. // need because it includes almost all "standard" wxWidgets headers)
  18. #ifndef WX_PRECOMP
  19. #include "wx/wx.h"
  20. #endif
  21. //-----------------------------------------------------------------------------
  22. // Header of this .cpp file
  23. //-----------------------------------------------------------------------------
  24. #include "custclas.h"
  25. //-----------------------------------------------------------------------------
  26. // Internal constants
  27. //-----------------------------------------------------------------------------
  28. // Popup menu (PU) item control IDs. In this example, they aren't hooked up
  29. // to any functions. Normally you would use these IDs in your event table, so
  30. // that if one of these menu items is clicked, then a certain function is
  31. // called.
  32. enum {
  33. PU_ADD_RECORD = wxID_HIGHEST + 1,
  34. PU_EDIT_RECORD,
  35. PU_DELETE_RECORD
  36. };
  37. // Columns of the listctrl (the leftmost one starts at 0, and so on).
  38. // Allows easier code maintenance if want to add/rearrangement of listctrl's
  39. // columns.
  40. enum {
  41. RECORD_COLUMN = 0,
  42. ACTION_COLUMN,
  43. PRIORITY_COLUMN
  44. };
  45. //-----------------------------------------------------------------------------
  46. // wxWidgets macro: implement dynamic class
  47. //-----------------------------------------------------------------------------
  48. IMPLEMENT_DYNAMIC_CLASS( MyResizableListCtrl, wxListCtrl )
  49. //-----------------------------------------------------------------------------
  50. // Event table: connect the events to the handler functions to process them
  51. //-----------------------------------------------------------------------------
  52. wxBEGIN_EVENT_TABLE( MyResizableListCtrl, wxListCtrl )
  53. // Something to do when right mouse down
  54. EVT_RIGHT_DOWN( MyResizableListCtrl::ContextSensitiveMenu )
  55. // Something to do when resized
  56. EVT_SIZE( MyResizableListCtrl::OnSize )
  57. wxEND_EVENT_TABLE()
  58. //-----------------------------------------------------------------------------
  59. // Public methods
  60. //-----------------------------------------------------------------------------
  61. // Constructor, including setting the dialog's m_configuration_section member
  62. // to the incoming configuration_section string.
  63. MyResizableListCtrl::MyResizableListCtrl( wxWindow *parent, wxWindowID id,
  64. const wxPoint& pos, const wxSize& size,
  65. long style, const wxValidator& validator,
  66. const wxString& name )
  67. : wxListCtrl( parent, id, pos, size, style, validator, name )
  68. {
  69. // This listctrl needs to insert its columns in the constructor, since
  70. // as soon as the listctrl is built, it is resized and grafted onto an
  71. // "unknown" XRC placeholder. This induces an OnSize() event, calling the
  72. // overrriden OnSize function for this class, which needs to have 3
  73. // columns to resize (else an assert on WXGTK debug build).
  74. InsertColumn( RECORD_COLUMN, _("Record"), wxLIST_FORMAT_LEFT, 140);
  75. InsertColumn( ACTION_COLUMN, _("Action"), wxLIST_FORMAT_LEFT, 70);
  76. InsertColumn( PRIORITY_COLUMN, _("Priority"), wxLIST_FORMAT_LEFT, 70 );
  77. }
  78. void MyResizableListCtrl::ContextSensitiveMenu( wxMouseEvent& event )
  79. {
  80. // Make an instance of a menu.
  81. wxMenu a_menu;
  82. a_menu.Append( PU_ADD_RECORD, _( "Add a new record...") );
  83. a_menu.Append( PU_EDIT_RECORD, _( "Edit selected record..." ) );
  84. a_menu.Append( PU_DELETE_RECORD, _( "Delete selected record" ) );
  85. // If no listctrl rows selected, then disable the menu items that
  86. // require selection
  87. if ( GetSelectedItemCount() == 0 ) {
  88. a_menu.Enable( PU_EDIT_RECORD, false );
  89. a_menu.Enable( PU_DELETE_RECORD, false );
  90. }
  91. // Show the popup menu (wxWindow::PopupMenu ), at the x,y position
  92. // of the click event
  93. PopupMenu( &a_menu, event.GetPosition() );
  94. }
  95. void MyResizableListCtrl::OnSize( wxSizeEvent &event )
  96. {
  97. // Call our custom width setting function.
  98. SetColumnWidths();
  99. // REQURED event.Skip() call to allow this event to propagate
  100. // upwards so others can do what they need to do in response to
  101. // this size event.
  102. event.Skip();
  103. }
  104. void MyResizableListCtrl::SetColumnWidths()
  105. {
  106. // Get width of entire listctrl
  107. int leftmostColumnWidth = GetSize().x;
  108. // Subtract width of other columns, scrollbar, and some padding
  109. leftmostColumnWidth -= GetColumnWidth( ACTION_COLUMN );
  110. leftmostColumnWidth -= GetColumnWidth( PRIORITY_COLUMN );
  111. leftmostColumnWidth -= wxSystemSettings::GetMetric( wxSYS_VSCROLL_X );
  112. leftmostColumnWidth -= 5;
  113. // Set the column width to the new value.
  114. SetColumnWidth( RECORD_COLUMN, leftmostColumnWidth );
  115. // This is just a debug message in case you want to watch the
  116. // events scroll by as you resize.
  117. wxLogDebug( wxT("Successfully set column widths") );
  118. }