style/RoundedWidget.C

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Koen Deforche, Kessel-Lo, Belgium.
00003  *
00004  * See the LICENSE file for terms of use.
00005  */
00006 
00007 #include <WCssDecorationStyle>
00008 #include <WContainerWidget>
00009 #include <WImage>
00010 
00011 #include "RoundedWidget.h"
00012 #include "CornerImage.h"
00013 
00014 RoundedWidget::RoundedWidget(int corners, WContainerWidget *parent)
00015   : WCompositeWidget(parent),
00016     backgroundColor_(WColor(0xD4,0xDD,0xFF)),
00017     surroundingColor_(WColor(0xFF,0xFF,0xFF)),
00018     radius_(10),
00019     corners_(corners)
00020 {
00021   setImplementation(impl_ = new WContainerWidget());
00022   contents_ = new WContainerWidget(impl_);
00023 
00024   create();
00025 }
00026 
00027 RoundedWidget::~RoundedWidget()
00028 {
00029   if (images_[1])
00030     delete images_[1];
00031   if (images_[3])
00032     delete images_[3];
00033 }
00034 
00035 void RoundedWidget::create()
00036 {
00037   if (corners_ & TopLeft)
00038     images_[0] = new CornerImage(CornerImage::TopLeft, backgroundColor_,
00039                                  surroundingColor_, radius_);
00040   else
00041     images_[0] = 0;
00042 
00043   if (corners_ & TopRight)
00044     images_[1] = new CornerImage(CornerImage::TopRight, backgroundColor_,
00045                                  surroundingColor_, radius_);
00046   else
00047     images_[1] = 0;
00048 
00049   if (corners_ & BottomLeft)
00050     images_[2] = new CornerImage(CornerImage::BottomLeft, backgroundColor_,
00051                                  surroundingColor_, radius_);
00052   else
00053     images_[2] = 0;
00054 
00055   if (corners_ & BottomRight)
00056     images_[3] = new CornerImage(CornerImage::BottomRight, backgroundColor_,
00057                                  surroundingColor_, radius_);
00058   else
00059     images_[3] = 0;
00060 
00061   /*
00062    * At the top: an image (top left corner) inside
00063    * a container widget with background image top right.
00064    */
00065   top_ = new WContainerWidget();
00066   top_->resize(WLength(), WLength(radius_));
00067   if (images_[1])
00068     top_->decorationStyle().setBackgroundImage(images_[1]->imageRef(),
00069                                                WCssDecorationStyle::NoRepeat,
00070                                                Top | Right);
00071   if (images_[0])
00072     top_->addWidget(images_[0]);
00073   impl_->insertWidget(top_, contents_); // insert top before the contents
00074 
00075   /*
00076    * At the bottom: an image (bottom left corner) inside
00077    * a container widget with background image bottom right.
00078    */
00079   bottom_ = new WContainerWidget();
00080   bottom_->resize(WLength(), WLength(radius_));
00081   if (images_[3])
00082     bottom_->decorationStyle().setBackgroundImage(images_[3]->imageRef(),
00083                                                   WCssDecorationStyle::NoRepeat,
00084                                                   Bottom | Right);
00085   if (images_[2])
00086     bottom_->addWidget(images_[2]);
00087   impl_->addWidget(bottom_);
00088 
00089   decorationStyle().setBackgroundColor(backgroundColor_);
00090 
00091   contents_->setMargin(WLength(radius_), Left | Right);
00092 }
00093 
00094 void RoundedWidget::setBackgroundColor(WColor color)
00095 {
00096   backgroundColor_ = color;
00097   adjust();
00098 }
00099 
00100 void RoundedWidget::setCornerRadius(int radius)
00101 {
00102   radius_ = radius;
00103   adjust();
00104 }
00105 
00106 void RoundedWidget::adjust()
00107 {
00108   if (images_[0]) images_[0]->setRadius(radius_);
00109   if (images_[1]) images_[1]->setRadius(radius_);
00110   if (images_[2]) images_[2]->setRadius(radius_);
00111   if (images_[3]) images_[3]->setRadius(radius_);
00112 
00113   if (images_[0]) images_[0]->setForeground(backgroundColor_);
00114   if (images_[1]) images_[1]->setForeground(backgroundColor_);
00115   if (images_[2]) images_[2]->setForeground(backgroundColor_);
00116   if (images_[3]) images_[3]->setForeground(backgroundColor_);
00117 
00118   if (images_[1]) 
00119     top_->decorationStyle().setBackgroundImage(images_[1]->imageRef(),
00120                                                WCssDecorationStyle::NoRepeat,
00121                                                Top | Right);
00122   if (images_[3])
00123     bottom_->decorationStyle().setBackgroundImage(images_[3]->imageRef(),
00124                                                   WCssDecorationStyle::NoRepeat,
00125                                                   Bottom | Right);
00126 
00127   top_->resize(WLength(), WLength(radius_));
00128   bottom_->resize(WLength(), WLength(radius_));
00129   contents_->setMargin(WLength(radius_), Left | Right);
00130 
00131   decorationStyle().setBackgroundColor(backgroundColor_);
00132 }

Generated on Sun Jul 1 19:37:23 2007 for Wt by doxygen 1.4.7