home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 April B / Pcwk4b98.iso / Borland / Dbase50w / SAMPLES1.PAK / EVENT.PRG < prev    next >
Text File  |  1994-08-02  |  13KB  |  323 lines

  1. *******************************************************************************
  2. *  PROGRAM:      Event.prg
  3. *
  4. *  WRITTEN BY:   Borland Samples Group
  5. *
  6. *  DATE:         6/93
  7. *
  8. *  UPDATED:      7/94
  9. *
  10. *  VERSION:      dBASE FOR WINDOWS 5.0
  11. *
  12. *  DESCRIPTION:  This program uses the dBase for Windows object model to create
  13. *                forms with event handlers that manipulate the properties of the
  14. *                form and its controls.
  15. *                The ButtonForm class creates a form which manipulates its
  16. *                pushbuttons.
  17. *                When you left click anywhere in that form, the pushbutton
  18. *                will move to that point, and its caption will change to show
  19. *                the current coordinates. You can also Ctrl-Click to
  20. *                make another button, and Shift-Click to align all the buttons.
  21. *
  22. *                The SizeForm class creates another type of form.  When
  23. *                you click and move in this form, its size will change
  24. *                as you move the mouse.
  25. *
  26. *                Indicator messages are attached to events that don't
  27. *                have any actions attached to them.  These messages will
  28. *                display in the Command Results window when the corresponding
  29. *                events occur.  Messages display both for button events and
  30. *                form events.
  31. *
  32. *                All properties are changed using event handling
  33. *                functions that are assigned to these forms.
  34. *
  35. *  PARAMETERS:   None
  36. *
  37. *  CALLS:        SetCapture()
  38. *
  39. *  USAGE:        DO Event
  40. *
  41. *******************************************************************************
  42. #include <Utils.h>
  43.  
  44. create session
  45. set talk off
  46. set ldCheck off
  47.  
  48. set procedure to program(1) additive
  49.  
  50. extern CWORD SetCapture(CWORD) User.exe && for sizeForm
  51.  
  52. local bForm,sForm
  53.  
  54. bForm = new buttonForm("Button Form","Click anywhere in the form to move button")
  55. sForm = new sizeForm("Size Form","Click in the form and move mouse to resize form")
  56. sForm.Open()
  57. bForm.Open()
  58.  
  59. *******************************************************************************
  60. *******************************************************************************
  61. class EventForm(name,titleText) of Form
  62.  
  63. * Basic form with a pushbutton in the center
  64. *******************************************************************************
  65.  
  66.    this.top = 0.00
  67.    this.left = 1.35
  68.    this.height = 11.22
  69.    this.width = 51.43
  70.    this.name = name
  71.    this.text = name + ": " + titleText
  72.    this.titleText = titleText
  73.  
  74.  
  75.  
  76.    * custom properties
  77.    this.buttonCnt = 1     && button count on this form
  78.  
  79.  
  80.    this.buttonHeight = 3.06           && initial height and width for buttons
  81.    this.buttonWidth = 10.81
  82.    this.curHeight = this.height    && initial height and width for form
  83.    this.curWidth  = this.width
  84.    this.totalRows = floor(this.height/this.buttonHeight)  && total allowed
  85.    this.totalCols = floor(this.width/this.buttonWidth)    && rows, columns
  86.  
  87.    * a button
  88.    this[1] = new EventButton(this)
  89.  
  90.    * Events
  91.    * These are just some of the events you can detect in a form
  92.  
  93.    this.OnOpen          = {;ShowEvent(this.name,"OnOpen")}
  94.    this.OnGotFocus      = {;ShowEvent(this.name,"OnGotFocus")}
  95.    this.OnSize          = {;ShowEvent(this.name,"OnSize")}
  96.    this.OnLeftMouseDown = {;ShowEvent(this.name,"OnLeftMouseDown")}
  97.    this.OnMove          = {;ShowEvent(this.name,"OnMove")}
  98.    this.OnLostFocus     = {;ShowEvent(this.name,"OnLostFocus")}
  99.    this.OnClose         = {;ShowEvent(this.name,"OnClose")}
  100.  
  101.  
  102. endclass   && EventForm
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. *******************************************************************************
  110. *******************************************************************************
  111. class EventButton(f) of PushButton(f)
  112. *******************************************************************************
  113.    this.height = form.buttonHeight
  114.    this.width = form.buttonWidth
  115.    this.top = (f.height - this.height)/2
  116.    this.left = (f.width - this.width)/2
  117.    this.text = ""
  118.    this.fontname = "MS Sans Serif"
  119.    this.fontsize = 15
  120.    this.fontbold = .t.
  121.  
  122.    * Events
  123.    * These are all the events you can detect in a pushbutton
  124.  
  125.    this.OnMouseMove       = {;ShowEvent(form.className+"."+this.name,"OnMouseMove")}
  126.    this.OnRightMouseUp    = {;ShowEvent(form.className+"."+this.name,"OnRightMouseUp")}
  127.    this.OnRightMouseDown  = {;ShowEvent(form.className+"."+this.name,"OnRightMouseDown")}
  128.    this.OnRightDblClick   = {;ShowEvent(form.className+"."+this.name,"OnRightDblClick")}
  129.    this.OnMiddleMouseUp   = {;ShowEvent(form.className+"."+this.name,"OnMiddleMouseUp")}
  130.    this.OnMiddleMouseDown = {;ShowEvent(form.className+"."+this.name,"OnMiddleMouseDown")}
  131.    this.OnMiddleDblClick  = {;ShowEvent(form.className+"."+this.name,"OnMiddleDblClick")}
  132.    this.OnLeftMouseUp     = {;ShowEvent(form.className+"."+this.name,"OnLeftMouseUp")}
  133.    this.OnLeftMouseDown   = {;ShowEvent(form.className+"."+this.name,"OnLeftMouseDown")}
  134.    this.OnLeftDblClick    = {;ShowEvent(form.className+"."+this.name,"OnLeftDblClick")}
  135.    this.OnLostFocus       = {;ShowEvent(form.className+"."+this.name,"OnLostFocus")}
  136.    this.OnGotFocus        = {;ShowEvent(form.className+"."+this.name,"OnGotFocus")}
  137.    this.OnOpen            = {;ShowEvent(form.className+"."+this.name,"OnOpen")}
  138.    this.When              = {ShowEvent(form.className+"."+this.name,"When")}
  139.    this.OnHelp            = {;ShowEvent(form.className+"."+this.name,"OnHelp")}
  140.    this.OnClick           = {;ShowEvent(form.className+"."+this.name,"OnClick")}
  141.  
  142. endclass   && EventButton
  143.  
  144. *******************************************************************************
  145. *******************************************************************************
  146. class ButtonForm(name,titleText) of EventForm(name,titleText)
  147.  
  148. * Create a form with a button that will move to new coordinates inside
  149. * the form when the left mouse button is clicked (and some other funcky stuff).
  150. *******************************************************************************
  151. this.statusmessage = "Click - move button, Ctrl+Click - copy button,;
  152.    Shift+Click - allign buttons."
  153.  
  154.  
  155.    *******************************************************************************
  156.    procedure OnSize (nType,width,height)
  157.    *******************************************************************************
  158.    local i,r,c,newBHeight,newBWidth,button,hRatio,wRatio
  159.  
  160.    if height <> form.curHeight .or. width <> form.curWidth
  161.       form.OnLeftMouseDown(MK_SHIFT,0,0)   && Align all buttons
  162.       hRatio = height/form.curHeight
  163.       wRatio = width/form.curWidth
  164.       newBHeight = form.buttonHeight * hRatio  && proportional
  165.       newBWidth  = form.buttonWidth  * wRatio  && button height
  166.       r = 0                                                     && and width
  167.       c = 0
  168.       for i = 1 to form.buttonCnt
  169.          button = form[i]
  170.          button.top     = r * newBHeight
  171.          button.left    = c * newBWidth
  172.          button.height  = newBHeight
  173.          button.width   = newBWidth
  174.          button.text    = TRIMSTR(button.top) + "," + TRIMSTR(button.left)
  175.          button.fontSize = button.fontSize * (hRatio + wRatio)/2
  176.          button.visible = .t.
  177.          r = r + 1
  178.          if (r = form.totalRows)
  179.             r = 0
  180.             c = c + 1
  181.          endif
  182.       next i
  183.       form.curHeight    = height              && Reset base height,width
  184.       form.curWidth     = width               && for buttons and current height,
  185.       form.buttonHeight = newBHeight          && width for form
  186.       form.buttonWidth  = newBWidth
  187.       ShowEvent(this.name,"OnSize")
  188.    endif
  189.  
  190.  
  191.    *******************************************************************************
  192.    procedure OnOpen
  193.    ********************************************************************************
  194.    * Give button its location and font
  195.    this.OnLeftMouseDown(0,this[1].left,this[1].top)
  196.  
  197.    *******************************************************************************
  198.    procedure OnLeftMouseDown
  199.    * OnLeftMouseDown event handler for buttonForm
  200.    *
  201.    * Click                -- moves last button to current location
  202.    * Shift + click        -- aligns buttons in form
  203.    * Ctrl + click         -- adds a new button at current location
  204.    *
  205.    *******************************************************************************
  206.    parameters flags,col,row
  207.    private f,i,r,c,button,maxCol,maxRow,bHeight,bWidth,bCnt
  208.    bHeight   = form[1].height
  209.    bWidth    = form[1].width
  210.    if bitand(flags,MK_SHIFT) = MK_SHIFT  && SHIFT key was pressed
  211.       r = 0
  212.       c = 0
  213.       for i = 1 to form.buttonCnt
  214.          button      = form[i]
  215.          button.top  = r * bHeight
  216.          button.left = c * bWidth
  217.          button.text = TRIMSTR(button.top) + "," + TRIMSTR(button.left)
  218.          r = r + 1
  219.          if (r = form.totalRows)
  220.             r = 0
  221.             c = c + 1
  222.          endif
  223.       next i
  224.    else
  225.       if bitand(flags,MK_CONTROL) = MK_CONTROL  && create another button
  226.          *** create another button
  227.          this.buttonCnt = form.buttonCnt + 1           && increase button count
  228.          form[form.buttonCnt] = new EventButton(form)
  229.          form[form.buttonCnt].visible = .f.
  230.       endif
  231.       button = form[form.buttonCnt]   && temporary reference, for easier reading
  232.       button.text = TRIMSTR(row) + "," + TRIMSTR(col)
  233.       button.left = col
  234.       button.top  = row
  235.       button.visible = .t.                 && make button visible only
  236.                                            && after all changes to it have been
  237.                                            && made.
  238.    endif
  239.    button.setFocus()    && So the status message would appear
  240.    endclass   && ButtonForm
  241.  
  242. *******************************************************************************
  243. *******************************************************************************
  244. class SizeForm(name,titleText) of EventForm(name,titleText)
  245.  
  246. * Create  a form that will get resized as you click
  247. * the left mouse button and move.
  248. *******************************************************************************
  249.  
  250.    this.top = 12.86
  251.    this.width = 51.43
  252.    this.height = 8.08
  253.    this.OnSize = CLASS::SizeButton  && Resize the button when form is resized
  254.    this.OnSize()
  255.    this[1].text = "Button"
  256.  
  257.    *******************************************************************************
  258.    procedure OnLeftMouseDown
  259.  
  260.    *  Event handler for sizeForm.
  261.    *  Makes the size of the form change depending on the location of the mouse
  262.    *  while the mouse is pressed.
  263.    *******************************************************************************
  264.    parameters flags, x, y
  265.    this.OnSize = .f.
  266.    SetCapture(this.hwnd)             && Current window receives all mouse messages
  267.    this.OnMouseMove = CLASS::SizeTheForm  && Resize form and button as mouse moves
  268.  
  269.    *******************************************************************************
  270.    procedure OnLeftMouseUp
  271.  
  272.    *  Event handler for sizeForm.
  273.    *  Stop resizing the form while mouse is pressed and moving.  Now assign the
  274.    *  Button resizing to the OnSize event.
  275.    *******************************************************************************
  276.    SetCapture(0)
  277.    this.OnMouseMove = .f.
  278.    this.OnSize = CLASS::SizeButton      && Resize button when form changes size
  279.  
  280.    *******************************************************************************
  281.    procedure SizeTheForm
  282.  
  283.    * Changes the size of the form to reflect the current mouse position
  284.    *******************************************************************************
  285.    parameters  flags,col,row
  286.    this.height = iif(row < 1, 1, row)        && make sure numbers not negative
  287.    this.width = iif(col < 1, 1, col)
  288.    this.SizeButton()
  289.  
  290.  
  291.    *******************************************************************************
  292.    function SizeButton
  293.  
  294.    * Changes button to always be in the center of the form, and the same size
  295.    * relative to the size of the form
  296.    *******************************************************************************
  297.    local button
  298.  
  299.    button = this[1]
  300.    button.fontName = "Serif"
  301.    button.height = this.height/3
  302.    button.width = this.width/3
  303.    button.top = this.height/3.03
  304.    button.left = this.width/2.55
  305.  
  306.    return .t.
  307.  
  308. endclass   && SizeForm
  309.  
  310.  
  311. *******************************************************************************
  312. function ShowEvent
  313.  
  314. * Display the object name in proper case, and the event that happened.
  315. *******************************************************************************
  316. param name,eventName
  317. ?proper(name), " -- ", eventName
  318. return .t.
  319.  
  320. **************************** End of Event.prg *********************************
  321.  
  322.  
  323.