summaryrefslogtreecommitdiff
path: root/lib/bottompanel/terminal.tcl
blob: 1d21a35daf07dc9ff1810a9d3a9cf593dae395c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/tclsh
# Part of MCU 8051 IDE ( http://https://sourceforge.net/projects/mcu8051ide/ )

############################################################################
#    Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 by Martin OŇ°mera     #
#    martin.osmera@gmail.com                                               #
#                                                                          #
#    Copyright (C) 2014 by Moravia Microsystems, s.r.o.                    #
#    martin.osmera@gmail.com                                #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

# >>> File inclusion guard
if { ! [ info exists _TERMINAL_TCL ] } {
set _TERMINAL_TCL _
# <<< File inclusion guard

# --------------------------------------------------------------------------
# DESCRIPTION
# Provides terminal emulator for bottom notebook
# --------------------------------------------------------------------------

class Terminal {
	# Terminal emulator configuration
	public common configuration
	public common configuration_def [subst {
		bg		#FFFFFF
		fg		#000000
		font_size	12
		font_family	{$::DEFAULT_FIXED_FONT}
	}]

	private variable terminal_counter	0	;# Int: Counter of terminal emulator instances
	private variable terminal_frame			;# Widget: ID of terminal container frame
	private variable wrapper_frame			;# Widget: Wrapper frame for $terminal_frame
	private variable parent				;# Widget: Parent frame
	private variable term_gui_initialized	0	;# Bool: GUI initialized
	private variable terminal_pid		{}	;# Int: PID of terminal emulator

	destructor {
		terminal_kill_childern
	}

	## Prepare this tab for GUI creation
	 # @parm Widget _parent -
	 # @return void
	public method PrepareTerminal {_parent} {
		set parent $_parent
		set term_gui_initialized 0
	}

	## Inform this tab than it has became active
	 # @return void
	public method TerminalTabRaised {} {
		focus $terminal_frame
	}

	## Create GUI
	 # @return void
	public method CreateTerminalEmulGUI {} {
		if {$term_gui_initialized || !${::PROGRAM_AVAILABLE(urxvt)}} {return}
		set term_gui_initialized 1

		set wrapper_frame [frame $parent.wrapper_frame -relief sunken -bd 2]
		pack $wrapper_frame -fill both -expand 1
		terminal_recreate_terminal
		unset parent
	}

	## Internal procedure -- (re)create frame with terminal emulator
	 # @return void
	public method terminal_recreate_terminal {} {
		if {![winfo exists $wrapper_frame]} {return}
		set terminal_frame [frame $wrapper_frame.terminal_frame_${terminal_counter} -container 1]
		bind $terminal_frame <Destroy> "$this terminal_recreate_terminal"

		set pwd [pwd]
		if {[catch {
			cd [$this cget -projectPath]
		}]} then {
			cd ~
		}
		if {[catch {
			set terminal_pid [exec -- urxvt				\
				-embed [expr [winfo id $terminal_frame]]	\
				-sr -b 0 -w 0 -bg ${configuration(bg)}		\
				-fg ${configuration(fg)}			\
				-fn "xft:$configuration(font_family):pixelsize=$configuration(font_size)" & \
			]
		} result]} then {
			tk_messageBox		\
				-parent .	\
				-icon warning	\
				-type ok	\
				-title [mc "Unable to find urxvt"]	\
				-message [mc "Unable to execute program \"urxvt\", terminal emulator is eiter not available or badly configured."]
			puts stderr $result
		}
		cd $pwd
		pack $terminal_frame -fill both -expand 1
		incr terminal_counter
	}

	## Restart terminal emulator
	 # @return void
	public method terminal_restart {} {
		if {!$term_gui_initialized} {return}
		if {!${::PROGRAM_AVAILABLE(urxvt)}} {return}
		foreach pid $terminal_pid {
			if {$pid == [pid] || $pid == 0} {
				continue
			}
			catch {
				exec -- kill $pid
			}
		}
	}

	## Kill terminal emulator
	 # @return void
	public method terminal_kill_childern {} {
		if {$term_gui_initialized} {
			if {[info exists terminal_frame] && [winfo exists $terminal_frame]} {
				bind $terminal_frame <Destroy> {}
			}
			foreach pid $terminal_pid {
				if {$pid == [pid] || $pid == 0} {
					continue
				}
				catch {
					exec -- kill $pid
				}
			}
		}
	}
}

# Initialize NS variables
array set ::Terminal::configuration ${::Terminal::configuration_def}

# >>> File inclusion guard
}
# <<< File inclusion guard