"""
Tk-based graphical user interface (GUI).

This package implements a Topographica GUI based on the Tk toolkit,
through the Tkinter interface to Python.  The rest of Topographica
does not depend on this package or any module in it, and thus other
GUIs or other types of interfaces can also be used.

$Id$
"""
__version__='$Revision$'

import Pmw, sys, Tkinter, platform
import topo.base.parameterizedobject
import topoconsole

# CB: I think we should setup some named font instances here
# to use throughout tkgui, and avoid all the various local
# specifications (which will become very tedious to maintain).
# Doing this will give other advantages, too, like maybe allowing
# a user to say "make all the fonts a bit bigger", and so on.
#
# Some font links:
# http://www.pythonware.com/library/tkinter/introduction/x444-fonts.htm
# http://www.astro.washington.edu/owen/ROTKFolklore.html




# When not using the GUI, Topographica does not ordinary import any of
# the classes in the separate Topographica packages. For example, none
# of the pattern types in topo.patterns is imported in Topographica by
# default.  But for the GUI, we want all such things to be available
# as lists from which the user can select.  To do this, we import all
# pattern types and other such classes here. User-defined classes will
# also appear in the GUI menus if they are derived from any class
# derived from the one specified in each widget, and imported before
# the relevant GUI window starts.
from topo.eps import *
from topo.learningfns import *
from topo.outputfns import *
from topo.patterns import *
from topo.projections import *
from topo.responsefns import *
from topo.sheets import *

from topo.base.cf import CFPLearningFnParameter,CFPOutputFnParameter,CFPResponseFnParameter
from topo.base.functionfamilies import LearningFnParameter,OutputFnParameter,ResponseFnParameter
from topo.base.patterngenerator import PatternGeneratorParameter



### Which os is being used (for gui purposes)?
#
# CEBALERT: let's test once for the OS here.
#
# Right now tkgui only needs to detect if the platform is linux (do I
# mean any kind of non-OS X unix*?) or mac, because there is some
# special-purpose code for both those two: the mac code below, and the
# menu-activating code in topoconsole.  We might have some Windows-
# specific code for the window icons later on, too.
# * actually it's the window manager that's important, right?
#
# platform.system()  sys.platform  os.name
# platform.dist()/platform.win32_ver()/platform.mac_ver
# Does tkinter give any useful information?




#
# Define up the right click (context menu) events. These variables can
# be appended or overridden in .topographicarc, if the user has some
# crazy input device.
#
if platform.mac_ver()[0]:
    # if it's on the mac, these are the context-menu events
    right_click_events = ['<Button-2>','<Control-Button-1>']
    right_click_release_events = ['ButtonRelease-2', 'Control-ButtonRelease-1']
else:
    # everywhere else (I think) it's Button-3
    right_click_events = ['<Button-3>']
    right_click_release_events = ['ButtonRelease-2']
    

def show_cmd_prompt():
    """
    Small helper to print the sys.ps1 prompt to the command-line.
    Useful after a bunch of output has been displayed to stdout,
    so as to let the user know that the command-line is still
    active.
    """
    if topo.base.parameterizedobject.min_print_level >= topo.base.parameterizedobject.MESSAGE:
        print "\n", sys.ps1,
        sys.stdout.flush()
    
def start(mainloop=False):
    """
    Startup code for GUI.

    mainloop: If True, then the command-line is frozen while the GUI
    is open.  If False, then commands can be entered at the command-line
    even while the GUI is operational.  Default is False.
    """
    console = topoconsole.TopoConsole()

    # This alows context menus to work on the Mac.  Widget code should bind
    # contextual menus to the virtual event <<right-click>>, not
    # <Button-3>.
    # JPALERT: We probably should do an OS check here if
    # ctrl-click means something else on Windows.

    console.event_add('<<right-click>>',*right_click_events)
    console.event_add('<<right-click-release>>',*right_click_release_events)
    Pmw.initialise(console)

    # mainloop() freezes the commandline until the GUI window exits.
    # Without this line the command-line remains responsive.
    if mainloop:
        console.mainloop()

    return console


####################### 

if __name__ == '__main__':
    start(mainloop=True)

@@ -1935,16 +1935,26 @@
 meter%0A%0A%0A
+##########
 %0A### Whi
@@ -2001,55 +2001,241 @@
 #%0A# 
-CEBALERT: let's test once for the OS her
+system_plaform can be:%0A# %22linux%22%0A# %22mac%22%0A# %22win%22%0A# %22unknown%22%0A#%0A# If you are programming tkgui and need to do something special%0A# for some other platform (or to further distinguish the above%0A# platforms, please modify this cod
 e.%0A#%0A# 
+(
 Righ
@@ -2616,20 +2616,94 @@
 , right?
+)
 %0A#
-%0A#
+ Does tkinter give any useful information?%0A#%0Asystem_platform = 'unknown'%0Aif
  platfor
@@ -2712,22 +2712,38 @@
 system()
+=='Linux':%0A  
   sys
-.
+tem_
 platform
@@ -2747,35 +2747,54 @@
 orm 
- os.name%0A# platform.dist()/
+= 'linux'%0Aelif platform.system()=='Darwin' or 
 plat
@@ -2802,82 +2802,123 @@
 orm.
-win32
+mac
 _ver()
-/platform.mac_ver%0A# Does tkinter give any useful information?%0A%0A
+%5B0%5D:%0A    system_platform = 'mac'%0Aelif platform.system()=='Windows':%0A    system_platform = 'win'%0A##########
 %0A%0A%0A#
@@ -3085,24 +3085,31 @@
 e.%0A#%0Aif 
+system_
 platform
 .mac_ver
@@ -3096,37 +3096,33 @@
 tem_platform
-.mac_ver()%5B0%5D
+ is 'mac'
 :%0A    # if i
@@ -4172,16 +4172,44 @@
 onsole()
+%0A    Pmw.initialise(console)
 %0A%0A    # 
@@ -4365,116 +4365,8 @@
 3%3E.%0A
-    # JPALERT: We probably should do an OS check here if%0A    # ctrl-click means something else on Windows.%0A%0A
     
@@ -4503,39 +4503,16 @@
 ts)%0A    
-Pmw.initialise(console)
 %0A%0A    # 

Test for the platform and store in system_platform for use by the rest of tkgui.