コード例 #1
0
ファイル: listbox.go プロジェクト: etel/walk
func main() {
	walk.Initialize(walk.InitParams{PanicOnError: true})
	defer walk.Shutdown()

	myWindow, _ := walk.NewMainWindow()

	myWindow.SetLayout(walk.NewVBoxLayout())
	myWindow.SetTitle("Listbox example")

	splitter, _ := walk.NewSplitter(myWindow)
	splitter.SetOrientation(walk.Vertical)

	lb, _ := walk.NewListBox(splitter)

	valueEdit, _ := walk.NewTextEdit(splitter)
	valueEdit.SetReadOnly(true)

	//env model
	em := NewEnvModel()

	for _, env := range os.Environ() {
		i := strings.Index(env, "=")
		if i == 0 {
			continue
		}
		varName := env[0:i]
		value := env[i+1:]
		envItem := EnvItem{varName, value}

		em.envItems = append(em.envItems, envItem)
	}

	fmt.Println("The len of Model", em.ItemCount())
	lb.SetModel(em)
	lb.CurrentIndexChanged().Attach(func() {
		if curVar, ok := em.Value(lb.CurrentIndex()).(string); ok {
			value := em.envItems[lb.CurrentIndex()].value
			value = strings.Replace(value, ";", "\r\n", -1)
			valueEdit.SetText(value)
			fmt.Println("CurrentIndex:", lb.CurrentIndex())
			fmt.Println("CurrentEnvVarName:", curVar)
		}
	})
	lb.DblClicked().Attach(func() {
		value := em.envItems[lb.CurrentIndex()].value
		value = strings.Replace(value, ";", "\r\n", -1)
		valueEdit.SetText(value)
		walk.MsgBox(myWindow, "About", value, walk.MsgBoxOK|walk.MsgBoxIconInformation)
	})
	myWindow.Show()
	myWindow.SetMinMaxSize(walk.Size{320, 240}, walk.Size{})
	myWindow.SetSize(walk.Size{400, 500})
	myWindow.Run()
}
コード例 #2
0
ファイル: splitter.go プロジェクト: etel/walk
func (s Splitter) Create(parent walk.Container) error {
	w, err := walk.NewSplitter(parent)
	if err != nil {
		return err
	}

	return InitWidget(s, w, func() error {
		if s.HandleWidth > 0 {
			if err := w.SetHandleWidth(s.HandleWidth); err != nil {
				return err
			}
		}
		if err := w.SetOrientation(walk.Orientation(s.Orientation)); err != nil {
			return err
		}

		if s.AssignTo != nil {
			*s.AssignTo = w
		}

		return nil
	})
}
コード例 #3
0
ファイル: splitter.go プロジェクト: nihao/walk
func (s Splitter) Create(builder *Builder) error {
	w, err := walk.NewSplitter(builder.Parent())
	if err != nil {
		return err
	}

	return builder.InitWidget(s, w, func() error {
		if s.HandleWidth > 0 {
			if err := w.SetHandleWidth(s.HandleWidth); err != nil {
				return err
			}
		}
		if err := w.SetOrientation(walk.Orientation(s.Orientation)); err != nil {
			return err
		}

		if s.AssignTo != nil {
			*s.AssignTo = w
		}

		return nil
	})
}
コード例 #4
0
ファイル: main.go プロジェクト: hemaolong/Rabbit
func (mw *MainWindow) initPoseInfo() {
	if modelItem == nil {
		return
	}

	model := modelItem.img
	if model == nil {
		return
	}
	// Get the pose count
	poseCnt := 1
	x := model.Bounds().Min.X
	y := model.Bounds().Min.Y
	w := model.Bounds().Dx()
	h := model.Bounds().Dy()
	for i := 2; i < POSE_CNT_MAX; i++ {
		sh := h / i
		for j := 1; j < i; j++ {
			beginY := sh * j
			// Erase the boundary by 1 pix to handel the neighbor pix
			for z := 1; z < w-1; z++ {
				_, _, _, a := model.At(x+z, y+beginY).RGBA()
				if a != 0 {
					_, _, _, la := model.At(x+z-1, y+beginY).RGBA()
					_, _, _, ra := model.At(x+z+1, y+beginY).RGBA()
					_, _, _, ta := model.At(x+z, y+beginY-1).RGBA()
					_, _, _, da := model.At(x+z, y+beginY+1).RGBA()
					if la != 0 && ra != 0 && ta != 0 && da != 0 {
						fmt.Println("Pose alpha:", x+z, y+beginY, a)
						goto nextPose
					}
				}
			}
		}
		poseCnt = i
		break

	nextPose:
	}

	fmt.Println("Pose count: ", poseCnt)

	// Init the pose list
	imageW = w / int(mw.uiFrameCnt.Value())
	imageH = h / poseCnt

	mw.resetImageList()
	boundary = image.Rect(0, 0, imageW, imageH)
	tmpBound := boundary
	// Read all png images
	for i := 0; i < poseCnt; i++ {
		for j := 0; j < int(mw.uiFrameCnt.Value()); j++ {
			deltaX := imageW * j
			deltaY := imageH * i
			tmpBound = boundary.Add(image.Point{deltaX, deltaY})

			newImg := new(ImageItem)
			newImg.fname = ""
			newImg.img = modelItem.img.SubImage(tmpBound).(ImageExt)
			newImg.bm, _ = walk.NewBitmapFromImage(newImg.img)

			imgList = append(imgList, newImg)
		}
	}

}

/*
func (mw *MainWindow) onUiSetFrameCnt() {
	if modelItem == nil {
		return
	}

	// imageW = modelItem.img.Bounds().Dx()
	// imageH = modelItem.img.Bounds().Dy()

	// poseCnt := mw.getPoseCnt()
	playPose = int(mw.uiPlayPose.Value())
	mw.setImageSize()
}*/

func (mw *MainWindow) refreshToolBar(mode int) {
	mw.uiConvirm.SetEnabled(false)
	mw.uiComposeAction.SetEnabled(false)
	mw.uiPoseCnt.SetEnabled(false)

	mw.mode = mode
	mw.uiFrameCnt.SetEnabled(false)
	if mw.mode == MODE_INVALID {
		return
	}

	if mw.mode == MODE_PLAY {
		return
	}
	if mw.mode == MODE_COMPOSE {
		mw.uiFrameCnt.SetEnabled(true)
		mw.uiComposeAction.SetEnabled(true)
	}
}

func (mw *MainWindow) getPoseInfo() (int, int) {
	totalFrame := len(imgList)
	poseCnt := mw.getPoseCnt()

	if poseCnt >= totalFrame {
		return 1, totalFrame
	}
	if totalFrame%poseCnt != 0 {
		return 1, totalFrame
	}
	return poseCnt, totalFrame / poseCnt
}

func (mw *MainWindow) composeImg(fullname string) {
	poseCnt, frame := mw.getPoseInfo()
	if frame == 0 {
		return
	}

	sw := boundary.Dx()
	sh := boundary.Dy() + yBoundAdd

	//var rgba bool
	_newBound := image.Rect(0, 0, sw*frame, sh*poseCnt)
	// No need to check the source image type.
	var result draw.Image
	firstImg := imgList[0].img
	switch firstImg.(type) {
	case *image.RGBA:
		result = image.NewRGBA(_newBound)
	case *image.RGBA64:
		result = image.NewRGBA64(_newBound)
	case *image.NRGBA:
		result = image.NewNRGBA(_newBound)
	case *image.NRGBA64:
		result = image.NewNRGBA64(_newBound)
	default:
		fmt.Println("image type: ", reflect.TypeOf(firstImg))
		println("Unsupported image type")
		return
	}
	// Compress to RGBA32, Stride
	// result := image.NewRGBA(_newBound)
	// result.Stride = result.Bounds().Dx()

	singleBound := image.Rect(0, 0, sw, sh)
	for i, _img := range imgList {
		_subImg := _img.img.SubImage(boundary)
		col := i % frame
		row := i / frame
		drawBound := singleBound.Add(image.Point{sw * col, sh * row})
		draw.Draw(result, drawBound, _subImg, _subImg.Bounds().Min, draw.Src)
	}
	// Modify stride
	// fmt.Println("Stride ", result.Stride)

	f, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, os.ModePerm)
	if err != nil {
		panic(err)
		return
	}
	defer f.Close()

	f.Truncate(0)
	// buf := bufio.NewWriterSize(f, 1024 * 1000)
	buf := bufio.NewWriter(f)
	png.Encode(buf, result)
}

func setIcon(ui *walk.Action, fname string) {
	fpath := "./img/" + fname
	_, err := os.Stat(fpath)
	if err != nil {
		fmt.Println(err)
		return
	}
	img, _ := walk.NewBitmapFromFile(fpath)
	ui.SetImage(img)
}

func (mw *MainWindow) initMenu() {
	fileMenu, _ := walk.NewMenu()
	fileMenuAction, _ := mw.Menu().Actions().AddMenu(fileMenu)
	fileMenuAction.SetText("&File")

	imageList, _ := walk.NewImageList(walk.Size{TB_H, TB_H}, 0)
	mw.ToolBar().SetImageList(imageList)

	openAction := walk.NewAction()
	setIcon(openAction, "open.png")
	openAction.SetText("&Open")
	openAction.Triggered().Attach(func() { go mw.openImage(MODE_COMPOSE) })
	fileMenu.Actions().Add(openAction)
	mw.ToolBar().Actions().Add(openAction)

	///
	// Load
	loadAction := walk.NewAction()
	setIcon(loadAction, "load.png")
	loadAction.SetText("&Load")
	loadAction.Triggered().Attach(func() { mw.openImage(MODE_PLAY) })
	fileMenu.Actions().Add(loadAction)
	mw.ToolBar().Actions().Add(loadAction)

	helpMenu, _ := walk.NewMenu()
	helpMenuAction, _ := mw.Menu().Actions().AddMenu(helpMenu)
	helpMenuAction.SetText("&Help")

	aboutAction := walk.NewAction()
	helpMenu.Actions().Add(aboutAction)
	aboutAction.SetText("&About")
	aboutAction.Triggered().Attach(func() {
		walk.MsgBox(mw, "About", "Image composer V0.1\nAuthor:heml",
			walk.MsgBoxOK|walk.MsgBoxIconInformation)
	})

	// Image operations
	// Save
	mw.uiComposeAction = walk.NewAction()
	setIcon(mw.uiComposeAction, "save.png")
	mw.uiComposeAction.SetText("&Save")
	mw.uiComposeAction.Triggered().Attach(func() { mw.saveImage() })
	fileMenu.Actions().Add(mw.uiComposeAction)
	mw.ToolBar().Actions().Add(mw.uiComposeAction)

	// Exit
	exitAction := walk.NewAction()
	exitAction.SetText("E&xit")
	exitAction.Triggered().Attach(func() { walk.App().Exit(0) })
	fileMenu.Actions().Add(exitAction)
}

func (mw *MainWindow) initCanvas() {
	for i := 0; i < POSE_CNT_MAX; i++ {
		iv, _ := selfWidget.NewMyImageView(mw)
		mw.imageView[i] = iv
	}
}
func (mw *MainWindow) initOtherBars() {
	sp, _ := walk.NewSplitter(mw)
	sp.SetSize(walk.Size{400, 20})

	lab, _ := walk.NewLabel(sp)
	lab.SetSize(walk.Size{16, 30})
	// lab.SetText("Pose")

	// others
	mw.uiFrameCnt, _ = walk.NewNumberEdit(sp)
	//mw.uiFrameCnt.SetSize(walk.Size{42, TB_H})
	mw.uiFrameCnt.SetRange(1, 100)
	mw.uiFrameCnt.SetDecimals(0)
	mw.uiFrameCnt.SetValue(8)
	mw.uiFrameCnt.SetEnabled(false)
	mw.uiFrameCnt.SetToolTipText(ttPlayPose)

	mw.uiPoseCnt, _ = walk.NewNumberEdit(sp)
	//mw.uiPoseCnt.SetSize(walk.Size{42, TB_H})
	mw.uiPoseCnt.SetRange(1, 100)
	mw.uiPoseCnt.SetValue(1)
	mw.uiPoseCnt.SetDecimals(0)
	mw.uiPoseCnt.SetToolTipText(ttPosCnt)

	mw.uiAddBoundY, _ = walk.NewNumberEdit(sp)
	mw.uiAddBoundY.SetRange(1, 1000)
	mw.uiAddBoundY.SetValue(0)
	mw.uiAddBoundY.SetDecimals(0)
	mw.uiAddBoundY.ValueChanged().Attach(func() {
		yBoundAdd = int(mw.uiAddBoundY.Value())
		if yBoundAdd < -imageH {
			yBoundAdd = -imageH
		}
		if yBoundAdd > (imageH - boundary.Max.Y) {
			yBoundAdd = imageH - boundary.Max.Y
		}
		mw.uiAddBoundY.SetValue(float64(yBoundAdd))
		mw.setImageSize()
	})

	mw.uiConvirm, _ = walk.NewPushButton(sp)
	mw.uiConvirm.SetText("OK")
	mw.uiConvirm.Clicked().Attach(func() {
		// Get some fresh data.
		// mw.onUiSetFrameCnt()
	})

	walk.InitWidget(sp, mw, FREEZEIZE_CLASS,
		winapi.CCS_NORESIZE,
		winapi.WS_EX_TOOLWINDOW|winapi.WS_EX_WINDOWEDGE)
}

func newMainWindow() {
	walk.SetPanicOnError(true)
	mainWnd, _ := walk.NewMainWindow()

	mw := &MainWindow{MainWindow: mainWnd}
	mw.viewGrid = walk.NewGridLayout()
	mw.SetLayout(mw.viewGrid)
	mw.viewGrid.SetRowStretchFactor(GRID_CNT, 2)
	mw.viewGrid.SetColumnStretchFactor(GRID_CNT, 2)
	mw.viewGrid.SetMargins(walk.Margins{6, 28, 2, 6})

	mw.SetTitle("Image composer")

	mw.initMenu()
	mw.initOtherBars()
	mw.initCanvas()

	mw.SetMinMaxSize(walk.Size{800, 600}, walk.Size{})
	mw.SetSize(walk.Size{800, 600})

	mw.refreshToolBar(MODE_INVALID)
	mw.Show()
	mw.Run()
}

func init() {
	walk.MustRegisterWindowClass(FREEZEIZE_CLASS)
	runtime.GOMAXPROCS(2)

	screenW = int(winapi.GetSystemMetrics(winapi.SM_CXSCREEN))
	screenH = int(winapi.GetSystemMetrics(winapi.SM_CYSCREEN))
}

func main() {
	newMainWindow()
}
コード例 #5
0
ファイル: filebrowser.go プロジェクト: hoperuin/walk
func main() {
	walk.Initialize(walk.InitParams{PanicOnError: true})
	defer walk.Shutdown()

	mainWnd, _ := walk.NewMainWindow()

	mw := &MainWindow{
		MainWindow:    mainWnd,
		fileInfoModel: &FileInfoModel{},
	}

	mw.SetTitle("Walk File Browser Example")
	mw.SetLayout(walk.NewHBoxLayout())

	fileMenu, _ := walk.NewMenu()
	fileMenuAction, _ := mw.Menu().Actions().AddMenu(fileMenu)
	fileMenuAction.SetText("&File")

	exitAction := walk.NewAction()
	exitAction.SetText("E&xit")
	exitAction.Triggered().Attach(func() { walk.App().Exit(0) })
	fileMenu.Actions().Add(exitAction)

	helpMenu, _ := walk.NewMenu()
	helpMenuAction, _ := mw.Menu().Actions().AddMenu(helpMenu)
	helpMenuAction.SetText("&Help")

	aboutAction := walk.NewAction()
	aboutAction.SetText("&About")
	aboutAction.Triggered().Attach(func() {
		walk.MsgBox(mw, "About", "Walk File Browser Example", walk.MsgBoxOK|walk.MsgBoxIconInformation)
	})
	helpMenu.Actions().Add(aboutAction)

	splitter, _ := walk.NewSplitter(mw)

	mw.treeView, _ = walk.NewTreeView(splitter)

	mw.treeView.ItemExpanded().Attach(func(item *walk.TreeViewItem) {
		children := item.Children()
		if children.Len() == 1 && children.At(0).Text() == "" {
			mw.populateTreeViewItem(item)
		}
	})

	mw.treeView.SelectionChanged().Attach(func(old, new *walk.TreeViewItem) {
		mw.selTvwItem = new
		mw.showError(mw.fileInfoModel.ResetRows(pathForTreeViewItem(new)))
	})

	drives, _ := walk.DriveNames()

	mw.treeView.SetSuspended(true)
	for _, drive := range drives {
		driveItem := newTreeViewItem(drive[:2])
		mw.treeView.Items().Add(driveItem)
	}
	mw.treeView.SetSuspended(false)

	mw.tableView, _ = walk.NewTableView(splitter)
	mw.tableView.SetModel(mw.fileInfoModel)
	mw.tableView.SetSingleItemSelection(true)

	mw.tableView.CurrentIndexChanged().Attach(func() {
		var url string

		index := mw.tableView.CurrentIndex()
		if index > -1 {
			name := mw.fileInfoModel.items[index].Name
			url = path.Join(pathForTreeViewItem(mw.selTvwItem), name)
		}

		mw.preview.SetURL(url)
	})

	mw.preview, _ = walk.NewWebView(splitter)

	mw.SetMinMaxSize(walk.Size{600, 400}, walk.Size{})
	mw.SetSize(walk.Size{800, 600})
	mw.Show()

	mw.Run()
}