LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2019, 05:52 PM   #1
LastC
LQ Newbie
 
Registered: Jan 2018
Location: Montreal
Distribution: Opensuse, Ubuntu Server, KDE Neon
Posts: 9

Rep: Reputation: Disabled
Error getting cairo context from DrawingArea (Gtkmm/glade)


I need to display an image on a DrawingArea widget from a .glade file for my application, but as soon as a I try to create a Cario::Context from the application window...
Code:
builder->get_widget( "main_da", focus.main_da );
...
Cairo::RefPtr<Cairo::Context> main_context = focus.main_da->get_window()->create_cairo_context();
I get a
Code:
fish: “./vulcanus” terminated by signal SIGSEGV (Address boundary error)
error when I try to run the executable. I added a few std::couts to see where the issue is, until I finally landed on the line with the create_cairo_context() function. I've been following this example for this part of the program, but I haven't been able to find a way to fix this issue yet.

Is there something I'm missing or doing wrong here? Do I need to create the widget and then add it to the window instead?

Here's the rest of the code
Code:
//vulcanus.cpp
#include "vulcanus.hpp"

using namespace Gtk;

namespace Vulcanus{

    Parser::Parser parser(CONTROL_INDEX_MAX);

    Vulcanus::Vulcanus( Glib::RefPtr<Gtk::Builder> builder  ){
        //Get widgets from glade file
        builder->get_widget( "main_window", focus.main_window );
        builder->get_widget( "main_da", focus.main_da );
        builder->get_widget( "msg_log", focus.msg_log );

        Cairo::RefPtr<Cairo::Context> main_context = focus.main_da->get_window()->create_cairo_context();

        if(!parser.init())
            Vulcanus::on_main_window_destroy( );
        else if(!parser.parse_settings())
            Vulcanus::on_main_window_destroy( );

        for(int i = 0; i < CONTROL_INDEX_MAX; i++){
            builder->get_widget(Glib::ustring(std::string(CONTROL_BUTTON_INDEX_INIT)
            .append(std::to_string(i))), controls[i].button);
            builder->get_widget(Glib::ustring(std::string(CONTROL_LABEL_INDEX_INIT)
            .append(std::to_string(i))), controls[i].label);
            builder->get_widget(Glib::ustring(std::string(CONTROL_LEVEL_BAR_INDEX_INIT)
            .append(std::to_string(i))), controls[i].level_bar);
            controls[i].label->set_label(Glib::ustring(std::to_string(0)));
            controls[i].button->set_label(Glib::ustring(parser.configs.at(i).name));
            controls[i].min_value = parser.configs.at(i).min_value;
            controls[i].max_value = parser.configs.at(i).max_value;
            controls[i].trigger_warning = parser.configs.at(i).trigger_warning;
            std::cout << controls[i].max_value << std::endl;
            controls[i].setup_bar();
        }
    }


    Vulcanus::~Vulcanus( void ){}

    void Vulcanus::on_main_window_destroy( void ){
        Gtk::Main::quit();
    }
}
Code:
//vulcanus.hpp
#ifndef VULCANUS_HPP_
#define VULCANUS_HPP_

#include <stdio.h>
#include <stdlib.h>
#include <gtkmm.h>
#include <gtk/gtk.h>
#include <gtkmm/glarea.h>
#include <giomm/resource.h>
#include <epoxy/gl.h>

#include "parser.hpp"

#define CONTROL_BUTTON_INDEX_INIT "control_button_"
#define CONTROL_LABEL_INDEX_INIT "control_label_"
#define CONTROL_LEVEL_BAR_INDEX_INIT "control_level_bar_"
#define CONTROL_INDEX_MAX 24

using namespace Gtk;

namespace Vulcanus{

    //Main window widgets
    struct main_widgets{
        Window *main_window;
        DrawingArea *main_da;
        TextView *msg_log;

    public:
        //Delete widgets
        ~main_widgets(){
            del_widget( main_window );
            del_widget( main_da );
            del_widget( msg_log );
        }

    private:
        void del_widget(auto ptr){
            if(ptr != NULL)
                delete ptr;
        }

    };

    //Control widgets
    struct control_widgets{
        Label * label;
        Button * button;
        LevelBar * level_bar;
        double trigger_warning, min_value, max_value;
        bool warning, error;

    public:
        //Set the bar values
        bool setup_bar( void ){
            if(!min_value || !max_value)
                return false;
            this->level_bar->set_min_value( min_value );
            this->level_bar->set_max_value( max_value );
            return true;
        }
        void set_bar( double val ){
            this->level_bar->set_value(val);
        }
        //Delete widgets
        ~control_widgets(){
            del_widget(label);
            del_widget(button);
            del_widget(level_bar);
        }

    private:
        void del_widget(auto ptr){
            if(ptr != NULL)
                delete ptr;
        }

    };

    //Main vulcanus class
    class Vulcanus{
    protected:
        control_widgets controls[CONTROL_INDEX_MAX];
        void update_drawing_area( main_widgets wg );
        //static gboolean render(GtkGLArea *area, GdkGLContext *context);

    public:
        void on_main_window_destroy( void );
        Vulcanus( Glib::RefPtr<Gtk::Builder> builder );
        ~Vulcanus( void );
        main_widgets focus;

    };
}

#endif
Code:
//main.cpp
#include <iostream>
#include <gtkmm.h>
#include "vulcanus.hpp"

#define GLADE_FILE "glade/vulcanus.glade"

int main(int argc, char *argv[]) {
    Glib::RefPtr<Gtk::Application> app = Gtk::Application::create( argc, argv );
    Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create();
    try{
        builder->add_from_file(GLADE_FILE);
    }catch(const Glib::FileError &ex){
        std::cerr << "FileError: " << ex.what() << std::endl;
        return 1;
    }
    Vulcanus::Vulcanus vulcanus( builder );
    app->run( *vulcanus.focus.main_window );
    return 0;
}
Note: Parser is just another class that parses a settings file for the app, but it does not have to deal with Gtk. I didn't included it because I didn't wanted to add too much info that might not be relevant to the question.
Note 2: I removed the displaying image part since, well, I can't display it without the context

Last edited by LastC; 07-24-2019 at 05:54 PM.
 
  


Reply

Tags
c++, glade, gtk+, gtkmm


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Slackware- GTK 3 with GTKMM 3 and cairo - some problems! PeterUK Slackware 3 05-06-2013 10:00 AM
LXer: How to Add Custom gtkmm Widget to Glade LXer Syndicated Linux News 0 06-16-2012 01:33 AM
[SOLVED] pygtk Gtk.DrawingArea - no expose event RockDoctor Programming 1 10-01-2011 07:43 AM
A little help with Glade 3 and gtkmm gfmartin05 Programming 2 12-18-2009 11:37 AM
ruby/gtk2 drawingarea Komakino Programming 3 07-06-2006 10:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration