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