Page 1 of 1

wxListCtrl - how to right-aligne the first column ?

Posted: Mon Feb 20, 2006 7:53 pm
by tiwag
i created a wxListCtrl with 3 columns,
i can right-aligne columns 1 and 2 without problems,
but column 0 is always Left-aligned regardless which alignement flags i send,

someone who knows a workaround ?
i'm using wxMSW-2.6.2

this is the modified code from the listctrl sample:

Code: Select all

void MyFrame::InitWithReportItems()
    m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);

    // note that under MSW for SetColumnWidth() to work we need to create the
    // items with images initially even if we specify dummy image id
    wxListItem itemCol;
    itemCol.SetText(_T("Column 1"));
    itemCol.SetAlign(wxLIST_FORMAT_RIGHT);   // <-- doesn't work, always left aligned ;-(
    m_listCtrl->InsertColumn(0, itemCol);

    itemCol.SetText(_T("Column 2"));
    m_listCtrl->InsertColumn(1, itemCol);

    itemCol.SetText(_T("Column 3"));
    m_listCtrl->InsertColumn(2, itemCol);

    // to speed up inserting we hide the control temporarily

    wxStopWatch sw;

    for ( int i = 0; i < NUM_ITEMS; i++ )

    m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
                                            NUM_ITEMS, sw.Time()));

    // we leave all mask fields to 0 and only change the colour
    wxListItem item;
    item.m_itemId = 0;
    m_listCtrl->SetItem( item );

    item.m_itemId = 2;
    m_listCtrl->SetItem( item );
    item.m_itemId = 4;
    m_listCtrl->SetItem( item );


    m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
    m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
    m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );

    // test SetItemFont too
    m_listCtrl->SetItemFont(0, *wxITALIC_FONT);

Posted: Mon Feb 20, 2006 8:01 pm
Hi, tiwag,
Maybe its an OS behavior?
Why do you need this? Maybe you will be beter with the Grid?

Thank you.

Posted: Mon Feb 20, 2006 8:11 pm
by tiwag
ONEEYEMAN wrote:Hi, tiwag,
Maybe its an OS behavior?
Why do you need this?
want to display in the first colum filenames with pathes, its hard to read when left-aligned and filenames have very long and of course different sizes...
Maybe you will be beter with the Grid?
maybe, but an existing project is using already wxListCtrl for that and rewriting all the code isn't an option for now.

i don't understand why it shouldn't work to right-aligne the first column ??
i've searched already for this (bug?) but i found no acknowledgments for that behaviour - maybe i'm doing something wrong ?

Posted: Mon Feb 20, 2006 8:39 pm
by ivan
If you are on Windows, The first column of the ListCtrl can only be left-aligned and you can not do nothing about that.

Posted: Tue Feb 21, 2006 6:46 am
by tiwag
is that a known wxWidgets bug ?

Posted: Tue Feb 21, 2006 10:13 am
This is not wx issue, it is the OS. Windows is very strict in this case.
Just do it the same way as the Explorer - create a simngle-line edit box for the path.

Thank you.

Posted: Tue Feb 21, 2006 11:17 am
by tiwag
ONEEYEMAN wrote:tiwag,
... create a simngle-line edit box for the path. ...
hahaha - how do you output data from a "search in files"
query in a single-line edit box ?

i'll explain it: assume you searched for "error" in all of your project files,
then the search-log output should look like:

Code: Select all

columns alignement mode
|              right |   right | left                |

|               File | linenr. | text                |
| |      12 | extern int error;   |
|../sdk/src/lib1.cpp |    2245 |     error = E12;    |

you get the idea ?

with the first column right-aligned, the filename could be read
even when the complete filename with path can't be displayed,
because it is too long.
when it's left-aligned, you'll ever read just the beginning of the path
of the filenames ... which is very bad !

Posted: Tue Feb 21, 2006 5:54 pm
Or you can do it the same way as Explorer.
You create 2 columns - one for file name, one for the path, and make them resizable.
What Windows version are you using?

Thank you.

Posted: Tue Feb 21, 2006 8:47 pm
by tiwag
thanks Oneeyeman for your tips,

i solved the issue by inserting a dummy column 0 which isn't shown.
all subsequent columns can be right-aligned

i'm using WinXP, but this doesn't matter, it seems to me as it is a wx bug.

in the meantime i found another code-fragment by googling around,
with a remark, that this wasn't working since early wx-2.5.3 !!!