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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
hljs.configure({languages: ['cpp']});
hljs.highlightAll();
</script><title>ColorDialog and ColorChooserDialog</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="chapter-dialogs.html" title="Chapter 17. Dialogs">
<link rel="prev" href="sec-dialogs-filedialog.html" title="FileDialog and FileChooserDialog">
<link rel="next" href="sec-dialogs-fontdialog.html" title="FontDialog and FontChooserDialog">
</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">ColorDialog and ColorChooserDialog</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-dialogs-filedialog.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 17. Dialogs</th>
<td width="20%" align="right"> <a accesskey="n" href="sec-dialogs-fontdialog.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-dialogs-colordialog"></a>ColorDialog and ColorChooserDialog</h2></div></div></div>
<p>
The <code class="classname">ColorChooserDialog</code> (deprecated since <span class="application">gtkmm</span> 4.10) and
<code class="classname">ColorDialog</code> (available since <span class="application">gtkmm</span> 4.10) allow the user
to choose a color. The <code class="classname">ColorButton</code> (deprecated since <span class="application">gtkmm</span> 4.10)
and <code class="classname">ColorDialogButton</code> (available since <span class="application">gtkmm</span> 4.10) open
a color selection dialog when it is clicked.
</p>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1ColorDialog.html" target="_top">ColorDialog Reference</a></p>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1ColorChooserDialog.html" target="_top">ColorChooserDialog Reference</a></p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="colordialog-example"></a>Example</h3></div></div></div>
<div class="figure">
<a name="figure-dialogs-colordialog"></a><p class="title"><b>Figure 17.3. ColorDialog</b></p>
<div class="figure-contents">
<div class="screenshot">
<div class="mediaobject"><img src="figures/dialogs_colordialog.png" alt="ColorDialog"></div>
</div>
</div>
</div>
<br class="figure-break">
<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/dialogs/colordialog" target="_top">Source Code</a></p>
<p>File: <code class="filename">examplewindow.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
~ExampleWindow() override;
protected:
//Signal handlers:
void on_color_button_color_set();
void on_button_dialog_clicked();
void on_dialog_finish(const Glib::RefPtr<Gio::AsyncResult>& result);
//Draw function:
void on_drawing_area_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height);
//Child widgets:
Gtk::Box m_VBox;
Gtk::ColorDialogButton m_ColorDialogButton;
Gtk::Button m_Button_Dialog;
Gtk::DrawingArea m_DrawingArea; //To show the color.
Glib::RefPtr<Gtk::ColorDialog> m_pDialog;
Gdk::RGBA m_Color;
};
#endif //GTKMM_EXAMPLEWINDOW_H
</code></pre>
<p>File: <code class="filename">examplewindow.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include <iostream>
ExampleWindow::ExampleWindow()
: m_VBox(Gtk::Orientation::VERTICAL, 5),
m_Button_Dialog("Choose Color")
{
set_title("Gtk::ColorDialog example");
set_default_size(200, 200);
m_pDialog = Gtk::ColorDialog::create();
m_ColorDialogButton.set_dialog(m_pDialog);
set_child(m_VBox);
m_VBox.append(m_ColorDialogButton);
m_ColorDialogButton.property_rgba().signal_changed().connect(
sigc::mem_fun(*this, &ExampleWindow::on_color_button_color_set));
m_VBox.append(m_Button_Dialog);
m_Button_Dialog.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_dialog_clicked) );
//Set start color:
m_Color.set_red(0.0);
m_Color.set_green(0.0);
m_Color.set_blue(1.0);
m_Color.set_alpha(1.0); //opaque
m_ColorDialogButton.set_rgba(m_Color);
m_VBox.append(m_DrawingArea);
m_DrawingArea.set_expand(true);
m_DrawingArea.set_draw_func(sigc::mem_fun(*this, &ExampleWindow::on_drawing_area_draw));
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_color_button_color_set()
{
//Store the chosen color:
m_Color = m_ColorDialogButton.get_rgba();
m_DrawingArea.queue_draw();
}
void ExampleWindow::on_button_dialog_clicked()
{
m_pDialog->choose_rgba(*this, m_Color,
sigc::mem_fun(*this, &ExampleWindow::on_dialog_finish));
}
void ExampleWindow::on_dialog_finish(const Glib::RefPtr<Gio::AsyncResult>& result)
{
try
{
// If this call changes the color, it will trigger a call to
// on_color_button_color_set().
m_ColorDialogButton.set_rgba(m_pDialog->choose_rgba_finish(result));
// This is what you should do if m_ColorDialogButton and
// on_color_button_color_set() did not exist:
//m_Color = m_pDialog->choose_rgba_finish(result);
//m_DrawingArea.queue_draw();
}
catch (const Gtk::DialogError& err)
{
// Can be thrown by m_pDialog->choose_rgba_finish(result).
std::cout << "No color selected. " << err.what() << std::endl;
}
catch (const Glib::Error& err)
{
std::cout << "Unexpected exception. " << err.what() << std::endl;
}
}
void ExampleWindow::on_drawing_area_draw(const Cairo::RefPtr<Cairo::Context>& cr, int, int)
{
Gdk::Cairo::set_source_rgba(cr, m_Color);
cr->paint();
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include <gtkmm/application.h>
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create("org.gtkmm.example");
//Shows the window and returns when it is closed.
return app->make_window_and_run<ExampleWindow>(argc, argv);
}
</code></pre>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-dialogs-filedialog.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-dialogs.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="sec-dialogs-fontdialog.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">FileDialog and FileChooserDialog </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> FontDialog and FontChooserDialog</td>
</tr>
</table>
</div>
</body>
</html>
|