2 * Copyright 2003-2007, Haiku. All rights reserved.
3 * Distributed under the terms of the MIT License.
11 #include "DeskButton.h"
20 #include <PopUpMenu.h>
26 #define OPEN_REF 'opre'
27 #define DO_ACTION 'doac'
30 #undef B_TRANSLATION_CONTEXT
31 #define B_TRANSLATION_CONTEXT "DeskButton"
34 extern const char *kAppSignature
;
38 DeskButton::DeskButton(BRect frame
, entry_ref
* ref
, const char* name
,
39 BList
& titles
, BList
& actions
, uint32 resizeMask
, uint32 flags
)
40 : BView(frame
, name
, resizeMask
, flags
),
46 fSegments
= new BBitmap(BRect(0, 0, 15, 15), B_RGBA32
);
48 fSegments
= new BBitmap(BRect(0, 0, 15, 15), B_CMAP8
);
50 BNodeInfo::GetTrackerIcon(&fRef
, fSegments
, B_MINI_ICON
);
54 DeskButton::DeskButton(BMessage
*message
)
57 message
->FindRef("ref", &fRef
);
59 BString title
, action
;
61 while(message
->FindString("title", index
, &title
)==B_OK
62 && message
->FindString("action", index
, &action
)==B_OK
) {
63 fTitleList
.AddItem(new BString(title
));
64 fActionList
.AddItem(new BString(action
));
69 fSegments
= new BBitmap(BRect(0, 0, 15, 15), B_RGBA32
);
71 fSegments
= new BBitmap(BRect(0, 0, 15, 15), B_CMAP8
);
73 BNodeInfo::GetTrackerIcon(&fRef
, fSegments
, B_MINI_ICON
);
77 DeskButton::~DeskButton()
83 // archiving overrides
85 DeskButton::Instantiate(BMessage
*data
)
87 if (!validate_instantiation(data
, "DeskButton"))
90 return new DeskButton(data
);
95 DeskButton::Archive(BMessage
*data
, bool deep
) const
97 BView::Archive(data
, deep
);
99 data
->AddRef("ref", &fRef
);
101 for (int32 i
= 0; i
< fTitleList
.CountItems()
102 && i
< fActionList
.CountItems(); i
++) {
103 data
->AddString("title", *(BString
*)fTitleList
.ItemAt(i
));
104 data
->AddString("action", *(BString
*)fActionList
.ItemAt(i
));
107 data
->AddString("add_on", kAppSignature
);
113 DeskButton::MessageReceived(BMessage
*message
)
115 switch (message
->what
) {
117 be_roster
->Launch(&fRef
);
123 if (message
->FindString("action", &action
) == B_OK
) {
125 system(action
.String());
131 BView::MessageReceived(message
);
138 DeskButton::AttachedToWindow()
140 BView
*parent
= Parent();
142 SetViewColor(parent
->ViewColor());
144 BView::AttachedToWindow();
149 DeskButton::Draw(BRect rect
)
154 SetDrawingMode(B_OP_ALPHA
);
155 SetBlendingMode(B_PIXEL_ALPHA
, B_ALPHA_OVERLAY
);
157 SetDrawingMode(B_OP_OVER
);
159 DrawBitmap(fSegments
);
164 DeskButton::MouseDown(BPoint point
)
166 uint32 mouseButtons
= 0;
167 if (Window()->CurrentMessage() != NULL
)
168 mouseButtons
= Window()->CurrentMessage()->FindInt32("buttons");
170 BPoint where
= ConvertToScreen(point
);
172 if (mouseButtons
& B_SECONDARY_MOUSE_BUTTON
) {
173 BString label
= B_TRANSLATE_COMMENT("Open %name", "Don't translate "
175 label
.ReplaceFirst("%name", fRef
.name
);
176 BPopUpMenu
*menu
= new BPopUpMenu("", false, false);
177 menu
->SetFont(be_plain_font
);
178 menu
->AddItem(new BMenuItem(label
.String(), new BMessage(OPEN_REF
)));
179 if (fTitleList
.CountItems() > 0 && fActionList
.CountItems() > 0) {
180 menu
->AddSeparatorItem();
181 for (int32 i
= 0; i
< fTitleList
.CountItems()
182 && i
< fActionList
.CountItems(); i
++) {
183 BMessage
*message
= new BMessage(DO_ACTION
);
184 message
->AddString("action", *(BString
*)fActionList
.ItemAt(i
));
185 menu
->AddItem(new BMenuItem(((BString
*)fTitleList
.ItemAt(i
))->String(), message
));
189 menu
->SetTargetForItems(this);
190 menu
->Go(where
, true, true, BRect(where
- BPoint(4, 4),
191 where
+ BPoint(4, 4)));
192 } else if (mouseButtons
& B_PRIMARY_MOUSE_BUTTON
) {
193 be_roster
->Launch(&fRef
);