home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 3 / GrEdit1.exe / main.cpp < prev    next >
C/C++ Source or Header  |  1998-02-09  |  8KB  |  215 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++Builder
  3. //Copyright (c) 1987, 1998 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl.h>
  7. #pragma hdrstop
  8.  
  9. #include "main.h"
  10. #include "palette.h"
  11. //---------------------------------------------------------------------------
  12. #pragma resource "*.dfm"
  13. #pragma resource "extrares.res"
  14. TDoodleForm *DoodleForm;
  15. //---------------------------------------------------------------------------
  16. __fastcall TDoodleForm::TDoodleForm(TComponent* Owner)
  17.     : TForm(Owner)
  18. {
  19. }
  20. //---------------------------------------------------------------------------
  21. void __fastcall TDoodleForm::FormCreate(TObject *Sender)
  22. {
  23.     HINSTANCE HInst;
  24.     HInst = reinterpret_cast<HINSTANCE>(HInstance);
  25.     // Load custom cursors for tools from extrares.res
  26.     Screen->Cursors[crFill] = LoadCursor(HInst, "FILL");
  27.     Screen->Cursors[crPlus] = LoadCursor(HInst, "PLUS");
  28.     Screen->Cursors[crDraw] = LoadCursor(HInst, "DRAW");
  29.     Screen->Cursors[crErase] = LoadCursor(HInst, "ERASE");
  30.     Cursor = TCursor(crDraw);
  31. }
  32. //---------------------------------------------------------------------------
  33. void __fastcall TDoodleForm::Image1MouseDown(TObject *Sender,
  34.     TMouseButton Button, TShiftState Shift, int X, int Y)
  35. {
  36.     // The Doodle form contains an Image control on which all of the painting
  37.     // will occur.  Another option would have been to draw directly on the
  38.     // form's canvas.  The benefit of drawing on the Image control is that
  39.     // it knows how to repaint itself.
  40.  
  41.     if (ToolPalette->FillButton->Down)
  42.     {
  43.         // The fill tool is the only one that makes use of the right mouse
  44.         // button.  The right mouse button is used to fill with the BG color.
  45.         if (Button == mbLeft)
  46.             Image1->Canvas->Brush->Color = ToolPalette->FGShape->Brush->Color;
  47.         else
  48.             Image1->Canvas->Brush->Color = ToolPalette->BGShape->Brush->Color;
  49.         Image1->Canvas->FloodFill(X, Y, Image1->Canvas->Pixels[X][Y], fsSurface);
  50.         return;
  51.     }
  52.  
  53.     // Otherwise, only the left mouse button is of interest.
  54.     if (Button != mbLeft)
  55.         return;
  56.  
  57.     if (ToolPalette->EraseButton->Down)
  58.     {
  59.         Image1->Canvas->Pen->Color = ToolPalette->BGShape->Brush->Color;
  60.         Image1->Canvas->Brush->Color = ToolPalette->BGShape->Brush->Color;
  61.         Image1->Canvas->Pen->Width = 13;
  62.         Image1->Canvas->Rectangle(X-1, Y-1, X, Y);
  63.         Image1->Canvas->MoveTo(X,Y);
  64.         return;
  65.     }
  66.  
  67.     if (ToolPalette->PencilButton->Down)
  68.     {
  69.         Image1->Canvas->Pen->Color = ToolPalette->FGShape->Brush->Color;
  70.         Image1->Canvas->Brush->Color = ToolPalette->BGShape->Brush->Color;
  71.         Image1->Canvas->MoveTo(X,Y);
  72.         return;
  73.     }
  74.  
  75.     // At this point, we know we are dealing with a circle, solid circle,
  76.     // square, or solid square
  77.  
  78.     InitialX = X;
  79.     InitialY = Y;
  80.  
  81.     // TmpImage will hold the image as it existed when the mouse went down.
  82.     // This will be retained until the mouse is released.  This image will be
  83.     // used to restore the image during sizing of squares and circles.
  84.     TmpImage = new TImage(this);
  85.     TmpImage->Picture = Image1->Picture;
  86. }
  87. //---------------------------------------------------------------------------
  88. void __fastcall TDoodleForm::Image1MouseMove(TObject *Sender, TShiftState Shift,
  89.     int X, int Y)
  90. {
  91.     // Only the left mouse button is of interest
  92.     if (!Shift.Contains(ssLeft))
  93.         return;
  94.  
  95.     if (ToolPalette->FillButton->Down)
  96.         return;
  97.  
  98.     if (ToolPalette->PencilButton->Down)
  99.     {
  100.         Image1->Canvas->LineTo(X,Y);
  101.         return;
  102.     }
  103.  
  104.     if (ToolPalette->EraseButton->Down)
  105.     {
  106.         Image1->Canvas->LineTo(X,Y);
  107.         return;
  108.     }
  109.  
  110.     // At this point, we know we are dealing with an ellipse or rectangle,
  111.     // filled or not filled.
  112.     DrawShape(X, Y);
  113. }
  114. //---------------------------------------------------------------------------
  115. void __fastcall TDoodleForm::Image1MouseUp(TObject *Sender, TMouseButton Button,
  116.     TShiftState Shift, int X, int Y)
  117. {
  118.     // Only the left mouse button is of interest
  119.     if (Button != mbLeft)
  120.         return;
  121.  
  122.     if ((ToolPalette->FillButton->Down) || (ToolPalette->PencilButton->Down))
  123.         return;
  124.  
  125.     if (ToolPalette->EraseButton->Down)
  126.     {
  127.         Image1->Canvas->Pen->Width = 1;
  128.         return;
  129.     }
  130.  
  131.     // At this point, we know we are dealing with an ellipse or rectangle,
  132.     // filled or not filled.
  133.     DrawShape(X, Y);
  134.  
  135.     delete TmpImage;
  136. }
  137. //---------------------------------------------------------------------------
  138. void __fastcall TDoodleForm::FormActivate(TObject *Sender)
  139. {
  140.     // Contains the code for positioning and showing the tool palette.
  141.     // The call to ToolPalette->Show() could have gone in DoodleForm's
  142.     // Show method if not for the desire to position the window.
  143.  
  144.     if (ToolPalette->Visible)
  145.         return;
  146.  
  147.     // Initial position of the palette relative to the main form
  148.     ToolPalette->Top = DoodleForm->Top + (DoodleForm->Height / 6);
  149.     ToolPalette->Left = DoodleForm->Left - ((DoodleForm->Width / 5));
  150.     ToolPalette->Show();
  151. }
  152. //---------------------------------------------------------------------------
  153. void __fastcall TDoodleForm::DrawShape(int X, int Y)
  154. {
  155.     TRect bounds;  // for graphics functions which require a rect
  156.  
  157.     // Start with the original image that we saved when the button
  158.     // first went down.  This blows away any previous rectangles or ellipses
  159.     // that were drawn while the user dragged.
  160.     Image1->Picture = TmpImage->Picture;
  161.  
  162.     // The above line blew away the brush.  The line below restores it.
  163.     Image1->Canvas->Brush->Color = ToolPalette->FGShape->Brush->Color;
  164.     Image1->Canvas->Pen->Color = ToolPalette->FGShape->Brush->Color;
  165.  
  166.     // Some graphics functions do not allow a rectangle with its bottom above
  167.     // its top, or right to the left of its left side.  This code is to deal
  168.     // with the case that the user started dragging from the bottom or right.
  169.     if (X < InitialX)
  170.     {
  171.         bounds.Left = X;
  172.         bounds.Right = InitialX;
  173.     }
  174.     else
  175.     {
  176.         bounds.Right = X;
  177.         bounds.Left = InitialX;
  178.     }
  179.  
  180.     if (Y < InitialY)
  181.     {
  182.         bounds.Top = Y;
  183.         bounds.Bottom = InitialY;
  184.     }
  185.     else
  186.     {
  187.         bounds.Bottom = Y;
  188.         bounds.Top = InitialY;
  189.     }
  190.  
  191.     // Draw the circle or square using the corresponding function.
  192.     if (ToolPalette->CircleButton->Down)
  193.         Image1->Canvas->Arc(InitialX, InitialY, X, Y, X, Y, X, Y);
  194.     else if (ToolPalette->SolidCirButton->Down)
  195.         Image1->Canvas->Ellipse(InitialX, InitialY, X, Y);
  196.     else if (ToolPalette->SquareButton->Down)
  197.         Image1->Canvas->FrameRect(bounds);
  198.     else if (ToolPalette->SolidSqButton->Down)
  199.         Image1->Canvas->FillRect(bounds);
  200. }
  201. //---------------------------------------------------------------------------
  202. void __fastcall TDoodleForm::FormShow(TObject *Sender)
  203. {
  204.  SetFocus();   
  205. }
  206. //---------------------------------------------------------------------------
  207.  
  208. void __fastcall TDoodleForm::FormClose(TObject *Sender,
  209.       TCloseAction &Action)
  210. {
  211.  Application->Terminate();   
  212. }
  213. //---------------------------------------------------------------------------
  214.  
  215.