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