1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Examples</title><meta name="generator" content="DocBook XSL Stylesheets V1.64.1"><link rel="home" href="index.html" title="Programming with gtkmm2"><link rel="up" href="ch09.html" title="Chapter9.TextView"><link rel="previous" href="ch09s02.html" title="Widgets and ChildAnchors"><link rel="next" href="ch10.html" title="Chapter10.Menus and Toolbars"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Examples</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch09s02.html">Prev</a></td><th width="60%" align="center">Chapter9.TextView</th><td width="20%" align="right"><a accesskey="n" href="ch10.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2513027"></a>Examples</h2></div></div><div></div></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2513031"></a>Simple Example</h3></div></div><div></div></div><div class="figure"><a name="figure-textview"></a><p class="title"><b>Figure9.1.TextView</b></p><div class="screenshot"><div><img src="../figures/textview.png" alt="TextView"></div></div></div><p><a href="../../../examples/book/textview/" target="_top">Source Code</a></p><p>File: examplewindow.h
</p><pre class="programlisting">
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
virtual ~ExampleWindow();
protected:
virtual void fill_buffers();
//Signal handlers:
virtual void on_button_quit();
virtual void on_button_buffer1();
virtual void on_button_buffer2();
//Child widgets:
Gtk::VBox m_VBox;
Gtk::ScrolledWindow m_ScrolledWindow;
Gtk::TextView m_TextView;
Glib::RefPtr<Gtk::TextBuffer> m_refTextBuffer1, m_refTextBuffer2;
Gtk::HButtonBox m_ButtonBox;
Gtk::Button m_Button_Quit, m_Button_Buffer1, m_Button_Buffer2;
};
#endif //GTKMM_EXAMPLEWINDOW_H
</pre><p>
</p><p>File: examplewindow.cc
</p><pre class="programlisting">
#include "examplewindow.h"
ExampleWindow::ExampleWindow()
: m_Button_Quit(Gtk::Stock::QUIT),
m_Button_Buffer1("Use buffer 1"),
m_Button_Buffer2("Use buffer 2")
{
set_title("Gtk::TextView example");
set_border_width(5);
set_default_size(400, 200);
add(m_VBox);
//Add the TreeView, inside a ScrolledWindow, with the button underneath:
m_ScrolledWindow.add(m_TextView);
//Only show the scrollbars when they are necessary:
m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
m_VBox.pack_start(m_ScrolledWindow);
//Add buttons:
m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK);
m_ButtonBox.pack_start(m_Button_Buffer1, Gtk::PACK_SHRINK);
m_ButtonBox.pack_start(m_Button_Buffer2, Gtk::PACK_SHRINK);
m_ButtonBox.pack_start(m_Button_Quit, Gtk::PACK_SHRINK);
m_ButtonBox.set_border_width(5);
m_ButtonBox.set_spacing(5);
m_ButtonBox.set_layout(Gtk::BUTTONBOX_END);
//Connect signals:
m_Button_Quit.signal_clicked().connect( SigC::slot(*this, &ExampleWindow::on_button_quit) );
m_Button_Buffer1.signal_clicked().connect( SigC::slot(*this, &ExampleWindow::on_button_buffer1) );
m_Button_Buffer2.signal_clicked().connect( SigC::slot(*this, &ExampleWindow::on_button_buffer2) );
fill_buffers();
on_button_buffer1();
show_all_children();
}
void ExampleWindow::fill_buffers()
{
//Create the first TextBuffer:
m_refTextBuffer1 = Gtk::TextBuffer::create();
m_refTextBuffer1->set_text("This is the text from TextBuffer #1.");
//Create a TagTable:
Glib::RefPtr<Gtk::TextBuffer::TagTable> refTagTable = m_refTextBuffer1->get_tag_table();
//Apply color to some text.
//Create the tags:
Glib::RefPtr<Gtk::TextBuffer::Tag> refTagOrange = Gtk::TextBuffer::Tag::create("example-orange"); //We give it an arbitrary name.
refTagOrange->property_background() = "orange";
refTagTable->add(refTagOrange);
Glib::RefPtr<Gtk::TextBuffer::Tag> refTagBold = Gtk::TextBuffer::Tag::create("example-bold"); //We give it an arbitrary name.
refTagBold->property_weight() = Pango::WEIGHT_BOLD;
refTagTable->add(refTagBold);
//Use a tag:
m_refTextBuffer1->apply_tag(refTagOrange, m_refTextBuffer1->begin(), m_refTextBuffer1->get_iter_at_offset(4));
//Fill the second buffer, using the tags as we add the text:
m_refTextBuffer2 = Gtk::TextBuffer::create(refTagTable); //share the TagTable. A future gtkmm will have a set_tag_table() method.
m_refTextBuffer2->set_text("This is some alternative text, from TextBuffer #2.");
m_refTextBuffer2->insert_with_tag(m_refTextBuffer2->end(), "\nAnd here is some more", refTagOrange);
m_refTextBuffer2->insert_with_tag(m_refTextBuffer2->end(), "\nAnd some more.", refTagBold);
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_button_quit()
{
hide();
}
void ExampleWindow::on_button_buffer1()
{
m_TextView.set_buffer(m_refTextBuffer1);
}
void ExampleWindow::on_button_buffer2()
{
m_TextView.set_buffer(m_refTextBuffer2);
}
</pre><p>
</p><p>File: main.cc
</p><pre class="programlisting">
#include <gtkmm/main.h>
#include "examplewindow.h"
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
ExampleWindow window;
Gtk::Main::run(window); //Shows the window and returns when it is closed.
return 0;
}
</pre><p>
</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch09s02.html">Prev</a></td><td width="20%" align="center"><a accesskey="u" href="ch09.html">Up</a></td><td width="40%" align="right"><a accesskey="n" href="ch10.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Widgets and ChildAnchors</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">Chapter10.Menus and Toolbars</td></tr></table></div></body></html>
|