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