0

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

  • You need to create the window before you ask for the control `d`. Move `win.Show()` above `win.d.show()`. What you are doing here is pretty unorthodox. I have no idea why you'd ever want to do it this way. You should be doing work on `drawingarea1` in the window code or in it's own User-Control – Andy Sep 10 '20 at 00:40
  • Yep, I am definitely lost. I want to be able to tap on a container (drawingarea1) in the GUI window that pops up. I then would like to be able to draw/color shapes in the displayed drawingarea1 object. But, the signals in the Gtk#2.0 default template (MainWindow.cs) do absolutely nothing when I tap on, click, scroll, mouseover, focus, etc., on the GUI screen when it pops up. So you are saying to call up an instance of drawingarea1 in the Main() method which creates the window? – Christopher Davidson Sep 10 '20 at 02:46
  • Try putting your drawing area inside an `EventBox` -- that will give it those capabilities. – Andy Sep 10 '20 at 02:47
  • I will try it. Thanks! – Christopher Davidson Sep 10 '20 at 04:44

0 Answers0