Skip to content

Commit

Permalink
Merge pull request #5 from harry1453/specify-hwnd
Browse files Browse the repository at this point in the history
Specify hwnd
  • Loading branch information
harryjph authored Jun 22, 2021
2 parents 486d1d9 + 6702082 commit 2383712
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 41 deletions.
4 changes: 2 additions & 2 deletions _examples/notusingutil/OpenFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func main() {
log.Fatal(err)
}
go func() {
time.Sleep(time.Second)
if err := openDialog.SetFileName("bruh"); err != nil {
time.Sleep(2 * time.Second)
if err := openDialog.SetFileName("hello world"); err != nil {
panic(err)
}
}()
Expand Down
2 changes: 2 additions & 0 deletions cfd/CommonFileDialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type Dialog interface {
// Show the dialog to the user.
// Blocks until the user has closed the dialog.
Show() error
// Sets the dialog's parent window. Use 0 to set the dialog to have no parent window.
SetParentWindowHandle(hwnd uintptr)
// Show the dialog to the user.
// Blocks until the user has closed the dialog and returns their selection.
// Returns an error if the user cancelled the dialog.
Expand Down
4 changes: 4 additions & 0 deletions cfd/CommonFileDialog_nonWindows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ import "fmt"

var unsupportedError = fmt.Errorf("common file dialogs are only available on windows")

// TODO doc
func NewOpenFileDialog(config DialogConfig) (OpenFileDialog, error) {
return nil, unsupportedError
}

// TODO doc
func NewOpenMultipleFilesDialog(config DialogConfig) (OpenMultipleFilesDialog, error) {
return nil, unsupportedError
}

// TODO doc
func NewSelectFolderDialog(config DialogConfig) (SelectFolderDialog, error) {
return nil, unsupportedError
}

// TODO doc
func NewSaveFileDialog(config DialogConfig) (SaveFileDialog, error) {
return nil, unsupportedError
}
14 changes: 9 additions & 5 deletions cfd/CommonFileDialog_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ package cfd

import "github.com/go-ole/go-ole"

func Initialize() {
func initialize() {
// Swallow error
_ = ole.CoInitializeEx(0, ole.COINIT_APARTMENTTHREADED|ole.COINIT_DISABLE_OLE1DDE)
}

// TODO doc
func NewOpenFileDialog(config DialogConfig) (OpenFileDialog, error) {
Initialize()
initialize()

openDialog, err := newIFileOpenDialog()
if err != nil {
Expand All @@ -23,8 +24,9 @@ func NewOpenFileDialog(config DialogConfig) (OpenFileDialog, error) {
return openDialog, nil
}

// TODO doc
func NewOpenMultipleFilesDialog(config DialogConfig) (OpenMultipleFilesDialog, error) {
Initialize()
initialize()

openDialog, err := newIFileOpenDialog()
if err != nil {
Expand All @@ -41,8 +43,9 @@ func NewOpenMultipleFilesDialog(config DialogConfig) (OpenMultipleFilesDialog, e
return openDialog, nil
}

// TODO doc
func NewSelectFolderDialog(config DialogConfig) (SelectFolderDialog, error) {
Initialize()
initialize()

openDialog, err := newIFileOpenDialog()
if err != nil {
Expand All @@ -59,8 +62,9 @@ func NewSelectFolderDialog(config DialogConfig) (SelectFolderDialog, error) {
return openDialog, nil
}

// TODO doc
func NewSaveFileDialog(config DialogConfig) (SaveFileDialog, error) {
Initialize()
initialize()

saveDialog, err := newIFileSaveDialog()
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions cfd/DialogConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type DialogConfig struct {
// For Save File Dialog, this extension will be used whenever a user does not specify an extension.
// Ignored by Select Folder Dialog.
DefaultExtension string
// ParentWindowHandle is the handle (HWND) to the parent window of the dialog.
// If left as 0 / nil, the dialog will have no parent window.
ParentWindowHandle uintptr
}

var defaultFilters = []FileFilter{
Expand Down Expand Up @@ -84,6 +87,8 @@ func (config *DialogConfig) apply(dialog Dialog) (err error) {
}
}

dialog.SetParentWindowHandle(config.ParentWindowHandle)

if dialog, ok := dialog.(FileDialog); ok {
var fileFilters []FileFilter
if config.FileFilters != nil && len(config.FileFilters) > 0 {
Expand Down
9 changes: 7 additions & 2 deletions cfd/iFileOpenDialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var (
)

type iFileOpenDialog struct {
vtbl *iFileOpenDialogVtbl
vtbl *iFileOpenDialogVtbl
parentWindowHandle uintptr
}

type iFileOpenDialogVtbl struct {
Expand All @@ -36,7 +37,11 @@ func newIFileOpenDialog() (*iFileOpenDialog, error) {
}

func (fileOpenDialog *iFileOpenDialog) Show() error {
return fileOpenDialog.vtbl.show(unsafe.Pointer(fileOpenDialog))
return fileOpenDialog.vtbl.show(unsafe.Pointer(fileOpenDialog), fileOpenDialog.parentWindowHandle)
}

func (fileOpenDialog *iFileOpenDialog) SetParentWindowHandle(hwnd uintptr) {
fileOpenDialog.parentWindowHandle = hwnd
}

func (fileOpenDialog *iFileOpenDialog) ShowAndGetResult() (string, error) {
Expand Down
9 changes: 7 additions & 2 deletions cfd/iFileSaveDialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var (
)

type iFileSaveDialog struct {
vtbl *iFileSaveDialogVtbl
vtbl *iFileSaveDialogVtbl
parentWindowHandle uintptr
}

type iFileSaveDialogVtbl struct {
Expand All @@ -36,7 +37,11 @@ func newIFileSaveDialog() (*iFileSaveDialog, error) {
}

func (fileSaveDialog *iFileSaveDialog) Show() error {
return fileSaveDialog.vtbl.show(unsafe.Pointer(fileSaveDialog))
return fileSaveDialog.vtbl.show(unsafe.Pointer(fileSaveDialog), fileSaveDialog.parentWindowHandle)
}

func (fileSaveDialog *iFileSaveDialog) SetParentWindowHandle(hwnd uintptr) {
fileSaveDialog.parentWindowHandle = hwnd
}

func (fileSaveDialog *iFileSaveDialog) ShowAndGetResult() (string, error) {
Expand Down
6 changes: 3 additions & 3 deletions cfd/vtblCommonFunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ func (vtbl *iUnknownVtbl) release(objPtr unsafe.Pointer) error {
return hresultToError(ret)
}

func (vtbl *iModalWindowVtbl) show(objPtr unsafe.Pointer) error {
func (vtbl *iModalWindowVtbl) show(objPtr unsafe.Pointer, hwnd uintptr) error {
ret, _, _ := syscall.Syscall(vtbl.Show,
1, // First argument is owner hWnd, we are just passing null
1,
uintptr(objPtr),
0,
hwnd,
0)
return hresultToError(ret)
}
Expand Down
6 changes: 4 additions & 2 deletions cfdutil/CFDUtil.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// +build windows

package cfdutil

import (
"github.com/harry1453/go-common-file-dialog/cfd"
)

// TODO doc
func ShowOpenFileDialog(config cfd.DialogConfig) (string, error) {
dialog, err := cfd.NewOpenFileDialog(config)
if err != nil {
Expand All @@ -15,6 +14,7 @@ func ShowOpenFileDialog(config cfd.DialogConfig) (string, error) {
return dialog.ShowAndGetResult()
}

// TODO doc
func ShowOpenMultipleFilesDialog(config cfd.DialogConfig) ([]string, error) {
dialog, err := cfd.NewOpenMultipleFilesDialog(config)
if err != nil {
Expand All @@ -24,6 +24,7 @@ func ShowOpenMultipleFilesDialog(config cfd.DialogConfig) ([]string, error) {
return dialog.ShowAndGetResults()
}

// TODO doc
func ShowPickFolderDialog(config cfd.DialogConfig) (string, error) {
dialog, err := cfd.NewSelectFolderDialog(config)
if err != nil {
Expand All @@ -33,6 +34,7 @@ func ShowPickFolderDialog(config cfd.DialogConfig) (string, error) {
return dialog.ShowAndGetResult()
}

// TODO doc
func ShowSaveFileDialog(config cfd.DialogConfig) (string, error) {
dialog, err := cfd.NewSaveFileDialog(config)
if err != nil {
Expand Down
25 changes: 0 additions & 25 deletions cfdutil/NonWindows.go

This file was deleted.

0 comments on commit 2383712

Please sign in to comment.