' Custom Component Creation for Rapid-Q (Windows) by William Yu
'
' QCoolForm    - Extension of QForm with customized titlebar that can
'                change colors. Adding menus might be a problem
'
' NEW PROPERTIES:
'    CaptionX  - Where the caption should be displayed
'  StartColor  - Starting color of titlebar
'    EndColor  - Ending color of titlebar
'   Increment  - Color increment count
'
' METHODS:
'    Repaint   - Repaints titlebar
'    Center    - Overrides original Center method
'
' INHERITED EVENTS:
'    OnPaint
'    OnResize


$APPTYPE GUI
$TYPECHECK ON
$INCLUDE "RAPIDQ.INC"

DIM D AS QDXScreen

' Some API constants, used by SendMessage
CONST WM_SYSCOMMAND = &H0112
CONST SC_SIZE = 61440
CONST SC_MOVE = 61456
CONST SC_MINIMIZE = 61472
CONST SC_MAXIMIZE = 61488
CONST SC_CLOSE = 61536
CONST SC_RESTORE = 61728

$RESOURCE BMP_Min AS "Min.BMP"
$RESOURCE BMP_Max AS "Max.BMP"
$RESOURCE BMP_Close AS "X.BMP"

$DEFINE TYPE STRUCT

TYPE QCoolForm EXTENDS QForm
  '-- PROTECTED Properties
  TitleBar AS QCanvas
  MinButton AS QCoolBtn
  MaxButton AS QCoolBtn
  CloseButton AS QCoolBtn
  PrevX AS INTEGER
  PrevY AS INTEGER
  MoveForm AS INTEGER

  '-- PUBLIC Properties
  CaptionX AS INTEGER
  StartColor AS INTEGER
  EndColor AS INTEGER
  Increment AS INTEGER

  SUB Center           '-- Have to override because Titlebar keeps popping up
    WITH QCoolForm
      .Left = Screen.Width/2 - .Width/2
      .Top = Screen.Height/2 - .Height/2
    END WITH
  END SUB

  SUB DrawTitleBar
    DIM I AS INTEGER
    DIM Col AS INTEGER
        Col = QCoolForm.StartColor

    WITH QCoolForm.TitleBar
      .Width = QCoolForm.ClientWidth - 1  '-- In case user override OnResize
      FOR I = 0 TO .Width STEP 4
        IF I<.Width-55 THEN
          .FillRect(I,0,I+4,.Height,Col)
        ELSE
          .FillRect(I,0,I+4,2,Col)
          .FillRect(I,.Height,I+4,.Height-2,Col)
        END IF
        IF Col < QCoolForm.EndColor THEN
          Col += QCoolForm.Increment
        END IF
      NEXT
      .FillRect(.Width-4,0,.Width,.Height,Col)
      .TextOut(QCoolForm.CaptionX+1,4,QCoolForm.Caption,&H777777,-1)
      .TextOut(QCoolForm.CaptionX,3,QCoolForm.Caption,&HFFFFFF,-1)
    END WITH
  END SUB

  SUB Repaint
    QCoolForm.DrawTitleBar
  END SUB

  EVENT OnResize
    WITH QCoolForm
      .TitleBar.Width = .ClientWidth - 1
      .MinButton.Left = .ClientWidth - 55
      .MaxButton.Left = .ClientWidth - 38
      .CloseButton.Left = .ClientWidth - 21
    END WITH
  END EVENT

  EVENT OnPaint
    QCoolForm.DrawTitleBar
  END EVENT

  EVENT MinButton.OnClick
    IF IsConsole THEN
      QCoolForm.WindowState = wsMinimized
    ELSE
      'SendMessage(QCoolForm.Handle, WM_SYSCOMMAND, SC_MINIMIZE, 0)
      Application.Minimize
    END IF
  END EVENT
  EVENT MaxButton.OnClick
    IF QCoolForm.WindowState = wsMaximized THEN
      QCoolForm.WindowState = wsNormal
      QCoolForm.MaxButton.Down = FALSE
    ELSE
      QCoolForm.WindowState = wsMaximized
      QCoolForm.MaxButton.Down = TRUE
    END IF
  END EVENT
  EVENT CloseButton.OnClick
    QCoolForm.Close
  END EVENT

  EVENT TitleBar.OnMouseDown(Button%, X%, Y%)
    IF Button% <> 0 THEN
      EXIT EVENT
    END IF
    QCoolForm.MoveForm = TRUE
    QCoolForm.PrevX = X%
    QCoolForm.PrevY = Y%
  END EVENT

  EVENT TitleBar.OnMouseMove(X%, Y%)
    WITH QCoolForm
      IF .MoveForm = FALSE THEN
        EXIT EVENT
      END IF
      IF .Left+(X% - .PrevX) < 0 THEN
        .Left = 0
      ELSE
        .Left = .Left + (X% - .PrevX)
      END IF
      IF .Top + (Y% - .PrevY) < 0 THEN
        .Top = 0
      ELSE
        .Top = .Top + (Y% - .PrevY)
      END IF
    END WITH
  END EVENT

  EVENT TitleBar.OnMouseUp(Button%, X%, Y%)
    IF Button% = 0 THEN
      QCoolForm.MoveForm = FALSE
    END IF
  END EVENT

  CONSTRUCTOR
    HideTitleBar
    MoveForm = FALSE
    StartColor = &H004400
    EndColor = &H00FF00
    Increment = &H000100
    TitleBar.Parent = QCoolForm
    TitleBar.Left = 0
    TitleBar.Height = 20
    MinButton.Parent = QCoolForm
    MinButton.Width = 16
    MinButton.Height = 16
    MinButton.Left = QCoolForm.ClientWidth - 55
    MinButton.Top = 2
    MinButton.BMPHandle = BMP_Min
    MaxButton.Parent = QCoolForm
    MaxButton.Width = 16
    MaxButton.Height = 16
    MaxButton.Left = QCoolForm.ClientWidth - 38
    MaxButton.Top = 2
    MaxButton.BMPHandle = BMP_Max
    MaxButton.AllowAllUp = TRUE
    MaxButton.GroupIndex = 1
    CloseButton.Parent = QCoolForm
    CloseButton.Width = 16
    CloseButton.Height = 16
    CloseButton.Left = QCoolForm.ClientWidth - 21
    CloseButton.Top = 2
    CloseButton.BMPHandle = BMP_Close
    CaptionX = 4
  END CONSTRUCTOR
END TYPE



'-- Test our new component

DECLARE SUB Button1Click
DECLARE SUB Button2Click
DECLARE SUB Button3Click

CREATE Form AS QCoolForm
   Center
   Caption = "Cool Form"
   CREATE Button1 AS QButton
     Width = 120
     Left = 5
     Top = 25
     Caption = "Blue Title Bar"
     OnClick = Button1Click
   END CREATE
   CREATE Button2 AS QButton
     Width = 120
     Left = 5
     Top = 55
     Caption = "Red Title Color"
     OnClick = Button2Click
   END CREATE
   CREATE Button3 AS QButton
     Width = 120
     Left = 5
     Top = 85
     Caption = "Yellow Title Color"
     OnClick = Button3Click
   END CREATE
   ShowModal
END CREATE


SUB Button1Click
  Form.StartColor = &H440000
  Form.EndColor = &HFF0000
  Form.Increment = &H010000
  Form.Repaint
END SUB

SUB Button2Click
  Form.StartColor = &H000044
  Form.EndColor = &H0000FF
  Form.Increment = &H0000001
  Form.Repaint
END SUB

SUB Button3Click
  Form.StartColor = &H004444
  Form.EndColor = &H55FFFF
  Form.Increment = &H010202
  Form.Repaint
END SUB
