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