xref: /haiku/src/add-ons/translators/png/PNGView.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
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 
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 	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
35 
36 	BStringView *titleView  = new BStringView("title",
37 		B_TRANSLATE("PNG image translator"));
38 	titleView->SetFont(be_bold_font);
39 
40 	char version[256];
41 	snprintf(version, sizeof(version), B_TRANSLATE("Version %d.%d.%d, %s"),
42 		int(B_TRANSLATION_MAJOR_VERSION(PNG_TRANSLATOR_VERSION)),
43 		int(B_TRANSLATION_MINOR_VERSION(PNG_TRANSLATOR_VERSION)),
44 		int(B_TRANSLATION_REVISION_VERSION(PNG_TRANSLATOR_VERSION)),
45 		__DATE__);
46 	BStringView *versionView =  new BStringView("version", version);
47 
48 	BStringView *copyrightView =  new BStringView(
49 		"Copyright", B_UTF8_COPYRIGHT "2003-2018 Haiku Inc.");
50 
51 	// setup PNG interlace options
52 
53 	fInterlaceMenu = new BPopUpMenu(B_TRANSLATE("Interlace Option"));
54 	BMenuItem* item = new BMenuItem(B_TRANSLATE("None"),
55 		_InterlaceMessage(PNG_INTERLACE_NONE));
56 	if (fSettings->SetGetInt32(PNG_SETTING_INTERLACE) == PNG_INTERLACE_NONE)
57 		item->SetMarked(true);
58 	fInterlaceMenu->AddItem(item);
59 
60 	item = new BMenuItem("Adam7", _InterlaceMessage(PNG_INTERLACE_ADAM7));
61 	if (fSettings->SetGetInt32(PNG_SETTING_INTERLACE) == PNG_INTERLACE_ADAM7)
62 		item->SetMarked(true);
63 	fInterlaceMenu->AddItem(item);
64 
65 	BMenuField* menuField = new BMenuField(
66 		B_TRANSLATE("PNG Interlace Menu"),
67 		B_TRANSLATE("Interlacing type:"), fInterlaceMenu);
68 	menuField->SetDivider(menuField->StringWidth(menuField->Label()) + 7.0f);
69 	menuField->ResizeToPreferred();
70 
71 	BString pngCopyright = png_get_copyright(NULL);
72 	pngCopyright.ReplaceLast("\n", "");
73 	BStringView* pngCopyrightView = new BStringView(
74 		"PNG copyright", pngCopyright);
75 
76 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
77 		.SetInsets(B_USE_DEFAULT_SPACING)
78 		.Add(titleView)
79 		.Add(versionView)
80 		.Add(copyrightView)
81 		.AddGlue()
82 		.AddGroup(B_HORIZONTAL)
83 			.Add(menuField)
84 			.AddGlue()
85 			.End()
86 		.AddGlue()
87 		.Add(pngCopyrightView);
88 }
89 
90 
91 PNGView::~PNGView()
92 {
93 	fSettings->Release();
94 }
95 
96 
97 BMessage *
98 PNGView::_InterlaceMessage(int32 kind)
99 {
100 	BMessage* message = new BMessage(M_PNG_SET_INTERLACE);
101 	message->AddInt32(PNG_SETTING_INTERLACE, kind);
102 
103 	return message;
104 }
105 
106 
107 void
108 PNGView::AttachedToWindow()
109 {
110 	BView::AttachedToWindow();
111 
112 	// set target for interlace options menu items
113 	fInterlaceMenu->SetTargetForItems(this);
114 }
115 
116 
117 void
118 PNGView::MessageReceived(BMessage *message)
119 {
120 	if (message->what == M_PNG_SET_INTERLACE) {
121 		// change setting for interlace option
122 		int32 option;
123 		if (message->FindInt32(PNG_SETTING_INTERLACE, &option) == B_OK) {
124 			fSettings->SetGetInt32(PNG_SETTING_INTERLACE, &option);
125 			fSettings->SaveSettings();
126 		}
127 	} else
128 		BView::MessageReceived(message);
129 }
130 
131