1 /* 2 * Copyright 2006-2009, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Stephan Aßmus <superstippi@gmx.de> 7 */ 8 9 #include "WonderBrushView.h" 10 11 #include <stdio.h> 12 #include <string.h> 13 14 #include <Catalog.h> 15 #include <MenuBar.h> 16 #include <MenuField.h> 17 #include <MenuItem.h> 18 #include <PopUpMenu.h> 19 #include <Window.h> 20 21 #include "WonderBrushImage.h" 22 #include "WonderBrushTranslator.h" 23 24 25 #undef B_TRANSLATION_CONTEXT 26 #define B_TRANSLATION_CONTEXT "WonderBrushView" 27 28 29 const char* kAuthor = "Stephan Aßmus, <superstippi@gmx.de>"; 30 const char* kWBICopyright = "Copyright "B_UTF8_COPYRIGHT" 2006 Haiku Inc."; 31 32 33 void 34 add_menu_item(BMenu* menu, 35 uint32 compression, 36 const char* label, 37 uint32 currentCompression) 38 { 39 BMessage* message = new BMessage(WonderBrushView::MSG_COMPRESSION_CHANGED); 40 message->AddInt32("value", compression); 41 BMenuItem* item = new BMenuItem(label, message); 42 item->SetMarked(currentCompression == compression); 43 menu->AddItem(item); 44 } 45 46 47 WonderBrushView::WonderBrushView(const BRect &frame, const char *name, 48 uint32 resize, uint32 flags, TranslatorSettings *settings) 49 : BView(frame, name, resize, flags), 50 fSettings(settings) 51 { 52 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 53 SetLowColor(ViewColor()); 54 55 // figure out where the text ends 56 font_height fh; 57 be_bold_font->GetHeight(&fh); 58 float xbold, ybold; 59 xbold = fh.descent + 1; 60 ybold = fh.ascent + fh.descent * 2 + fh.leading; 61 62 font_height plainh; 63 be_plain_font->GetHeight(&plainh); 64 float yplain; 65 yplain = plainh.ascent + plainh.descent * 2 + plainh.leading; 66 67 ResizeToPreferred(); 68 } 69 70 71 WonderBrushView::~WonderBrushView() 72 { 73 fSettings->Release(); 74 } 75 76 77 void 78 WonderBrushView::MessageReceived(BMessage* message) 79 { 80 switch (message->what) { 81 default: 82 BView::MessageReceived(message); 83 } 84 } 85 86 87 void 88 WonderBrushView::AttachedToWindow() 89 { 90 // Hack for DataTranslations which doesn't resize visible area to requested by view 91 // which makes some parts of bigger than usual translationviews out of visible area 92 // so if it was loaded to DataTranslations resize window if needed 93 BWindow *window = Window(); 94 if (!strcmp(window->Name(), "DataTranslations")) { 95 BView *view = Parent(); 96 if (view) { 97 BRect frame = view->Frame(); 98 float x, y; 99 GetPreferredSize(&x, &y); 100 if (frame.Width() < x || (frame.Height() - 48) < y) { 101 x -= frame.Width(); 102 y -= frame.Height() - 48; 103 if (x < 0) x = 0; 104 if (y < 0) y = 0; 105 106 // DataTranslations has main view called "Background" 107 // change it's resizing mode so it will always resize with window 108 // also make sure view will be redrawed after resize 109 view = window->FindView("Background"); 110 if (view) { 111 view->SetResizingMode(B_FOLLOW_ALL); 112 view->SetFlags(B_FULL_UPDATE_ON_RESIZE); 113 } 114 115 // The same with "Info..." button, except redrawing, which isn't needed 116 view = window->FindView("Info" B_UTF8_ELLIPSIS); 117 if (view) 118 view->SetResizingMode(B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM); 119 120 window->ResizeBy( x, y); 121 } 122 } 123 } 124 } 125 126 127 void 128 WonderBrushView::Draw(BRect area) 129 { 130 SetFont(be_bold_font); 131 font_height fh; 132 GetFontHeight(&fh); 133 float xbold = fh.descent + 1; 134 float ybold = fh.ascent + fh.descent * 2 + fh.leading; 135 136 BPoint offset(xbold, ybold); 137 138 const char* text = B_TRANSLATE("WonderBrush image translator"); 139 DrawString(text, offset); 140 141 SetFont(be_plain_font); 142 font_height plainh; 143 GetFontHeight(&plainh); 144 float yplain = plainh.ascent + plainh.descent * 2 + plainh.leading; 145 146 offset.y += yplain; 147 148 char detail[100]; 149 sprintf(detail, B_TRANSLATE("Version %d.%d.%d %s"), 150 static_cast<int>(B_TRANSLATION_MAJOR_VERSION(WBI_TRANSLATOR_VERSION)), 151 static_cast<int>(B_TRANSLATION_MINOR_VERSION(WBI_TRANSLATOR_VERSION)), 152 static_cast<int>(B_TRANSLATION_REVISION_VERSION( 153 WBI_TRANSLATOR_VERSION)), __DATE__); 154 DrawString(detail, offset); 155 156 offset.y += 2 * ybold; 157 158 text = B_TRANSLATE("written by:"); 159 DrawString(text, offset); 160 offset.y += ybold; 161 162 DrawString(kAuthor, offset); 163 offset.y += 2 * ybold; 164 165 DrawString(kWBICopyright, offset); 166 } 167 168 169 void 170 WonderBrushView::GetPreferredSize(float* width, float* height) 171 { 172 if (width) { 173 // look at the two widest strings 174 float width1 = StringWidth(kWBICopyright) + 15.0; 175 float width2 = be_plain_font->StringWidth(kAuthor) + 15.0; 176 177 *width = max_c(width1, width2); 178 } 179 180 if (height) { 181 // take the height of the bold system font and 182 // the number of lines of text we render 183 font_height fh; 184 be_bold_font->GetHeight(&fh); 185 float ybold = fh.ascent + fh.descent * 2 + fh.leading; 186 187 *height = 7 * ybold; 188 } 189 } 190