(***********************************************************************
   This file is part of GTKpas, an OOP wrapper for GTK+ written
   for Pascal.

   Copyright (C) 2000 by Johann Glaser <Johann.Glaser@gmx.at>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program in the file COPYING; if not, write to the 
   Free Software Foundation, Inc., 
   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
***********************************************************************)
Program Buttons;
{$MODE OBJFPC}
Uses GLib,GDK,GTK,GTKpas;

Type CPixmapButtonWin = Class(CWindow)
                          Function DeleteEventImpl(aEvent:PGdkEventAny):Gint; virtual; override;
                          Procedure Destroy_; virtual; override;
                          Function OnCoolButtonClicked(Sender:CButton;Data:GPointer):GBoolean;
                        End;

Var Window : CWindow;
    Button : CButton;
    Box1   : CBox;

(* It's a good idea to do this for all windows. *)
Function CPixmapButtonWin.DeleteEventImpl(aEvent:PGdkEventAny):Gint;
Begin
  inherited DeleteEventImpl(aEvent);
  DeleteEventImpl := 0;
End;
Procedure CPixmapButtonWin.Destroy_;
Begin
  inherited Destroy_;
  WriteLn('Destroy_');
  Instance.Quit;
End;
(* our callback when the button is pressed *)
Function CPixmapButtonWin.OnCoolButtonClicked(Sender:CButton;Data:GPointer):GBoolean;
Begin
  WriteLn('Hello again - ',PChar(Data),' was pressed');
  OnCoolButtonClicked := False;  { let event handling continue }
End;

(* Create a new hbox with an image and a label packed into it
 * and return the box. *)

Function XPMLabelBox(Parent : CWidget;
                     xpm_filename : PGChar;
                     label_text : PGChar ) : CBox;
Var Box1      : CBox;
    Style     : PGtkStyle;
    Pixmap    : PGdkPixmap;
    PixmapWid : CPixmap;
    Mask      : PGdkBitmap;
    TheLabel  : CLabel;
Begin
  (* Create box for xpm and label *)
  Box1 := CHBox.Create(False, 0);
  Box1.BorderWidth := 2;

  (* Get the style of the button to get the
   * background color. *)
  Style := Parent.Style;

  (* Now on to the xpm stuff *)
  Pixmap := gdk_pixmap_create_from_xpm(PGtkWidget(Parent.GtkObject)^.Window, @Mask,
                                       @Style^.bg[GTK_STATE_NORMAL],
			               xpm_filename);
  PixmapWid := CPixmap.Create(Pixmap,Mask);

  (* Create a label for the button *)
  TheLabel := CLabel.Create(label_text);

  (* Pack the pixmap and label into the box *)
  Box1.PackStart(PixmapWid, FALSE, FALSE, 3);
  Box1.PackStart(TheLabel,  FALSE, FALSE, 3);

  PixmapWid.Show;
  TheLabel.Show;

  XPMLabelBox := Box1;
End;

Const ButtonName : PGChar = 'cool button';

Begin
  Instance := CMain.Create(@ArgC,@ArgV,False);

  (* Create a new window *)
  Window := CPixmapButtonWin.Create(GTK_WINDOW_TOPLEVEL);
  (* Sets the border width of the window. *)
  Window.BorderWidth := 10;
  Window.Title := 'Pixmap''d Buttons!';
  Window.Realize;

  (* Create a new button *)
  Button := CButton.Create;
  (* Connect the 'clicked' signal of the button to our callback *)
  Button.OnClicked := @CPixmapButtonWin(Window).OnCoolButtonClicked;
  (* set data for Clicked signal callback *)
  Button.OnClickedData := ButtonName;

  (* This calls our box creating function *)
  Box1 := XPMLabelBox(Window, 'info.xpm', ButtonName);
  (* Pack and show all our widgets *)
  Box1.Show;

  Button.Add(Box1);
  Button.Show;

  Window.Add(Button);

  Window.Show;

  (* Rest in Run and wait for the fun to begin! *)
  Instance.Run;
End.
