bars.cxx
上传用户:hzhsqp
上传日期:2007-01-06
资源大小:1600k
文件大小:16k
- /*
- * bars.cxx
- *
- * Tool bar classes.
- *
- * Portable Windows Library
- *
- * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
- *
- * The contents of this file are subject to the Mozilla Public License
- * Version 1.0 (the "License"); you may not use this file except in
- * compliance with the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS"
- * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
- * the License for the specific language governing rights and limitations
- * under the License.
- *
- * The Original Code is Portable Windows Library.
- *
- * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
- *
- * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
- * All Rights Reserved.
- *
- * Contributor(s): ______________________________________.
- *
- * $Log: bars.cxx,v $
- * Revision 1.43 1998/12/20 09:14:39 robertj
- * Added pragma implementation for GNU compiler.
- *
- * Revision 1.42 1998/11/30 04:52:14 robertj
- * New directory structure
- *
- * Revision 1.41 1998/10/13 14:06:35 robertj
- * Complete rewrite of memory leak detection code.
- *
- * Revision 1.40 1998/09/23 06:29:35 robertj
- * Added open source copyright license.
- *
- * Revision 1.39 1996/07/15 10:34:03 robertj
- * Fixed uninitialised variable.
- *
- * Revision 1.38 1996/06/10 11:28:27 robertj
- * Changes for better C++ compatibility.
- *
- * Revision 1.37 1996/04/30 12:33:48 robertj
- * Changed "inPixels" boolean to enum for three coordinate systems.
- *
- * Revision 1.36 1995/10/14 15:06:56 robertj
- * Fixed bug in changing fonts on status bar.
- *
- * Revision 1.35 1995/08/12 22:32:43 robertj
- * Fixed setting of foreground/background colours with adjusted DrawBevelledRect().
- *
- * Revision 1.34 1995/07/31 12:13:01 robertj
- * Added balloon help string to toolbar button codes.
- *
- * Revision 1.33 1995/06/04 12:38:24 robertj
- * Redesign to add vertical tool bars and control wrapping.
- *
- * Revision 1.32 1995/04/25 11:28:13 robertj
- * Fixed Borland compiler warnings.
- *
- * Revision 1.31 1995/04/03 11:21:31 robertj
- * Fixed SetSectionText.
- *
- * Revision 1.30 1995/04/02 09:27:26 robertj
- * Added "balloon" help.
- *
- * Revision 1.29 1995/03/22 13:50:58 robertj
- * Split SetText() to separate function for SetSectionText() to guarentee that
- * the parameters can be resolved.
- *
- * Revision 1.28 1995/02/19 04:19:19 robertj
- * Added dynamically linked command processing.
- *
- * Revision 1.27 1995/01/21 05:20:54 robertj
- * Allowed callback functions on button bar buttons.
- * Made disabled icon default to enabled icon.
- *
- * Revision 1.26 1995/01/18 09:04:23 robertj
- * Added initialiser arrays for button bar and status bar.
- *
- * Revision 1.25 1995/01/15 04:53:08 robertj
- * Fixed problems with stdarg and PString parameter on GNU compiler.
- *
- * Revision 1.24 1995/01/10 11:44:12 robertj
- * Removed PString parameter in stdarg function for GNU C++ compatibility.
- *
- * Revision 1.23 1995/01/06 10:43:55 robertj
- * Changed PRealFont usage from pointer to reference.
- *
- * Revision 1.22 1994/12/14 11:17:14 robertj
- * Changed PDIMENSION to be unsigned causing untold number of changes.
- *
- * Revision 1.21 1994/11/28 23:30:21 robertj
- * Added Update() call whenever a status bar section text is changed.
- *
- * Revision 1.21 1994/11/28 12:44:37 robertj
- * Added Update() after a change of status bar text to guarentee that it is visible at
- * the time of setting. But only if the string changes!
- *
- * Revision 1.20 1994/10/30 11:47:10 robertj
- * Changed mechanism for doing notification callback functions.
- *
- * Revision 1.19 1994/08/21 23:43:02 robertj
- * Changed parameter before variable argument list to NOT be a reference.
- *
- * Revision 1.18 1994/08/15 07:02:42 robertj
- * Optimised status bar with transparent text display.
- *
- * Revision 1.17 1994/08/01 03:41:24 robertj
- * Use of PNEW instead of new for heap debugging. Need undef for Unix end.
- *
- * Revision 1.16 1994/07/27 05:58:07 robertj
- * Synchronisation.
- *
- * Revision 1.15 1994/06/25 11:55:15 robertj
- * Unix version synchronisation.
- *
- * Revision 1.14 1994/04/20 12:17:44 robertj
- * assert changes
- *
- * Revision 1.13 1994/04/01 14:00:00 robertj
- * Fixed SetDimensions change bug
- * ..
- *
- * Revision 1.12 1994/03/07 07:47:00 robertj
- * Major upgrade
- *
- * Revision 1.11 1994/01/15 02:52:02 robertj
- * Mac compatibilty changes (no nested classes).
- *
- * Revision 1.10 1994/01/13 03:36:48 robertj
- * Created intermediate class PInteractorLayout for dialog-ish windows.
- *
- * Revision 1.9 1994/01/03 10:17:10 robertj
- * Changed IsAncestor mechanism.
- * ..
- *
- * Revision 1.8 1993/12/31 06:49:35 robertj
- * Added PImgIcon class.
- *
- * Revision 1.7 1993/12/04 05:21:59 robertj
- * Moved code from pwlib.inl
- *
- * Revision 1.6 1993/12/01 16:09:05 robertj
- * Windows NT port.
- *
- * Revision 1.5 1993/11/20 17:26:28 robertj
- * Change of button picture from PImage to PPixels
- *
- * Revision 1.4 1993/08/27 18:17:47 robertj
- * Fixed bars code to use the new canvas function for drawing bevelled areas.
- *
- * Revision 1.3 1993/08/21 17:09:50 robertj
- * Added checking of enabled menu in button bar selections.
- * Fixed problem with G++ compatibility in temprary string object creation.
- *
- * Revision 1.2 1993/08/21 01:50:33 robertj
- * Made Clone() function optional, default will assert if called.
- *
- * Revision 1.1 1993/08/21 01:07:35 robertj
- * Initial revision
- *
- */
- #ifdef __GNUC__
- #pragma implementation "toolbars.h"
- #endif
- #include <pwlib.h>
- #include <pwclib/toolbars.h>
- #define new PNEW
- //////////////////////////////////////////////////////////////////////////////
- // PToolBar
- PToolBar::PToolBar(PInteractor * parent, BOOL vertical, BOOL autoWrap)
- : PInteractorLayout(parent),
- positionDown(vertical),
- wrapControls(autoWrap)
- {
- margin = owner->GetBorderSize()*3;
- if (positionDown)
- margin.SetHeight(margin.Height()*2);
- else
- margin.SetWidth(margin.Width()*2);
- SetForegroundColour(owner->GetButtonFgColour());
- SetBackgroundColour(owner->GetButtonBkColour());
- }
- void PToolBar::OnRedraw(PCanvas & canvas)
- {
- canvas.SetMappingRect(canvas.GetViewportRect());
- PRect bounds = canvas.GetDrawingBounds();
- canvas.DrawBevelledRect(bounds, TRUE, FALSE);
- }
- PBalloon * PToolBar::OnBalloonHelp()
- {
- return owner->DoBalloonHelp(NULL, 2);
- }
- void PToolBar::AddControl(PControl * control,
- PDIMENSION gapSize, PDIMENSION elasticity)
- {
- item.Append(new BarItem(PAssertNULL(control), gapSize, elasticity));
- PDim myDim = GetDimensions(PixelCoords);
- PDim ctlDim = control->GetDimensions(PixelCoords);
- ctlDim += margin*2;
- if (ctlDim.Width() > myDim.Width())
- myDim.SetWidth(ctlDim.Width());
- if (ctlDim.Height() > myDim.Height())
- myDim.SetHeight(ctlDim.Height());
- SetDimensions(myDim.Width(), myDim.Height(), PixelCoords);
- }
- void PToolBar::_SetDimensions(PDIMENSION width, PDIMENSION height,
- CoordinateSystem coords)
- {
- PInteractorLayout::_SetDimensions(width, height, coords);
- DoControlGeometry();
- }
- void PToolBar::DoControlGeometry()
- {
- PDim myDim = GetDimensions(PixelCoords);
- PDIMENSION barSize = positionDown ? myDim.Height() : myDim.Width();
- PDIMENSION marginSize = positionDown ? margin.Height() : margin.Width();
- PDIMENSION maxSize = 0;
- PDIMENSION fixedSize = 0;
- PDIMENSION propTotal = 0;
- PINDEX i;
- for (i = 0; i < item.GetSize(); i++) {
- PDim ctlDim = item[i].control->GetDimensions(PixelCoords);
- if (item[i].elasticity > 0)
- propTotal += item[i].elasticity;
- else
- fixedSize += positionDown ? ctlDim.Height() : ctlDim.Width();
- fixedSize += item[i].gapSize*marginSize;
- if (positionDown) {
- if (maxSize < ctlDim.Width())
- maxSize = ctlDim.Width();
- }
- else {
- if (maxSize < ctlDim.Height())
- maxSize = ctlDim.Height();
- }
- }
- PDIMENSION elasticUnit = 0;
- if (propTotal > 0) {
- elasticUnit = (barSize - fixedSize - marginSize*2)/propTotal;
- if (elasticUnit < 10)
- elasticUnit = 10;
- }
- PPoint ctlPos = margin;
- for (i = 0; i < item.GetSize(); i++) {
- PDim ctlDim = item[i].control->GetDimensions(PixelCoords);
- if (item[i].elasticity > 0) {
- if (positionDown)
- ctlDim.SetHeight(item[i].elasticity*elasticUnit);
- else
- ctlDim.SetWidth(item[i].elasticity*elasticUnit);
- item[i].control->SetDimensions(ctlDim, PixelCoords);
- }
- if (wrapControls) {
- if (positionDown) {
- if (ctlPos.Y() + ctlDim.Height() > myDim.Height()) {
- ctlPos.SetX(maxSize + margin.Width());
- ctlPos.SetY(margin.Height());
- }
- }
- else {
- if (ctlPos.X() + ctlDim.Width() > myDim.Width()) {
- ctlPos.SetX(margin.Width());
- ctlPos.SetY(maxSize + margin.Height());
- }
- }
- }
- item[i].control->SetPosition(ctlPos, TopLeftPixels, TopLeftPixels);
- if (positionDown)
- ctlPos.SetY(ctlPos.Y() + ctlDim.Height() + item[i].gapSize*marginSize);
- else
- ctlPos.SetX(ctlPos.X() + ctlDim.Width() + item[i].gapSize*marginSize);
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- // PButtonBar
- PButtonBar::PButtonBar(PInteractor *parent,
- const ButtonID *IDs, PINDEX numIDs, BOOL vertical, BOOL autoWrap)
- : PToolBar(parent, vertical, autoWrap)
- {
- AddButtons(IDs, numIDs);
- }
- void PButtonBar::AddButtons(const ButtonID * IDs, PINDEX numIDs)
- {
- // Calculate the maximum button size so far in bar.
- PDIMENSION max_x = 0;
- PDIMENSION max_y = 0;
- PINDEX i;
- for (i = 0; i < GetNumChildren(); i++) {
- if (children[i].IsDescendant(PImageButton::Class())) {
- PDim dim = children[i].GetDimensions(PixelCoords);
- if (max_x < dim.Width())
- max_x = dim.Width();
- if (max_y < dim.Height())
- max_y = dim.Height();
- }
- }
- // Add in the new buttons, increasing the maximum size
- for (i = 0; i < numIDs; i++) {
- if (IDs[i].commandName != NULL || IDs[i].menuItem != 0) {
- PRESOURCE_ID img1 = IDs[i].enabledImage;
- if (img1 == 0)
- img1 = IDs[i].menuItem;
- PRESOURCE_ID img2 = IDs[i].disabledImage;
- if (img2 == 0)
- img2 = img1;
- PPushButton * button;
- if (img1 != 0)
- button = new PImageButton(this, PImgIcon(img1), PImgIcon(img2));
- else
- button = new PImageButton(this);
- button->SetControlID(IDs[i].menuItem);
- if (IDs[i].commandName != NULL)
- button->SetNotifier(PCREATE_COMMAND(IDs[i].commandName), TRUE);
- PRESOURCE_ID help = IDs[i].balloonHelp;
- if (help == 0)
- help = IDs[i].menuItem;
- if (help != 0) {
- PResourceString str(help);
- button->SetBalloonHelp(str);
- }
- button->Show();
- PDim dim = button->GetDimensions(PixelCoords);
- if (max_x < dim.Width())
- max_x = dim.Width();
- if (max_y < dim.Height())
- max_y = dim.Height();
- }
- }
- for (i = 0; i < numIDs; i++) {
- if (IDs[i].commandName != NULL || IDs[i].menuItem != 0) {
- PControl * button = GetControl(IDs[i].menuItem);
- PAssertNULL(button)->SetDimensions(max_x, max_y, PixelCoords);
- int gaps = 1;
- while (i+gaps < numIDs &&
- (IDs[i+gaps].commandName == NULL && IDs[i+gaps].menuItem == 0))
- gaps++;
- AddControl(button, gaps-1);
- }
- }
- }
- void PButtonBar::OnControlNotify(PControl & control, int code)
- {
- PInteractorLayout::OnControlNotify(control, code);
- if (control.GetNotifier().IsNULL()) {
- PInteractor * p = parent;
- while (p->GetParent() != NULL)
- p = p->GetParent();
- PTopLevelWindow * mainWindow = (PTopLevelWindow *)p;
- mainWindow->OnMenuStartSelect();
- PRootMenu * menu = mainWindow->GetMenu();
- if (menu != NULL) {
- PMenuItem * item = menu->GetItemFromKey(control.GetControlID());
- if (item != NULL && item->IsEnabled())
- mainWindow->OnMenuItemSelect(*item);
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- // PStatusBar
- PStatusBar::PStatusBar(PInteractor * parent, PINDEX numSections)
- : PToolBar(parent)
- {
- PDIMENSION width =
- parent->GetDimensions(PixelCoords).Width()/numSections - margin.Width();
- PDIMENSION height = font.GetHeight(TRUE) + margin.Height();
- for (PINDEX i = 0; i < numSections; i++) {
- Section * s = new Section(this);
- s->SetDimensions(width, height, PixelCoords);
- AddControl(s, i < numSections-1, 1);
- }
- }
- void PStatusBar::SetFont(const PFont & newFont, BOOL toChildren)
- {
- PToolBar::SetFont(newFont, toChildren);
- PDIMENSION height = font.GetHeight(TRUE) + margin.Height();
- for (PINDEX i = 0; i < item.GetSize(); i++)
- item[i].control->SetDimensions(1, height, PixelCoords);
- DoControlGeometry();
- height += margin.Height()*2;
- SetDimensions(GetDimensions(ScreenCoords).Width(), height, PixelCoords);
- }
- void PStatusBar::SetText(const PString & str)
- {
- SetSectionText(0, str);
- }
- void PStatusBar::SetText(PRESOURCE_ID resId, ...)
- {
- va_list args;
- va_start(args, resId);
- PString str;
- PResourceString fmt(resId);
- str.vsprintf(fmt, args);
- SetSectionText(0, str);
- }
- void PStatusBar::SetText(const char * fmt, ...)
- {
- va_list args;
- va_start(args, fmt);
- PString str;
- str.vsprintf(fmt, args);
- SetSectionText(0, str);
- }
- void PStatusBar::SetSectionText(PINDEX section, const PString & str)
- {
- Section & s = GetSection(section);
- if (s.GetName() != str) {
- s.SetName(str);
- s.Update();
- }
- }
- void PStatusBar::SetSectionText(PINDEX section, PRESOURCE_ID resId, ...)
- {
- va_list args;
- va_start(args, resId);
- PResourceString fmt(resId);
- SetSectionText(section, pvsprintf(fmt, args));
- }
- void PStatusBar::SetSectionText(PINDEX section, const char * fmt, ...)
- {
- va_list args;
- va_start(args, fmt);
- SetSectionText(section, pvsprintf(fmt, args));
- }
- void PStatusBar::SetSectionAlignment(PINDEX section,
- PCanvas::DrawStringOptions align)
- {
- GetSection(section).SetAlignment(align);
- }
- void PStatusBar::SetSectionWidth(PINDEX section, const PString & longestString)
- {
- SetSectionWidth(section,
- PDrawCanvas(this).MeasureString(longestString).Width());
- }
- void PStatusBar::SetSectionWidth(PINDEX section,
- const PRESOURCE_ID * strIDs, PINDEX nStrings)
- {
- PDrawCanvas canvas(this);
- PDIMENSION max = 0;
- for (PINDEX i = 0; i < nStrings; i++) {
- PDIMENSION width=canvas.MeasureString(PResourceString(strIDs[i])).Width();
- if (max < width)
- max = width;
- }
- SetSectionWidth(section, max);
- }
- void PStatusBar::SetSectionWidth(PINDEX section, int width)
- {
- Section & s = GetSection(section);
- if (width > 0)
- s.SetDimensions(width, s.GetDimensions(LocalCoords).Height(), LocalCoords);
- item[section].elasticity = width < 0 ? -width : 0;
- SetDimensions(GetDimensions(LocalCoords), LocalCoords);
- }
- PStatusBar::Section & PStatusBar::GetSection(PINDEX section) const
- {
- PInteractor & s = *item[section].control;
- PAssert(s.IsDescendant(Section::Class()), PInvalidParameter);
- return (Section &)s;
- }
- PStatusBar::Section::Section(PInteractor * parent)
- : PStaticText(parent)
- {
- SetBackgroundColour(owner->GetButtonBkColour());
- Show();
- }
- void PStatusBar::Section::OnRedraw(PCanvas & canvas)
- {
- canvas.SetMappingRect(canvas.GetViewportRect());
- PRect bounds = canvas.GetDrawingBounds();
- canvas.DrawBevelledRect(bounds, FALSE, FALSE);
- bounds.Inflate(-1, -1);
- canvas.SetTextFgColour(owner->GetButtonFgColour());
- canvas.SetTextBkColour(PColour(0,0,0,0));
- canvas.DrawString(bounds, GetName(), alignment);
- }
- // End Of File ///////////////////////////////////////////////////////////////