1/*
2
3MarginView.h
4
5Copyright (c) 2002 Haiku.
6
7Authors:
8	Philippe Houdoin
9	Simon Gauvin
10	Michael Pfeiffer
11
12Permission is hereby granted, free of charge, to any person obtaining a copy of
13this software and associated documentation files (the "Software"), to deal in
14the Software without restriction, including without limitation the rights to
15use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
16of the Software, and to permit persons to whom the Software is furnished to do
17so, subject to the following conditions:
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28THE SOFTWARE.
29
30	Documentation:
31
32	The MarginView is designed to be a self contained component that manages
33	the display of a BBox control that shows a graphic of a page and its'
34	margings. The component also includes text fields that are used to mofify
35	the margin values and a popup to change the units used for the margins.
36
37	There are two interfaces for the MarginView component:
38
39	1) Set methods:
40		- page size
41		- orientation
42
43	   Get methods to retrieve:
44		- margins
45		- page size
46
47		The method interface is available for the parent Component to call on
48		the MarginView in response to the Window receiveing messages from
49		other BControls that it contains, such as a Page Size popup. The
50		Get methods are used to extract the page size and margins so that
51		the printer driver may put these values into a BMessage for printing.
52
53	2) 'Optional' Message interface:
54		- Set Page Size
55		- Flip Orientation
56
57		The message interface is available for GUI Controls, BPopupMenu to send
58		messages to the MarginView if the parent Window is not used to handle
59		the messages.
60
61	General Use of MarginView component:
62
63		1) Simply construct a new MarginView object with the margins
64			you want as defaults and add this view to the parent view
65			of the dialog.
66
67			MarginView *mv;
68			mv = new MarginView(viewSizeRect, pageWidth, pageHeight);
69			parentView->AddChild(mv);
70
71			* you can also set the margins in the constructor, and the units:
72
73			mv = new MarginView(viewSizeRect, pageWidth, pageHeight
74						marginRect, kUnitPointS);
75
76			! but remeber to have the marginRect values match the UNITS :-)
77
78		2) Set Page Size with methods:
79
80			mv-SetPageSize( pageWidth, pageHeight );
81			mv->UpdateView();
82
83		3) Set Page Size with BMessage:
84
85			BMessage* msg = new BMessage(CHANGE_PAGE_SIZE);
86			msg->AddFloat("width", pageWidth);
87			msg->AddFloat("height", pageHeight);
88			mv->PostMessage(msg);
89
90		4) Flip Page with methods:
91
92			mv-SetPageSize( pageHeight, pageWidth );
93			mv->UpdateView();
94
95		5) Flip Page with BMessage:
96
97			BMessage* msg = new BMessage(FLIP_PAGE);
98			mv->Looper()->PostMessage(msg);
99
100		Note: the MarginView DOES NOT keep track of the orientation. This
101				should be done by the code for the Page setup dialog.
102
103		6) Get Page Size
104
105			BPoint pageSize = mv->GetPageSize();
106
107		7) Get Margins
108
109			BRect margins = mv->GetMargins();
110
111		8) Get Units
112
113			uint32 units = mv->GetUnits();
114
115			where units is one of:
116				kUnitInch,  72 points/in
117				kUnitCM,    28.346 points/cm
118				kUnitPoint, 1 point/point
119*/
120
121#ifndef _MARGIN_VIEW_H
122#define _MARGIN_VIEW_H
123
124#include <InterfaceKit.h>
125#include <Looper.h>
126
127class BTextControl;
128class MarginManager;
129
130// Messages that the MarginManager accepts
131const uint32 TOP_MARGIN_CHANGED    = 'tchg';
132const uint32 RIGHT_MARGIN_CHANGED  = 'rchg';
133const uint32 LEFT_MARGIN_CHANGED   = 'lchg';
134const uint32 BOTTOM_MARGIN_CHANGED = 'bchg';
135const uint32 MARGIN_CHANGED        = 'mchg';
136const uint32 CHANGE_PAGE_SIZE      = 'chps';
137const uint32 FLIP_PAGE             = 'flip';
138const uint32 MARGIN_UNIT_CHANGED   = 'mucg';
139
140enum MarginUnit {
141	kUnitInch = 0,
142	kUnitCM,
143	kUnitPoint
144};
145
146class PageView : public BView
147{
148public:
149					PageView();
150
151	void			SetPageSize(float pageWidth, float pageHeight);
152	void			SetMargins(BRect margins);
153
154	virtual	void	Draw(BRect bounds);
155
156
157private:
158	float	fPageWidth;
159	float	fPageHeight;
160
161	BRect	fMargins;
162};
163
164/**
165 * Class MarginView
166 */
167class MarginView : public BBox
168{
169friend class MarginManager;
170
171public:
172							MarginView(int32 pageWidth = 0,
173								int32 pageHeight = 0,
174								BRect margins = BRect(1, 1, 1, 1), // 1 inch
175								MarginUnit unit = kUnitInch);
176
177	virtual					~MarginView();
178
179	virtual	void			AttachedToWindow();
180	virtual	void			MessageReceived(BMessage *msg);
181
182			// point.x = width, point.y = height
183			BPoint			PageSize() const;
184			void			SetPageSize(float pageWidth, float pageHeight);
185
186			// margin
187			BRect			Margin() const;
188
189			// units
190			MarginUnit		Unit() const;
191
192			// will cause a recalc and redraw
193			void			UpdateView(uint32 msg);
194
195private:
196			// all the GUI construction code
197			void			_ConstructGUI();
198
199			// utility method
200			void			_AllowOnlyNumbers(BTextControl *textControl,
201								int32 maxNum);
202
203			// performed internally using text fields
204			void			_SetMargin(BRect margin);
205
206			// performed internally using the supplied popup
207			void			_SetMarginUnit(MarginUnit unit);
208
209private:
210			BTextControl*	fTop;
211			BTextControl*	fBottom;
212			BTextControl*	fLeft;
213			BTextControl*	fRight;
214
215			// the actual size of the page in points
216			float			fPageHeight;
217			float			fPageWidth;
218
219			// rect that holds the margins for the page as a set of point offsets
220			BRect			fMargins;
221
222			// the units used to calculate the page size
223			MarginUnit		fMarginUnit;
224			float			fUnitValue;
225
226			PageView*		fPage;
227			BStringView*	fPageSize;
228};
229
230#endif // _MARGIN_VIEW_H
231