Merge lp:~victored/granite/mode-button-cleanup into lp:~elementary-pantheon/granite/granite

Proposed by Victor Martinez
Status: Merged
Merged at revision: 169
Proposed branch: lp:~victored/granite/mode-button-cleanup
Merge into: lp:~elementary-pantheon/granite/granite
Diff against target: 322 lines (+59/-150)
2 files modified
demo/main.vala (+9/-9)
lib/Widgets/ModeButton.vala (+50/-141)
To merge this branch: bzr merge lp:~victored/granite/mode-button-cleanup
Reviewer Review Type Date Requested Status
xapantu Pending
Review via email: mp+89970@code.launchpad.net

Description of the change

See lp:914502

Granite.Widgets.ModeButton
* Code cleanup (this includes coding style fixes)
* Reworked symbolic icon support to use GTK's built-in feature

Granite Demo:
* Added symbolic icons to the toolbar's mode button
* Updated welcome screen text

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'demo/main.vala'
--- demo/main.vala 2012-01-11 20:08:44 +0000
+++ demo/main.vala 2012-01-24 18:29:25 +0000
@@ -69,7 +69,7 @@
69 /* welcome */69 /* welcome */
7070
71 // These strings will be automatically corrected by the widget71 // These strings will be automatically corrected by the widget
72 var welcome = new Welcome("welcome widget", "description text");72 var welcome = new Welcome("Granite's Welcome Screen", "This is Granite's Welcome widget.");
73 notebook.append_page(welcome, new Gtk.Label("Welcome"));73 notebook.append_page(welcome, new Gtk.Label("Welcome"));
7474
75 Gdk.Pixbuf? pixbuf = null;75 Gdk.Pixbuf? pixbuf = null;
@@ -84,10 +84,10 @@
84 Gtk.Image? image = new Gtk.Image.from_icon_name("document-open", Gtk.IconSize.DIALOG);84 Gtk.Image? image = new Gtk.Image.from_icon_name("document-open", Gtk.IconSize.DIALOG);
8585
86 // Adding elements. Use the most convenient function to add an icon86 // Adding elements. Use the most convenient function to add an icon
87 welcome.append_with_pixbuf(pixbuf, "create", "write a new document");87 welcome.append_with_pixbuf(pixbuf, "Create", "Write a new document.");
88 welcome.append_with_image(image, "open", "select a file");88 welcome.append_with_image(image, "Open", "select a file.");
89 welcome.append("document-save", "save", "with a much longer description");89 welcome.append("document-save", "Save", "With a much longer description.");
90 welcome.append("help-info", "Discover", "Learn more about this app");90 welcome.append("help-info", "Discover", "Learn more about this application.");
9191
92 /* modebutton */92 /* modebutton */
93 var mode_button = new ModeButton();93 var mode_button = new ModeButton();
@@ -104,10 +104,10 @@
104 toolbar.get_style_context().add_class("primary-toolbar");104 toolbar.get_style_context().add_class("primary-toolbar");
105 var toolbutton = new Gtk.ToolItem();105 var toolbutton = new Gtk.ToolItem();
106 var tool_mode = new ModeButton();106 var tool_mode = new ModeButton();
107 tool_mode.append(new Gtk.Label("1"));107 tool_mode.append_icon ("view-list-column-symbolic", Gtk.IconSize.MENU);
108 tool_mode.append(new Gtk.Label("2"));108 tool_mode.append_icon ("view-list-details-symbolic", Gtk.IconSize.MENU);
109 tool_mode.append(new Gtk.Label("3"));109 tool_mode.append_icon ("view-list-icons-symbolic", Gtk.IconSize.MENU);
110 tool_mode.append(new Gtk.Label("4"));110 tool_mode.append_icon ("view-list-video-symbolic", Gtk.IconSize.MENU);
111 toolbutton.add(tool_mode);111 toolbutton.add(tool_mode);
112 toolbar.insert(toolbutton, -1);112 toolbar.insert(toolbutton, -1);
113 toolbar.insert(create_appmenu(new Gtk.Menu()), -1);113 toolbar.insert(create_appmenu(new Gtk.Menu()), -1);
114114
=== modified file 'lib/Widgets/ModeButton.vala'
--- lib/Widgets/ModeButton.vala 2012-01-22 04:01:12 +0000
+++ lib/Widgets/ModeButton.vala 2012-01-24 18:29:25 +0000
@@ -27,13 +27,10 @@
27 public signal void mode_removed (int index, Gtk.Widget widget);27 public signal void mode_removed (int index, Gtk.Widget widget);
28 public signal void mode_changed (Gtk.Widget widget);28 public signal void mode_changed (Gtk.Widget widget);
2929
30 /* Style properties. Please note that style class names are for internal30 // Style properties. Please note that style class names are for internal
31 usage only. Theme developers should use GraniteWidgetsModeButton instead.31 // use only. Theme developers should use GraniteWidgetsModeButton instead.
32 */
33
34 internal static CssProvider style_provider;32 internal static CssProvider style_provider;
35 internal static StyleContext widget_style;33 internal static StyleContext widget_style;
36
37 private const int style_priority = Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION;34 private const int style_priority = Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION;
3835
39 private const string STYLESHEET = """36 private const string STYLESHEET = """
@@ -80,7 +77,7 @@
8077
81 public uint n_items {78 public uint n_items {
82 get {79 get {
83 return get_children().length();80 return get_children ().length ();
84 }81 }
85 }82 }
8683
@@ -102,84 +99,90 @@
102 homogeneous = true;99 homogeneous = true;
103 spacing = 0;100 spacing = 0;
104 app_paintable = true;101 app_paintable = true;
105 set_visual (get_screen().get_rgba_visual());102 set_visual (get_screen ().get_rgba_visual ());
106103
107 can_focus = true;104 can_focus = true;
108 }105 }
109106
110 public void append_pixbuf (Gdk.Pixbuf? pixbuf) {107 public void append_pixbuf (Gdk.Pixbuf? pixbuf) {
111 if (pixbuf == null)108 if (pixbuf == null) {
109 warning ("GraniteWidgetsModeButton: Attempt to add null pixbuf failed.");
112 return;110 return;
111 }
113112
114 var image = new Image.from_pixbuf (pixbuf);113 var image = new Image.from_pixbuf (pixbuf);
115 append (image);114 append (image);
116 }115 }
117116
118 public void append_text (string? text) {117 public void append_text (string? text) {
119 if (text == null)118 if (text == null) {
119 warning ("GraniteWidgetsModeButton: Attempt to add null text string failed.");
120 return;120 return;
121 }
121122
122 append (new Gtk.Label(text));123 append (new Gtk.Label(text));
123 }124 }
124125
125 /**126 /**
126 * This is the recommended function for adding icons to the ModeButton widget.127 * This is the recommended method for adding icons to the ModeButton widget.
127 * If you pass the name of a symbolic icon, it will be properly themed for128 * If the name of a symbolic icon is passed, it will be properly themed for
128 * every state of the widget. That is, it will match the foreground color129 * each state of the widget. That is, it will match the foreground color
129 * defined by the theme for each state (active, prelight, insensitive, etc.)130 * defined by the theme for each state (active, prelight, insensitive, etc.)
130 */131 */
131 public void append_icon (string icon_name, Gtk.IconSize size) {132 public void append_icon (string icon_name, Gtk.IconSize size) {
132 append_mode_button_item (null, icon_name, size);133 append (new Image.from_icon_name (icon_name, size));
133 }134 }
134135
135 public void append (Gtk.Widget w) {136 public void append (Gtk.Widget w) {
136 append_mode_button_item (w, null, null);137 if (w == null) {
137 }138 warning ("GraniteWidgetsModeButton: Attempt to add null widget failed.");
138139 return;
139 /**
140 * This function adds the foreground style properties of the given style
141 * context to the widget's icons. This is useful when you want to make the widget
142 * adapt its symbolic icon color to that of the parent in case the GTK+
143 * theme has not set them correctly. This function only affects the behavior
144 * of icons added with append_icon().
145 */
146 public void set_icon_foreground_style (Gtk.StyleContext icon_style) {
147 foreach (weak Widget button in get_children ()) {
148 (button as ModeButtonItem).set_icon_foreground_style (icon_style);
149 }140 }
141
142 var button = new ModeButtonItem ();
143
144 button.add (w);
145
146 button.button_press_event.connect (() => {
147 int selected = get_children().index (button);
148 set_active (selected);
149 return true;
150 });
151
152 add (button);
153 button.show_all ();
154
155 mode_added ((int)get_children ().length (), w);
150 }156 }
151157
152 public void set_active (int new_active_index) {158 public void set_active (int new_active_index) {
153159 if (new_active_index >= get_children ().length () || _selected == new_active_index)
154 if (new_active_index >= get_children().length () || _selected == new_active_index)
155 return;160 return;
156161
157 if (_selected >= 0)162 if (_selected >= 0)
158 ((ToggleButton) get_children().nth_data(_selected)).set_active (false);163 ((ToggleButton) get_children ().nth_data (_selected)).set_active (false);
159164
160 _selected = new_active_index;165 _selected = new_active_index;
161 ((ToggleButton) get_children().nth_data(_selected)).set_active (true);166 ((ToggleButton) get_children ().nth_data (_selected)).set_active (true);
162167
163 mode_changed(((ToggleButton) get_children().nth_data(_selected)).get_child());168 mode_changed (((ToggleButton) get_children ().nth_data (_selected)).get_child ());
164 }169 }
165170
166 public void set_item_visible(int index, bool val) {171 public void set_item_visible (int index, bool val) {
167 var item = get_children().nth_data(index);172 var item = get_children ().nth_data (index);
168 if(item == null)173 if (item == null)
169 return;174 return;
170175
171 item.set_no_show_all(!val);176 item.set_no_show_all (!val);
172 item.set_visible(val);177 item.set_visible (val);
173 }178 }
174179
175 public new void remove(int index)180 public new void remove (int index) {
176 {181 mode_removed (index, (get_children ().nth_data (index) as Gtk.Bin).get_child ());
177 mode_removed(index, (get_children().nth_data(index) as Gtk.Bin).get_child ());182 get_children ().nth_data (index).destroy ();
178 get_children().nth_data(index).destroy();
179 }183 }
180184
181 public void clear_children () {185 public void clear_children () {
182
183 foreach (weak Widget button in get_children ()) {186 foreach (weak Widget button in get_children ()) {
184 button.hide ();187 button.hide ();
185 if (button.get_parent () != null)188 if (button.get_parent () != null)
@@ -190,7 +193,7 @@
190 }193 }
191194
192 protected override bool scroll_event (EventScroll ev) {195 protected override bool scroll_event (EventScroll ev) {
193 if(ev.direction == Gdk.ScrollDirection.DOWN) {196 if (ev.direction == Gdk.ScrollDirection.DOWN) {
194 selected ++;197 selected ++;
195 }198 }
196 else if (ev.direction == Gdk.ScrollDirection.UP) {199 else if (ev.direction == Gdk.ScrollDirection.UP) {
@@ -199,110 +202,16 @@
199202
200 return false;203 return false;
201 }204 }
202
203 private void append_mode_button_item (Gtk.Widget? w, string? icon_name, Gtk.IconSize? size) {
204 var button = new ModeButtonItem ();
205
206 /* Modifying properties */
207 if (icon_name != null && size != null && w == null) {
208 button.set_icon (icon_name, size);
209 } else {
210 button.add(w);
211 }
212
213 button.button_press_event.connect (() => {
214 int selected = get_children().index (button);
215 set_active (selected);
216 return true;
217 });
218
219 add(button);
220 button.show_all ();
221
222 mode_added((int)get_children().length(), w);
223 }
224
225 }205 }
226206
227 private class ModeButtonItem : Gtk.ToggleButton {207 private class ModeButtonItem : Gtk.ToggleButton {
228
229 /* The main purpose of this class is handling icon theming */
230
231 private bool has_themed_icon;
232 private StyleContext? icon_style;
233
234 private string icon_name = "";
235 private Gtk.IconSize? icon_size = null;
236
237 private const int style_priority = Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION;
238
239 public ModeButtonItem () {208 public ModeButtonItem () {
240 can_focus = false;209 can_focus = false;
241 has_themed_icon = false;210
242211 const int style_priority = Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION;
243 icon_style = null;212
244213 get_style_context ().add_class ("raised");
245 get_style_context().add_class ("button");214 get_style_context ().add_provider (ModeButton.style_provider, style_priority);
246 get_style_context().add_class ("raised");
247 get_style_context().add_provider (ModeButton.style_provider, style_priority);
248
249 /* We need to track state changes in order to modify the icon */
250 state_flags_changed.connect ( () => {
251 if (has_themed_icon)
252 load_icon ();
253 });
254 }
255
256 public void set_icon_foreground_style (StyleContext? icon_style) {
257 this.icon_style = icon_style;
258 }
259
260 public new void set_icon (string name, Gtk.IconSize size) {
261 icon_name = name;
262 icon_size = size;
263
264 has_themed_icon = true;
265
266 load_icon ();
267 }
268
269 public new void set_image (Gtk.Image? image) {
270 if (image == null)
271 return;
272
273 /* Remove previous images */
274 foreach (weak Widget _image in get_children ()) {
275 if (this.get_parent () != null && _image is Gtk.Image)
276 _image.destroy();
277 }
278
279 /* Add new image */
280 add (image);
281
282 show_all ();
283 }
284
285 private void load_icon () {
286 set_image (new Image.from_pixbuf (render_themed_icon()));
287 }
288
289 private Gdk.Pixbuf? render_themed_icon () {
290 Gdk.Pixbuf? rv = null;
291
292 int width = 0, height = 0;
293 icon_size_lookup (icon_size, out width, out height);
294
295 try {
296 var themed_icon = new GLib.ThemedIcon.with_default_fallbacks (icon_name);
297 Gtk.IconInfo? icon_info = IconTheme.get_default().lookup_by_gicon (themed_icon as GLib.Icon, height, Gtk.IconLookupFlags.GENERIC_FALLBACK);
298 if (icon_info != null)
299 rv = icon_info.load_symbolic_for_context (icon_style ?? ModeButton.widget_style);
300 }
301 catch (Error err) {
302 warning ("%s", err.message);
303 }
304
305 return rv;
306 }215 }
307 }216 }
308}217}

Subscribers

People subscribed via source and target branches