forked from AidanSun05/ImGuiTextSelect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
130 lines (101 loc) · 3.66 KB
/
main.cpp
File metadata and controls
130 lines (101 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2024-2025 Aidan Sun and the ImGuiTextSelect contributors
// SPDX-License-Identifier: MIT
#include <string_view>
#include <vector>
#include <GLFW/glfw3.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include "textselect.hpp"
const std::vector<std::string_view> lines{
"Line 1",
"Line 2",
"Line 3",
"A longer line",
"Text selection in Dear ImGui",
"UTF-8 characters Ë ⑤ 三【 】┌──┐",
};
std::string_view getLineAtIdx(size_t idx) {
return lines[idx];
}
size_t getNumLines() {
return lines.size();
}
int main() {
if (!glfwInit()) {
return 1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(1280, 720, "ImGuiTextSelect example", nullptr, nullptr);
if (!window) {
return 1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
// UTF-8 support requires a compatible font that can be used to display the characters
// An example of loading a font is below
// Tip: ImGuiTextSelect works with both monospace and variable-width fonts.
//
// ImVector<ImWchar> ranges;
// ImFontGlyphRangesBuilder builder;
// builder.AddText("Ë ⑤ 三【 】┌──┐"); // Add special characters from the lines above
// builder.AddRanges(ImGui::GetIO().Fonts->GetGlyphRangesDefault());
// builder.BuildRanges(&ranges);
// ImGui::GetIO().Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\msjh.ttc", 18.0f, nullptr, ranges.Data);
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init();
TextSelect textSelect{ getLineAtIdx, getNumLines };
// If using word wrapping:
// TextSelect textSelect{ getLineAtIdx, getNumLines, true };
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::SetNextWindowSize({ 300, 200 }, ImGuiCond_FirstUseEver);
ImGui::Begin("Text selection");
ImGui::BeginChild("text", {}, 0, ImGuiWindowFlags_NoMove);
for (const auto& line : lines) {
ImGui::TextUnformatted(line.data());
// If using word wrapping:
// ImGui::TextWrapped("%s", line.data());
}
textSelect.update();
if (ImGui::BeginPopupContextWindow()) {
ImGui::BeginDisabled(!textSelect.hasSelection());
if (ImGui::MenuItem("Copy", "Ctrl+C")) {
textSelect.copy();
}
ImGui::EndDisabled();
if (ImGui::MenuItem("Select all", "Ctrl+A")) {
textSelect.selectAll();
}
if (ImGui::MenuItem("Clear selection")) {
textSelect.clearSelection();
}
ImGui::EndPopup();
}
ImGui::EndChild();
ImGui::End();
ImGui::Render();
int displayX, displayY;
glfwGetFramebufferSize(window, &displayX, &displayY);
glViewport(0, 0, displayX, displayY);
glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
}