]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GXpmBtnTbl.C
Change glob() API to accept a dir parameter.
[lyx.git] / src / frontends / gtk / GXpmBtnTbl.C
1 /**
2  * \file GXpmBtnTbl.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Huang Ying
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 // Too hard to make concept checks work with this file
14 #ifdef _GLIBCPP_CONCEPT_CHECKS
15 #undef _GLIBCPP_CONCEPT_CHECKS
16 #endif
17
18 #include "GXpmBtnTbl.h"
19
20 #include <gtkmm.h>
21
22 #include <boost/assert.hpp>
23
24
25 void GXpmBtnTbl::GXpmBtn::setXpm(XpmData xpm)
26 {
27         Glib::RefPtr<Gdk::Colormap> clrmap = get_colormap();
28         pixmap_ = Gdk::Pixmap::create_from_xpm(clrmap,
29                                                mask_,
30                                                xpm);
31         Gtk::Image * image = Gtk::manage(new Gtk::Image(pixmap_, mask_));
32         image->show();
33         add(*image);
34 }
35
36
37 void GXpmBtnTbl::GXpmBtn::setXpm(Glib::RefPtr<Gdk::Pixmap> pixmap,
38                                  Glib::RefPtr<Gdk::Bitmap> mask)
39 {
40         pixmap_ = pixmap;
41         mask_ = mask;
42         Gtk::Image * image =
43                 Gtk::manage(new Gtk::Image(pixmap_, mask_));
44         image->show();
45         add(*image);
46 }
47
48
49 GXpmBtnTbl::GXpmBtnTbl(int rows, int cols, XpmData xpms[]) :
50         Gtk::Table(rows, cols, true), rows_(rows), cols_(cols),
51         xbm_(0)
52 {
53         construct();
54         setBtnXpm(xpms);
55 }
56
57
58 GXpmBtnTbl::GXpmBtnTbl(int rows, int cols, XbmData const & xbm) :
59         Gtk::Table(rows, cols, true), rows_(rows), cols_(cols),
60         xbm_(&xbm)
61 {
62         construct();
63 }
64
65
66 GXpmBtnTbl::~GXpmBtnTbl()
67 {
68 }
69
70
71 void GXpmBtnTbl::construct()
72 {
73         BOOST_ASSERT(rows_);
74         BOOST_ASSERT(cols_);
75         btns_.reset(new GXpmBtn[rows_ * cols_]);
76         BOOST_ASSERT(btns_.get());
77
78         for (int row = 0; row < rows_; ++row)
79                 for (int col = 0; col < cols_; ++col) {
80                         GXpmBtn * btn = &btns_[index(row, col)];
81                         btn->setRow(row);
82                         btn->setCol(col);
83                         btn->signalClicked().connect(signalClicked_);
84                         btn->show();
85                         attach(*btn, col, col + 1,  row, row + 1);
86                 }
87 }
88
89
90 void GXpmBtnTbl::setBtnXpm(XpmData xpms[])
91 {
92         for (int row = 0; row < rows_; ++row)
93                 for (int col = 0; col < cols_; ++col)
94                         btns_[index(row, col)].setXpm(xpms[index(row, col)]);
95 }
96
97
98 void GXpmBtnTbl::setBtnXpm(XbmData const & xbm)
99 {
100         Glib::RefPtr<Gdk::Colormap> clrmap = get_colormap();
101         Gdk::Color fg(const_cast<GdkColor *>(&xbm.fg_));
102         clrmap->alloc_color(fg);
103         Glib::RefPtr<Gdk::Window> window = get_window();
104
105         Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create_from_data(
106                 window,
107                 reinterpret_cast<char const * const>(xbm.data_),
108                 xbm.width_,
109                 xbm.height_,
110                 window->get_depth(),
111                 fg,
112                 get_style()->get_bg(Gtk::STATE_NORMAL));
113
114         Glib::RefPtr<Gdk::Bitmap> mask = Gdk::Bitmap::create(
115                 window,
116                 reinterpret_cast<char const * const>(xbm.data_),
117                 xbm.width_,
118                 xbm.height_);
119
120
121
122         Glib::RefPtr<Gdk::GC> gc = Gdk::GC::create(mask);
123         int const btnWidth = xbm.width_ / cols_;
124         int const btnHeight = xbm.height_ / rows_;
125         for (int row = 0; row < rows_; ++row)
126                 for (int col = 0; col < cols_; ++col) {
127                                 Glib::RefPtr<Gdk::Pixmap> pixmapBtn =
128                                         Gdk::Pixmap::create(
129                                 window,
130                                 btnWidth,
131                                 btnHeight,
132                                 window->get_depth());
133                         pixmapBtn->draw_drawable(get_style()->get_black_gc(),
134                                                  pixmap,
135                                                  col * btnWidth,
136                                                  row * btnHeight,
137                                                  0,
138                                                  0,
139                                                  btnWidth,
140                                                  btnHeight);
141                                 Glib::RefPtr<Gdk::Bitmap> maskBtn =
142                                         Gdk::Bitmap::create(
143                                 window,
144                                 reinterpret_cast<char const * const>(xbm.data_),
145                                 btnWidth,
146                                 btnHeight);
147                         maskBtn->draw_drawable(gc,
148                                                mask,
149                                                col * btnWidth,
150                                                row * btnHeight,
151                                                0,
152                                                0,
153                                                btnWidth,
154                                                btnHeight);
155                         btns_[index(row, col)].setXpm(pixmapBtn, maskBtn);
156                 }
157 }
158
159
160 void GXpmBtnTbl::on_realize()
161 {
162         Gtk::Table::on_realize();
163         if (!xbm_)
164                 return;
165         setBtnXpm(*xbm_);
166 }
167
168
169 void buttonSetXpm(Gtk::Button * btn, char const ** xpm)
170 {
171         Glib::RefPtr<Gdk::Colormap> clrmap = btn->get_colormap();
172
173         Glib::RefPtr<Gdk::Bitmap> mask;
174         Glib::RefPtr<Gdk::Pixmap> pixmap =
175                 Gdk::Pixmap::create_from_xpm(clrmap, mask, xpm);
176         Gtk::Image * image = Gtk::manage(new Gtk::Image(pixmap, mask));
177         image->show();
178         btn->add(*image);
179 }