xref: /haiku/src/apps/overlayimage/OverlayView.cpp (revision 4bd0c1066b227cec4b79883bdef697c7a27f2e90)
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 
23 #undef B_TRANSLATION_CONTEXT
24 #define B_TRANSLATION_CONTEXT "Main view"
25 
26 const float kDraggerSize = 7;
27 
28 
29 OverlayView::OverlayView(BRect frame)
30 	:
31 	BView(frame, "OverlayImage", B_FOLLOW_NONE, B_WILL_DRAW)
32 {
33 	fBitmap = NULL;
34 	fReplicated = false;
35 
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);
40 
41 	SetViewColor(B_TRANSPARENT_COLOR);
42 
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);
58 }
59 
60 
61 OverlayView::OverlayView(BMessage *archive)
62 	:
63 	BView(archive)
64 {
65 	fReplicated = true;
66 	fBitmap = new BBitmap(archive);
67 }
68 
69 
70 OverlayView::~OverlayView()
71 {
72 	delete fBitmap;
73 }
74 
75 
76 void
77 OverlayView::Draw(BRect)
78 {
79 	SetDrawingMode(B_OP_ALPHA);
80 	SetViewColor(B_TRANSPARENT_COLOR);
81 
82 	if (fBitmap)
83 		DrawBitmap(fBitmap, B_ORIGIN);
84 }
85 
86 
87 void
88 OverlayView::MessageReceived(BMessage *msg)
89 {
90 	switch (msg->what) {
91 		case B_SIMPLE_DATA:
92 		{
93 			if (fReplicated)
94 				break;
95 
96 			entry_ref ref;
97 			msg->FindRef("refs", &ref);
98 			BEntry entry(&ref);
99 			BPath path(&entry);
100 
101 			delete fBitmap;
102 			fBitmap = BTranslationUtils::GetBitmap(path.Path());
103 
104 			if (fBitmap != NULL) {
105 				if (fText != NULL) {
106 					RemoveChild(fText);
107 					fText = NULL;
108 				}
109 
110 				BRect rect = fBitmap->Bounds();
111 				if (!fReplicated) {
112 					Window()->ResizeTo(rect.right, rect.bottom);
113 					Window()->Activate(true);
114 				}
115 				ResizeTo(rect.right, rect.bottom);
116 				Invalidate();
117 			}
118 			break;
119 		}
120 	    case B_ABOUT_REQUESTED:
121 	      	OverlayAboutRequested();
122     		break;
123 		case B_COLORS_UPDATED:
124 		{
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;
130 		}
131 		default:
132 			BView::MessageReceived(msg);
133 			break;
134 	}
135 }
136 
137 
138 BArchivable *OverlayView::Instantiate(BMessage *data)
139 {
140 	return new OverlayView(data);
141 }
142 
143 
144 status_t
145 OverlayView::Archive(BMessage *archive, bool deep) const
146 {
147 	BView::Archive(archive, deep);
148 
149 	archive->AddString("add_on", "application/x-vnd.Haiku-OverlayImage");
150 	archive->AddString("class", "OverlayImage");
151 
152 	if (fBitmap) {
153 		fBitmap->Lock();
154 		fBitmap->Archive(archive);
155 		fBitmap->Unlock();
156 	}
157 	//archive->PrintToStream();
158 
159 	return B_OK;
160 }
161 
162 
163 void
164 OverlayView::OverlayAboutRequested()
165 {
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();
182 }
183