xref: /haiku/src/add-ons/translators/wonderbrush/WonderBrushView.cpp (revision 22440f4105cafc95cc1d49f9bc65bb395c527d86)
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 	ResizeToPreferred();
56 }
57 
58 
59 WonderBrushView::~WonderBrushView()
60 {
61 	fSettings->Release();
62 }
63 
64 
65 void
66 WonderBrushView::MessageReceived(BMessage* message)
67 {
68 	switch (message->what) {
69 		default:
70 			BView::MessageReceived(message);
71 	}
72 }
73 
74 
75 void
76 WonderBrushView::AttachedToWindow()
77 {
78 	// Hack for DataTranslations which doesn't resize visible area to requested by view
79 	// which makes some parts of bigger than usual translationviews out of visible area
80 	// so if it was loaded to DataTranslations resize window if needed
81 	BWindow *window = Window();
82 	if (!strcmp(window->Name(), "DataTranslations")) {
83 		BView *view = Parent();
84 		if (view) {
85 			BRect frame = view->Frame();
86 			float x, y;
87 			GetPreferredSize(&x, &y);
88 			if (frame.Width() < x || (frame.Height() - 48) < y) {
89 				x -= frame.Width();
90 				y -= frame.Height() - 48;
91 				if (x < 0) x = 0;
92 				if (y < 0) y = 0;
93 
94 				// DataTranslations has main view called "Background"
95 				// change it's resizing mode so it will always resize with window
96 				// also make sure view will be redrawed after resize
97 				view = window->FindView("Background");
98 				if (view) {
99 					view->SetResizingMode(B_FOLLOW_ALL);
100 					view->SetFlags(B_FULL_UPDATE_ON_RESIZE);
101 				}
102 
103 				// The same with "Info..." button, except redrawing, which isn't needed
104 				view = window->FindView("Info" B_UTF8_ELLIPSIS);
105 				if (view)
106 					view->SetResizingMode(B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
107 
108 				window->ResizeBy( x, y);
109 			}
110 		}
111 	}
112 }
113 
114 
115 void
116 WonderBrushView::Draw(BRect area)
117 {
118 	SetFont(be_bold_font);
119 	font_height fh;
120 	GetFontHeight(&fh);
121 	float xbold = fh.descent + 1;
122 	float ybold = fh.ascent + fh.descent * 2 + fh.leading;
123 
124 	BPoint offset(xbold, ybold);
125 
126 	const char* text = B_TRANSLATE("WonderBrush image translator");
127 	DrawString(text, offset);
128 
129 	SetFont(be_plain_font);
130 	font_height plainh;
131 	GetFontHeight(&plainh);
132 	float yplain = plainh.ascent + plainh.descent * 2 + plainh.leading;
133 
134 	offset.y += yplain;
135 
136 	char detail[100];
137 	sprintf(detail, B_TRANSLATE("Version %d.%d.%d %s"),
138 		static_cast<int>(B_TRANSLATION_MAJOR_VERSION(WBI_TRANSLATOR_VERSION)),
139 		static_cast<int>(B_TRANSLATION_MINOR_VERSION(WBI_TRANSLATOR_VERSION)),
140 		static_cast<int>(B_TRANSLATION_REVISION_VERSION(
141 			WBI_TRANSLATOR_VERSION)), __DATE__);
142 	DrawString(detail, offset);
143 
144 	offset.y += 2 * ybold;
145 
146 	text = B_TRANSLATE("written by:");
147 	DrawString(text, offset);
148 	offset.y += ybold;
149 
150 	DrawString(kAuthor, offset);
151 	offset.y += 2 * ybold;
152 
153 	DrawString(kWBICopyright, offset);
154 }
155 
156 
157 void
158 WonderBrushView::GetPreferredSize(float* width, float* height)
159 {
160 	if (width) {
161 		// look at the two widest strings
162 		float width1 = StringWidth(kWBICopyright) + 15.0;
163 		float width2 = be_plain_font->StringWidth(kAuthor) + 15.0;
164 
165 		*width = max_c(width1, width2);
166 	}
167 
168 	if (height) {
169 		// take the height of the bold system font and
170 		// the number of lines of text we render
171 		font_height fh;
172 		be_bold_font->GetHeight(&fh);
173 		float ybold = fh.ascent + fh.descent * 2 + fh.leading;
174 
175 		*height = 7 * ybold;
176 	}
177 }
178