xref: /haiku/src/preferences/backgrounds/ImageFilePanel.cpp (revision 82a8a20999118b748396cf16a33c47c3b0c0222d)
1 /*
2  * Copyright 2002-2005, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Jerome Duval (jerome.duval@free.fr)
7  */
8 
9 
10 #include "ImageFilePanel.h"
11 
12 #include <Bitmap.h>
13 #include <NodeInfo.h>
14 #include <String.h>
15 #include <StringView.h>
16 #include <TranslationUtils.h>
17 #include <Window.h>
18 
19 
20 ImageFilePanel::ImageFilePanel(file_panel_mode mode, BMessenger *target,
21 	const entry_ref *startDirectory, uint32 nodeFlavors,
22 	bool allowMultipleSelection, BMessage* message, BRefFilter* filter,
23 	bool modal,	bool hideWhenDone)
24 	: BFilePanel(mode, target, startDirectory, nodeFlavors,
25 		allowMultipleSelection, message, filter, modal, hideWhenDone),
26 	fImageView(NULL),
27 	fResolutionView(NULL),
28 	fImageTypeView(NULL)
29 {
30 }
31 
32 
33 ImageFilePanel::~ImageFilePanel()
34 {
35 	if (RefFilter())
36 		delete RefFilter();
37 }
38 
39 
40 void
41 ImageFilePanel::Show()
42 {
43 	if (fImageView == NULL) {
44 		Window()->Lock();
45 		BView *background = Window()->ChildAt(0);
46 		uint32 poseViewResizingMode =
47 			background->FindView("PoseView")->ResizingMode();
48 		uint32 countVwResizingMode =
49 			background->FindView("CountVw")->ResizingMode();
50 		uint32 vScrollBarResizingMode =
51 			background->FindView("VScrollBar")->ResizingMode();
52 		uint32 hScrollBarResizingMode =
53 			background->FindView("HScrollBar")->ResizingMode();
54 
55 		background->FindView("PoseView")->SetResizingMode(B_FOLLOW_LEFT | B_FOLLOW_TOP);
56 		background->FindView("CountVw")->SetResizingMode(B_FOLLOW_LEFT | B_FOLLOW_TOP);
57 		background->FindView("VScrollBar")->SetResizingMode(B_FOLLOW_LEFT | B_FOLLOW_TOP);
58 		background->FindView("HScrollBar")->SetResizingMode(B_FOLLOW_LEFT | B_FOLLOW_TOP);
59 		Window()->ResizeBy(0, 70);
60 		background->FindView("PoseView")->SetResizingMode(poseViewResizingMode);
61 		background->FindView("CountVw")->SetResizingMode(countVwResizingMode);
62 		background->FindView("VScrollBar")->SetResizingMode(vScrollBarResizingMode);
63 		background->FindView("HScrollBar")->SetResizingMode(hScrollBarResizingMode);
64 
65 		BRect rect(background->Bounds().left + 15, background->Bounds().bottom - 94,
66 			background->Bounds().left + 122, background->Bounds().bottom - 15);
67 		fImageView = new BView(rect, "ImageView", B_FOLLOW_LEFT | B_FOLLOW_BOTTOM,
68 			B_SUBPIXEL_PRECISE);
69 		fImageView->SetViewColor(background->ViewColor());
70 		background->AddChild(fImageView);
71 
72 		rect = BRect(background->Bounds().left + 132, background->Bounds().bottom - 85,
73 			background->Bounds().right, background->Bounds().bottom - 65);
74 		fResolutionView = new BStringView(rect, "ResolutionView", NULL,
75 			B_FOLLOW_LEFT | B_FOLLOW_BOTTOM);
76 		background->AddChild(fResolutionView);
77 
78 		rect.OffsetBy(0, -16);
79 		fImageTypeView = new BStringView(rect, "ImageTypeView", NULL,
80 			B_FOLLOW_LEFT | B_FOLLOW_BOTTOM);
81 		background->AddChild(fImageTypeView);
82 
83 		Window()->Unlock();
84 	}
85 
86 	BFilePanel::Show();
87 }
88 
89 
90 void
91 ImageFilePanel::SelectionChanged()
92 {
93 	entry_ref ref;
94 	Rewind();
95 
96 	if (GetNextSelectedRef(&ref) == B_OK) {
97 		BEntry entry(&ref);
98 		BNode node(&ref);
99 		fImageView->ClearViewBitmap();
100 
101 		if (node.IsFile()) {
102 			BBitmap *bitmap = BTranslationUtils::GetBitmap(&ref);
103 
104 			if (bitmap != NULL) {
105 				BRect dest(fImageView->Bounds());
106 				if (bitmap->Bounds().Width() > bitmap->Bounds().Height()) {
107 					dest.InsetBy(0, (dest.Height() + 1 -
108 						((bitmap->Bounds().Height() + 1) /
109 						 (bitmap->Bounds().Width() + 1) *(dest.Width() + 1))) / 2);
110 				} else {
111 					dest.InsetBy((dest.Width() + 1 -
112 						((bitmap->Bounds().Width() + 1) /
113 						 (bitmap->Bounds().Height() + 1) *(dest.Height() + 1))) / 2, 0);
114 				}
115 				fImageView->SetViewBitmap(bitmap, bitmap->Bounds(), dest,
116 					B_FOLLOW_LEFT | B_FOLLOW_TOP, 0);
117 
118 				BString resolution;
119 				resolution << "Resolution: " << (int)(bitmap->Bounds().Width() + 1)
120 					<< "x" << (int)(bitmap->Bounds().Height() + 1);
121 				fResolutionView->SetText(resolution.String());
122 				delete bitmap;
123 
124 				BNodeInfo nodeInfo(&node);
125 				char type[B_MIME_TYPE_LENGTH];
126 				if (nodeInfo.GetType(type) == B_OK) {
127 					BMimeType mimeType(type);
128 					mimeType.GetShortDescription(type);
129 					// if this fails, the MIME type will be displayed
130 					fImageTypeView->SetText(type);
131 				} else {
132 					BMimeType refType;
133 					if (BMimeType::GuessMimeType(&ref, &refType) == B_OK) {
134 						refType.GetShortDescription(type);
135 						// if this fails, the MIME type will be displayed
136 						fImageTypeView->SetText(type);
137 					} else
138 						fImageTypeView->SetText("");
139 				}
140 			}
141 		} else {
142 			fResolutionView->SetText("");
143 			fImageTypeView->SetText("");
144 		}
145 		fImageView->Invalidate();
146 		fResolutionView->Invalidate();
147 	}
148 
149 	BFilePanel::SelectionChanged();
150 }
151 
152 
153 //	#pragma mark -
154 
155 
156 CustomRefFilter::CustomRefFilter(bool imageFiltering)
157 	:
158 	fImageFiltering(imageFiltering)
159 {
160 }
161 
162 
163 bool
164 CustomRefFilter::Filter(const entry_ref *ref, BNode* node,
165 	struct stat_beos *stat, const char *filetype)
166 {
167 	if (!fImageFiltering)
168 		return node->IsDirectory();
169 
170 	if (node->IsDirectory())
171 		return true;
172 
173 	BMimeType imageType("image"), refType;
174 	BMimeType::GuessMimeType(ref, &refType);
175 	if (imageType.Contains(&refType))
176 		return true;
177 
178 	return false;
179 }
180 
181