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