dgtkd

D Classes, OOP and GTKd Notebook


I am trying to append a page to a gtk notebook using gtkd from the second class (MyNewClass) below. The notebook is created within the first class, main_window and is called by the second. The program compiles fine, however when I open the program the gtk mainwindow is blank except for the tester box.

import gtk.Box;
import gtk.Button;
import gtk.Grid;
import gtk.Label;
import gtk.MainWindow;
import gtk.Main;
import gtk.Notebook;

class main_window : MainWindow
{
  Notebook notebook;

  this()
  {
super("MyProg");
    setDefaultSize(600,100);

       //Here is the creation of the notebook
 this.notebook=new Notebook;

Box tester=new Box(Orientation.VERTICAL, 1);  
notebook.appendPage(tester, new Label("test")); //This works fine from this class

 Grid grid=new Grid();
 grid.setColumnSpacing(12);  //establish the main grid
 grid.setRowSpacing(3);
 grid.attach(notebook, 0,0,1,9);
add(grid);
showAll();
  }
}

class MyNewClass : main_window
{
    this()
    {
File MFile = File("file.txt", "r");

    Grid MGrid;
  int row=0;
 int col=0;  //Set the column and row number for the gtk grid.
  string[] list;
 string i;
float p;
Label MLabel;


          while(!MFile.eof)
          {
             if (match(line, `\[\[`)){
             MGrid=new Grid();
        MGrid.setColumnSpacing(12);
         MGrid.setRowSpacing(3);
             row=0;

             line=replace(line, regex(r"(\[)", "g"), "");
            line=replace(line, regex(r"(\])", "g"), "");

                //I HAVE USED A TEST WRITELN HERE TO MAKE SURE THE FUNCTION IS CALLED.
                //Below is the notebook append that fails. When I test it from the first class above, I can append. When I call it here, it compiles but nothing is done.

                Box MBox=new Box(Orientation.VERTICAL, 1);
                MBox.add(MGrid);
         super.notebook.appendPage(MBox, new Label(line));//
          }
      }
}

void main(string[] args)
{
Main.init(args);
new main_window;

new M;
Main.run();
}

Solution

  • Although the code doesn't compile, but the reason your pages are not showing is that you need to call MBox.show() to show the boxes.

    Box MBox = new Box(Orientation.VERTICAL, 1);
    MBox.add(MGrid);
    super.notebook.appendPage(MBox, new Label(line));
    MBox.show();
    

    You can also call showAll on a container widget to show all children widgets. In your case, that's the notebook, the grid containing the notebook or the main window. So you can add notebook.showAll() after the loop to achieve the same thing.

    On a side note, you might want to follow the D style for writing D code: http://dlang.org/dstyle.html.