[lazarus] dialogs 0.3
Sergio Kessler
sak at perio.unlp.edu.ar
Fri May 21 02:50:02 EDT 1999
Hi all,
dialogs.pp 0.3 is out
I've tested extensively and is working fine, if something is
broke on your pc, you are not following th How-To :)
attached and pasted here:
//==================================================================
//
// Dialogs Unit
//
// Contains : TOpenFileDialog
// TSaveFileDialog
// ShowMessage
//
//-----------------------------------------------------------------
//
// Authors: Sergio A. Kessler
//
// License: in consideration (MPL ?).
//
// Version: 0.3
//
//==================================================================
unit dialogs;
{$mode objfpc}
interface
uses
gtk, classes, Controls, vclGlobals, sysutils;
type
TCustomDialog = class(TWinControl)
private
FUserChoice: (user_thinking, user_ok, user_cancel, user_close);
function gtk_Ok( widget: PGtkWidget) : Boolean; cdecl;
function gtk_Cancel( widget: PGtkWidget) : Boolean; cdecl;
function gtk_Close( widget: PGtkWidget) : Boolean; cdecl;
protected
public
constructor Create(AOwner : TComponent); override;
destructor destroy; override;
end;
TOpenFileDialog = class(TCustomDialog)
private
FFileName : AnsiString;
FTitle: AnsiString;
FFilter: AnsiString;
procedure gtk_create;
procedure gtk_destroy;
public
constructor Create (AOwner : TComponent); override;
destructor destroy; override;
Function Execute : boolean;
property FileName : AnsiString read FFileName write FFileName;
property Title : AnsiString read FTitle write FTitle;
property Filter : AnsiString read FFilter write FFilter;
end;
TSaveFileDialog = class(TOpenFileDialog);
procedure ShowMessage( const msg: AnsiString );
Implementation
var btn_ok: boolean;
//----------------------------------------------------------------------------
function TCustomDialog.gtk_Ok( widget: PGtkWidget) : Boolean; cdecl;
begin
writeln('--> in gtk_ok');
FUserChoice := user_ok;
end;
//----------------------------------------------------------------------------
function TCustomDialog.gtk_Cancel( widget: PGtkWidget) : Boolean; cdecl;
begin
writeln('--> in gtk_cancel');
FUserChoice := user_cancel;
end;
//----------------------------------------------------------------------------
function TCustomDialog.gtk_Close( widget: PGtkWidget) : Boolean; cdecl;
begin
writeln('--> in gtk_close');
FUserChoice := user_close;
end;
//----------------------------------------------------------------------------
constructor TCustomDialog.Create (AOwner : TComponent);
begin
// the TCustomDialog should not create the file_selection
inherited Create(AOwner);
End;
//----------------------------------------------------------------------------
destructor TCustomDialog.destroy;
begin
inherited Destroy;
End;
//----------------------------------------------------------------------------
constructor TOpenFileDialog.Create (AOwner : TComponent);
begin
inherited Create(AOwner);
gtk_create;
end;
//----------------------------------------------------------------------------
destructor TOpenFileDialog.destroy;
begin
gtk_destroy;
inherited Destroy;
end;
//----------------------------------------------------------------------------
Function TOpenFileDialog.Execute : boolean;
begin
if not (GTK_IS_FILE_SELECTION( FComponent)) then gtk_create;
if FTitle <> '' then
begin
gtk_window_set_title( @((PGtkFileSelection( FComponent))^.Window),
PChar(FTitle));
end;
if FFileName <> '' then
begin
gtk_file_selection_set_filename( PGtkFileSelection( FComponent),
PChar(FFileName));
end;
if FFilter <> '' then
begin
gtk_file_selection_complete( PGtkFileSelection( FComponent),
PChar(FFilter));
end;
FUserChoice := user_thinking;
writeln('--> about to file_sel_show');
Show;
repeat
gtk_main_iteration;
until FUserChoice <> user_thinking;
writeln('--> about to file_sel_hide');
if (FUserChoice <> user_close) then Hide;
Execute := (FUserChoice = user_ok);
if Execute then
begin
FFileName := gtk_file_selection_get_filename( PGtkFileSelection(
FComponent));
end;
end;
//----------------------------------------------------------------------------
Procedure TOpenFileDialog.gtk_create;
begin
FComponent := gtk_file_selection_new( PChar( FTitle));
gtk_window_set_modal( @((PGtkFileSelection( FComponent))^.Window),
True);
gtk_signal_connect_object( gtk_object(
(PGtkFileSelection(FComponent))^.ok_button),
'clicked',
gtk_signal_func( @gtk_ok),
gtk_object( self));
gtk_signal_connect_object( gtk_object(
(PGtkFileSelection(FComponent))^.cancel_button),
'clicked',
gtk_signal_func( @gtk_cancel),
gtk_object( self));
gtk_signal_connect_object( gtk_object( FComponent),
'destroy',
gtk_signal_func( @gtk_close),
gtk_object( self));
end;
//----------------------------------------------------------------------------
Procedure TOpenFileDialog.gtk_destroy;
begin
if (GTK_IS_FILE_SELECTION( FComponent)) then
begin
writeln('--> destroying file_selection');
gtk_signal_handlers_destroy( gtk_object(
(PGtkFileSelection(FComponent))^.ok_button));
gtk_signal_handlers_destroy( gtk_object(
(PGtkFileSelection(FComponent))^.cancel_button));
gtk_signal_handlers_destroy( gtk_object( FComponent));
gtk_widget_destroy( PGtkWidget( FComponent));
end;
end;
{function TCustomDialog.GetFileName : String;
Begin
// this is buggy because you don't know if the widget is destroyed !!
FFileName := gtk_file_selection_get_filename( PGtkFileSelection(
FComponent));
GetFileName := FFileName;
End;}
//----------------------------------------------------------------------------
procedure ShowMessage( const msg: AnsiString );
var dlg: PGtkWidget;
btn: PGtkWidget;
lab: PGtkWidget;
function gtk_btn_ok( widget: PGtkWidget) : Boolean; cdecl;
begin
writeln('--> in gtk_btn_ok');
btn_ok := true;
end;
begin
// this fu* shit is absolutely trivial in Gnome
dlg := gtk_dialog_new;
btn := gtk_button_new_with_label('Ok');
lab := gtk_label_new( PChar(msg));
gtk_box_pack_start( PGtkBox( PGtkDialog( dlg)^.action_area), btn, True,
True, 0);
gtk_box_pack_start( PGtkBox( PGtkDialog( dlg)^.vbox), lab, True, True,
0);
gtk_signal_connect_object( gtk_object( btn),
'clicked',
gtk_signal_func( @gtk_btn_ok),
gtk_object( btn));
gtk_signal_connect_object( gtk_object( dlg),
'destroy',
gtk_signal_func( @gtk_btn_ok),
gtk_object( dlg));
btn_ok := false;
gtk_widget_show( btn);
gtk_widget_show( lab);
gtk_widget_show( dlg);
repeat
gtk_main_iteration;
until btn_ok;
if GTK_IS_DIALOG( dlg) then
begin
gtk_widget_hide( dlg);
gtk_signal_handlers_destroy( gtk_object( btn));
gtk_widget_destroy( dlg);
end;
end;
end.
--
| Sergio A. Kessler http://perio.unlp.edu.ar/~sergio
-O_O- Keep working at it... you will either succeed, or become an expert.
//==================================================================
//
// Dialogs Unit
//
// Contains : TOpenFileDialog
// TSaveFileDialog
// ShowMessage
//
//-----------------------------------------------------------------
//
// Authors: Sergio A. Kessler
//
// License: in consideration (MPL ?).
//
// Version: 0.3
//
//==================================================================
unit dialogs;
{$mode objfpc}
interface
uses
gtk, classes, Controls, vclGlobals, sysutils;
type
TCustomDialog = class(TWinControl)
private
FUserChoice: (user_thinking, user_ok, user_cancel, user_close);
function gtk_Ok( widget: PGtkWidget) : Boolean; cdecl;
function gtk_Cancel( widget: PGtkWidget) : Boolean; cdecl;
function gtk_Close( widget: PGtkWidget) : Boolean; cdecl;
protected
public
constructor Create(AOwner : TComponent); override;
destructor destroy; override;
end;
TOpenFileDialog = class(TCustomDialog)
private
FFileName : AnsiString;
FTitle: AnsiString;
FFilter: AnsiString;
procedure gtk_create;
procedure gtk_destroy;
public
constructor Create (AOwner : TComponent); override;
destructor destroy; override;
Function Execute : boolean;
property FileName : AnsiString read FFileName write FFileName;
property Title : AnsiString read FTitle write FTitle;
property Filter : AnsiString read FFilter write FFilter;
end;
TSaveFileDialog = class(TOpenFileDialog);
procedure ShowMessage( const msg: AnsiString );
Implementation
var btn_ok: boolean;
//----------------------------------------------------------------------------
function TCustomDialog.gtk_Ok( widget: PGtkWidget) : Boolean; cdecl;
begin
writeln('--> in gtk_ok');
FUserChoice := user_ok;
end;
//----------------------------------------------------------------------------
function TCustomDialog.gtk_Cancel( widget: PGtkWidget) : Boolean; cdecl;
begin
writeln('--> in gtk_cancel');
FUserChoice := user_cancel;
end;
//----------------------------------------------------------------------------
function TCustomDialog.gtk_Close( widget: PGtkWidget) : Boolean; cdecl;
begin
writeln('--> in gtk_close');
FUserChoice := user_close;
end;
//----------------------------------------------------------------------------
constructor TCustomDialog.Create (AOwner : TComponent);
begin
// the TCustomDialog should not create the file_selection
inherited Create(AOwner);
End;
//----------------------------------------------------------------------------
destructor TCustomDialog.destroy;
begin
inherited Destroy;
End;
//----------------------------------------------------------------------------
constructor TOpenFileDialog.Create (AOwner : TComponent);
begin
inherited Create(AOwner);
gtk_create;
end;
//----------------------------------------------------------------------------
destructor TOpenFileDialog.destroy;
begin
gtk_destroy;
inherited Destroy;
end;
//----------------------------------------------------------------------------
Function TOpenFileDialog.Execute : boolean;
begin
if not (GTK_IS_FILE_SELECTION( FComponent)) then gtk_create;
if FTitle <> '' then
begin
gtk_window_set_title( @((PGtkFileSelection( FComponent))^.Window),
PChar(FTitle));
end;
if FFileName <> '' then
begin
gtk_file_selection_set_filename( PGtkFileSelection( FComponent),
PChar(FFileName));
end;
if FFilter <> '' then
begin
gtk_file_selection_complete( PGtkFileSelection( FComponent),
PChar(FFilter));
end;
FUserChoice := user_thinking;
writeln('--> about to file_sel_show');
Show;
repeat
gtk_main_iteration;
until FUserChoice <> user_thinking;
writeln('--> about to file_sel_hide');
if (FUserChoice <> user_close) then Hide;
Execute := (FUserChoice = user_ok);
if Execute then
begin
FFileName := gtk_file_selection_get_filename( PGtkFileSelection( FComponent));
end;
end;
//----------------------------------------------------------------------------
Procedure TOpenFileDialog.gtk_create;
begin
FComponent := gtk_file_selection_new( PChar( FTitle));
gtk_window_set_modal( @((PGtkFileSelection( FComponent))^.Window), True);
gtk_signal_connect_object( gtk_object( (PGtkFileSelection(FComponent))^.ok_button),
'clicked',
gtk_signal_func( @gtk_ok),
gtk_object( self));
gtk_signal_connect_object( gtk_object( (PGtkFileSelection(FComponent))^.cancel_button),
'clicked',
gtk_signal_func( @gtk_cancel),
gtk_object( self));
gtk_signal_connect_object( gtk_object( FComponent),
'destroy',
gtk_signal_func( @gtk_close),
gtk_object( self));
end;
//----------------------------------------------------------------------------
Procedure TOpenFileDialog.gtk_destroy;
begin
if (GTK_IS_FILE_SELECTION( FComponent)) then
begin
writeln('--> destroying file_selection');
gtk_signal_handlers_destroy( gtk_object( (PGtkFileSelection(FComponent))^.ok_button));
gtk_signal_handlers_destroy( gtk_object( (PGtkFileSelection(FComponent))^.cancel_button));
gtk_signal_handlers_destroy( gtk_object( FComponent));
gtk_widget_destroy( PGtkWidget( FComponent));
end;
end;
{function TCustomDialog.GetFileName : String;
Begin
// this is buggy because you don't know if the widget is destroyed !!
FFileName := gtk_file_selection_get_filename( PGtkFileSelection( FComponent));
GetFileName := FFileName;
End;}
//----------------------------------------------------------------------------
procedure ShowMessage( const msg: AnsiString );
var dlg: PGtkWidget;
btn: PGtkWidget;
lab: PGtkWidget;
function gtk_btn_ok( widget: PGtkWidget) : Boolean; cdecl;
begin
writeln('--> in gtk_btn_ok');
btn_ok := true;
end;
begin
// this fu* shit is absolutely trivial in Gnome
dlg := gtk_dialog_new;
btn := gtk_button_new_with_label('Ok');
lab := gtk_label_new( PChar(msg));
gtk_box_pack_start( PGtkBox( PGtkDialog( dlg)^.action_area), btn, True, True, 0);
gtk_box_pack_start( PGtkBox( PGtkDialog( dlg)^.vbox), lab, True, True, 0);
gtk_signal_connect_object( gtk_object( btn),
'clicked',
gtk_signal_func( @gtk_btn_ok),
gtk_object( btn));
gtk_signal_connect_object( gtk_object( dlg),
'destroy',
gtk_signal_func( @gtk_btn_ok),
gtk_object( dlg));
btn_ok := false;
gtk_widget_show( btn);
gtk_widget_show( lab);
gtk_widget_show( dlg);
repeat
gtk_main_iteration;
until btn_ok;
if GTK_IS_DIALOG( dlg) then
begin
gtk_widget_hide( dlg);
gtk_signal_handlers_destroy( gtk_object( btn));
gtk_widget_destroy( dlg);
end;
end;
end.
More information about the Lazarus
mailing list