xref: /haiku/src/add-ons/translators/tiff/TIFFView.cpp (revision 52c4471a3024d2eb81fe88e2c3982b9f8daa5e56)
1 /*****************************************************************************/
2 // TIFFView
3 // Written by Michael Wilber, Haiku Translation Kit Team
4 // Picking the compression method added by Stephan Aßmus, <stippi@yellowbites.com>
5 // Use of Layout API added by Maxime Simon, maxime.simon@gmail.com, 2009.
6 //
7 // TIFFView.cpp
8 //
9 // This BView based object displays information about the TIFFTranslator.
10 //
11 //
12 // Copyright (c) 2003 Haiku Project
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 /*****************************************************************************/
32 
33 
34 #include "TIFFView.h"
35 
36 #include <stdio.h>
37 #include <string.h>
38 
39 #include <Catalog.h>
40 #include <LayoutBuilder.h>
41 #include <MenuBar.h>
42 #include <MenuField.h>
43 #include <MenuItem.h>
44 #include <PopUpMenu.h>
45 
46 #define TIFF_DISABLE_DEPRECATED
47 #include "tiff.h"
48 #include "tiffvers.h"
49 
50 #include "TIFFTranslator.h"
51 #include "TranslatorSettings.h"
52 
53 
54 #undef B_TRANSLATION_CONTEXT
55 #define B_TRANSLATION_CONTEXT "TIFFView"
56 
57 // add_menu_item
58 void
59 add_menu_item(BMenu* menu,
60 			  uint32 compression,
61 			  const char* label,
62 			  uint32 currentCompression)
63 {
64 	// COMPRESSION_NONE
65 	BMessage* message = new BMessage(TIFFView::MSG_COMPRESSION_CHANGED);
66 	message->AddInt32("value", compression);
67 	BMenuItem* item = new BMenuItem(label, message);
68 	item->SetMarked(currentCompression == compression);
69 	menu->AddItem(item);
70 }
71 
72 
73 TIFFView::TIFFView(const char* name, uint32 flags,
74 	TranslatorSettings* settings)
75 	:
76 	BView(name, flags)
77 {
78 	fSettings = settings;
79 
80 	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
81 	SetLowUIColor(ViewUIColor());
82 
83 	fTitle = new BStringView("title", B_TRANSLATE("TIFF image translator"));
84 	fTitle->SetFont(be_bold_font);
85 
86 	char detail[100];
87 	sprintf(detail, B_TRANSLATE("Version %d.%d.%d, %s"),
88 		static_cast<int>(B_TRANSLATION_MAJOR_VERSION(TIFF_TRANSLATOR_VERSION)),
89 		static_cast<int>(B_TRANSLATION_MINOR_VERSION(TIFF_TRANSLATOR_VERSION)),
90 		static_cast<int>(B_TRANSLATION_REVISION_VERSION(
91 			TIFF_TRANSLATOR_VERSION)), __DATE__);
92 	fDetail = new BStringView("detail", detail);
93 
94 	int16 i = 1;
95 	fLibTIFF[0] = new BStringView(NULL, B_TRANSLATE("TIFF Library:"));
96 	char libtiff[] = TIFFLIB_VERSION_STR;
97 	char* tok = strtok(libtiff, "\n");
98 	while (i < 5 && tok) {
99 		fLibTIFF[i] = new BStringView(NULL, tok);
100 		tok = strtok(NULL, "\n");
101 		i++;
102 	}
103 
104 	BPopUpMenu* menu = new BPopUpMenu("pick compression");
105 
106 	uint32 currentCompression = fSettings->SetGetInt32(TIFF_SETTING_COMPRESSION);
107 	// create the menu items with the various compression methods
108 	add_menu_item(menu, COMPRESSION_NONE, B_TRANSLATE("None"),
109 		currentCompression);
110 	menu->AddSeparatorItem();
111 	add_menu_item(menu, COMPRESSION_PACKBITS, B_TRANSLATE("RLE (Packbits)"),
112 		currentCompression);
113 	add_menu_item(menu, COMPRESSION_DEFLATE, B_TRANSLATE("ZIP (Deflate)"),
114 		currentCompression);
115 	add_menu_item(menu, COMPRESSION_LZW, B_TRANSLATE("LZW"),
116 		currentCompression);
117 
118 	// TODO: the disabled compression modes are not configured in libTIFF
119 	//	menu->AddSeparatorItem();
120 	//	add_menu_item(menu, COMPRESSION_JPEG, "JPEG", currentCompression);
121 	// TODO ? - strip encoding is not implemented in libTIFF for this compression
122 	//	add_menu_item(menu, COMPRESSION_JP2000, "JPEG2000", currentCompression);
123 
124 	fCompressionMF = new BMenuField(B_TRANSLATE("Use compression:"), menu);
125 
126 	// Build the layout
127 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
128 		.SetInsets(B_USE_DEFAULT_SPACING)
129 		.Add(fTitle)
130 		.Add(fDetail)
131 		.AddGlue()
132 		.AddGroup(B_HORIZONTAL)
133 			.Add(fCompressionMF)
134 			.AddGlue()
135 			.End()
136 		.AddGlue()
137 		.Add(fLibTIFF[0])
138 		.Add(fLibTIFF[1])
139 		.Add(fLibTIFF[2])
140 		.Add(fLibTIFF[3]);
141 			// Theses 4 adding above work because we know there are 4 strings
142 			// but it's fragile: one string less in the library version and the
143 			// application breaks
144 
145 	BFont font;
146 	GetFont(&font);
147 	SetExplicitPreferredSize(
148 		BSize((font.Size() * 350)/12, (font.Size() * 200)/12));
149 }
150 
151 
152 TIFFView::~TIFFView()
153 {
154 	fSettings->Release();
155 }
156 
157 
158 void
159 TIFFView::AllAttached()
160 {
161 	fCompressionMF->Menu()->SetTargetForItems(this);
162 }
163 
164 
165 void
166 TIFFView::MessageReceived(BMessage* message)
167 {
168 	switch (message->what) {
169 		case MSG_COMPRESSION_CHANGED: {
170 			int32 value;
171 			if (message->FindInt32("value", &value) >= B_OK) {
172 				fSettings->SetGetInt32(TIFF_SETTING_COMPRESSION, &value);
173 				fSettings->SaveSettings();
174 			}
175 			break;
176 		}
177 		default:
178 			BView::MessageReceived(message);
179 	}
180 }
181