Gtk3アプリ ダイアログを利用する

####メッセージダイアログを利用する

<img width="300" alt="Screenshot from 2021-06-14 14-20-30.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/182324/e89e5f30-d93e-fee3-5ba4-b56616426578.png">
####ダイアログのサンプルサイトが見つかる

http://docs.go-mono.com/?link=T%3aGtk.MessageDialog

https://github.com/mono/gtk-sharp/blob/main/doc/en/Gtk/FileChooserDialog.xml

monoですが利用できます。
C#のダイアログのサンプルプログラムがそのまま載っています。他のダイアログサンプルも載っています。

galdeに配置したメッセージダイアログを配置したやり方
<img width="400" alt="Screenshot from 2021-06-14 14-22-21.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/182324/13438ca5-910b-0213-57d7-eecca53409d6.png">

galdeに配置した場合、プロパティをGUIから変更できます。
<img width="400" alt="Screenshot from 2021-06-14 14-31-12.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/182324/80b64ec0-389a-9473-3029-acc076f19b04.png">

プログラムでやる場合はMessageDialogを宣言します

```cs
      MessageDialog md = new MessageDialog (this, 
      DialogFlags,
      MessageType.Question, 
      ButtonsType.YesNo, "Are you sure you want to quit?");

      ResponseType result = (ResponseType)md.Run ();
		if (result == ResponseType.Yes)
			Application.Quit();
		else
			md.Destroy();
```

```cs
using System;
using Gtk;
using UI = Gtk.Builder.ObjectAttribute;

namespace SubWinGtkApplication
{
    partial class MainWindow
    {
		//Gladeに配置したやり方
		private void on_MainWindow_mesWinOpen_Clicked(object sender , EventArgs e){
			
			ResponseType result = (ResponseType)mesDialog.Run ();

			if (result == ResponseType.Yes)
				Application.Quit();
			else
				mesDialog.Destroy();
		}

		//プログラムですべてやるやり方
		private void on_MainWindow_mesWinOpen2_Clicked(object sender , EventArgs e){
			
			MessageDialog md = new MessageDialog (this, 
				DialogFlags,
				MessageType.Question, 
				ButtonsType.YesNo, "Are you sure you want to quit?");
	
			ResponseType result = (ResponseType)md.Run ();

			if (result == ResponseType.Yes)
				Application.Quit();
			else
				md.Destroy();
		}
    }
}

```

builder.Autoconnect(this)でgldadeのweditとソースのwedit変数をフェッチしています。

```
builder.Autoconnect(this);
```


```cs
using System;
using Gtk;
using UI = Gtk.Builder.ObjectAttribute;
using GtkSharp.Webkitgtk3Sharp;

namespace SubWinGtkApplication
{
    partial class MainWindow
    {
		[UI] private readonly Gtk.MessageDialog mesDialog = null;
		[UI] private readonly Gtk.Button mesWinOpen = null;
		[UI] private readonly Gtk.Button mesWinOpen2 = null;
	}
}

namespace SubWinGtkApplication
{
    partial class MainWindow : Window
    {

        public MainWindow() : this(new Builder("MainWindow.glade"))
        {

        }

        private MainWindow(Builder builder) : base(builder.GetRawOwnedObject("MainWindow"))
        {
            builder.Autoconnect(this);
        }
    }
}

```

####Blocks風に書く
Objective-cのBlocks風な書き方にする

その場でMessageDialogを呼び出して結果を返します。

```cs
   public void _mkMesDialog(Window win ,string title,Action<MessageDialog,ResponseType> func)
   {
       MessageDialog md = new MessageDialog (win, 
          DialogFlags.DestroyWithParent,
          MessageType.Question, 
          ButtonsType.YesNo, title);
	  ResponseType result = (ResponseType)md.Run ();
       func(md , result);
   }

   _mkMesDialog(this, "タイトル", delegate(MessageDialog md, ResponseType result)
   {
      if (result == ResponseType.Yes)
          Application.Quit();
      else
          md.Destroy();
   });

```

```cs
using System;
using Gtk;
using UI = Gtk.Builder.ObjectAttribute;

namespace SubWinGtkApplication2
{
    class MainWindow : Window
    {
        public void _mkMesDialog(Window win ,Action<MessageDialog,ResponseType> func)
        {
            MessageDialog md = new MessageDialog (win, 
                DialogFlags.DestroyWithParent,
                MessageType.Question, 
                ButtonsType.YesNo, "Are you sure you want to quit?");
	
            ResponseType result = (ResponseType)md.Run ();
            func(md , result);
        }
    
        public MainWindow() : this(new Builder("MainWindow.glade"))
        {
            _mkMesDialog(this, delegate(MessageDialog md, ResponseType result)
            {
                if (result == ResponseType.Yes)
                    Application.Quit();
                else
                    md.Destroy();
            });
        }

        private MainWindow(Builder builder) : base(builder.GetRawOwnedObject("MainWindow"))
        {
            builder.Autoconnect(this);
        }
    }
}

```
```
﻿<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.18" />
  <object class="GtkWindow" id="MainWindow">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Example Window</property>
    <property name="default_width">480</property>
    <property name="default_height">240</property>
    <child>
      <placeholder />
    </child>
    <child>
      <object class="GtkLayout">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkButton" id="mesWinOpen">
            <property name="label" translatable="yes">メッセージボタン</property>
            <property name="width_request">134</property>
            <property name="height_request">36</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <signal name="clicked" handler="on_MainWindow_mesWinOpen_Clicked" swapped="false" />
          </object>
          <packing>
            <property name="x">20</property>
            <property name="y">16</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="mesWinOpen2">
            <property name="label" translatable="yes">メッセージボタン２</property>
            <property name="width_request">159</property>
            <property name="height_request">34</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <signal name="clicked" handler="on_MainWindow_mesWinOpen2_Clicked" swapped="false" />
          </object>
          <packing>
            <property name="x">23</property>
            <property name="y">76</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkMessageDialog" id="mesDialog">
    <property name="can_focus">False</property>
    <property name="type_hint">dialog</property>
    <property name="buttons">ok-cancel</property>
    <child internal-child="vbox">
      <object class="GtkBox">
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <property name="spacing">2</property>
        <child internal-child="action_area">
          <object class="GtkButtonBox">
            <property name="can_focus">False</property>
            <property name="homogeneous">True</property>
            <property name="layout_style">end</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">False</property>
            <property name="position">0</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
```

####サブウィンドウを開く
同じグレードファイルの中にサブウィンドウを置いて、同じMainWindowクラス内定義し開くやり方

<img width="400" alt="Screenshot from 2021-06-15 15-48-52.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/182324/e5c0b547-56c4-fb82-c7b1-849f5244415f.png">

```cs
namespace SubWinGtkApplication2
{
    partial class MainWindow
    {

	    [UI] private readonly Gtk.Window subWin = null;

	    private void on_MainWindow_mainBtn_Clicked(object sender , EventArgs e){			
			subWin.Show();			
	    }
    }
}
```


[Gtk3アプリ ポップアップウィンドウを作る](https://qiita.com/tana_/items/8cb89aae07a217778b47)に続く