Skip to content

Commit 55d8db2

Browse files
author
tthomps
committed
Begin implementing a Run window to run an arbitrary program.
Begin implementing a basic text-edit control (used by the run window). Allow controls to gain and lose focus. Allow for a blinking cursor.
1 parent 1c21160 commit 55d8db2

16 files changed

+300
-4
lines changed

MyOS_GUI_For_Windows/MyOS_GUI_For_Windows.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
<ClInclude Include="..\MyOS_GUI_Shell\GUI_Object.h" />
156156
<ClInclude Include="..\MyOS_GUI_Shell\GUI_PopupMenu.h" />
157157
<ClInclude Include="..\MyOS_GUI_Shell\GUI_Rect.h" />
158+
<ClInclude Include="..\MyOS_GUI_Shell\GUI_RunWindow.h" />
158159
<ClInclude Include="..\MyOS_GUI_Shell\GUI_Taskbar.h" />
159160
<ClInclude Include="..\MyOS_GUI_Shell\GUI_Window.h" />
160161
<ClInclude Include="..\MyOS_GUI_Shell\MyOS_GUI_Shell.h" />
@@ -165,10 +166,12 @@
165166
<ClCompile Include="..\MyOS_GUI_Shell\font.c" />
166167
<ClCompile Include="..\MyOS_GUI_Shell\GUI_Button.cpp" />
167168
<ClCompile Include="..\MyOS_GUI_Shell\GUI_Control.cpp" />
169+
<ClCompile Include="..\MyOS_GUI_Shell\GUI_EditControl.cpp" />
168170
<ClCompile Include="..\MyOS_GUI_Shell\GUI_Kernel_Shell.cpp" />
169171
<ClCompile Include="..\MyOS_GUI_Shell\GUI_MessageBox.cpp" />
170172
<ClCompile Include="..\MyOS_GUI_Shell\GUI_Object.cpp" />
171173
<ClCompile Include="..\MyOS_GUI_Shell\GUI_PopupMenu.cpp" />
174+
<ClCompile Include="..\MyOS_GUI_Shell\GUI_RunWindow.cpp" />
172175
<ClCompile Include="..\MyOS_GUI_Shell\GUI_Taskbar.cpp" />
173176
<ClCompile Include="..\MyOS_GUI_Shell\GUI_TerminalWindow.cpp" />
174177
<ClCompile Include="..\MyOS_GUI_Shell\GUI_Window.cpp" />

MyOS_GUI_For_Windows/MyOS_GUI_For_Windows.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
<ClInclude Include="..\MyOS_GUI_Shell\GUI_PopupMenu.h">
4646
<Filter>Header Files</Filter>
4747
</ClInclude>
48+
<ClInclude Include="..\MyOS_GUI_Shell\GUI_RunWindow.h">
49+
<Filter>Header Files</Filter>
50+
</ClInclude>
4851
</ItemGroup>
4952
<ItemGroup>
5053
<ClCompile Include="..\MyOS_GUI_Shell\MyOS_GUI_Shell.cpp">
@@ -86,5 +89,11 @@
8689
<ClCompile Include="..\MyOS_GUI_Shell\GUI_PopupMenu.cpp">
8790
<Filter>Source Files</Filter>
8891
</ClCompile>
92+
<ClCompile Include="..\MyOS_GUI_Shell\GUI_RunWindow.cpp">
93+
<Filter>Source Files</Filter>
94+
</ClCompile>
95+
<ClCompile Include="..\MyOS_GUI_Shell\GUI_EditControl.cpp">
96+
<Filter>Source Files</Filter>
97+
</ClCompile>
8998
</ItemGroup>
9099
</Project>

MyOS_GUI_Shell/GUI_Control.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ class GUI_Control : public GUI_Object
1313
this->controlID = controlID;
1414
}
1515

16-
virtual void OnHover(int relX, int relY) {};
16+
virtual void LoseFocus() {}
17+
virtual void GainFocus() {}
18+
virtual void OnHover(int relX, int relY) {}
1719
virtual void OnClick(int relX, int relY);
18-
virtual void OnDrag(int relClickX, int relClickY, int relDestX, int relDestY) {};
20+
virtual void OnDrag(int relClickX, int relClickY, int relDestX, int relDestY) {}
1921
virtual void Resize(GUI_Rect newDimensions) { dimensions = newDimensions; }
22+
virtual void UpdateCursor() {}
2023

2124
uint32_t controlID;
2225
protected:

MyOS_GUI_Shell/GUI_EditControl.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "GUI_EditControl.h"
2+
3+
GUI_EditControl::GUI_EditControl(GUI_Rect guiRect, GUI_Window *pOwner, uint32_t controlID)
4+
: GUI_Control(pOwner, controlID)
5+
{
6+
pSurface = NULL;
7+
dimensions = guiRect;
8+
CreateSurface();
9+
cursorX = 2;
10+
}
11+
12+
13+
GUI_EditControl::~GUI_EditControl()
14+
{
15+
SDL_FreeSurface(pSurface);
16+
}
17+
18+
void GUI_EditControl::CreateSurface()
19+
{
20+
if (pSurface)
21+
SDL_FreeSurface(pSurface);
22+
23+
pSurface = SDL_CreateRGBSurface(0,
24+
dimensions.width,
25+
dimensions.height,
26+
32,
27+
0xFF000000,
28+
0x00FF0000,
29+
0x0000FF00,
30+
0x000000FF);
31+
32+
FillSurface(pSurface, SDL_WHITE);
33+
34+
Draw3D_InsetBox(pSurface, 0, 0, dimensions.width - 1, dimensions.height - 1);
35+
}
36+
37+
void GUI_EditControl::LoseFocus()
38+
{
39+
if(cursorBlinkOn)
40+
DrawVerticalLine(pSurface, cursorX, 1, dimensions.height - 2, SDL_WHITE);
41+
42+
cursorBlinkOn = false;
43+
}
44+
45+
void GUI_EditControl::OnClick(int relX, int relY)
46+
{
47+
pOwner->SetFocus(controlID);
48+
}
49+
50+
void GUI_EditControl::PaintToSurface(SDL_Surface * pTargetSurface)
51+
{
52+
SDL_BlitSurface(pSurface, NULL, pTargetSurface, dimensions.GetSDL_Rect());
53+
}
54+
55+
void GUI_EditControl::UpdateCursor()
56+
{
57+
cursorBlinkOn = !cursorBlinkOn;
58+
59+
if (cursorBlinkOn)
60+
{
61+
DrawVerticalLine(pSurface, cursorX, 1, dimensions.height - 2, SDL_BLACK);
62+
}
63+
else
64+
{
65+
DrawVerticalLine(pSurface, cursorX, 1, dimensions.height - 2, SDL_WHITE);
66+
}
67+
}

MyOS_GUI_Shell/GUI_EditControl.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
#include "GUI_Control.h"
3+
4+
class GUI_EditControl :
5+
public GUI_Control
6+
{
7+
public:
8+
GUI_EditControl(GUI_Rect guiRect, GUI_Window *pOwner, uint32_t controlID);
9+
~GUI_EditControl();
10+
11+
void CreateSurface();
12+
void LoseFocus();
13+
void OnClick(int relX, int relY);
14+
void PaintToSurface(SDL_Surface *pTargetSurface);
15+
void UpdateCursor();
16+
17+
SDL_Surface *pSurface;
18+
19+
bool cursorBlinkOn;
20+
int cursorX;
21+
};
22+

MyOS_GUI_Shell/GUI_PopupMenu.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ bool GUI_PopupMenu::MouseOver(int relX, int relY)
123123
return true;
124124
}
125125

126+
void GUI_PopupMenu::OnClick(int relX, int relY)
127+
{
128+
if (relX < 0 || relY < 0 || relX > dimensions.width || relY > dimensions.height)
129+
{
130+
MessageBox("Menu called with mouse out of position\n", "ERROR");
131+
return;
132+
}
133+
134+
// Get the selection
135+
int heightPerOption = dimensions.height / choices;
136+
int selection = relY / heightPerOption;
137+
138+
// Call the callback with the ID for this selection
139+
(*handlerCallback)(choiceIDs[selection]);
140+
141+
shown = false;
142+
}
143+
126144
void GUI_PopupMenu::PaintToSurface(SDL_Surface * pTargetSurface)
127145
{
128146
if (!shown)

MyOS_GUI_Shell/GUI_PopupMenu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class GUI_PopupMenu :
2626
void CreateSurface();
2727
void Draw(int selectedChoice);
2828
bool MouseOver(int relX, int relY);
29+
void OnClick(int relX, int relY);
2930
void PaintToSurface(SDL_Surface *pTargetSurface);
3031
void RegisterMenuHandler(MENU_HANDLER handler);
3132
void ShowMenu(SDL_Point origin);

MyOS_GUI_Shell/GUI_RunWindow.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "GUI_RunWindow.h"
2+
#include "MyOS_GUI_Shell.h"
3+
#include "GUI_Button.h"
4+
#include "GUI_EditControl.h"
5+
6+
// TODO: place window relative to bottom-left
7+
GUI_RunWindow::GUI_RunWindow() : GUI_Window(0, 0, RUN_WINDOW_WIDTH, RUN_WINDOW_HEIGHT, "Run" )
8+
{
9+
// Create a button control for an "OK" button
10+
pControls[0] = new GUI_Button("OK", SYSTEM_MENU_CLOSE_BUTTON_ID, this);
11+
12+
// Move button to bottom-right corner
13+
pControls[0]->dimensions.left = dimensions.width - pControls[0]->dimensions.width - 2;
14+
pControls[0]->dimensions.top = dimensions.height - pControls[0]->dimensions.height - 2;
15+
16+
pControls[1] = new GUI_EditControl(RunWindowEditBoxDimensions, this, 1);
17+
18+
AddNewWindow(this);
19+
}
20+
21+
22+
GUI_RunWindow::~GUI_RunWindow()
23+
{
24+
25+
}

MyOS_GUI_Shell/GUI_RunWindow.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
#include "GUI_Window.h"
3+
4+
5+
#define RUN_WINDOW_Y_FROM_BOTTOM 200
6+
#define RUN_WINDOW_WIDTH 300
7+
#define RUN_WINDOW_HEIGHT 75
8+
9+
const int editHeight = FNT_FONTHEIGHT + (FNT_TOPBOTTOMMARGIN * 2);
10+
const GUI_Rect RunWindowEditBoxDimensions = { RUN_WINDOW_HEIGHT - 10 - editHeight, 10, RUN_WINDOW_WIDTH - 50, editHeight };
11+
12+
class GUI_RunWindow :
13+
public GUI_Window
14+
{
15+
public:
16+
GUI_RunWindow();
17+
~GUI_RunWindow();
18+
};
19+

MyOS_GUI_Shell/GUI_Taskbar.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,23 @@
55
#endif
66
#include "GUI_Taskbar.h"
77
#include "GUI_Button.h"
8+
#include "GUI_RunWindow.h"
89

910
void StartMenuHandler(int choice)
10-
{}
11+
{
12+
switch (choice)
13+
{
14+
case 0:
15+
new GUI_RunWindow();
16+
break;
17+
case 1:
18+
MessageBox("Option 1 clicked", "Click");
19+
break;
20+
case 2:
21+
MessageBox("Option 2 clicked", "Click");
22+
break;
23+
}
24+
}
1125

1226
GUI_Taskbar::GUI_Taskbar(uint32_t desktopWidth, uint32_t desktopHeight) :
1327
GUI_Window(desktopHeight - GUI_TASKBAR_HEIGHT, 0, desktopWidth, GUI_TASKBAR_HEIGHT, "Taskbar")

MyOS_GUI_Shell/GUI_Window.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@ void GUI_Window::CreateSurface()
3232
DrawWindow();
3333
}
3434

35+
void GUI_Window::SetFocus(int controlID)
36+
{
37+
// Ignore if this control alread has the focus
38+
if (focusedControlIndex >= 0 && pControls[focusedControlIndex]->controlID == controlID)
39+
return;
40+
41+
// Unfocus the previously focused control
42+
if (focusedControlIndex >= 0)
43+
pControls[focusedControlIndex]->LoseFocus();
44+
45+
// Find the control with the given ID and focus it
46+
for (int i = 0; i < MAX_WINDOW_CONTROLS; ++i)
47+
{
48+
if (pControls[i] && pControls[i]->controlID == controlID)
49+
{
50+
focusedControlIndex = i;
51+
return;
52+
}
53+
}
54+
55+
MessageBox("Couldn't find a matching control for SetFocus", "ERROR");
56+
}
57+
3558
void GUI_Window::PaintToSurface(SDL_Surface *pTargetSurface)
3659
{
3760
// TEMP
@@ -44,6 +67,14 @@ void GUI_Window::PaintToSurface(SDL_Surface *pTargetSurface)
4467
SDL_BlitSurface(pSurface, NULL, pTargetSurface, dimensions.GetSDL_Rect());
4568
}
4669

70+
void GUI_Window::UpdateCursor()
71+
{
72+
if (focusedControlIndex < 0)
73+
return;
74+
75+
pControls[focusedControlIndex]->UpdateCursor();
76+
}
77+
4778
void GUI_Window::ControlClicked(uint32_t controlID)
4879
{
4980
if (controlID == SYSTEM_MENU_CLOSE_BUTTON_ID)
@@ -62,6 +93,14 @@ void GUI_Window::FillSurface(SDL_Color color)
6293
SDL_FillRect(pSurface, NULL, col);
6394
}
6495

96+
void GUI_Window::LoseFocus()
97+
{
98+
if (focusedControlIndex < 0)
99+
return;
100+
101+
pControls[focusedControlIndex]->LoseFocus();
102+
}
103+
65104
void GUI_Window::OnClick(int relX, int relY)
66105
{
67106
if (relX < 0 || relY < 0 || relX > dimensions.width || relY > dimensions.height)

MyOS_GUI_Shell/GUI_Window.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class GUI_Window : public GUI_Object
2222
pControls[i] = NULL;
2323

2424
pClickedControl = NULL;
25+
focusedControlIndex = -1;
2526
}
2627

2728
GUI_Window(int top, int left, int width, int height, const char *name)
@@ -38,6 +39,7 @@ class GUI_Window : public GUI_Object
3839
pControls[i] = NULL;
3940

4041
pClickedControl = NULL;
42+
focusedControlIndex = -1;
4143
}
4244

4345
virtual void ControlClicked(uint32_t controlID);
@@ -46,6 +48,8 @@ class GUI_Window : public GUI_Object
4648

4749
void FillSurface(SDL_Color color);
4850

51+
virtual void LoseFocus();
52+
4953
void OnClick(int relX, int relY);
5054

5155
void OnDrag(int startRelX, int startRelY, int relX, int relY);
@@ -61,14 +65,21 @@ class GUI_Window : public GUI_Object
6165
(void)text; // text sent to GUI_Window base class is ignored
6266
}
6367

68+
void SetFocus(int controlID);
69+
6470
SDL_Surface *GetSurface() { return pSurface; }
71+
6572
void PaintToSurface(SDL_Surface *pTargetSurface);
6673

74+
void UpdateCursor();
75+
76+
6777
SDL_Surface *pSurface;
6878
GUI_Control *pClickedControl;
6979
char windowName[MAX_WINDOW_NAME_LENGTH];
7080
protected:
7181
void GUI_Window::CreateSurface();
7282
SDL_Color backgroundColor;
7383
GUI_Control *pControls[MAX_WINDOW_CONTROLS];
84+
int focusedControlIndex;
7485
};

0 commit comments

Comments
 (0)