xref: /haiku/src/apps/icon-o-matic/gui/IconObjectListView.cpp (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
1 /*
2  * Copyright 2006-2007, Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 
9 #include "IconObjectListView.h"
10 
11 #include <new>
12 #include <stdio.h>
13 #include <string.h>
14 
15 #include <Catalog.h>
16 #include <Locale.h>
17 
18 #include "CommandStack.h"
19 #include "CommonPropertyIDs.h"
20 #include "IconObject.h"
21 #include "Property.h"
22 #include "PropertyItemView.h"
23 #include "PropertyObject.h"
24 #include "Selection.h"
25 #include "SetPropertiesCommand.h"
26 
27 
28 #undef B_TRANSLATION_CONTEXT
29 #define B_TRANSLATION_CONTEXT "Icon-O-Matic-PropertiesList"
30 
31 
32 using std::nothrow;
33 
34 // constructor
35 IconObjectListView::IconObjectListView()
36 	: PropertyListView(),
37 
38 	  fSelection(NULL),
39 	  fCommandStack(NULL),
40 	  fObject(NULL),
41 	  fIgnoreObjectChange(false)
42 {
43 }
44 
45 // destructor
46 IconObjectListView::~IconObjectListView()
47 {
48 	SetSelection(NULL);
49 	_SetObject(NULL);
50 }
51 
52 // Draw
53 void
54 IconObjectListView::Draw(BRect updateRect)
55 {
56 	PropertyListView::Draw(updateRect);
57 
58 	if (fObject)
59 		return;
60 
61 	// display helpful messages
62 	const char* message1 = B_TRANSLATE_CONTEXT(
63 		"Click on an object in", "Empty property list - 1st line");
64 	const char* message2 = B_TRANSLATE_CONTEXT(
65 		"any of the other lists to", "Empty property list - 2nd line");
66 	const char* message3 = B_TRANSLATE_CONTEXT(
67 		"edit it's properties here.", "Empty property list - 3rd line");
68 
69 	SetHighColor(tint_color(LowColor(), B_DARKEN_2_TINT));
70 	font_height fh;
71 	GetFontHeight(&fh);
72 	BRect b(Bounds());
73 
74 	BPoint middle;
75 	float textHeight = (fh.ascent + fh.descent) * 1.5;
76 	middle.y = (b.top + b.bottom) / 2.0 - textHeight;
77 	middle.x = (b.left + b.right - StringWidth(message1)) / 2.0;
78 	DrawString(message1, middle);
79 
80 	middle.y += textHeight;
81 	middle.x = (b.left + b.right - StringWidth(message2)) / 2.0;
82 	DrawString(message2, middle);
83 
84 	middle.y += textHeight;
85 	middle.x = (b.left + b.right - StringWidth(message3)) / 2.0;
86 	DrawString(message3, middle);
87 }
88 
89 // PropertyChanged
90 void
91 IconObjectListView::PropertyChanged(const Property* previous,
92 									const Property* current)
93 {
94 	if (!fCommandStack || !fObject)
95 		return;
96 
97 	PropertyObject* oldObject = new (nothrow) PropertyObject();
98 	if (oldObject)
99 		oldObject->AddProperty(previous->Clone());
100 
101 	PropertyObject* newObject = new (nothrow) PropertyObject();
102 	if (newObject)
103 		newObject->AddProperty(current->Clone());
104 
105 	IconObject** objects = new (nothrow) IconObject*[1];
106 	if (objects)
107 		objects[0] = fObject;
108 
109 	Command* command = new (nothrow) SetPropertiesCommand(objects, 1,
110 														  oldObject,
111 														  newObject);
112 	fIgnoreObjectChange = true;
113 	fCommandStack->Perform(command);
114 	fIgnoreObjectChange = false;
115 }
116 
117 // PasteProperties
118 void
119 IconObjectListView::PasteProperties(const PropertyObject* object)
120 {
121 	// TODO: command for this
122 	if (fObject)
123 		fObject->SetToPropertyObject(object);
124 
125 	PropertyListView::PasteProperties(object);
126 }
127 
128 // IsEditingMultipleObjects
129 bool
130 IconObjectListView::IsEditingMultipleObjects()
131 {
132 	return false;
133 }
134 
135 // #pragma mark -
136 
137 // ObjectChanged
138 void
139 IconObjectListView::ObjectChanged(const Observable* object)
140 {
141 	if (object == fSelection) {
142 		Selectable* selected = fSelection->SelectableAt(0);
143 		_SetObject(dynamic_cast<IconObject*>(selected));
144 	}
145 
146 	if (object == fObject/* && !fIgnoreObjectChange*/) {
147 //printf("IconObjectListView::ObjectChanged(fObject)\n");
148 		SetTo(fObject->MakePropertyObject());
149 	}
150 }
151 
152 // #pragma mark -
153 
154 // SetSelection
155 void
156 IconObjectListView::SetSelection(Selection* selection)
157 {
158 	if (fSelection == selection)
159 		return;
160 
161 	if (fSelection)
162 		fSelection->RemoveObserver(this);
163 
164 	fSelection = selection;
165 
166 	if (fSelection)
167 		fSelection->AddObserver(this);
168 }
169 
170 // SetCommandStack
171 void
172 IconObjectListView::SetCommandStack(CommandStack* stack)
173 {
174 	fCommandStack = stack;
175 }
176 
177 // FocusNameProperty
178 void
179 IconObjectListView::FocusNameProperty()
180 {
181 	if (fObject == NULL)
182 		return;
183 
184 	int32 count = _CountItems();
185 	for (int32 i = 0; i < count; i++) {
186 		PropertyItemView* item = _ItemAt(i);
187 		Property* property = item->GetProperty();
188 		if (property != NULL && property->Identifier() == PROPERTY_NAME) {
189 			item->MakeFocus(true);
190 			break;
191 		}
192 	}
193 }
194 
195 
196 // #pragma mark -
197 
198 // _SetObject
199 void
200 IconObjectListView::_SetObject(IconObject* object)
201 {
202 	if (fObject == object)
203 		return;
204 
205 	if (fObject) {
206 		fObject->RemoveObserver(this);
207 		fObject->Release();
208 	}
209 
210 	fObject = object;
211 	PropertyObject* propertyObject = NULL;
212 
213 	if (fObject) {
214 		fObject->Acquire();
215 		fObject->AddObserver(this);
216 		propertyObject = fObject->MakePropertyObject();
217 	}
218 
219 	SetTo(propertyObject);
220 }
221 
222