Skip to content

Commit 58c9bc5

Browse files
authored
Merge pull request cpp-best-practices#5 from lefticus/add_gui_examples
Add gui examples
2 parents c4d166a + bc832b9 commit 58c9bc5

25 files changed

+27002
-20
lines changed

.clang-format

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
AccessModifierOffset: -2
2+
AlignAfterOpenBracket: DontAlign
3+
AlignConsecutiveAssignments: false
4+
AlignConsecutiveDeclarations: false
5+
AlignEscapedNewlines: Left
6+
AlignOperands: true
7+
AlignTrailingComments: false
8+
AllowAllParametersOfDeclarationOnNextLine: false
9+
AllowShortBlocksOnASingleLine: true
10+
AllowShortCaseLabelsOnASingleLine: false
11+
AllowShortFunctionsOnASingleLine: All
12+
AllowShortIfStatementsOnASingleLine: true
13+
AllowShortLoopsOnASingleLine: true
14+
AlwaysBreakAfterDefinitionReturnType: None
15+
AlwaysBreakAfterReturnType: None
16+
AlwaysBreakBeforeMultilineStrings: true
17+
AlwaysBreakTemplateDeclarations: false
18+
BinPackArguments: false
19+
BinPackParameters: false
20+
BraceWrapping:
21+
AfterClass: true
22+
AfterControlStatement: false
23+
AfterEnum: false
24+
AfterFunction: true
25+
AfterNamespace: false
26+
AfterObjCDeclaration: false
27+
AfterStruct: true
28+
AfterUnion: false
29+
BeforeCatch: false
30+
BeforeElse: false
31+
IndentBraces: false
32+
SplitEmptyFunction: false
33+
SplitEmptyNamespace: true
34+
SplitEmptyRecord: true
35+
BreakAfterJavaFieldAnnotations: true
36+
BreakBeforeBinaryOperators: NonAssignment
37+
BreakBeforeBraces: Custom
38+
BreakBeforeInheritanceComma: true
39+
BreakBeforeTernaryOperators: true
40+
BreakConstructorInitializers: BeforeColon
41+
BreakConstructorInitializersBeforeComma: false
42+
BreakStringLiterals: true
43+
ColumnLimit: 0
44+
CommentPragmas: '^ IWYU pragma:'
45+
CompactNamespaces: false
46+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
47+
ConstructorInitializerIndentWidth: 2
48+
ContinuationIndentWidth: 2
49+
Cpp11BracedListStyle: false
50+
DerivePointerAlignment: false
51+
DisableFormat: false
52+
ExperimentalAutoDetectBinPacking: true
53+
FixNamespaceComments: true
54+
ForEachMacros:
55+
- foreach
56+
- Q_FOREACH
57+
- BOOST_FOREACH
58+
IncludeCategories:
59+
- Priority: 2
60+
Regex: ^"(llvm|llvm-c|clang|clang-c)/
61+
- Priority: 3
62+
Regex: ^(<|"(gtest|gmock|isl|json)/)
63+
- Priority: 1
64+
Regex: .*
65+
IncludeIsMainRegex: (Test)?$
66+
IndentCaseLabels: false
67+
IndentWidth: 2
68+
IndentWrappedFunctionNames: true
69+
JavaScriptQuotes: Leave
70+
JavaScriptWrapImports: true
71+
KeepEmptyLinesAtTheStartOfBlocks: true
72+
Language: Cpp
73+
MacroBlockBegin: ''
74+
MacroBlockEnd: ''
75+
MaxEmptyLinesToKeep: 2
76+
NamespaceIndentation: Inner
77+
ObjCBlockIndentWidth: 7
78+
ObjCSpaceAfterProperty: true
79+
ObjCSpaceBeforeProtocolList: false
80+
PointerAlignment: Right
81+
ReflowComments: true
82+
SortIncludes: false
83+
SortUsingDeclarations: false
84+
SpaceAfterCStyleCast: false
85+
SpaceAfterTemplateKeyword: false
86+
SpaceBeforeAssignmentOperators: true
87+
SpaceBeforeParens: ControlStatements
88+
SpaceInEmptyParentheses: false
89+
SpacesBeforeTrailingComments: 0
90+
SpacesInAngles: false
91+
SpacesInCStyleCastParentheses: false
92+
SpacesInContainerLiterals: true
93+
SpacesInParentheses: false
94+
SpacesInSquareBrackets: false
95+
Standard: Cpp11
96+
TabWidth: 8
97+
UseTab: Never
98+

.cmake-format.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
additional_commands:
2+
foo:
3+
flags:
4+
- BAR
5+
- BAZ
6+
kwargs:
7+
DEPENDS: '*'
8+
HEADERS: '*'
9+
SOURCES: '*'
10+
bullet_char: '*'
11+
dangle_parens: false
12+
enum_char: .
13+
line_ending: unix
14+
line_width: 100
15+
max_subargs_per_line: 3
16+
separate_ctrl_name_with_space: false
17+
separate_fn_name_with_space: false
18+
tab_size: 2
19+

CMakeLists.txt

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,103 @@
11
cmake_minimum_required(VERSION 3.2)
22

3+
option(BUILD_SHARED_LIBS "Enable compilation of shared libraries" FALSE)
4+
35
# Link this 'library' to use the following warnings
46
add_library(project_warnings INTERFACE)
57

6-
if (CMAKE_COMPILER_IS_GNUCC)
8+
if(CMAKE_COMPILER_IS_GNUCC)
79
option(ENABLE_COVERAGE "Enable coverage reporting for gcc/clang" FALSE)
810

9-
if (ENABLE_COVERAGE)
11+
if(ENABLE_COVERAGE)
1012
add_compile_options(--coverage -O0)
1113
endif()
1214
endif()
1315

14-
if (MSVC)
16+
if(MSVC)
1517
target_compile_options(project_warnings INTERFACE /W4)
1618
else()
17-
target_compile_options(project_warnings INTERFACE -Wall -Wextra -Wpedantic)
19+
target_compile_options(project_warnings
20+
INTERFACE
21+
-Wall
22+
-Wextra # reasonable and standard
23+
-Wshadow # warn the user if a variable declaration shadows one from a
24+
# parent context
25+
-Wnon-virtual-dtor # warn the user if a class with virtual functions has a
26+
# non-virtual destructor. This helps catch hard to
27+
# track down memory errors
28+
-Wold-style-cast # warn for c-style casts
29+
-Wcast-align # warn for potential performance problem casts
30+
-Wunused # warn on anything being unused
31+
-Woverloaded-virtual # warn if you overload (not override) a virtual
32+
# function
33+
-Wpedantic # warn if non-standard C++ is used
34+
-Wconversion # warn on type conversions that may lose data
35+
-Wsign-conversion # warn on sign conversions
36+
-Wmisleading-indentation # warn if identation implies blocks where blocks
37+
# do not exist
38+
-Wduplicated-cond # warn if if / else chain has duplicated conditions
39+
-Wduplicated-branches # warn if if / else branches have duplicated code
40+
-Wlogical-op # warn about logical operations being used where bitwise were
41+
# probably wanted
42+
-Wnull-dereference # warn if a null dereference is detected
43+
-Wuseless-cast # warn if you perform a cast to the same type
44+
-Wdouble-promotion # warn if float is implicit promoted to double
45+
-Wformat=2 # warn on security issues around functions that format output
46+
# (ie printf)
47+
)
1848
endif()
1949

2050
add_executable(intro main.cpp)
21-
target_compile_features(intro PRIVATE cxx_lambda_init_captures)
22-
target_link_libraries(intro PRIVATE project_warnings)
23-
24-
target_link_libraries(intro --coverage)
51+
target_compile_features(intro PRIVATE cxx_std_17)
52+
target_link_libraries(intro PRIVATE project_warnings --coverage)
2553

2654
enable_testing()
2755

2856
add_executable(tester tester.cpp)
29-
target_link_libraries(tester PRIVATE project_warnings)
57+
target_link_libraries(tester PRIVATE project_warnings --coverage)
3058

31-
target_link_libraries(tester --coverage)
3259
add_test(Tester tester)
3360

61+
#fltk test
62+
find_package(FLTK REQUIRED)
63+
add_executable(test_fltk fltk/test_fltk.cpp)
64+
target_link_libraries(test_fltk PRIVATE project_warnings ${FLTK_LIBRARIES})
65+
target_include_directories(test_fltk PRIVATE ${FLTK_INCLUDE_DIR})
66+
67+
# gtkmm test
68+
find_package(PkgConfig REQUIRED)
69+
pkg_check_modules(GTKMM REQUIRED gtkmm-3.0)
70+
add_executable(test_gtkmm gtkmm/main.cpp gtkmm/hello_world.cpp)
71+
target_link_libraries(test_gtkmm PRIVATE project_warnings ${GTKMM_LIBRARIES})
72+
target_include_directories(test_gtkmm PRIVATE ${GTKMM_INCLUDE_DIRS})
73+
74+
# imgui example
75+
find_package(SFML COMPONENTS graphics window system)
76+
find_package(OpenGL)
77+
78+
# imgui + sfml built as a lib, intentionally not using full warning flags
79+
add_library(imgui imgui/lib/imgui.cpp imgui/lib/imgui_draw.cpp imgui/lib/imgui-SFML.cpp)
80+
target_link_libraries(imgui INTERFACE ${SFML_LIBRARIES} ${OPENGL_gl_LIBRARY})
81+
82+
# imgui test executable, with full warnings enabled
83+
add_executable(test_imgui imgui/test.cpp)
84+
target_link_libraries(test_imgui PRIVATE project_warnings imgui)
85+
target_include_directories(test_imgui PRIVATE ${SFML_INCLUDE_DIR})
86+
87+
# Nana
88+
include(ExternalProject)
89+
ExternalProject_add(
90+
Nana
91+
GIT_REPOSITORY https://github.com/cnjinhao/nana.git
92+
GIT_TAG v1.5.6
93+
CMAKE_CACHE_ARGS "-DNANA_CMAKE_SHARED_LIB"
94+
INSTALL_COMMAND ""
95+
)
96+
97+
#ExternalProject_Get_Property(Nana NANA_INCLUDE_DIR)
98+
ExternalProject_Get_Property(Nana SOURCE_DIR BINARY_DIR)
99+
add_executable(test_nana nana/main.cpp)
100+
target_include_directories(test_nana PRIVATE ${SOURCE_DIR}/include)
101+
target_link_libraries(test_nana PRIVATE ${BINARY_DIR}/libnana.so ${NANA_LINKS})
102+
34103

fltk/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Example FLTK code from http://www.fltk.org/doc-1.3/basics.html

fltk/test_fltk.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <FL/Fl.H>
2+
#include <FL/Fl_Window.H>
3+
#include <FL/Fl_Box.H>
4+
#include <iostream>
5+
6+
int main(int argc, char **argv)
7+
{
8+
std::cout << "utf8 locale " << fl_utf8locale() << '\n';
9+
Fl_Window *window = new Fl_Window(340, 180);
10+
Fl_Box *box = new Fl_Box(20, 40, 300, 100, "Hello World");
11+
box->box(FL_UP_BOX);
12+
box->labelfont(FL_BOLD + FL_ITALIC);
13+
box->labelsize(32);
14+
box->labeltype(FL_SHADOW_LABEL);
15+
window->end();
16+
window->show(argc, argv);
17+
return Fl::run();
18+
}

gtkmm/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Example Hello World from https://developer.gnome.org/gtkmm-tutorial/stable/sec-helloworld.html.en

gtkmm/hello_world.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "hello_world.hpp"
2+
#include <iostream>
3+
4+
HelloWorld::HelloWorld()
5+
: m_button("Hello World")// creates a new button with label "Hello World".
6+
{
7+
// Sets the border width of the window.
8+
set_border_width(10);
9+
10+
// When the button receives the "clicked" signal, it will call the
11+
// on_button_clicked() method defined below.
12+
m_button.signal_clicked().connect(sigc::mem_fun(*this,
13+
&HelloWorld::on_button_clicked));
14+
15+
// This packs the button into the Window (a container).
16+
add(m_button);
17+
18+
// The final step is to display this newly created widget...
19+
m_button.show();
20+
}
21+
22+
23+
void HelloWorld::on_button_clicked()
24+
{
25+
std::cout << "Hello World\n";
26+
}

gtkmm/hello_world.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef GTKMM_EXAMPLE_HELLO_WORLD_HPP
2+
#define GTKMM_EXAMPLE_HELLO_WORLD_HPP
3+
4+
#include <gtkmm/button.h>
5+
#include <gtkmm/window.h>
6+
7+
class HelloWorld : public Gtk::Window
8+
{
9+
10+
public:
11+
HelloWorld();
12+
13+
protected:
14+
//Signal handlers:
15+
void on_button_clicked();
16+
17+
//Member widgets:
18+
Gtk::Button m_button;
19+
};
20+
21+
#endif// GTKMM_EXAMPLE_HELLO_WORLD_HPP

gtkmm/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "hello_world.hpp"
2+
#include <gtkmm/application.h>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
7+
8+
HelloWorld helloworld;
9+
10+
//Shows the window and returns when it is closed.
11+
return app->run(helloworld);
12+
}

imgui/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Example of using imgui with SFML from https://github.com/eliasdaler/imgui-sfml

0 commit comments

Comments
 (0)