xref: /haiku/src/apps/overlayimage/OverlayView.cpp (revision 3634f142352af2428aed187781fc9d75075e9140)
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 <AboutWindow.h>
17 #include <Catalog.h>
18 #include <InterfaceDefs.h>
19 #include <Locale.h>
20 #include <String.h>
21 #include <TextView.h>
22 
23 
24 #undef B_TRANSLATION_CONTEXT
25 #define B_TRANSLATION_CONTEXT "Main view"
26 
27 const float kDraggerSize = 7;
28 
29 
30 OverlayView::OverlayView(BRect frame)
31 	:
32 	BView(frame, "OverlayImage", B_FOLLOW_NONE, B_WILL_DRAW)
33 {
34 	fBitmap = NULL;
35 	fReplicated = false;
36 
37 	frame.left = frame.right - kDraggerSize;
38 	frame.top = frame.bottom - kDraggerSize;
39 	BDragger* dragger = new BDragger(frame, this, B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
40 	AddChild(dragger);
41 
42 	SetViewColor(B_TRANSPARENT_COLOR);
43 
44 	fText = new BTextView(Bounds(), "bgView", Bounds(), B_FOLLOW_ALL, B_WILL_DRAW);
45 	fText->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
46 	rgb_color color = ui_color(B_PANEL_TEXT_COLOR);
47 	fText->SetFontAndColor(be_plain_font, B_FONT_ALL, &color);
48 	AddChild(fText);
49 	BString text;
50 	text << B_TRANSLATE(
51 		"Enable \"Show replicants\" in Deskbar.\n"
52 		"Drag & drop an image.\n"
53 		"Drag the replicant to the Desktop.");
54 	fText->SetText(text);
55 	fText->SetAlignment(B_ALIGN_CENTER);
56 	fText->MakeEditable(false);
57 	fText->MakeSelectable(false);
58 	fText->MoveBy(0, (Bounds().bottom - fText->TextRect().bottom) / 2);
59 }
60 
61 
62 OverlayView::OverlayView(BMessage* archive)
63 	:
64 	BView(archive)
65 {
66 	fReplicated = true;
67 	fBitmap = new BBitmap(archive);
68 }
69 
70 
71 OverlayView::~OverlayView()
72 {
73 	delete fBitmap;
74 }
75 
76 
77 void
78 OverlayView::Draw(BRect)
79 {
80 	SetDrawingMode(B_OP_ALPHA);
81 	SetViewColor(B_TRANSPARENT_COLOR);
82 
83 	if (fBitmap)
84 		DrawBitmap(fBitmap, B_ORIGIN);
85 }
86 
87 
88 void
89 OverlayView::MessageReceived(BMessage* msg)
90 {
91 	switch (msg->what) {
92 		case B_SIMPLE_DATA:
93 		{
94 			if (fReplicated)
95 				break;
96 
97 			entry_ref ref;
98 			msg->FindRef("refs", &ref);
99 			BEntry entry(&ref);
100 			BPath path(&entry);
101 
102 			delete fBitmap;
103 			fBitmap = BTranslationUtils::GetBitmap(path.Path());
104 
105 			if (fBitmap != NULL) {
106 				if (fText != NULL) {
107 					RemoveChild(fText);
108 					fText = NULL;
109 				}
110 
111 				BRect rect = fBitmap->Bounds();
112 				if (!fReplicated) {
113 					Window()->ResizeTo(rect.right, rect.bottom);
114 					Window()->Activate(true);
115 				}
116 				ResizeTo(rect.right, rect.bottom);
117 				Invalidate();
118 			}
119 			break;
120 		}
121 		case B_ABOUT_REQUESTED:
122 		{
123 			OverlayAboutRequested();
124 			break;
125 		}
126 		case B_COLORS_UPDATED:
127 		{
128 			rgb_color color;
129 			if (msg->FindColor(ui_color_name(B_PANEL_TEXT_COLOR), &color) == 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*
141 OverlayView::Instantiate(BMessage* data)
142 {
143 	return new OverlayView(data);
144 }
145 
146 
147 status_t
148 OverlayView::Archive(BMessage* archive, bool deep) const
149 {
150 	BView::Archive(archive, deep);
151 
152 	archive->AddString("add_on", kAppSignature);
153 	archive->AddString("class", "OverlayImage");
154 
155 	if (fBitmap) {
156 		fBitmap->Lock();
157 		fBitmap->Archive(archive);
158 		fBitmap->Unlock();
159 	}
160 
161 	return B_OK;
162 }
163 
164 
165 void
166 OverlayView::OverlayAboutRequested()
167 {
168 	BAboutWindow* aboutwindow
169 		= new BAboutWindow(B_TRANSLATE_SYSTEM_NAME("OverlayImage"), kAppSignature);
170 
171 	const char* authors[] = {
172 		"Seth Flaxman",
173 		"Hartmuth Reh",
174 		"Humdinger",
175 		NULL
176 	};
177 
178 	aboutwindow->AddCopyright(1999, "Seth Flaxman");
179 	aboutwindow->AddCopyright(2010, "Haiku, Inc.");
180 	aboutwindow->AddAuthors(authors);
181 	aboutwindow->Show();
182 }
183