Skip to content

Commit 041bbef

Browse files
committed
Create a control for the system menu and move system-menu-drawing code into that control.
Add a window style member to GUI_Window indicating if the window should have a system menu or not (the taskbar is a GUI_Window that doesn't have one).
1 parent 6905454 commit 041bbef

16 files changed

+188
-106
lines changed

MyOS_GUI_For_Windows/MyOS_GUI_For_Windows.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
<ClInclude Include="..\MyOS_GUI_Shell\GUI_PopupMenu.h" />
157157
<ClInclude Include="..\MyOS_GUI_Shell\GUI_Rect.h" />
158158
<ClInclude Include="..\MyOS_GUI_Shell\GUI_RunWindow.h" />
159+
<ClInclude Include="..\MyOS_GUI_Shell\GUI_SystemMenuControl.h" />
159160
<ClInclude Include="..\MyOS_GUI_Shell\GUI_Taskbar.h" />
160161
<ClInclude Include="..\MyOS_GUI_Shell\GUI_Window.h" />
161162
<ClInclude Include="..\MyOS_GUI_Shell\MyOS_GUI_Shell.h" />
@@ -172,6 +173,7 @@
172173
<ClCompile Include="..\MyOS_GUI_Shell\GUI_Object.cpp" />
173174
<ClCompile Include="..\MyOS_GUI_Shell\GUI_PopupMenu.cpp" />
174175
<ClCompile Include="..\MyOS_GUI_Shell\GUI_RunWindow.cpp" />
176+
<ClCompile Include="..\MyOS_GUI_Shell\GUI_SystemMenuControl.cpp" />
175177
<ClCompile Include="..\MyOS_GUI_Shell\GUI_Taskbar.cpp" />
176178
<ClCompile Include="..\MyOS_GUI_Shell\GUI_TerminalWindow.cpp" />
177179
<ClCompile Include="..\MyOS_GUI_Shell\GUI_Window.cpp" />

MyOS_GUI_For_Windows/MyOS_GUI_For_Windows.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
<ClInclude Include="..\MyOS_GUI_Shell\GUI_RunWindow.h">
4949
<Filter>Header Files</Filter>
5050
</ClInclude>
51+
<ClInclude Include="..\MyOS_GUI_Shell\GUI_SystemMenuControl.h">
52+
<Filter>Header Files</Filter>
53+
</ClInclude>
5154
</ItemGroup>
5255
<ItemGroup>
5356
<ClCompile Include="..\MyOS_GUI_Shell\MyOS_GUI_Shell.cpp">
@@ -95,5 +98,8 @@
9598
<ClCompile Include="..\MyOS_GUI_Shell\GUI_EditControl.cpp">
9699
<Filter>Source Files</Filter>
97100
</ClCompile>
101+
<ClCompile Include="..\MyOS_GUI_Shell\GUI_SystemMenuControl.cpp">
102+
<Filter>Source Files</Filter>
103+
</ClCompile>
98104
</ItemGroup>
99105
</Project>

MyOS_GUI_Shell/GUI_MessageBox.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "MyOS_GUI_Shell.h"
55

66
GUI_MessageBox::GUI_MessageBox(char * messageText, char * windowTitle)
7-
: GUI_Window( NewWindowPosition(MESSAGE_BOX_WIDTH, MESSAGE_BOX_HEIGHT), windowTitle)
7+
: GUI_Window( NewWindowPosition(MESSAGE_BOX_WIDTH, MESSAGE_BOX_HEIGHT), windowTitle, WINDOW_STYLE_NORMAL)
88
{
99
// TODO: Use static memory so we can display out of memory messages
1010
SDL_Surface *pFont = FNT_Render(messageText, SDL_BLACK);

MyOS_GUI_Shell/GUI_Object.cpp

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -82,74 +82,6 @@ void GUI_Object::FillSurface(SDL_Surface * pSurface, SDL_Color color)
8282
SDL_FillRect(pSurface, NULL, col);
8383
}
8484

85-
void GUI_Object::DrawSystemMenu(SDL_Surface *pSurface, char *windowName)
86-
{
87-
// Fill in a bar at the top
88-
SDL_Rect sysRect;
89-
sysRect.x = sysRect.y = 0;
90-
sysRect.w = dimensions.width;
91-
sysRect.h = SYSTEM_MENU_HEIGHT;
92-
93-
SDL_Color sysColor;
94-
sysColor.r = SYSTEM_MENU_COLOR_R;
95-
sysColor.g = SYSTEM_MENU_COLOR_G;
96-
sysColor.b = SYSTEM_MENU_COLOR_B;
97-
uint32_t color = SDL_MapRGB(pSurface->format, sysColor.r, sysColor.g, sysColor.b);
98-
SDL_FillRect(pSurface, &sysRect, color);
99-
100-
// Draw the title of the window
101-
SDL_Color black;
102-
black.r = black.g = black.b = 0;
103-
SDL_Surface *pFont = FNT_Render(windowName, black);
104-
SDL_Rect destRect = { 8, 8, 8, 32 };
105-
SDL_BlitSurface(pFont, NULL, pSurface, &destRect);
106-
SDL_FreeSurface(pFont);
107-
108-
// Draw the system boxes
109-
int margin = 4;
110-
int boxHeight = SYSTEM_MENU_HEIGHT - margin + 1;
111-
int boxWidth = boxHeight;
112-
int boxTop = (margin / 2);
113-
int boxLeft = dimensions.width - boxWidth - margin + 2;
114-
SDL_Rect boxRect = { boxLeft, boxTop, boxWidth, boxHeight };
115-
uint32_t red = SDL_MapRGB(pSurface->format, 255, 120, 120 );
116-
SDL_FillRect(pSurface, &boxRect, red);
117-
Draw3D_Box(pSurface, boxLeft, boxTop, boxWidth, boxHeight);
118-
119-
pFont = FNT_Render("X", black);
120-
boxRect.x += 7;
121-
boxRect.y += 7;
122-
SDL_BlitSurface(pFont, NULL, pSurface, &boxRect);
123-
SDL_FreeSurface(pFont);
124-
125-
126-
boxLeft = boxLeft - boxWidth - 2;
127-
boxRect = { boxLeft, boxTop, boxWidth, boxHeight };
128-
SDL_FillRect(pSurface, &boxRect, SDL_MapRGB(pSurface->format, 205, 205, 205));
129-
Draw3D_Box(pSurface, boxLeft, boxTop, boxWidth, boxHeight);
130-
131-
pFont = FNT_Render("[", black);
132-
boxRect.x += 4;
133-
boxRect.y += 7;
134-
SDL_BlitSurface(pFont, NULL, pSurface, &boxRect);
135-
boxRect.x += 4;
136-
SDL_FreeSurface(pFont);
137-
pFont = FNT_Render("]", black);
138-
SDL_BlitSurface(pFont, NULL, pSurface, &boxRect);
139-
SDL_FreeSurface(pFont);
140-
141-
boxLeft = boxLeft - boxWidth - 2;
142-
boxRect = { boxLeft, boxTop, boxWidth, boxHeight };
143-
SDL_FillRect(pSurface, &boxRect, SDL_MapRGB(pSurface->format, 205, 205, 205));
144-
Draw3D_Box(pSurface, boxLeft, boxTop, boxWidth, boxHeight);
145-
146-
pFont = FNT_Render("_", black);
147-
boxRect.x += 7;
148-
boxRect.y += 7;
149-
SDL_BlitSurface(pFont, NULL, pSurface, &boxRect);
150-
SDL_FreeSurface(pFont);
151-
}
152-
15385
#ifdef __cplusplus
15486
};
15587
#endif /* __cplusplus */

MyOS_GUI_Shell/GUI_Object.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ extern "C" {
1717
#define SYSTEM_MENU_COLOR_G 160
1818
#define SYSTEM_MENU_COLOR_B 255
1919

20+
#define SDL_R_MASK 0xFF000000
21+
#define SDL_G_MASK 0x00FF0000
22+
#define SDL_B_MASK 0x0000FF00
23+
#define SDL_A_MASK 0x000000FF
24+
2025
const SDL_Color SDL_BLACK = { 0, 0, 0, 255 };
2126
const SDL_Color SDL_DARKGRAY = { 40, 40, 40, 255 };
2227
const uint32_t RGB_BLACK = 0x000000;
@@ -41,7 +46,6 @@ class GUI_Object
4146
GUI_Rect dimensions;
4247

4348
protected:
44-
void DrawSystemMenu(SDL_Surface *pSurface, char *windowName);
4549
void DrawVerticalLine(SDL_Surface *pSurface, int x, int startY, int endY, SDL_Color lineColor);
4650
void DrawHorizontalLine(SDL_Surface *pSurface, int startX, int endX, int y, SDL_Color lineColor);
4751
void Draw3D_Box(SDL_Surface *pSurface, int x, int y, int width, int height);

MyOS_GUI_Shell/GUI_RunWindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "GUI_EditControl.h"
55

66
// TODO: place window relative to bottom-left
7-
GUI_RunWindow::GUI_RunWindow() : GUI_Window(0, 0, RUN_WINDOW_WIDTH, RUN_WINDOW_HEIGHT, "Run" )
7+
GUI_RunWindow::GUI_RunWindow() : GUI_Window(0, 0, RUN_WINDOW_WIDTH, RUN_WINDOW_HEIGHT, "Run", WINDOW_STYLE_NORMAL)
88
{
99
// Create a button control for an "OK" button
1010
pControls[0] = new GUI_Button("OK", BUTTON_ID_OK, this);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include "GUI_SystemMenuControl.h"
2+
3+
4+
GUI_SystemMenuControl::GUI_SystemMenuControl(GUI_Window *pOwner) : GUI_Control(pOwner, SYSTEM_MENU_CONTROL_ID)
5+
{
6+
dimensions.top = 1;
7+
dimensions.left = 1;
8+
dimensions.width = pOwner->dimensions.width - 2;
9+
dimensions.height = SYSTEM_MENU_HEIGHT;
10+
11+
pSurface = NULL;
12+
CreateSurface();
13+
}
14+
15+
GUI_SystemMenuControl::~GUI_SystemMenuControl()
16+
{
17+
SDL_FreeSurface(pSurface);
18+
}
19+
20+
void GUI_SystemMenuControl::CreateSurface()
21+
{
22+
if (pSurface)
23+
SDL_FreeSurface(pSurface);
24+
25+
26+
pSurface = SDL_CreateRGBSurface(0,
27+
dimensions.width,
28+
dimensions.height,
29+
32,
30+
SDL_R_MASK,
31+
SDL_G_MASK,
32+
SDL_B_MASK,
33+
SDL_A_MASK);
34+
35+
if (!pSurface)
36+
{
37+
//printf("Error! Couldn't allocate surface for system menu\n");
38+
return;
39+
}
40+
41+
// Fill the surface with the menu color
42+
FillSurface(pSurface, SDL_SYS_MENU_COLOR);
43+
44+
// Draw the title of the window
45+
SDL_Surface *pFont = FNT_Render(pOwner->windowName, SDL_BLACK);
46+
SDL_Rect destRect = { 8, 8, 8, 32 };
47+
SDL_BlitSurface(pFont, NULL, pSurface, &destRect);
48+
SDL_FreeSurface(pFont);
49+
50+
// Draw the system boxes
51+
int margin = 4;
52+
int boxHeight = SYSTEM_MENU_HEIGHT - margin + 1;
53+
int boxWidth = boxHeight;
54+
int boxTop = (margin / 2);
55+
int boxLeft = dimensions.width - boxWidth - margin + 2;
56+
SDL_Rect boxRect = { boxLeft, boxTop, boxWidth, boxHeight };
57+
uint32_t red = SDL_MapRGB(pSurface->format, 255, 120, 120);
58+
SDL_FillRect(pSurface, &boxRect, red);
59+
Draw3D_Box(pSurface, boxLeft, boxTop, boxWidth, boxHeight);
60+
61+
pFont = FNT_Render("X", SDL_BLACK);
62+
boxRect.x += 7;
63+
boxRect.y += 7;
64+
SDL_BlitSurface(pFont, NULL, pSurface, &boxRect);
65+
SDL_FreeSurface(pFont);
66+
67+
boxLeft = boxLeft - boxWidth - 2;
68+
boxRect = { boxLeft, boxTop, boxWidth, boxHeight };
69+
SDL_FillRect(pSurface, &boxRect, SDL_MapRGB(pSurface->format, 205, 205, 205));
70+
Draw3D_Box(pSurface, boxLeft, boxTop, boxWidth, boxHeight);
71+
72+
pFont = FNT_Render("[", SDL_BLACK);
73+
boxRect.x += 4;
74+
boxRect.y += 7;
75+
SDL_BlitSurface(pFont, NULL, pSurface, &boxRect);
76+
boxRect.x += 4;
77+
SDL_FreeSurface(pFont);
78+
pFont = FNT_Render("]", SDL_BLACK);
79+
SDL_BlitSurface(pFont, NULL, pSurface, &boxRect);
80+
SDL_FreeSurface(pFont);
81+
82+
boxLeft = boxLeft - boxWidth - 2;
83+
boxRect = { boxLeft, boxTop, boxWidth, boxHeight };
84+
SDL_FillRect(pSurface, &boxRect, SDL_MapRGB(pSurface->format, 205, 205, 205));
85+
Draw3D_Box(pSurface, boxLeft, boxTop, boxWidth, boxHeight);
86+
87+
pFont = FNT_Render("_", SDL_BLACK);
88+
boxRect.x += 7;
89+
boxRect.y += 7;
90+
SDL_BlitSurface(pFont, NULL, pSurface, &boxRect);
91+
SDL_FreeSurface(pFont);
92+
}
93+
94+
void GUI_SystemMenuControl::PaintToSurface(SDL_Surface * pTargetSurface)
95+
{
96+
SDL_BlitSurface(pSurface, NULL, pTargetSurface, dimensions.GetSDL_Rect());
97+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
#include "GUI_Control.h"
3+
4+
class GUI_SystemMenuControl :
5+
public GUI_Control
6+
{
7+
public:
8+
GUI_SystemMenuControl(GUI_Window *pOwner);
9+
~GUI_SystemMenuControl();
10+
11+
void CreateSurface();
12+
void PaintToSurface(SDL_Surface *pTargetSurface);
13+
14+
SDL_Surface *pSurface;
15+
};
16+

MyOS_GUI_Shell/GUI_Taskbar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void StartMenuHandler(int choice)
2525
}
2626

2727
GUI_Taskbar::GUI_Taskbar(uint32_t desktopWidth, uint32_t desktopHeight) :
28-
GUI_Window(desktopHeight - GUI_TASKBAR_HEIGHT, 0, desktopWidth, GUI_TASKBAR_HEIGHT, "Taskbar")
28+
GUI_Window(desktopHeight - GUI_TASKBAR_HEIGHT, 0, desktopWidth, GUI_TASKBAR_HEIGHT, "Taskbar", WINDOW_STYLE_NO_SYSTEM_MENU)
2929
{
3030
// taskbar has no system menu or "standard window controls
3131

MyOS_GUI_Shell/GUI_TerminalWindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern "C" {
1111
#include <string.h>
1212

1313
GUI_TerminalWindow::GUI_TerminalWindow(const char *name)
14-
: GUI_Window(NewWindowPosition(GUI_TERMINAL_DEFAULT_WIDTH, GUI_TERMINAL_DEFAULT_HEIGHT), name)
14+
: GUI_Window(NewWindowPosition(GUI_TERMINAL_DEFAULT_WIDTH, GUI_TERMINAL_DEFAULT_HEIGHT), name, WINDOW_STYLE_NORMAL)
1515
{
1616
// TODO: Check for out of memory error
1717
characterBuffer = new char(characterBufferSize);

MyOS_GUI_Shell/GUI_Window.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extern "C" {
44

55
#include "GUI_Window.h"
66
#include "MyOS_GUI_Shell.h"
7+
#include "GUI_SystemMenuControl.h"
78

89
#ifdef __MYOS
910
#include "../MyOS_1/Interrupts/System_Calls.h"
@@ -12,6 +13,43 @@ extern "C" {
1213
#endif
1314

1415

16+
GUI_Window::GUI_Window(GUI_Rect size, const char *name, int style) : GUI_Window(size.top, size.left, size.width, size.height, name, style)
17+
{
18+
}
19+
20+
GUI_Window::GUI_Window(int top, int left, int width, int height, const char *name, int windowStyle)
21+
{
22+
dimensions.top = top;
23+
dimensions.left = left;
24+
dimensions.width = width;
25+
dimensions.height = height;
26+
27+
SDL_strlcpy(windowName, name, MAX_WINDOW_NAME_LENGTH);
28+
CreateSurface();
29+
30+
style = windowStyle;
31+
32+
for (int i = 0; i < MAX_WINDOW_CONTROLS; ++i)
33+
pControls[i] = NULL;
34+
35+
// Should we create a system menu control?
36+
if (!(style & WINDOW_STYLE_NO_SYSTEM_MENU))
37+
{
38+
// create system menu
39+
pControls[MAX_WINDOW_CONTROLS - 1] = new GUI_SystemMenuControl(this);
40+
}
41+
42+
pClickedControl = NULL;
43+
focusedControlIndex = -1;
44+
enterClicksButtonID = -1;
45+
}
46+
47+
GUI_Window::~GUI_Window()
48+
{
49+
if (!pSurface)
50+
SDL_FreeSurface(pSurface);
51+
}
52+
1553
void GUI_Window::CreateSurface()
1654
{
1755
pSurface = SDL_CreateRGBSurface(0, // flags (unused)
@@ -169,7 +207,7 @@ void GUI_Window::DrawWindow()
169207
FillSurface(backgroundColor);
170208

171209
// Draw the system menu at the top
172-
DrawSystemMenu(pSurface, windowName);
210+
//DrawSystemMenu(pSurface, windowName);
173211

174212
// Draw a black outline around the background
175213
Draw3D_Box(pSurface, 0, 0, dimensions.width, dimensions.height);

0 commit comments

Comments
 (0)