Skip to content
This repository
tree: 09597c7fe3
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 71 lines (55 sloc) 1.675 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package walk

import (
"fmt"
"syscall"
"unsafe"
)

import . "github.com/lxn/go-winapi"

type FileDialog struct {
Title string
FilePath string
InitialDirPath string
Filter string
FilterIndex int
}

func (dlg *FileDialog) show(owner RootWidget, fun func(ofn *OPENFILENAME) bool) (accepted bool, err error) {
ofn := &OPENFILENAME{}

ofn.LStructSize = uint32(unsafe.Sizeof(*ofn))
ofn.HwndOwner = owner.BaseWidget().hWnd

filter := make([]uint16, len(dlg.Filter)+2)
copy(filter, syscall.StringToUTF16(dlg.Filter))
// Replace '|' with the expected '\0'.
for i, c := range filter {
if byte(c) == '|' {
filter[i] = uint16(0)
}
}
ofn.LpstrFilter = &filter[0]
ofn.NFilterIndex = uint32(dlg.FilterIndex)

filePath := make([]uint16, 1024)
copy(filePath, syscall.StringToUTF16(dlg.FilePath))
ofn.LpstrFile = &filePath[0]
ofn.NMaxFile = uint32(len(filePath))

ofn.LpstrInitialDir = syscall.StringToUTF16Ptr(dlg.InitialDirPath)
ofn.LpstrTitle = syscall.StringToUTF16Ptr(dlg.Title)
ofn.Flags = OFN_FILEMUSTEXIST

if !fun(ofn) {
errno := CommDlgExtendedError()
if errno != 0 {
err = newError(fmt.Sprintf("Error %d", errno))
}
return
}

dlg.FilePath = syscall.UTF16ToString(filePath)

accepted = true

return
}

func (dlg *FileDialog) ShowOpen(owner RootWidget) (accepted bool, err error) {
return dlg.show(owner, GetOpenFileName)
}

func (dlg *FileDialog) ShowSave(owner RootWidget) (accepted bool, err error) {
return dlg.show(owner, GetSaveFileName)
}
Something went wrong with that request. Please try again.