2023-01-25 18:53:15 +00:00
|
|
|
//========================================================================
|
|
|
|
// Theming test program
|
|
|
|
// Copyright (c) Camilla Löwy <elmindreda@glfw.org>
|
|
|
|
//
|
|
|
|
// This software is provided 'as-is', without any express or implied
|
|
|
|
// warranty. In no event will the authors be held liable for any damages
|
|
|
|
// arising from the use of this software.
|
|
|
|
//
|
|
|
|
// Permission is granted to anyone to use this software for any purpose,
|
|
|
|
// including commercial applications, and to alter it and redistribute it
|
|
|
|
// freely, subject to the following restrictions:
|
|
|
|
//
|
|
|
|
// 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
// claim that you wrote the original software. If you use this software
|
|
|
|
// in a product, an acknowledgment in the product documentation would
|
|
|
|
// be appreciated but is not required.
|
|
|
|
//
|
|
|
|
// 2. Altered source versions must be plainly marked as such, and must not
|
|
|
|
// be misrepresented as being the original software.
|
|
|
|
//
|
|
|
|
// 3. This notice may not be removed or altered from any source
|
|
|
|
// distribution.
|
|
|
|
//
|
|
|
|
//========================================================================
|
|
|
|
//
|
|
|
|
// This program is used to test the theming features.
|
|
|
|
//
|
|
|
|
//========================================================================
|
|
|
|
|
|
|
|
#define GLAD_GL_IMPLEMENTATION
|
|
|
|
#include <glad/gl.h>
|
|
|
|
#define GLFW_INCLUDE_NONE
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-01-29 20:06:42 +00:00
|
|
|
const float theme_colors[6][4] =
|
2023-01-25 18:53:15 +00:00
|
|
|
{
|
2023-01-29 20:06:42 +00:00
|
|
|
{ 0, 0, 0, 1.0 }, // black
|
|
|
|
{ 1.0, 0, 0, 1.0 }, // red
|
|
|
|
{ 0, 1.0, 0, 1.0 }, // green
|
|
|
|
{ 0, 0, 1.0, 1.0 }, // blue
|
|
|
|
{ 1.0, 1.0, 1.0, 1.0 }, // white
|
|
|
|
{ 0.5, 0.5, 0.5, 1.0 } // gray (no theme color)
|
2023-01-25 18:53:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int cur_theme_color = 0;
|
|
|
|
|
2023-01-29 20:06:42 +00:00
|
|
|
static GLFWtheme* theme;
|
2023-01-25 18:53:15 +00:00
|
|
|
|
2023-02-23 21:48:21 +00:00
|
|
|
static void print_theme(const GLFWtheme* theme, const char* title)
|
2023-01-25 23:46:37 +00:00
|
|
|
{
|
|
|
|
int n = 0;
|
2023-02-23 21:48:21 +00:00
|
|
|
int variation = glfwThemeGetVariation(theme);
|
2023-01-25 23:46:37 +00:00
|
|
|
|
|
|
|
printf("%s: {\n", title);
|
2023-02-23 21:48:21 +00:00
|
|
|
printf(" Variation: %s\n", variation == GLFW_THEME_DEFAULT ? "default" : variation == GLFW_THEME_LIGHT ? "light" : "dark");
|
2023-02-01 03:44:22 +00:00
|
|
|
printf(" Attributes: [");
|
2023-02-23 21:48:21 +00:00
|
|
|
|
2023-02-01 03:44:22 +00:00
|
|
|
if (glfwThemeGetAttribute(theme, GLFW_THEME_COLOR_MAIN))
|
|
|
|
printf(n++ > 0 ? ", %s" : "%s", "COLOR_MAIN");
|
2023-02-23 21:48:21 +00:00
|
|
|
|
2023-01-30 00:12:12 +00:00
|
|
|
if (glfwThemeGetAttribute(theme, GLFW_THEME_ATTRIBUTE_HIGH_CONTRAST))
|
2023-01-25 23:46:37 +00:00
|
|
|
printf(n++ > 0 ? ", %s" : "%s", "HIGH_CONTRAST");
|
2023-02-23 21:48:21 +00:00
|
|
|
|
|
|
|
if (glfwThemeGetAttribute(theme, GLFW_THEME_ATTRIBUTE_REDUCE_TRANSPARENCY))
|
|
|
|
printf(n++ > 0 ? ", %s" : "%s", "REDUCE_TRANSPARENCY");
|
|
|
|
|
|
|
|
if (glfwThemeGetAttribute(theme, GLFW_THEME_ATTRIBUTE_REDUCE_MOTION))
|
|
|
|
printf(n++ > 0 ? ", %s" : "%s", "REDUCE_MOTION");
|
|
|
|
|
2023-01-25 23:46:37 +00:00
|
|
|
printf("]\n");
|
2023-02-23 21:48:21 +00:00
|
|
|
|
2023-02-01 03:44:22 +00:00
|
|
|
if (glfwThemeGetAttribute(theme, GLFW_THEME_COLOR_MAIN))
|
2023-01-25 23:46:37 +00:00
|
|
|
{
|
2023-01-29 20:06:42 +00:00
|
|
|
float r, g, b, a;
|
2023-02-01 03:44:22 +00:00
|
|
|
glfwThemeGetColor(theme, GLFW_THEME_COLOR_MAIN, &r, &g, &b, &a);
|
|
|
|
printf(" Main color: [%f, %f, %f, %f]\n", r, g, b, a);
|
2023-01-25 23:46:37 +00:00
|
|
|
}
|
|
|
|
printf("}\n");
|
|
|
|
}
|
|
|
|
|
2023-01-25 18:53:15 +00:00
|
|
|
static void set_theme(GLFWwindow* window, int theme_color)
|
|
|
|
{
|
2023-01-29 20:06:42 +00:00
|
|
|
glfwThemeSetColor(
|
|
|
|
theme,
|
2023-02-01 03:44:22 +00:00
|
|
|
GLFW_THEME_COLOR_MAIN,
|
2023-01-29 20:06:42 +00:00
|
|
|
theme_colors[theme_color][0],
|
|
|
|
theme_colors[theme_color][1],
|
|
|
|
theme_colors[theme_color][2],
|
|
|
|
theme_colors[theme_color][3]
|
|
|
|
);
|
2023-01-25 18:53:15 +00:00
|
|
|
|
2023-02-23 21:48:21 +00:00
|
|
|
if (theme_color == 5)
|
2023-02-01 03:44:22 +00:00
|
|
|
glfwThemeSetAttribute(theme, GLFW_THEME_COLOR_MAIN, GLFW_FALSE);
|
2023-01-30 00:12:12 +00:00
|
|
|
else
|
2023-02-01 03:44:22 +00:00
|
|
|
glfwThemeSetAttribute(theme, GLFW_THEME_COLOR_MAIN, GLFW_TRUE);
|
2023-01-25 18:53:15 +00:00
|
|
|
|
|
|
|
const char* title;
|
|
|
|
|
2023-01-29 20:06:42 +00:00
|
|
|
switch (glfwThemeGetVariation(theme)) {
|
|
|
|
case GLFW_THEME_DEFAULT:
|
2023-01-25 18:53:15 +00:00
|
|
|
title = "Default theme";
|
|
|
|
break;
|
2023-01-29 20:06:42 +00:00
|
|
|
case GLFW_THEME_LIGHT:
|
2023-01-25 18:53:15 +00:00
|
|
|
title = "Light theme";
|
|
|
|
break;
|
2023-01-29 20:06:42 +00:00
|
|
|
case GLFW_THEME_DARK:
|
2023-01-25 18:53:15 +00:00
|
|
|
title = "Dark theme";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
glfwSetWindowTitle(window, title);
|
2023-01-29 20:06:42 +00:00
|
|
|
glfwSetTheme(window, theme);
|
2023-01-25 18:53:15 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 21:48:21 +00:00
|
|
|
static void flip_attribute(int attribute)
|
|
|
|
{
|
|
|
|
glfwThemeSetAttribute(theme, attribute, !glfwThemeGetAttribute(theme, attribute));
|
|
|
|
}
|
|
|
|
|
2023-01-25 18:53:15 +00:00
|
|
|
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
|
|
|
{
|
|
|
|
if (action != GLFW_PRESS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (key)
|
|
|
|
{
|
2023-02-23 21:48:21 +00:00
|
|
|
// No theme specified
|
2023-01-25 18:53:15 +00:00
|
|
|
case GLFW_KEY_0:
|
|
|
|
glfwSetWindowTitle(window, "Default theme (NULL)");
|
|
|
|
glfwSetTheme(window, NULL);
|
|
|
|
break;
|
2023-02-23 21:48:21 +00:00
|
|
|
// Theme with default variation
|
2023-01-25 18:53:15 +00:00
|
|
|
case GLFW_KEY_1:
|
2023-01-29 20:06:42 +00:00
|
|
|
glfwThemeSetVariation(theme, GLFW_THEME_DEFAULT);
|
2023-01-25 18:53:15 +00:00
|
|
|
set_theme(window, cur_theme_color);
|
|
|
|
break;
|
2023-02-23 21:48:21 +00:00
|
|
|
// Theme with light variation
|
2023-01-25 18:53:15 +00:00
|
|
|
case GLFW_KEY_2:
|
2023-01-29 20:06:42 +00:00
|
|
|
glfwThemeSetVariation(theme, GLFW_THEME_LIGHT);
|
2023-01-25 18:53:15 +00:00
|
|
|
set_theme(window, cur_theme_color);
|
|
|
|
break;
|
2023-02-23 21:48:21 +00:00
|
|
|
// Theme with dark variation
|
2023-01-25 18:53:15 +00:00
|
|
|
case GLFW_KEY_3:
|
2023-01-29 20:06:42 +00:00
|
|
|
glfwThemeSetVariation(theme, GLFW_THEME_DARK);
|
2023-01-25 18:53:15 +00:00
|
|
|
set_theme(window, cur_theme_color);
|
|
|
|
break;
|
2023-02-23 21:48:21 +00:00
|
|
|
// Traverse colors
|
2023-01-25 18:53:15 +00:00
|
|
|
case GLFW_KEY_SPACE:
|
|
|
|
cur_theme_color = (cur_theme_color + 1) % 6;
|
|
|
|
set_theme(window, cur_theme_color);
|
|
|
|
break;
|
2023-02-23 21:48:21 +00:00
|
|
|
// Inspect
|
|
|
|
case GLFW_KEY_I:
|
|
|
|
print_theme(theme, "Constructed theme");
|
|
|
|
break;
|
|
|
|
// Print
|
2023-01-25 23:46:37 +00:00
|
|
|
case GLFW_KEY_P:
|
2023-02-23 21:48:21 +00:00
|
|
|
print_theme(glfwGetTheme(window, GLFW_FALSE), "Specified window theme");
|
|
|
|
print_theme(glfwGetTheme(window, GLFW_TRUE), "Active window theme");
|
|
|
|
printf("\n");
|
|
|
|
break;
|
|
|
|
// High contrast
|
|
|
|
case GLFW_KEY_H:
|
|
|
|
flip_attribute(GLFW_THEME_ATTRIBUTE_HIGH_CONTRAST);
|
|
|
|
set_theme(window, cur_theme_color);
|
|
|
|
break;
|
|
|
|
// Transparency
|
|
|
|
case GLFW_KEY_T:
|
|
|
|
flip_attribute(GLFW_THEME_ATTRIBUTE_REDUCE_TRANSPARENCY);
|
|
|
|
set_theme(window, cur_theme_color);
|
|
|
|
break;
|
|
|
|
// Motion
|
|
|
|
case GLFW_KEY_M:
|
|
|
|
flip_attribute(GLFW_THEME_ATTRIBUTE_REDUCE_MOTION);
|
|
|
|
set_theme(window, cur_theme_color);
|
|
|
|
break;
|
|
|
|
// Exit
|
|
|
|
case GLFW_KEY_ESCAPE:
|
|
|
|
glfwSetWindowShouldClose(window, GLFW_TRUE);
|
2023-01-25 23:46:37 +00:00
|
|
|
break;
|
2023-01-25 18:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-25 23:46:37 +00:00
|
|
|
static void theme_callback(GLFWtheme* theme)
|
|
|
|
{
|
|
|
|
print_theme(theme, "System theme changed to");
|
|
|
|
}
|
|
|
|
|
2023-01-25 18:53:15 +00:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
GLFWwindow* window;
|
|
|
|
|
|
|
|
if (!glfwInit())
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to initialize GLFW\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2023-01-30 02:09:39 +00:00
|
|
|
|
|
|
|
print_theme(glfwGetSystemDefaultTheme(), "System theme");
|
2023-01-25 18:53:15 +00:00
|
|
|
|
|
|
|
window = glfwCreateWindow(200, 200, "Window Icon", NULL, NULL);
|
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
glfwTerminate();
|
|
|
|
|
|
|
|
fprintf(stderr, "Failed to open GLFW window\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2023-02-23 21:48:21 +00:00
|
|
|
|
|
|
|
print_theme(glfwGetTheme(window, GLFW_FALSE), "Initial specified window theme");
|
|
|
|
print_theme(glfwGetTheme(window, GLFW_TRUE), "Initial active window theme");
|
|
|
|
printf("\n");
|
2023-01-25 18:53:15 +00:00
|
|
|
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
|
gladLoadGL(glfwGetProcAddress);
|
|
|
|
|
|
|
|
glfwSetKeyCallback(window, key_callback);
|
|
|
|
|
2023-01-29 20:06:42 +00:00
|
|
|
theme = glfwCreateTheme();
|
2023-01-25 18:53:15 +00:00
|
|
|
set_theme(window, cur_theme_color);
|
2023-01-25 23:46:37 +00:00
|
|
|
|
|
|
|
glfwSetSystemThemeCallback(theme_callback);
|
2023-01-25 18:53:15 +00:00
|
|
|
|
|
|
|
while (!glfwWindowShouldClose(window))
|
|
|
|
{
|
|
|
|
glClearColor(
|
2023-01-29 21:57:30 +00:00
|
|
|
theme_colors[cur_theme_color][0],
|
|
|
|
theme_colors[cur_theme_color][1],
|
|
|
|
theme_colors[cur_theme_color][2],
|
|
|
|
theme_colors[cur_theme_color][3]
|
2023-01-25 18:53:15 +00:00
|
|
|
);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glfwSwapBuffers(window);
|
|
|
|
glfwWaitEvents();
|
|
|
|
}
|
|
|
|
|
2023-01-29 20:06:42 +00:00
|
|
|
glfwDestroyTheme(theme);
|
2023-01-25 18:53:15 +00:00
|
|
|
glfwDestroyWindow(window);
|
|
|
|
glfwTerminate();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|