wxListCtrl - how to right-aligne the first column ? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
tiwag
Earned some good credits
Earned some good credits
Posts: 123
Joined: Tue Dec 21, 2004 8:51 pm
Location: Austria

wxListCtrl - how to right-aligne the first column ?

Post by tiwag » Mon Feb 20, 2006 7:53 pm

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.SetImage(-1);
    itemCol.SetAlign(wxLIST_FORMAT_RIGHT);   // <-- doesn't work, always left aligned ;-(
    m_listCtrl->InsertColumn(0, itemCol);

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

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

    // to speed up inserting we hide the control temporarily
    m_listCtrl->Hide();

    wxStopWatch sw;

    for ( int i = 0; i < NUM_ITEMS; i++ )
    {
        m_listCtrl->InsertItemInReportView(i);
    }

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

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

    item.m_itemId = 2;
    item.SetTextColour(*wxGREEN);
    m_listCtrl->SetItem( item );
    item.m_itemId = 4;
    item.SetTextColour(*wxLIGHT_GREY);
    item.SetFont(*wxITALIC_FONT);
    item.SetBackgroundColour(*wxRED);
    m_listCtrl->SetItem( item );

    m_listCtrl->SetTextColour(*wxBLUE);
    m_listCtrl->SetBackgroundColour(*wxLIGHT_GREY);

    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);
}

-tiwag

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4425
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN » 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.

tiwag
Earned some good credits
Earned some good credits
Posts: 123
Joined: Tue Dec 21, 2004 8:51 pm
Location: Austria

Post by tiwag » Mon Feb 20, 2006 8:11 pm

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 ?
-tiwag

ivan
Earned a small fee
Earned a small fee
Posts: 17
Joined: Wed Feb 15, 2006 9:40 am
Location: Serbia & Montenegro

Post by ivan » Mon Feb 20, 2006 8:39 pm

If you are on Windows, The first column of the ListCtrl can only be left-aligned and you can not do nothing about that.

tiwag
Earned some good credits
Earned some good credits
Posts: 123
Joined: Tue Dec 21, 2004 8:51 pm
Location: Austria

Post by tiwag » Tue Feb 21, 2006 6:46 am

is that a known wxWidgets bug ?
-tiwag

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4425
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN » Tue Feb 21, 2006 10:13 am

tiwag,
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.

tiwag
Earned some good credits
Earned some good credits
Posts: 123
Joined: Tue Dec 21, 2004 8:51 pm
Location: Austria

Post by tiwag » Tue Feb 21, 2006 11:17 am

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                |
|..dk/include/lib1.h |      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 !
-tiwag

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4425
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN » 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.

tiwag
Earned some good credits
Earned some good credits
Posts: 123
Joined: Tue Dec 21, 2004 8:51 pm
Location: Austria

Post by tiwag » Tue Feb 21, 2006 8:47 pm

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 !!!

thanks
-tiwag

Post Reply