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