xref: /haiku/src/apps/overlayimage/OverlayView.cpp (revision daf10ef34051db2f97ac3a116c86a6828fab9d66)
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 		{
122 			OverlayAboutRequested();
123 			break;
124 		}
125 		case B_COLORS_UPDATED:
126 		{
127 			rgb_color color;
128 			if (msg->FindColor(ui_color_name(B_PANEL_TEXT_COLOR), &color)
129 					== B_OK)
130 				fText->SetFontAndColor(be_plain_font, B_FONT_ALL, &color);
131 			break;
132 		}
133 		default:
134 			BView::MessageReceived(msg);
135 			break;
136 	}
137 }
138 
139 
140 BArchivable *OverlayView::Instantiate(BMessage *data)
141 {
142 	return new OverlayView(data);
143 }
144 
145 
146 status_t
147 OverlayView::Archive(BMessage *archive, bool deep) const
148 {
149 	BView::Archive(archive, deep);
150 
151 	archive->AddString("add_on", "application/x-vnd.Haiku-OverlayImage");
152 	archive->AddString("class", "OverlayImage");
153 
154 	if (fBitmap) {
155 		fBitmap->Lock();
156 		fBitmap->Archive(archive);
157 		fBitmap->Unlock();
158 	}
159 	//archive->PrintToStream();
160 
161 	return B_OK;
162 }
163 
164 
165 void
166 OverlayView::OverlayAboutRequested()
167 {
168 	BAlert *alert = new BAlert("about",
169 		B_TRANSLATE("OverlayImage\n"
170 		"Copyright 1999-2010\n\n\t"
171 		"originally by Seth Flaxman\n\t"
172 		"modified by Hartmuth Reh\n\t"
173 		"further modified by Humdinger\n"),
174 		"OK");
175 	BTextView *view = alert->TextView();
176 	BFont font;
177 	view->SetStylable(true);
178 	view->GetFont(&font);
179 	font.SetSize(font.Size() + 7.0f);
180 	font.SetFace(B_BOLD_FACE);
181 	view->SetFontAndColor(0, 12, &font);
182 	alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
183 	alert->Go();
184 }
185