* reordered MarinView.h, MarginView.cpp

* rename private functions to use underscore
* rename some public functions to match the usual style
* adjust pdf/ preview driver to use the renamed functions
* we where leaking a single message in _ConstructGui, otherwise no functional change
  


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26396 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Karsten Heimrich
2008-07-12 21:09:49 +00:00
parent fc53a63149
commit 4db0488ead
5 changed files with 414 additions and 425 deletions
+59 -62
View File
@@ -124,6 +124,7 @@ THE SOFTWARE.
#include <InterfaceKit.h>
#include <Looper.h>
class BTextControl;
class MarginManager;
// Messages that the MarginManager accepts
@@ -149,77 +150,73 @@ class MarginView : public BBox
{
friend class MarginManager;
private:
// GUI components
BTextControl *fTop, *fBottom, *fLeft, *fRight;
// rect that holds the margins for the page as a set of point offsets
BRect fMargins;
// the maximum size of the page view calculated from the view size
float fMaxPageWidth;
float fMaxPageHeight;
// the actual size of the page in points
float fPageHeight;
float fPageWidth;
// the units used to calculate the page size
MarginUnit fMarginUnit;
float fUnitValue;
// the size of the drawing area we have to draw the view in pixels
float fViewHeight;
float fViewWidth;
// Calculate the view size for the margins
void CalculateViewSize(uint32 msg);
// performed internally using the supplied popup
void SetMarginUnit(MarginUnit unit);
// performed internally using text fields
void SetMargin(BRect margin);
// utility method
void AllowOnlyNumbers(BTextControl *textControl, int maxNum);
public:
MarginView(BRect rect,
int32 pageWidth = 0,
int32 pageHeight = 0,
BRect margins = BRect(1, 1, 1, 1), // default to 1 inch
MarginUnit unit = kUnitInch);
MarginView(BRect rect, int32 pageWidth = 0,
int32 pageHeight = 0,
BRect margins = BRect(1, 1, 1, 1), // 1 inch
MarginUnit unit = kUnitInch);
~MarginView();
virtual ~MarginView();
/// all the GUI construction code
void ConstructGUI();
virtual void AttachedToWindow();
virtual void Draw(BRect rect);
virtual void FrameResized(float width, float height);
virtual void MessageReceived(BMessage *msg);
// page size
void SetPageSize(float pageWidth, float pageHeight);
// point.x = width, point.y = height
BPoint GetPageSize(void);
// point.x = width, point.y = height
BPoint PageSize() const;
void SetPageSize(float pageWidth, float pageHeight);
// margin
BRect GetMargin(void);
// margin
BRect Margin() const;
// orientation
// None, this state should be saved elsewhere in the page setup code
// and not here. See the FLIP_PAGE message to perform this function.
// units
MarginUnit Unit() const;
// units
MarginUnit GetMarginUnit(void);
// will cause a recalc and redraw
void UpdateView(uint32 msg);
// will cause a recalc and redraw
void UpdateView(uint32 msg);
private:
// all the GUI construction code
void _ConstructGUI();
// BeOS Hook methods
virtual void AttachedToWindow(void);
void Draw(BRect rect);
void FrameResized(float width, float height);
void MessageReceived(BMessage *msg);
// utility method
void _AllowOnlyNumbers(BTextControl *textControl,
int32 maxNum);
// performed internally using text fields
void _SetMargin(BRect margin);
// performed internally using the supplied popup
void _SetMarginUnit(MarginUnit unit);
// Calculate the view size for the margins
void _CalculateViewSize(uint32 msg);
private:
BTextControl* fTop;
BTextControl* fBottom;
BTextControl* fLeft;
BTextControl* fRight;
// rect that holds the margins for the page as a set of point offsets
BRect fMargins;
// the maximum size of the page view calculated from the view size
float fMaxPageWidth;
float fMaxPageHeight;
// the actual size of the page in points
float fPageHeight;
float fPageWidth;
// the units used to calculate the page size
MarginUnit fMarginUnit;
float fUnitValue;
// the size of the drawing area we have to draw the view in pixels
float fViewHeight;
float fViewWidth;
};
#endif // _MARGIN_VIEW_H
@@ -403,7 +403,7 @@ PageSetupWindow::UpdateSetupMessage()
fSetupMsg->ReplaceRect("paper_rect", r);
// Save the printable_rect
BRect margin = fMarginView->GetMargin();
BRect margin = fMarginView->Margin();
if (orientation == 0) {
margin.right = w - margin.right;
margin.bottom = h - margin.bottom;
@@ -414,7 +414,7 @@ PageSetupWindow::UpdateSetupMessage()
fSetupMsg->ReplaceRect("printable_rect", margin);
// save the units used
int32 units = fMarginView->GetMarginUnit();
int32 units = fMarginView->Unit();
if (fSetupMsg->HasInt32("units")) {
fSetupMsg->ReplaceInt32("units", units);
} else {
@@ -544,7 +544,7 @@ PageSetupWindow::MessageReceived(BMessage *msg)
// Simon added
case 'ornt':
{
BPoint p = fMarginView->GetPageSize();
BPoint p = fMarginView->PageSize();
int32 orientation;
msg->FindInt32("orientation", &orientation);
if (orientation == PrinterDriver::LANDSCAPE_ORIENTATION
@@ -276,7 +276,7 @@ PageSetupWindow::UpdateSetupMessage()
AddString(fSetupMsg, "preview:paper_size", item->Label());
// Save the printable_rect
BRect margin = fMarginView->GetMargin();
BRect margin = fMarginView->Margin();
if (fCurrentOrientation == PrinterDriver::PORTRAIT_ORIENTATION) {
margin.right = w - margin.right;
margin.bottom = h - margin.bottom;
@@ -287,7 +287,7 @@ PageSetupWindow::UpdateSetupMessage()
SetRect(fSetupMsg, "preview:printable_rect", margin);
SetRect(fSetupMsg, "printable_rect", ScaleRect(margin, scaleR));
SetInt32(fSetupMsg, "units", fMarginView->GetMarginUnit());
SetInt32(fSetupMsg, "units", fMarginView->Unit());
}
}
@@ -323,7 +323,7 @@ PageSetupWindow::MessageReceived(BMessage *msg)
if (fCurrentOrientation != orientation) {
fCurrentOrientation = orientation;
BPoint p = fMarginView->GetPageSize();
BPoint p = fMarginView->PageSize();
fMarginView->SetPageSize(p.y, p.x);
fMarginView->UpdateView(MARGIN_CHANGED);
}
@@ -363,8 +363,8 @@ PageSetupView::UpdateJobData()
}
// adjust printable rect by margin
fJobData->setMarginUnit(fMarginView->GetMarginUnit());
BRect margin = fMarginView->GetMargin();
fJobData->setMarginUnit(fMarginView->Unit());
BRect margin = fMarginView->Margin();
BRect printable_rect;
printable_rect.left = paper_rect.left + margin.left;
printable_rect.top = paper_rect.top + margin.top;
@@ -35,12 +35,16 @@ THE SOFTWARE.
#include "MarginView.h"
#include <AppKit.h>
#include <SupportKit.h>
#include <TextControl.h>
#include <stdio.h>
#include <stdlib.h>
/*----------------- MarginView Private Constants --------------------*/
const int kOffsetY = 20;
@@ -58,9 +62,7 @@ const static float kMinUnitHeight = 30; // pixels
const static float kUnitFormat[] = { kInchUnits, kCMUnits, kPointUnits };
const static char *kUnitNames[] = { "Inch", "cm", "Points", NULL };
const static MarginUnit kUnitMsg[] = { kUnitInch,
kUnitCM,
kUnitPoint };
const static MarginUnit kUnitMsg[] = { kUnitInch, kUnitCM, kUnitPoint };
const pattern kDots = {{ 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55 }};
@@ -69,7 +71,6 @@ const rgb_color kRed = { 255,0,0,0 };
const rgb_color kWhite = { 255,255,255,0 };
const rgb_color kGray = { 220,220,220,0 };
/*----------------- MarginView Public Methods --------------------*/
/**
* Constructor
@@ -81,13 +82,9 @@ const rgb_color kGray = { 220,220,220,0 };
* @param units, unit32 enum for units used in view
* @return void
*/
MarginView::MarginView(BRect frame,
int32 pageWidth,
int32 pageHeight,
BRect margins,
MarginUnit units)
:BBox(frame, NULL, B_FOLLOW_ALL)
MarginView::MarginView(BRect frame, int32 pageWidth, int32 pageHeight,
BRect margins, MarginUnit units)
: BBox(frame, NULL, B_FOLLOW_ALL)
{
fMarginUnit = units;
fUnitValue = kUnitFormat[units];
@@ -103,16 +100,33 @@ MarginView::MarginView(BRect frame,
fPageHeight = pageHeight;
}
/**
* Destructor
*
* @param none
* @return void
*/
MarginView::~MarginView() {
MarginView::~MarginView()
{
}
/**
* AttachToWindow
*
* @param none
* @return void
*/
void
MarginView::AttachedToWindow()
{
if (Parent())
SetViewColor(Parent()->ViewColor());
_ConstructGUI();
}
/*----------------- MarginView Public BeOS Hook Methods --------------------*/
/**
* Draw
@@ -176,53 +190,85 @@ MarginView::FrameResized(float width, float height)
fMaxPageHeight = height - kMinUnitHeight - kOffsetX;
fMaxPageWidth = width - kMinFieldWidth - kOffsetY;
CalculateViewSize(MARGIN_CHANGED);
_CalculateViewSize(MARGIN_CHANGED);
Invalidate();
}
/**
* AttachToWindow
* MesssageReceived()
*
* @param none
* Receive messages for the view
*
* @param BMessage* , the message being received
* @return void
*/
void
MarginView::AttachedToWindow()
MarginView::MessageReceived(BMessage *msg)
{
if (Parent()) {
SetViewColor(Parent()->ViewColor());
switch (msg->what) {
case CHANGE_PAGE_SIZE: {
float w;
float h;
msg->FindFloat("width", &w);
msg->FindFloat("height", &h);
SetPageSize(w, h);
UpdateView(MARGIN_CHANGED);
} break;
case FLIP_PAGE: {
BPoint p = PageSize();
SetPageSize(p.y, p.x);
UpdateView(MARGIN_CHANGED);
} break;
case MARGIN_CHANGED:
UpdateView(MARGIN_CHANGED);
break;
case TOP_MARGIN_CHANGED:
UpdateView(TOP_MARGIN_CHANGED);
break;
case LEFT_MARGIN_CHANGED:
UpdateView(LEFT_MARGIN_CHANGED);
break;
case RIGHT_MARGIN_CHANGED:
UpdateView(RIGHT_MARGIN_CHANGED);
break;
case BOTTOM_MARGIN_CHANGED:
UpdateView(BOTTOM_MARGIN_CHANGED);
break;
case MARGIN_UNIT_CHANGED: {
int32 marginUnit;
if (msg->FindInt32("marginUnit", &marginUnit) == B_OK)
_SetMarginUnit((MarginUnit)marginUnit);
} break;
default:
BView::MessageReceived(msg);
break;
}
ConstructGUI();
}
/*----------------- MarginView Public Methods --------------------*/
/**
* GetUnits
* PageSize
*
* @param none
* @return uint32 enum, units in inches, cm, points
* @return BPoint, contains actual point values of page in x, y of point
*/
MarginUnit
MarginView::GetMarginUnit(void) {
return fMarginUnit;
BPoint
MarginView::PageSize() const
{
return BPoint(fPageWidth, fPageHeight);
}
/**
* UpdateView, recalculate and redraw the view
*
* @param msg is a message to the calculate size to tell which field caused
* the update to occur, or it is a general update.
* @return void
*/
void
MarginView::UpdateView(uint32 msg)
{
Window()->Lock();
CalculateViewSize(msg); // nur Preview in Margins BBox!
Invalidate();
Window()->Unlock();
}
/**
* SetPageSize
@@ -238,25 +284,15 @@ MarginView::SetPageSize(float pageWidth, float pageHeight)
fPageHeight = pageHeight;
}
/**
* GetPageSize
*
* @param none
* @return BPoint, contains actual point values of page in x, y of point
*/
BPoint
MarginView::GetPageSize(void) {
return BPoint(fPageWidth, fPageHeight);
}
/**
* GetMargin
* Margin
*
* @param none
* @return rect, return margin values always in points
*/
BRect
MarginView::GetMargin(void)
MarginView::Margin() const
{
BRect margin;
@@ -267,8 +303,7 @@ MarginView::GetMargin(void)
float fbottom = atof(fBottom->Text());
// convert to units to points
switch (fMarginUnit)
{
switch (fMarginUnit) {
case kUnitInch:
// convert to points
ftop *= kInchUnits;
@@ -292,75 +327,42 @@ MarginView::GetMargin(void)
return margin;
}
/**
* MesssageReceived()
* Unit
*
* Receive messages for the view
* @param none
* @return uint32 enum, units in inches, cm, points
*/
MarginUnit
MarginView::Unit() const
{
return fMarginUnit;
}
/**
* UpdateView, recalculate and redraw the view
*
* @param BMessage* , the message being received
* @param msg is a message to the calculate size to tell which field caused
* the update to occur, or it is a general update.
* @return void
*/
void
MarginView::MessageReceived(BMessage *msg)
MarginView::UpdateView(uint32 msg)
{
switch (msg->what)
{
case CHANGE_PAGE_SIZE: {
float w;
float h;
msg->FindFloat("width", &w);
msg->FindFloat("height", &h);
SetPageSize(w, h);
UpdateView(MARGIN_CHANGED);
}
break;
case FLIP_PAGE: {
BPoint p;
p = GetPageSize();
SetPageSize(p.y, p.x);
UpdateView(MARGIN_CHANGED);
}
break;
case MARGIN_CHANGED:
UpdateView(MARGIN_CHANGED);
break;
case TOP_MARGIN_CHANGED:
UpdateView(TOP_MARGIN_CHANGED);
break;
case LEFT_MARGIN_CHANGED:
UpdateView(LEFT_MARGIN_CHANGED);
break;
case RIGHT_MARGIN_CHANGED:
UpdateView(RIGHT_MARGIN_CHANGED);
break;
case BOTTOM_MARGIN_CHANGED:
UpdateView(BOTTOM_MARGIN_CHANGED);
break;
case MARGIN_UNIT_CHANGED:
{
int32 marginUnit;
if (msg->FindInt32("marginUnit", &marginUnit) == B_OK) {
SetMarginUnit((MarginUnit)marginUnit);
}
}
break;
default:
BView::MessageReceived(msg);
break;
}
Window()->Lock();
_CalculateViewSize(msg); // only Preview in Margins BBox!
Invalidate();
Window()->Unlock();
}
/*----------------- MarginView Private Methods --------------------*/
/**
* ConstructGUI()
* _ConstructGUI()
*
* Creates the GUI for the View. MUST be called AFTER the View is attached to
* the Window, or will crash and/or create strange behaviour
@@ -369,16 +371,10 @@ MarginView::MessageReceived(BMessage *msg)
* @return void
*/
void
MarginView::ConstructGUI()
MarginView::_ConstructGUI()
{
BMessage *msg;
BString str;
BMenuItem *item;
BMenuField *mf;
BPopUpMenu *menu;
// Create text fields
msg = new BMessage(MARGIN_CHANGED);
// Create text fields
BRect r(Frame().Width() - be_plain_font->StringWidth("Top#") - kWidth,
kOffsetY, Frame().Width() - kOffsetX, kWidth);
@@ -390,7 +386,7 @@ MarginView::ConstructGUI()
fTop->SetModificationMessage(new BMessage(TOP_MARGIN_CHANGED));
fTop->SetDivider(be_plain_font->StringWidth("Top#"));
fTop->SetTarget(this);
AllowOnlyNumbers(fTop, kNumCount);
_AllowOnlyNumbers(fTop, kNumCount);
AddChild(fTop);
@@ -407,7 +403,7 @@ MarginView::ConstructGUI()
fLeft->SetModificationMessage(new BMessage(LEFT_MARGIN_CHANGED));
fLeft->SetDivider(be_plain_font->StringWidth("Left#"));
fLeft->SetTarget(this);
AllowOnlyNumbers(fLeft, kNumCount);
_AllowOnlyNumbers(fLeft, kNumCount);
AddChild(fLeft);
@@ -425,7 +421,7 @@ MarginView::ConstructGUI()
fBottom->SetDivider(be_plain_font->StringWidth("Bottom#"));
fBottom->SetTarget(this);
AllowOnlyNumbers(fBottom, kNumCount);
_AllowOnlyNumbers(fBottom, kNumCount);
AddChild(fBottom);
@@ -442,7 +438,7 @@ MarginView::ConstructGUI()
fRight->SetModificationMessage(new BMessage(RIGHT_MARGIN_CHANGED));
fRight->SetDivider(be_plain_font->StringWidth("Right#"));
fRight->SetTarget(this);
AllowOnlyNumbers(fRight, kNumCount);
_AllowOnlyNumbers(fRight, kNumCount);
AddChild(fRight);
@@ -451,27 +447,26 @@ MarginView::ConstructGUI()
r.OffsetBy(-kOffsetX, kOffsetY);
r.right += kOffsetY;
menu = new BPopUpMenu("units");
mf = new BMenuField(r, "units", "Units", menu,
B_FOLLOW_BOTTOM|B_FOLLOW_RIGHT);
BPopUpMenu *menu = new BPopUpMenu("units");
BMenuField *mf = new BMenuField(r, "units", "Units", menu,
B_FOLLOW_BOTTOM | B_FOLLOW_RIGHT);
mf->ResizeToPreferred();
mf->SetDivider(be_plain_font->StringWidth("Units#"));
BMenuItem *item;
// Construct menu items
for (int i=0; kUnitNames[i] != NULL; i++ )
{
msg = new BMessage(MARGIN_UNIT_CHANGED);
for (int32 i = 0; kUnitNames[i] != NULL; i++) {
BMessage *msg = new BMessage(MARGIN_UNIT_CHANGED);
msg->AddInt32("marginUnit", kUnitMsg[i]);
menu->AddItem(item = new BMenuItem(kUnitNames[i], msg));
item->SetTarget(this);
if (fMarginUnit == kUnitMsg[i]) {
if (fMarginUnit == kUnitMsg[i])
item->SetMarked(true);
}
}
AddChild(mf);
// calculate the sizes for drawing page view
CalculateViewSize(MARGIN_CHANGED);
_CalculateViewSize(MARGIN_CHANGED);
}
@@ -483,16 +478,16 @@ MarginView::ConstructGUI()
* @return void
*/
void
MarginView::AllowOnlyNumbers(BTextControl *textControl, int maxNum)
MarginView::_AllowOnlyNumbers(BTextControl *textControl, int32 maxNum)
{
BTextView *tv = textControl->TextView();
for (long i = 0; i < 256; i++) {
for (int32 i = 0; i < 256; i++)
tv->DisallowChar(i);
}
for (long i = '0'; i <= '9'; i++) {
for (int32 i = '0'; i <= '9'; i++)
tv->AllowChar(i);
}
tv->AllowChar(B_BACKSPACE);
tv->AllowChar('.');
tv->SetMaxBytes(maxNum);
@@ -505,10 +500,12 @@ MarginView::AllowOnlyNumbers(BTextControl *textControl, int maxNum)
* @return void
*/
void
MarginView::SetMargin(BRect margin) {
MarginView::_SetMargin(BRect margin)
{
fMargins = margin;
}
/**
* SetUnits, called by the MarginMgr when the units popup is selected
*
@@ -516,7 +513,7 @@ MarginView::SetMargin(BRect margin) {
* @return void
*/
void
MarginView::SetMarginUnit(MarginUnit unit)
MarginView::_SetMarginUnit(MarginUnit unit)
{
// do nothing if the current units are the same as requested
if (unit == fMarginUnit) {
@@ -603,12 +600,13 @@ MarginView::SetMarginUnit(MarginUnit unit)
fBottom->SetText(str.String());
// update UI
CalculateViewSize(MARGIN_CHANGED);
_CalculateViewSize(MARGIN_CHANGED);
Invalidate();
Window()->Unlock();
}
/**
* CalculateViewSize
*
@@ -621,7 +619,7 @@ MarginView::SetMarginUnit(MarginUnit unit)
* @return void
*/
void
MarginView::CalculateViewSize(uint32 msg)
MarginView::_CalculateViewSize(uint32 msg)
{
// determine page orientation
if (fPageHeight < fPageWidth) { // LANDSCAPE
@@ -664,8 +662,7 @@ MarginView::CalculateViewSize(uint32 msg)
BString str;
// Bounds calculation rules:
if (msg == TOP_MARGIN_CHANGED)
{
if (msg == TOP_MARGIN_CHANGED) {
// top must be <= bottom
if (ftop > (ph - fbottom)) {
ftop = ph - fbottom;
@@ -678,8 +675,7 @@ MarginView::CalculateViewSize(uint32 msg)
}
if (msg == BOTTOM_MARGIN_CHANGED)
{
if (msg == BOTTOM_MARGIN_CHANGED) {
// bottom must be <= pageHeight
if (fbottom > (ph - ftop)) {
fbottom = ph - ftop;
@@ -691,8 +687,7 @@ MarginView::CalculateViewSize(uint32 msg)
}
}
if (msg == LEFT_MARGIN_CHANGED)
{
if (msg == LEFT_MARGIN_CHANGED) {
// left must be <= right
if (fleft > (pw - fright)) {
fleft = pw - fright;
@@ -704,8 +699,7 @@ MarginView::CalculateViewSize(uint32 msg)
}
}
if (msg == RIGHT_MARGIN_CHANGED)
{
if (msg == RIGHT_MARGIN_CHANGED) {
// right must be <= fPageWidth
if (fright > (pw - fleft)) {
fright = pw - fleft;
@@ -724,5 +718,3 @@ MarginView::CalculateViewSize(uint32 msg)
fMargins.bottom = fbottom * pixelLength;
fMargins.left = fleft * pixelLength;
}