Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

app-helper.h

Go to the documentation of this file.
00001 /* 
00002  * Copyright 2000 Karl Nelson
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public
00015  * License along with this library; if not, write to the Free
00016  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  *
00018  * UIInfo "huffs goat choad" but we will try to make the best of it.
00019  */
00020 
00021 #ifndef GNOMEMM_APP_HELPER_H
00022 #define GNOMEMM_APP_HELPER_H
00023 
00024 #include <new>
00025 #include <gtkmm/menushell.h>
00026 #include <gtkmm/toolbar.h>
00027 #include <gtkmm/accelgroup.h>
00028 #include <libgnomeuimm/ui-items-icon.h>
00029 #include <libgnomeui/gnome-app.h>
00030 #include <libgnomeui/gnome-app-helper.h>
00031 #include <vector>
00032 
00033 namespace Gnome
00034 {
00035 
00036 namespace UI
00037 {
00038 
00039 namespace Items
00040 {
00041 
00042 class Icon;
00043 
00044 /*******************************************************/
00045 // Info is the base of the UI item tree it represents broad types
00046 // of GUI items for construction later.
00047 
00048 template<class T_Info>
00049 class Array;
00050 
00051 class InfoData;
00052 
00053 /*** Derived GnomeUIInfo
00054  * Note: When deriving this, you must not add any fields or add any virtuals
00055  */
00056 class Info : public GnomeUIInfo
00057 {
00058   friend class InfoData;
00059   friend class Array<Info>;
00060 
00061   // must not be dynamic
00062   void* operator new(size_t s);
00063 public:
00064   void* operator new(size_t s, void* v) {return ::operator new(s, v);}
00065 
00066   Info();
00067   Info(const Info& src);
00068   ~Info();
00069   Info& operator=(const Info& src);
00070 
00071   Gtk::Widget* get_widget();
00072   const Gtk::Widget* get_widget() const;
00073 
00074   enum Type
00075   {
00076     END = GNOME_APP_UI_ENDOFINFO,
00077     ITEM = GNOME_APP_UI_ITEM,
00078     TOGGLEITEM = GNOME_APP_UI_TOGGLEITEM,
00079     RADIOITEMS = GNOME_APP_UI_RADIOITEMS,
00080     SUBTREE = GNOME_APP_UI_SUBTREE,
00081     SEPARATOR = GNOME_APP_UI_SEPARATOR,
00082     HELP = GNOME_APP_UI_HELP,
00083     BUILDER = GNOME_APP_UI_BUILDER_DATA,
00084     CONFIGURABLE = GNOME_APP_UI_ITEM_CONFIGURABLE,
00085     SUBTREE_STOCK = GNOME_APP_UI_SUBTREE_STOCK
00086   };
00087   
00088   Type type() const;
00089 
00090   const gchar* debug_get_icon_info() const;
00091 
00092   void set_accel(const Gtk::AccelKey& ak = Gtk::AccelKey());
00093 
00094   typedef sigc::slot<void> Callback;
00095   
00096 protected:
00097   void init(Type type_);
00098   void init_cb(Type type_, const Icon& icon,
00099             const Glib::ustring& label, const Callback& cb,
00100             const Glib::ustring& tooltip);
00101   void init_sub(Type type_, const Icon& icon,
00102             const Glib::ustring& label, const Array<Info>& sub,
00103             const Glib::ustring& tooltip);
00104   InfoData* init_common(Type type_, const Icon& icon_,
00105             const Glib::ustring& label_, const Glib::ustring& hint_);
00106 
00107   InfoData* get_data_();
00108   const InfoData* get_data_() const;
00109   
00110   void set_data_(InfoData* infodata);
00111 };
00112 
00113 
00114 
00115 
00116 
00117 
00118 // subtree or submenu
00119 class SubTree : public Info
00120 {
00121 protected:
00122   SubTree();
00123 public:
00124   SubTree(const Glib::ustring& label, const Array<Info>& uitree,
00125           const Glib::ustring& tip = Glib::ustring());
00126   SubTree(const Icon& icon, const Glib::ustring& label,
00127           const Array<Info>& uitree, const Glib::ustring& tip = Glib::ustring());
00128   ~SubTree();
00129 
00130   Array<Info>& get_uitree();
00131 };
00132 typedef SubTree Menu;
00133 
00134 
00135 
00136 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00137 // begin marker for C structures (not really for user use)
00138 class Begin : public Info
00139 {
00140 public:
00141   Begin();
00142 
00143 private:
00144   static GnomeUIBuilderData build_data_;
00145 };
00146 
00147 // end marker for C structures (not really for user use)
00148 class End : public Info
00149 {
00150 public:
00151   End();
00152 };
00153 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
00154 
00155 namespace Array_Helpers
00156 {
00157 
00158 template<class T>
00159 struct Traits
00160 {
00161   typedef typename T::const_iterator iterator;
00162   static iterator begin(const T& t) {return t.begin();}
00163   static iterator end(const T& t) {return t.end();}
00164 };
00165 
00166 // You must place an End() to use this type
00167 template<class T_Info>
00168 struct Traits<T_Info*>
00169 {
00170   typedef const T_Info* iterator;
00171   static iterator begin(const T_Info* t) {return t;}
00172   static iterator end( T_Info* t) {return t+64;} //64?
00173 };
00174 
00175 template<size_t N, class T_Info>
00176 struct Traits<T_Info[N]>
00177 {
00178   typedef const T_Info* iterator;
00179   static iterator begin(const T_Info* t) {return t;}
00180   static iterator end(const T_Info* t) {return &t[N];}
00181 };
00182 
00183 } /* namespace Array_Helpers */
00184 
00185 
00186 
00187 // Array converter class
00188 template<class T_Info>
00189 class Array 
00190 {
00191   //void* operator new (size_t s);  // not implemented
00192   Info* data_;
00193   Info* begin_;
00194   int size_;
00195 
00196   template <class I> void create(I b, I e);
00197 public:
00198   typedef T_Info value_type;
00199   typedef T_Info& reference_type;
00200   typedef T_Info* iterator;
00201   typedef T_Info* const const_iterator;
00202   typedef T_Info* const pointer;
00203   typedef T_Info* const const_pointer;
00204   typedef size_t size_type;
00205   typedef ptrdiff_t difference_type;
00206 
00207   Array& operator=(const Array& a)
00208   {
00209     if (this == &a)
00210       return *this;
00211 
00212     delete [] data_;
00213     data_ = 0;
00214     size_ = 0;
00215     
00216     create(a.begin(), a.end());
00217     return *this;
00218   }
00219 
00220   Array()
00221   : data_(0), begin_(0), size_(0)
00222   { create((T_Info*)0, (T_Info*)0); }
00223 
00224   Array(Array& a)
00225   : data_(0), begin_(0), size_(0)
00226   { create(a.begin(), a.end()); }
00227 
00228   template <class T>
00229   Array(const T& t)
00230   : data_(0), begin_(0), size_(0)
00231   { create(Array_Helpers::Traits<T>::begin(t),
00232            Array_Helpers::Traits<T>::end(t));
00233   }
00234 
00235   template <class I>
00236   Array(I b, I e)
00237   : data_(0), begin_(0), size_(0)
00238   { create(b, e); }
00239 
00240   ~Array()
00241   {
00242     delete [] data_;
00243     data_ = 0;
00244     size_ = 0;
00245   }
00246 
00247   size_t size() const { return size_; }
00248 
00249   iterator begin() const
00250   { return static_cast<T_Info*>(begin_); }
00251   
00252   iterator end() const
00253   { return static_cast<T_Info*>(begin_ + size_); }
00254   
00255   reference_type operator[](size_t n) const
00256   { return static_cast<T_Info&>(begin_[n]); }
00257   
00258 
00259   GnomeUIInfo* gobj() const
00260   { return static_cast<GnomeUIInfo*>(data_); }
00261   
00262 };
00263 
00264 
00265 template <class T_Info>
00266 template <class I>
00267 void
00268 Array<T_Info>::create(I b, I e)
00269 {
00270 
00271   // NULL list
00272   if (b == e)
00273   {
00274     data_ = new End[1];
00275     return;
00276   }
00277 
00278   // determine number of Items
00279   for (I b2 = b ; b2 != e; ++b2, ++size_)
00280   {
00281     if (b2->type() == Info::END)
00282       break;
00283   }
00284 
00285   // must have a builder data on head
00286   if (b->type() != Info::BUILDER)
00287   {
00288     begin_ = data_ = new Info[size_+2]; //plus space for BEGIN and END.
00289     new (begin_) Begin(); //constructor without allocation.
00290     begin_++; //Hide Begin() element from outside, by keeping it before begin().
00291   }
00292   else
00293     begin_ = data_ = new Info[size_+1]; //plus space for END.
00294 
00295   // Copy data
00296   for (int i = 0; b != e; ++b, ++i)
00297   {
00298     new (&begin_[i]) T_Info(*b); //constructor without allocation.
00299   }
00300 
00301   new (&begin_[size_]) End(); //constructor without allocation.
00302 
00303   //At this point _size excludes the Begin() and End() GNOME internals elements,
00304   //so users of begin() and end() will never see them.
00305 }
00306 
00307 
00308 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00309 // This is a refcounted holder to deal with C interface badness
00310 // users don't need to deal with these unless they are making new Info types.
00311 //   This is probably refcounted because that's easier than copying in copy constructors, given that we can't
00312 //   add member data to the Info class.
00313 class InfoData
00314 {
00315 public:
00316   InfoData();
00317   InfoData(const Glib::ustring& label, const Glib::ustring& hint, const Icon& icon = Icon());
00318 
00319 private:
00320   InfoData(const InfoData&); //Prevent use of copy constructor.
00321 protected:
00322   virtual ~InfoData();
00323 
00324 public:
00325 
00326   void ref();
00327   void unref();
00328 
00329   virtual void connect(Info&);
00330 
00331   typedef sigc::slot<void> Callback;
00332 
00333   void set_callback(const Callback& callback);
00334   void set_subtree(const Array<Info>& subtree);
00335   Array<Info>& get_subtree();
00336 
00337 
00338   Callback callback_;
00339   Array<Info> subtree_;
00340   Glib::ustring label_;
00341   Glib::ustring hint_;
00342   Icon icon_;
00343 
00344 private:
00345   int ref_count_;
00346 };
00347 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
00348 
00349 
00353 Items::Array<Info> fill (Gtk::MenuShell                      &menu_shell,
00354                          const Items::Array<Info>            &info,
00355                          const Glib::RefPtr<Gtk::AccelGroup> &accel_group,
00356                          bool                                 uline_accels = true,
00357                          int                                  pos = 0);
00358 
00359 
00363 Items::Array<Info> fill (Gtk::Toolbar                        &toolbar, 
00364                          const Items::Array<Info>            &info,
00365                          const Glib::RefPtr<Gtk::AccelGroup> &accel_group);
00366 
00367 
00368 
00369 //: Utility functions for Gtk::MenuShell.
00370 //- These should really be member methods of Gtk::MenuShell,
00371 //- but they're part of gnomeui, not gtk, and they didn't subclass GtkMenuShell.
00372 //static Gtk::MenuShell* MenuShell_find_menu_pos(const Gtk::MenuShell& parent,
00373 //                                               const Glib::ustring &path,
00374 //                                               int& pos);
00375 
00376 //IGNORED gnome_app_fill_menu_with_data()
00377 //IGNORED gnome_app_fill_menu_custom()
00378 //IGNORED gnome_app_fill_toolbar_with_data()
00379 //IGNORED gnome_app_fill_toolbar_custom()
00380 
00381 } /* namespace Items */
00382 } /* namespace UI */
00383 } /* namespace Gnome */
00384 
00385 #endif //GNOMEMM_APP_HELPER_H

Generated on Wed Sep 7 11:18:38 2005 for libgnomeuimm by  doxygen 1.4.4