#
# $Id: iconbutton.icn,v 1.5 2006-07-09 23:43:07 rparlett Exp $
#
# This file is in the public domain.
#
# Author: Robert Parlett (parlett@dial.pipex.com)
#

package gui
link graphics

$include "guih.icn"

#
# This is a button with an Icon image within it.
#
# There is a useful program in the Icon program library called
# {xpmtoims}, which will take an xpm file and output the
# equivalent Icon image string, which can then be inserted
# into a program.  See also the X window programs sxpm and
# pixmap for viewing and editing xpm files respectively.
# 
# A border may be requested with {set_draw_border().}
#
# Unless explicitly specified, the size will default to the
# image's size, plus a standard surround if a border is
# requested.
# @example
# @ Create a button with a diamond image and a border
# @ b := IconButton()
# @ b.toggle_draw_border()
# @ b.set_img("11,c1,_
# @ ~~~~~0~~~~~_
# @ ~~~~000~~~~_
# @ ~~0000000~~_
# @ ~000000000~_
# @ ~~0000000~~_
# @ ~~~~000~~~~_
# @ ~~~~~0~~~~~_
# @")
#
class IconButton : Button()
   method display(buffer_flag)
      local i, cw

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

      EraseRectangle(self.cbwin, self.x, self.y, self.w, self.h)

      DrawImageEx(self.cbwin, self.x + (self.w - self.img_w) / 2, self.y + (self.h - self.img_h) / 2, i)
      if \self.is_down then {
         #
         # Invert the inside of the area
         #
         cw := Clone(self.cbwin, "drawop=reverse")
         FillRectangle(cw, self.x, self.y, self.w, self.h)
         Uncouple(cw)
         DrawSunkenRectangle(self.cbwin, self.x, self.y, self.w, self.h)
      } else {
         if \self.draw_border_flag then {
            if /self.is_checked_flag then
               DrawRaisedRectangle(self.cbwin, self.x, self.y, self.w, self.h)
            else
               DrawSunkenRectangle(self.cbwin, self.x, self.y, self.w, self.h)
         }
      }

      if /self.no_keyboard_flag & \self.has_focus then {
         DashedRectangle(self.cbwin, self.x + BORDER_WIDTH + 1, self.y + BORDER_WIDTH + 1, self.w - 2 * BORDER_WIDTH - 2, self.h - 2 * BORDER_WIDTH - 2)
      }

      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)
      return
   end

   method resize()
      if /self.img_up | /self.img_down then
         fatal("no image specified")
      if \self.draw_border_flag then {
         /self.w_spec := self.img_w + 2 * DEFAULT_TEXT_X_SURROUND
         /self.h_spec := self.img_h + 2 * DEFAULT_TEXT_Y_SURROUND
      } else {
         /self.w_spec := self.img_w
         /self.h_spec := self.img_h
      }
      self.Component.resize()
   end

   initially(a[])
      self.Button.initially()
      set_fields(a)
end
