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