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-2018 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 94 95 PNGView::~PNGView() 96 { 97 fSettings->Release(); 98 } 99 100 101 BMessage * 102 PNGView::_InterlaceMessage(int32 kind) 103 { 104 BMessage* message = new BMessage(M_PNG_SET_INTERLACE); 105 message->AddInt32(PNG_SETTING_INTERLACE, kind); 106 107 return message; 108 } 109 110 111 void 112 PNGView::AttachedToWindow() 113 { 114 BView::AttachedToWindow(); 115 116 // set target for interlace options menu items 117 fInterlaceMenu->SetTargetForItems(this); 118 } 119 120 121 void 122 PNGView::FrameResized(float width, float height) 123 { 124 // This works around a flaw of BTextView 125 fCopyrightView->SetTextRect(fCopyrightView->Bounds()); 126 } 127 128 129 void 130 PNGView::MessageReceived(BMessage *message) 131 { 132 if (message->what == M_PNG_SET_INTERLACE) { 133 // change setting for interlace option 134 int32 option; 135 if (message->FindInt32(PNG_SETTING_INTERLACE, &option) == B_OK) { 136 fSettings->SetGetInt32(PNG_SETTING_INTERLACE, &option); 137 fSettings->SaveSettings(); 138 } 139 } else 140 BView::MessageReceived(message); 141 } 142 143