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