Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

CSButton.cpp

Go to the documentation of this file.
00001 #ifdef WIN32
00002 #pragma warning(disable : 4786 )
00003 #endif
00004 
00005 #include "CSButton.h"
00006 #include "CSLAF.h"
00007 
00008 const char *CSButton::CLASS = "CSButton";
00009 const char *CSDragButton::CLASS = "CSDragButton";
00010 
00011 // button frees Icon!
00012 CSButton::CSButton(CSIcon *icon) : CSGrafikElement(0, 0)
00013 {
00014     static char *functionName="CSButton";
00015     LOG_ENTER 
00016     mIcon = icon;
00017     initButton();
00018     LOG_EXIT
00019 }
00020 
00021 CSButton::CSButton(const std::string &text)  : CSGrafikElement(0, 0)
00022 {
00023     static char *functionName="CSButton";
00024     LOG_ENTER 
00025     mIcon = CSTextIcon::buildIcon(text);
00026     initButton();
00027 
00028     LOG_EXIT
00029 }
00030 
00031 CSButton::~CSButton()
00032 {
00033     if (mIcon)
00034     {
00035         delete(mIcon);
00036         mIcon = 0;
00037     }
00038 }
00039 
00040 void CSButton::initButton()
00041 {
00042     // Button layout
00043     getLayoutDataInternal()->setPackedHorizontal(true);
00044     getLayoutDataInternal()->setPackedVertical(true);
00045     getLayoutDataInternal()->setCenteredHorizontal(true);
00046     getLayoutDataInternal()->setCenteredVertical(true);
00047 
00048     
00049     setLayoutManager(new CSLayoutManagerStackVertical());
00050 
00051     // Icon layout
00052     CSLayoutData layoutData = CSLayoutData(POSITION_CENTER, true);
00053 
00054     if (mIcon)
00055     {
00056         layoutData.setPackedHorizontal(true);
00057         layoutData.setPackedVertical(true);
00058         layoutData.setCenteredHorizontal(true);
00059         layoutData.setCenteredVertical(true);
00060         layoutData.setSpacing(0,0);
00061         mIcon->setLayoutData(layoutData);
00062 
00063         addElement(mIcon);
00064         mIcon->addMessageListener(this, GUI_MESSAGE);
00065     }
00066     mBorderState = BUTTON_DEFAULT;
00067     MESSAGE_BUTTON_PRESSED.setSubtype(BUTTON_PRESSED_MESSAGE);
00068     MESSAGE_BUTTON_RELEASED.setSubtype(BUTTON_RELEASED_MESSAGE);
00069     layoutSetup();
00070     getLayoutManager()->pack(this);
00071 }
00072 
00073 void CSButton::reactOnMessage(CSMessage *message)
00074 {
00075     reactOnMessageButton(message);
00076 }
00077 
00078 void CSButton::reactOnMessageButton(CSMessage *message)
00079 {
00080     static char *functionName="reactOnMessage";
00081     if (message->mIsHandled)
00082     {
00083         reactOnMessageGrafikElement(message);
00084         return;
00085     }
00086     if (message->getType() == GUI_MESSAGE)
00087     {
00088         GuiMessage *gm = (GuiMessage *) message;
00089         
00090         if (isChild(gm->receiver))
00091         {
00092             switch (message->getSubtype())
00093             {
00094                 case MOUSE_BUTTON_RELEASED_MESSAGE:
00095                 {
00096                     mBorderState = getButtonState() & (~BUTTON_PRESSED);
00097                     MESSAGE_BUTTON_RELEASED.actionId = mActionId;
00098                     MESSAGE_BUTTON_RELEASED.receiver = 0;
00099                     MESSAGE_BUTTON_RELEASED.mIsHandled = false;
00100                     sendMessage(MESSAGE_BUTTON_RELEASED);
00101                     gm->mIsHandled = true;
00102                     break;
00103                 }
00104 
00105                 case MOUSE_BUTTON_PRESSED_MESSAGE:
00106                 {
00107                     if (getButtonState() & BUTTON_ENABLED)
00108                     {
00109                         mBorderState = getButtonState() | BUTTON_PRESSED;
00110                         MESSAGE_BUTTON_PRESSED.actionId = mActionId;
00111                         MESSAGE_BUTTON_PRESSED.receiver = 0;
00112                         MESSAGE_BUTTON_PRESSED.mIsHandled = false;
00113                         sendMessage(MESSAGE_BUTTON_PRESSED);
00114                     }
00115                     gm->mIsHandled = true;
00116                     break;
00117                 }
00118             }
00119         }
00120         else
00121         {
00122             // if somewhere else released
00123             // at least reset the state of the button
00124             // (but don't tell anyone about it)
00125             if (message->getSubtype() ==  MOUSE_BUTTON_RELEASED_MESSAGE)
00126             {
00127                 mBorderState = getButtonState() & (~BUTTON_PRESSED);
00128                 gm->mIsHandled = true;
00129             }
00130         }
00131     }
00132     reactOnMessageGrafikElement(message);
00133 }
00134 
00135 void CSButton::layoutSetupButton()
00136 {
00137     static char *functionName="layoutSetupButton";
00138     LOG_ENTER 
00139     if (mIcon)
00140     {
00141         mMinHeight = mIcon->getMinHeight();
00142         mMinWidth = mIcon->getMinWidth();
00143     }
00144     else
00145     {
00146         mMinHeight = 0;
00147         mMinWidth = 0;
00148     }
00149     LOG_EXIT
00150 }
00151 
00152 // button frees Icon!
00153 CSDragButton::CSDragButton(CSIcon *icon) : CSButton(icon)
00154 {
00155     static char *functionName="CSDragButton";
00156     LOG_ENTER 
00157     mDragging=false;
00158     MESSAGE_BUTTON_DRAGGED.setSubtype(BUTTON_DRAGGED_MESSAGE);
00159     getLayoutDataInternal()->setPackedHorizontal(false);
00160     getLayoutDataInternal()->setPackedVertical(false);
00161     LOG_EXIT
00162 }
00163 
00164 void CSDragButton::reactOnMessage(CSMessage *message)
00165 {
00166     reactOnMessageDragButton(message);
00167 }
00168 
00169 void CSDragButton::reactOnMessageDragButton(CSMessage *message)
00170 {
00171     static char *functionName="reactOnMessage";
00172     if (message->mIsHandled)
00173     {
00174         if (message->getType() == GUI_MESSAGE)
00175         {
00176             GuiMessage *gm = (GuiMessage *) message;
00177             if (message->getSubtype() ==  MOUSE_BUTTON_RELEASED_MESSAGE)
00178             {
00179                 mDragging = false;
00180                 gm->mIsHandled = false;
00181             }
00182         }
00183         reactOnMessageButton(message);
00184         return;
00185     }
00186     if (message->getType() == GUI_MESSAGE)
00187     {
00188         GuiMessage *gm = (GuiMessage *) message;
00189         if (message->getSubtype() ==  MOUSE_MOTION_MESSAGE)
00190         {
00191             if (mDragging)
00192             {
00193                 int deltaX = gm->screenX - mDragStartX;
00194                 int deltaY = gm->screenY - mDragStartY;
00195 
00196                 MESSAGE_BUTTON_DRAGGED.screenX = gm->screenX;
00197                 MESSAGE_BUTTON_DRAGGED.screenY = gm->screenY;
00198                 MESSAGE_BUTTON_DRAGGED.deltaX = deltaX;
00199                 MESSAGE_BUTTON_DRAGGED.deltaY = deltaY;
00200                 MESSAGE_BUTTON_DRAGGED.actionId = mActionId;
00201                 MESSAGE_BUTTON_DRAGGED.mIsHandled = false;
00202                 MESSAGE_BUTTON_DRAGGED.receiver = 0;
00203                 sendMessage(MESSAGE_BUTTON_DRAGGED);
00204 
00205                 mDragStartX = MESSAGE_BUTTON_DRAGGED.deltaX;
00206                 mDragStartY = MESSAGE_BUTTON_DRAGGED.deltaY;
00207             }
00208             gm->mIsHandled = true;
00209         }
00210         if (message->getSubtype() ==  MOUSE_BUTTON_RELEASED_MESSAGE)
00211         {
00212             mDragging = false;
00213             gm->mIsHandled = false;
00214         }
00215         if ((gm->receiver == this) || ( gm->receiver == mIcon))
00216         {
00217             switch (message->getSubtype())
00218             {
00219                 case MOUSE_BUTTON_PRESSED_MESSAGE:
00220                 {
00221                     mDragging = true;
00222                     mDragStartX = gm->screenX;
00223                     mDragStartY = gm->screenY;
00224                     gm->mIsHandled = true;
00225                     break;
00226                 }
00227             }
00228         }
00229     }
00230     reactOnMessageButton(message);
00231 }

Generated on Wed Jul 14 00:43:29 2004 for CSLib by doxygen 1.3.6