xref: /haiku/src/preferences/backgrounds/ImageFilePanel.cpp (revision 56eb8e78cc702792e3b032e3f5f45da9e5dbea9e)
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 void
34 ImageFilePanel::Show()
35 {
36 	if (fImageView == NULL) {
37 		Window()->Lock();
38 		BView *background = Window()->ChildAt(0);
39 		uint32 poseViewResizingMode =
40 			background->FindView("PoseView")->ResizingMode();
41 		uint32 countVwResizingMode =
42 			background->FindView("CountVw")->ResizingMode();
43 		uint32 vScrollBarResizingMode =
44 			background->FindView("VScrollBar")->ResizingMode();
45 		uint32 hScrollBarResizingMode =
46 			background->FindView("HScrollBar")->ResizingMode();
47 
48 		background->FindView("PoseView")->SetResizingMode(B_FOLLOW_LEFT | B_FOLLOW_TOP);
49 		background->FindView("CountVw")->SetResizingMode(B_FOLLOW_LEFT | B_FOLLOW_TOP);
50 		background->FindView("VScrollBar")->SetResizingMode(B_FOLLOW_LEFT | B_FOLLOW_TOP);
51 		background->FindView("HScrollBar")->SetResizingMode(B_FOLLOW_LEFT | B_FOLLOW_TOP);
52 		Window()->ResizeBy(0, 70);
53 		background->FindView("PoseView")->SetResizingMode(poseViewResizingMode);
54 		background->FindView("CountVw")->SetResizingMode(countVwResizingMode);
55 		background->FindView("VScrollBar")->SetResizingMode(vScrollBarResizingMode);
56 		background->FindView("HScrollBar")->SetResizingMode(hScrollBarResizingMode);
57 
58 		BRect rect(background->Bounds().left + 15, background->Bounds().bottom - 94,
59 			background->Bounds().left + 122, background->Bounds().bottom - 15);
60 		fImageView = new BView(rect, "ImageView", B_FOLLOW_LEFT | B_FOLLOW_BOTTOM,
61 			B_SUBPIXEL_PRECISE);
62 		fImageView->SetViewColor(background->ViewColor());
63 		background->AddChild(fImageView);
64 
65 		rect = BRect(background->Bounds().left + 132, background->Bounds().bottom - 85,
66 			background->Bounds().right, background->Bounds().bottom - 65);
67 		fResolutionView = new BStringView(rect, "ResolutionView", NULL,
68 			B_FOLLOW_LEFT | B_FOLLOW_BOTTOM);
69 		background->AddChild(fResolutionView);
70 
71 		rect.OffsetBy(0, -16);
72 		fImageTypeView = new BStringView(rect, "ImageTypeView", NULL,
73 			B_FOLLOW_LEFT | B_FOLLOW_BOTTOM);
74 		background->AddChild(fImageTypeView);
75 
76 		Window()->Unlock();
77 	}
78 
79 	BFilePanel::Show();
80 }
81 
82 
83 void
84 ImageFilePanel::SelectionChanged()
85 {
86 	entry_ref ref;
87 	Rewind();
88 
89 	if (GetNextSelectedRef(&ref) == B_OK) {
90 		BEntry entry(&ref);
91 		BNode node(&ref);
92 		fImageView->ClearViewBitmap();
93 
94 		if (node.IsFile()) {
95 			BBitmap *bitmap = BTranslationUtils::GetBitmap(&ref);
96 
97 			if (bitmap != NULL) {
98 				BRect dest(fImageView->Bounds());
99 				if (bitmap->Bounds().Width() > bitmap->Bounds().Height()) {
100 					dest.InsetBy(0, (dest.Height() + 1 -
101 						((bitmap->Bounds().Height() + 1) /
102 						 (bitmap->Bounds().Width() + 1) *(dest.Width() + 1))) / 2);
103 				} else {
104 					dest.InsetBy((dest.Width() + 1 -
105 						((bitmap->Bounds().Width() + 1) /
106 						 (bitmap->Bounds().Height() + 1) *(dest.Height() + 1))) / 2, 0);
107 				}
108 				fImageView->SetViewBitmap(bitmap, bitmap->Bounds(), dest,
109 					B_FOLLOW_LEFT | B_FOLLOW_TOP, 0);
110 
111 				BString resolution;
112 				resolution << "Resolution: " << (int)(bitmap->Bounds().Width() + 1)
113 					<< "x" << (int)(bitmap->Bounds().Height() + 1);
114 				fResolutionView->SetText(resolution.String());
115 				delete bitmap;
116 
117 				BNode node(&ref);
118 				BNodeInfo nodeInfo(&node);
119 				char type[B_MIME_TYPE_LENGTH];
120 				if (nodeInfo.GetType(type) == B_OK) {
121 					BMimeType mimeType(type);
122 					mimeType.GetShortDescription(type);
123 					// if this fails, the MIME type will be displayed
124 					fImageTypeView->SetText(type);
125 				} else {
126 					BMimeType refType;
127 					if (BMimeType::GuessMimeType(&ref, &refType) == B_OK) {
128 						refType.GetShortDescription(type);
129 						// if this fails, the MIME type will be displayed
130 						fImageTypeView->SetText(type);
131 					} else
132 						fImageTypeView->SetText("");
133 				}
134 			}
135 		} else {
136 			fResolutionView->SetText("");
137 			fImageTypeView->SetText("");
138 		}
139 		fImageView->Invalidate();
140 		fResolutionView->Invalidate();
141 	}
142 
143 	BFilePanel::SelectionChanged();
144 }
145 
146 
147 //	#pragma mark -
148 
149 
150 CustomRefFilter::CustomRefFilter(bool imageFiltering)
151 	:
152 	fImageFiltering(imageFiltering)
153 {
154 }
155 
156 
157 bool
158 CustomRefFilter::Filter(const entry_ref *ref, BNode* node,
159 	struct stat *stat, const char *filetype)
160 {
161 	if (!fImageFiltering)
162 		return node->IsDirectory();
163 
164 	if (node->IsDirectory())
165 		return true;
166 
167 	BMimeType imageType("image"), refType;
168 	BMimeType::GuessMimeType(ref, &refType);
169 	if (imageType.Contains(&refType))
170 		return true;
171 
172 	return false;
173 }
174 
175