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