github.com/secoba/wails/v2@v2.6.4/internal/frontend/desktop/windows/winc/commondlgs.go (about) 1 //go:build windows 2 3 /* 4 * Copyright (C) 2019 The Winc Authors. All Rights Reserved. 5 * Copyright (C) 2010-2013 Allen Dang. All Rights Reserved. 6 */ 7 8 package winc 9 10 import ( 11 "fmt" 12 "syscall" 13 "unsafe" 14 15 "github.com/secoba/wails/v2/internal/frontend/desktop/windows/winc/w32" 16 ) 17 18 func genOFN(parent Controller, title, filter string, filterIndex uint, initialDir string, buf []uint16) *w32.OPENFILENAME { 19 var ofn w32.OPENFILENAME 20 ofn.StructSize = uint32(unsafe.Sizeof(ofn)) 21 ofn.Owner = parent.Handle() 22 23 if filter != "" { 24 filterBuf := make([]uint16, len(filter)+1) 25 copy(filterBuf, syscall.StringToUTF16(filter)) 26 // Replace '|' with the expected '\0' 27 for i, c := range filterBuf { 28 if byte(c) == '|' { 29 filterBuf[i] = uint16(0) 30 } 31 } 32 ofn.Filter = &filterBuf[0] 33 ofn.FilterIndex = uint32(filterIndex) 34 } 35 36 ofn.File = &buf[0] 37 ofn.MaxFile = uint32(len(buf)) 38 39 if initialDir != "" { 40 ofn.InitialDir = syscall.StringToUTF16Ptr(initialDir) 41 } 42 if title != "" { 43 ofn.Title = syscall.StringToUTF16Ptr(title) 44 } 45 46 ofn.Flags = w32.OFN_FILEMUSTEXIST 47 return &ofn 48 } 49 50 func ShowOpenFileDlg(parent Controller, title, filter string, filterIndex uint, initialDir string) (filePath string, accepted bool) { 51 buf := make([]uint16, 1024) 52 ofn := genOFN(parent, title, filter, filterIndex, initialDir, buf) 53 54 if accepted = w32.GetOpenFileName(ofn); accepted { 55 filePath = syscall.UTF16ToString(buf) 56 } 57 return 58 } 59 60 func ShowSaveFileDlg(parent Controller, title, filter string, filterIndex uint, initialDir string) (filePath string, accepted bool) { 61 buf := make([]uint16, 1024) 62 ofn := genOFN(parent, title, filter, filterIndex, initialDir, buf) 63 64 if accepted = w32.GetSaveFileName(ofn); accepted { 65 filePath = syscall.UTF16ToString(buf) 66 } 67 return 68 } 69 70 func ShowBrowseFolderDlg(parent Controller, title string) (folder string, accepted bool) { 71 var bi w32.BROWSEINFO 72 bi.Owner = parent.Handle() 73 bi.Title = syscall.StringToUTF16Ptr(title) 74 bi.Flags = w32.BIF_RETURNONLYFSDIRS | w32.BIF_NEWDIALOGSTYLE 75 76 w32.CoInitialize() 77 ret := w32.SHBrowseForFolder(&bi) 78 w32.CoUninitialize() 79 80 folder = w32.SHGetPathFromIDList(ret) 81 accepted = folder != "" 82 return 83 } 84 85 // MsgBoxOkCancel basic pop up message. Returns 1 for OK and 2 for CANCEL. 86 func MsgBoxOkCancel(parent Controller, title, caption string) int { 87 return MsgBox(parent, title, caption, w32.MB_ICONEXCLAMATION|w32.MB_OKCANCEL) 88 } 89 90 func MsgBoxYesNo(parent Controller, title, caption string) int { 91 return MsgBox(parent, title, caption, w32.MB_ICONEXCLAMATION|w32.MB_YESNO) 92 } 93 94 func MsgBoxOk(parent Controller, title, caption string) { 95 MsgBox(parent, title, caption, w32.MB_ICONINFORMATION|w32.MB_OK) 96 } 97 98 // Warningf is generic warning message with OK and Cancel buttons. Returns 1 for OK. 99 func Warningf(parent Controller, format string, data ...interface{}) int { 100 caption := fmt.Sprintf(format, data...) 101 return MsgBox(parent, "Warning", caption, w32.MB_ICONWARNING|w32.MB_OKCANCEL) 102 } 103 104 // Printf is generic info message with OK button. 105 func Printf(parent Controller, format string, data ...interface{}) { 106 caption := fmt.Sprintf(format, data...) 107 MsgBox(parent, "Information", caption, w32.MB_ICONINFORMATION|w32.MB_OK) 108 } 109 110 // Errorf is generic error message with OK button. 111 func Errorf(parent Controller, format string, data ...interface{}) { 112 caption := fmt.Sprintf(format, data...) 113 MsgBox(parent, "Error", caption, w32.MB_ICONERROR|w32.MB_OK) 114 } 115 116 func MsgBox(parent Controller, title, caption string, flags uint) int { 117 var result int 118 if parent != nil { 119 result = w32.MessageBox(parent.Handle(), caption, title, flags) 120 } else { 121 result = w32.MessageBox(0, caption, title, flags) 122 } 123 124 return result 125 }