add new sigc++2 directory
[ardour.git] / libs / gtkmm2 / gtk / src / style.ccg
1 // -*- c++ -*-
2 /* $Id: style.ccg,v 1.5 2006/11/23 14:47:48 murrayc Exp $ */
3
4 /* Copyright 1998-2002 The gtkmm Development Team
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include <gtkmm/widget.h>
22 #include <gtkmm/rc.h>
23 #include <gtk/gtkstyle.h>
24
25
26 namespace Gtk
27 {
28
29 /* 
30 Design notes:
31
32 Okay these are my notes on how a GtkStyle works.
33 They are not meant to be taken as documentation as I didn't
34 write the code.  
35
36 styles keeps a copy of itself for each color depth.  Thus
37 changing a style after it has been attached wont work!
38
39 At allocation time a Gtk Style has 
40   - all GC set to 0 as they will be allocated later
41   - has all color values set to default values.
42   - a ref count of 1 and an attach count of 0 (floating?)
43   - a properly referenced font.
44   - colormap and depth are invalid.
45   - The style list and rcstyle are 0. (??? styles wasn't set explicitly!)
46
47 It returns to this state if the style is detatched from
48 all widgets.
49
50 Attach acts to sink the object removing it from the floating state.
51
52 attaching a style for the first time initializes it. 
53 Initializing a style
54   - sets the colormap and depth.
55   - sets the mid colors. (thus allowing user to set these would be pointless)
56   - gets black and white from the colormap.
57   - allocates all the colors.
58   - uses gtk_gc_get to share a gc if there is a matching one.
59
60 Conclusions, we need to rework the concept of Gdk to allow
61 for const objects.  
62
63 */
64
65 void Style::set_font(const Pango::FontDescription& font_desc)
66 {
67   g_return_if_fail(font_desc.gobj() != 0);
68
69   // It will be freed when it goes out of scope.
70   const Pango::FontDescription fontBefore (gobj()->font_desc, false);
71
72   gobj()->font_desc = font_desc.gobj_copy();
73 }
74
75 Pango::FontDescription Style::get_font() const
76 {
77   // Direct struct access seems the only way.
78   return Pango::FontDescription(gobj()->font_desc, true); // true = make a copy.
79 }
80
81 Glib::RefPtr<Gdk::GC> Style::get_fg_gc(StateType state_type)
82 {
83   return Glib::wrap(gobj()->fg_gc[state_type], true);
84 }
85
86 Glib::RefPtr<const Gdk::GC> Style::get_fg_gc(StateType state_type) const
87 {
88   return Glib::wrap(gobj()->fg_gc[state_type], true);
89 }
90
91 Glib::RefPtr<Gdk::GC> Style::get_bg_gc(StateType state_type)
92 {
93   return Glib::wrap(gobj()->bg_gc[state_type], true);
94 }
95
96 Glib::RefPtr<const Gdk::GC> Style::get_bg_gc(StateType state_type) const
97 {
98   return Glib::wrap(gobj()->bg_gc[state_type], true);
99 }
100
101 Glib::RefPtr<Gdk::GC> Style::get_light_gc(StateType state_type)
102 {
103   return Glib::wrap(gobj()->light_gc[state_type], true);
104 }
105
106 Glib::RefPtr<const Gdk::GC> Style::get_light_gc(StateType state_type) const
107 {
108   return Glib::wrap(gobj()->light_gc[state_type], true);
109 }
110
111 Glib::RefPtr<Gdk::GC> Style::get_dark_gc(StateType state_type)
112 {
113   return Glib::wrap(gobj()->dark_gc[state_type], true);
114 }
115
116 Glib::RefPtr<const Gdk::GC> Style::get_dark_gc(StateType state_type) const
117 {
118   return Glib::wrap(gobj()->dark_gc[state_type], true);
119 }
120
121 Glib::RefPtr<Gdk::GC> Style::get_mid_gc(StateType state_type)
122 {
123   return Glib::wrap(gobj()->mid_gc[state_type], true);
124 }
125
126 Glib::RefPtr<const Gdk::GC> Style::get_mid_gc(StateType state_type) const
127 {
128   return Glib::wrap(gobj()->mid_gc[state_type], true);
129 }
130
131 Glib::RefPtr<Gdk::GC> Style::get_text_gc(StateType state_type)
132 {
133   return Glib::wrap(gobj()->text_gc[state_type], true);
134 }
135
136 Glib::RefPtr<const Gdk::GC> Style::get_text_gc(StateType state_type) const
137 {
138   return Glib::wrap(gobj()->text_gc[state_type], true);
139 }
140
141 Glib::RefPtr<Gdk::GC> Style::get_base_gc(StateType state_type)
142 {
143   return Glib::wrap(gobj()->base_gc[state_type], true);
144 }
145
146 Glib::RefPtr<const Gdk::GC> Style::get_base_gc(StateType state_type) const
147 {
148   return Glib::wrap(gobj()->base_gc[state_type], true);
149 }
150
151 _DEPRECATE_IFDEF_START
152 Gdk::Color Style::get_background(StateType state_type) const
153 {
154   GdkColor* pGdkColor = const_cast<GdkColor*>(&(gobj()->bg[state_type]));
155   return Glib::wrap(pGdkColor, true); //true = take_copy.
156 }
157
158 Glib::RefPtr<Gdk::Pixmap> Style::get_background_pixmap(StateType state_type)
159 {
160   return Glib::wrap((GdkPixmapObject*) (gobj()->bg_pixmap[state_type]), true); //true = take_copy.
161 }
162
163 void Style::set_bg_pixmap(StateType state_type, const Glib::RefPtr<Gdk::Pixmap>& pixmap)
164 {
165   GdkPixmap *const old_pixmap = gobj()->bg_pixmap[state_type];
166   gobj()->bg_pixmap[state_type] = Glib::unwrap_copy(pixmap);
167
168   if(old_pixmap)
169     g_object_unref(old_pixmap);
170 }
171 _DEPRECATE_IFDEF_END
172
173 void Style::set_bg_pixmap(StateType state_type, const Glib::RefPtr<const Gdk::Pixmap>& pixmap)
174 {
175   GdkPixmap *const old_pixmap = gobj()->bg_pixmap[state_type];
176   Glib::RefPtr<Gdk::Pixmap> pixmap_unconst = Glib::RefPtr<Gdk::Pixmap>::cast_const(pixmap);
177   gobj()->bg_pixmap[state_type] = Glib::unwrap_copy(pixmap_unconst); //unwrap_copy() needs a non-const.
178
179   if(old_pixmap)
180     g_object_unref(old_pixmap);
181 }
182
183 Glib::RefPtr<Gdk::Pixmap> Style::get_bg_pixmap(StateType state_type)
184 {
185   return Glib::wrap((GdkPixmapObject*) (gobj()->bg_pixmap[state_type]), true); // true = take_copy
186 }
187
188 Glib::RefPtr<const Gdk::Pixmap> Style::get_bg_pixmap(StateType state_type) const
189 {
190   return Glib::wrap((GdkPixmapObject*) (gobj()->bg_pixmap[state_type]), true); // true = take_copy
191 }
192
193 void Style::set_xthickness(int xthickness)
194 {
195   gobj()->xthickness = xthickness;
196 }
197
198 void Style::set_ythickness(int ythickness)
199 {
200   gobj()->ythickness = ythickness;
201 }
202
203 void Style::paint_polygon(const Glib::RefPtr<Gdk::Window>&      window,
204                           StateType                             state_type,
205                           ShadowType                            shadow_type,
206                           const Gdk::Rectangle&                 area,
207                           Widget&                               widget,
208                           const Glib::ustring&                  detail,
209                           const Glib::ArrayHandle<Gdk::Point>&  points,
210                           bool                                  fill) const
211 {
212   gtk_paint_polygon(
213       const_cast<GtkStyle*>(gobj()),
214       Glib::unwrap(window),
215       (GtkStateType) state_type,
216       (GtkShadowType) shadow_type,
217       const_cast<GdkRectangle*>(area.gobj()),
218       widget.gobj(),
219       detail.c_str(),
220       reinterpret_cast<GdkPoint*>(const_cast<Gdk::Point*>(points.data())),
221       points.size(),
222       fill);
223 }
224
225 IconSet Style::lookup_icon_set(const Gtk::StockID& stock_id)
226 {
227   GtkIconSet* pIconSet = gtk_style_lookup_icon_set(gobj(), stock_id.get_c_str());
228   return IconSet(pIconSet, true); //true = take_copy.
229
230 }
231
232 } // namespace Gtk
233