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