xref: /haiku/src/libs/alm/SharedSolver.h (revision 25a7b01d15612846f332751841da3579db313082)
1 /*
2  * Copyright 2012, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _SHARED_SOLVER_H
6 #define _SHARED_SOLVER_H
7 
8 
9 #include <set>
10 
11 #include <Archivable.h>
12 #include <LayoutContext.h>
13 #include <ObjectList.h>
14 #include <Referenceable.h>
15 
16 #include "LinearSpec.h"
17 
18 
19 class BMessage;
20 
21 namespace BALM {
22 	class BALMLayout;
23 	class Area;
24 };
25 
26 using BALM::BALMLayout;
27 using BALM::Area;
28 
29 
30 namespace BPrivate {
31 
32 
33 class SharedSolver : BLayoutContextListener, public BReferenceable,
34 	   public BArchivable {
35 public:
36 								SharedSolver(BMessage* archive);
37 								SharedSolver();
38 								~SharedSolver();
39 
40 			void				Invalidate(bool children);
41 
42 			LinearSpec*			Solver() const;
43 			ResultType			Result();
44 
45 			void				RegisterLayout(BALMLayout* layout);
46 			void				LayoutLeaving(const BALMLayout* layout);
47 
48 			ResultType			ValidateMinSize();
49 			ResultType			ValidateMaxSize();
50 			ResultType			ValidatePreferredSize();
51 			ResultType			ValidateLayout(BLayoutContext* context);
52 
53 			status_t			AddFriendReferences(const BALMLayout* layout,
54 									BMessage* archive, const char* field);
55 
56 			status_t			Archive(BMessage* archive, bool deep) const;
57 			status_t			AllArchived(BMessage* archive) const;
58 			status_t			AllUnarchived(const BMessage* archive);
59 
60 	static	BArchivable*		Instantiate(BMessage* archive);
61 
62 private:
63 			struct MinSizeValidator;
64 			struct MaxSizeValidator;
65 			struct PreferredSizeValidator;
66 
67 			friend struct MinSizeValidator;
68 			friend struct MaxSizeValidator;
69 			friend struct PreferredSizeValidator;
70 
71 
72 	static	void				_AddConstraintsToSet(Area* area,
73 									std::set<const Constraint*>& constraints);
74 	static	status_t			_AddConstraintToArchive(Constraint* constraint,
75 									BMessage* archive);
76 			status_t			_InstantiateConstraint(const void* rawData,
77 									ssize_t numBytes, BUnarchiver& unarchiver);
78 
79 			void				SetMaxSize(BALM::BALMLayout* layout,
80 									const BSize& max);
81 			void				SetMinSize(BALM::BALMLayout* layout,
82 									const BSize& min);
83 			void				SetPreferredSize(BALM::BALMLayout* layout,
84 									const BSize& preferred);
85 
86 	virtual	void				LayoutContextLeft(BLayoutContext* context);
87 
88 			void				_UpdateConstraints();
89 			void				_SetContext(BLayoutContext* context);
90 			bool				_IsMinSet();
91 			bool				_IsMaxSet();
92 			void				_ValidateConstraints();
93 
94 			template <class Validator>
95 			void				_Validate(bool& isValid, ResultType& result);
96 
97 
98 			bool				fConstraintsValid;
99 			bool				fMinValid;
100 			bool				fMaxValid;
101 			bool				fPreferredValid;
102 			bool				fLayoutValid;
103 
104 			BLayoutContext*		fLayoutContext;
105 			BObjectList<BALM::BALMLayout> fLayouts;
106 			LinearSpec			fLinearSpec;
107 
108 			ResultType			fMinResult;
109 			ResultType			fMaxResult;
110 			ResultType			fPreferredResult;
111 			ResultType			fLayoutResult;
112 };
113 
114 
115 };
116 
117 
118 using BPrivate::SharedSolver;
119 
120 
121 #endif
122