xref: /haiku/src/preferences/filetypes/IconView.cpp (revision 55b40aa53a835472ec7952b138ae4256203d02e4)
1 /*
2  * Copyright 2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "IconView.h"
8 
9 #include <Application.h>
10 #include <Bitmap.h>
11 #include <MenuItem.h>
12 #include <Mime.h>
13 #include <NodeInfo.h>
14 #include <PopUpMenu.h>
15 #include <Resources.h>
16 
17 #include <string.h>
18 
19 
20 IconView::IconView(BRect rect, const char* name, uint32 resizeMode)
21 	: BView(rect, name, resizeMode, B_WILL_DRAW),
22 	fIcon(NULL),
23 	fHeapIcon(NULL)
24 {
25 }
26 
27 
28 IconView::~IconView()
29 {
30 }
31 
32 
33 void
34 IconView::AttachedToWindow()
35 {
36 	if (Parent())
37 		SetViewColor(Parent()->ViewColor());
38 	else
39 		SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
40 }
41 
42 
43 void
44 IconView::Draw(BRect updateRect)
45 {
46 	SetDrawingMode(B_OP_ALPHA);
47 
48 	if (fHeapIcon != NULL)
49 		DrawBitmap(fHeapIcon, BPoint(0.0f, 0.0f));
50 	else if (fIcon != NULL)
51 		DrawBitmap(fIcon, BPoint(0.0f, 0.0f));
52 	else {
53 		// draw frame so that the user knows here is something he
54 		// might be able to click on
55 		SetHighColor(tint_color(ViewColor(), B_DARKEN_1_TINT));
56 		StrokeRect(Bounds());
57 	}
58 }
59 
60 
61 void
62 IconView::MouseDown(BPoint where)
63 {
64 	int32 buttons = B_PRIMARY_MOUSE_BUTTON;
65 	if (Looper() != NULL && Looper()->CurrentMessage() != NULL)
66 		Looper()->CurrentMessage()->FindInt32("buttons", &buttons);
67 
68 	if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0) {
69 		// show context menu
70 
71 		ConvertToScreen(&where);
72 
73 		BPopUpMenu* menu = new BPopUpMenu("context");
74 		menu->SetFont(be_plain_font);
75 		BMenuItem* item;
76 		menu->AddItem(item = new BMenuItem(fIcon != NULL
77 			? "Edit Icon" B_UTF8_ELLIPSIS : "Add Icon" B_UTF8_ELLIPSIS, NULL));
78 		item->SetEnabled(false);
79 		menu->AddItem(item = new BMenuItem("Remove Icon", NULL));
80 		item->SetEnabled(false);
81 
82 		menu->Go(where);
83 	}
84 }
85 
86 
87 void
88 IconView::SetTo(entry_ref* ref)
89 {
90 	delete fIcon;
91 	fIcon = NULL;
92 
93 	if (ref != NULL) {
94 		BNode node(ref);
95 		BNodeInfo info(&node);
96 		if (node.InitCheck() != B_OK
97 			|| info.InitCheck() != B_OK)
98 			return;
99 
100 		BBitmap* icon = new BBitmap(BRect(0, 0, B_LARGE_ICON - 1, B_LARGE_ICON - 1), B_RGB32);
101 		if (info.GetIcon(icon, B_LARGE_ICON) != B_OK) {
102 			delete icon;
103 			return;
104 		}
105 
106 		fIcon = icon;
107 	}
108 
109 	Invalidate();
110 }
111 
112 
113 void
114 IconView::ShowIconHeap(bool show)
115 {
116 	if (show == (fHeapIcon != NULL))
117 		return;
118 
119 	if (show) {
120 		BResources* resources = be_app->AppResources();
121 		const void* data = NULL;
122 		if (resources != NULL)
123 			data = resources->LoadResource('ICON', "icon heap", NULL);
124 		if (data != NULL) {
125 			fHeapIcon = new BBitmap(BRect(0, 0, B_LARGE_ICON - 1, B_LARGE_ICON - 1),
126 				B_CMAP8);
127 			memcpy(fHeapIcon->Bits(), data, fHeapIcon->BitsLength());
128 		}
129 	} else {
130 		delete fHeapIcon;
131 		fHeapIcon = NULL;
132 	}
133 }
134 
135