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 <MenuBar.h> 37 #include <MenuField.h> 38 #include <MenuItem.h> 39 #include <PopUpMenu.h> 40 #include <Window.h> 41 42 #include "SGIImage.h" 43 #include "SGITranslator.h" 44 #include "SGIView.h" 45 46 const char* author = "Stephan Aßmus, <stippi@yellowbites.com>"; 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 BMessage* message = new BMessage(SGIView::MSG_COMPRESSION_CHANGED); 56 message->AddInt32("value", compression); 57 BMenuItem* item = new BMenuItem(label, message); 58 item->SetMarked(currentCompression == compression); 59 menu->AddItem(item); 60 } 61 62 // --------------------------------------------------------------- 63 // Constructor 64 // 65 // Sets up the view settings 66 // 67 // Preconditions: 68 // 69 // Parameters: 70 // 71 // Postconditions: 72 // 73 // Returns: 74 // --------------------------------------------------------------- 75 SGIView::SGIView(const BRect &frame, const char *name, 76 uint32 resize, uint32 flags, TranslatorSettings *settings) 77 : BView(frame, name, resize, flags), 78 fSettings(settings) 79 { 80 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 81 82 BPopUpMenu* menu = new BPopUpMenu("pick compression"); 83 84 uint32 currentCompression = fSettings->SetGetInt32(SGI_SETTING_COMPRESSION); 85 // create the menu items with the various compression methods 86 add_menu_item(menu, SGI_COMP_NONE, "None", currentCompression); 87 // menu->AddSeparatorItem(); 88 add_menu_item(menu, SGI_COMP_RLE, "RLE", currentCompression); 89 90 // DON'T turn this on, it's so slow that I didn't wait long enough 91 // the one time I tested this. So I don't know if the code even works. 92 // Supposedly, this would look for an already written scanline, and 93 // modify the scanline tables so that the current row is not written 94 // at all... 95 96 // add_menu_item(menu, SGI_COMP_ARLE, "Agressive RLE", currentCompression); 97 98 BRect menuFrame = Bounds(); 99 menuFrame.bottom = menuFrame.top + menu->Bounds().Height(); 100 fCompressionMF = new BMenuField(menuFrame, "compression", 101 "Use Compression:", menu, true/*, 102 B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP*/); 103 if (fCompressionMF->MenuBar()) 104 fCompressionMF->MenuBar()->ResizeToPreferred(); 105 fCompressionMF->ResizeToPreferred(); 106 107 // figure out where the text ends 108 font_height fh; 109 be_bold_font->GetHeight(&fh); 110 float xbold, ybold; 111 xbold = fh.descent + 1; 112 ybold = fh.ascent + fh.descent * 2 + fh.leading; 113 114 font_height plainh; 115 be_plain_font->GetHeight(&plainh); 116 float yplain; 117 yplain = plainh.ascent + plainh.descent * 2 + plainh.leading; 118 119 // position the menu field below all the text we draw in Draw() 120 BPoint textOffset(0.0, yplain * 2 + ybold); 121 fCompressionMF->MoveTo(textOffset); 122 123 AddChild(fCompressionMF); 124 125 ResizeToPreferred(); 126 } 127 128 // --------------------------------------------------------------- 129 // Destructor 130 // 131 // Does nothing 132 // 133 // Preconditions: 134 // 135 // Parameters: 136 // 137 // Postconditions: 138 // 139 // Returns: 140 // --------------------------------------------------------------- 141 SGIView::~SGIView() 142 { 143 fSettings->Release(); 144 } 145 146 // --------------------------------------------------------------- 147 // MessageReceived 148 // 149 // Handles state changes of the Compression menu field 150 // 151 // Preconditions: 152 // 153 // Parameters: area, not used 154 // 155 // Postconditions: 156 // 157 // Returns: 158 // --------------------------------------------------------------- 159 void 160 SGIView::MessageReceived(BMessage* message) 161 { 162 switch (message->what) { 163 case MSG_COMPRESSION_CHANGED: { 164 int32 value; 165 if (message->FindInt32("value", &value) >= B_OK) { 166 fSettings->SetGetInt32(SGI_SETTING_COMPRESSION, &value); 167 fSettings->SaveSettings(); 168 } 169 break; 170 } 171 default: 172 BView::MessageReceived(message); 173 } 174 } 175 176 // --------------------------------------------------------------- 177 // AllAttached 178 // 179 // sets the target for the controls controlling the configuration 180 // 181 // Preconditions: 182 // 183 // Parameters: area, not used 184 // 185 // Postconditions: 186 // 187 // Returns: 188 // --------------------------------------------------------------- 189 void 190 SGIView::AllAttached() 191 { 192 fCompressionMF->Menu()->SetTargetForItems(this); 193 } 194 195 // --------------------------------------------------------------- 196 // AttachedToWindow 197 // 198 // hack to make the window recognize our size 199 // 200 // Preconditions: 201 // 202 // Parameters: area, not used 203 // 204 // Postconditions: 205 // 206 // Returns: 207 // --------------------------------------------------------------- 208 void 209 SGIView::AttachedToWindow() 210 { 211 // Hack for DataTranslations which doesn't resize visible area to requested by view 212 // which makes some parts of bigger than usual translationviews out of visible area 213 // so if it was loaded to DataTranslations resize window if needed 214 BWindow *window = Window(); 215 if (!strcmp(window->Name(), "DataTranslations")) { 216 BView *view = Parent(); 217 if (view) { 218 BRect frame = view->Frame(); 219 float x, y; 220 GetPreferredSize(&x, &y); 221 if (frame.Width() < x || (frame.Height() - 48) < y) { 222 x -= frame.Width(); 223 y -= frame.Height() - 48; 224 if (x < 0) x = 0; 225 if (y < 0) y = 0; 226 227 // DataTranslations has main view called "Background" 228 // change it's resizing mode so it will always resize with window 229 // also make sure view will be redrawed after resize 230 view = window->FindView("Background"); 231 if (view) { 232 view->SetResizingMode(B_FOLLOW_ALL); 233 view->SetFlags(B_FULL_UPDATE_ON_RESIZE); 234 } 235 236 // The same with "Info..." button, except redrawing, which isn't needed 237 view = window->FindView("Info…"); 238 if (view) 239 view->SetResizingMode(B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM); 240 241 window->ResizeBy( x, y); 242 } 243 } 244 } 245 } 246 247 // --------------------------------------------------------------- 248 // Draw 249 // 250 // Draws information about the SGITranslator to this view. 251 // 252 // Preconditions: 253 // 254 // Parameters: area, not used 255 // 256 // Postconditions: 257 // 258 // Returns: 259 // --------------------------------------------------------------- 260 void 261 SGIView::Draw(BRect area) 262 { 263 SetFont(be_bold_font); 264 font_height fh; 265 GetFontHeight(&fh); 266 float xbold, ybold; 267 xbold = fh.descent + 1; 268 ybold = fh.ascent + fh.descent * 2 + fh.leading; 269 270 const char* text = "OpenBeOS SGI Image Translator"; 271 DrawString(text, BPoint(xbold, ybold)); 272 273 SetFont(be_plain_font); 274 font_height plainh; 275 GetFontHeight(&plainh); 276 float yplain; 277 yplain = plainh.ascent + plainh.descent * 2 + plainh.leading; 278 279 char detail[100]; 280 sprintf(detail, "Version %d.%d.%d %s", 281 static_cast<int>(B_TRANSLATION_MAJOR_VERSION(SGI_TRANSLATOR_VERSION)), 282 static_cast<int>(B_TRANSLATION_MINOR_VERSION(SGI_TRANSLATOR_VERSION)), 283 static_cast<int>(B_TRANSLATION_REVISION_VERSION(SGI_TRANSLATOR_VERSION)), 284 __DATE__); 285 DrawString(detail, BPoint(xbold, yplain + ybold)); 286 287 BPoint offset = fCompressionMF->Frame().LeftBottom(); 288 offset.x += xbold; 289 offset.y += 2 * ybold; 290 291 text = "written by:"; 292 DrawString(text, offset); 293 offset.y += ybold; 294 295 DrawString(author, offset); 296 offset.y += 2 * ybold; 297 298 text = "based on GIMP SGI plugin v1.5:"; 299 DrawString(text, offset); 300 offset.y += ybold; 301 302 DrawString(kSGICopyright, offset); 303 } 304 305 // --------------------------------------------------------------- 306 // Draw 307 // 308 // calculated the preferred size of this view 309 // 310 // Preconditions: 311 // 312 // Parameters: width and height 313 // 314 // Postconditions: 315 // 316 // Returns: in width and height, the preferred size... 317 // --------------------------------------------------------------- 318 void 319 SGIView::GetPreferredSize(float* width, float* height) 320 { 321 *width = fCompressionMF->Bounds().Width(); 322 // look at the two biggest strings 323 float width1 = StringWidth(kSGICopyright) + 15.0; 324 if (*width < width1) 325 *width = width1; 326 float width2 = be_plain_font->StringWidth(author) + 15.0; 327 if (*width < width2) 328 *width = width2; 329 330 font_height fh; 331 be_bold_font->GetHeight(&fh); 332 float ybold = fh.ascent + fh.descent * 2 + fh.leading; 333 334 *height = fCompressionMF->Bounds().bottom + 7 * ybold; 335 } 336