new wrappers for GTK widgets

This commit is contained in:
Selim Mustafaev 2022-05-11 00:09:04 +03:00
parent 6e948f5df0
commit 88d1126822
9 changed files with 163 additions and 24 deletions

View File

@ -49,7 +49,7 @@ add_executable(autocat_gnome main.cpp
gtkpp/Application.cpp
gtkpp/Application.h
gtkpp/Window.cpp
gtkpp/Window.h)
gtkpp/Window.h gtkpp/Box.cpp gtkpp/Box.h gtkpp/Widget.cpp gtkpp/Widget.h gtkpp/Entry.cpp gtkpp/Entry.h)
target_link_libraries(autocat_gnome ${GTKMM_LIBRARIES}
${GLIBMM_LIBRARIES}

17
gtkpp/Box.cpp Normal file
View File

@ -0,0 +1,17 @@
//
// Created by selim on 10.05.2022.
//
#include "Box.h"
gtkpp::Box::Box(GtkOrientation orientation, int spacing) {
_widget = gtk_box_new(orientation, spacing);
}
void gtkpp::Box::append(const gtkpp::Widget& widget) {
gtk_box_append(GTK_BOX(_widget), widget.gobj());
}
void gtkpp::Box::append(GtkWidget *widget) {
gtk_box_append(GTK_BOX(_widget), widget);
}

23
gtkpp/Box.h Normal file
View File

@ -0,0 +1,23 @@
//
// Created by selim on 10.05.2022.
//
#ifndef AUTOCAT_GNOME_BOX_H
#define AUTOCAT_GNOME_BOX_H
#include "Widget.h"
#include <gtk/gtkbox.h>
#include <gtk/gtkwidget.h>
namespace gtkpp {
class Box: public Widget{
public:
Box(GtkOrientation orientation, int spacing);
void append(const Widget& widget);
void append(GtkWidget* widget);
};
}
#endif //AUTOCAT_GNOME_BOX_H

21
gtkpp/Entry.cpp Normal file
View File

@ -0,0 +1,21 @@
//
// Created by selim on 10.05.2022.
//
#include "Entry.h"
gtkpp::Entry::Entry() {
_widget = gtk_entry_new();
}
void gtkpp::Entry::setPlaceholder(const std::string &placeholder) {
gtk_entry_set_placeholder_text(GTK_ENTRY(_widget), placeholder.c_str());
}
void gtkpp::Entry::setPurpose(GtkInputPurpose purpose) {
gtk_entry_set_input_purpose(GTK_ENTRY(_widget), purpose);
}
void gtkpp::Entry::setVisibility(bool visibility) {
gtk_entry_set_visibility(GTK_ENTRY(_widget), visibility);
}

25
gtkpp/Entry.h Normal file
View File

@ -0,0 +1,25 @@
//
// Created by selim on 10.05.2022.
//
#ifndef AUTOCAT_GNOME_ENTRY_H
#define AUTOCAT_GNOME_ENTRY_H
#include "Widget.h"
#include <string>
namespace gtkpp {
class Entry: public Widget {
private:
public:
Entry();
void setPlaceholder(const std::string& placeholder);
void setPurpose(GtkInputPurpose purpose);
void setVisibility(bool visibility);
};
}
#endif //AUTOCAT_GNOME_ENTRY_H

24
gtkpp/Widget.cpp Normal file
View File

@ -0,0 +1,24 @@
//
// Created by selim on 10.05.2022.
//
#include "Widget.h"
GtkWidget *gtkpp::Widget::gobj() const {
return _widget;
}
void gtkpp::Widget::setMargins(int margin) {
gtk_widget_set_margin_top(_widget, margin);
gtk_widget_set_margin_bottom(_widget, margin);
gtk_widget_set_margin_start(_widget, margin);
gtk_widget_set_margin_end(_widget, margin);
}
void gtkpp::Widget::setVAlign(GtkAlign align) {
gtk_widget_set_valign(_widget, align);
}
void gtkpp::Widget::setVExpand(bool expand) {
gtk_widget_set_vexpand(_widget, expand);
}

25
gtkpp/Widget.h Normal file
View File

@ -0,0 +1,25 @@
//
// Created by selim on 10.05.2022.
//
#ifndef AUTOCAT_GNOME_WIDGET_H
#define AUTOCAT_GNOME_WIDGET_H
#include <gtk/gtk.h>
namespace gtkpp {
class Widget {
protected:
GtkWidget* _widget;
public:
[[nodiscard]] GtkWidget* gobj() const;
void setMargins(int margin);
void setVAlign(GtkAlign align);
void setVExpand(bool expand);
};
}
#endif //AUTOCAT_GNOME_WIDGET_H

View File

@ -11,21 +11,14 @@
#include <folly/experimental/coro/Task.h>
#include <folly/executors/IOThreadPoolExecutor.h>
LoginWindow::LoginWindow() {
LoginWindow::LoginWindow(): _rootBox(GTK_ORIENTATION_VERTICAL, 0),
_contentBox(GTK_ORIENTATION_VERTICAL, 8) {
setDefaultSize(640, 480);
auto header = adw_header_bar_new();
adw_header_bar_set_title_widget(ADW_HEADER_BAR(header), gtk_label_new("Login"));
auto loginField = gtk_entry_new();
gtk_entry_set_placeholder_text(GTK_ENTRY(loginField), "Email");
gtk_entry_set_input_purpose(GTK_ENTRY(loginField), GTK_INPUT_PURPOSE_EMAIL);
auto passwordField = gtk_entry_new();
gtk_entry_set_placeholder_text(GTK_ENTRY(passwordField), "Password");
gtk_entry_set_input_purpose(GTK_ENTRY(passwordField), GTK_INPUT_PURPOSE_PASSWORD);
gtk_entry_set_visibility(GTK_ENTRY(passwordField), false);
auto loginButton = gtk_button_new();
gtk_button_set_label(GTK_BUTTON(loginButton), "Log in");
gtk_widget_set_margin_top(loginButton, 8);
@ -34,21 +27,25 @@ LoginWindow::LoginWindow() {
auto spinner = gtk_spinner_new();
auto contentBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
gtk_box_append(GTK_BOX(contentBox), loginField);
gtk_box_append(GTK_BOX(contentBox), passwordField);
gtk_box_append(GTK_BOX(contentBox), loginButton);
gtk_box_append(GTK_BOX(contentBox), spinner);
gtk_widget_set_margin_start(contentBox, 48);
gtk_widget_set_margin_end(contentBox, 48);
gtk_widget_set_valign(contentBox, GTK_ALIGN_CENTER);
gtk_widget_set_vexpand(contentBox, true);
_loginEntry.setPlaceholder("Email");
_loginEntry.setPurpose(GTK_INPUT_PURPOSE_EMAIL);
auto rootBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_box_append(GTK_BOX(rootBox), header);
gtk_box_append(GTK_BOX(rootBox), contentBox);
_passwordEntry.setPlaceholder("Password");
_passwordEntry.setPurpose(GTK_INPUT_PURPOSE_PASSWORD);
_passwordEntry.setVisibility(false);
adw_window_set_content(ADW_WINDOW(_window), rootBox);
_contentBox.append(_loginEntry);
_contentBox.append(_passwordEntry);
_contentBox.append(loginButton);
_contentBox.append(spinner);
_contentBox.setMargins(48);
_contentBox.setVAlign(GTK_ALIGN_CENTER);
_contentBox.setVExpand(true);
_rootBox.append(header);
_rootBox.append(_contentBox);
adw_window_set_content(ADW_WINDOW(_window), _rootBox.gobj());
// _emailField.set_placeholder_text("Email");
// _passwordField.set_placeholder_text("Password");

View File

@ -7,6 +7,8 @@
#include <memory>
#include "../gtkpp/Window.h"
#include "../gtkpp/Box.h"
#include "../gtkpp/Entry.h"
class LoginWindow: public gtkpp::Window {
private:
@ -16,6 +18,11 @@ private:
// Gtk::Spinner _spinner;
// std::unique_ptr<Gtk::MessageDialog> _dialog;
gtkpp::Box _rootBox;
gtkpp::Box _contentBox;
gtkpp::Entry _loginEntry;
gtkpp::Entry _passwordEntry;
public:
LoginWindow();