From: Vadim Zeitlin Date: Mon, 25 May 2009 13:00:28 +0000 (+0000) Subject: add support for defining list control columns in XRC too (and correct a few things... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/ef18e792233801b3d92ea4302e54d0de627d348f add support for defining list control columns in XRC too (and correct a few things in previously added list items support) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@60738 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index d94c3c86dd..4f525f48a8 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -334,7 +334,7 @@ All: All (GUI): - wxWindow::SetAutoLayout() now works for all windows, not just panels. -- Support loading wxListCtrl items and image lists from XRC (Kinaou Hervé). +- Support wxListCtrl columns, items and image lists in XRC (Kinaou Hervé). - wxGrid: add possibility to prevent resizing of individual rows/columns. - wxHTML: add support for table borders width (Laurent Humbertclaude). - Added wxMouseEventsManager. diff --git a/docs/doxygen/overviews/xrc_format.h b/docs/doxygen/overviews/xrc_format.h index bc07cb0c45..aeb6780ad1 100644 --- a/docs/doxygen/overviews/xrc_format.h +++ b/docs/doxygen/overviews/xrc_format.h @@ -959,13 +959,36 @@ Each @c listbookpage has exactly one non-toplevel window as its child. @subsubsection xrc_wxlistctrl wxListCtrl -A listctrl can have one or more child objects of the class @ref xrc_wxlistitem +A list control can have one or more child objects of the class @ref xrc_wxlistitem and one or more objects of the @ref xrc_wximagelist class. The latter is defined either using @c \ tag for the control with @c wxLC_ICON style or using @c \ tag for the control with @c wxLC_SMALL_ICON style. -@subsubsection xrc_wxlistitem wxListItem +Report mode list controls (i.e. created with @c wxLC_REPORT style) can in +addition have one or more @ref xrc_wxlistcol child elements. + +@subsubsection xrc_wxlistcol listcol + +The @c listcol class can only be used for wxListCtrl children. It can have the +following properties: +@beginTable +@hdr3col{property, type, description} +@row3col{align, wxListColumnFormat, + The alignment for the item. + Can be one of @c wxLIST_FORMAT_LEFT, @c wxLIST_FORMAT_RIGHT or + @c wxLIST_FORMAT_CENTRE.} +@row3col{text, @ref overview_xrcformat_type_string, + The title of the column. } +@row3col{width, integer, + The column width. } +@endTable + +The columns are appended to the control in order of their appearance and may be +referenced by 0-based index in the @c col attributes of subsequent @c listitem +objects. + +@subsubsection xrc_wxlistitem listitem The @c listitem is a child object for the class @ref xrc_wxlistctrl. It can have the following properties: @@ -986,14 +1009,14 @@ It can have the following properties: Add a bitmap in the 'small' @ref xrc_wximagelist associated with the @ref xrc_wxlistctrl parent and associate it with this item. If the 'small' imagelist is not defined it will be created implicitly.} +@row3col{col, integer, + The zero-based column index.} @row3col{image, integer, The zero-based index of the image associated with the item in the (normal) image list.} @row3col{image-small, integer, The zero-based index of the image associated with the item in the 'small' image list.} -@row3col{col, integer, - The zero-based column index.} @row3col{data, integer, The client data for the item.} @row3col{font, @ref overview_xrcformat_type_font, @@ -1001,33 +1024,14 @@ It can have the following properties: @row3col{image, integer, The zero-based index of the image associated with the item into the image list.} -@row3col{mask, @ref overview_xrcformat_type_style, - The mask indicating which fields of this class are valid. - Can be any combination of the following values: - - wxLIST_MASK_STATE: @b GetState is valid. - - wxLIST_MASK_TEXT: @b GetText is valid. - - wxLIST_MASK_IMAGE: @b GetImage is valid. - - wxLIST_MASK_DATA: @b GetData is valid. - - wxLIST_MASK_WIDTH: @b GetWidth is valid. - - wxLIST_MASK_FORMAT: @b GetFormat is valid. } @row3col{state, @ref overview_xrcformat_type_style, - The item state flags (note that the valid state flags are influenced - by the value of the @c statemask, see below). - Can be any combination of the following values: + The item state. Can be any combination of the following values: - @c wxLIST_STATE_FOCUSED: The item has the focus. - @c wxLIST_STATE_SELECTED: The item is selected. - - @c wxLIST_STATE_DONTCARE: Don't care what the state is. Win32 only. - - @c wxLIST_STATE_DROPHILITED: The item is highlighted to receive a drop event. Win32 only. - - @c wxLIST_STATE_CUT: The item is in the cut state. Win32 only. } -@row3col{statemask, bitlist, - A mask indicating which state flags are valid. This is a bitlist of the - flags reported above for the item state. } @row3col{text, @ref overview_xrcformat_type_string, - The text label (or header for columns) for the item. } + The text label for the item. } @row3col{textcolour, @ref overview_xrcformat_type_colour, The text colour for the item. } -@row3col{width, integer, - The column width. } @endTable Notice that the item position can't be specified here, the items are appended diff --git a/include/wx/xrc/xh_listc.h b/include/wx/xrc/xh_listc.h index 5ca936f28b..ecb38b6761 100644 --- a/include/wx/xrc/xh_listc.h +++ b/include/wx/xrc/xh_listc.h @@ -16,6 +16,7 @@ #if wxUSE_XRC && wxUSE_LISTCTRL class WXDLLIMPEXP_FWD_CORE wxListCtrl; +class WXDLLIMPEXP_FWD_CORE wxListItem; class WXDLLIMPEXP_XRC wxListCtrlXmlHandler : public wxXmlResourceHandler { @@ -25,8 +26,13 @@ public: virtual bool CanHandle(wxXmlNode *node); private: - long Handle_wxListItem(); - wxObject* Handle_wxListCtrl(); + // handlers for wxListCtrl itself and its listcol and listitem children + wxListCtrl *HandleListCtrl(); + void HandleListCol(); + void HandleListItem(); + + // common part to HandleList{Col,Item}() + void HandleCommonItemAttrs(wxListItem& item); // gets the items image index in the corresponding image list (normal if // which is wxIMAGE_LIST_NORMAL or small if it is wxIMAGE_LIST_SMALL) diff --git a/samples/xrc/myframe.cpp b/samples/xrc/myframe.cpp index 3a6453d3e8..175d883bae 100644 --- a/samples/xrc/myframe.cpp +++ b/samples/xrc/myframe.cpp @@ -207,22 +207,20 @@ void MyFrame::OnControlsToolOrMenuCommand(wxCommandEvent& WXUNUSED(event)) wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("controls_dialog")); #if wxUSE_LISTCTRL - // There is no data in the listctrl. This will add some columns - // and some data. You don't need to use any pointers - // at all to manipulate the controls, just simply use the XRCCTL(...) macros. - // "controls_treectrl" is the name of this control in the XRC. - // (1) Insert a column, with the column header of "Name" - // (The '_' function around "Name" marks this string as one to translate). - XRCCTRL(dlg, "controls_listctrl", wxListCtrl)->InsertColumn( 0, - _("Name"), - wxLIST_FORMAT_LEFT, - ( 200 ) - ); - // (2) Insert some items into the listctrl - XRCCTRL(dlg, "controls_listctrl", wxListCtrl)->InsertItem(0,wxT("Todd Hope")); - XRCCTRL(dlg, "controls_listctrl", wxListCtrl)->InsertItem(1,wxT("Kim Wynd")); - XRCCTRL(dlg, "controls_listctrl", wxListCtrl)->InsertItem(2,wxT("Leon Li")); -#endif + // The resource file specifies the columns of the control as they are + // typically static while the items themselves are added from here as + // usually they are not static (but if they are, they can be defined in the + // resources too, see the two other list controls definitions in + // controls.xrc) + + // Insert some items into the listctrl: notice that we can access it using + // XRCCTRL + wxListCtrl * const list = XRCCTRL(dlg, "controls_listctrl", wxListCtrl); + + list->InsertItem(0, "Athos"); list->SetItem(0, 1, "90"); + list->InsertItem(1, "Porthos"); list->SetItem(1, 1, "120"); + list->InsertItem(2, "Aramis"); list->SetItem(2, 1, "80"); +#endif // wxUSE_LISTCTRL #if wxUSE_TREECTRL // There is no data in the tree ctrl. These lines will add some. diff --git a/samples/xrc/rc/controls.xrc b/samples/xrc/rc/controls.xrc index c93c7d9cdc..3ce4f099fd 100644 --- a/samples/xrc/rc/controls.xrc +++ b/samples/xrc/rc/controls.xrc @@ -584,8 +584,17 @@ 5 220,160 - - + + + Name + 150 + + + Weight + wxLIST_FORMAT_RIGHT + 50 + + @@ -593,7 +602,7 @@ 5 220,160 - + Information @@ -609,7 +618,7 @@ 5 220,160 - + 20,20 diff --git a/src/xrc/xh_listc.cpp b/src/xrc/xh_listc.cpp index db99373ef7..a6499e422a 100644 --- a/src/xrc/xh_listc.cpp +++ b/src/xrc/xh_listc.cpp @@ -1,10 +1,11 @@ ///////////////////////////////////////////////////////////////////////////// // Name: src/xrc/xh_listc.cpp // Purpose: XRC resource for wxListCtrl -// Author: Brian Gavin +// Author: Brian Gavin, Kinaou Hervé, Vadim Zeitlin // Created: 2000/09/09 // RCS-ID: $Id$ // Copyright: (c) 2000 Brian Gavin +// (c) 2009 Vadim Zeitlin // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -26,11 +27,20 @@ #include "wx/listctrl.h" #include "wx/imaglist.h" +namespace +{ + +const char *LISTCTRL_CLASS_NAME = "wxListCtrl"; +const char *LISTITEM_CLASS_NAME = "listitem"; +const char *LISTCOL_CLASS_NAME = "listcol"; + +} // anonymous namespace + IMPLEMENT_DYNAMIC_CLASS(wxListCtrlXmlHandler, wxXmlResourceHandler) wxListCtrlXmlHandler::wxListCtrlXmlHandler() -: wxXmlResourceHandler() + : wxXmlResourceHandler() { // wxListItem styles XRC_ADD_STYLE(wxLIST_FORMAT_LEFT); @@ -42,11 +52,8 @@ wxListCtrlXmlHandler::wxListCtrlXmlHandler() XRC_ADD_STYLE(wxLIST_MASK_DATA); XRC_ADD_STYLE(wxLIST_MASK_WIDTH); XRC_ADD_STYLE(wxLIST_MASK_FORMAT); - XRC_ADD_STYLE(wxLIST_STATE_DONTCARE); - XRC_ADD_STYLE(wxLIST_STATE_DROPHILITED); XRC_ADD_STYLE(wxLIST_STATE_FOCUSED); XRC_ADD_STYLE(wxLIST_STATE_SELECTED); - XRC_ADD_STYLE(wxLIST_STATE_CUT); // wxListCtrl styles XRC_ADD_STYLE(wxLC_LIST); @@ -71,30 +78,69 @@ wxListCtrlXmlHandler::wxListCtrlXmlHandler() wxObject *wxListCtrlXmlHandler::DoCreateResource() { - if (m_class == wxT("listitem")) + if ( m_class == LISTITEM_CLASS_NAME ) + { + HandleListItem(); + } + else if ( m_class == LISTCOL_CLASS_NAME ) { - Handle_wxListItem(); - return m_parentAsWindow; + HandleListCol(); } else - return Handle_wxListCtrl(); + { + wxASSERT_MSG( m_class == LISTCTRL_CLASS_NAME, + "can't handle unknown node" ); + + return HandleListCtrl(); + } + + return m_parentAsWindow; } bool wxListCtrlXmlHandler::CanHandle(wxXmlNode *node) { - return IsOfClass(node, wxT("wxListCtrl")) || - IsOfClass(node, wxT("listitem")); + return IsOfClass(node, LISTCTRL_CLASS_NAME) || + IsOfClass(node, LISTITEM_CLASS_NAME) || + IsOfClass(node, LISTCOL_CLASS_NAME); } -long wxListCtrlXmlHandler::Handle_wxListItem() +void wxListCtrlXmlHandler::HandleCommonItemAttrs(wxListItem& item) +{ + if (HasParam(wxT("align"))) + item.SetAlign((wxListColumnFormat)GetStyle(wxT("align"))); + if (HasParam(wxT("text"))) + item.SetText(GetText(wxT("text"))); +} + +void wxListCtrlXmlHandler::HandleListCol() { wxListCtrl * const list = wxDynamicCast(m_parentAsWindow, wxListCtrl); - wxCHECK_MSG( list, -1, "must have wxListCtrl parent" ); + wxCHECK_RET( list, "must have wxListCtrl parent" ); + + if ( !list->HasFlag(wxLC_REPORT) ) + { + ReportError("Only report mode list controls can have columns."); + return; + } wxListItem item; - if (HasParam(wxT("align"))) - item.SetAlign((wxListColumnFormat)GetStyle(wxT("align"))); + HandleCommonItemAttrs(item); + if (HasParam(wxT("width"))) + item.SetWidth((int)GetLong(wxT("width"))); + + list->InsertColumn(list->GetColumnCount(), item); +} + +void wxListCtrlXmlHandler::HandleListItem() +{ + wxListCtrl * const list = wxDynamicCast(m_parentAsWindow, wxListCtrl); + wxCHECK_RET( list, "must have wxListCtrl parent" ); + + wxListItem item; + + HandleCommonItemAttrs(item); + if (HasParam(wxT("bg"))) item.SetBackgroundColour(GetColour(wxT("bg"))); if (HasParam(wxT("col"))) @@ -103,20 +149,12 @@ long wxListCtrlXmlHandler::Handle_wxListItem() item.SetData(GetLong(wxT("data"))); if (HasParam(wxT("font"))) item.SetFont(GetFont()); - if (HasParam(wxT("mask"))) - item.SetMask(GetStyle(wxT("mask"))); if (HasParam(wxT("state"))) item.SetState(GetStyle(wxT("state"))); - if (HasParam(wxT("statemask"))) - item.SetStateMask(GetStyle(wxT("statemask"))); - if (HasParam(wxT("text"))) - item.SetText(GetText(wxT("text"))); if (HasParam(wxT("textcolour"))) item.SetTextColour(GetColour(wxT("textcolour"))); if (HasParam(wxT("textcolor"))) item.SetTextColour(GetColour(wxT("textcolor"))); - if (HasParam(wxT("width"))) - item.SetWidth((int)GetLong(wxT("width"))); // the list control icon style, may be 0 int image; @@ -133,10 +171,10 @@ long wxListCtrlXmlHandler::Handle_wxListItem() // append the list item to the control item.SetId(list->GetItemCount()); - return list->InsertItem(item); + list->InsertItem(item); } -wxObject* wxListCtrlXmlHandler::Handle_wxListCtrl() +wxListCtrl *wxListCtrlXmlHandler::HandleListCtrl() { XRC_MAKE_INSTANCE(list, wxListCtrl)