-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreplitxtReplicant.cpp
More file actions
205 lines (161 loc) · 4.81 KB
/
replitxtReplicant.cpp
File metadata and controls
205 lines (161 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// replitxtReplicant: implementation of a replitxt replicant
#ifndef __replitxtREPLICANT_H__
#include "replitxtReplicant.h"
#endif
const char *app_signature = "application/x-vnd.rxc-replitxt";
const char *replicant_name = "replitxt";
replitxtReplicant *global_pointer;
filter_result filter(BMessage *message, BHandler **target, BMessageFilter *messageFilter);
// *******************************
// * replitxtReplicant()
// * default constructor
// *******************************
replitxtReplicant::replitxtReplicant(const char* text)
: xReplicant(BRect(10, 10, 151, 41), replicant_name)
{
SetText(text);
fNeedID = true;
}
// *******************************
// * replitxtReplicant()
// * replicant constructor
// *******************************
replitxtReplicant::replitxtReplicant(BMessage *data)
: xReplicant(data)
{
SetViewColor(B_TRANSPARENT_32_BIT);
fString = data->FindString("text");
font_ascent = data->FindFloat("ascent");
fNeedID = data->FindBool("need_id");
if (!fNeedID)
fID = data->FindInt32("id");
// tell me where the mouse always is!
// SetEventMask(B_POINTER_EVENTS, B_NO_POINTER_HISTORY);
global_pointer = this;
}
#pragma mark -
// archive the replitxt replicant
status_t
replitxtReplicant::Archive(BMessage *data, bool deep) const
{
xReplicant::Archive(data, deep);
data->AddString("add_on", app_signature);
data->AddString("class", replicant_name);
data->AddString("text", fString);
data->AddFloat("ascent", font_ascent);
data->AddBool("need_id", fNeedID);
if (!fNeedID)
data->AddInt32("id", fID);
return B_OK;
}
// method to instantiate the view from a message
BArchivable*
replitxtReplicant::Instantiate(BMessage* inArchive)
{
if (!validate_instantiation(inArchive, replicant_name)) return NULL;
return new replitxtReplicant(inArchive);
}
#pragma mark -
void
replitxtReplicant::AttachedToWindow()
{
xReplicant::AttachedToWindow();
if (Replicated()) {
Parent()->Looper()->Lock();
fMessageFilter = new BMessageFilter('vivi', filter);
Parent()->Looper()->AddFilter(fMessageFilter);
// printf("there are %ld filters\n", Parent()->Looper()->FilterList()->CountItems());
Parent()->Looper()->Unlock();
fReplicantNum = Parent()->CountChildren()-1;
}
}
void
replitxtReplicant::DetachedFromWindow()
{
if (Replicated()) {
xReplicant::DetachedFromWindow();
Parent()->Looper()->Lock();
Parent()->Looper()->RemoveFilter(fMessageFilter);
Parent()->Looper()->Unlock();
}
}
// *******************************
// * Draw()
// *******************************
void
replitxtReplicant::Draw(BRect updateRect)
{
xReplicant::Draw(updateRect);
// write some text
SetDrawingMode(B_OP_ALPHA);
SetHighColor(0,0,0,40);
FillRect(Bounds());
SetHighColor(0,0,80,100);
StrokeRect(Bounds());
// SetBlendingMode(B_CONSTANT_ALPHA, B_ALPHA_OVERLAY);
MovePenTo(6,2+font_ascent);
DrawString(fString);
}
// *******************************
// * SetText()
// *******************************
void
replitxtReplicant::SetText(const char* text)
{
float width;
font_height FontAttributes;
fString = strdup(text);
// find out the width in pixels
width = be_plain_font->StringWidth(fString);
// and the height of the font
be_plain_font->GetHeight(&FontAttributes);
font_ascent = ceil(FontAttributes.ascent);
float font_height = font_ascent + ceil(FontAttributes.descent);
// now we should resize ourselves!
xResizeTo(5+width+5, 2+font_height+2);
Invalidate();
// printf("resizing to %.2f\n", width+20);
}
const char*
replitxtReplicant::GetText() const
{
return (strdup(fString));
}
filter_result
filter(BMessage *message, BHandler **target, BMessageFilter *messageFilter)
{
filter_result result = B_DISPATCH_MESSAGE;
replitxtReplicant *replicant;
if (message->what == 'vivi') {
int32 id;
int32 index;
bool set_text;
index = message->FindInt32("index");
id = message->FindInt32("id");
set_text = message->FindBool("set_text");
printf("v");
replicant = ((replitxtReplicant*)((BWindow*)messageFilter->Looper())->ChildAt(0)->ChildAt(index));
printf("%s\n", replicant->Name());
printf("[%ld:%ld, %c, %ld:%ld (%s)]\n", index, replicant->Index(), (replicant->NeedID()?'t':'f'), id, replicant->ID(), global_pointer->GetText());
if (replicant->NeedID())
if (index == replicant->Index()) {
replicant->SetID(id);
result = B_SKIP_MESSAGE;
}
// is the message for us?
if (replicant->ID() == id)
// should we return the string?
if (!set_text) {
BMessage *msg = new BMessage('vivi');
msg->AddString("text", replicant->GetText());
message->SendReply(msg);
} else { // we set the string
replicant->SetText(message->FindString("text"));
printf("should set text to %s\n", message->FindString("text"));
}
result = B_SKIP_MESSAGE;
} else
printf(".");
return result;
}
// ------------------------------------------------------------- xReplicant.cpp --