xref: /haiku/src/add-ons/translators/tiff/TIFFView.cpp (revision 16d5c24e533eb14b7b8a99ee9f3ec9ba66335b1e)
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 //
6 // TIFFView.cpp
7 //
8 // This BView based object displays information about the TIFFTranslator.
9 //
10 //
11 // Copyright (c) 2003 OpenBeOS Project
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 /*****************************************************************************/
31 
32 #include <stdio.h>
33 #include <string.h>
34 
35 #include <MenuBar.h>
36 #include <MenuField.h>
37 #include <MenuItem.h>
38 #include <PopUpMenu.h>
39 
40 #include "tiff.h"
41 #include "tiffvers.h"
42 
43 #include "TIFFTranslator.h"
44 #include "TranslatorSettings.h"
45 
46 #include "TIFFView.h"
47 
48 // add_menu_item
49 void
50 add_menu_item(BMenu* menu,
51 			  uint32 compression,
52 			  const char* label,
53 			  uint32 currentCompression)
54 {
55 	// COMPRESSION_NONE
56 	BMessage* message = new BMessage(TIFFView::MSG_COMPRESSION_CHANGED);
57 	message->AddInt32("value", compression);
58 	BMenuItem* item = new BMenuItem(label, message);
59 	item->SetMarked(currentCompression == compression);
60 	menu->AddItem(item);
61 }
62 
63 // ---------------------------------------------------------------
64 // Constructor
65 //
66 // Sets up the view settings
67 //
68 // Preconditions:
69 //
70 // Parameters:
71 //
72 // Postconditions:
73 //
74 // Returns:
75 // ---------------------------------------------------------------
76 TIFFView::TIFFView(const BRect &frame, const char *name,
77 	uint32 resize, uint32 flags, TranslatorSettings *settings)
78 	:	BView(frame, name, resize, flags)
79 {
80 	fSettings = settings;
81 
82 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
83 	SetLowColor(ViewColor());
84 
85 	BPopUpMenu* menu = new BPopUpMenu("pick compression");
86 
87 	uint32 currentCompression = fSettings->SetGetInt32(TIFF_SETTING_COMPRESSION);
88 	// create the menu items with the various compression methods
89 	add_menu_item(menu, COMPRESSION_NONE, "None", currentCompression);
90 	menu->AddSeparatorItem();
91 	add_menu_item(menu, COMPRESSION_PACKBITS, "RLE (Packbits)", currentCompression);
92 	add_menu_item(menu, COMPRESSION_DEFLATE, "ZIP (Deflate)", currentCompression);
93 	add_menu_item(menu, COMPRESSION_LZW, "LZW", currentCompression);
94 
95 // TODO: the disabled compression modes are not configured in libTIFF
96 //	menu->AddSeparatorItem();
97 //	add_menu_item(menu, COMPRESSION_JPEG, "JPEG", currentCompression);
98 // TODO ? - strip encoding is not implemented in libTIFF for this compression
99 //	add_menu_item(menu, COMPRESSION_JP2000, "JPEG2000", currentCompression);
100 
101 	fCompressionMF = new BMenuField(BRect(20, 50, 215, 70), "compression",
102 									"Use Compression:", menu, true);
103 	fCompressionMF->ResizeToPreferred();
104 	fCompressionMF->SetDivider(
105 		fCompressionMF->StringWidth(fCompressionMF->Label()) + 7);
106 	AddChild(fCompressionMF);
107 
108 }
109 
110 // ---------------------------------------------------------------
111 // Destructor
112 //
113 // Does nothing
114 //
115 // Preconditions:
116 //
117 // Parameters:
118 //
119 // Postconditions:
120 //
121 // Returns:
122 // ---------------------------------------------------------------
123 TIFFView::~TIFFView()
124 {
125 	fSettings->Release();
126 }
127 
128 // ---------------------------------------------------------------
129 // MessageReceived
130 //
131 // Handles state changes of the Compression menu field
132 //
133 // Preconditions:
134 //
135 // Parameters: area,	not used
136 //
137 // Postconditions:
138 //
139 // Returns:
140 // ---------------------------------------------------------------
141 void
142 TIFFView::MessageReceived(BMessage* message)
143 {
144 	switch (message->what) {
145 		case MSG_COMPRESSION_CHANGED: {
146 			int32 value;
147 			if (message->FindInt32("value", &value) >= B_OK) {
148 				fSettings->SetGetInt32(TIFF_SETTING_COMPRESSION, &value);
149 				fSettings->SaveSettings();
150 			}
151 			fCompressionMF->ResizeToPreferred();
152 			break;
153 		}
154 		default:
155 			BView::MessageReceived(message);
156 	}
157 }
158 
159 // ---------------------------------------------------------------
160 // AllAttached
161 //
162 // sets the target for the controls controlling the configuration
163 //
164 // Preconditions:
165 //
166 // Parameters: area,	not used
167 //
168 // Postconditions:
169 //
170 // Returns:
171 // ---------------------------------------------------------------
172 void
173 TIFFView::AllAttached()
174 {
175 	fCompressionMF->Menu()->SetTargetForItems(this);
176 	fCompressionMF->SetDivider(fCompressionMF->StringWidth(fCompressionMF->Label()) + 3);
177 	fCompressionMF->ResizeToPreferred();
178 }
179 
180 
181 // ---------------------------------------------------------------
182 // Draw
183 //
184 // Draws information about the TIFFTranslator to this view.
185 //
186 // Preconditions:
187 //
188 // Parameters: area,	not used
189 //
190 // Postconditions:
191 //
192 // Returns:
193 // ---------------------------------------------------------------
194 void
195 TIFFView::Draw(BRect area)
196 {
197 	SetFont(be_bold_font);
198 	font_height fh;
199 	GetFontHeight(&fh);
200 	float xbold, ybold;
201 	xbold = fh.descent + 1;
202 	ybold = fh.ascent + fh.descent * 2 + fh.leading;
203 
204 	char title[] = "TIFF Image Translator";
205 	DrawString(title, BPoint(xbold, ybold));
206 
207 	SetFont(be_plain_font);
208 	font_height plainh;
209 	GetFontHeight(&plainh);
210 	float yplain;
211 	yplain = plainh.ascent + plainh.descent * 2 + plainh.leading;
212 
213 	char detail[100];
214 	sprintf(detail, "Version %d.%d.%d %s",
215 		static_cast<int>(B_TRANSLATION_MAJOR_VERSION(TIFF_TRANSLATOR_VERSION)),
216 		static_cast<int>(B_TRANSLATION_MINOR_VERSION(TIFF_TRANSLATOR_VERSION)),
217 		static_cast<int>(B_TRANSLATION_REVISION_VERSION(TIFF_TRANSLATOR_VERSION)),
218 		__DATE__);
219 	DrawString(detail, BPoint(xbold, yplain + ybold));
220 
221 	int32 lineno = 6;
222 	DrawString("TIFF Library:", BPoint(xbold, yplain * lineno + ybold));
223 	lineno += 2;
224 
225 	char libtiff[] = TIFFLIB_VERSION_STR;
226 	char *tok = strtok(libtiff, "\n");
227 	while (tok) {
228 		DrawString(tok, BPoint(xbold, yplain * lineno + ybold));
229 		lineno++;
230 		tok = strtok(NULL, "\n");
231 	}
232 }
233