xref: /haiku/src/apps/icon-o-matic/document/Document.cpp (revision 220d04022750f40f8bac8f01fa551211e28d04f2)
1 /*
2  * Copyright 2006-2007, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 
9 
10 #include "Document.h"
11 
12 #include "CommandStack.h"
13 #include "DocumentSaver.h"
14 #include "Icon.h"
15 #include "PathContainer.h"
16 #include "Selection.h"
17 #include "ShapeContainer.h"
18 #include "StyleContainer.h"
19 
20 #include <Entry.h>
21 
22 #include <new>
23 #include <stdio.h>
24 
25 
26 using std::nothrow;
27 _USING_ICON_NAMESPACE
28 
29 
30 // constructor
31 Document::Document(const char* name)
32 	: RWLocker("document rw lock"),
33 	  fIcon(new (nothrow) _ICON_NAMESPACE Icon()),
34 	  fCommandStack(new (nothrow) ::CommandStack()),
35 	  fSelection(new (nothrow) ::Selection()),
36 
37 	  fName(name),
38 
39 	  fNativeSaver(NULL),
40 	  fExportSaver(NULL)
41 {
42 }
43 
44 // destructor
45 Document::~Document()
46 {
47 	delete fCommandStack;
48 	delete fSelection;
49 	fIcon->Release();
50 	delete fNativeSaver;
51 	delete fExportSaver;
52 }
53 
54 // SetName
55 void
56 Document::SetName(const char* name)
57 {
58 	if (fName == name)
59 		return;
60 
61 	fName = name;
62 	Notify();
63 }
64 
65 // Name
66 const char*
67 Document::Name() const
68 {
69 	return fName.String();
70 }
71 
72 // SetNativeSaver
73 void
74 Document::SetNativeSaver(::DocumentSaver* saver)
75 {
76 	delete fNativeSaver;
77 	fNativeSaver = saver;
78 }
79 
80 // SetExportSaver
81 void
82 Document::SetExportSaver(::DocumentSaver* saver)
83 {
84 	delete fExportSaver;
85 	fExportSaver = saver;
86 }
87 
88 // SetIcon
89 void
90 Document::SetIcon(_ICON_NAMESPACE Icon* icon)
91 {
92 	if (fIcon == icon)
93 		return;
94 
95 	fIcon->Release();
96 
97 	fIcon = icon;
98 
99 	// we don't acquire, since we own the icon
100 }
101 
102 // MakeEmpty
103 void
104 Document::MakeEmpty(bool includingSavers)
105 {
106 	fCommandStack->Clear();
107 	fSelection->DeselectAll();
108 	fIcon->MakeEmpty();
109 
110 	if (includingSavers) {
111 		delete fNativeSaver;
112 		fNativeSaver = NULL;
113 		delete fExportSaver;
114 		fExportSaver = NULL;
115 	}
116 }
117 
118 // IsEmpty
119 bool
120 Document::IsEmpty() const
121 {
122 	return fIcon->Styles()->CountStyles() == 0
123 		&& fIcon->Paths()->CountPaths() == 0
124 		&& fIcon->Shapes()->CountShapes() == 0;
125 }
126 
127