xref: /haiku/src/add-ons/translators/tiff/TIFFView.cpp (revision 1c09002cbee8e797a0f8bbfc5678dfadd39ee1a7)
1 /*****************************************************************************/
2 // TIFFView
3 // Written by Michael Wilber, OBOS 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 OpenBeOS 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 #include <stdio.h>
34 #include <string.h>
35 
36 #include <Catalog.h>
37 #include <GridLayoutBuilder.h>
38 #include <GroupLayout.h>
39 #include <GroupLayoutBuilder.h>
40 #include <MenuBar.h>
41 #include <MenuField.h>
42 #include <MenuItem.h>
43 #include <PopUpMenu.h>
44 
45 #include "tiff.h"
46 #include "tiffvers.h"
47 
48 #include "TIFFTranslator.h"
49 #include "TranslatorSettings.h"
50 
51 #include "TIFFView.h"
52 
53 #undef B_TRANSLATE_CONTEXT
54 #define B_TRANSLATE_CONTEXT "TIFFView"
55 
56 // add_menu_item
57 void
58 add_menu_item(BMenu* menu,
59 			  uint32 compression,
60 			  const char* label,
61 			  uint32 currentCompression)
62 {
63 	// COMPRESSION_NONE
64 	BMessage* message = new BMessage(TIFFView::MSG_COMPRESSION_CHANGED);
65 	message->AddInt32("value", compression);
66 	BMenuItem* item = new BMenuItem(label, message);
67 	item->SetMarked(currentCompression == compression);
68 	menu->AddItem(item);
69 }
70 
71 // ---------------------------------------------------------------
72 // Constructor
73 //
74 // Sets up the view settings
75 //
76 // Preconditions:
77 //
78 // Parameters:
79 //
80 // Postconditions:
81 //
82 // Returns:
83 // ---------------------------------------------------------------
84 TIFFView::TIFFView(const char *name, uint32 flags,
85 	TranslatorSettings *settings)
86 	:	BView(name, flags)
87 {
88 	fSettings = settings;
89 
90 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
91 	SetLowColor(ViewColor());
92 
93 	fTitle = new BStringView("title", B_TRANSLATE("TIFF Image Translator"));
94 	fTitle->SetFont(be_bold_font);
95 
96 	char detail[100];
97 	sprintf(detail, B_TRANSLATE("Version %d.%d.%d %s"),
98 		static_cast<int>(B_TRANSLATION_MAJOR_VERSION(TIFF_TRANSLATOR_VERSION)),
99 		static_cast<int>(B_TRANSLATION_MINOR_VERSION(TIFF_TRANSLATOR_VERSION)),
100 		static_cast<int>(B_TRANSLATION_REVISION_VERSION(
101 			TIFF_TRANSLATOR_VERSION)), __DATE__);
102 	fDetail = new BStringView("detail", detail);
103 
104 	int16 i = 1;
105 	fLibTIFF[0] = new BStringView(NULL, B_TRANSLATE("TIFF Library:"));
106 	char libtiff[] = TIFFLIB_VERSION_STR;
107     char *tok = strtok(libtiff, "\n");
108     while (i < 5 && tok) {
109 		fLibTIFF[i] = new BStringView(NULL, tok);
110 		tok = strtok(NULL, "\n");
111 		i++;
112     }
113 
114 	BPopUpMenu* menu = new BPopUpMenu("pick compression");
115 
116 	uint32 currentCompression = fSettings->SetGetInt32(TIFF_SETTING_COMPRESSION);
117 	// create the menu items with the various compression methods
118 	add_menu_item(menu, COMPRESSION_NONE, B_TRANSLATE("None"),
119 		currentCompression);
120 	menu->AddSeparatorItem();
121 	add_menu_item(menu, COMPRESSION_PACKBITS, B_TRANSLATE("RLE (Packbits)"),
122 		currentCompression);
123 	add_menu_item(menu, COMPRESSION_DEFLATE, B_TRANSLATE("ZIP (Deflate)"),
124 		currentCompression);
125 	add_menu_item(menu, COMPRESSION_LZW, B_TRANSLATE("LZW"),
126 		currentCompression);
127 
128 // TODO: the disabled compression modes are not configured in libTIFF
129 //	menu->AddSeparatorItem();
130 //	add_menu_item(menu, COMPRESSION_JPEG, "JPEG", currentCompression);
131 // TODO ? - strip encoding is not implemented in libTIFF for this compression
132 //	add_menu_item(menu, COMPRESSION_JP2000, "JPEG2000", currentCompression);
133 
134  	fCompressionMF = new BMenuField(B_TRANSLATE("Use Compression:"), menu,
135 		NULL);
136 
137  	// Build the layout
138  	SetLayout(new BGroupLayout(B_VERTICAL));
139 
140  	i = 0;
141  	AddChild(BGroupLayoutBuilder(B_VERTICAL, 7)
142  		.Add(fTitle)
143  		.Add(fDetail)
144  		.AddGlue()
145  		.Add(fCompressionMF)
146  		.AddGlue()
147  		.Add(fLibTIFF[0])
148  		.Add(fLibTIFF[1])
149  		.Add(fLibTIFF[2])
150  		.Add(fLibTIFF[3])
151  			// Theses 4 adding above work because we know there are 4 strings
152  			// but it's fragile: one string less in the library version and the application breaks
153  		.AddGlue()
154  		.SetInsets(5, 5, 5, 5)
155  	);
156 
157  	BFont font;
158  	GetFont(&font);
159  	SetExplicitPreferredSize(BSize((font.Size() * 350)/12, (font.Size() * 200)/12));
160 }
161 
162 // ---------------------------------------------------------------
163 // Destructor
164 //
165 // Does nothing
166 //
167 // Preconditions:
168 //
169 // Parameters:
170 //
171 // Postconditions:
172 //
173 // Returns:
174 // ---------------------------------------------------------------
175 TIFFView::~TIFFView()
176 {
177 	fSettings->Release();
178 }
179 
180 // ---------------------------------------------------------------
181 // MessageReceived
182 //
183 // Handles state changes of the Compression menu field
184 //
185 // Preconditions:
186 //
187 // Parameters: area,	not used
188 //
189 // Postconditions:
190 //
191 // Returns:
192 // ---------------------------------------------------------------
193 void
194 TIFFView::MessageReceived(BMessage* message)
195 {
196 	switch (message->what) {
197 		case MSG_COMPRESSION_CHANGED: {
198 			int32 value;
199 			if (message->FindInt32("value", &value) >= B_OK) {
200 				fSettings->SetGetInt32(TIFF_SETTING_COMPRESSION, &value);
201 				fSettings->SaveSettings();
202 			}
203 			break;
204 		}
205 		default:
206 			BView::MessageReceived(message);
207 	}
208 }
209 
210 // ---------------------------------------------------------------
211 // AllAttached
212 //
213 // sets the target for the controls controlling the configuration
214 //
215 // Preconditions:
216 //
217 // Parameters: area,	not used
218 //
219 // Postconditions:
220 //
221 // Returns:
222 // ---------------------------------------------------------------
223 void
224 TIFFView::AllAttached()
225 {
226 	fCompressionMF->Menu()->SetTargetForItems(this);
227 	fCompressionMF->SetDivider(fCompressionMF->StringWidth(fCompressionMF->Label()) + 3);
228 	fCompressionMF->ResizeToPreferred();
229 }
230 
231 
232