GTK4

Gtk4 with Cpp. References: Gtk 1. Introduction Gtk4 is a widget toolkit for creating graphical user interface (UI). It works on many UNIX-like platform, Windows and Macos. 2. Setup the environment Note: - This is the guides for Windows machine using Windows Subsystem for Linux (WSL). - [W]: The action will be performed on Windows Machine. - [U]: The action will be performed on WSL (in my case, it named "Ubuntu") on the host Windows Machine. - (Guides: ...): The reference link that I refer the step. - (Not try yet): This is referred from the guides but I was not in that case, so that I could not try. ------------------------------- A. Installing from packages ------------------------------- 1. [L] Install required packages. 1.1. [L] Debian/Ubuntu: sudo apt install libgtk-4-1 libgtk-4-dev gtk-4-examples 2. [L] Verify installation. 2.1. [L] gtk version - Run: pkg-config --modversion gtk4 2.2. [L] examples - Run: gtk4-demo ------------------------------- B. (Optional) Build from Source ------------------------------- 1. [L] Install build tools. 1.1. [L] sudo apt install build-essential meson ninja-build \ libglib2.0-dev libpango1.0-dev libgdk-pixbuf-2.0-dev \ libatk1.0-dev libepoxy-dev libgirepository1.0-dev 2. [L] Clone GTK source. 2.1. [L] git clone https://gitlab.gnome.org/GNOME/gtk.git 2.2. [L] cd gtk 3. [L] Build with Meson + Ninja. 3.1. [L] meson setup builddir 3.2. [L] ninja -C builddir 4. [L] (Optional) Install system-wide. 4.1. [L] sudo ninja -C builddir install 3. HelloWorld Create a new file named hello-world-gtk4.c with following content: #include <gtk/gtk.h> static void print_hello (GtkWidget *widget, gpointer data) { g_print ("Hello World\n"); } static void activate (GtkApplication *app, gpointer user_data) { GtkWidget *window; GtkWidget *button; window = gtk_application_window_new (app); gtk_window_set_title (GTK_WINDOW (window), "Hello"); gtk_window_set_default_size (GTK_WINDOW (window), 200, 200); button = gtk_button_new_with_label ("Hello World"); g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL); gtk_window_set_child (GTK_WINDOW (window), button); gtk_window_present (GTK_WINDOW (window)); } int main (int argc, char **argv) { GtkApplication *app; int status; app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS); g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); status = g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app); return status; } - Hints: ...

August 21, 2025 · 2 min · Phong Nguyen

Cpp

See plus plus :) . Refer 0. Notes printf/snprintf Cheat Sheet {C / C++} Integer Data type Specifier Notes int8_t / signed char %hhd signed 8-bit uint8_t / unsigned char %hhu unsigned 8-bit int16_t / short %hd signed 16-bit uint16_t / unsigned short %hu unsigned 16-bit int32_t / long %ld signed 32-bit uint32_t / unsigned long %lu unsigned 32-bit int64_t / long long %lld signed 64-bit uint64_t / unsigned long long %llu unsigned 64-bit Floating point Data type Specifier Notes float %f 4-byte float double %f Arduino AVR: double = float long double %Lf depends on platform %e → scientific notation %g → auto select %f or %e Char / String Data type Specifier Notes char %c single character char* / String %s null-terminated string Pointer / Address Data type Specifier Notes void* %p memory address, hex Hex / Octal / Binary Data type Specifier Notes unsigned int %x / %X hexadecimal unsigned int %o octal Arduino only %b binary Flags, Width, Precision %-10d → left-justify, width 10 %010d → pad with zeros, width 10 %.2f → 2 decimal digits %*d → dynamic width Specific Notes uint32_t → %lu int32_t → %ld uint16_t → %u int16_t → %d or %hd uint8_t → %u or %hhu int8_t → %d or %hhd float → %f Use snprintf() with correctly sized buffer to avoid overflow Code Timming C++11 comes with some functionality in the chrono library to time our code to see how long it takes to run. e.g. #include <array> #include <chrono> // for std::chrono functions #include <cstddef> // for std::size_t #include <iostream> #include <numeric> // for std::iota const int g_arrayElements { 10000 }; class Timer { private: // Type aliases to make accessing nested type easier using Clock = std::chrono::steady_clock; using Second = std::chrono::duration<double, std::ratio<1> >; std::chrono::time_point<Clock> m_beg{ Clock::now() }; public: void reset() { m_beg = Clock::now(); } double elapsed() const { return std::chrono::duration_cast<Second>(Clock::now() - m_beg).count(); } }; void sortArray(std::array<int, g_arrayElements>& array) { // Step through each element of the array // (except the last one, which will already be sorted by the time we get there) for (std::size_t startIndex{ 0 }; startIndex < (g_arrayElements - 1); ++startIndex) { // smallestIndex is the index of the smallest element we’ve encountered this iteration // Start by assuming the smallest element is the first element of this iteration std::size_t smallestIndex{ startIndex }; // Then look for a smaller element in the rest of the array for (std::size_t currentIndex{ startIndex + 1 }; currentIndex < g_arrayElements; ++currentIndex) { // If we've found an element that is smaller than our previously found smallest if (array[currentIndex] < array[smallestIndex]) { // then keep track of it smallestIndex = currentIndex; } } // smallestIndex is now the smallest element in the remaining array // swap our start element with our smallest element (this sorts it into the correct place) std::swap(array[startIndex], array[smallestIndex]); } } int main() { std::array<int, g_arrayElements> array; std::iota(array.rbegin(), array.rend(), 1); // fill the array with values 10000 to 1 Timer t; sortArray(array); std::cout << "Time taken: " << t.elapsed() << " seconds\n"; return 0; } Command line Command line arguments are optional string arguments that are passed by the operating system to the program when it launch. Passing command line arguments: we simply list the command line arguments right after the executeable name. Using command line arguments: by using different form of main(): main(int argc, char* argv[]) / main(int argc, char** argv) argc: argument count, always be at least 1, because the first argument is always the name of the program itself. argv: is where the actual argument values are stored (think: argv = argument values) e.g. #include <iostream> #include <sstream> // for std::stringstream #include <string> int main(int argc, char* argv[]) { if (argc <= 1) { // On some operating systems, argv[0] can end up as an empty string instead of the program's name. // We'll conditionalize our response on whether argv[0] is empty or not. if (argv[0]) std::cout << "Usage: " << argv[0] << " <number>" << '\n'; else std::cout << "Usage: <program name> <number>" << '\n'; return 1; } std::stringstream convert{ argv[1] }; // set up a stringstream variable named convert, initialized with the input from argv[1] int myint{}; if (!(convert >> myint)) // do the conversion myint = 0; // if conversion fails, set myint to a default value std::cout << "Got integer: " << myint << '\n'; return 0; } Things that can impact the performance of the program: TBD Measuring performance: gather at least 3 results. the program runs in 10 seconds etc 1. Introduction C++ was developed as an extension to C. It adds man few features to the C language, and tis perhaps best through of as a superset of C. Step 1: Define the problem that you would like to solve ...

February 9, 2025 · 101 min · Phong Nguyen