Skip to content

Commit 4f0fe30

Browse files
author
Zoltán Turányi
committed
Allow the use to select fonts per file type
In addition to the user selecting color and icon per filter, this patch allows the user to specify an ImFont, as well. This is a backwards compatible API change, exitsing code works the same way with no changes.
1 parent 13c07da commit 4f0fe30

2 files changed

Lines changed: 30 additions & 17 deletions

File tree

ImGuiFileDialog.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,11 @@ namespace IGFD
325325

326326
}
327327

328-
IGFD::FileExtentionInfos::FileExtentionInfos(const ImVec4& vColor, const std::string& vIcon)
328+
IGFD::FileExtentionInfos::FileExtentionInfos(const ImVec4& vColor, const std::string& vIcon, ImFont* f)
329329
{
330330
color = vColor;
331331
icon = vIcon;
332+
font = f;
332333
}
333334

334335
/////////////////////////////////////////////////////////////////////////////////////
@@ -824,12 +825,12 @@ namespace IGFD
824825
prFileExtentionInfos[vFilter] = vInfos;
825826
}
826827

827-
void IGFD::FilterManager::SetExtentionInfos(const std::string& vFilter, const ImVec4& vColor, const std::string& vIcon)
828+
void IGFD::FilterManager::SetExtentionInfos(const std::string& vFilter, const ImVec4& vColor, const std::string& vIcon, ImFont* vFont)
828829
{
829-
prFileExtentionInfos[vFilter] = FileExtentionInfos(vColor, vIcon);
830+
prFileExtentionInfos[vFilter] = FileExtentionInfos(vColor, vIcon, vFont);
830831
}
831832

832-
bool IGFD::FilterManager::GetExtentionInfos(const std::string& vFilter, ImVec4* vOutColor, std::string* vOutIcon)
833+
bool IGFD::FilterManager::GetExtentionInfos(const std::string& vFilter, ImVec4* vOutColor, std::string* vOutIcon, ImFont **vOutFont)
833834
{
834835
if (vOutColor)
835836
{
@@ -840,6 +841,10 @@ namespace IGFD
840841
{
841842
*vOutIcon = prFileExtentionInfos[vFilter].icon;
842843
}
844+
if (vOutFont)
845+
{
846+
*vOutFont = prFileExtentionInfos[vFilter].font;
847+
}
843848
return true;
844849
}
845850
}
@@ -3757,19 +3762,22 @@ namespace IGFD
37573762

37583763
ImVec4 c;
37593764
std::string icon;
3765+
ImFont* font = 0;
37603766
//Directory and Link infos override the one specified by extension
37613767
bool showColor;
37623768
if (infos->fileType == 'd')
3763-
showColor = prFileDialogInternal.puFilterManager.GetExtentionInfos(DIR_FILTER_STRING, &c, &icon);
3769+
showColor = prFileDialogInternal.puFilterManager.GetExtentionInfos(DIR_FILTER_STRING, &c, &icon, &font);
37643770
else if (infos->fileType == 'l')
3765-
showColor = prFileDialogInternal.puFilterManager.GetExtentionInfos(LINK_FILTER_STRING, &c, &icon);
3771+
showColor = prFileDialogInternal.puFilterManager.GetExtentionInfos(LINK_FILTER_STRING, &c, &icon, &font);
37663772
else
37673773
showColor = false;
37683774
if (!showColor)
3769-
showColor = prFileDialogInternal.puFilterManager.GetExtentionInfos(infos->fileExt, &c, &icon);
3775+
showColor = prFileDialogInternal.puFilterManager.GetExtentionInfos(infos->fileExt, &c, &icon, &font);
37703776

37713777
if (showColor)
37723778
ImGui::PushStyleColor(ImGuiCol_Text, c);
3779+
if (font)
3780+
ImGui::PushFont(font);
37733781

37743782
std::string str;// = " " + infos->fileName;
37753783
if (showColor && !icon.empty()) str = icon;
@@ -3809,6 +3817,8 @@ namespace IGFD
38093817
ImGui::Text("%s", infos->fileModifDate.c_str());
38103818
}
38113819

3820+
if (font)
3821+
ImGui::PopFont();
38123822
if (showColor)
38133823
ImGui::PopStyleColor();
38143824

@@ -4129,14 +4139,14 @@ namespace IGFD
41294139
prFileDialogInternal.puFilterManager.SetExtentionInfos(vFilter, vInfos);
41304140
}
41314141

4132-
void IGFD::FileDialog::SetExtentionInfos(const std::string& vFilter, const ImVec4& vColor, const std::string& vIcon)
4142+
void IGFD::FileDialog::SetExtentionInfos(const std::string& vFilter, const ImVec4& vColor, const std::string& vIcon, ImFont* vFont)
41334143
{
4134-
prFileDialogInternal.puFilterManager.SetExtentionInfos(vFilter, vColor, vIcon);
4144+
prFileDialogInternal.puFilterManager.SetExtentionInfos(vFilter, vColor, vIcon, vFont);
41354145
}
41364146

4137-
bool IGFD::FileDialog::GetExtentionInfos(const std::string& vFilter, ImVec4* vOutColor, std::string* vOutIcon)
4147+
bool IGFD::FileDialog::GetExtentionInfos(const std::string& vFilter, ImVec4* vOutColor, std::string* vOutIcon, ImFont **vOutFont)
41384148
{
4139-
return prFileDialogInternal.puFilterManager.GetExtentionInfos(vFilter, vOutColor, vOutIcon);
4149+
return prFileDialogInternal.puFilterManager.GetExtentionInfos(vFilter, vOutColor, vOutIcon, vOutFont);
41404150
}
41414151

41424152
void IGFD::FileDialog::ClearExtentionInfos()

ImGuiFileDialog.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,11 @@ namespace IGFD
659659
public:
660660
ImVec4 color = ImVec4(0, 0, 0, 0);
661661
std::string icon;
662+
ImFont* font = nullptr;
662663

663664
public:
664665
FileExtentionInfos();
665-
FileExtentionInfos(const ImVec4& vColor, const std::string& vIcon = "");
666+
FileExtentionInfos(const ImVec4& vColor, const std::string& vIcon = "", ImFont *f=nullptr);
666667
};
667668

668669
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -731,13 +732,13 @@ namespace IGFD
731732
void ParseFilters(const char* vFilters); // Parse filter syntax, detect and parse filter collection
732733
void SetSelectedFilterWithExt(const std::string& vFilter); // Select filter
733734
void SetExtentionInfos(const std::string& vFilter, const FileExtentionInfos& vInfos); // link filter to ExtentionInfos
734-
void SetExtentionInfos(const std::string& vFilter, const ImVec4& vColor, const std::string& vIcon); // link filter to Color and Icon
735-
bool GetExtentionInfos(const std::string& vFilter, ImVec4* vOutColor, std::string* vOutIcon); // get Color and Icon for Filter
735+
void SetExtentionInfos(const std::string& vFilter, const ImVec4& vColor, const std::string& vIcon, ImFont* vFont);// link filter to Color and Icon and Font
736+
bool GetExtentionInfos(const std::string& vFilter, ImVec4* vOutColor, std::string* vOutIcon, ImFont **vOutFont); // get Color and Icon for Filter
736737
void ClearExtentionInfos(); // clear prFileExtentionInfos
737738
bool IsCoveredByFilters(const std::string& vTag) const; // check if current file extention (vTag) is covered by current filter
738739
bool DrawFilterComboBox(FileDialogInternal& vFileDialogInternal); // draw the filter combobox
739740
FilterInfosStruct GetSelectedFilter(); // get the current selected filter
740-
std::string ReplaceExtentionWithCurrentFilter(const std::string vFile) const; // replace the extention of the current file by the selected filter
741+
std::string ReplaceExtentionWithCurrentFilter(const std::string vFile) const; // replace the extention of the current file by the selected filter
741742
void SetDefaultFilterIfNotDefined(); // define the first filter if no filter is selected
742743
};
743744

@@ -1207,11 +1208,13 @@ namespace IGFD
12071208
void SetExtentionInfos( // SetExtention datas for have custom display of particular file type
12081209
const std::string& vFilter, // extention filter to tune
12091210
const ImVec4& vColor, // wanted color for the display of the file with extention filter
1210-
const std::string& vIcon = ""); // wanted text or icon of the file with extention filter
1211+
const std::string& vIcon = "", // wanted text or icon of the file with extention filter
1212+
ImFont *vFont = nullptr); // wantes font
12111213
bool GetExtentionInfos( // GetExtention datas. return true is extention exist
12121214
const std::string& vFilter, // extention filter (same as used in SetExtentionInfos)
12131215
ImVec4* vOutColor, // color to retrieve
1214-
std::string* vOutIcon = 0); // icon or text to retrieve
1216+
std::string* vOutIcon = nullptr, // icon or text to retrieve
1217+
ImFont** vOutFont = nullptr); // font to retreive
12151218
void ClearExtentionInfos(); // clear extentions setttings
12161219

12171220
void SetLocales( // set locales to use before and after the dialog display

0 commit comments

Comments
 (0)