File: ch06s05.html

package info (click to toggle)
gtkmm2.0 2.2.12-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 56,872 kB
  • ctags: 51,600
  • sloc: xml: 73,173; cpp: 20,565; sh: 8,608; perl: 2,702; makefile: 1,233
file content (190 lines) | stat: -rw-r--r-- 8,332 bytes parent folder | download | duplicates (2)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>ProgressBar</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="ch06.html" title="Chapter6.Miscellaneous Widgets"><link rel="previous" href="ch06s04.html" title="SpinButton"><link rel="next" href="ch06s06.html" title="Tooltips"></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">ProgressBar</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06s04.html">Prev</a></td><th width="60%" align="center">Chapter6.Miscellaneous Widgets</th><td width="20%" align="right"><a accesskey="n" href="ch06s06.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="sec-ProgressBar"></a>ProgressBar</h2></div></div><div></div></div><p>
Progress bars are used to show the status of an ongoing operation. For instance, a <tt class="literal">ProgressBar</tt> can show how much of a task has been completed.
</p><p>To change the value shown, use the <tt class="literal">set_fraction()</tt> method, passing a double between 0 and 1 to provide the new percentage.</p><p>
where <tt class="literal">percentage</tt> is a number, from 0 to 1, indicating what
fraction of the bar should be filled.
</p><p>
A <tt class="literal">ProgressBar</tt>is horizontal and left-to-right by default, but you can change it to a vertical progress bar by using the <tt class="literal">set_orientation()</tt> method.
</p><p><a href="../../reference/html/classGtk_1_1ProgressBar.html" target="_top">Reference</a></p><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2505919"></a>Activity Mode</h3></div></div><div></div></div><p>
Besides indicating the amount of progress that has occured, the
progress bar can also be used to indicate that there is some activity;
this is done by placing the progress bar in <span class="emphasis"><em>activity mode</em></span>.  In
this mode, the progress bar displays a small rectangle which moves
back and forth.  Activity mode is useful in situations where the
progress of an operation cannot be calculated as a value range (e.g.,
receiving a file of unknown length).
</p><p>
To do this, you need to call the <tt class="literal">pulse()</tt> method at regular intervals. You can also choose the step size, with the <tt class="literal">set_pulse_step()</tt> method.
</p><p>
When in continuous mode, the progress bar can also display a
configurable text string within its trough, using the <tt class="literal">set_text()</tt> method.
</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2505968"></a>Example</h3></div></div><div></div></div><div class="figure"><a name="figure-progressbar"></a><p class="title"><b>Figure6.5.ProgressBar</b></p><div class="screenshot"><div><img src="../figures/progressbar.png" alt="ProgressBar"></div></div></div><p><a href="../../../examples/book/progressbar" target="_top">Source Code</a></p><p>File: examplewindow.h
</p><pre class="programlisting">
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include &lt;gtkmm.h&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow();
  virtual ~ExampleWindow();

protected:
  //Signal handlers:
  virtual void on_checkbutton_text();
  virtual void on_checkbutton_activity();
  virtual void on_checkbutton_orientation();
  virtual bool on_timeout();
  virtual void on_button_close();

  //Child widgets:
  Gtk::VBox m_VBox;
  Gtk::Alignment m_Alignment;
  Gtk::Table m_Table;
  Gtk::ProgressBar m_ProgressBar;
  Gtk::HSeparator m_Separator;
  Gtk::CheckButton m_CheckButton_Text, m_CheckButton_Activity, m_CheckButton_Orientation;
  Gtk::Button m_Button_Close;

  int m_connection_id_timeout;
  bool m_bActivityMode;
};

#endif //GTKMM_EXAMPLEWINDOW_H
</pre><p>
</p><p>File: examplewindow.cc
</p><pre class="programlisting">
#include "examplewindow.h"
#include &lt;iostream&gt;

ExampleWindow::ExampleWindow()
: m_VBox(false, 5),
  m_Alignment(0.5, 0.5, 0, 0),
  m_Table(2, 2, true),
  m_CheckButton_Text("Show text"),
  m_CheckButton_Activity("Activity mode"),
  m_CheckButton_Orientation("Right to Left"),
  m_Button_Close("Close"),
  m_bActivityMode(false)
{
  set_resizable();
  set_title("Gtk::ProgressBar");

  m_VBox.set_border_width(10);
  add(m_VBox);

  m_VBox.pack_start(m_Alignment, Gtk::PACK_SHRINK, 5);
  m_Alignment.add(m_ProgressBar);

  //Add a timer callback to update the value of the progress bar:
  m_connection_id_timeout = Glib::signal_timeout().connect( SigC::slot(*this, &amp;ExampleWindow::on_timeout), 50 );

  m_VBox.pack_start(m_Separator, Gtk::PACK_SHRINK);
  m_VBox.pack_start(m_Table);

  //Add a check button to select displaying of the trough text:
  m_Table.attach(m_CheckButton_Text, 0, 1, 0, 1, Gtk::EXPAND | Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 5, 5);
  m_CheckButton_Text.signal_clicked().connect( SigC::slot(*this, &amp;ExampleWindow::on_checkbutton_text) );

  //Add a check button to select displaying of the trough text:
  m_Table.attach(m_CheckButton_Activity, 0, 1, 1, 2, Gtk::EXPAND | Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 5, 5);
  m_CheckButton_Activity.signal_clicked().connect( SigC::slot(*this, &amp;ExampleWindow::on_checkbutton_activity) );

  //Add a check button to toggle activity mode:
  m_Table.attach(m_CheckButton_Orientation, 0, 1, 2, 3, Gtk::EXPAND | Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 5, 5);
  m_CheckButton_Orientation.signal_clicked().connect( SigC::slot(*this, &amp;ExampleWindow::on_checkbutton_orientation) );

  //Add a button to exit the program.
  m_VBox.pack_start(m_Button_Close, Gtk::PACK_SHRINK);
  m_Button_Close.signal_clicked().connect( SigC::slot(*this, &amp;ExampleWindow::on_button_close) );
  m_Button_Close.set_flags(Gtk::CAN_DEFAULT);
  m_Button_Close.grab_default();

  show_all_children();
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_checkbutton_text()
{
  const Glib::ustring text = m_ProgressBar.get_text();

  if(!text.empty())
    m_ProgressBar.set_text("");
  else
    m_ProgressBar.set_text("some text");
}

void ExampleWindow::on_checkbutton_activity()
{
  m_bActivityMode = m_CheckButton_Activity.get_active();

  if(m_bActivityMode)
    m_ProgressBar.pulse();
  else
    m_ProgressBar.set_fraction(0.0);
}

void ExampleWindow::on_checkbutton_orientation()
{
  switch(m_ProgressBar.get_orientation())
  {
    case Gtk::PROGRESS_LEFT_TO_RIGHT:
      m_ProgressBar.set_orientation(Gtk::PROGRESS_RIGHT_TO_LEFT);
      break;
    case Gtk::PROGRESS_RIGHT_TO_LEFT:
      m_ProgressBar.set_orientation(Gtk::PROGRESS_LEFT_TO_RIGHT);
      break;
    default:
      break; // do nothing	
  }
}

void ExampleWindow::on_button_close()
{
  hide();
}

/* Update the value of the progress bar so that we get
 * some movement */
bool ExampleWindow::on_timeout()
{
  if(m_bActivityMode)
    m_ProgressBar.pulse();
  else
  {
    double new_val = m_ProgressBar.get_fraction() + 0.01;

    if(new_val &gt; 1.0)
      new_val = 0.0;

    //Set the new value:
    m_ProgressBar.set_fraction(new_val);
  }

  //As this is a timeout function, return true so that it
  //continues to get called
  return true;
}

</pre><p>
</p><p>File: main.cc
</p><pre class="programlisting">
#include &lt;gtkmm/main.h&gt;
#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="ch06s04.html">Prev</a></td><td width="20%" align="center"><a accesskey="u" href="ch06.html">Up</a></td><td width="40%" align="right"><a accesskey="n" href="ch06s06.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">SpinButton</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">Tooltips</td></tr></table></div></body></html>