2 * Copyright 2008, Haiku.
3 * Distributed under the terms of the MIT license.
6 * Michael Pfeiffer <laplace@users.sourceforge.net>
9 #include "PrinterSelection.h"
10 #include "PPDParser.h"
11 #include "StatementListVisitor.h"
14 #include <Directory.h>
18 #include <ScrollView.h>
19 #include <StringView.h>
22 const float kLeftMargin
= 3.0;
23 const float kRightMargin
= 3.0;
24 const float kTopMargin
= 3.0;
25 const float kBottomMargin
= 3.0;
27 // space between views
28 const float kHorizontalSpace
= 8.0;
29 const float kVerticalSpace
= 8.0;
33 PrinterSelectionView::PrinterSelectionView(BRect bounds
, const char *name
, uint32 resizeMask
, uint32 flags
)
34 : BView(bounds
, name
, resizeMask
, flags
)
36 // add vendor list view
37 bounds
.OffsetTo(0, 0);
38 BRect
listBounds(bounds
.left
+ kLeftMargin
, bounds
.top
+ kTopMargin
,
39 bounds
.right
/ 3.0 - kHorizontalSpace
/ 2, bounds
.bottom
- kBottomMargin
);
40 listBounds
.right
-= B_V_SCROLL_BAR_WIDTH
;
41 listBounds
.bottom
-= B_H_SCROLL_BAR_HEIGHT
;
43 BStringView
* label
= new BStringView(listBounds
, "vendors-label", "Vendors:");
45 label
->ResizeToPreferred();
47 listBounds
.top
+= label
->Bounds().bottom
+ 5;
49 fVendors
= new BListView(listBounds
, "vendors", B_SINGLE_SELECTION_LIST
,
53 BScrollView
* scrollView
= new BScrollView("vendors-scroll-view",
54 fVendors
, B_FOLLOW_LEFT
| B_FOLLOW_TOP_BOTTOM
, 0, true, true);
59 BRect
printerBounds(listBounds
);
60 printerBounds
.left
= B_V_SCROLL_BAR_WIDTH
+ printerBounds
.right
+ kHorizontalSpace
;
61 printerBounds
.right
= bounds
.right
- kRightMargin
- B_V_SCROLL_BAR_WIDTH
;
62 printerBounds
.top
= bounds
.top
+ kTopMargin
;
63 label
= new BStringView(printerBounds
, "printers-label", "Printers:");
65 label
->ResizeToPreferred();
67 BRect
detailBounds(listBounds
);
68 detailBounds
.left
= B_V_SCROLL_BAR_WIDTH
+ detailBounds
.right
+ kHorizontalSpace
;
69 detailBounds
.right
= bounds
.right
- kRightMargin
- B_V_SCROLL_BAR_WIDTH
;
70 fPrinters
= new BListView(detailBounds
, "printers", B_SINGLE_SELECTION_LIST
,
73 scrollView
= new BScrollView("printers-scroll-view",
74 fPrinters
, B_FOLLOW_LEFT
| B_FOLLOW_TOP_BOTTOM
, 0, true, true);
79 void PrinterSelectionView::AttachedToWindow()
81 fVendors
->SetSelectionMessage(new BMessage('sel'));
82 fVendors
->SetTarget(this);
84 fPrinters
->SetSelectionMessage(new BMessage('prnt'));
85 fPrinters
->SetTarget(this);
89 void PrinterSelectionView::FillVendors()
91 BDirectory
directory("/boot/beos/etc/ppd");
93 while (directory
.GetNextEntry(&entry
) == B_OK
) {
94 char name
[B_FILE_NAME_LENGTH
];
98 fVendors
->AddItem(new FileItem(name
, path
.Path()));
102 void PrinterSelectionView::FillPrinters(const char* vendor
)
104 MakeEmpty(fPrinters
);
107 BDirectory
directory(vendor
);
109 while (directory
.GetNextEntry(&entry
) == B_OK
) {
110 char name
[B_FILE_NAME_LENGTH
];
113 entry
.GetPath(&path
);
115 PPDParser
parser(path
.Path());
116 PPD
* ppd
= parser
.ParseHeader();
117 if (parser
.HasWarning()) {
118 fprintf(stderr
, "Warning(s): %s", parser
.GetWarningMessage());
125 s
= ppd
->GetValue("ModelName");
129 s
= ppd
->GetValue("PCFileName");
131 label
<< " [" << s
<< "]";
133 s
= ppd
->GetValue("Manufacturer");
135 label
<< " (" << s
<< ")";
137 printers
.AddItem(new FileItem(label
.String(), path
.Path()));
140 fprintf(stderr
, "Parsing error (%s)\n%s\n", path
.Path(),
141 parser
.GetErrorMessage());
145 fPrinters
->AddList(&printers
);
148 void PrinterSelectionView::MessageReceived(BMessage
* msg
)
153 if (msg
->FindInt32("index", &index
) == B_OK
) {
154 FileItem
* file
= (FileItem
*)fVendors
->ItemAt(index
);
156 FillPrinters(file
->GetFile());
161 if (msg
->FindInt32("index", &index
) == B_OK
) {
162 FileItem
* file
= (FileItem
*)fPrinters
->ItemAt(index
);
164 BMessage
copy(*Message());
165 copy
.AddString("file", file
->GetFile());
171 BView::MessageReceived(msg
);