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