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