diff --git a/src/preferences/print/PrinterListView.cpp b/src/preferences/print/PrinterListView.cpp index 2bd757ccb3..bb953f90f4 100644 --- a/src/preferences/print/PrinterListView.cpp +++ b/src/preferences/print/PrinterListView.cpp @@ -36,6 +36,8 @@ PrinterListView::PrinterListView(BRect frame) fFolder(NULL), fActivePrinter(NULL) { + fLayoutData.fLeftColumnMaximumWidth = 100; + fLayoutData.fRightColumnMaximumWidth = 100; } @@ -65,8 +67,10 @@ PrinterListView::BuildPrinterList() BEntry entry; while(dir.GetNextEntry(&entry) == B_OK) { BDirectory printer(&entry); - _AddPrinter(printer); + _AddPrinter(printer, false); } + + _LayoutPrinterItems(); } @@ -147,7 +151,7 @@ PrinterListView::SelectedItem() const void -PrinterListView::_AddPrinter(BDirectory& printer) +PrinterListView::_AddPrinter(BDirectory& printer, bool calculateLayout) { BString state; node_ref node; @@ -164,12 +168,38 @@ PrinterListView::_AddPrinter(BDirectory& printer) if (info.GetType(buffer) == B_OK && strcmp(buffer, PSRV_PRINTER_FILETYPE) == 0) { // Yes, it is a printer definition node - AddItem(new PrinterItem(dynamic_cast(Window()), printer)); + AddItem(new PrinterItem(dynamic_cast(Window()), + printer, fLayoutData)); + if (calculateLayout) + _LayoutPrinterItems(); } } } +void +PrinterListView::_LayoutPrinterItems() +{ + float& leftColumnMaximumWidth = fLayoutData.fLeftColumnMaximumWidth; + float& rightColumnMaximumWidth = fLayoutData.fRightColumnMaximumWidth; + + for (int32 i = 0; i < CountItems(); i ++) { + PrinterItem* item = dynamic_cast(ItemAt(i)); + + float leftColumnWidth = 0; + float rightColumnWidth = 0; + item->GetColumnWidth(this, leftColumnWidth, rightColumnWidth); + + leftColumnMaximumWidth = MAX(leftColumnMaximumWidth, + leftColumnWidth); + rightColumnMaximumWidth = MAX(rightColumnMaximumWidth, + rightColumnWidth); + } + + Invalidate(); +} + + PrinterItem* PrinterListView::_FindItem(node_ref* node) const { @@ -187,7 +217,7 @@ void PrinterListView::_EntryCreated(node_ref* node, entry_ref* entry) { BDirectory printer(node); - _AddPrinter(printer); + _AddPrinter(printer, true); } @@ -209,7 +239,7 @@ void PrinterListView::_AttributeChanged(node_ref* node) { BDirectory printer(node); - _AddPrinter(printer); + _AddPrinter(printer, true); } @@ -222,10 +252,12 @@ BBitmap* PrinterItem::sIcon = NULL; BBitmap* PrinterItem::sSelectedIcon = NULL; -PrinterItem::PrinterItem(PrintersWindow* window, const BDirectory& node) +PrinterItem::PrinterItem(PrintersWindow* window, const BDirectory& node, + PrinterListLayoutData& layoutData) : BListItem(0, false), fFolder(NULL), - fNode(node) + fNode(node), + fLayoutData(layoutData) { BRect rect(0, 0, B_LARGE_ICON - 1, B_LARGE_ICON - 1); if (sIcon == NULL) { @@ -287,6 +319,21 @@ PrinterItem::~PrinterItem() } +void +PrinterItem::GetColumnWidth(BView* view, float& leftColumn, float& rightColumn) +{ + BFont font; + view->GetFont(&font); + + leftColumn = font.StringWidth(fName.String()); + leftColumn = MAX(leftColumn, font.StringWidth(fDriverName.String())); + + rightColumn = font.StringWidth(fPendingJobs.String()); + rightColumn = MAX(rightColumn, font.StringWidth(fTransport.String())); + rightColumn = MAX(rightColumn, font.StringWidth(fComments.String())); +} + + void PrinterItem::Update(BView *owner, const BFont *font) { @@ -348,13 +395,24 @@ PrinterItem::DrawItem(BView *owner, BRect /*bounds*/, bool complete) owner->SetLowColor(oldLowColor); owner->SetHighColor(oldHighColor); - float x = B_LARGE_ICON + 8.0; + float iconColumnWidth = B_LARGE_ICON + 8.0; + float x = iconColumnWidth; BPoint iconPt(bounds.LeftTop() + BPoint(2.0, 2.0)); BPoint namePt(iconPt + BPoint(x, fntheight)); BPoint driverPt(iconPt + BPoint(x, fntheight * 2.0)); BPoint defaultPt(iconPt + BPoint(x, fntheight * 3.0)); - float width = owner->StringWidth(B_TRANSLATE("No pending jobs.")); + float totalWidth = bounds.Width() - iconColumnWidth; + float maximumWidth = fLayoutData.fLeftColumnMaximumWidth + + fLayoutData.fRightColumnMaximumWidth; + float width; + if (totalWidth < maximumWidth) { + width = fLayoutData.fRightColumnMaximumWidth * totalWidth / + maximumWidth; + } else { + width = fLayoutData.fRightColumnMaximumWidth; + } + BPoint pendingPt(bounds.right - width - 8.0, namePt.y); BPoint transportPt(bounds.right - width - 8.0, driverPt.y); BPoint commentPt(bounds.right - width - 8.0, defaultPt.y); diff --git a/src/preferences/print/PrinterListView.h b/src/preferences/print/PrinterListView.h index 5b5fd9b732..9ad77864b0 100644 --- a/src/preferences/print/PrinterListView.h +++ b/src/preferences/print/PrinterListView.h @@ -25,6 +25,14 @@ class BBitmap; class PrintersWindow; +struct PrinterListLayoutData +{ + float fLeftColumnMaximumWidth; + float fRightColumnMaximumWidth; +}; + + + class PrinterListView : public BListView, public FolderListener { public: PrinterListView(BRect frame); @@ -39,11 +47,12 @@ public: PrinterItem *ActivePrinter() const; void SetActivePrinter(PrinterItem* item); - + private: typedef BListView Inherited; - void _AddPrinter(BDirectory &printer); + void _AddPrinter(BDirectory &printer, bool calculateLayout); + void _LayoutPrinterItems(); PrinterItem *_FindItem(node_ref* node) const; void _EntryCreated(node_ref *node, @@ -53,15 +62,20 @@ private: FolderWatcher *fFolder; PrinterItem *fActivePrinter; + PrinterListLayoutData fLayoutData; }; class PrinterItem : public BListItem { public: - PrinterItem(PrintersWindow *window, - const BDirectory &node); + PrinterItem(PrintersWindow* window, + const BDirectory& node, + PrinterListLayoutData& layoutData); ~PrinterItem(); + void GetColumnWidth(BView* view, float& leftColumn, + float& rightColumn); + void DrawItem(BView *owner, BRect bounds, bool complete); void Update(BView *owner, const BFont *font); @@ -87,6 +101,7 @@ private: BString fDriverName; BString fName; BString fPendingJobs; + PrinterListLayoutData& fLayoutData; static BBitmap *sIcon; static BBitmap *sSelectedIcon;