// Dear ImGui: standalone example application for GLFW + OpenGL2, using legacy fixed pipeline // (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.) // Learn about Dear ImGui: // - FAQ https://dearimgui.com/faq // - Getting Started https://dearimgui.com/getting-started // - Documentation https://dearimgui.com/docs (same as your local docs/ folder). // - Introduction, links and more at the top of imgui.cpp // **DO NOT USE THIS CODE IF YOUR CODE/ENGINE IS USING MODERN OPENGL (SHADERS, VBO, VAO, etc.)** // **Prefer using the code in the example_glfw_opengl2/ folder** // See imgui_impl_glfw.cpp for details. #include #include #include #include #ifdef __APPLE__ #define GL_SILENCE_DEPRECATION #endif #include #include #include #include "ui.hpp" #include "administration.hpp" #include "administration_writer.hpp" #include "administration_reader.hpp" // [Win32] Our example includes a copy of glfw3.lib pre-compiled with VS2010 to maximize ease of testing and compatibility with old VS compilers. // To link with VS2010-era libraries, VS2015+ requires linking with legacy_stdio_definitions.lib, which we do using this pragma. // Your own project should not be affected, as you are likely to link with a newer binary of GLFW that is adequate for your version of Visual Studio. #if defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) #pragma comment(lib, "legacy_stdio_definitions") #endif static GLFWwindow* window = 0; static bool redo_window = 0; void ui::recreate_window_for_main_views() { redo_window = 1; } static void glfw_error_callback(int error, const char* description) { fprintf(stderr, "GLFW Error %d: %s\n", error, description); } static void _create_window(bool is_setup_window) { if (window) { ImGui_ImplOpenGL2_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); glfwDestroyWindow(window); } float main_scale = ImGui_ImplGlfw_GetContentScaleForMonitor(glfwGetPrimaryMonitor()); // Valid on GLFW 3.3+ only int windowWidth = (int)(1280 * main_scale); int windowHeight = (int)(800 * main_scale); if (is_setup_window) { glfwWindowHint(GLFW_RESIZABLE, false); windowWidth = (int)(300 * main_scale); windowHeight = (int)(300 * main_scale); } else { glfwWindowHint(GLFW_RESIZABLE, true); } window = glfwCreateWindow(windowWidth, windowHeight, "OpenBooks", nullptr, nullptr); if (window == nullptr) return; glfwMakeContextCurrent(window); glfwSwapInterval(1); // Enable vsync IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls // Setup Dear ImGui style ImGui::StyleCustom(); //ImGui::StyleColorsLight(); // Setup scaling ImGuiStyle& style = ImGui::GetStyle(); style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again) style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose) // Setup Platform/Renderer backends ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplOpenGL2_Init(); io.Fonts->Clear(); style.FontSizeBase = 16.0f; float iconFontSize = 10.0f; static ImWchar icon_range[] = { ICON_MIN_FA, ICON_MAX_FA, 0 }; ImFontConfig icons_config; icons_config.GlyphMinAdvanceX = iconFontSize; io.Fonts->AddFontFromFileTTF( "/home/aldrik/Projects/open-books/build/" FONT_ICON_FILE_NAME_FAS, iconFontSize, &icons_config, icon_range); ImFontConfig cfg1; cfg1.MergeMode = true; io.Fonts->AddFontFromFileTTF("/home/aldrik/Projects/open-books/build/Roboto-Regular.ttf", 0, &cfg1); ui::fontBold = io.Fonts->AddFontFromFileTTF("/home/aldrik/Projects/open-books/build/Roboto-Bold.ttf", 0); ui::fontBig = io.Fonts->AddFontFromFileTTF("/home/aldrik/Projects/open-books/build/Roboto-Bold.ttf", 30); ui::fontSmall = io.Fonts->AddFontFromFileTTF("/home/aldrik/Projects/open-books/build/Roboto-Regular.ttf", 12); io.Fonts->Build(); } // Main code int main(int argc, char** argv) { glfwSetErrorCallback(glfw_error_callback); if (!glfwInit()) return 1; if (argc < 2) { administration::create_default(""); _create_window(true); } else { if (administration_reader::open_existing(argv[1])) { _create_window(false); ui::set_state(ui::main_state::UI_INVOICES); } else { _create_window(true); } } ImVec4 clear_color = ImVec4(210 / 255.0f, 210 / 255.0f, 210 / 255.0f, 255 / 255.0f); timer_lib_initialize(); administration_writer::create(); while (!glfwWindowShouldClose(window)) { glfwPollEvents(); if (glfwGetWindowAttrib(window, GLFW_ICONIFIED) != 0) { ImGui_ImplGlfw_Sleep(10); continue; } // Start the Dear ImGui frame ImGui_ImplOpenGL2_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); ui::draw_main(); // Rendering ImGui::Render(); int display_w, display_h; glfwGetFramebufferSize(window, &display_w, &display_h); glViewport(0, 0, display_w, display_h); glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); glClear(GL_COLOR_BUFFER_BIT); ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData()); glfwMakeContextCurrent(window); glfwSwapBuffers(window); if (redo_window) { _create_window(false); redo_window = 0; } } administration_writer::save_activities_blocking(); administration_writer::destroy(); timer_lib_shutdown(); administration::destroy(); // Cleanup ImGui_ImplOpenGL2_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); glfwDestroyWindow(window); glfwTerminate(); return 0; }