import gtk
import gtk.glade

import callbacks

__all__ = ["ui_tree", "window_main", "store_plot", "tree_plot",
    "draw_graph", "status_main", "trace"]

glade_ui = "ui.glade"
ui_tree = gtk.glade.XML(glade_ui)

# function, draw, error
store_plot = gtk.TreeStore(str, bool)
store_plot.append(None, ["((x**3 + 2*x**2 + 3*x + 4) / x) / 8", True])
store_plot.append(None, ["(x**2 + 2*x + 3) / 8", True])
store_plot.append(None, ["", False])

# Setup columns
# COLUMN str -- Function
col_function = gtk.TreeViewColumn("Function")

col_function_cell = gtk.CellRendererText()
col_function_cell.set_property("editable", True)
col_function_cell.connect("edited",
    callbacks.on_col_function_cell_edited, store_plot)
col_function.pack_start(col_function_cell)

col_function.add_attribute(col_function_cell, "text", 0)

# COLUMN bool -- Plot the function?
col_draw = gtk.TreeViewColumn()
col_draw_cell = gtk.CellRendererToggle()
col_draw_cell.set_property("activatable", True)
col_draw_cell.connect("toggled", callbacks.on_col_draw_cell_toggled, store_plot)
col_draw.pack_start(col_draw_cell)
col_draw.add_attribute(col_draw_cell, "active", 1)

tree_plot = ui_tree.get_widget("tree_plot")
tree_plot.set_model(store_plot)
tree_plot.append_column(col_draw)
tree_plot.append_column(col_function)

draw_graph = ui_tree.get_widget("draw_graph")
#draw_graph.drag_source_set(gtk.gdk.BUTTON1_MASK,
#    [("",gtk.TARGET_SAME_WIDGET,0)], gtk.gdk.ACTION_DEFAULT)
#draw_graph.drag_dest_set(gtk.DEST_DEFAULT_MOTION,
#    [("",gtk.TARGET_SAME_WIDGET,0)], gtk.gdk.ACTION_DEFAULT)

window_main = ui_tree.get_widget("window_main")
status_main = ui_tree.get_widget("status_main")

# Prepare graph resolution
xmin = ui_tree.get_widget("spinner_graphwindow_x_min")
xmax = ui_tree.get_widget("spinner_graphwindow_x_max")
ymin = ui_tree.get_widget("spinner_graphwindow_y_min")
ymax = ui_tree.get_widget("spinner_graphwindow_y_max")

def trace():
    x_value = ui_tree.get_widget("spin_trace").get_value()
    b_trace = ui_tree.get_widget("chk_trace").get_active()
    return (b_trace, x_value)

def resolution():
    return (xmin.get_value(), xmax.get_value(),
        ymin.get_value(), ymax.get_value())

# Set default windowing values
xmin.set_value(-5.0)
xmax.set_value(5.0)
ymin.set_value(-5.0)
ymax.set_value(5.0)

#drag_vector = [(0,0)]
signals = {
    "gtk_main_quit": gtk.main_quit,
    "on_tree_plot_key_press_event": callbacks.on_tree_plot_key_press_event,
    #"on_draw_graph_drag_begin": (callbacks.on_draw_graph_drag_begin, drag_vector),
    #"on_draw_graph_drag_motion": (callbacks.on_draw_graph_drag_motion,
    #    (xmin, xmax, ymin, ymax), draw_graph, drag_vector),
    #"on_draw_graph_drag_end": (callbacks.on_draw_graph_drag_end, drag_vector),
    "on_draw_graph_scroll_event": (callbacks.on_draw_graph_scroll_event,
        (xmin, xmax, ymin, ymax), draw_graph),
    "on_graphwindow_changed": (callbacks.on_graphwindow_changed, draw_graph)
}

ui_tree.signal_autoconnect(signals)


@@ -144,16 +144,30 @@
  %22trace%22
+, %22resolution%22
 %5D%0A%0Aglade
@@ -1918,24 +1918,111 @@
 ef trace():%0A
+    %22%22%22 Returns a float to be traced, or None if tracing is disabled%0A    %0A    %22%22%22%0A    %0A
     x_value 
@@ -2142,45 +2142,161 @@
 urn 
-(b_trace, x_value)%0A%0Adef resolution():
+x_value if b_trace else None%0A%0Adef resolution():%0A    %22%22%22 Returns the resolution of the graph window as%0A    tuple(xmin, xmax, ymin, ymax)%0A    %0A    %22%22%22%0A    
 %0A   

Add resolution function to ui.__all__; alter ui.trace to return None if it is off, rather than tuple(bool, float); Document both ui.trace and ui.resolution