#
# $Id: checkbox.icn,v 1.6 2004/11/11 15:16:35 rparlett Exp $
#
# This file is in the public domain.
#
# Author: Robert Parlett (parlett@dial.pipex.com)
#

package gui
link graphics

$include "guih.icn"

#
# An instance of this class is a small button with a label
# which is either in an on or off state.  The button is an
# Icon image, which may be specified by the user if desired.
#
# The images will default to appropriate values if not specified.
#
# The size will also default if not specified.  The methods
# used to control the images, label and check the status are
# inherited from the superclasses {Button} and {Toggle}.
# @example
# @ c := CheckBox()
# @ c.set_pos(200, 100)
# @ c.set_label("Checkbox")
# @ self.add(c)
#
class CheckBox : Button(tx, tw)
   method resize()
      if /self.label then
         fatal("no label specified")

      #
      # Set the icons if necessary
      #
      if /self.img_up then {
         if /self.parent_check_box_group then
            self.set_imgs(img_style("box_up"), img_style("box_down"))
         else
            self.set_imgs(img_style("diamond_up"), img_style("diamond_down"))
      }

      if /self.h_spec := WAttrib(self.cwin, "fheight") then
         self.h_spec <:= img_h
      #
      # We give extra border space; this looks better with the focus rectangle.
      #
      /self.w_spec := TextWidth(self.cwin, self.label) + 
         self.img_w + DEFAULT_TEXT_X_SURROUND + HIGHLIGHT_TEXT_SPACING

      self.Component.resize()

      self.tx := self.x + self.img_w + DEFAULT_TEXT_X_SURROUND
      self.tw := self.w - self.img_w - DEFAULT_TEXT_X_SURROUND - HIGHLIGHT_TEXT_SPACING
   end
      
   method display(buffer_flag)
      local cw

      if \self.is_down then
         i := if /self.is_checked_flag then img_down else img_up
      else
         i := if \self.is_checked_flag then img_down else img_up

      #
      # Draw image and string centred vertically; image has img_w pixel to its right
      #
      EraseRectangle(self.cbwin, self.x, self.y, self.w, self.h)
      DrawImageEx(self.cbwin, self.x, self.y + (self.h - img_h) / 2, i)
      left_string(self.cbwin, self.tx, self.y + self.h / 2, self.label, self.accel)

      if /self.no_keyboard_flag & \self.has_focus then {
         DashedRectangle(self.cbwin, self.tx - HIGHLIGHT_TEXT_SPACING, self.y, 
                         self.tw + 2 * HIGHLIGHT_TEXT_SPACING, self.h)
      }

      self.do_shading(self.cbwin)

      if /buffer_flag then
         CopyArea(self.cbwin, self.cwin, self.x, self.y, self.w, self.h, self.x, self.y)
   end

   initially(a[])
      self.Button.initially()
      self.toggles_flag := 1
      set_fields(a)
end