Home  Contents

Dialogs in GTK#

نوافذ(صناديق)الحوار هى جزء مهم فى التطبيقات الرسومية، الحوار هو محادثة بين شخصين او اكثر، هنا المحادثة (المستخدم والتطبيق) وسيلة لإيصال معلومة للتطبيق. يمكن استخدامها فى اضافة بيانات ، تعديلها، او التحكم فى اعدادات البرنامج.. الخ.

يوجد نوعان (نافذة حوار معرفة مسبقا ، او واحدة اعدّت خصيصا)



Message dialogs

صناديق الرسائل هى صناديق تقدم رسائل للمستخدم مكونة من نص وصورة



messages.cs

using Gtk;
using System;
 
class SharpApp : Window {
 

    public SharpApp() : base("Messages")
    {
        SetDefaultSize(250, 100);
        SetPosition(WindowPosition.Center);
        DeleteEvent += delegate { Application.Quit(); }; 

        
        Table table = new Table(2, 2, true);
        
        Button info = new Button("Information");
        Button warn = new Button("Warning");
        Button ques = new Button("Question");
        Button erro = new Button("Error");

        info.Clicked += delegate {
            MessageDialog md = new MessageDialog(this, 
                DialogFlags.DestroyWithParent, MessageType.Info, 
                ButtonsType.Close, "Download completed");
            md.Run();
            md.Destroy();
        };

        warn.Clicked += delegate {
            MessageDialog md = new MessageDialog(this, 
                DialogFlags.DestroyWithParent, MessageType.Warning, 
                ButtonsType.Close, "Unallowed operation");
            md.Run();
            md.Destroy();
        };
        
        
        ques.Clicked += delegate {
            MessageDialog md = new MessageDialog(this, 
                DialogFlags.DestroyWithParent, MessageType.Question, 
                ButtonsType.Close, "Are you sure to quit?");
            md.Run();
            md.Destroy();
        };
        
        erro.Clicked += delegate {
            MessageDialog md = new MessageDialog (this, 
                DialogFlags.DestroyWithParent, MessageType.Error, 
                ButtonsType.Close, "Error loading file");
            md.Run();
            md.Destroy();
        };

        table.Attach(info, 0, 1, 0, 1);
        table.Attach(warn, 1, 2, 0, 1);
        table.Attach(ques, 0, 1, 1, 2);
        table.Attach(erro, 1, 2, 1, 2);
        
        Add(table);

        ShowAll();
    }

    public static void Main()
    {
        Application.Init();
        new SharpApp();
        Application.Run();
    }
}



فى مثالنا سنعرض 4 انواع من صناديق الرسائل (معلومات، تحذير، سؤال، خطأ)



 Button info = new Button("Information");
 Button warn = new Button("Warning");
 Button ques = new Button("Question");
 Button erro = new Button("Error");

لدينا 4 ازرار كل منها سيعرض رسالة مختلفة



 info.Clicked += delegate {
     MessageDialog md = new MessageDialog(this, 
         DialogFlags.DestroyWithParent, MessageType.Info, 
         ButtonsType.Close, "Download completed");
      md.Run();
      md.Destroy();
 };

اذا ضغطنا على الزر info فيتم عرض صندوق رسالة معلومة. و MessageType.Info يحدد نوعها على انها رسالة معلومة. وbuttonsType.Close يحدد الأزرار الظاهرة مع الرسالة، والمعامل الأخير هو الرسالة.

يتم عرض الرسالة بإستخدام الطريقة Run، ويجب على المبرمج استدعاء اى من الطرق Destroy او Hide عند الإنتهاء


Information message dialog   Warning message dialog   Question message dialog   Error message dialog

AboutDialog

صندوق "حول" AboutDialog مسئول عن عرض معلومات حول التطبيق.. تستطيع عرض اللوجو واسم التطبيق، والإصدار، والحقوق، والموقع ، ومعلومات عن الرخصة وايضا التعبير عن امتننانك للكتاب والموثقين والمترجمين والمصممين



aboutdialog.cs

using Gtk;
using System;
 
class SharpApp : Window {
 

    public SharpApp() : base("About")
    {
        SetDefaultSize(300, 270);
        SetPosition(WindowPosition.Center);
        DeleteEvent += delegate { Application.Quit(); } ;
        
        Button button = new Button("About");
        button.Clicked += OnClicked;
        
        Fixed fix = new Fixed();
        fix.Put(button, 20, 20);
        Add(fix);

        ShowAll();
    }


    void OnClicked(object sender, EventArgs args)
    {
        AboutDialog about = new AboutDialog();
        about.ProgramName = "Battery";
        about.Version = "0.1";
        about.Copyright = "(c) Jan Bodnar";
        about.Comments = @"Battery is a simple tool for 
battery checking";
        about.Website = "http://www.zetcode.com";
        about.Logo = new Gdk.Pixbuf("battery.png");
        about.Run();
        about.Destroy();
    }


    public static void Main()
    {
        Application.Init();
        new SharpApp();
        Application.Run();
    }
}

هذا المثال يستخدم بعض مميزات الصف AboutDialog

 AboutDialog about = new AboutDialog();

ننشئ كائن من الصف AboutDialog

 about.ProgramName = "Battery";
 about.Version = "0.1";
 about.Copyright = "(c) Jan Bodnar";

بتحديد تلك الخصائص نقوم بتحديد اسم البرنامج والإصدار وحقوق النسخ

 about.Logo = new Gdk.Pixbuf("battery.png");

وهذا السطر ينشئ اللوجو


AboutDialog

Figure: AboutDialog



FontSelectionDialog

صندوق لإختيار خط ما

fontdialog.cs

using Gtk;
using System;
 
class SharpApp : Window {
 
    Label label;

    public SharpApp() : base("Font Selection Dialog")
    {
        SetDefaultSize(300, 220);
        SetPosition(WindowPosition.Center);
        DeleteEvent += delegate { Application.Quit(); } ;
        
        label = new Label("The only victory over love is flight.");
        Button button = new Button("Select font");
        button.Clicked += OnClicked;

        Fixed fix = new Fixed();
        fix.Put(button, 100, 30);
        fix.Put(label, 30, 90);
        Add(fix);

        ShowAll();
    }


    void OnClicked(object sender, EventArgs args)
    {
        FontSelectionDialog fdia = new FontSelectionDialog("Select font name");
        fdia.Response += delegate (object o, ResponseArgs resp) {

            if (resp.ResponseId == ResponseType.Ok) {
               Pango.FontDescription fontdesc = 
                   Pango.FontDescription.FromString(fdia.FontName);
               label.ModifyFont(fontdesc);
            }
        };

        fdia.Run();
        fdia.Destroy();
    }

    public static void Main()
    {
        Application.Init();
        new SharpApp();
        Application.Run();
    }
}

فى هذه الجزئية لدينا زر و label ، ونعرض صندوق اختيار الخط عند الضغط على الزر



 FontSelectionDialog fdia = new FontSelectionDialog("Select font name");

ننشئ كائن صندوق اختيار الخط

 fdia.Response += delegate (object o, ResponseArgs resp) {

     if (resp.ResponseId == ResponseType.Ok) {
         Pango.FontDescription fontdesc = Pango.FontDescription.FromString(fdia.FontName);
         label.ModifyFont(fontdesc);
     }
 };

اذا ضغطنا زر OK يتم تعديل الخط الخاص بال label للخط المختار


FontSelectionDialog

Figure: FontSelectionDialog



ColorSelectionDialog

صندوق لإختيار لون ما

colordialog.cs

using Gtk;
using System;
 
class SharpApp : Window {
 
    Label label;

    public SharpApp() : base("Color Dialog")
    {
        SetDefaultSize(300, 220);
        SetPosition(WindowPosition.Center);
        DeleteEvent += delegate { Application.Quit(); } ;
        
        label = new Label("The only victory over love is flight.");
        Button button = new Button("Select color");
        button.Clicked += OnClicked;

        Fixed fix = new Fixed();
        fix.Put(button, 100, 30);
        fix.Put(label, 30, 90);
        Add(fix);

        ShowAll();
    }


    void OnClicked(object sender, EventArgs args)
    {
        ColorSelectionDialog cdia = new ColorSelectionDialog("Select color");
        cdia.Response += delegate (object o, ResponseArgs resp) {

            if (resp.ResponseId == ResponseType.Ok) {
               label.ModifyFg(StateType.Normal, cdia.ColorSelection.CurrentColor);
            }
        };

        cdia.Run();
        cdia.Destroy();
    }


    public static void Main()
    {
        Application.Init();
        new SharpApp();
        Application.Run();
    }
}

هذا المثال مشابه للأعلى ولكن حان الوقت لنغير لون ال label

 ColorSelectionDialog cdia = new ColorSelectionDialog("Select color");

ننشئ كائن صندوق اختيار اللون

 cdia.Response += delegate (object o, ResponseArgs resp) {

     if (resp.ResponseId == ResponseType.Ok) {
        label.ModifyFg(StateType.Normal, cdia.ColorSelection.CurrentColor);
     }
 };

اذا ضغطنا OK نحصل على اللون ونغير اللون المستخدم للون المختار


ColorSelectionDialog

Figure: ColorSelectionDialog




Home ‡ Contents ‡ Top of Page