1 /**
2  * Tk interpreter module.
3  *
4  * License:
5  *     MIT. See LICENSE for full details.
6  */
7 module tkd.interpreter.tk;
8 
9 /**
10  * Imports.
11  */
12 import std.conv;
13 import std.string;
14 import tcltk.tk;
15 import tkd.interpreter.tcl;
16 
17 /**
18  * Simple singleton wrapper for the Tk interpreter.
19  */
20 class Tk : Tcl
21 {
22 	/**
23 	 * An instance of this tk interpreter.
24 	 */
25 	private static Tk _instance;
26 
27 	/*
28 	 * Create the interpreter and initialise it.
29 	 *
30 	 * Throws:
31 	 *     Exception if Tk interpreter cannot be initialised.
32 	 */
33 	protected this()
34 	{
35 		super();
36 
37 		debug (log) this._log.info("Inititalising Tk");
38 
39 		if (Tk_Init(this._interpreter) != TCL_OK)
40 		{
41 			string result = Tcl_GetStringResult(this._interpreter).to!(string);
42 			throw new Exception(format("Tk interpreter extension could not be initialised. %s", result));
43 		}
44 	}
45 
46 	/**
47 	 * Get an instance of this class.
48 	 *
49 	 * Returns:
50 	 *     If An instance doesn't exist, one is created and returned.
51 	 *     If one already exists, that is returned.
52 	 */
53 	public static Tk getInstance()
54 	{
55 		if (Tk._instance is null)
56 		{
57 			Tk._instance = new Tk();
58 		}
59 		return Tk._instance;
60 	}
61 
62 	/**
63 	 * Run the tk main loop, show the gui and start processing events.
64 	 */
65 	public void run()
66 	{
67 		debug (log) this._log.info("Running Tk main loop");
68 		Tk_MainLoop();
69 	}
70 }