xref: /haiku/src/apps/overlayimage/OverlayView.cpp (revision 508f54795f39c3e7552d87c95aae9dd8ec6f505b)
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 window"
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 
110 				ResizeTo(rect.right, rect.bottom);
111 				Invalidate();
112 			}
113 			break;
114 		}
115 	    case B_ABOUT_REQUESTED:
116 	      	OverlayAboutRequested();
117     		break;
118 
119 		default:
120 			BView::MessageReceived(msg);
121 			break;
122 	}
123 }
124 
125 
126 BArchivable *OverlayView::Instantiate(BMessage *data)
127 {
128 	return new OverlayView(data);
129 }
130 
131 
132 status_t
133 OverlayView::Archive(BMessage *archive, bool deep) const
134 {
135 	BView::Archive(archive, deep);
136 
137 	archive->AddString("add_on", "application/x-vnd.Haiku-OverlayImage");
138 	archive->AddString("class", "OverlayImage");
139 
140 	if (fBitmap) {
141 		fBitmap->Lock();
142 		fBitmap->Archive(archive);
143 		fBitmap->Unlock();
144 	}
145 	//archive->PrintToStream();
146 
147 	return B_OK;
148 }
149 
150 
151 void
152 OverlayView::OverlayAboutRequested()
153 {
154 	BAlert *alert = new BAlert("about",
155 		"OverlayImage\n"
156 		"Copyright 1999-2010\n\n\t"
157 		"originally by Seth Flaxman\n\t"
158 		"modified by Hartmuth Reh\n\t"
159 		"further modified by Humdinger\n",
160 		"OK");
161 
162 	BTextView *view = alert->TextView();
163 	BFont font;
164 	view->SetStylable(true);
165 	view->GetFont(&font);
166 	font.SetSize(font.Size() + 7.0f);
167 	font.SetFace(B_BOLD_FACE);
168 	view->SetFontAndColor(0, 12, &font);
169 
170 	alert->Go();
171 }
172