-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglfw_opengl3_filedialog.nim
163 lines (142 loc) · 6.38 KB
/
glfw_opengl3_filedialog.nim
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Compiling:
# nim c -d:ImGuiFileDialogEnable glfw_opengl3_filedialog
import ../utils/appImGui
import ../utils/themes/themeGold
when defined(windows):
when not defined(vcc): # imguinVcc.res TODO WIP
include ./res/resource
const MainWinWidth = 1024
const MainWinHeight = 800
# Forward definitions
proc setFileStyle*(cfd: ptr ImGuiFileDialog)
template copyToString(sName:string, pName:cstring) =
if not isNil pName:
defer: free(pName)
var sz = pName.len
sName = newString(sz)
copyMem(addr sName[0], pName, sz)
#------
# main
#------
proc main() =
var win = createImGui(MainWinWidth, MainWinHeight, title="FileDialog demo")
defer: destroyImGui(win)
let theme = Theme.classic
setTheme(theme)
var
showDemoWindow = false
sFnameSelected{.global.}:string
var
sFilePathName:string
sFileDirPath:string
sFilter:string
sDatas:string
#------------------------------
# Create FileDialog object
#------------------------------
let cfd = IGFD_Create()
defer: IGFD_Destroy(cfd) # destroy ImGuiFileDialog
setFileStyle(cfd)
#-----------
# main loop
#-----------
while not win.handle.windowShouldClose:
glfwPollEvents()
newFrame()
if showDemoWindow:
igShowDemoWindow(addr showDemoWindow)
block:
igBegin("FileOpenDialog demo", nil, 0)
defer: igEnd()
# Show file open dialog
if igButton("Open file", vec2(100, 50)):
#---------------------------
# Triggered FileOpenDialog
#---------------------------
var config = IGFD_FileDialog_Config_Get()
config.path = "."
config.flags = ImGuiFileDialogFlags_Modal.int32 or
ImGuiFileDialogFlags_ShowDevicesButton.int32 or
ImGuiFileDialogFlags_CaseInsensitiveExtentionFiltering.int32
# ImGuiFileDialogFlags_ConfirmOverwrite.int32
IGFD_OpenDialog(cfd,
"filedlg".cstring, # dialog key (make it possible to have different treatment reagrding the dialog key
(ICON_FA_FILE & " Open a File").cstring, # dialog title
"all (*){.*},c files(*.c *.h){.c,.h}".cstring, # dialog filter syntax : simple => .h,.c,.pp, etc and collections : text1{filter0,filter1,filter2}, text2{filter0,filter1,filter2}, etc..
# dialog filter syntax : if one wants to select directory then set nil
config) # the file dialog config
themeGold()
setTooltip("[Open file]") # Show hint
#------------------------------
# Start display FileDialog
#------------------------------
let ioptr = igGetIO()
let maxSize = vec2(ioptr.DisplaySize.x * 0.8, ioptr.DisplaySize.y * 0.8)
let minSize = vec2(maxSize.x * 0.25, maxSize.y * 0.25)
if IGFD_DisplayDialog(cfd, "filedlg".cstring, ImGuiWindowFlags_NoCollapse.ImGuiWindowFlags, minSize, maxSize):
defer: IGFD_CloseDialog(cfd)
if IGFD_IsOk(cfd) : # result ok
var cstr:cstring
cstr = IGFD_GetFilePathName(cfd, IGFD_ResultMode_AddIfNoFileExt.IGFD_ResultMode)
copyToString(sFilePathName, cstr)
cstr = IGFD_GetCurrentPath(cfd)
copyToString(sFileDirPath, cstr)
cstr = IGFD_GetCurrentFilter(cfd)
copyToString(sFilter, cstr)
# here convert from string because a string was passed as a userDatas, but it can be what you want
let pDatas = IGFD_GetUserDatas(cfd)
if not isNil pDatas:
cstr = cast[cstring](pDatas)
copyToString(sDatas, cstr)
# TODO
#var csel = IGFD_GetSelection(cfd, IGFD_ResultMode_KeepInputFile.IGFD_ResultMode) # multi selection
#defer: IGFD_Selection_DestroyContent(addr csel)
#echo "Selection :\n"
#for i in 0..<csel.count:
#let table = cast[UncheckedArray[IGFD_Selection_Pair]](csel.table)
# # echo "($#) FileName $# => path $#\n" % [$i, $csel.table[i].fileName, $csel.table[i].filePathName]
setTheme(theme)
# end DisplayDialog
igText("Selected file = %s", sFilePathName.cstring)
igText("Dir = %s", sFileDirPath.cstring)
igText("Filter = %s", sFilter.cstring)
igText("Datas = %s", sDatas.cstring)
block:
igBegin("Nim: Dear ImGui test with Futhark", nil, 0)
defer: igEnd()
igText((ICON_FA_COMMENT & " " & getFrontendVersionString()).cstring)
igText((ICON_FA_COMMENT_SMS & " " & getBackendVersionString()).cstring)
igText("%s %s", ICON_FA_COMMENT_DOTS & " Dear ImGui", igGetVersion())
igText("%s%s", ICON_FA_COMMENT_MEDICAL & " Nim-", NimVersion)
igCheckbox("Demo window", addr showDemoWindow)
igText("Application average %.3f ms/frame (%.1f FPS)", (1000.0f / igGetIO().Framerate).cfloat, igGetIO().Framerate.cfloat)
#
render(win)
#### end while
#------
# main
#------
main()
#-----------------
#--- setFileStyle
#-----------------
proc setFileStyle*(cfd: ptr ImGuiFileDialog) =
let clGreen = vec4(0f, 1f, 0f, 1f)
let clYellow = vec4(1f, 1f, 0f, 1f)
let clOrange = vec4(1f, 165.0/255.0, 0f, 1f)
let clWhite2 = vec4(0.98, 0.98, 1f, 1f)
let clWhite = vec4(1f, 0f, 1f, 1f)
let clCyan = vec4(0f, 1f, 1f, 1f)
let clPurple = vec4(255f, 51/255.0, 255f, 1f)
let pFont = igGetDefaultFont()
let byExt = IGFD_FileStyleByExtention.IGFD_FileStyleFlags
IGFD_SetFileStyle(cfd, byExt , ".exe", clCyan, ICON_FA_FILE, pFont)
IGFD_SetFileStyle(cfd, byExt , ".nim", clPurple, ICON_FA_FILE, pFont)
IGFD_SetFileStyle(cfd, byExt , ".c", clGreen, ICON_FA_FILE, pFont)
IGFD_SetFileStyle(cfd, byExt , ".h", clYellow, ICON_FA_FILE, pFont)
IGFD_SetFileStyle(cfd, byExt , ".txt", clWhite2, ICON_FA_FILE_LINES, pFont)
IGFD_SetFileStyle(cfd, byExt , ".bat", clWhite2, ICON_FA_FILE, pFont)
IGFD_SetFileStyle(cfd, byExt , ".ini", clWhite2, ICON_FA_FILE, pFont)
IGFD_SetFileStyle(cfd, byExt , ".md", clWhite, ICON_FA_FILE, pFont)
#IGFD_SetFileStyle(cfd, byExt , "(.+[.].+)", clWhite2, ICON_FA_FILE, pFont)
IGFD_SetFileStyle(cfd, IGFD_FileStyleByTypeDir.IGFD_FileStyleFlags , nil, clOrange, ICON_FA_FOLDER, pFont)