xref: /haiku/src/apps/overlayimage/OverlayView.cpp (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
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_TRANSLATION_CONTEXT
23 #define B_TRANSLATION_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->MakeEditable(false);
53 	fText->MakeSelectable(false);
54 	fText->MoveBy(0, (Bounds().bottom - fText->TextRect().bottom) / 2);
55 }
56 
57 
58 OverlayView::OverlayView(BMessage *archive)
59 	:
60 	BView(archive)
61 {
62 	fReplicated = true;
63 	fBitmap = new BBitmap(archive);
64 }
65 
66 
67 OverlayView::~OverlayView()
68 {
69 	delete fBitmap;
70 }
71 
72 
73 void
74 OverlayView::Draw(BRect)
75 {
76 	SetDrawingMode(B_OP_ALPHA);
77 	SetViewColor(B_TRANSPARENT_COLOR);
78 
79 	if (fBitmap)
80 		DrawBitmap(fBitmap, B_ORIGIN);
81 }
82 
83 
84 void
85 OverlayView::MessageReceived(BMessage *msg)
86 {
87 	switch (msg->what) {
88 		case B_SIMPLE_DATA:
89 		{
90 			if (fReplicated)
91 				break;
92 
93 			entry_ref ref;
94 			msg->FindRef("refs", &ref);
95 			BEntry entry(&ref);
96 			BPath path(&entry);
97 
98 			delete fBitmap;
99 			fBitmap = BTranslationUtils::GetBitmap(path.Path());
100 
101 			if (fBitmap != NULL) {
102 				if (fText != NULL) {
103 					RemoveChild(fText);
104 					fText = NULL;
105 				}
106 
107 				BRect rect = fBitmap->Bounds();
108 				if (!fReplicated) {
109 					Window()->ResizeTo(rect.right, rect.bottom);
110 					Window()->Activate(true);
111 				}
112 				ResizeTo(rect.right, rect.bottom);
113 				Invalidate();
114 			}
115 			break;
116 		}
117 	    case B_ABOUT_REQUESTED:
118 	      	OverlayAboutRequested();
119     		break;
120 
121 		default:
122 			BView::MessageReceived(msg);
123 			break;
124 	}
125 }
126 
127 
128 BArchivable *OverlayView::Instantiate(BMessage *data)
129 {
130 	return new OverlayView(data);
131 }
132 
133 
134 status_t
135 OverlayView::Archive(BMessage *archive, bool deep) const
136 {
137 	BView::Archive(archive, deep);
138 
139 	archive->AddString("add_on", "application/x-vnd.Haiku-OverlayImage");
140 	archive->AddString("class", "OverlayImage");
141 
142 	if (fBitmap) {
143 		fBitmap->Lock();
144 		fBitmap->Archive(archive);
145 		fBitmap->Unlock();
146 	}
147 	//archive->PrintToStream();
148 
149 	return B_OK;
150 }
151 
152 
153 void
154 OverlayView::OverlayAboutRequested()
155 {
156 	BAlert *alert = new BAlert("about",
157 		"OverlayImage\n"
158 		"Copyright 1999-2010\n\n\t"
159 		"originally by Seth Flaxman\n\t"
160 		"modified by Hartmuth Reh\n\t"
161 		"further modified by Humdinger\n",
162 		"OK");
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 	alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
171 	alert->Go();
172 }
173