[Aptitude-svn-commit] r3509 - in branches/aptitude-0.3/aptitude: . src/vscreen

Daniel Burrows dburrows@costa.debian.org
Wed, 29 Jun 2005 18:21:01 +0000


Author: dburrows
Date: Wed Jun 29 18:20:59 2005
New Revision: 3509

Modified:
   branches/aptitude-0.3/aptitude/ChangeLog
   branches/aptitude-0.3/aptitude/src/vscreen/vs_menu.cc
   branches/aptitude-0.3/aptitude/src/vscreen/vs_menu.h
Log:
Always fully calculate menu widths.

Modified: branches/aptitude-0.3/aptitude/ChangeLog
==============================================================================
--- branches/aptitude-0.3/aptitude/ChangeLog	(original)
+++ branches/aptitude-0.3/aptitude/ChangeLog	Wed Jun 29 18:20:59 2005
@@ -2,6 +2,12 @@
 
 	* src/vscreen/vs_menu.cc, src/vscreen/vs_menu.h:
 
+	  Don't bother caching the width for now (this is much more
+	  obviously correct; width caching can be reinstated without
+	  losing correctness if speed ever becomes an issue).
+
+	* src/vscreen/vs_menu.cc, src/vscreen/vs_menu.h:
+
 	  Rewrote vs_menu to properly handle wide characters.
 
 	* src/vscreen/curses++.h, src/vscreen/vscreen_widget.h:

Modified: branches/aptitude-0.3/aptitude/src/vscreen/vs_menu.cc
==============================================================================
--- branches/aptitude-0.3/aptitude/src/vscreen/vs_menu.cc	(original)
+++ branches/aptitude-0.3/aptitude/src/vscreen/vs_menu.cc	Wed Jun 29 18:20:59 2005
@@ -64,7 +64,7 @@
       }
 }
 
-bool vs_menu_item::is_enabled()
+bool vs_menu_item::is_enabled() const
 {
   if(enabled.empty())
     return !selected.empty();
@@ -73,7 +73,7 @@
 }
 
 vs_menu::vs_menu()
-  :vscreen_widget(), cursorloc(0), req_width(2)
+  :vscreen_widget(), cursorloc(0), min_width(2)
 {
   shown_sig.connect(sigc::mem_fun(*this, &vs_menu::appear));
 }
@@ -85,7 +85,7 @@
 }
 
 vs_menu::vs_menu(int x, int y, int w, vs_menu_info *inf)
-  :vscreen_widget(), cursorloc(0), req_width(w)
+  :vscreen_widget(), cursorloc(0), min_width(w)
 {
   while(inf->item_type!=vs_menu_info::VS_MENU_END)
     {
@@ -128,28 +128,6 @@
 void vs_menu::append_item(vs_menu_item *newitem)
 {
   items.push_back(newitem);
-  if(newitem)
-    {
-      int shortcutwidth=0;
-      int menuwidth=0;
-
-      const wstring &title=newitem->get_title();
-      const string &binding=newitem->get_binding();
-
-      for(wstring::size_type i=0; i<title.size(); ++i)
-	if(title[i]!=L'^')
-	  menuwidth+=wcwidth(title[i]);
-
-      if(binding.empty())
-	shortcutwidth=0;
-      else
-	{
-	  wstring keyname=global_bindings.readable_keyname(binding);
-	  shortcutwidth=wcswidth(keyname.c_str(), keyname.size())+1;
-	}
-
-      req_width=max<int>(menuwidth+2+shortcutwidth,req_width);
-    }
 
   if(get_visible())
     {
@@ -181,11 +159,33 @@
     vscreen_queuelayout();
 }
 
-// Right now the width is cached; maybe it should be calculated
-// dynamically?  At the moment it probably doesn't make a difference.
-// I guess.
 int vs_menu::width_request()
 {
+  int req_width=min_width;
+
+  for(itemlist::const_iterator item=items.begin();
+      item!=items.end(); ++item)
+    if(*item)
+      {
+	int titlewidth=0, shortcutwidth=0;
+	const wstring &title=(*item)->get_title();
+	const string &binding=(*item)->get_binding();
+
+	for(wstring::size_type i=0; i<title.size(); ++i)
+	  if(title[i]!=L'^')
+	    titlewidth+=wcwidth(title[i]);
+
+	if(binding.empty())
+	  shortcutwidth=0;
+	else
+	  {
+	    wstring keyname=global_bindings.readable_keyname(binding);
+	    shortcutwidth=wcswidth(keyname.c_str(), keyname.size())+1;
+	  }
+
+	req_width=max<int>(titlewidth+2+shortcutwidth,req_width);
+      }
+
   return req_width;
 }
 

Modified: branches/aptitude-0.3/aptitude/src/vscreen/vs_menu.h
==============================================================================
--- branches/aptitude-0.3/aptitude/src/vscreen/vs_menu.h	(original)
+++ branches/aptitude-0.3/aptitude/src/vscreen/vs_menu.h	Wed Jun 29 18:20:59 2005
@@ -35,13 +35,13 @@
   vs_menu_item(const std::wstring &_title, const std::string &_binding,
 	       const std::wstring &_description);
 
-  std::wstring get_title() {return title;}
-  std::string get_binding() {return binding;}
-  std::wstring get_description() {return description;}
-  chtype get_hotkey() {return hotkey;}
+  std::wstring get_title() const {return title;}
+  std::string get_binding() const {return binding;}
+  std::wstring get_description() const {return description;}
+  chtype get_hotkey() const {return hotkey;}
 
   /** The canonical way to test whether an item is really enabled. */
-  bool is_enabled();
+  bool is_enabled() const;
 
   /** Emitted when the menu item is selected. */
   sigc::signal0<void> selected;
@@ -106,15 +106,15 @@
   /** The location of the cursor, or items.size() if no item is selected. */
   itemlist::size_type cursorloc;
 
+  /** The minimum width of this menu. */
+  int min_width;
+
   // connected to "shown"
   void appear();
 
   // connected to "hidden"
   void disappear();
 
-  // (cache the requested width, since it's O(n) to find it)
-  int req_width;
-
   /** Returns \b true iff the given item is selectable. */
   bool selectable(itemlist::size_type pos);