GTK# Widget Gtk:: DrawingArea not displaying or responding to signals

374 views Asked by At

I am using MonoDevelop IDE on linux/UNIX machine and coding in C# and .NET framework. I cannot seem to figure out how to get the Gtk::DrawingArea to show up in the parent Gtk.Window when the project builds?

I have tried adding a ::drawingArea object manually into the Code, placing it directly on the window (in the design view, see screenshot) and then I also tried placing the ::drawingArea object within a Fixed box.

Below is the code that I am trying to implement. The Design is as basic as you can get, just so I can understand catching and working with the signals to the drawingArea - it has the parent window (MainWindow) and the drawing area (drawingarea1).

Screenshot

My question: How can I get a drawingArea object to display in a Gtk.Window? And, more importantly is there something I am missing to make the object respond to signals that are set to the drawingarea? I want to ultimately be able to draw and color inside the drawing area window but as of right now I can't even get the drawing area widget to display and catch signals.

Thanks in advance!

MainWidow.cs

using System;
using Gtk;

public partial class MainWindow : Gtk.Window
{


    public DrawingArea d;

    public MainWindow() : base(Gtk.WindowType.Toplevel)
    {
        d = drawingarea1;
        Build();
    }

    protected void OnDeleteEvent(object sender, DeleteEventArgs a)
    {
        Application.Quit();
        a.RetVal = true;
    }

    protected void OnExpose(object o, ExposeEventArgs args)
    {
        Console.WriteLine("OnExpose...");
    }

    protected void OnRealization(object sender, EventArgs e)
    {
        Console.WriteLine("OnRealization...");
    }

    protected void OnDragMotion(object o, DragMotionArgs args)
    {
        Console.WriteLine("OnDragMotion...");
    }
}

Driver.cs

using System;
using Gtk;

    class MainClass
    {
        public static void Main(string[] args)
        {
            Application.Init();

            MainWindow win = new MainWindow();
            // throws NULLREFERENCE EXECPTION
            win.d.Show();
         
            win.Show();

            Application.Run();
        }
    }

//******* PARTIAL SOLUTION **********//

ColorSelectorDraw.cs

using System;
using Gtk;
using Gdk;

namespace GUIDraw4
{
    public class DemoColorSelection : Gtk.Window
    {
        private Gdk.Color color;
        private Gtk.DrawingArea drawingArea;

        public DemoColorSelection() : base("Color Selection")
        {
            BorderWidth = 8;
            VBox vbox = new VBox(false, 8);
            vbox.BorderWidth = 8;
            Add(vbox);

            // Create the color swatch area
            Frame frame = new Frame();
            frame.ShadowType = ShadowType.In;
            vbox.PackStart(frame, true, true, 0);

            drawingArea = new DrawingArea();
            drawingArea.ExposeEvent += new ExposeEventHandler(ExposeEventCallback);
            // set a minimum size
            drawingArea.SetSizeRequest(400, 250);
            // set the color
            color = new Gdk.Color(255, 255, 255);
            drawingArea.ModifyBg(StateType.Normal, color);
            frame.Add(drawingArea);

            Alignment alignment = new Alignment(1.0f, 0.5f, 0.0f, 0.0f);
            Button button = new Button("_Change the above color");
            button.Clicked += new EventHandler(ChangeColorCallback);
            alignment.Add(button);
            vbox.PackStart(alignment);

            ShowAll();
        }

        protected override bool OnDeleteEvent(Gdk.Event evt)
        {
            Destroy();
            return true;
        }

        // Expose callback for the drawing area
        private void ExposeEventCallback(object o, ExposeEventArgs args)
        {
            EventExpose eventExpose = args.Event;
            Gdk.Window window = eventExpose.Window;
            Rectangle area = eventExpose.Area;

            window.DrawRectangle(drawingArea.Style.BackgroundGC(StateType.Normal),
                          true,
                          area.X, area.Y,
                          area.Width, area.Height);
            args.RetVal = true;
        }

        private void ChangeColorCallback(object o, EventArgs args)
        {
            using (ColorSelectionDialog colorSelectionDialog = new ColorSelectionDialog("Changing color"))
            {
                colorSelectionDialog.TransientFor = this;
                colorSelectionDialog.ColorSelection.PreviousColor = color;
                colorSelectionDialog.ColorSelection.CurrentColor = color;
                colorSelectionDialog.ColorSelection.HasPalette = true;

                if (colorSelectionDialog.Run() == (int)ResponseType.Ok)
                {
                    Gdk.Color selected = colorSelectionDialog.ColorSelection.CurrentColor;
                    drawingArea.ModifyBg(StateType.Normal, selected);
                }

                colorSelectionDialog.Hide();
            }
        }
    }
}

Program.cs

using System;
using Gtk;

namespace GUIDraw4
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Application.Init();
            //MainWindow win = new MainWindow();
            //win.Show();

            DemoColorSelection cs = new DemoColorSelection();
            Application.Run();
        }
    }
}

This seemed to at least get an interactive drawing board up on the GUI. Taken from csharp.hotexamples.com

0

There are 0 answers