1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * gtkD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with gtkD; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
17  */
18 
19 module sourceView.SVTest;
20 
21 
22 private import gtk.MainWindow;
23 private import gtk.Main;
24 private import gtk.ScrolledWindow;
25 private import gtk.Widget;
26 
27 private import glib.Str;
28 
29 private import gsv.SourceView;
30 private import gsv.SourceBuffer;
31 private import gsv.SourceLanguage;
32 private import gsv.SourceLanguageManager;
33 private import gsv.SourceBuffer;
34 
35 version(Tango)
36 {
37 	private import tango.io.Stdout;
38 	private import tango.text.Util;
39 	private import tango.io.device.File;
40 	private import tango.core.Vararg;
41 
42     void writefln( string frm, ... ){
43         string frm2 = substitute( frm, "%s", "{}" );
44         Stdout( Stdout.layout.convert( _arguments, _argptr, frm2 )).newline;
45     }
46 }
47 else
48 {
49 	private import std.stdio;
50 	private import std.file;
51 }
52 
53 /**
54  * Demos for SourceView.
55  * TODO on gsv: override methods from TextView, TextBuffer, etc
56  */
57 
58 class HelloWorld : MainWindow
59 {
60 
61 	SourceView sourceView;
62 	
63 	this()
64 	{
65 		super("GtkD SourceView");
66 		setBorderWidth(10);
67 		add(getSourceView());
68 		setDefaultSize(640,400);
69 		showAll();
70 	}
71 	
72 	private string getDemoText()
73 	{
74 		string text;
75 
76 		version(Tango)
77 		{
78 			try
79 			{
80 				auto file = new File ("SVTest.d");
81 				text = new char[file.length];
82 				file.read(text);
83 			}
84 			catch (Exception) { }
85 		}
86 		else
87 		{
88 			try
89 			{
90 				text = cast(string)std.file.read("SVTest.d");
91 			}
92 			catch ( FileException fe ) { }
93 		}
94 
95 		return text;
96 	}
97 	
98 	private Widget getSourceView()
99 	{
100 		sourceView = new SourceView();
101 		sourceView.setShowLineNumbers(true);
102 		
103 		sourceView.setInsertSpacesInsteadOfTabs(false);
104 		sourceView.setTabWidth(4);
105 		sourceView.setHighlightCurrentLine(true);
106 		
107 		SourceBuffer sb = sourceView.getBuffer();
108 		sb.setText(getDemoText());
109 		
110 		ScrolledWindow scWindow = new ScrolledWindow();
111 		scWindow.add(sourceView);
112 
113 		
114 		SourceLanguageManager slm = new SourceLanguageManager();
115 		SourceLanguage dLang = slm.getLanguage("d");
116 		
117 		if ( dLang !is null )
118 		{
119 			writefln("Setting language to D");
120 			sb.setLanguage(dLang);
121 			sb.setHighlightSyntax(true);
122 		}
123 		
124 		//sourceView.modifyFont("Courier", 9);
125 		sourceView.setRightMarginPosition(72);
126 		sourceView.setShowRightMargin(true);
127 		sourceView.setAutoIndent(true);
128 		
129 		
130 		return scWindow;
131 	}
132 }
133 
134 void main(string[] args)
135 {
136 	Main.init(args);
137 	new HelloWorld();
138 	Main.run();
139 
140 }