xref: /haiku/src/add-ons/translators/png/PNGView.cpp (revision 23d878482ed22e55dad6d1fca1df7bea42eb157c)
1 /*
2  * Copyright 2003-2009, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Michael Wilber
7  *		Axel Dörfler, axeld@pinc-software.de
8  */
9 
10 
11 #include "PNGView.h"
12 #include "PNGTranslator.h"
13 
14 #include <Alert.h>
15 #include <Catalog.h>
16 #include <MenuField.h>
17 #include <MenuItem.h>
18 #include <PopUpMenu.h>
19 #include <StringView.h>
20 #include <TextView.h>
21 
22 #include <stdio.h>
23 #define PNG_NO_PEDANTIC_WARNINGS
24 #include <png.h>
25 
26 #undef B_TRANSLATION_CONTEXT
27 #define B_TRANSLATION_CONTEXT "PNGTranslator"
28 
29 PNGView::PNGView(const BRect &frame, const char *name, uint32 resizeMode,
30 		uint32 flags, TranslatorSettings *settings)
31 	: BView(frame, name, resizeMode, flags | B_FRAME_EVENTS)
32 {
33 	fSettings = settings;
34 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
35 
36 	font_height fontHeight;
37 	be_bold_font->GetHeight(&fontHeight);
38 	float height = fontHeight.descent + fontHeight.ascent + fontHeight.leading;
39 
40 	BRect rect(10, 10, 200, 10 + height);
41 	BStringView *stringView = new BStringView(rect, "title",
42 		B_TRANSLATE("PNG image translator"));
43 	stringView->SetFont(be_bold_font);
44 	stringView->ResizeToPreferred();
45 	AddChild(stringView);
46 
47 	float maxWidth = stringView->Bounds().Width();
48 
49 	rect.OffsetBy(0, height + 10);
50 	char version[256];
51 	snprintf(version, sizeof(version), B_TRANSLATE("Version %d.%d.%d, %s"),
52 		int(B_TRANSLATION_MAJOR_VERSION(PNG_TRANSLATOR_VERSION)),
53 		int(B_TRANSLATION_MINOR_VERSION(PNG_TRANSLATOR_VERSION)),
54 		int(B_TRANSLATION_REVISION_VERSION(PNG_TRANSLATOR_VERSION)),
55 		__DATE__);
56 	stringView = new BStringView(rect, "version", version);
57 	stringView->ResizeToPreferred();
58 	AddChild(stringView);
59 
60 	if (stringView->Bounds().Width() > maxWidth)
61 		maxWidth = stringView->Bounds().Width();
62 
63 	GetFontHeight(&fontHeight);
64 	height = fontHeight.descent + fontHeight.ascent + fontHeight.leading;
65 
66 	rect.OffsetBy(0, height + 5);
67 	stringView = new BStringView(rect,
68 		"Copyright", B_UTF8_COPYRIGHT "2003-2006 Haiku Inc.");
69 	stringView->ResizeToPreferred();
70 	AddChild(stringView);
71 
72 	// setup PNG interlace options
73 
74 	fInterlaceMenu = new BPopUpMenu(B_TRANSLATE("Interlace Option"));
75 	BMenuItem* item = new BMenuItem(B_TRANSLATE("None"),
76 		_InterlaceMessage(PNG_INTERLACE_NONE));
77 	if (fSettings->SetGetInt32(PNG_SETTING_INTERLACE) == PNG_INTERLACE_NONE)
78 		item->SetMarked(true);
79 	fInterlaceMenu->AddItem(item);
80 
81 	item = new BMenuItem("Adam7", _InterlaceMessage(PNG_INTERLACE_ADAM7));
82 	if (fSettings->SetGetInt32(PNG_SETTING_INTERLACE) == PNG_INTERLACE_ADAM7)
83 		item->SetMarked(true);
84 	fInterlaceMenu->AddItem(item);
85 
86 	rect.OffsetBy(0, stringView->Frame().Height() + 20.0f);
87 	BMenuField* menuField = new BMenuField(rect,
88 		B_TRANSLATE("PNG Interlace Menu"),
89 		B_TRANSLATE("Interlacing type:"), fInterlaceMenu);
90 	menuField->SetDivider(menuField->StringWidth(menuField->Label()) + 7.0f);
91 	menuField->ResizeToPreferred();
92 	AddChild(menuField);
93 
94 	rect.OffsetBy(0, height + 15);
95 	rect.right = Bounds().right;
96 	rect.bottom = Bounds().bottom;
97 	fCopyrightView = new BTextView(rect, "PNG copyright",
98 		rect.OffsetToCopy(B_ORIGIN),
99 		B_FOLLOW_ALL, B_WILL_DRAW | B_FRAME_EVENTS);
100 	fCopyrightView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
101 	fCopyrightView->SetLowColor(fCopyrightView->ViewColor());
102 	fCopyrightView->MakeEditable(false);
103 	fCopyrightView->SetText(png_get_copyright(NULL));
104 
105 	BFont font;
106 	font.SetSize(font.Size() * 0.8);
107 	fCopyrightView->SetFontAndColor(&font, B_FONT_SIZE, NULL);
108 	AddChild(fCopyrightView);
109 
110 	if (maxWidth + 20 > Bounds().Width())
111 		ResizeTo(maxWidth + 20, Bounds().Height());
112 	if (Bounds().Height() < rect.top + stringView->Bounds().Height()
113 		* 3.0f + 8.0f)
114 		ResizeTo(Bounds().Width(), rect.top + height * 3.0f + 8.0f);
115 
116 	fCopyrightView->SetTextRect(fCopyrightView->Bounds());
117 }
118 
119 
120 PNGView::~PNGView()
121 {
122 	fSettings->Release();
123 }
124 
125 
126 BMessage *
127 PNGView::_InterlaceMessage(int32 kind)
128 {
129 	BMessage* message = new BMessage(M_PNG_SET_INTERLACE);
130 	message->AddInt32(PNG_SETTING_INTERLACE, kind);
131 
132 	return message;
133 }
134 
135 
136 void
137 PNGView::AttachedToWindow()
138 {
139 	BView::AttachedToWindow();
140 
141 	// set target for interlace options menu items
142 	fInterlaceMenu->SetTargetForItems(this);
143 }
144 
145 
146 void
147 PNGView::FrameResized(float width, float height)
148 {
149 	// This works around a flaw of BTextView
150 	fCopyrightView->SetTextRect(fCopyrightView->Bounds());
151 }
152 
153 
154 void
155 PNGView::MessageReceived(BMessage *message)
156 {
157 	if (message->what == M_PNG_SET_INTERLACE) {
158 		// change setting for interlace option
159 		int32 option;
160 		if (message->FindInt32(PNG_SETTING_INTERLACE, &option) == B_OK) {
161 			fSettings->SetGetInt32(PNG_SETTING_INTERLACE, &option);
162 			fSettings->SaveSettings();
163 		}
164 	} else
165 		BView::MessageReceived(message);
166 }
167 
168