xref: /haiku/src/add-ons/translators/sgi/SGIView.cpp (revision 7a74a5df454197933bc6e80a542102362ee98703)
1 /*****************************************************************************/
2 // SGIView
3 // Adopted by Stephan Aßmus, <stippi@yellowbites.com>
4 // from TIFFView written by
5 // Picking the compression method added by Stephan Aßmus, <stippi@yellowbites.com>
6 //
7 // SGIView.cpp
8 //
9 // This BView based object displays information about the SGITranslator.
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 #include <String.h>
43 #include <StringView.h>
44 #include <TextView.h>
45 #include <Window.h>
46 
47 #include "SGIImage.h"
48 #include "SGITranslator.h"
49 #include "SGIView.h"
50 
51 const char* author = "Stephan Aßmus, <stippi@yellowbites.com>";
52 
53 #undef B_TRANSLATION_CONTEXT
54 #define B_TRANSLATION_CONTEXT "SGIView"
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 	BMessage* message = new BMessage(SGIView::MSG_COMPRESSION_CHANGED);
64 	message->AddInt32("value", compression);
65 	BMenuItem* item = new BMenuItem(label, message);
66 	item->SetMarked(currentCompression == compression);
67 	menu->AddItem(item);
68 }
69 
70 // ---------------------------------------------------------------
71 // Constructor
72 //
73 // Sets up the view settings
74 //
75 // Preconditions:
76 //
77 // Parameters:
78 //
79 // Postconditions:
80 //
81 // Returns:
82 // ---------------------------------------------------------------
83 SGIView::SGIView(const char* name, uint32 flags, TranslatorSettings* settings)
84 	:
85 	BView(name, flags, new BGroupLayout(B_VERTICAL)),
86 	fSettings(settings)
87 {
88 	BPopUpMenu* menu = new BPopUpMenu("pick compression");
89 
90 	uint32 currentCompression =
91 		fSettings->SetGetInt32(SGI_SETTING_COMPRESSION);
92 	// create the menu items with the various compression methods
93 	add_menu_item(menu, SGI_COMP_NONE, B_TRANSLATE("None"),
94 		currentCompression);
95 //	menu->AddSeparatorItem();
96 	add_menu_item(menu, SGI_COMP_RLE, B_TRANSLATE("RLE"), currentCompression);
97 
98 // DON'T turn this on, it's so slow that I didn't wait long enough
99 // the one time I tested this. So I don't know if the code even works.
100 // Supposedly, this would look for an already written scanline, and
101 // modify the scanline tables so that the current row is not written
102 // at all...
103 
104 //	add_menu_item(menu, SGI_COMP_ARLE, "Agressive RLE", currentCompression);
105 
106 	fCompressionMF = new BMenuField("compression",
107 		B_TRANSLATE("Use compression:"), menu);
108 
109 	BAlignment labelAlignment(B_ALIGN_LEFT, B_ALIGN_NO_VERTICAL);
110 
111 	BStringView* titleView = new BStringView("title",
112 		B_TRANSLATE("SGI image translator"));
113 	titleView->SetFont(be_bold_font);
114 	titleView->SetExplicitAlignment(labelAlignment);
115 
116 	char detail[100];
117 	sprintf(detail, B_TRANSLATE("Version %d.%d.%d %s"),
118 		static_cast<int>(B_TRANSLATION_MAJOR_VERSION(SGI_TRANSLATOR_VERSION)),
119 		static_cast<int>(B_TRANSLATION_MINOR_VERSION(SGI_TRANSLATOR_VERSION)),
120 		static_cast<int>(B_TRANSLATION_REVISION_VERSION(
121 			SGI_TRANSLATOR_VERSION)), __DATE__);
122 	BStringView* detailView = new BStringView("details", detail);
123 	detailView->SetExplicitAlignment(labelAlignment);
124 
125 	BTextView* infoView = new BTextView("info");
126 	infoView->SetText(BString(B_TRANSLATE("written by:\n"))
127 			.Append(author)
128 			.Append(B_TRANSLATE("\n\nbased on GIMP SGI plugin v1.5:\n"))
129 			.Append(kSGICopyright).String());
130 	infoView->SetExplicitAlignment(labelAlignment);
131 	infoView->SetWordWrap(false);
132 	infoView->MakeEditable(false);
133 	infoView->MakeResizable(true);
134 	infoView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
135 
136 	float padding = 5.0f;
137 	BLayoutBuilder::Group<>(this, B_VERTICAL, padding)
138 		.SetInsets(padding)
139 		.Add(titleView)
140 		.Add(detailView)
141 		.Add(fCompressionMF)
142 		.Add(infoView)
143 		.AddGlue();
144 
145 	BFont font;
146 	GetFont(&font);
147  	SetExplicitPreferredSize(
148 		BSize((font.Size() * 390) / 12, (font.Size() * 180) / 12));
149 
150 	// TODO: remove this workaround for ticket #4217
151 	infoView->SetExplicitPreferredSize(
152 		BSize(infoView->LineWidth(4), infoView->TextHeight(0, 80)));
153 	infoView->SetExplicitMaxSize(infoView->ExplicitPreferredSize());
154 	infoView->SetExplicitMinSize(infoView->ExplicitPreferredSize());
155 }
156 
157 
158 // ---------------------------------------------------------------
159 // Destructor
160 //
161 // Does nothing
162 //
163 // Preconditions:
164 //
165 // Parameters:
166 //
167 // Postconditions:
168 //
169 // Returns:
170 // ---------------------------------------------------------------
171 SGIView::~SGIView()
172 {
173 	fSettings->Release();
174 }
175 
176 // ---------------------------------------------------------------
177 // MessageReceived
178 //
179 // Handles state changes of the Compression menu field
180 //
181 // Preconditions:
182 //
183 // Parameters: area,	not used
184 //
185 // Postconditions:
186 //
187 // Returns:
188 // ---------------------------------------------------------------
189 void
190 SGIView::MessageReceived(BMessage* message)
191 {
192 	switch (message->what) {
193 		case MSG_COMPRESSION_CHANGED: {
194 			int32 value;
195 			if (message->FindInt32("value", &value) >= B_OK) {
196 				fSettings->SetGetInt32(SGI_SETTING_COMPRESSION, &value);
197 				fSettings->SaveSettings();
198 			}
199 			fCompressionMF->ResizeToPreferred();
200 			break;
201 		}
202 		default:
203 			BView::MessageReceived(message);
204 	}
205 }
206 
207 // ---------------------------------------------------------------
208 // AllAttached
209 //
210 // sets the target for the controls controlling the configuration
211 //
212 // Preconditions:
213 //
214 // Parameters: area,	not used
215 //
216 // Postconditions:
217 //
218 // Returns:
219 // ---------------------------------------------------------------
220 void
221 SGIView::AllAttached()
222 {
223 	fCompressionMF->Menu()->SetTargetForItems(this);
224 }
225 
226