diff --git a/tk/canvas.go b/tk/canvas.go index 2b69cff..683804a 100644 --- a/tk/canvas.go +++ b/tk/canvas.go @@ -240,6 +240,108 @@ func (w *Canvas) YScrollIncrement() int { return r } +// Canvas Plot Geometry +func (w *Canvas) PlotLine(xy []Pos, options map[string]string) error { + // canvas create line x1 y1... xn yn ?option value ...? // 不闭合折线 + // canvas create line 10 10 200 50 -fill red -width 3 -tags line1 + + var tmp1 = "" + for _,pos := range xy { + tmp1 = tmp1 + strconv.Itoa(pos.X) + " " + strconv.Itoa(pos.Y) + " " + } + var tmp2 = "" + for k,v := range options { + tmp2 = tmp2 + "-" +k+ " " + v + " " + } + + return eval(fmt.Sprintf("%v create line %v%v", w.id, tmp1, tmp2)) +} + +func (w *Canvas) PlotRectangle(x1,y1,x2,y2 int, options map[string]string) error { + // canvas create rectangle x1 y1 x2 y2 ?option value ...? // 矩形 + // canvas create rectangle 10 10 200 50 -fill red -outline blue -tags rec1 + + var tmp2 = "" + for k,v := range options { + tmp2 = tmp2 + "-" +k+ " " + v + " " + } + + return eval(fmt.Sprintf("%v create rectangle %v %v %v %v %v", w.id, x1,y1,x2,y2, tmp2)) +} + + +func (w *Canvas) PlotOval(x1,y1,x2,y2 int, options map[string]string) error { + // canvas create oval x1 y1 x2 y2 ?option value ...? // 矩形内切椭圆或圆 + // canvas create oval 10 10 200 50 -fill red -outline blue -tags oval1 + + var tmp2 = "" + for k,v := range options { + tmp2 = tmp2 + "-" +k+ " " + v + " " + } + + return eval(fmt.Sprintf("%v create oval %v %v %v %v %v", w.id, x1,y1,x2,y2, tmp2)) +} + +func (w *Canvas) PlotPolygon(xy []Pos, options map[string]string) error { + // canvas create polygon x1 y1 ... xn yn ?option value ...? // 多边形 + // canvas create polygon 10 10 180 90 20 45 -fill red -width 3 -tags pol1 + + var tmp1 = "" + for _,pos := range xy { + tmp1 = tmp1 + strconv.Itoa(pos.X) + " " + strconv.Itoa(pos.Y) + " " + } + var tmp2 = "" + for k,v := range options { + tmp2 = tmp2 + "-" +k+ " " + v + " " + } + + return eval(fmt.Sprintf("%v create polygon %v%v", w.id, tmp1, tmp2)) +} + + +func (w *Canvas) PlotText(x1,y1 int, options map[string]string) error { + // canvas create text x y ?option value ...? // 文字 + // canvas create text 100 100 -text "A wonderful story" -anchor nw -fill black -tags txt1 + + var tmp2 = "" + for k,v := range options { + tmp2 = tmp2 + "-" +k+ " " + v + " " + } + // v 值含有空格时使用{}, "text":"{A wonderful story}" + + return eval(fmt.Sprintf("%v create text %v %v %v", w.id, x1,y1, tmp2)) +} + +func (w *Canvas) PlotImage(x1,y1 int, options map[string]string) error { + // canvas create image x y ?option value ...? + // canvas create image 10 10 -image myimg -anchor nw + var tmp2 = "" + for k,v := range options { + tmp2 = tmp2 + "-" +k+ " " + v + " " + } + + return eval(fmt.Sprintf("%v create image %v %v %v", w.id, x1,y1, tmp2)) +} + + +func (w *Canvas) PlotWidget(x1,y1 int, options map[string]string) error { + // canvas create window x y ?option value ...? + // canvas create window 10 10 -anchor nw -window .canvas.b + var tmp2 = "" + for k,v := range options { + tmp2 = tmp2 + "-" +k+ " " + v + " " + } + + return eval(fmt.Sprintf("%v create window %v %v %v", w.id, x1,y1, tmp2)) +} +// 参考: +// http://www.tcl-lang.org/man/tcl8.6/TkCmd/canvas.htm +// https://tkdocs.com/tutorial/canvas.html +// 例子:https://github.com/visualfc/atk/issues/16 + + + +// WidgetAttr func CanvasAttrBackground(color string) *WidgetAttr { return &WidgetAttr{"background", color} } diff --git a/tk/plotchart.go b/tk/plotchart.go new file mode 100644 index 0000000..28c74d2 --- /dev/null +++ b/tk/plotchart.go @@ -0,0 +1,22 @@ +// package require Plotchart +// https://github.com/tcltk/tklib/blob/master/modules/plotchart/plotchart.tcl +// https://core.tcl-lang.org/tklib/doc/trunk/embedded/www/tklib/files/modules/plotchart/plotchart.html + +// package require Plotchart +// canvas .c -background white -width 400 -height 200 +// pack .c -fill both +// # +// # Create the plot with its x- and y-axes +// # +// set s [::Plotchart::createXYPlot .c {0.0 100.0 10.0} {0.0 100.0 20.0}] +// foreach {x y} {0.0 32.0 10.0 50.0 25.0 60.0 78.0 11.0 } { +// $s plot series1 $x $y +// } +// $s title "Data series" + + + + + + + diff --git a/tk/theme_ttk.go b/tk/theme_ttk.go index 471ad78..4ebbca8 100644 --- a/tk/theme_ttk.go +++ b/tk/theme_ttk.go @@ -47,6 +47,57 @@ var ( TtkTheme = &ttkTheme{} ) +func (w *BaseWidget) StyleName() string { + // ttk::button .b; winfo class .b // ==> TButton + r, _ := evalAsString(fmt.Sprintf("winfo class %v", w.id)) + return r +} + +func (w *BaseWidget) StyleLookUp(name, option string) string { + // ttk::style lookup style -option + // ttk::style lookup 1.TButton -font // [---> helvetica 24] + r1, _ := evalAsString(fmt.Sprintf("ttk::style lookup %v -%v", name,option)) + return r1 +} + + +func StyleConfigure(name string, options map[string]string) error { + // ttk::style configure style ?-option ?value option value...? ? + // ttk::style configure Emergency.TButton -foreground red -padding 10 + // ttk::button .b -text "Hello" -style "Fun.TButton" + var tmp = "" + for k,v := range options { + tmp = tmp + "-" + k + " " + v + " " + } + + return eval(fmt.Sprintf("ttk::style configure %v %v", name,tmp)) +} + +func StyleMap(name string, options map[string]map[string]string) error{ + // ttk::style map style ?-option { statespec value... }? + // ttk::style map TRadiobutton -foreground [list !pressed blue pressed yellow] -background [list selected black !selected white] + var tmp1 = "" + var tmp2 = "" + for k1,v1 := range options { + tmp2 = "[list " + tmp1 = tmp1 + "-" + k1 + " " + for k2,v2 := range v1 { + tmp2 = tmp2 + k2 + " " + v2 + " " + } + tmp2 = tmp2 + "] " + tmp1 = tmp1 + tmp2 + } + // fmt.Println(fmt.Sprintf("ttk::style map %v %v", name,tmp1)) + return eval(fmt.Sprintf("ttk::style map %v %v", name,tmp1)) +} + +// 参考: +// https://tkdocs.com/tutorial/styles.html +// http://www.tcl-lang.org/man/tcl8.6/TkCmd/ttk_style.htm +// https://tkdocs.com/shipman/ttk-map.html +// 例子:https://github.com/visualfc/atk/issues/17 + + func init() { registerInit(func() { ttk_theme_list, _ = evalAsStringList("ttk::themes") diff --git a/tk/tooltip.go b/tk/tooltip.go new file mode 100644 index 0000000..15c188b --- /dev/null +++ b/tk/tooltip.go @@ -0,0 +1,27 @@ +// a simple tooltip + +// 参考:http://tmml.sourceforge.net/doc/tcllib/tooltip.html + +// 例子:widget.Tooltip("This is a widget") + +package tk + +import "fmt" + +var isPackReqTip = false // 保证 "package require tooltip" 只进行一次 + +func (w *BaseWidget) Tooltip(tip string) error { + + // ::tooltip::tooltip pathName ? option arg ? message + // tooltip::tooltip .l "This is a label widget" + if !isPackReqTip { + + _ = eval(fmt.Sprintf("package require tooltip")) + // fmt.Printf("package require tooltip ok ? %v\n", ok) + } + // s1 := fmt.Sprintf("tooltip::tooltip %v {%v}", w.id, tip) + // fmt.Printf("s1 is %v\n",s1) + return eval(fmt.Sprintf("tooltip::tooltip %v {%v}", w.id, tip)) +} + +