Ali
Ali

Reputation: 7

Increase the font size of a TextView in gtkmm-4.0

Assume that We have this simple application, a Gtk::TextView inside a window. I use gtkmm-4.0

mainwindow.h

#pragma once
#include <gtkmm.h>
class mainwindow : public Gtk::Window
{
public:
    mainwindow();

protected:
    Gtk::TextView myTextEntry;
};

mainwindow.cpp

#include "mainwindow.h"

mainwindow::mainwindow()
{
    this->set_default_size(300,300);
    this->set_child(myTextEntry);
}

main.cpp

#include <gtkmm.h>
#include "mainwindow.h"

int main (int argc, char** argv)
{
    auto app = Gtk::Application::create();

    return app->make_window_and_run<mainwindow>(argc,argv);
}

here is the output:

Screenshot

I wanna make the font size of the myTextEntry a bit larger how can i do that?

Thanks.

Upvotes: -1

Views: 440

Answers (1)

Ali
Ali

Reputation: 7

OK.This is how I did it. I had to style my app using the Gtk::CssProvider class. basically you define a class with a name in css language. you name your widget using name() method it (both with same name) then give that css class to your wigdet using get_style_context()->add_provider():

In My mainwindow.h to the protected: section add Glib::RefPtr<Gtk::CssProvider> provider;

then i went to the body the constructor in mainwindow.cpp and added this:

provider=Gtk::CssProvider::create();
provider->load_from_data("#MainCodeText{"
        "font-size:22px;"
    "}");
myTextEntry.set_name("MainCodeText");
myTextEntry.get_style_context()->add_provider(TextViewStyle,1);

note that you can use another methods of CssProvider to import your css style you can add it from file (load_from_file) or a path (load_from_path) or a simple string like I did. so for my example that code i mentioned in the above is something like this.

mainwindow.h

#pragma once
#include <gtkmm.h>
class mainwindow : public Gtk::Window
{
public:
    mainwindow();

protected:
    Gtk::TextView myTextEntry;
    Glib::RefPtr<Gtk::CssProvider> provider;
};


mainwindow.cpp

#include "mainwindow.h"

mainwindow::mainwindow()
{
    this->set_default_size(300,300);
    this->set_child(myTextEntry);
    provider=Gtk::CssProvider::create();
    provider->load_from_data("#MainCodeText{"
        "font-size:22px;"
    "}");
myTextEntry.set_name("MainCodeText");
myTextEntry.get_style_context()->add_provider(TextViewStyle,1);
}

main.cpp

#include <gtkmm.h>
#include "mainwindow.h"

int main (int argc, char** argv)
{
    auto app = Gtk::Application::create();

    return app->make_window_and_run<mainwindow>(argc,argv);
}

Upvotes: 0

Related Questions