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