
# Copyright (c) 1999 ICEM CFD Engineering
# Author:  Wayne A. Christopher, wayne@icemcfd.com
#          ICEM CFD Engineering, Berkeley, California

array set window_cursors {
    busy wait
    add_entity crosshair
    add_group crosshair
    control crosshair
    dynamics arrow
    edit crosshair
    pick crosshair
    point_edit center_ptr
    point_move center_ptr
    polypick arrow
    circlepick arrow
    remove_entity crosshair
    remove_group crosshair
    zoomin icon
}

proc set_cursor {w spec} {
    global window_cursors is_win cursor_color guibase_dir 
    global gui_style_mode tcl_platform

    if {([llength $spec] == 2) && [info exists cursor_color]} {
        set spec [list [lindex $spec 0] $cursor_color]
    }

    if {$is_win || ([info exists gui_style_mode] && $gui_style_mode == "wb" && \
        [info exists tcl_platform(os)] && $tcl_platform(os) == "Linux")} {
        set name [file rootname [file tail [lindex $spec 0]]]
        if [info exists window_cursors($name)] {
            set spec $window_cursors($name)
        } else {
            switch $name {
                crosshair - wait - center_ptr - icon -
                sb_v_double_arrow - sb_h_double_arrow {
                    set spec $name
                }
                default {
                    set spec arrow
                }
            }
        }
    } else {
    # This is really wierd.  Because tk will free the cursor when we no
    # longer need it for the current window, create an unmapped window that
    # has subwindows that have all the cursors so they will never get
    # freed and re-loaded.

        global set_cursor_hack
        if ![info exists set_cursor_hack($spec)] {
            if ![winfo exists .cursor_hack] {
                toplevel .cursor_hack ;# not that this doesn't use toplevel.tcl
                wm withdraw .cursor_hack
            }
            for {set i 0} {[winfo exists .cursor_hack.c$i]} {incr i} {}
            set cc .cursor_hack.c$i
            frame $cc -cursor $spec
            pack $cc
            set set_cursor_hack($spec) $cc
        }
    }
    set ospec [$w cget -cursor]
    $w configure -cursor $spec
    return $ospec
}
