xref: /haiku/src/add-ons/translators/png/PNGView.cpp (revision 125b262675217084e0c59014b4a98f724f1c4fb3)
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("PNG Interlace Menu",
66 		B_TRANSLATE("Interlacing type:"), fInterlaceMenu);
67 	menuField->SetDivider(menuField->StringWidth(menuField->Label()) + 7.0f);
68 	menuField->ResizeToPreferred();
69 
70 	BString pngCopyright = png_get_copyright(NULL);
71 	pngCopyright.ReplaceLast("\n", "");
72 	BStringView* pngCopyrightView = new BStringView(
73 		"PNG copyright", pngCopyright);
74 
75 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
76 		.SetInsets(B_USE_DEFAULT_SPACING)
77 		.Add(titleView)
78 		.Add(versionView)
79 		.Add(copyrightView)
80 		.AddGlue()
81 		.AddGroup(B_HORIZONTAL)
82 			.Add(menuField)
83 			.AddGlue()
84 			.End()
85 		.AddGlue()
86 		.Add(pngCopyrightView);
87 }
88 
89 
90 PNGView::~PNGView()
91 {
92 	fSettings->Release();
93 }
94 
95 
96 BMessage *
97 PNGView::_InterlaceMessage(int32 kind)
98 {
99 	BMessage* message = new BMessage(M_PNG_SET_INTERLACE);
100 	message->AddInt32(PNG_SETTING_INTERLACE, kind);
101 
102 	return message;
103 }
104 
105 
106 void
107 PNGView::AttachedToWindow()
108 {
109 	BView::AttachedToWindow();
110 
111 	// set target for interlace options menu items
112 	fInterlaceMenu->SetTargetForItems(this);
113 }
114 
115 
116 void
117 PNGView::MessageReceived(BMessage *message)
118 {
119 	if (message->what == M_PNG_SET_INTERLACE) {
120 		// change setting for interlace option
121 		int32 option;
122 		if (message->FindInt32(PNG_SETTING_INTERLACE, &option) == B_OK) {
123 			fSettings->SetGetInt32(PNG_SETTING_INTERLACE, &option);
124 			fSettings->SaveSettings();
125 		}
126 	} else
127 		BView::MessageReceived(message);
128 }
129 
130