summaryrefslogtreecommitdiff
path: root/lib/main.tcl
blob: e942c00a82ab11ca8398b25760999a3c5958e397 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
#!/bin/sh
# the next line restarts using wish \
exec tclsh "$0" "$@"

# Part of MCU 8051 IDE ( http://http://www.moravia-microsystems.com/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@moravia-microsystems.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 _MAIN_TCL ] } {
set _MAIN_TCL _
# <<< File inclusion guard


# --------------------------------------------------------------------------
# DESCRIPTION
# Initilizator of the program
# --------------------------------------------------------------------------

# GENERAL CONSTANTS
# -----------------------------
set DEBUG		0				;# Turn on debugging
encoding system {utf-8}					;# System encoding
set LIB_DIRNAME	[file normalize [file dirname $argv0]]	;# Path to directory where the *.tcl file are located
set INSTALLATION_DIR [file normalize "$LIB_DIRNAME/.."]	;# Path to the main executable (this file)
set ROOT_DIRNAME [file normalize "$LIB_DIRNAME/.."]	;# On Linux it's the same as INSTALLATION_DIR but it's different on Windows
set VERSION		"1.4.9"				;# Version of this IDE
set SHORTNAME		"MCU8051IDE"			;# Program short name (without white space)
set APPNAME		"MCU 8051 IDE v$VERSION"	;# Full program name
set MIN_TCL_VER		"8.5"				;# Minimum required Tcl version
set APPLICATION_LOADED	0				;# True if program is loaded
set TRANSLATION_LOADED	0				;# Bool: Translation loaded
set MICROSOFT_WINDOWS	0				;# Bool: Windows OS running on the host computer
set CONFIG_DIR		{}				;# Directory containing configuration files

# Check for correct Tcl version
if {[package vcompare $::tcl_version $::MIN_TCL_VER] < 0} {
	puts stderr "ERROR: This program requires Tcl version $::MIN_TCL_VER or higher but you have Tcl $::tcl_version ."
	puts stderr "       Please install Tcl $::MIN_TCL_VER in order to run this program"

	exit 1
}

# Make sure that there is "ttk::spinbox" even when Tk version is lower than 8.5.9
namespace eval ttk {
	proc spinbox {args} {
		eval "set result \[::spinbox $args\]"
		return $result
	}
}

## Tcl packages used by this software
 # Format: {
 # 	{pkg_name	pkg_verison}
 # 	...
 # }
set LIBRARIES_TO_LOAD {
	{BWidget	1.8}
	{Itcl		3.4}
	{md5		2.0}
	{Tk		8.5}
	{img::png	1.3}
	{tdom		0.8}
	{Tclx		8.0}
	{Signal		1.4}
	{Tcl		8.2}
}

set OPTIONAL_LIBRARIES {
	Signal
	Tclx
}

## Bool:
 #     1 == Normal GUI operation
 #     0 == Program GUI suppressed
set ::GUI_AVAILABLE 1

## Bool:
 #     1 == library TclX is available
 #     0 == library TclX is NOT available
 #
 # TclX is used here only to handle signals (e.g. SIGINT), so the IDE can run
 # without it, that's the reason for this variable. If TCLX_AVAILABLE is 0 then
 # we are not able to handle signals, but everything else works normally.
set ::TCLX_AVAILABLE 1

## Bool:
 #     1 == library Signal is available
 #     0 == library Signal is NOT available
set ::SIGNAL_AVAILABLE 1

## Determinate the host OS
set ::MICROSOFT_WINDOWS 0
if {[string first {Windows} ${tcl_platform(os)}] != -1} {
	# Note:
	#   Microsoft Windows is NOT a POSIX system, and because of that, we need
	#   to do some workarounds here in order to make the IDE functional.
	set ::MICROSOFT_WINDOWS 1

	set LIB_DIRNAME_SPECIFIC_FOR_MS_WINDOWS "<AIPCS:LIB_DIRNAME_SPECIFIC_FOR_MS_WINDOWS>" ;# <-- The auto. inst. pkg. creation script will fill this in
	set AUTO_PATH_FOR_MS_WINDOWS "<AIPCS:AUTO_PATH_FOR_MS_WINDOWS>" ;# <-- The auto. inst. pkg. creation script will fill this in
	set INSTALLATION_DIR $LIB_DIRNAME
	set LIB_DIRNAME $LIB_DIRNAME_SPECIFIC_FOR_MS_WINDOWS
	set ROOT_DIRNAME [regsub {\/\w+\/?$} $LIB_DIRNAME {}]

	foreach dir $AUTO_PATH_FOR_MS_WINDOWS {
		lappend ::auto_path "${::ROOT_DIRNAME}/${dir}"
	}
	set env(ITCL_LIBRARY) "${::ROOT_DIRNAME}/libraries/itcl"
}

# Set directory containing configuration files according to the host OS
if {!$::MICROSOFT_WINDOWS} {
	set CONFIG_DIR [file join ${::env(HOME)} .[string tolower ${::SHORTNAME}]]
} else {
	set CONFIG_DIR [file join ${::env(USERPROFILE)} ".[string tolower ${::SHORTNAME}]"]
}

# Handle CLI options
# -----------------------------
proc mc args {return [eval "format $args"]}
source "${::LIB_DIRNAME}/cli.tcl"

# SHOW WARNING MESSAGE
# -----------------------------

if {!$::CLI_OPTION(quiet)} {
	if {$::CLI_OPTION(nocolor)} {
		puts "IMPORTANT INFORMATION:"
		puts "\tThis program is distributed in the hope that it will be useful, but with ABSOLUTELY NO WARRANTY !"
		puts "\tThis is free software, so you are free to change and redistribute it."
		puts "\tLicense: GNU General Public License version 2 or later"
		puts "\tPlease report bugs at http://mcu8051ide.sf.net"
		puts "Authors:"
		puts "\tMartin Osmera <martin.osmera@moravia-microsystems.com>"
	} else {
		puts "IMPORTANT INFORMATION :"
		puts "\tThis program is distributed in the hope that it will be useful, but with \033\[31;1mABSOLUTELY NO WARRANTY !\033\[m"
		puts "\tThis is free software, so you are free to change and redistribute it."
		puts "\tLicense: GNU General Public License version 2 or later"
		puts "\tPlease report bugs at \033\[34;1mhttp://mcu8051ide.sf.net\033\[m"
		puts "Authors:"
		puts "\tMartin Osmera \033\[33;1m<martin.osmera@moravia-microsystems.com>\033\[m"
	}
}

## This function should be called when some Tcl library fail to load
 # @parm String library - Name of failed library
 # @return void
proc libraryLoadFailed {library} {

	# Itcl workarond for Debian
	if {$library == {Itcl}} {
		if {[package vcompare $::tcl_version "8.6"] >= 0} {
			if {![catch {package require Itcl}]} {
				return
			}
		}

		set library_version "3.4"
		set libname "libitcl"

		set ::env(ITCL_LIBRARY) ${::LIB_DIRNAME}

		puts stderr "\nERROR: Unable to load Itcl library compatible with this version of Tcl/Tk!"
		puts stderr "Trying to workaround ..."

		if {[lsearch {Linux} ${::tcl_platform(os)}] == -1} {
			puts stderr "FATAL ERROR: Unsupported operating system. ${::tcl_platform(os)}"
			puts stderr "You can contact authors of the project at <martin.osmera@gmail.com> if you want to get you OS supported."
			exit 1
		}

		if {[lsearch {x86_64 i386 i486 i586 i686 x86} ${::tcl_platform(machine)}] == -1} {
			puts stderr "FATAL ERROR: Unsupported system architecture. ${::tcl_platform(machine)}"
			puts stderr "You can contact authors of the project at <martin.osmera@gmail.com> if you want to get you OS supported."
			exit 1
		}

		puts stderr "Loading library $library for ${::tcl_platform(os)} on ${::tcl_platform(machine)} ... (filename: ${libname}${library_version}.so.${::tcl_platform(os)}.${::tcl_platform(machine)})"
		if {[catch {load "${::LIB_DIRNAME}/${libname}${library_version}.so.${::tcl_platform(os)}.${::tcl_platform(machine)}" Itcl} error_info]} {
			puts stderr "FAILED !"
			puts stderr "Reason: ${error_info}"
			puts "\nPlease try to run mcu8051ide with --check-libraries to see what's wrong."

			exit 1
		} else {
			puts stderr "WORKAROUND SUCCESSFUL ... \n(But don't be much happy about this, it is still serious failure. And please don't forget to comply to developers of your Linux distribution. Missing library is: ${library} version ${library_version})"
			return
		}

	# Tclx workarond for Debian (1/2)
	} elseif {$library == {Tclx}} {
		set ::TCLX_AVAILABLE 0
		puts stderr "\nERROR: Unable to load library Tclx, MCU 805 1IDE functionality might be limited."
		return

	# Tclx workarond for Debian (2/2)
	} elseif { $library == {Signal} } {
		set ::SIGNAL_AVAILABLE 0
		if {!$::TCLX_AVAILABLE} {
			puts stderr "\nERROR: Unable to load library Signal, MCU 805 1IDE functionality might be limited."
		}
		return
	}

	# Print error message
	if {$::CLI_OPTION(nocolor)} {
		puts stderr "\n\nERROR: Unable to load library $library"
	} else {
		puts stderr "\n\n\033\[31mERROR:\033\[m Unable to load library \033\[32m$library\033\[m"
	}

	# Print tip
	puts "\nTip: try to run mcu8051ide with --check-libraries to see what's wrong."

	# Terminate the program
	exit 1
}

# PRE-INITIALIZATION
# -----------------------------
# Load Tk ToolKit
set T [lindex [time {
	if {[catch {package require img::png 1.3} e]} {
		libraryLoadFailed "img::png"
	}
	if {[catch {package require Tk $::MIN_TCL_VER} errinfo]} {
		puts stderr "Unable to initialize Tk\n$errinfo"
	}
}] 0]
# Hide main window
wm withdraw .
update

# Determinate default Fixed font
set ::DEFAULT_FIXED_FONT {Courier}
if {!$::MICROSOFT_WINDOWS} {
	if {[lsearch -ascii -exact [font families] {DejaVu Sans Mono}] != -1} {
		set ::DEFAULT_FIXED_FONT {DejaVu Sans Mono}
	}
}

# Set default background color
set ::COMMON_BG_COLOR [. cget -bg]

# ------------------------------------------------------------------------------
# Microsoft Windows OS specific code
# ------------------------------------------------------------------------------
if {$::MICROSOFT_WINDOWS} {
	# Print windows related warning
	puts ""
	puts "        THE IDE WAS ORIGINALY WRITTEN FOR POSIX, SO IT IS POSSIBLE THAT SOME"
	puts "        FUNCTIONALITY WILL BE LIMITED ON MS WINDOWS DUE TO ABSENCE OF CERTAIN"
	puts "        POSIX FUNCTIONALITY!"
	puts ""
}
# ------------------------------------------------------------------------------


# ------------------------------------------------------------------------------
# Make key shortcuts independent on Caps Lock.
# ------------------------------------------------------------------------------
rename bind original_command_bind
proc mcu8051ide_bind args {
	set widget [lindex $args 0]
	set event_str [lindex $args 1]
	set command [lindex $args 2]

	if {[llength $args] > 3} {
		error "Too many arguments to bind command."
	} elseif {[llength $args] == 2} {
		return [original_command_bind $widget $event_str]
	}

	# MS Windows doesn't recognize ISO and XFree86 codes
	if {$::MICROSOFT_WINDOWS} {
		if {
			[string first {ISO} $event_str] != -1	||
			[string first {XF86} $event_str] != -1
		} then {
			return
		}
	}

	if {
		[string index $event_str end-2] == {-}
			&&
		[string is alpha [string index $event_str end-1]]
			&&
		[string index $event_str end] == {>}
	} then {
		set letter [string index $event_str end-1]
		set event_str [string range $event_str 1 end-2]
		if {[string is upper $letter]} {
			set event_str "Shift-${event_str}"
		}

		original_command_bind $widget "<${event_str}[string toupper $letter]>" $command
		original_command_bind $widget "<${event_str}[string tolower $letter]>" $command
	} else {
		original_command_bind $widget $event_str $command
	}
}
rename mcu8051ide_bind bind
# ------------------------------------------------------------------------------

# Load base config file
# -----------------------------

# Initialize variable containing the font size factor
set font_size_factor 1.0

# Load i18n library
# (It must be loaded here because ::msgcat::mclocale must be available when
# base config file is being loaded)
incr T [lindex [time {
	if {[catch {package require msgcat 1.3.4}]} {
		libraryLoadFailed "msgcat"
	} else {
		namespace import -force ::msgcat::mc
		namespace import -force ::msgcat::mcset
		namespace import -force ::msgcat::*
	}
}] 0]
# Check if the file exits
if {![file exists ${::CONFIG_DIR}]} {
	file mkdir ${::CONFIG_DIR}
	puts "\nCreating program configuration files in directory: \"[file normalize ${::CONFIG_DIR}]\""
	if {!$::MICROSOFT_WINDOWS} {
		puts "Welcome in this IDE, [file tail [file normalize ~]] !"
	} else {
		catch {	;# Make the configuration directory in Microsoft Windows hidden
			file attributes $::CONFIG_DIR -hidden 1
		}
		puts "Welcome in this IDE, ${::env(USERNAME)} !"
	}
}
## Open and read the file
if {[catch {
	set conf_file [open "${::CONFIG_DIR}/base.conf" r]
 # File doesn't exits -> create it with default configuration
}]} then {
	# Default settings
	set detected_lang [string tolower [lindex [split [::msgcat::mclocale] {_}] 0]]
	if {{} != [lindex [split [::msgcat::mclocale] {_}] 1]} {
		append detected_lang {_} [string toupper [lindex [split [::msgcat::mclocale] {_}] 1]]
	}
	array set GLOBAL_CONFIG [list			\
		splash		1			\
		tips		1			\
		language	$detected_lang		\
		background	{Default}		\
		wstyle		{clam}			\
	]

	# Create the file
	if {[catch {
		set conf_file [open "${::CONFIG_DIR}/base.conf" w]
		puts -nonewline $conf_file [list	\
			$GLOBAL_CONFIG(splash)		\
			$GLOBAL_CONFIG(tips)		\
			$GLOBAL_CONFIG(language)	\
			${::font_size_factor}		\
			$GLOBAL_CONFIG(background)	\
			$GLOBAL_CONFIG(wstyle)		\
		]
		close $conf_file
	}]} then {
		puts stderr "Unable to create base configuration file"
	}
 # File exits -> read configuration from it
} else {
	# Read file contents
	set data [read $conf_file]
	close $conf_file

	# Set configuration according to the file contents
	set GLOBAL_CONFIG(splash)	[lindex $data 0]
	set GLOBAL_CONFIG(tips)		[lindex $data 1]
	set GLOBAL_CONFIG(language)	[lindex $data 2]
	set ::font_size_factor		[lindex $data 3]
	set GLOBAL_CONFIG(background)	[lindex $data 4]
	set GLOBAL_CONFIG(wstyle)	[lindex $data 5]

	## Validate read values
	if {![regexp {^1\.\d+$} $::font_size_factor] || $::font_size_factor < 1.0 || $::font_size_factor >= 2.0} {
		set ::font_size_factor 1.0
	}
	if {![string is boolean -strict ${::GLOBAL_CONFIG(splash)}]} {
		set GLOBAL_CONFIG(splash) 1
	}
	if {![string is boolean -strict ${::GLOBAL_CONFIG(tips)}]} {
		set GLOBAL_CONFIG(tips) 1
	}
	# This feature is not supported on MS Windows due to BWidget
	if {!$::MICROSOFT_WINDOWS} {
		switch -- ${::GLOBAL_CONFIG(background)} {
			{Default}	{set ::COMMON_BG_COLOR {#eeeeee}}
			{Windows}	{set ::COMMON_BG_COLOR {#ece9d9}}
			{Tk}		{}
			{Light}		{set ::COMMON_BG_COLOR {#f8f8f8}}
			{Dark}		{set ::COMMON_BG_COLOR {#cccccc}}
			default		{
				set ::GLOBAL_CONFIG(background) {Default}
				set ::COMMON_BG_COLOR {#eeeeee}
			}
		}
	}
	if {[lsearch -ascii -exact [ttk::style theme names] ${::GLOBAL_CONFIG(wstyle)}] == -1} {
		set ::GLOBAL_CONFIG(wstyle) {clam}
	}
	 # Check if the cpecified translation is valid
	set tmp [list]
	catch {	;# For Microsoft Windows it has to be enclosed by catch
		set tmp [glob -nocomplain -types f -tails			\
			-directory "${::INSTALLATION_DIR}/translations" *.msg	\
		]
	}
	set translations {en}
	foreach translation $tmp {
		lappend translations [file rootname $translation]
	}

	if {[lsearch $translations ${GLOBAL_CONFIG(language)}] == -1} {
		set GLOBAL_CONFIG(language) {en}
	}
}


# Load translation
# -----------------------------

# Load list of language names
set ::LANGUAGE_CODES [list]
namespace eval ::configDialogues::global {
	if {[catch {
		set file [open "${::INSTALLATION_DIR}/translations/languages.txt" r]
		set languages [read $file]
		close $file

		regsub -all -line {\s*#.*$} $languages {} languages
		set ::LANGUAGE_CODES $languages
		lindex $languages 0
	} e]} {
		puts stderr "Error while loading list of laguages: $e"
	}
}

# Load language specific translation file
if {!${::CLI_OPTION(notranslation)} && ${GLOBAL_CONFIG(language)} != {en}} {
	if {[catch {
		::msgcat::mclocale ${GLOBAL_CONFIG(language)}
		incr T [lindex [time {
			source "${::INSTALLATION_DIR}/translations/${GLOBAL_CONFIG(language)}.msg"
		}] 0]

	} result]} then {
		puts stderr "Unable to load translation"
		puts stderr "\tFile: '${::INSTALLATION_DIR}/translations/${GLOBAL_CONFIG(language)}.msg'"

	} else {
		set ::TRANSLATION_LOADED 1
	}
}

# CREATE SPLASH SCREEN
# -----------------------------
if {!$::CLI_OPTION(nosplash) && ${::GLOBAL_CONFIG(splash)}} {

	# Workaround for multiple monitors
	toplevel .splash_aux
	wm attributes .splash_aux -fullscreen 1
	update
	set sw [winfo width .splash_aux]
	set sh [winfo height .splash_aux]
	destroy .splash_aux
	update

	# Crete toplevel  window
	toplevel .splash -class {Splash creen} -bg ${::COMMON_BG_COLOR}

	# Show image of splash creen
	place [label .splash.bg	\
		-bg {#FFFFFF}	\
		-image [	\
			image create photo -format png	\
				-file "${::ROOT_DIRNAME}/icons/other/splash.png"	\
			]	\
	] -x 0 -y 0 -width 400 -height 199

	# Show status bar
	place [label .splash.status		\
		-bg {#FFFFFF} -fg {#0000FF}	\
		-text [mc "Initializing %s" $APPNAME]	\
	] -x 200 -y 180 -anchor center

	# Set window parameters
	wm geometry .splash "=400x199+[expr {$sw / 2 - 200}]+[expr {$sh / 2 - 100}]"
	wm overrideredirect .splash 1
	wm attributes .splash -alpha 0.8

	# Click on splash creen destroys it
	bind .splash <1> {wm withdraw .splash}

	# Done ..
	update
}


# BASIC FUNCTIONS
# -----------------------------

## Print content of $T in mili seconds ($T must contain value in [us])
 # @return void
proc time_in_msec {} {
	global T	;# Time in microseconds

	# Determinate number of miliseconds
	set msec [lindex $T 0]
	set msec [expr {$msec / 1000}]

	# print the message
	if {!$::CLI_OPTION(quiet)} {
		if {$::CLI_OPTION(nocolor)} {
			puts "... $msec ms"
		} else {
			puts "... \033\[33m$msec ms\033\[m"
		}
	}
}

## Print some initialization message (splash screen and CLI)
 # @parm String message - text of the message
 # @return void
proc showInitMessage {message} {

	# Change content of splash screen status bar
	if {!${::CLI_OPTION(nosplash)} && ${::GLOBAL_CONFIG(splash)}} {
		if {[winfo exists .splash.status]} {
			.splash.status configure -text [string trim $message]
			update
		}
	}

	# Print message to console output
	if {!${::CLI_OPTION(quiet)}} {
		if {${::CLI_OPTION(nocolor)}} {
			puts -nonewline $message
		} else {
			puts -nonewline "\033\[37m$message\033\[m"
		}

		puts -nonewline [string repeat { } [expr {38 - [string length $message]}]]
		flush stdout
	}
}

## Set status bar tip for some widget
 # Usage:
 #	setStatusTip -widget $some_widget -text "some text"
 #
 # @return void
proc setStatusTip args {

	# Local variables
	set widgetIsSet	0		;# True if widget is set
	set textIsSet	0		;# True if text is set
	set argsLength	[llength $args]	;# Number of arguments
	set widget	{}		;# ID of widget specified by argument '-widget'
	set helpText	{}		;# Help text specified by argument '-text'

	# Iterate over given arguments and evaluate them
	for {set i 0} {$i < $argsLength} {incr i} {
		# Currently parsed argument
		set arg [lindex $args $i]
		# Decide what $arg means
		switch -- $arg {
			-widget {	;# ID of the widget

				# check if that widget wasn't already specified
				if {$widgetIsSet} {
					error "Widget has been already specified"
				}

				# Check if widget's ID follow the arument
				incr i
				if {$i >= $argsLength} {
					error "Expected widget name after -widget option"
				}

				# Set ID of the widget
				set widget [lindex $args $i]
				if {![winfo exists $widget]} {
					error "The specified widget does not exist"
				}

				# Widget is now set
				set widgetIsSet 1
			}

			-text {		;# The help text

				# Check if help text follow the argument
				incr i
				if {$i >= $argsLength} {
					error "Expected text after -text option"
				}

				# Set the help text
				set helpText [lindex $args $i]

				# Help text is now set
				set textIsSet 1
			}

			default {	;# Unrecognized opton -> invoke ERROR
				error "Invalid argument '$arg', possible options are -widget and -text"
			}
		}
	}

	# Ckeck if both aruments are properly specified
	if {!$widgetIsSet || !$textIsSet} {
		error "You must specify text and widget"
	}

	# Create binding
	bind $widget <Enter> "Sbar -freeze {$helpText}"
	bind $widget <Leave> "Sbar {}"
}



# INITIALIZATION
# -----------------------------

# Show "first line message"
if {!$CLI_OPTION(quiet)} {
	if {$CLI_OPTION(nocolor)} {
		puts [mc "\nInitializing MCU 8051 IDE %s" $VERSION]
	} else {
		puts [mc "\nInitializing \033\[1mMCU 8051 IDE \033\[32m%s\033\[m" $VERSION]
	}
}

## Load libraries
showInitMessage [mc "\tLoading libraries"]
incr T [lindex [time {
	# Iterate over list of libraries and lod each of them
	foreach library $::LIBRARIES_TO_LOAD {
		# Loading successful
		if {[catch {package require [lindex $library 0] [lindex $library 1]}]} {
			libraryLoadFailed [lindex $library 0]
		# Loading failed
		} else {
			if {!$::CLI_OPTION(nosplash)} update
		}
	}

	if {$::MICROSOFT_WINDOWS} { ;# Load dde - Dynamic Data Exchange on Microsoft Windows
		package require dde
	}

	# Import NS for managing OOP in Tcl
	namespace import -force ::itcl::*
}] 0]

# Look for some external programs
foreach program {
		urxvt	vim	emacs	kwrite	gedit	nano	dav
		le	sdcc	indent	doxygen	asl	asem	doxywizard
		as31	sdcc-sdcc	gvim	hunspell	d52
		make
	} \
{
		if {[auto_execok $program] == {}} {
			set ::PROGRAM_AVAILABLE($program) 0
		} else {
			set ::PROGRAM_AVAILABLE($program) 1
		}
}
time_in_msec	;# Print time info

## Load program sources
showInitMessage [mc "\tLoading program sources"]
set T [time {
	source "${::LIB_DIRNAME}/lib/modern_notebook.tcl"	;# NoteBook widget with a modern look (modern in 2011)
	source "${::LIB_DIRNAME}/lib/FSnotifications.tcl"	;# Filesystem watcher
	source "${::LIB_DIRNAME}/lib/innerwindow.tcl"		;# Tool for creating inner windows
	source "${::LIB_DIRNAME}/dialogues/errorhandler.tcl"	;# Background error handler
	source "${::LIB_DIRNAME}/dialogues/my_tk_messageBox.tcl";# A replacement for tk_messageBox
	source "${::LIB_DIRNAME}/lib/settings.tcl"		;# Settings management
	source "${::LIB_DIRNAME}/project.tcl"			;# Project management
	source "${::LIB_DIRNAME}/dialogues/fsd.tcl"		;# File selection dialog
	source "${::LIB_DIRNAME}/X.tcl"				;# GUI <==> Implementation Interface
	source "${::LIB_DIRNAME}/configdialogues/configdialogues.tcl";# Configuration dialogues
	source "${::LIB_DIRNAME}/editor/editor.tcl"		;# Source code editor
	source "${::LIB_DIRNAME}/lib/Math.tcl"			;# Special mathematical operations
	source "${::LIB_DIRNAME}/compiler/compiler.tcl"		;# 8051 Assemly language compiler
	source "${::LIB_DIRNAME}/dialogues/tips.tcl"		;# Tips on start-up
	source "${::LIB_DIRNAME}/lib/hexeditor.tcl"		;# Hexadecimal editor
	source "${::LIB_DIRNAME}/utilities/hexeditdlg.tcl"	;# Hexadecimal editor dialog
	source "${::LIB_DIRNAME}/environment.tcl"		;# Main window "trappings" (menu and such)
	source "${::LIB_DIRNAME}/rightpanel/rightpanel.tcl"	;# Right panel
	source "${::LIB_DIRNAME}/leftpanel/filelist.tcl"	;# Left and middle panel
	source "${::LIB_DIRNAME}/simulator/simulator.tcl"	;# MCU Simulator
	source "${::LIB_DIRNAME}/bottompanel/bottomnotebook.tcl";# Bottom panel
	source "${::LIB_DIRNAME}/maintab.tcl"			;# Central widget
	source "${::LIB_DIRNAME}/lib/ihextools.tcl"		;# Tools for manipulating Intel 8 HEX
	source "${::LIB_DIRNAME}/utilities/symbol_viewer.tcl"	;# Assembly symbols viewer
	source "${::LIB_DIRNAME}/utilities/eightsegment.tcl"	;# 8-Segment LED display editor
	source "${::LIB_DIRNAME}/utilities/asciichart.tcl"	;# ASCII chart
	source "${::LIB_DIRNAME}/utilities/table_of_instructions.tcl";# 8051 Instruction Table
	source "${::LIB_DIRNAME}/utilities/notes.tcl"		;# Scribble notepad
	source "${::LIB_DIRNAME}/utilities/baseconverter.tcl"	;# Base converter
	source "${::LIB_DIRNAME}/utilities/speccalc.tcl"	;# Special calculator for x51 MCU's
	source "${::LIB_DIRNAME}/utilities/rs232debugger.tcl"	;# UART/RS232 applications debugger
}]
time_in_msec	;# Print time info

# CHECK FOR VALIDITY OF THE MCU DATABASE
if {${::X::available_processors} == {}} {
	destroy .splash
	bell
	tk_messageBox		\
		-icon error	\
		-type ok	\
		-title [mc "FATAL ERROR"]	\
		-message [mc "MCUs database file is corrupted,\nthis program cannot run without it.\nPlease reinstall MCU 8051 IDE."]
	exit 1
}

# Load global configuration
loadApplicationConfiguration

# Start spell checker process and wait until it is started
::Editor::restart_spellchecker_process
::Editor::adjust_spell_checker_config_button

# Initialize GUI environment
mainmenu_redraw		;# Main menu
iconbar_redraw		;# Main toolbar
shortcuts_reevaluate	;# Key shortcuts

## Remove splash screen
if {[winfo exists .splash]} {
	destroy .splash
}
if {$CLI_OPTION(minimalized)} {
	wm state . iconic
} else {
	wm deiconify .
}

# Configure signal handling
if {$::TCLX_AVAILABLE || $::SIGNAL_AVAILABLE} {
	proc signal_handler {signal_name} {
		global cntrlc_flag
		puts stderr [mc "\nExiting on signal %s" $signal_name]
		catch {
			::X::__exit 1 1
		}
		puts stderr [mc "\nERROR: Unable to exit the program correctly -- TERMINATING NOW!" $signal_name]
		exit 1
	}

	if {$::TCLX_AVAILABLE} {
		signal trap SIGINT	{signal_handler SIGINT}
		signal trap SIGTERM	{signal_handler SIGTERM}
	} else {
		signal add SIGINT	{signal_handler SIGINT}
		signal add SIGTERM	{signal_handler SIGTERM}
	}
}


# ---------------------------------------------------------
# Ugly job ... dirty workarounds and such things ... :(
# ---------------------------------------------------------

catch {
	NoteBook .foo
	proc NoteBook::_getoption {path page option} {
		if {$option == {-background}} {
			return ${::COMMON_BG_COLOR}
		}

		catch {
			set value [Widget::cget $path.f$page $option]
			if {![string length $value]} {
				set value [Widget::cget $path $option]
			}

			return $value
		}
	}
	destroy .foo
}

# ---------------------------------------------------------

## Open the last session
# Print message
showInitMessage [mc "\tOpening last session"]
flush stdout
# Evaluate new geometry of the main window
update
evaluate_new_window_geometry
if {![winfo viewable .mainMenu]} {
	wm geometry . $::CONFIG(WINDOW_GEOMETRY)
	update
}
# Open projects of last session
set T [time {
	foreach project_file $::CONFIG(OPENED_PROJECTS) {
		if {![Project::open_project_file $project_file]} {
			tk_messageBox			\
				-title [mc "File not found"]	\
				-icon warning		\
				-type ok		\
				-message [mc "Unable to open project file:\n\"%s\"" $project_file]
		}
	}
}]

# Reopen base converters
foreach cfg $::CONFIG(BASE_CONVERTERS) {
	if {[catch {
		set obj [::X::__base_converter]
		::X::$obj set_config $cfg
	}]} then {
		puts stderr {}
		puts stderr $::errorInfo
	}
}

time_in_msec	;# Print time info

# Without this "help windows" won't work properly on MS Windows
if {$::MICROSOFT_WINDOWS} {
	if {[wm state .] == {zoomed}} {
		set ::LATS_KNOWN_WM_STATE_IS_ZOOMED 1
	} else {
		set ::LATS_KNOWN_WM_STATE_IS_ZOOMED 0
	}
}
# Create binding for panes management
bind . <Configure> {
	X::redraw_panes

	# Without this "help windows" won't work properly on MS Windows
	if {$::MICROSOFT_WINDOWS} {
		if {[wm state .] == {zoomed}} {
			set now_zoomed 1
		} else {
			set now_zoomed 0
		}

		if {!${::LATS_KNOWN_WM_STATE_IS_ZOOMED} && $now_zoomed} {
			after idle {
				update
				regsub {[\+\-].*$} [wm geometry .] {+0+0} geometry
				wm geometry . $geometry
			}
		}
		set ::LATS_KNOWN_WM_STATE_IS_ZOOMED $now_zoomed
	}
}
# Print final message
if {!$CLI_OPTION(quiet)} {
	puts [mc "%s is now operational\n" $APPNAME]
}

# Program is now operational
set ::Compiler::in_IDE 1
set APPLICATION_LOADED 1
set X::critical_procedure_in_progress 0
update
X::redraw_panes
update
foreach project ${::X::openedProjects} {
	$project bottomNB_redraw_pane
	$project filelist_adjust_size_of_tabbar
	$project ensure_that_both_editors_are_properly_initialized
}
# Focus on the active editor
if {${::X::actualProject} != {}} {
	update
	focus [${::X::actualProject} editor_procedure {} cget -editor]
	focus [${::X::actualProject} editor_procedure {} Configure {}]
	focus [${::X::actualProject} editor_procedure {} highlight_visible_area {}]
}

# Correct strange behavior concerning restoration of the last window size and position
if {$::MICROSOFT_WINDOWS} {
	update
	wm geometry . $::CONFIG(WINDOW_GEOMETRY)
}

# Initialize file change notifications mechanism
FSnotifications::init

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