Showing posts with label gtkmm. Show all posts
Showing posts with label gtkmm. Show all posts

Wednesday, February 17, 2010

How to update a GTK::TextView from a separate object

If you are wondering how to update a text box that was created in your GTK GUI from a separate object then you have come to the right place.  As of this positing date I have searched for the solution to this problem, but there is no real mention on how to do it.  The basic premise is to get the underlying object of the TextView and pass it to the object that wants to edit it.  Then that object must create a pointer to this object and can then access its information.  Anyways lets get to it.

File List:
  • textEdit.cpp - This is where the code is written to update the TextView declared in example.cpp
  • textEdit.h - This is the header file for the code that will update the TextView declared in example.cpp
  • example.cpp - This is where the GUI and TextView are defined

textEdit.h

#include 
using namespace std;

class textEdit{

private:
   GtkTextView *l_text_view;
   Glib::RefPtr l_text_view_buffer;

public:
   textEdit(){l_text_view = NULL; l_text_view_buffer = Gtk::TextBuffer::create();}
   void updateText();
   void setTextViewObject(GtkTextView *text_view){l_text_view = text_view;}
   virtual ~textEdit(){}

}


textEdit.cpp

#include "textEdit.h"

void updateText(){
   string update_words = "Testing, Testing, Testing";

   // Setup a temporary pointer to a TextView
   Gtk::TextView *temp;

   // Get the old buffer and append the new update to it (this can be changed to whatever)
   string bufferNew = update_words + "\n" + l_text_view_buffer->get_text();

  // Set the new buffer text to the actual buffer
  l_text_view_buffer->set_text(bufferNew);

  // Take the l_text_view and assign this pointer to temp
  temp = Glib::wrap(l_text_view,false);

  // Update the temp TextView with the buffer
  temp->set_buffer(l_text_view_buffer);
}


example.cpp

.
.
.

// Create the object
textEdit textEdit;

// Send the text_view object.  This would be defined in you header file as such:
//      Gtk::TextView text_view;
textEdit.setTextViewObject(text_view.gobj)

.
.
.

Wednesday, August 26, 2009

Working with gtkmm - A basic intro

I am not attempting to know everything about gtkmm or post an exhaustive tutorial on it. However if you would like to create a basic GUI in a relatively short period of time then you have come to the right place.

Here is a link to a more exhaustive tutorial (where I learned myself):Programming with gtkmm

Also visit this link which has detailed information on each class/namespace/file:
gtkmm 2.4 Documentation

Quick Introduction:

Abstract:
This tutorial covers how to create a GUI including one button arranged within a single frame. In another post I will eventually cover the basics of how to create a "more" section (i.e. hidden widgets). This is a very basic GUI that simply allows you to run commands depending on the callback of the button.

Idea:
The basic idea of gtkmm (GTK in general) is to create a gui where all elements are relative in position and size (for the most part). Each element is considered a widget and the things that hold it, containers. Elements are typically considered children, and containers parent(s).

Main.h
Add:
#include 
#include "gui.h"


Main.cpp
Add:
Gtk::Main kit(argc,argv);
gui gui;
Gtk::Main::run(gui);


gui.h
Add:
class gui : public Gtk::Window
{
public:
gui();
virtual ~gui();

protected:
//Signal handlers:
virtual void on_button_clicked();

//WINDOW - Child widgets
Gtk::Frame window_frame;
Gtk::VBox window_box;
Gtk::Button window_button,

gui.cpp
Add:
// Sets the Window Title
set_title("Window Title");

// Sets the border width of the window
set_border_width(10);

// Add the box to the window (the window can contain one widget/container)
add(window_box);

// Add the frame to the box (boxes can contain multiple widgets/containers)
window_box.pack_start(window_frame);

// Label the frame
window_frame.set_label("Main");

// Add a button to the frame (frames can contain one widget/container)
window_frame.add(button);

// This is how you do a call back (to a function where you define whatever you want)
button.signal_clicked().connect(sigc::mem_fun(*this,
&gui::on_button_clicked));


That's it. Just define your on_button_clicked() function and you are all set to make a simple GUI.