Module: FileDialog

Defined in:
lib/filedialog.rb,
lib/filedialog/version.rb,
ext/filedialog/filedialog.c

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.2.2"

Class Method Summary collapse

Class Method Details

.open_dialog(filter_list, default_path, multiple: false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/filedialog.rb', line 24

def self.open_dialog(filter_list, default_path, multiple: false)
  if multiple
    result = open_dialog_multi_native(
      filter_list.encode("UTF-8"), default_path.encode("UTF-8"))
    result.map{|x| x.force_encoding("UTF-8") }
  else
    result = open_dialog_native(
      filter_list.encode("UTF-8"), default_path.encode("UTF-8"))
    result.force_encoding("UTF-8")
  end
end

.open_dialog_multi_native(filter_list, default_path) ⇒ Object



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
# File 'ext/filedialog/filedialog.c', line 42

static VALUE FileDialog_s_open_dialog_multi(
	VALUE self, VALUE filter_list, VALUE default_path)
{
	nfdresult_t rc;
	nfdpathset_t pathset;
	size_t pathsetCount;
	VALUE result;

	Check_Type(filter_list, T_STRING);
	Check_Type(default_path, T_STRING);

	rc = NFD_OpenDialogMultiple(StringValueCStr(filter_list), StringValueCStr(default_path), &pathset);
	if (rc == NFD_ERROR)
		rb_raise(eFileDialogError, "%s", NFD_GetError());
	else if (rc == NFD_CANCEL)
		return Qnil;

	pathsetCount = NFD_PathSet_GetCount(&pathset);
	result = rb_ary_new_capa(pathsetCount);

	for (int i = 0; i < pathsetCount; i++)
		rb_ary_push(result, rb_str_new_cstr(NFD_PathSet_GetPath(&pathset, i)));

	NFD_PathSet_Free(&pathset);
	return result;
}

.open_dialog_native(filter_list, default_path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'ext/filedialog/filedialog.c', line 21

static VALUE FileDialog_s_open_dialog(
	VALUE self, VALUE filter_list, VALUE default_path)
{
	nfdresult_t rc;
	nfdchar_t *strResult;
	VALUE result;

	Check_Type(filter_list, T_STRING);
	Check_Type(default_path, T_STRING);

	rc = NFD_OpenDialog(StringValueCStr(filter_list), StringValueCStr(default_path), &strResult);
	if (rc == NFD_ERROR)
		rb_raise(eFileDialogError, "%s", NFD_GetError());
	else if (rc == NFD_CANCEL)
		return Qnil;

	result = rb_str_new_cstr(strResult);
	free(strResult);
	return result;
}

.pick_folder(default_path) ⇒ Object



42
43
44
# File 'lib/filedialog.rb', line 42

def self.pick_folder(default_path)
  pick_folder_native(default_path.encode("UTF-8")).force_encoding("UTF-8")
end

.pick_folder_native(default_path) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'ext/filedialog/filedialog.c', line 90

static VALUE FileDialog_s_pick_folder(
	VALUE self, VALUE default_path)
{
	nfdresult_t rc;
	nfdchar_t *strResult;
	VALUE result;

	Check_Type(default_path, T_STRING);

	rc = NFD_PickFolder(StringValueCStr(default_path), &strResult);
	if (rc == NFD_ERROR)
		rb_raise(eFileDialogError, "%s", NFD_GetError());
	else if (rc == NFD_CANCEL)
		return Qnil;

	result = rb_str_new_cstr(strResult);
	free(strResult);
	return result;
}

.save_dialog(filter_list, default_path) ⇒ Object



36
37
38
39
40
# File 'lib/filedialog.rb', line 36

def self.save_dialog(filter_list, default_path)
  result = save_dialog_native(
    filter_list.encode("UTF-8"), default_path.encode("UTF-8"))
  result.force_encoding("UTF-8")
end

.save_dialog_native(filter_list, default_path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'ext/filedialog/filedialog.c', line 69

static VALUE FileDialog_s_save_dialog(
	VALUE self, VALUE filter_list, VALUE default_path)
{
	nfdresult_t rc;
	nfdchar_t *strResult;
	VALUE result;

	Check_Type(filter_list, T_STRING);
	Check_Type(default_path, T_STRING);

	rc = NFD_SaveDialog(StringValueCStr(filter_list), StringValueCStr(default_path), &strResult);
	if (rc == NFD_ERROR)
		rb_raise(eFileDialogError, "%s", NFD_GetError());
	else if (rc == NFD_CANCEL)
		return Qnil;

	result = rb_str_new_cstr(strResult);
	free(strResult);
	return result;
}