xref: /haiku/src/add-ons/translators/png/PNGView.cpp (revision 93aeb8c3bc3f13cb1f282e3e749258a23790d947)
1 /*****************************************************************************/
2 // PNGView
3 // Written by Michael Wilber, OBOS Translation Kit Team
4 //
5 // PNGView.cpp
6 //
7 // This BView based object displays information about the PNGTranslator.
8 //
9 //
10 // Copyright (c) 2003 OpenBeOS Project
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining a
13 // copy of this software and associated documentation files (the "Software"),
14 // to deal in the Software without restriction, including without limitation
15 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 // and/or sell copies of the Software, and to permit persons to whom the
17 // Software is furnished to do so, subject to the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be included
20 // in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 // DEALINGS IN THE SOFTWARE.
29 /*****************************************************************************/
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <png.h>
34 #include <Alert.h>
35 #include "PNGView.h"
36 #include "PNGTranslator.h"
37 
38 BMessage *
39 interlace_msg(int option)
40 {
41 	BMessage *pmsg = new BMessage(M_PNG_SET_INTERLACE);
42 	pmsg->AddInt32(PNG_SETTING_INTERLACE, option);
43 	return pmsg;
44 }
45 
46 // ---------------------------------------------------------------
47 // Constructor
48 //
49 // Sets up the view settings
50 //
51 // Preconditions:
52 //
53 // Parameters:
54 //
55 // Postconditions:
56 //
57 // Returns:
58 // ---------------------------------------------------------------
59 PNGView::PNGView(const BRect &frame, const char *name,
60 	uint32 resize, uint32 flags, TranslatorSettings *settings)
61 	:	BView(frame, name, resize, flags)
62 {
63 	fSettings = settings;
64 
65 	SetViewColor(220,220,220,0);
66 
67 	// setup PNG interlace options menu
68 	fpmnuInterlace = new BPopUpMenu("Interlace Option");
69 	BMenuItem *pitmNone, *pitmAdam7;
70 	pitmNone = new BMenuItem("None", interlace_msg(PNG_INTERLACE_NONE));
71 	pitmAdam7 = new BMenuItem("Adam7", interlace_msg(PNG_INTERLACE_ADAM7));
72 	fpmnuInterlace->AddItem(pitmNone);
73 	fpmnuInterlace->AddItem(pitmAdam7);
74 
75 	fpfldInterlace = new BMenuField(BRect(20, 50, 200, 70),
76 		"PNG Interlace Menu", "Interlacing Type", fpmnuInterlace);
77 	fpfldInterlace->SetViewColor(ViewColor());
78 	AddChild(fpfldInterlace);
79 
80 	// set Interlace option to show the current configuration
81 	if (fSettings->SetGetInt32(PNG_SETTING_INTERLACE) == PNG_INTERLACE_NONE)
82 		pitmNone->SetMarked(true);
83 	else
84 		pitmAdam7->SetMarked(true);
85 }
86 
87 // ---------------------------------------------------------------
88 // Destructor
89 //
90 // Releases the PNGTranslator settings
91 //
92 // Preconditions:
93 //
94 // Parameters:
95 //
96 // Postconditions:
97 //
98 // Returns:
99 // ---------------------------------------------------------------
100 PNGView::~PNGView()
101 {
102 	fSettings->Release();
103 }
104 
105 void
106 PNGView::AllAttached()
107 {
108 	// Tell all controls that this view is the target for
109 	// messages from controls on this view
110 	BView::AllAttached();
111 	BMessenger msgr(this);
112 
113 	// set target for interlace options menu items
114 	for (int32 i = 0; i < fpmnuInterlace->CountItems(); i++) {
115 		BMenuItem *pitm = fpmnuInterlace->ItemAt(i);
116 		if (pitm)
117 			pitm->SetTarget(msgr);
118 	}
119 }
120 
121 // ---------------------------------------------------------------
122 // Draw
123 //
124 // Draws information about the PNGTranslator to this view.
125 //
126 // Preconditions:
127 //
128 // Parameters: area,	not used
129 //
130 // Postconditions:
131 //
132 // Returns:
133 // ---------------------------------------------------------------
134 void
135 PNGView::Draw(BRect area)
136 {
137 	SetFont(be_bold_font);
138 	font_height fh;
139 	GetFontHeight(&fh);
140 	float xbold, ybold;
141 	xbold = fh.descent + 1;
142 	ybold = fh.ascent + fh.descent * 2 + fh.leading;
143 
144 	char title[] = "OpenBeOS PNG Image Translator";
145 	DrawString(title, BPoint(xbold, ybold));
146 
147 	SetFont(be_plain_font);
148 	font_height plainh;
149 	GetFontHeight(&plainh);
150 	float yplain;
151 	yplain = plainh.ascent + plainh.descent * 2 + plainh.leading;
152 
153 	char detail[100];
154 	sprintf(detail, "Version %d.%d.%d %s",
155 		static_cast<int>(B_TRANSLATION_MAJOR_VERSION(PNG_TRANSLATOR_VERSION)),
156 		static_cast<int>(B_TRANSLATION_MINOR_VERSION(PNG_TRANSLATOR_VERSION)),
157 		static_cast<int>(B_TRANSLATION_REVISION_VERSION(PNG_TRANSLATOR_VERSION)),
158 		__DATE__);
159 	DrawString(detail, BPoint(xbold, yplain + ybold));
160 
161 	int32 lineno = 6;
162 	DrawString("PNG Library:", BPoint(xbold, yplain * lineno + ybold));
163 	lineno += 1;
164 
165 	const int32 kinfolen = 512;
166 	char libinfo[kinfolen] = { 0 };
167 	strncpy(libinfo, png_get_copyright(NULL), kinfolen - 1);
168 	char *tok = strtok(libinfo, "\n");
169 	while (tok) {
170 		DrawString(tok, BPoint(xbold, yplain * lineno + ybold));
171 		lineno++;
172 		tok = strtok(NULL, "\n");
173 	}
174 }
175 
176 void
177 PNGView::MessageReceived(BMessage *pmsg)
178 {
179 	if (pmsg->what == M_PNG_SET_INTERLACE) {
180 		// change setting for interlace option
181 		int32 option;
182 		if (pmsg->FindInt32(PNG_SETTING_INTERLACE, &option) == B_OK) {
183 			fSettings->SetGetInt32(PNG_SETTING_INTERLACE, &option);
184 			fSettings->SaveSettings();
185 		}
186 	} else
187 		BView::MessageReceived(pmsg);
188 }
189 
190