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