vfs: check userland buffers before reading them.
[haiku.git] / src / apps / overlayimage / OverlayView.cpp
blob0e7c33d77b14b1e575ad17f54cf564c406ddea71
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.
5 * OverlayImage is based on the code presented in this article:
6 * http://www.haiku-os.org/documents/dev/replishow_a_replicable_image_viewer
8 * Authors:
9 * Seth Flexman
10 * Hartmuth Reh
11 * Humdinger <humdingerb@gmail.com>
14 #include "OverlayView.h"
16 #include <Catalog.h>
17 #include <InterfaceDefs.h>
18 #include <Locale.h>
19 #include <String.h>
20 #include <TextView.h>
23 #undef B_TRANSLATION_CONTEXT
24 #define B_TRANSLATION_CONTEXT "Main view"
26 const float kDraggerSize = 7;
29 OverlayView::OverlayView(BRect frame)
31 BView(frame, "OverlayImage", B_FOLLOW_NONE, B_WILL_DRAW)
33 fBitmap = NULL;
34 fReplicated = false;
36 frame.left = frame.right - kDraggerSize;
37 frame.top = frame.bottom - kDraggerSize;
38 BDragger *dragger = new BDragger(frame, this, B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
39 AddChild(dragger);
41 SetViewColor(B_TRANSPARENT_COLOR);
43 fText = new BTextView(Bounds(), "bgView", Bounds(), B_FOLLOW_ALL, B_WILL_DRAW);
44 fText->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
45 rgb_color color = ui_color(B_PANEL_TEXT_COLOR);
46 fText->SetFontAndColor(be_plain_font, B_FONT_ALL, &color);
47 AddChild(fText);
48 BString text;
49 text << B_TRANSLATE(
50 "Enable \"Show replicants\" in Deskbar.\n"
51 "Drag & drop an image.\n"
52 "Drag the replicant to the Desktop.");
53 fText->SetText(text);
54 fText->SetAlignment(B_ALIGN_CENTER);
55 fText->MakeEditable(false);
56 fText->MakeSelectable(false);
57 fText->MoveBy(0, (Bounds().bottom - fText->TextRect().bottom) / 2);
61 OverlayView::OverlayView(BMessage *archive)
63 BView(archive)
65 fReplicated = true;
66 fBitmap = new BBitmap(archive);
70 OverlayView::~OverlayView()
72 delete fBitmap;
76 void
77 OverlayView::Draw(BRect)
79 SetDrawingMode(B_OP_ALPHA);
80 SetViewColor(B_TRANSPARENT_COLOR);
82 if (fBitmap)
83 DrawBitmap(fBitmap, B_ORIGIN);
87 void
88 OverlayView::MessageReceived(BMessage *msg)
90 switch (msg->what) {
91 case B_SIMPLE_DATA:
93 if (fReplicated)
94 break;
96 entry_ref ref;
97 msg->FindRef("refs", &ref);
98 BEntry entry(&ref);
99 BPath path(&entry);
101 delete fBitmap;
102 fBitmap = BTranslationUtils::GetBitmap(path.Path());
104 if (fBitmap != NULL) {
105 if (fText != NULL) {
106 RemoveChild(fText);
107 fText = NULL;
110 BRect rect = fBitmap->Bounds();
111 if (!fReplicated) {
112 Window()->ResizeTo(rect.right, rect.bottom);
113 Window()->Activate(true);
115 ResizeTo(rect.right, rect.bottom);
116 Invalidate();
118 break;
120 case B_ABOUT_REQUESTED:
121 OverlayAboutRequested();
122 break;
123 case B_COLORS_UPDATED:
125 rgb_color color;
126 if (msg->FindColor(ui_color_name(B_PANEL_TEXT_COLOR), &color)
127 == B_OK)
128 fText->SetFontAndColor(be_plain_font, B_FONT_ALL, &color);
129 break;
131 default:
132 BView::MessageReceived(msg);
133 break;
138 BArchivable *OverlayView::Instantiate(BMessage *data)
140 return new OverlayView(data);
144 status_t
145 OverlayView::Archive(BMessage *archive, bool deep) const
147 BView::Archive(archive, deep);
149 archive->AddString("add_on", "application/x-vnd.Haiku-OverlayImage");
150 archive->AddString("class", "OverlayImage");
152 if (fBitmap) {
153 fBitmap->Lock();
154 fBitmap->Archive(archive);
155 fBitmap->Unlock();
157 //archive->PrintToStream();
159 return B_OK;
163 void
164 OverlayView::OverlayAboutRequested()
166 BAlert *alert = new BAlert("about",
167 "OverlayImage\n"
168 "Copyright 1999-2010\n\n\t"
169 "originally by Seth Flaxman\n\t"
170 "modified by Hartmuth Reh\n\t"
171 "further modified by Humdinger\n",
172 "OK");
173 BTextView *view = alert->TextView();
174 BFont font;
175 view->SetStylable(true);
176 view->GetFont(&font);
177 font.SetSize(font.Size() + 7.0f);
178 font.SetFace(B_BOLD_FACE);
179 view->SetFontAndColor(0, 12, &font);
180 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
181 alert->Go();