Code

New widget helperclass for Gtk:Entry
[inkscape.git] / src / ui / widget / panel.cpp
1 /**
2  * \brief Panel widget
3  *
4  * Authors:
5  *   Bryce Harrington <bryce@bryceharrington.org>
6  *   Jon A. Cruz <jon@joncruz.org>
7  *
8  * Copyright (C) 2004 Bryce Harrington
9  * Copyright (C) 2005 Jon A. Cruz
10  *
11  * Released under GNU GPL.  Read the file 'COPYING' for more information
12  */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
18 #include <glibmm/i18n.h>
20 #include "panel.h"
21 #include "../../icon-size.h"
22 #include "../../prefs-utils.h"
24 namespace Inkscape {
25 namespace UI {
26 namespace Widget {
28 static const int PANEL_SETTING_SIZE = 0;
29 static const int PANEL_SETTING_MODE = 1;
30 static const int PANEL_SETTING_WRAP = 2;
31 static const int PANEL_SETTING_NEXTFREE = 3;
33 /**
34  *    Construct a Panel
35  *
36  *    \param label Label.
37  */
39 Panel::Panel() :
40     _prefs_path(NULL),
41     _menuDesired(false),
42     _tempArrow( Gtk::ARROW_LEFT, Gtk::SHADOW_ETCHED_OUT ),
43     menu(0),
44     _fillable(0)
45 {
46     init();
47 }
49 Panel::Panel( Glib::ustring const &label, gchar const* prefs_path, bool menuDesired ) :
50     _prefs_path(prefs_path),
51     _menuDesired(menuDesired),
52     label(label),
53     _tempArrow( Gtk::ARROW_LEFT, Gtk::SHADOW_ETCHED_OUT ),
54     menu(0),
55     _fillable(0)
56 {
57     init();
58 }
60 Panel::~Panel()
61 {
62     delete menu;
63 }
65 void Panel::_popper(GdkEventButton* event)
66 {
67     if ( (event->type == GDK_BUTTON_PRESS) && (event->button == 3 || event->button == 1) ) {
68         if (menu) {
69             menu->popup( event->button, event->time );
70         }
71     }
72 }
74 void Panel::init()
75 {
76     Glib::ustring tmp("<");
77     _anchor = Gtk::ANCHOR_CENTER;
78     tabTitle.set_label(this->label);
80     guint panel_size = 0;
81     if (_prefs_path) {
82         panel_size = prefs_get_int_attribute_limited( _prefs_path, "panel_size", 1, 0, 10 );
83     }
85     guint panel_mode = 0;
86     if (_prefs_path) {
87         panel_mode = prefs_get_int_attribute_limited( _prefs_path, "panel_mode", 1, 0, 10 );
88     }
90     guint panel_wrap = 0;
91     if (_prefs_path) {
92         panel_wrap = prefs_get_int_attribute_limited( _prefs_path, "panel_wrap", 0, 0, 1 );
93     }
95     menu = new Gtk::Menu();
96     {
97         const char *things[] = {
98             N_("tiny"),
99             N_("small"),
100             N_("medium"),
101             N_("large"),
102             N_("huge")
103         };
104         Gtk::RadioMenuItem::Group groupOne;
105         for ( unsigned int i = 0; i < G_N_ELEMENTS(things); i++ ) {
106             Glib::ustring foo(gettext(things[i]));
107             Gtk::RadioMenuItem* single = manage(new Gtk::RadioMenuItem(groupOne, foo));
108             menu->append(*single);
109             if ( i == panel_size ) {
110                 single->set_active(true);
111             }
112             single->signal_activate().connect( sigc::bind<int, int>( sigc::mem_fun(*this, &Panel::bounceCall), PANEL_SETTING_SIZE, i) );
113        }
114     }
115     menu->append( *manage(new Gtk::SeparatorMenuItem()) );
116     Gtk::RadioMenuItem::Group group;
117     Glib::ustring oneLab(_("List"));
118     Glib::ustring twoLab(_("Grid"));
119     Gtk::RadioMenuItem *one = manage(new Gtk::RadioMenuItem(group, oneLab));
120     Gtk::RadioMenuItem *two = manage(new Gtk::RadioMenuItem(group, twoLab));
122     if (panel_mode == 0) {
123         one->set_active(true);
124     } else if (panel_mode == 1) {
125         two->set_active(true);
126     }
128     menu->append( *one );
129     nonHorizontal.push_back( one );
130     menu->append( *two );
131     nonHorizontal.push_back( two );
132     Gtk::MenuItem* sep = manage( new Gtk::SeparatorMenuItem());
133     menu->append( *sep );
134     nonHorizontal.push_back( sep );
135     one->signal_activate().connect( sigc::bind<int, int>( sigc::mem_fun(*this, &Panel::bounceCall), PANEL_SETTING_MODE, 0) );
136     two->signal_activate().connect( sigc::bind<int, int>( sigc::mem_fun(*this, &Panel::bounceCall), PANEL_SETTING_MODE, 1) );
138     {
139         Glib::ustring wrapLab(_("Wrap"));
140         Gtk::CheckMenuItem *check = manage(new Gtk::CheckMenuItem(wrapLab));
141         check->set_active( panel_wrap );
142         menu->append( *check );
143         nonVertical.push_back(check);
145         check->signal_toggled().connect( sigc::bind<Gtk::CheckMenuItem*>(sigc::mem_fun(*this, &Panel::_wrapToggled), check) );
147         sep = manage( new Gtk::SeparatorMenuItem());
148         menu->append( *sep );
149         nonVertical.push_back( sep );
150     }
152     menu->show_all_children();
153     for ( std::vector<Gtk::Widget*>::iterator iter = nonVertical.begin(); iter != nonVertical.end(); ++iter ) {
154         (*iter)->hide();
155     }
157     //closeButton.set_label("X");
159     topBar.pack_start(tabTitle);
161     //topBar.pack_end(closeButton, false, false);
164     if ( _menuDesired ) {
165         topBar.pack_end(menuPopper, false, false);
166         Gtk::Frame* outliner = manage(new Gtk::Frame());
167         outliner->set_shadow_type( Gtk::SHADOW_ETCHED_IN );
168         outliner->add( _tempArrow );
169         menuPopper.add( *outliner );
170         menuPopper.signal_button_press_event().connect_notify( sigc::mem_fun(*this, &Panel::_popper) );
171     }
173     pack_start( topBar, false, false );
175     Gtk::HBox* boxy = manage( new Gtk::HBox() );
177     boxy->pack_start( contents, true, true );
178     boxy->pack_start( rightBar, false, true );
180     pack_start( *boxy, true, true );
182     show_all_children();
184     bounceCall( PANEL_SETTING_SIZE, panel_size );
185     bounceCall( PANEL_SETTING_MODE, panel_mode );
186     bounceCall( PANEL_SETTING_WRAP, panel_wrap );
189 void Panel::setLabel(Glib::ustring const &label)
191     this->label = label;
192     tabTitle.set_label(this->label);
195 void Panel::setOrientation( Gtk::AnchorType how )
197     if ( _anchor != how )
198     {
199         _anchor = how;
200         switch ( _anchor )
201         {
202             case Gtk::ANCHOR_NORTH:
203             case Gtk::ANCHOR_SOUTH:
204             {
205                 if ( _menuDesired ) {
206                     menuPopper.reference();
207                     topBar.remove(menuPopper);
208                     rightBar.pack_start(menuPopper, false, false);
209                     menuPopper.unreference();
211                     for ( std::vector<Gtk::Widget*>::iterator iter = nonHorizontal.begin(); iter != nonHorizontal.end(); ++iter ) {
212                         (*iter)->hide();
213                     }
214                     for ( std::vector<Gtk::Widget*>::iterator iter = nonVertical.begin(); iter != nonVertical.end(); ++iter ) {
215                         (*iter)->show();
216                     }
217                 }
218                 // Ensure we are not in "list" mode
219                 bounceCall( PANEL_SETTING_MODE, 1 );
220                 topBar.remove(tabTitle);
221             }
222             break;
224             default:
225             {
226                 if ( _menuDesired ) {
227                     for ( std::vector<Gtk::Widget*>::iterator iter = nonHorizontal.begin(); iter != nonHorizontal.end(); ++iter ) {
228                         (*iter)->show();
229                     }
230                     for ( std::vector<Gtk::Widget*>::iterator iter = nonVertical.begin(); iter != nonVertical.end(); ++iter ) {
231                         (*iter)->hide();
232                     }
233                 }
234             }
235         }
236     }
239 void Panel::_regItem( Gtk::MenuItem* item, int group, int id )
241     menu->append( *item );
242     item->signal_activate().connect( sigc::bind<int, int>( sigc::mem_fun(*this, &Panel::bounceCall), group + PANEL_SETTING_NEXTFREE, id) );
243     item->show();
246 void Panel::restorePanelPrefs()
248     guint panel_size = 0;
249     if (_prefs_path) {
250         panel_size = prefs_get_int_attribute_limited (_prefs_path, "panel_size", 1, 0, 10);
251     }
252     guint panel_mode = 0;
253     if (_prefs_path) {
254         panel_mode = prefs_get_int_attribute_limited (_prefs_path, "panel_mode", 1, 0, 10);
255     }
256     guint panel_wrap = 0;
257     if (_prefs_path) {
258         panel_wrap = prefs_get_int_attribute_limited( _prefs_path, "panel_wrap", 0, 0, 1 );
259     }
260     bounceCall( PANEL_SETTING_SIZE, panel_size );
261     bounceCall( PANEL_SETTING_MODE, panel_mode );
262     bounceCall( PANEL_SETTING_WRAP, panel_wrap );
265 void Panel::bounceCall(int i, int j)
267     menu->set_active(0);
268     switch ( i ) {
269     case PANEL_SETTING_SIZE:
270         if (_prefs_path) {
271             prefs_set_int_attribute( _prefs_path, "panel_size", j );
272         }
273         if ( _fillable ) {
274             ViewType currType = _fillable->getPreviewType();
275             switch ( j ) {
276             case 0:
277             {
278                 _fillable->setStyle(Inkscape::ICON_SIZE_DECORATION, currType);
279             }
280             break;
281             case 1:
282             {
283                 _fillable->setStyle(Inkscape::ICON_SIZE_MENU, currType);
284             }
285             break;
286             case 2:
287             {
288                 _fillable->setStyle(Inkscape::ICON_SIZE_SMALL_TOOLBAR, currType);
289             }
290             break;
291             case 3:
292             {
293                 _fillable->setStyle(Inkscape::ICON_SIZE_BUTTON, currType);
294             }
295             break;
296             case 4:
297             {
298                 _fillable->setStyle(Inkscape::ICON_SIZE_DIALOG, currType);
299             }
300             break;
301             default:
302                 ;
303             }
304         }
305         break;
306     case PANEL_SETTING_MODE:
307         if (_prefs_path) {
308             prefs_set_int_attribute (_prefs_path, "panel_mode", j);
309         }
310         if ( _fillable ) {
311             Inkscape::IconSize currSize = _fillable->getPreviewSize();
312             switch ( j ) {
313             case 0:
314             {
315                 _fillable->setStyle(currSize, VIEW_TYPE_LIST);
316             }
317             break;
318             case 1:
319             {
320                 _fillable->setStyle(currSize, VIEW_TYPE_GRID);
321             }
322             break;
323             default:
324                 break;
325             }
326         }
327         break;
328     case PANEL_SETTING_WRAP:
329         if (_prefs_path) {
330             prefs_set_int_attribute (_prefs_path, "panel_wrap", j ? 1 : 0);
331         }
332         if ( _fillable ) {
333             _fillable->setWrap( j );
334         }
335         break;
336     default:
337         _handleAction( i - PANEL_SETTING_NEXTFREE, j );
338     }
342 void Panel::_wrapToggled(Gtk::CheckMenuItem* toggler)
344     if ( toggler ) {
345         bounceCall( PANEL_SETTING_WRAP, toggler->get_active() ? 1 : 0 );
346     }
353 Glib::ustring const &Panel::getLabel() const
355     return label;
358 void Panel::_setTargetFillable( PreviewFillable *target )
360     _fillable = target;
363 void Panel::_handleAction( int setId, int itemId )
365 // for subclasses to override
368 } // namespace Widget
369 } // namespace UI
370 } // namespace Inkscape
372 /*
373   Local Variables:
374   mode:c++
375   c-file-style:"stroustrup"
376   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
377   indent-tabs-mode:nil
378   fill-column:99
379   End:
380 */
381 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :