Skip to content

Commit

Permalink
fix(grid): grid now calculates correctly
Browse files Browse the repository at this point in the history
addition(grid): added fixed size
addition(window): added HideAll() and ShowAll()
addition(demo): added demo of creating 2 different windows
rename(test): renamed test project to demo
  • Loading branch information
MurkyYT committed Feb 18, 2024
1 parent ee9addd commit b71a816
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 92 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions Test/Test.vcxproj → Demo/Demo.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<ProjectGuid>{e0afc75e-90ec-4eeb-b3f7-29605a1ef545}</ProjectGuid>
<RootNamespace>Test</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectName>Test</ProjectName>
<ProjectName>Demo</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
Expand Down Expand Up @@ -143,7 +143,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Test.rc" />
<ResourceCompile Include="Demo.rc" />
</ItemGroup>
<ItemGroup>
<Image Include="icon1.ico" />
Expand Down
File renamed without changes.
103 changes: 65 additions & 38 deletions Test/main.cpp → Demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ void CheckBoxClick();
void ButtonClick();
void RadioGroupChanged();
void WindowClose();
void OpenGridExample();
void InitializeGridDemoWindow();
TextBox box(L"Test-TextBox", FALSE, 0, 0, 100, 100);
Button button(L"Input-Box text",FALSE, 0, 100, 100, 20);
CheckBox chkbox(L"CheckBox", 100, 300, 70, 15);
Expand All @@ -16,35 +18,73 @@ RadioButton button1(L"Fullscreen", 130, 90, 90, 15);
RadioButton button2(L"Windowed Fullscreen", 130, 110, 140, 15);
RadioButton button3(L"Windowed", 130, 130, 140, 15);
ListView lstView(10, 10, 200, 200);
HINSTANCE m_hInstance;
Grid grid;
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow) {
Window* gridWindow;
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow) {
(void)hPrevInstance;
(void)lpCmdLine;

Window window(hInstance);
window.SubscribeToOnClose(WindowClose);
m_hInstance = hInstance;

button.SubscribeToOnClick(ButtonClick);
chkbox.SubscribeToOnClick(CheckBoxClick);
group.SubscribeToOnChange(RadioGroupChanged);
group.AddRadioButton(&button1);
group.AddRadioButton(&button2);
group.AddRadioButton(&button3);
/*
* Add vertical scroll bar.
* If before window creation it shows correctly, if after, only scroll wheel works(could be a bug ? )
*/
* Add vertical scroll bar.
* If before window creation it shows correctly, if after, only scroll wheel works(could be a bug ? )
*/
box.SetStyle(box.GetStyle() | WS_VSCROLL);
chkbox.SetChecked(1);
grid.AddColumn(0, L"Auto");
grid.AddColumn(0, L"*");
grid.AddColumn(0, L"Auto");
lstView.SetHorizontalAligment(Stretch);
lstView.SetVerticalAligment(Stretch);
block.SetVerticalAligment(Center);
chkbox.SetVerticalAligment(Center);
grid.AddItem(&lstView, 0, 1);
grid.AddItem(&chkbox, 0, 2);
grid.AddItem(&block, 0, 0);

/* window.SetBackroundColor(Color::PURPLE);
window.m_StaticBacgkround = Color::PURPLE;
window.m_StaticTextColor = Color::LIGHT_PURPLE;
window.m_EditBacgkround = Color::LIGHT_PURPLE;
window.m_ButtonBacgkround = Color::WHITE;
window.m_ButtonTextColor = Color::DARK_PURPLE;*/
if (!window.Create(L"MUI Demo",(DWORD)IDI_ICON1)) {
return 0;

Window mainWindow(hInstance);
mainWindow.Create(L"MUI Demo", (DWORD)IDI_ICON1);
mainWindow.Show();
Button btn(L"Open grid demo", FALSE, 0, 0, 0, 0);
btn.SetHorizontalAligment(Stretch);
btn.SetVerticalAligment(Stretch);
mainWindow.AddComponent(&btn);
btn.SubscribeToOnClick(OpenGridExample);

/* window.MinSize.x = 500;
window.MinSize.y = 500;
window.MaxSize = window.MinSize;*/

MSG msg = {};

while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
chkbox.SetChecked(1);

return static_cast<int>(msg.wParam);
}
void InitializeGridDemoWindow()
{
gridWindow = new Window(m_hInstance);
gridWindow->SubscribeToOnClose(WindowClose);
if (!gridWindow->Create(L"MUI Grid Demo", (DWORD)IDI_ICON1)) {
return;
}
std::vector<UIComponent*> comps =
{
&button,
Expand All @@ -63,41 +103,28 @@ INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine,
chkbox.SetVerticalAligment(Top);
chkbox.SetChecked(TRUE);
window.AddComponent(&lstView);*/
grid.AddColumn(100, L"*");
grid.AddColumn(150, L"Auto");
grid.AddRow(100, L"*");
grid.AddRow(150, L"Auto");
lstView.SetHorizontalAligment(Stretch);
lstView.SetVerticalAligment(Stretch);
block.SetVerticalAligment(Center);
chkbox.SetVerticalAligment(Center);
grid.AddItem(&lstView, 1,1);
grid.AddItem(&chkbox, 1, 2);
grid.AddItem(&block, 1, 0);
window.SetGrid(&grid);
window.Show(SW_SHOW);
//lstView.Hide();
HICON ico = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
lstView.AddIcon(ico);

gridWindow->SetGrid(&grid);

lstView.Clear();
HICON ico = LoadIcon(m_hInstance, MAKEINTRESOURCE(IDI_ICON1));
BOOL suc = lstView.AddIcon(ico);
lstView.AddColumn(L"Test", 100);
lstView.AddColumn(L"Test2", 100);
ListItem itm(0, std::vector<std::wstring> {L"TEST1", L"TEST2"});
lstView.AddItem(&itm);
ListItem itm2(0, std::vector<std::wstring> {L"TEST3", L"TEST4"});
lstView.AddItem(&itm2);

/* window.MinSize.x = 500;
window.MinSize.y = 500;
window.MaxSize = window.MinSize;*/

MSG msg = {};

while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
void OpenGridExample()
{
if (!gridWindow || gridWindow->IsHidden())
{
InitializeGridDemoWindow();
gridWindow->Show();
}

return static_cast<int>(msg.wParam);
else
gridWindow->Activate();
}
void ButtonClick()
{
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion MUI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VisualStudioVersion = 17.7.34221.43
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MUI", "MUI\MUI.vcxproj", "{4372496E-37CE-4388-B06E-855EE89FCCDA}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Test", "Test\Test.vcxproj", "{E0AFC75E-90EC-4EEB-B3F7-29605A1EF545}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Demo", "Demo\Demo.vcxproj", "{E0AFC75E-90EC-4EEB-B3F7-29605A1EF545}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
114 changes: 81 additions & 33 deletions MUI/Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ namespace MUI
{
Grid::Grid()
{
this->AddColumn(0, L"Auto");
this->AddRow(0, L"Auto");
this->AddColumn(0, L"*");
this->AddRow(0, L"*");
}
std::vector<UIComponent*> Grid::GetComponents()
{
Expand All @@ -16,6 +16,12 @@ namespace MUI
}
void Grid::AddColumn(int x, const wchar_t* width)
{
if (!m_AddedCustomColumn && this->m_columns.size() > 0)
{
delete this->m_columns[0];
this->m_columns.erase(this->m_columns.begin());
m_AddedCustomColumn = TRUE;
}
GridColumn* col = new GridColumn();
col->x = x;
col->width = 0;
Expand All @@ -24,6 +30,12 @@ namespace MUI
}
void Grid::AddRow(int y, const wchar_t* height)
{
if (!m_AddedCustomRow && this->m_rows.size() > 0)
{
delete this->m_rows[0];
this->m_rows.erase(this->m_rows.begin());
m_AddedCustomRow = TRUE;
}
GridRow* row = new GridRow();
row->y = y;
row->height = 0;
Expand All @@ -34,7 +46,7 @@ namespace MUI
{
if (row == this->m_rows.size() || column == this->m_columns.size())
{
OutputDebugString(L"Row index or column index is out of range");
OutputDebugString(L"Row index or column index is out of range\n");
return;
}
GridItem* itm = new GridItem();
Expand All @@ -58,56 +70,92 @@ namespace MUI
if (GetClientRect(windowHandle, &rect))
{
std::vector<std::tuple<GridColumn*,int>> starColumns;
for (int i = 0; i < this->m_columns.size(); i++) {
std::vector<std::tuple<GridRow*, int>> starRows;
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
int freeWidth = width;
int freeHeight = height;
for (int i = 0; i < this->m_columns.size(); i++)
{
GridColumn* o_column = this->m_columns[i];

if (o_column->text_width == L"Auto")
{
o_column->width = o_column->GetBiggestWidth();
for (int j = i + 1; j < this->m_columns.size(); j++)
this->m_columns[j]->x += o_column->width - clip((this->m_columns[j]->x - o_column->x),0);
if (i + 1 < this->m_columns.size())
this->m_columns[i + 1]->x = o_column->width + o_column->x;
freeWidth -= o_column->width;
}
if (o_column->text_width == L"*") {
else if (o_column->text_width == L"*") {
starColumns.push_back(std::make_tuple(o_column, i));
o_column->width = o_column->GetBiggestWidth();
}
else
{
if (std::wstring(o_column->text_width).find_first_not_of(L"0123456789") == std::string::npos) {
size_t widthLen = wcslen(o_column->text_width) * sizeof(wchar_t);
wchar_t* s = (wchar_t*)malloc(widthLen + 1);
wcscpy_s(s, widthLen , o_column->text_width);
s[widthLen + 1] = 0;
int lenInt = _wtoi(s);
o_column->width = lenInt;
if (i + 1 < this->m_columns.size())
this->m_columns[i + 1]->x = o_column->width + o_column->x;
freeWidth -= lenInt;
}
}
}
for(std::tuple<GridColumn*,int> star : starColumns)
if (starColumns.size() > 0)
{
GridColumn* o_column = std::get<0>(star);
int othersWidth = 0;
for (int j = std::get<1>(star) + 1; j < this->m_columns.size(); j++)
othersWidth += this->m_columns[j]->width;
o_column->width = rect.right - rect.left - o_column->x - othersWidth;
for (int j = std::get<1>(star) + 1; j < this->m_columns.size(); j++)
this->m_columns[j]->x += o_column->width;
int eachStarWidth = freeWidth / (int)starColumns.size();
for (int i = 0; i < starColumns.size(); i++)
{
std::tuple<GridColumn*, int> column = starColumns[i];
GridColumn* o_column = std::get<0>(column);
o_column->width = eachStarWidth;
size_t index = std::get<1>(column);
if (index + 1 < this->m_columns.size())
this->m_columns[index + 1]->x = o_column->width + o_column->x;
}
}
std::vector<std::tuple<GridRow*, int>> starRows;
for (int i = 0; i < this->m_rows.size(); i++) {
GridRow* o_row = this->m_rows[i];
if (o_row->text_height == L"Auto")
{
o_row->height = o_row->GetBiggestHeight();
for (int j = i + 1; j < this->m_rows.size(); j++)
this->m_rows[j]->y += o_row->height - clip((this->m_rows[j]->y - o_row->y),0);
if (i + 1 < this->m_rows.size())
this->m_rows[i + 1]->y = o_row->height + o_row->y;
freeHeight -= o_row->height;
}
if (o_row->text_height == L"*") {
starRows.push_back(std::make_tuple(o_row, i));
o_row->height = o_row->GetBiggestHeight();
}
else
{
if (std::wstring(o_row->text_height).find_first_not_of(L"0123456789") == std::string::npos) {
size_t heightLen = wcslen(o_row->text_height) * sizeof(wchar_t);
wchar_t* s = (wchar_t*)malloc(heightLen + 1);
wcscpy_s(s, heightLen, o_row->text_height);
s[heightLen + 1] = 0;
int lenInt = _wtoi(s);
o_row->height = lenInt;
if (i + 1 < this->m_rows.size())
this->m_rows[i + 1]->y = o_row->height + o_row->y;
freeHeight -= lenInt;
}
}
}
for (std::tuple<GridRow*, int> star : starRows)
if (starRows.size() > 0)
{
GridRow* o_row = std::get<0>(star);
int othersHeight = 0;
for (int j = std::get<1>(star) + 1; j < this->m_rows.size(); j++)
othersHeight += this->m_rows[j]->height;
o_row->height = rect.bottom - rect.top - o_row->y - othersHeight;
for (int j = std::get<1>(star) + 1; j < this->m_rows.size(); j++)
this->m_rows[j]->y += o_row->height;
int eachStarHeight = freeHeight / (int)starRows.size();
for (int i = 0; i < starRows.size(); i++)
{
std::tuple<GridRow*, int> column = starRows[i];
GridRow* o_row = std::get<0>(column);
o_row->height = eachStarHeight;
size_t index = std::get<1>(column);
if (index + 1 < this->m_rows.size())
this->m_rows[index + 1]->y = o_row->height + o_row->y;
}
}
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
GridRow* row = this->m_rows[m_rows.size() - 1];
GridColumn* col = this->m_columns[m_columns.size() - 1];
if (row->text_height == L"*")
Expand All @@ -126,9 +174,9 @@ namespace MUI
UIComponent* comp = itm->component;
int width = max(o_column->width, 0);
int height = max(o_row->height, 0);
for (size_t i = 0; i < itm->component->columnSpan && i < this->m_columns.size(); i++)
for (size_t i = 1; i < itm->component->columnSpan && i < this->m_columns.size(); i++)
width += this->m_columns[itm->column + i]->width;
for (size_t i = 0; i < itm->component->rowSpan && i < this->m_rows.size(); i++)
for (size_t i = 1; i < itm->component->rowSpan && i < this->m_rows.size(); i++)
height += this->m_rows[itm->row + i]->height;
comp->x = o_column->x;
comp->y = o_row->y;
Expand Down
18 changes: 18 additions & 0 deletions MUI/ListView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ namespace MUI
{
ImageList_RemoveAll(this->hLarge);
ImageList_RemoveAll(this->hSmall);
this->hLarge = ImageList_Create(32,
32,
ILC_COLOR32, 3, 0);
this->hSmall = ImageList_Create(32,
32,
ILC_COLOR32, 3, 0);
}
void ListView::DeleteIconAt(int i)
{
Expand All @@ -44,6 +50,18 @@ namespace MUI
this->m_Items.erase(this->m_Items.begin(), this->m_Items.end());
this->itemIndex = 0;
}
void ListView::ClearColumns()
{
for (size_t i = 0; i < this->columnIndex; i++)
ListView_DeleteColumn(this->handle, i);
this->columnIndex = 0;
}
void ListView::Clear()
{
this->ClearColumns();
this->ClearIcons();
this->ClearItems();
}
int ListView::GetSelectedIndex()
{
return ListView_GetNextItem(this->handle, -1, LVNI_SELECTED);
Expand Down
Loading

0 comments on commit b71a816

Please sign in to comment.