xref: /haiku/src/add-ons/translators/png/PNGView.cpp (revision 6aff37d1c79e20748c683ae224bd629f88a5b0be)
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 <LayoutBuilder.h>
17 #include <MenuField.h>
18 #include <MenuItem.h>
19 #include <PopUpMenu.h>
20 #include <StringView.h>
21 #include <TextView.h>
22 
23 #include <stdio.h>
24 #define PNG_NO_PEDANTIC_WARNINGS
25 #include <png.h>
26 
27 #undef B_TRANSLATION_CONTEXT
28 #define B_TRANSLATION_CONTEXT "PNGTranslator"
29 
30 PNGView::PNGView(const BRect &frame, const char *name, uint32 resizeMode,
31 		uint32 flags, TranslatorSettings *settings)
32 	: BView(frame, name, resizeMode, flags | B_FRAME_EVENTS)
33 {
34 	fSettings = settings;
35 	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
36 
37 	BStringView *titleView  = new BStringView("title",
38 		B_TRANSLATE("PNG image translator"));
39 	titleView->SetFont(be_bold_font);
40 
41 	char version[256];
42 	snprintf(version, sizeof(version), B_TRANSLATE("Version %d.%d.%d, %s"),
43 		int(B_TRANSLATION_MAJOR_VERSION(PNG_TRANSLATOR_VERSION)),
44 		int(B_TRANSLATION_MINOR_VERSION(PNG_TRANSLATOR_VERSION)),
45 		int(B_TRANSLATION_REVISION_VERSION(PNG_TRANSLATOR_VERSION)),
46 		__DATE__);
47 	BStringView *versionView =  new BStringView("version", version);
48 
49 	BStringView *copyrightView =  new BStringView(
50 		"Copyright", B_UTF8_COPYRIGHT "2003-2006 Haiku Inc.");
51 
52 	// setup PNG interlace options
53 
54 	fInterlaceMenu = new BPopUpMenu(B_TRANSLATE("Interlace Option"));
55 	BMenuItem* item = new BMenuItem(B_TRANSLATE("None"),
56 		_InterlaceMessage(PNG_INTERLACE_NONE));
57 	if (fSettings->SetGetInt32(PNG_SETTING_INTERLACE) == PNG_INTERLACE_NONE)
58 		item->SetMarked(true);
59 	fInterlaceMenu->AddItem(item);
60 
61 	item = new BMenuItem("Adam7", _InterlaceMessage(PNG_INTERLACE_ADAM7));
62 	if (fSettings->SetGetInt32(PNG_SETTING_INTERLACE) == PNG_INTERLACE_ADAM7)
63 		item->SetMarked(true);
64 	fInterlaceMenu->AddItem(item);
65 
66 	BMenuField* menuField = new BMenuField(
67 		B_TRANSLATE("PNG Interlace Menu"),
68 		B_TRANSLATE("Interlacing type:"), fInterlaceMenu);
69 	menuField->SetDivider(menuField->StringWidth(menuField->Label()) + 7.0f);
70 	menuField->ResizeToPreferred();
71 
72 	fCopyrightView = new BTextView("PNG copyright");
73 	fCopyrightView->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
74 	fCopyrightView->SetLowColor(fCopyrightView->ViewColor());
75 	fCopyrightView->MakeEditable(false);
76 	fCopyrightView->SetWordWrap(false);
77 	fCopyrightView->MakeResizable(true);
78 	fCopyrightView->SetText(png_get_copyright(NULL));
79 
80 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
81 		.SetInsets(B_USE_DEFAULT_SPACING)
82 		.Add(titleView)
83 		.Add(versionView)
84 		.Add(copyrightView)
85 		.AddGlue()
86 		.AddGroup(B_HORIZONTAL)
87 			.Add(menuField)
88 			.AddGlue()
89 			.End()
90 		.AddGlue()
91 		.Add(fCopyrightView);
92 
93 	BFont font;
94 	GetFont(&font);
95 	SetExplicitPreferredSize(BSize((font.Size() * 390) / 12,
96 		(font.Size() * 180) / 12));
97 
98 	// TODO: remove this workaround for ticket #4217
99 	fCopyrightView->SetExplicitPreferredSize(
100 		BSize(fCopyrightView->LineWidth(4), fCopyrightView->TextHeight(0, 80)));
101 	fCopyrightView->SetExplicitMaxSize(fCopyrightView->ExplicitPreferredSize());
102 	fCopyrightView->SetExplicitMinSize(fCopyrightView->ExplicitPreferredSize());
103 }
104 
105 
106 PNGView::~PNGView()
107 {
108 	fSettings->Release();
109 }
110 
111 
112 BMessage *
113 PNGView::_InterlaceMessage(int32 kind)
114 {
115 	BMessage* message = new BMessage(M_PNG_SET_INTERLACE);
116 	message->AddInt32(PNG_SETTING_INTERLACE, kind);
117 
118 	return message;
119 }
120 
121 
122 void
123 PNGView::AttachedToWindow()
124 {
125 	BView::AttachedToWindow();
126 
127 	// set target for interlace options menu items
128 	fInterlaceMenu->SetTargetForItems(this);
129 }
130 
131 
132 void
133 PNGView::FrameResized(float width, float height)
134 {
135 	// This works around a flaw of BTextView
136 	fCopyrightView->SetTextRect(fCopyrightView->Bounds());
137 }
138 
139 
140 void
141 PNGView::MessageReceived(BMessage *message)
142 {
143 	if (message->what == M_PNG_SET_INTERLACE) {
144 		// change setting for interlace option
145 		int32 option;
146 		if (message->FindInt32(PNG_SETTING_INTERLACE, &option) == B_OK) {
147 			fSettings->SetGetInt32(PNG_SETTING_INTERLACE, &option);
148 			fSettings->SaveSettings();
149 		}
150 	} else
151 		BView::MessageReceived(message);
152 }
153 
154