Skip to content

Commit 78f3df4

Browse files
committed
[ADD] : add a way for tune the width of validation buttons, ok and cancel (defines okButtonWidth and cancelButtonWidth)
[ADD] : add a way for tune the position of the validations buttons. okCancelButtonAlignement is a ratio by ex 0.0 is left, 0.5 middle, 1.0 right, and other ratios
1 parent d8aaf0e commit 78f3df4

3 files changed

Lines changed: 72 additions & 41 deletions

File tree

ImGuiFileDialog.cpp

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,18 @@ namespace IGFD
128128
#ifndef okButtonString
129129
#define okButtonString "OK"
130130
#endif // okButtonString
131+
#ifndef okButtonWidth
132+
#define okButtonWidth 0.0f
133+
#endif // okButtonWidth
131134
#ifndef cancelButtonString
132135
#define cancelButtonString "Cancel"
133136
#endif // cancelButtonString
137+
#ifndef cancelButtonWidth
138+
#define cancelButtonWidth 0.0f
139+
#endif // cancelButtonWidth
140+
#ifndef okCancelButtonAlignement
141+
#define okCancelButtonAlignement 0.0f
142+
#endif // okCancelButtonAlignement
134143
#ifndef resetButtonString
135144
#define resetButtonString "R"
136145
#endif // resetButtonString
@@ -331,6 +340,15 @@ namespace IGFD
331340
}
332341
#endif
333342

343+
inline void imAlignForWidth(float width, float alignment = 0.5f)
344+
{
345+
ImGuiStyle& style = ImGui::GetStyle();
346+
float avail = ImGui::GetContentRegionAvail().x;
347+
float off = (avail - width) * alignment;
348+
if (off > 0.0f)
349+
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + off);
350+
}
351+
334352
/////////////////////////////////////////////////////////////////////////////////////
335353
//// FILE EXTENTIONS INFOS //////////////////////////////////////////////////////////
336354
/////////////////////////////////////////////////////////////////////////////////////
@@ -3845,6 +3863,43 @@ namespace IGFD
38453863
}
38463864
}
38473865

3866+
bool IGFD::FileDialog::prDrawValidationButtons()
3867+
{
3868+
bool res = false;
3869+
3870+
auto& fdFile = prFileDialogInternal.puFileManager;
3871+
3872+
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + (ImGui::GetContentRegionAvail().x - prOkCancelButtonWidth) * okCancelButtonAlignement);
3873+
3874+
ImGui::BeginGroup();
3875+
3876+
// OK Button
3877+
if (prFileDialogInternal.puCanWeContinue && strlen(fdFile.puFileNameBuffer))
3878+
{
3879+
if (IMGUI_BUTTON(okButtonString "##validationdialog", ImVec2(okButtonWidth, 0.0f)) || prFileDialogInternal.puIsOk)
3880+
{
3881+
prFileDialogInternal.puIsOk = true;
3882+
res = true;
3883+
}
3884+
3885+
ImGui::SameLine();
3886+
}
3887+
3888+
// Cancel Button
3889+
if (IMGUI_BUTTON(cancelButtonString "##validationdialog", ImVec2(cancelButtonWidth, 0.0f)) ||
3890+
prFileDialogInternal.puNeedToExitDialog) // dialog exit asked
3891+
{
3892+
prFileDialogInternal.puIsOk = false;
3893+
res = true;
3894+
}
3895+
3896+
ImGui::EndGroup();
3897+
3898+
prOkCancelButtonWidth = ImGui::GetItemRectSize().x;
3899+
3900+
return res;
3901+
}
3902+
38483903
bool IGFD::FileDialog::prDrawFooter()
38493904
{
38503905
auto& fdFile = prFileDialogInternal.puFileManager;
@@ -3891,35 +3946,17 @@ namespace IGFD
38913946

38923947
bool res = false;
38933948

3894-
// OK Button
3895-
if (prFileDialogInternal.puCanWeContinue && strlen(fdFile.puFileNameBuffer))
3896-
{
3897-
if (IMGUI_BUTTON(okButtonString "##validationdialog") || prFileDialogInternal.puIsOk)
3898-
{
3899-
prFileDialogInternal.puIsOk = true;
3900-
res = true;
3901-
}
3902-
3903-
ImGui::SameLine();
3904-
}
3905-
3906-
// Cancel Button
3907-
if (IMGUI_BUTTON(cancelButtonString "##validationdialog") ||
3908-
prFileDialogInternal.puNeedToExitDialog) // dialog exit asked
3909-
{
3910-
prFileDialogInternal.puIsOk = false;
3911-
res = true;
3912-
}
3949+
res = prDrawValidationButtons();
39133950

39143951
prFileDialogInternal.puFooterHeight = ImGui::GetCursorPosY() - posY;
39153952

39163953
return res;
39173954
}
39183955

3919-
bool IGFD::FileDialog::prSelectableItem(int vidx, std::shared_ptr<FileInfos> vInfos, bool vSelected, const char* vFmt, ...)
3956+
void IGFD::FileDialog::prSelectableItem(int vidx, std::shared_ptr<FileInfos> vInfos, bool vSelected, const char* vFmt, ...)
39203957
{
39213958
if (!vInfos.use_count())
3922-
return false;
3959+
return;
39233960

39243961
auto& fdi = prFileDialogInternal.puFileManager;
39253962

@@ -3974,8 +4011,6 @@ namespace IGFD
39744011
fdi.SelectFileName(prFileDialogInternal, vInfos);
39754012
}
39764013
}
3977-
3978-
//return true; // needToBreakTheloop
39794014
}
39804015
else
39814016
{
@@ -3987,8 +4022,6 @@ namespace IGFD
39874022
}
39884023
}
39894024
}
3990-
3991-
return false;
39924025
}
39934026

39944027
void IGFD::FileDialog::prBeginFileColorIconStyle(std::shared_ptr<FileInfos> vFileInfos, bool& vOutShowColor, std::string& vOutStr, ImFont** vOutFont)
@@ -4164,11 +4197,9 @@ namespace IGFD
41644197

41654198
ImGui::TableNextRow();
41664199

4167-
bool needToBreakTheloop = false;
4168-
41694200
if (ImGui::TableNextColumn()) // file name
41704201
{
4171-
needToBreakTheloop = prSelectableItem(i, infos, selected, _str.c_str());
4202+
prSelectableItem(i, infos, selected, _str.c_str());
41724203
}
41734204
if (ImGui::TableNextColumn()) // file type
41744205
{
@@ -4191,9 +4222,6 @@ namespace IGFD
41914222
}
41924223

41934224
prEndFileColorIconStyle(_showColor, _font);
4194-
4195-
if (needToBreakTheloop)
4196-
break;
41974225
}
41984226
}
41994227
prFileListClipper.End();
@@ -4381,11 +4409,9 @@ namespace IGFD
43814409

43824410
ImGui::TableNextRow();
43834411

4384-
bool needToBreakTheloop = false;
4385-
43864412
if (ImGui::TableNextColumn()) // file name
43874413
{
4388-
needToBreakTheloop = prSelectableItem(i, infos, selected, _str.c_str());
4414+
prSelectableItem(i, infos, selected, _str.c_str());
43894415
}
43904416
if (ImGui::TableNextColumn()) // file type
43914417
{
@@ -4424,9 +4450,6 @@ namespace IGFD
44244450
}
44254451

44264452
prEndFileColorIconStyle(_showColor, _font);
4427-
4428-
if (needToBreakTheloop)
4429-
break;
44304453
}
44314454
}
44324455
prFileListClipper.End();

ImGuiFileDialog.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,10 +1162,11 @@ namespace IGFD
11621162
private:
11631163
FileDialogInternal prFileDialogInternal;
11641164
ImGuiListClipper prFileListClipper;
1165+
float prOkCancelButtonWidth = 0.0f;
11651166

11661167
public:
11671168
bool puAnyWindowsHovered = false; // not remember why haha :) todo : to check if we can remove
1168-
1169+
11691170
public:
11701171
static FileDialog* Instance() // Singleton for easier accces form anywhere but only one dialog at a time
11711172
{
@@ -1329,8 +1330,9 @@ namespace IGFD
13291330
virtual bool prDrawFooter(); // draw footer part of the dialog (file field, fitler combobox, ok/cancel btn's)
13301331

13311332
// widgets components
1333+
virtual bool prDrawValidationButtons(); // ok, cancel buttons
13321334
virtual void prDrawSidePane(float vHeight); // draw side pane
1333-
virtual bool prSelectableItem(int vidx,
1335+
virtual void prSelectableItem(int vidx,
13341336
std::shared_ptr<FileInfos> vInfos,
13351337
bool vSelected, const char* vFmt, ...); // draw a custom selectable behavior item
13361338
virtual void prDrawFileListView(ImVec2 vSize); // draw file list view (default mode)

ImGuiFileDialogConfig.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252

5353
// locales string
5454
//#define createDirButtonString "+"
55-
//#define okButtonString " OK"
56-
//#define cancelButtonString " Cancel"
5755
//#define resetButtonString "R"
5856
//#define drivesButtonString "Drives"
5957
//#define editPathButtonString "E"
@@ -73,6 +71,14 @@
7371
//#define OverWriteDialogConfirmButtonString "Confirm"
7472
//#define OverWriteDialogCancelButtonString "Cancel"
7573

74+
//Validation buttons
75+
//#define okButtonString " OK"
76+
//#define cancelButtonString " Cancel"
77+
//#define okButtonWidth 0.0f
78+
//#define cancelButtonWidth 0.0f
79+
//alignement [0:1], 0.0 is left, 0.5 middle, 1.0 right, and other ratios
80+
//#define okCancelButtonAlignement 0.0f
81+
7682
// DateTimeFormat
7783
// see strftime functionin <ctime> for customize
7884
// "%Y/%m/%d %H:%M" give 2021:01:22 11:47

0 commit comments

Comments
 (0)