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 
41     void writefln( string frm, ... ){
42         string frm2 = substitute( frm, "%s", "{}" );
43         Stdout( Stdout.layout.convert( _arguments, _argptr, frm2 )).newline;
44     }
45 }
46 else
47 {
48 	private import std.stdio;
49 	private import std.file;
50 }
51 
52 /**
53  * Demos for SourceView.
54  * TODO on gsv: override methods from TextView, TextBuffer, etc
55  */
56 
57 class HelloWorld : MainWindow
58 {
59 
60 	SourceView sourceView;
61 	
62 	this()
63 	{
64 		super("GtkD SourceView");
65 		setBorderWidth(10);
66 		add(getSourceView());
67 		setDefaultSize(640,400);
68 		showAll();
69 	}
70 	
71 	private string getDemoText()
72 	{
73 		string text;
74 
75 		version(Tango)
76 		{
77 			try
78 			{
79 				auto file = new File ("SVTest.d");
80 				text = new char[file.length];
81 				file.read(text);
82 			}
83 			catch (Exception) { }
84 		}
85 		else
86 		{
87 			try
88 			{
89 				text = cast(string)std.file.read("SVTest.d");
90 			}
91 			catch ( FileException fe ) { }
92 		}
93 
94 		return text;
95 	}
96 	
97 	private Widget getSourceView()
98 	{
99 		sourceView = new SourceView();
100 		sourceView.setShowLineNumbers(true);
101 		
102 		sourceView.setInsertSpacesInsteadOfTabs(false);
103 		sourceView.setTabWidth(4);
104 		sourceView.setHighlightCurrentLine(true);
105 		
106 		SourceBuffer sb = sourceView.getBuffer();
107 		sb.setText(getDemoText());
108 		
109 		ScrolledWindow scWindow = new ScrolledWindow();
110 		scWindow.add(sourceView);
111 
112 		
113 		SourceLanguageManager slm = new SourceLanguageManager();
114 		SourceLanguage dLang = slm.getLanguage("d");
115 		
116 		if ( dLang !is null )
117 		{
118 			writefln("Setting language to D");
119 			sb.setLanguage(dLang);
120 			sb.setHighlightSyntax(true);
121 		}
122 		
123 		//sourceView.modifyFont("Courier", 9);
124 		sourceView.setRightMarginPosition(72);
125 		sourceView.setShowRightMargin(true);
126 		sourceView.setAutoIndent(true);
127 		
128 		
129 		return scWindow;
130 	}
131 }
132 
133 void main(string[] args)
134 {
135 	Main.init(null);
136 	new HelloWorld();
137 	Main.run();
138 
139 }