NvDialog 0.10.1
A cross-platform modal dialogs library for C/C++ that uses the native OS theme.
Loading...
Searching...
No Matches
nvdialog_tutorial.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2025 Aggelos Tselios
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25/************************************************************************************
26 * This file is NOT source code. It will just be used by Doxygen when
27 * generating documentation for a tutorial page. It won't be installed, it won't
28 * be used within the library or by a user, and probably isn't of interest to
29 * anyone outside this.
30 ************************************************************************************/
31
32#define __nvd_tutorial
33
34/**
35 * @page NvDialog Tutorial
36 * @version v0.10.1
37 * @author Aggelos Tselios
38
39 * # NvDialog Tutorial
40 * This page provides a simple tutorial of the NvDialog library. For more
41 information about the
42 * library itself, see <b>https://tseli0s.github.io/nv.dialog</b>.
43 * Proper documentation of the API used in this tutorial is also available in
44 the documentation.
45 * This tutorial assumes you have already installed NvDialog in your system. If
46 not, visit the [GitHub
47 Repo](https://github.com/tseli0s/nvdialog/#installation).
48 * NvDialog is compiler and platform independent, so this tutorial should work
49 as expected on all platforms.
50 * # 1. Creating our environment.
51 * <b>Skip this step if you aren't interested in the setup.</b>
52 * Create a new file named `main.c` and add the following to it:
53 * @code
54 * #include <nvdialog/nvdialog.h>
55 *
56 * int main() {
57 *
58 * }
59 * @endcode
60 * This snippet contains a barebones C program which includes NvDialog. You can
61 try compiling this
62 * to verify that the headers can be found at compile time.
63 * @note Remember that in some platforms headers are installed in non-standard
64 places by CMake. If that's the
65 * case, you will have to manually instruct the compiler to find the headers.
66 * # 2. Creating a basic dialog box.
67 * A dialog box is the simplest and most straightforward feature of NvDialog: It
68 shows a dialog
69 * with some parameters given. You will mainly use this for error messages
70 (Although it can be used
71 * for literally anything, see @ref NvdDialogType ).
72 *
73 * To get started, we first need to initialize NvDialog. This can be done with a
74 single call:
75 * > Initialization is mainly required by the backends. Each backend is often
76 required to be initialized first.
77 * @code
78 * if (nvd_init(argv[0]) != 0) { return -1; }
79 * @endcode
80 * Go to your main function, and create a new dialog box object. The title and
81 the message need to be
82 * a regular NULL terminated C string. For the sake of simplicity we will create
83 them in seperate variables:
84 * @code
85 * const char *title = "Hello NvDialog!";
86 * const char *message = "This dialog box was created with NvDialog.";
87 *
88 * NvdDialogBox *dialog = nvd_dialog_box_new(title, message, NVD_DIALOG_SIMPLE);
89 * @endcode
90 * @ref nvd_dialog_box_new will return NULL on failure, so you may want to
91 handle that too.
92 * # 3. Showing the dialog.
93 * Before @version v0.3.0, creating and showing the dialog was a single call. So
94 the above snippet
95 * would show us the dialog directly, without any extra calls.
96 * This changed because on some platforms (Especially slow ones) caused serious
97 issues that could halt
98 * the thread for 1-2 seconds while creating the dialog! So these have been
99 seperated since.
100 * Now, we will also have to call @ref nvd_show_dialog :
101 * @code
102 * nvd_show_dialog(dialog);
103 * @endcode
104 * The function takes a pointer to the dialog we want to show. For more
105 information read the documentation
106 * by clicking the function.
107 * # 4. Cleanup and finally compiling.
108 * Now, we are technically done. However, we have caused a memory leak here
109 because after all, the
110 * @ref NvdDialogBox type like most others is heap-allocated! Which means we
111 need to `free()` it.
112 * Calling `free()` directly though will cause undefined behavior. So instead,
113 we will use a special
114 * function provided by NvDialog, which frees most types. That function is
115 called @ref nvd_free_object .
116 * @code
117 * nvd_free_object(dialog);
118 * // Not required but let's have it here to indicate success.
119 * return 0;
120 * @endcode
121 * Your final source file should look like this:
122 * @code
123 #include <nvdialog/nvdialog.h>
124
125 int main() {
126 if (nvd_init() != 0) return -1;
127
128 const char *title = "Hello NvDialog!";
129 const char *message = "This dialog box was created with NvDialog.";
130 NvdDialogBox *dialog = nvd_dialog_box_new(title, message,
131 NVD_DIALOG_SIMPLE);
132
133 nvd_show_dialog(dialog);
134 nvd_free_object(dialog);
135 return 0;
136 }
137 * @endcode
138 * <b>Now let's compile it:</b>
139 * @note Each compiler has different options for the same thing. In this case, I
140 am using GCC 12.1
141 * on Arch Linux.
142 *
143 * On the terminal, go to the directory where your source file is located (Using
144 `cd`).
145 * Then run the following command to compile your file:
146 * ```sh
147 * $ gcc \ # Our compiler
148 * $ main.c \ # Your file
149 * $ -l nvdialog \ # Link with the NvDialog library
150 * $ -o nvdialog-tutorial \ # Executable filename
151 * ```
152 * And now running it should give you a dialog box.
153 * That's all you need to get started. For other dialogs to use, take a look at
154 the
155 * following types:
156 * - @ref NvdQuestionBox
157 * - @ref NvdFileDialog
158 * - @ref NvdAboutDialog
159 */