2 * Copyright 2001-2017 Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
8 * Jeremy Rand, jrand@magma.ca
9 * John Scipione, jscipione@gmail.com
14 #include <Messenger.h>
18 #include <InterfaceDefs.h>
24 // ToDo: in case the BDeskbar methods are called from a Deskbar add-on,
25 // they will currently deadlock most of the time (only those that do
26 // not need a reply will work).
27 // That should be fixed in the Deskbar itself, even if the Be API found
28 // a way around that (that doesn't work too well, BTW)
30 // The API in this file should be considered as part of OpenTracker - but
31 // should work with all versions of Tracker available for Haiku.
34 static const char* kDeskbarSignature
= "application/x-vnd.Be-TSKB";
36 static const uint32 kMsgIsAlwaysOnTop
= 'gtop';
37 static const uint32 kMsgAlwaysOnTop
= 'stop';
38 static const uint32 kMsgIsAutoRaise
= 'grse';
39 static const uint32 kMsgAutoRaise
= 'srse';
40 static const uint32 kMsgIsAutoHide
= 'ghid';
41 static const uint32 kMsgAutoHide
= 'shid';
43 static const uint32 kMsgAddView
= 'icon';
44 static const uint32 kMsgAddAddOn
= 'adon';
45 static const uint32 kMsgHasItem
= 'exst';
46 static const uint32 kMsgGetItemInfo
= 'info';
47 static const uint32 kMsgCountItems
= 'cwnt';
48 static const uint32 kMsgRemoveItem
= 'remv';
49 static const uint32 kMsgLocation
= 'gloc';
50 static const uint32 kMsgIsExpanded
= 'gexp';
51 static const uint32 kMsgSetLocation
= 'sloc';
52 static const uint32 kMsgExpand
= 'sexp';
56 get_deskbar_frame(BRect
* frame
)
58 BMessenger
deskbar(kDeskbarSignature
);
62 BMessage
request(B_GET_PROPERTY
);
63 request
.AddSpecifier("Frame");
64 request
.AddSpecifier("Window", "Deskbar");
67 result
= deskbar
.SendMessage(&request
, &reply
);
69 result
= reply
.FindRect("result", frame
);
75 // #pragma mark - BDeskbar
80 fMessenger(new BMessenger(kDeskbarSignature
))
92 BDeskbar::IsRunning() const
94 return fMessenger
->IsValid();
98 // #pragma mark - Item querying methods
102 BDeskbar::Frame() const
104 BRect
frame(0.0, 0.0, 0.0, 0.0);
105 get_deskbar_frame(&frame
);
112 BDeskbar::Location(bool* _isExpanded
) const
114 deskbar_location location
= B_DESKBAR_RIGHT_TOP
;
115 BMessage
request(kMsgLocation
);
121 if (fMessenger
->IsTargetLocal()) {
122 // ToDo: do something about this!
123 // (if we just ask the Deskbar in this case, we would deadlock)
127 if (fMessenger
->SendMessage(&request
, &reply
) == B_OK
) {
129 if (reply
.FindInt32("location", &value
) == B_OK
)
130 location
= static_cast<deskbar_location
>(value
);
132 if (_isExpanded
&& reply
.FindBool("expanded", _isExpanded
) != B_OK
)
141 BDeskbar::SetLocation(deskbar_location location
, bool expanded
)
143 BMessage
request(kMsgSetLocation
);
144 request
.AddInt32("location", static_cast<int32
>(location
));
145 request
.AddBool("expand", expanded
);
147 return fMessenger
->SendMessage(&request
);
151 // #pragma mark - Other state methods
155 BDeskbar::IsExpanded() const
157 BMessage
request(kMsgIsExpanded
);
159 bool isExpanded
= true;
161 if (fMessenger
->SendMessage(&request
, &reply
) == B_OK
)
162 reply
.FindBool("expanded", &isExpanded
);
169 BDeskbar::Expand(bool expand
)
171 BMessage
request(kMsgExpand
);
172 request
.AddBool("expand", expand
);
174 return fMessenger
->SendMessage(&request
);
179 BDeskbar::IsAlwaysOnTop() const
181 BMessage
request(kMsgIsAlwaysOnTop
);
183 bool isAlwaysOnTop
= false;
185 if (fMessenger
->SendMessage(&request
, &reply
) == B_OK
)
186 reply
.FindBool("always on top", &isAlwaysOnTop
);
188 return isAlwaysOnTop
;
193 BDeskbar::SetAlwaysOnTop(bool alwaysOnTop
)
195 BMessage
request(kMsgAlwaysOnTop
);
196 request
.AddBool("always on top", alwaysOnTop
);
198 return fMessenger
->SendMessage(&request
);
203 BDeskbar::IsAutoRaise() const
205 BMessage
request(kMsgIsAutoRaise
);
207 bool isAutoRaise
= false;
209 if (fMessenger
->SendMessage(&request
, &reply
) == B_OK
)
210 reply
.FindBool("auto raise", &isAutoRaise
);
217 BDeskbar::SetAutoRaise(bool autoRaise
)
219 BMessage
request(kMsgAutoRaise
);
220 request
.AddBool("auto raise", autoRaise
);
222 return fMessenger
->SendMessage(&request
);
227 BDeskbar::IsAutoHide() const
229 BMessage
request(kMsgIsAutoHide
);
231 bool isAutoHidden
= false;
233 if (fMessenger
->SendMessage(&request
, &reply
) == B_OK
)
234 reply
.FindBool("auto hide", &isAutoHidden
);
241 BDeskbar::SetAutoHide(bool autoHide
)
243 BMessage
request(kMsgAutoHide
);
244 request
.AddBool("auto hide", autoHide
);
246 return fMessenger
->SendMessage(&request
);
250 // #pragma mark - Item querying methods
254 BDeskbar::GetItemInfo(int32 id
, const char** _name
) const
259 // Note: Be's implementation returns B_BAD_VALUE if *_name was NULL,
260 // not just if _name was NULL. This doesn't make much sense, so we
261 // do not imitate this behaviour.
263 BMessage
request(kMsgGetItemInfo
);
264 request
.AddInt32("id", id
);
267 status_t result
= fMessenger
->SendMessage(&request
, &reply
);
268 if (result
== B_OK
) {
270 result
= reply
.FindString("name", &name
);
271 if (result
== B_OK
) {
272 *_name
= strdup(name
);
274 result
= B_NO_MEMORY
;
283 BDeskbar::GetItemInfo(const char* name
, int32
* _id
) const
288 BMessage
request(kMsgGetItemInfo
);
289 request
.AddString("name", name
);
292 status_t result
= fMessenger
->SendMessage(&request
, &reply
);
294 result
= reply
.FindInt32("id", _id
);
301 BDeskbar::HasItem(int32 id
) const
303 BMessage
request(kMsgHasItem
);
304 request
.AddInt32("id", id
);
307 if (fMessenger
->SendMessage(&request
, &reply
) == B_OK
)
308 return reply
.FindBool("exists");
315 BDeskbar::HasItem(const char* name
) const
317 BMessage
request(kMsgHasItem
);
318 request
.AddString("name", name
);
321 if (fMessenger
->SendMessage(&request
, &reply
) == B_OK
)
322 return reply
.FindBool("exists");
329 BDeskbar::CountItems() const
331 BMessage
request(kMsgCountItems
);
334 if (fMessenger
->SendMessage(&request
, &reply
) == B_OK
)
335 return reply
.FindInt32("count");
341 // #pragma mark - Item querying methods
345 BDeskbar::AddItem(BView
* view
, int32
* _id
)
348 status_t result
= view
->Archive(&archive
);
352 BMessage
request(kMsgAddView
);
353 request
.AddMessage("view", &archive
);
356 result
= fMessenger
->SendMessage(&request
, &reply
);
357 if (result
== B_OK
) {
359 result
= reply
.FindInt32("id", _id
);
361 reply
.FindInt32("error", &result
);
369 BDeskbar::AddItem(entry_ref
* addon
, int32
* _id
)
371 BMessage
request(kMsgAddAddOn
);
372 request
.AddRef("addon", addon
);
375 status_t status
= fMessenger
->SendMessage(&request
, &reply
);
376 if (status
== B_OK
) {
378 status
= reply
.FindInt32("id", _id
);
380 reply
.FindInt32("error", &status
);
388 BDeskbar::RemoveItem(int32 id
)
390 BMessage
request(kMsgRemoveItem
);
391 request
.AddInt32("id", id
);
393 // ToDo: the Deskbar does not reply to this message, so we don't
394 // know if it really succeeded - we can just acknowledge that the
395 // message was sent to the Deskbar
397 return fMessenger
->SendMessage(&request
);
402 BDeskbar::RemoveItem(const char* name
)
404 BMessage
request(kMsgRemoveItem
);
405 request
.AddString("name", name
);
407 // ToDo: the Deskbar does not reply to this message, so we don't
408 // know if it really succeeded - we can just acknowledge that the
409 // message was sent to the Deskbar
411 return fMessenger
->SendMessage(&request
);