Advertisement
cgrunwald

Untitled

Dec 15th, 2010
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 27.27 KB | None | 0 0
  1. //
  2. // C++ Implementation: j_window
  3. //
  4. // Description:
  5. //
  6. //
  7. // Author: Charles Grunwald (Juntalis) <cgrunwald@gmail.com>, (C) 2009
  8. //
  9. // Copyright: See COPYING file that comes with this distribution
  10. //
  11. //
  12. #include "j_window.h"
  13. #include "j_parentcontainer.h"
  14. #include <guichan/graphics.hpp>
  15. #include <guichan/image.hpp>
  16. #include <cmath>
  17. #include <guichan/font.hpp>
  18.  
  19. using namespace std;
  20. /*
  21. // Callback for the Minimize (shade) Button.
  22. void minCall(gcn::ActionEvent actionEvent, void * inf)
  23. {
  24.     // First things first; we want to get all info associated with the callback.
  25.     // We are able to get the ParentContainer and the jWindow used.
  26.     // We don't really need the parent container, but if we wanted it, we would do :
  27.     // ParentContainer * pSource = ((ParentContainer *)(((gcn::ImageButton * )(actionEvent.getSource()))->getParent()));
  28.     jWindow * pWindow = (jWindow *)inf;
  29.  
  30.     // As usual, simple debugging.
  31.     #ifdef DEBUG
  32.             std::cout << " " << pWindow->getId() << "-> " << actionEvent.getSource()->getId() << " pressed.\n";
  33.     #endif
  34.  
  35.     // Check if window is minimized.
  36.     if  (!pWindow->isMinimized())
  37.     {
  38.         // If the window is not minimized, we must store the current height for later use.
  39.         pWindow->setDHeight (pWindow->getHeight());
  40.         // We will then shrink the widget into its titlebar.
  41.         pWindow->setHeight  (pWindow->getTitleBarHeight());
  42.         //Finally, we let the window know it is now minimized.
  43.         pWindow->setMinimized(true);
  44.     }
  45.     else
  46.     {
  47.         // Simply set the window back to its default state.
  48.         pWindow->setHeight  (pWindow->getDHeight());
  49.         // Let the window know that it is no longer minimized.
  50.         pWindow->setMinimized(false);
  51.     }
  52.     #ifdef DEBUG
  53.         std::cout << " " << pWindow->getId() << "->setMinimized(" << pWindow->isMinimized() << ")\n";
  54.     #endif
  55. }
  56.  
  57. // Callback for the X (Close) Button
  58. void xCall(gcn::ActionEvent actionEvent, void * inf)
  59. {
  60.     // First things first; we want to get all info associated with the callback.
  61.     // We are able to get the ParentContainer and the jWindow used.
  62.     ParentContainer * pSource = ((ParentContainer *)(((gcn::Button * )(actionEvent.getSource()))->getParent()));
  63.     jWindow * pWindow = (jWindow *)inf;
  64.  
  65.     #ifdef DEBUG
  66.         std::cout << " " << pWindow->getId() << "-> " << actionEvent.getSource()->getId() << " pressed.\n";
  67.     #endif
  68.  
  69.     // We have to remove the control box.
  70.     pSource->remove(pWindow->getBMin());
  71.     pSource->remove(pWindow->getBX());
  72.     // And now we call the deconstructor for the class.
  73.     delete pWindow;
  74. }
  75. */
  76.  
  77. void jWindow::Setup(std::string Id, CONTROLBOX cb)
  78. {
  79.     // We start off by setting the Id.
  80.     jWindow::setId(Id);
  81.     #ifdef DEBUG
  82.         std::cout << " " << getId() << "-> Constructor called.\n";
  83.     #endif
  84.  
  85.     /* * * Control box shit * * */
  86.     //Next, we're going to set the control box up.
  87.     jWindow::setControlBox(cb);
  88.  
  89.     setMinimized(false);
  90.     /* * * /Control box shit * * */
  91.     // I also need to make sure that the window knows that it's not minimized.
  92.     /* Set Defaults */
  93.     jWindow::bFormImage = false;
  94.     jWindow::bTitleImage = false;
  95.     jWindow::setAlignment(gcn::Graphics::LEFT);
  96.     jWindow::setWindowAlpha(255);
  97.     jWindow::setAlwaysOnTop(false);
  98.     jWindow::setSnapToEdges(false);
  99.     jWindow::setTitleBarHeight(20);
  100.     jWindow::setTitleBarAppearance(RAISED);
  101.     jWindow::setFormAppearance(RAISED);
  102.     jWindow::setTitlebarColor(gcn::Color(175,175,175));
  103.     jWindow::setFormBackground(gcn::Color(125,125,125));
  104.     jWindow::setTitleBarTextColor(gcn::Color(0,0,0));
  105.     jWindow::setFormBorder(gcn::Color(200,200,200,255));
  106.     jWindow::setSize(300,300);
  107.     xMouseDown = false;
  108.     minMouseDown = false;
  109.     bResizable = false;
  110.     /* /Defaults */
  111. }
  112.  
  113. // Deconstructor
  114. jWindow::~jWindow()
  115. {
  116.     #ifdef DEBUG
  117.         std::cout << " " << getId() << "-> Deconstructor called.\n";
  118.     #endif
  119.         ((ParentContainer*)getParent())->remove(this);
  120. }
  121.  
  122. /***********************
  123. ******* WRAPPERS *******
  124. ***********************/
  125.  
  126. void jWindow::add(gcn::Widget *widget)
  127. {
  128.     gcn::Window::add(widget);
  129.     #ifdef DEBUG
  130.         if (strlen(widget->getId().c_str()))
  131.             std::cout << "  " << getId() << "-> " << widget->getId() << " added.\n";
  132.         else
  133.             std::cout << "  " << getId() << "-> Widget added.\n";
  134.     #endif
  135. }
  136.  
  137.  
  138. void jWindow::add(gcn::Widget *widget, int x, int y)
  139. {
  140.     gcn::Window::add(widget,x,y);
  141.     #ifdef DEBUG
  142.         if (strlen(widget->getId().c_str()))
  143.             std::cout << "  " << getId() << "-> " << widget->getId() << " added.\n";
  144.         else
  145.             std::cout << "  mImages[1]" << getId() << "-> Widget added.\n";
  146.     #endif
  147. }
  148.  
  149. void jWindow::draw(gcn::Graphics *graphics)
  150. {
  151.     jWindow::drawForm(graphics);
  152.     jWindow::drawTitleBar(graphics);
  153. }
  154.  
  155.  
  156. void jWindow::logic()
  157. {
  158.         // I want to make sure it stays inside the window.
  159.         if (getY() < 0) setY(0);
  160.         if (getY() + getHeight() > getParent()->getHeight()) setY(getParent()->getHeight() - getHeight());
  161.         if (getX() < 0) setX(0);
  162.         if (getX() + getWidth() > getParent()->getWidth()) setX(getParent()->getWidth() - getWidth());
  163.  
  164.         // Autosnap.
  165.         if (bSnapToEdges)
  166.         {
  167.             if(getY() <= iSnaptoEdges)
  168.                 setY(0);
  169.             else if ((getY()+getHeight()) >= (getParent()->getHeight() - iSnaptoEdges))
  170.                 setY(getParent()->getHeight() - getHeight());
  171.             if(getX() <= iSnaptoEdges)
  172.                 setX(0);
  173.             else if ((getX()+getWidth()) >= (getParent()->getWidth() - iSnaptoEdges))
  174.                 setX(getParent()->getWidth() - getWidth());
  175.         }
  176.         // Of course we want to call the window logic, as usual.
  177.         gcn::Window::logic();
  178.         // For starters, I'd like the controlbox to match the jWindow..
  179.         rectMinButton.height = ((int)floor(getTitleBarHeight() * 0.8));
  180.         rectMinButton.width = rectMinButton.height;
  181.         rectXButton.height = rectMinButton.height;
  182.         rectXButton.width = rectMinButton.height;
  183.         rectMinButton.y = ((int)floor((getTitleBarHeight() - rectMinButton.height)/ 2));
  184.         rectXButton.y = rectMinButton.y;
  185.         switch(mControlBox)
  186.         {
  187.             case NONE:
  188.                 break;
  189.             case MINBOX:
  190.                 rectMinButton.x = (getWidth() - 1 - (int)floor((getTitleBarHeight() - rectMinButton.height)/ 2)) - rectMinButton.width;
  191.                 ControlBoxWidth = rectXButton.width;
  192.                 break;
  193.             case XBOX:
  194.                 rectXButton.x = (getWidth() - 1 - (int)floor((getTitleBarHeight() - rectMinButton.height)/ 2)) - rectMinButton.width;
  195.                 ControlBoxWidth = rectXButton.width;
  196.                 break;
  197.             case BOTH:
  198.                 rectXButton.x = (getWidth() - 1 - (int)floor((getTitleBarHeight() - rectMinButton.height)/ 2)) - rectMinButton.width;
  199.                 rectMinButton.x = rectXButton.x - rectMinButton.width - 1;
  200.                 ControlBoxWidth = rectXButton.width * 2;
  201.                 break;
  202.         }
  203.  
  204.  
  205. }
  206.  
  207.  
  208. void jWindow::mousePressed(gcn::MouseEvent& mouseEvent)
  209. {
  210.         gcn::Window::mousePressed(mouseEvent);
  211.         switch(mControlBox)
  212.         {
  213.             case NONE:
  214.                 break;
  215.             case MINBOX:
  216.                 if (rectMinButton.isPointInRect(mouseEvent.getX(),mouseEvent.getY()))
  217.                 {
  218.                         mMoved = false;
  219.                         minMouseDown = true;
  220.                 }
  221.                 break;
  222.             case XBOX:
  223.                 if (rectXButton.isPointInRect(mouseEvent.getX(),mouseEvent.getY()))
  224.                 {
  225.                         mMoved = false;
  226.                         xMouseDown = true;
  227.                 }
  228.                 break;
  229.             case BOTH:
  230.                 if (rectXButton.isPointInRect(mouseEvent.getX(),mouseEvent.getY()))
  231.                 {
  232.                         mMoved = false;
  233.                         xMouseDown = true;
  234.                 }
  235.                 else if (rectMinButton.isPointInRect(mouseEvent.getX(),mouseEvent.getY()))
  236.                 {
  237.                         mMoved = false;
  238.                         minMouseDown = true;
  239.                 }
  240.                 break;
  241.         }
  242. }
  243.  
  244. void jWindow::mouseReleased(gcn::MouseEvent& mouseEvent)
  245. {
  246.       gcn::Window::mouseReleased(mouseEvent);
  247.               switch(mControlBox)
  248.         {
  249.             case NONE:
  250.                 break;
  251.             case MINBOX:
  252.                 if ( (rectMinButton.isPointInRect(mouseEvent.getX(),mouseEvent.getY())) && (minMouseDown) )
  253.                         MinButton();
  254.                 break;
  255.             case XBOX:
  256.                 if ((rectXButton.isPointInRect(mouseEvent.getX(),mouseEvent.getY())) && (xMouseDown) )
  257.                         XButton();
  258.                 break;
  259.             case BOTH:
  260.                 if ( (rectMinButton.isPointInRect(mouseEvent.getX(),mouseEvent.getY())) && (minMouseDown) )
  261.                         MinButton();
  262.                 else if ((rectXButton.isPointInRect(mouseEvent.getX(),mouseEvent.getY())) && (xMouseDown) )
  263.                         XButton();
  264.                 break;
  265.         }
  266.  
  267.       minMouseDown = false;
  268.       xMouseDown = false;
  269. }
  270.  
  271. void jWindow::XButton()
  272. {
  273.     delete this;
  274.  
  275.  
  276. }
  277.  
  278. void jWindow::MinButton()
  279. {
  280.     if  (!isMinimized())
  281.     {
  282.         // If the window is not minimized, we must store the current height for later use.
  283.         setDHeight  (getHeight());
  284.         // We will then shrink the widget into its titlebar.
  285.         setHeight   (getTitleBarHeight());
  286.         //Finally, we let the window know it is now minimized.
  287.         setMinimized(true);
  288.     }
  289.     else
  290.     {
  291.         // Simply set the window back to its default state.
  292.         setHeight   (getDHeight());
  293.         // Let the window know that it is no longer minimized.
  294.         setMinimized(false);
  295.     }
  296. }
  297.  
  298. /************************
  299. ******** DRAWING ********
  300. ************************/
  301. /*FLATNOBORDER,
  302. FLATBORDER,
  303. RAISED,
  304. SUNKEN*/
  305. /*IMAGE,
  306. HGRADIENT,
  307. VGRADIENT,
  308. SOLID}*/
  309. void jWindow::drawTitleBar(gcn::Graphics *graphics)
  310. {
  311.     int stB = 0;
  312.     int width = getWidth();
  313.     int height = getTitleBarHeight();
  314.     int destX, destY;
  315.     gcn::Color selColor;
  316.     int imgWidth, imgHeight;
  317.     int iColorDiff[4];
  318.     float fColorDiff[4];
  319.     float fColorCurr[4];
  320.     switch(jWindow::tStyle)
  321.     {
  322.         case IMAGE:
  323.             // Tile the image.
  324.             imgWidth = jWindow::tBG->getWidth();
  325.             imgHeight = jWindow::tBG->getHeight();
  326.             destX = imgWidth;
  327.             destY = imgHeight;
  328.             for (int x=stB;x<=width;x+=imgWidth)
  329.             {
  330.                 for(int y=stB;y<height;y+=imgHeight)
  331.                 {
  332.                     if ((x + destX) > width)
  333.                         destX = (x + destX) - width;
  334.                     if ((y + destY) > height)
  335.                         destY = (y + destY) - height;
  336.                     graphics->drawImage(jWindow::tBG, 0, 0, x, y, destX, destY);
  337.                 }
  338.             }
  339.             break;
  340.         case HGRADIENT:
  341.             destX = width - ControlBoxWidth - 1;
  342.             selColor = jWindow::tBaseColor;
  343.  
  344.             iColorDiff[0] = jWindow::tEndColor.r - jWindow::tBaseColor.r;
  345.             iColorDiff[1] = jWindow::tEndColor.g - jWindow::tBaseColor.g;
  346.             iColorDiff[2] = jWindow::tEndColor.b - jWindow::tBaseColor.b;
  347.             iColorDiff[3] = jWindow::tEndColor.a - jWindow::tBaseColor.a;
  348.  
  349.             fColorDiff[0] = (float)iColorDiff[0] / (float)destX;
  350.             fColorDiff[1] = (float)iColorDiff[1] / (float)destX;
  351.             fColorDiff[2] = (float)iColorDiff[2] / (float)destX;
  352.             fColorDiff[3] = (float)iColorDiff[3] / (float)destX;
  353.  
  354.             fColorCurr[0] = (float)selColor.r;
  355.             fColorCurr[1] = (float)selColor.b;
  356.             fColorCurr[2] = (float)selColor.g;
  357.             fColorCurr[3] = (float)selColor.a;
  358.  
  359.             for(int x=stB;x<destX;x++)
  360.             {
  361.                 graphics->setColor(selColor);
  362.                 graphics->drawLine(x,stB,x,height);
  363.                 fColorCurr[0] += fColorDiff[0];
  364.                 fColorCurr[1] += fColorDiff[1];
  365.                 fColorCurr[2] += fColorDiff[2];
  366.                 fColorCurr[3] += fColorDiff[3];
  367.                 iColorDiff[0] = (int)(ceil(fColorCurr[0]));
  368.                 iColorDiff[1] = (int)(ceil(fColorCurr[1]));
  369.                 iColorDiff[2] = (int)(ceil(fColorCurr[2]));
  370.                 iColorDiff[3] = (int)(ceil(fColorCurr[3]));
  371.                 selColor.r = iColorDiff[0] > 255 ? 255 : (iColorDiff[0] < 0 ? 0 : iColorDiff[0]);
  372.                 selColor.g = iColorDiff[1] > 255 ? 255 : (iColorDiff[1] < 0 ? 0 : iColorDiff[1]);
  373.                 selColor.b = iColorDiff[2] > 255 ? 255 : (iColorDiff[2] < 0 ? 0 : iColorDiff[2]);
  374.                 selColor.a = iColorDiff[3] > 255 ? 255 : (iColorDiff[3] < 0 ? 0 : iColorDiff[3]);
  375.             }
  376.             graphics->setColor(jWindow::tEndColor);
  377.             graphics->fillRectangle(gcn::Rectangle(destX,stB,width,height));
  378.             break;
  379.         case VGRADIENT:
  380.             selColor = jWindow::tBaseColor;
  381.  
  382.             iColorDiff[0] = jWindow::tEndColor.r - jWindow::tBaseColor.r;
  383.             iColorDiff[1] = jWindow::tEndColor.g - jWindow::tBaseColor.g;
  384.             iColorDiff[2] = jWindow::tEndColor.b - jWindow::tBaseColor.b;
  385.             iColorDiff[3] = jWindow::tEndColor.a - jWindow::tBaseColor.a;
  386.  
  387.             fColorDiff[0] = (float)iColorDiff[0] / (float)height;
  388.             fColorDiff[1] = (float)iColorDiff[1] / (float)height;
  389.             fColorDiff[2] = (float)iColorDiff[2] / (float)height;
  390.             fColorDiff[3] = (float)iColorDiff[3] / (float)height;
  391.  
  392.             fColorCurr[0] = (float)selColor.r;
  393.             fColorCurr[1] = (float)selColor.b;
  394.             fColorCurr[2] = (float)selColor.g;
  395.             fColorCurr[3] = (float)selColor.a;
  396.  
  397.             for(int y=stB;y<=height;y++)
  398.             {
  399.                 graphics->setColor(selColor);
  400.                 graphics->drawLine(stB,y,width,y);
  401.                 fColorCurr[0] += fColorDiff[0];
  402.                 fColorCurr[1] += fColorDiff[1];
  403.                 fColorCurr[2] += fColorDiff[2];
  404.                 fColorCurr[3] += fColorDiff[3];
  405.                 iColorDiff[0] = (int)(ceil(fColorCurr[0]));
  406.                 iColorDiff[1] = (int)(ceil(fColorCurr[1]));
  407.                 iColorDiff[2] = (int)(ceil(fColorCurr[2]));
  408.                 iColorDiff[3] = (int)(ceil(fColorCurr[3]));
  409.                 selColor.r = iColorDiff[0] > 255 ? 255 : (iColorDiff[0] < 0 ? 0 : iColorDiff[0]);
  410.                 selColor.g = iColorDiff[1] > 255 ? 255 : (iColorDiff[1] < 0 ? 0 : iColorDiff[1]);
  411.                 selColor.b = iColorDiff[2] > 255 ? 255 : (iColorDiff[2] < 0 ? 0 : iColorDiff[2]);
  412.                 selColor.a = iColorDiff[3] > 255 ? 255 : (iColorDiff[3] < 0 ? 0 : iColorDiff[3]);
  413.             }
  414.  
  415.             break;
  416.         case SOLID:
  417.             graphics->setColor(jWindow::tBaseColor);
  418.             graphics->fillRectangle(gcn::Rectangle(stB,stB,width,height));
  419.             break;
  420.     }
  421.  
  422.         gcn::Color minBackcolor, xBackcolor;
  423.  
  424.         if(!minMouseDown)
  425.             minBackcolor = tBaseColor + 0x303030;
  426.         else
  427.             minBackcolor = tBaseColor - 0x303030;
  428.  
  429.         if(!xMouseDown)
  430.             xBackcolor = tBaseColor + 0x303030;
  431.         else
  432.             xBackcolor = tBaseColor - 0x303030;
  433.  
  434.         switch(mControlBox)
  435.         {
  436.             case NONE:
  437.                 break;
  438.             case MINBOX:
  439.                 graphics->setColor(minBackcolor);
  440.                 graphics->fillRectangle(rectMinButton);
  441.                 break;
  442.             case XBOX:
  443.                 graphics->setColor(xBackcolor);
  444.                 graphics->fillRectangle(rectXButton);
  445.                 break;
  446.             case BOTH:
  447.                 graphics->setColor(minBackcolor);
  448.                 graphics->fillRectangle(rectMinButton);
  449.                 graphics->setColor(xBackcolor);
  450.                 graphics->fillRectangle(rectXButton);
  451.                 break;
  452.         }
  453.  
  454.         if((tAppearance!=FLATNOBORDER)&&(fAppearance!=FLATNOBORDER))
  455.         {
  456.             graphics->setColor(jWindow::tBorderColor);
  457.             graphics->drawLine(0,height, jWindow::getWidth(),height);
  458.         }
  459.         else if ((tAppearance==FLATNOBORDER)&&(fAppearance!=FLATNOBORDER))
  460.         {
  461.             graphics->setColor(jWindow::fBorderColor);
  462.             graphics->drawLine(0,height, jWindow::getWidth(),height);
  463.         }
  464.         else if ((tAppearance!=FLATNOBORDER)&&(fAppearance==FLATNOBORDER))
  465.         {
  466.             graphics->setColor(jWindow::tBorderColor);
  467.             graphics->drawLine(0,height, jWindow::getWidth(),height);
  468.         }
  469.         else if ((tAppearance==FLATNOBORDER)&&(fAppearance==FLATNOBORDER))
  470.         {
  471.             graphics->setColor(getForegroundColor());
  472.         }
  473.  
  474.  
  475.         gcn::Color minForecolor, xForecolor;
  476.         if(!minMouseDown)
  477.             minForecolor = getForegroundColor();
  478.         else
  479.             minForecolor = getForegroundColor() - 0x303030;
  480.  
  481.         if(!xMouseDown)
  482.             xForecolor = getForegroundColor();
  483.         else
  484.             xForecolor = getForegroundColor() - 0x303030;
  485.  
  486.  
  487.         switch(mControlBox)
  488.         {
  489.             case NONE:
  490.                 break;
  491.             case MINBOX:
  492.                 graphics->drawRectangle(rectMinButton);
  493.                 graphics->setColor(minForecolor);
  494.                 graphics->drawLine(rectMinButton.x + 2,  rectMinButton.y + 3, rectMinButton.x + rectMinButton.width - 3, rectMinButton.y + 3);
  495.                 graphics->drawLine(rectMinButton.x + 2,  rectMinButton.y + 4, rectMinButton.x + rectMinButton.width - 3, rectMinButton.y + 4);
  496.                 break;
  497.             case XBOX:
  498.                 graphics->drawRectangle(rectXButton);
  499.                 graphics->setColor(xForecolor);
  500.                 graphics->drawLine(rectXButton.x + 3,  rectXButton.y + 3, rectXButton.x + rectXButton.width - 4, rectXButton.y + rectXButton.height - 4);
  501.                 graphics->drawLine(rectXButton.x + 4,  rectXButton.y + 3, rectXButton.x + rectXButton.width - 5, rectXButton.y + rectXButton.height - 4);
  502.                 graphics->drawLine(rectXButton.x + 3,  rectXButton.y + rectXButton.height - 4, rectXButton.x + rectXButton.width - 4,  rectXButton.y + 3);
  503.                 graphics->drawLine(rectXButton.x + 4,  rectXButton.y + rectXButton.height - 4, rectXButton.x + rectXButton.width - 5,  rectXButton.y + 3);
  504.  
  505.                 graphics->drawPoint(rectXButton.x + 6,rectXButton.y + rectXButton.height - 5);
  506.                 graphics->drawPoint(rectXButton.x + rectXButton.width - 7, rectXButton.y + 6);
  507.                 break;
  508.             case BOTH:
  509.                 graphics->drawRectangle(rectXButton);
  510.                 graphics->drawRectangle(rectMinButton);
  511.                 graphics->setColor(minForecolor);
  512.                 graphics->drawLine(rectMinButton.x + 2,  rectMinButton.y + 3, rectMinButton.x + rectMinButton.width - 3, rectMinButton.y + 3);
  513.                 graphics->drawLine(rectMinButton.x + 2,  rectMinButton.y + 4, rectMinButton.x + rectMinButton.width - 3, rectMinButton.y + 4);
  514.                 graphics->setColor(xForecolor);
  515.                 graphics->drawLine(rectXButton.x + 3,  rectXButton.y + 3, rectXButton.x + rectXButton.width - 4, rectXButton.y + rectXButton.height - 4);
  516.                 graphics->drawLine(rectXButton.x + 4,  rectXButton.y + 3, rectXButton.x + rectXButton.width - 5, rectXButton.y + rectXButton.height - 4);
  517.                 graphics->drawLine(rectXButton.x + 3,  rectXButton.y + rectXButton.height - 4, rectXButton.x + rectXButton.width - 4,  rectXButton.y + 3);
  518.                 graphics->drawLine(rectXButton.x + 4,  rectXButton.y + rectXButton.height - 4, rectXButton.x + rectXButton.width - 5,  rectXButton.y + 3);
  519.  
  520.                 graphics->drawPoint(rectXButton.x + 6,rectXButton.y + rectXButton.height - 5);
  521.                 graphics->drawPoint(rectXButton.x + rectXButton.width - 7, rectXButton.y + 6);
  522.                 break;
  523.         }
  524.  
  525.  
  526.  
  527.  
  528.         int textX;
  529.         int textY = (int)((getTitleBarHeight() - getFont()->getHeight()) / 2);
  530.  
  531.         switch (getAlignment())
  532.         {
  533.           case gcn::Graphics::LEFT:
  534.               textX = 4;
  535.               break;
  536.           case gcn::Graphics::CENTER:
  537.               textX = getWidth() / 2;
  538.               break;
  539.           case gcn::Graphics::RIGHT:
  540.               textX = getWidth() - 4;
  541.               break;
  542.           default:
  543.               throw GCN_EXCEPTION("Unknown alignment.");
  544.         }
  545.  
  546.  
  547.         graphics->setColor(getForegroundColor());
  548.         graphics->setFont(getFont());
  549.         graphics->pushClipArea(gcn::Rectangle(0, 0, getWidth(), getTitleBarHeight() - 1));
  550.         graphics->drawText(getCaption(), textX, textY, getAlignment());
  551.         graphics->popClipArea();
  552.  
  553. }
  554. void jWindow::drawForm(gcn::Graphics *graphics)
  555. {
  556.     int stB = 0;
  557.     int width = getWidth() - (stB * 2);
  558.     int height = getHeight() - (stB * 2);
  559.     int destX, destY;
  560.     gcn::Color selColor;
  561.     int iColorDiff[4];
  562.     float fColorDiff[4];
  563.     float fColorCurr[4];
  564.     int imgWidth, imgHeight;
  565.     switch(jWindow::fStyle)
  566.     {
  567.         case IMAGE:
  568.             // Tile the image.
  569.             imgWidth = jWindow::fBG->getWidth();
  570.             imgHeight = jWindow::fBG->getHeight();
  571.             destX = imgWidth;
  572.             destY = imgHeight;
  573.             for (int x=stB;x<=width;x+=imgWidth)
  574.             {
  575.                 for(int y=stB;y<height;y+=imgHeight)
  576.                 {
  577.                     if ((x + destX) > width)
  578.                         destX = (x + destX) - width;
  579.                     if ((y + destY) > height)
  580.                         destY = (y + destY) - height;
  581.                     graphics->drawImage(jWindow::fBG, 0, 0, x, y, destX, destY);
  582.                 }
  583.             }
  584.             break;
  585.         case HGRADIENT:
  586.             destX = width;
  587.             selColor = jWindow::fBaseColor;
  588.             selColor.a = fAlpha;
  589.             iColorDiff[0] = jWindow::fEndColor.r - jWindow::fBaseColor.r;
  590.             iColorDiff[1] = jWindow::fEndColor.g - jWindow::fBaseColor.g;
  591.             iColorDiff[2] = jWindow::fEndColor.b - jWindow::fBaseColor.b;
  592.             iColorDiff[3] = jWindow::fEndColor.a - jWindow::fBaseColor.a;
  593.  
  594.             fColorDiff[0] = (float)iColorDiff[0] / (float)destX;
  595.             fColorDiff[1] = (float)iColorDiff[1] / (float)destX;
  596.             fColorDiff[2] = (float)iColorDiff[2] / (float)destX;
  597.             fColorDiff[3] = (float)iColorDiff[3] / (float)destX;
  598.  
  599.             fColorCurr[0] = (float)selColor.r;
  600.             fColorCurr[1] = (float)selColor.b;
  601.             fColorCurr[2] = (float)selColor.g;
  602.             fColorCurr[3] = (float)selColor.a;
  603.  
  604.             for(int x=stB;x<destX;x++)
  605.             {
  606.                 graphics->setColor(selColor);
  607.                 graphics->drawLine(x,stB,x,height);
  608.                 fColorCurr[0] += fColorDiff[0];
  609.                 fColorCurr[1] += fColorDiff[1];
  610.                 fColorCurr[2] += fColorDiff[2];
  611.                 fColorCurr[3] += fColorDiff[3];
  612.                 iColorDiff[0] = (int)(ceil(fColorCurr[0]));
  613.                 iColorDiff[1] = (int)(ceil(fColorCurr[1]));
  614.                 iColorDiff[2] = (int)(ceil(fColorCurr[2]));
  615.                 iColorDiff[3] = (int)(ceil(fColorCurr[3]));
  616.                 selColor.r = iColorDiff[0] > 255 ? 255 : (iColorDiff[0] < 0 ? 0 : iColorDiff[0]);
  617.                 selColor.g = iColorDiff[1] > 255 ? 255 : (iColorDiff[1] < 0 ? 0 : iColorDiff[1]);
  618.                 selColor.b = iColorDiff[2] > 255 ? 255 : (iColorDiff[2] < 0 ? 0 : iColorDiff[2]);
  619.                 selColor.a = iColorDiff[3] > 255 ? 255 : (iColorDiff[3] < 0 ? 0 : iColorDiff[3]);
  620.             }
  621.             break;
  622.         case VGRADIENT:
  623.             selColor = jWindow::fBaseColor;
  624.  
  625.             iColorDiff[0] = jWindow::fEndColor.r - jWindow::fBaseColor.r;
  626.             iColorDiff[1] = jWindow::fEndColor.g - jWindow::fBaseColor.g;
  627.             iColorDiff[2] = jWindow::fEndColor.b - jWindow::fBaseColor.b;
  628.             iColorDiff[3] = jWindow::fEndColor.a - jWindow::fBaseColor.a;
  629.  
  630.             fColorDiff[0] = (float)iColorDiff[0] / (float)height;
  631.             fColorDiff[1] = (float)iColorDiff[1] / (float)height;
  632.             fColorDiff[2] = (float)iColorDiff[2] / (float)height;
  633.             fColorDiff[3] = (float)iColorDiff[3] / (float)height;
  634.  
  635.             fColorCurr[0] = (float)selColor.r;
  636.             fColorCurr[1] = (float)selColor.b;
  637.             fColorCurr[2] = (float)selColor.g;
  638.             fColorCurr[3] = (float)selColor.a;
  639.  
  640.             for(int y=stB;y<=height;y++)
  641.             {
  642.                 graphics->setColor(selColor);
  643.                 graphics->drawLine(stB,y,width,y);
  644.                 fColorCurr[0] += fColorDiff[0];
  645.                 fColorCurr[1] += fColorDiff[1];
  646.                 fColorCurr[2] += fColorDiff[2];
  647.                 fColorCurr[3] += fColorDiff[3];
  648.                 iColorDiff[0] = (int)(ceil(fColorCurr[0]));
  649.                 iColorDiff[1] = (int)(ceil(fColorCurr[1]));
  650.                 iColorDiff[2] = (int)(ceil(fColorCurr[2]));
  651.                 iColorDiff[3] = (int)(ceil(fColorCurr[3]));
  652.                 selColor.r = iColorDiff[0] > 255 ? 255 : (iColorDiff[0] < 0 ? 0 : iColorDiff[0]);
  653.                 selColor.g = iColorDiff[1] > 255 ? 255 : (iColorDiff[1] < 0 ? 0 : iColorDiff[1]);
  654.                 selColor.b = iColorDiff[2] > 255 ? 255 : (iColorDiff[2] < 0 ? 0 : iColorDiff[2]);
  655.                 selColor.a = iColorDiff[3] > 255 ? 255 : (iColorDiff[3] < 0 ? 0 : iColorDiff[3]);
  656.             }
  657.             break;
  658.         case SOLID:
  659.             graphics->setColor(jWindow::fBaseColor);
  660.             graphics->fillRectangle(gcn::Rectangle(stB,stB,width,height));
  661.             break;
  662.     }
  663.  
  664.         drawChildren(graphics);
  665.  
  666. }
  667.  
  668. void jWindow::drawFrame(gcn::Graphics * graphics)
  669. {
  670.     gcn::Color highlightColor, shadowColor;
  671.     int width, height;
  672.     if(fAppearance != FLATNOBORDER)
  673.     {
  674.  
  675.  
  676.         switch(jWindow::fAppearance)
  677.         {
  678.             case FLATBORDER:
  679.                 highlightColor = jWindow::fBorderColor;
  680.                 shadowColor = jWindow::fBorderColor;
  681.                 break;
  682.             case RAISED:
  683.                 highlightColor = jWindow::fBorderColor + 0x303030;
  684.                 shadowColor = jWindow::fBorderColor - 0x303030;
  685.                 break;
  686.             case SUNKEN:
  687.                 highlightColor = jWindow::fBorderColor - 0x303030;
  688.                 shadowColor = jWindow::fBorderColor + 0x303030;
  689.                 break;
  690.         }
  691.         // Make sure transparency remains.
  692.         highlightColor.a = jWindow::fAlpha;
  693.         shadowColor.a = jWindow::fAlpha;
  694.  
  695.         width = getWidth() + getFrameSize() * 2 - 1;
  696.         height = getHeight() + getFrameSize() * 2 - 1;
  697.  
  698.  
  699.             for (unsigned int i = 0,y = getTitleBarHeight()+2; i < getFrameSize(); ++i,++y)
  700.             {
  701.                 // Top - left
  702.                 graphics->setColor(shadowColor);
  703.                 graphics->drawLine(i,y, i, height - i - 1);
  704.                 // Right, bottom
  705.                 graphics->setColor(highlightColor);
  706.                 graphics->drawLine(width - i,y, width - i, height - i);
  707.                 graphics->drawLine(i,height - i, width - i - 1, height - i);
  708.             }
  709.     }
  710.     if(tAppearance!=FLATNOBORDER)
  711.     {
  712.         switch(jWindow::tAppearance)
  713.         {
  714.             case FLATBORDER:
  715.                 highlightColor = jWindow::tBorderColor;
  716.                 shadowColor = jWindow::tBorderColor;
  717.                 break;
  718.             case RAISED:
  719.                 highlightColor = jWindow::tBorderColor + 0x303030;
  720.                 shadowColor = jWindow::tBorderColor - 0x303030;
  721.                 break;
  722.             case SUNKEN:
  723.                 highlightColor = jWindow::tBorderColor - 0x303030;
  724.                 shadowColor = jWindow::tBorderColor + 0x303030;
  725.                 break;
  726.         }
  727.         // Make sure transparency remains.
  728.         highlightColor.a = jWindow::tAlpha;
  729.         shadowColor.a = jWindow::tAlpha;
  730.  
  731.         width = getWidth() + getFrameSize() * 2 - 1;
  732.         height = getTitleBarHeight() + getFrameSize() * 2 - 1 + 2;
  733.  
  734.  
  735.             for (unsigned int i = 0; i < getFrameSize(); ++i)
  736.             {
  737.                 graphics->setColor(shadowColor);
  738.                 graphics->drawLine(i,i, width - i, i);
  739.                 graphics->drawLine(i,i + 1, i, height);
  740.                 graphics->setColor(highlightColor);
  741.                 graphics->drawLine(width - i,i + 1, width - i, height);
  742.             }
  743.  
  744.     }
  745.  
  746. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement