1 /* 2 * Copyright 1999-2010, Be Incorporated. All Rights Reserved. 3 * This file may be used under the terms of the Be Sample Code License. 4 * 5 * OverlayImage is based on the code presented in this article: 6 * http://www.haiku-os.org/documents/dev/replishow_a_replicable_image_viewer 7 * 8 * Authors: 9 * Seth Flexman 10 * Hartmuth Reh 11 * Humdinger <humdingerb@gmail.com> 12 */ 13 14 #include "OverlayView.h" 15 16 #include <Catalog.h> 17 #include <InterfaceDefs.h> 18 #include <Locale.h> 19 #include <String.h> 20 #include <TextView.h> 21 22 #undef B_TRANSLATE_CONTEXT 23 #define B_TRANSLATE_CONTEXT "Main view" 24 25 const float kDraggerSize = 7; 26 27 28 OverlayView::OverlayView(BRect frame) 29 : 30 BView(frame, "OverlayImage", B_FOLLOW_NONE, B_WILL_DRAW) 31 { 32 fBitmap = NULL; 33 fReplicated = false; 34 35 frame.left = frame.right - kDraggerSize; 36 frame.top = frame.bottom - kDraggerSize; 37 BDragger *dragger = new BDragger(frame, this, B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM); 38 AddChild(dragger); 39 40 SetViewColor(B_TRANSPARENT_COLOR); 41 42 fText = new BTextView(Bounds(), "bgView", Bounds(), B_FOLLOW_ALL, B_WILL_DRAW); 43 fText->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 44 AddChild(fText); 45 BString text; 46 text << B_TRANSLATE( 47 "Enable \"Show replicants\" in Deskbar.\n" 48 "Drag & drop an image.\n" 49 "Drag the replicant to the Desktop."); 50 fText->SetText(text); 51 fText->SetAlignment(B_ALIGN_CENTER); 52 fText->MakeSelectable(false); 53 fText->MoveBy(0, (Bounds().bottom - fText->TextRect().bottom) / 2); 54 } 55 56 57 OverlayView::OverlayView(BMessage *archive) 58 : 59 BView(archive) 60 { 61 fReplicated = true; 62 fBitmap = new BBitmap(archive); 63 } 64 65 66 OverlayView::~OverlayView() 67 { 68 delete fBitmap; 69 } 70 71 72 void 73 OverlayView::Draw(BRect) 74 { 75 SetDrawingMode(B_OP_ALPHA); 76 SetViewColor(B_TRANSPARENT_COLOR); 77 78 if (fBitmap) 79 DrawBitmap(fBitmap, B_ORIGIN); 80 } 81 82 83 void 84 OverlayView::MessageReceived(BMessage *msg) 85 { 86 switch (msg->what) { 87 case B_SIMPLE_DATA: 88 { 89 if (fReplicated) 90 break; 91 92 entry_ref ref; 93 msg->FindRef("refs", &ref); 94 BEntry entry(&ref); 95 BPath path(&entry); 96 97 delete fBitmap; 98 fBitmap = BTranslationUtils::GetBitmap(path.Path()); 99 100 if (fBitmap != NULL) { 101 if (fText != NULL) { 102 RemoveChild(fText); 103 fText = NULL; 104 } 105 106 BRect rect = fBitmap->Bounds(); 107 if (!fReplicated) { 108 Window()->ResizeTo(rect.right, rect.bottom); 109 Window()->Activate(true); 110 } 111 ResizeTo(rect.right, rect.bottom); 112 Invalidate(); 113 } 114 break; 115 } 116 case B_ABOUT_REQUESTED: 117 OverlayAboutRequested(); 118 break; 119 120 default: 121 BView::MessageReceived(msg); 122 break; 123 } 124 } 125 126 127 BArchivable *OverlayView::Instantiate(BMessage *data) 128 { 129 return new OverlayView(data); 130 } 131 132 133 status_t 134 OverlayView::Archive(BMessage *archive, bool deep) const 135 { 136 BView::Archive(archive, deep); 137 138 archive->AddString("add_on", "application/x-vnd.Haiku-OverlayImage"); 139 archive->AddString("class", "OverlayImage"); 140 141 if (fBitmap) { 142 fBitmap->Lock(); 143 fBitmap->Archive(archive); 144 fBitmap->Unlock(); 145 } 146 //archive->PrintToStream(); 147 148 return B_OK; 149 } 150 151 152 void 153 OverlayView::OverlayAboutRequested() 154 { 155 BAlert *alert = new BAlert("about", 156 "OverlayImage\n" 157 "Copyright 1999-2010\n\n\t" 158 "originally by Seth Flaxman\n\t" 159 "modified by Hartmuth Reh\n\t" 160 "further modified by Humdinger\n", 161 "OK"); 162 163 BTextView *view = alert->TextView(); 164 BFont font; 165 view->SetStylable(true); 166 view->GetFont(&font); 167 font.SetSize(font.Size() + 7.0f); 168 font.SetFace(B_BOLD_FACE); 169 view->SetFontAndColor(0, 12, &font); 170 171 alert->Go(); 172 } 173