xref: /haiku/src/kits/interface/ZombieReplicantView.cpp (revision 0d452c8f34013b611a54c746a71c05e28796eae2)
1 /*
2  * Copyright 2001-2010, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Marc Flerackers (mflerackers@androme.be)
7  */
8 
9 #include <Alert.h>
10 #include <Catalog.h>
11 #include <LocaleBackend.h>
12 #include <Message.h>
13 #include <MimeType.h>
14 #include <String.h>
15 
16 #include "ZombieReplicantView.h"
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <new>
22 
23 using BPrivate::gLocaleBackend;
24 using BPrivate::LocaleBackend;
25 
26 #undef B_TRANSLATE_CONTEXT
27 #define B_TRANSLATE_CONTEXT "ZombieReplicantView"
28 
29 #undef B_TRANSLATE
30 #define B_TRANSLATE(str) \
31 	gLocaleBackend->GetString(B_TRANSLATE_MARK(str), "ZombieReplicantView")
32 
33 
34 _BZombieReplicantView_::_BZombieReplicantView_(BRect frame, status_t error)
35 	:
36 	BBox(frame, "<Zombie>", B_FOLLOW_NONE, B_WILL_DRAW),
37 	fError(error)
38 {
39 	BFont font(be_bold_font);
40 	font.SetSize(9.0f); // TODO
41 	SetFont(&font);
42 	SetViewColor(kZombieColor);
43 
44 	// we need to translate some strings, and in order to do so, we need
45 	// to use the LocaleBackend to reach liblocale.so
46 	if (gLocaleBackend == NULL)
47 		LocaleBackend::LoadBackend();
48 }
49 
50 
51 _BZombieReplicantView_::~_BZombieReplicantView_()
52 {
53 }
54 
55 
56 void
57 _BZombieReplicantView_::MessageReceived(BMessage* msg)
58 {
59 	switch (msg->what) {
60 		case B_ABOUT_REQUESTED:
61 		{
62 			const char* addOn = NULL;
63 			BString error;
64 			if (fArchive->FindString("add_on", &addOn) == B_OK) {
65 				char description[B_MIME_TYPE_LENGTH] = "";
66 				BMimeType type(addOn);
67 				type.GetShortDescription(description);
68 				error = B_TRANSLATE("Cannot create the replicant for "
69 						"\"%description\".\n%error");
70 				error.ReplaceFirst("%description", description);
71 			} else
72 				error = B_TRANSLATE("Cannot locate the application for the "
73 					"replicant. No application signature supplied.\n%error");
74 
75 			error.ReplaceFirst("%error", strerror(fError));
76 
77 			BAlert* alert = new (std::nothrow) BAlert(B_TRANSLATE("Error"),
78 				error.String(), B_TRANSLATE("OK"), NULL, NULL,
79 				B_WIDTH_AS_USUAL, B_STOP_ALERT);
80 			if (alert != NULL)
81 				alert->Go();
82 
83 			break;
84 		}
85 		default:
86 			BView::MessageReceived(msg);
87 	}
88 }
89 
90 
91 void
92 _BZombieReplicantView_::Draw(BRect updateRect)
93 {
94 	BRect bounds(Bounds());
95 	font_height fh;
96 
97 	GetFontHeight(&fh);
98 
99 	DrawChar('?', BPoint(bounds.Width() / 2.0f - StringWidth("?") / 2.0f,
100 		bounds.Height() / 2.0f - fh.ascent / 2.0f));
101 
102 	BBox::Draw(updateRect);
103 }
104 
105 
106 void
107 _BZombieReplicantView_::MouseDown(BPoint)
108 {
109 }
110 
111 
112 status_t
113 _BZombieReplicantView_::Archive(BMessage* archive, bool) const
114 {
115 	*archive = *fArchive;
116 
117 	return B_OK;
118 }
119 
120 
121 void
122 _BZombieReplicantView_::SetArchive(BMessage* archive)
123 {
124 	fArchive = archive;
125 }
126