GtkD Code Examples

Here are some example code snippets you can compile and run. You could try building gtkD with DSSS and modify the dsss.conf file for gtkDTests to compile these examples.

Hello World

import gtk.MainWindow;
import gtk.Label;
import gtk.Main;

void main(char[][] args)
{
	Main.init(args);
	MainWindow win = new MainWindow("Hello World");
	win.setDefaultSize(200, 100);
	win.add(new Label("Hello World"));
	win.showAll();

	Main.run();
}

Exit Button

import gtk.MainWindow;
import gtk.Button;
import gtk.Main;

class ExitButton : MainWindow
{
	this()
	{
		super("Exit Button");
		setDefaultSize(50, 30);
		Button exitbtn = new Button();
		exitbtn.setLabel("Exit");
		exitbtn.addOnClicked(&exitProg);
		add(exitbtn);
		showAll();
	}
	void exitProg(Button button)
	{
		Main.exit(0);
	}
}

void main(char[][] args)
{
	Main.init(args);
	new ExitButton();
	Main.run();
}

Popup Message

import gtk.MainWindow;
import gtk.Button;
import gtk.MessageDialog;
import gtk.Main;

class PopupMessage : MainWindow
{
	this()
	{
		super("Popup Message");
		setDefaultSize(50, 30);
		add(new Button("Message", &popupMsg));
		showAll();
	}
	void popupMsg(Button button)
	{
		MessageDialog d = new MessageDialog(this, GtkDialogFlags.MODAL, MessageType.INFO, ButtonsType.OK, "This is a popup message!");
		d.run();
		d.destroy();
	}
}

void main(char[][] args)
{
	Main.init(args);
	new PopupMessage();
	Main.run();
}

Button Usage

import gtk.MainWindow;
import gtk.AboutDialog;
import gtk.Label;
import gtk.Button;
import gtk.VBox;
import gtk.Main;

class ButtonUsage : MainWindow
{
	Label StatusLbl;
	this()
	{
		super("Button Usage");
		setDefaultSize(200, 100);
		VBox box = new VBox(false, 2);
		StatusLbl = new Label("Click a Button");
		box.add(StatusLbl);
		box.add(new Button("Button 1", &onBtn1));
		box.add(new Button("Exit", &onBtn2));
		box.add(new Button("About", &onBtn3));
		add(box);
		showAll();	
	}
	void onBtn1(Button button)
	{
		StatusLbl.setText("You Clicked Button 1");
	}
	void onBtn2(Button button)
	{
		Main.exit(0);
	}
	void onBtn3(Button button)
	{
		with (new AboutDialog())
		{
			char** names = (new char*[2]).ptr;
			int i = 0;
			names[i++] = cast(char*)"Jake Day (Okibi)";
			setAuthors(names);
			setWebsite("http://ddev.ratedo.com");
			showAll();
		}
	}
}

void main(char[][] args)
{
	Main.init(args);
	new ButtonUsage();
	Main.run();
}