Author: baby-guest
Date: 2007-06-20 21:36:06 +0000 (Wed, 20 Jun 2007)
New Revision: 3039

Modified:
   software/ui/src/games.cpp
   software/ui/src/pkgbrowser.cpp
   software/ui/src/pkgbrowser.h
   software/ui/src/ui.fld
Log:
Improved browser with multiple columns



Modified: software/ui/src/games.cpp
===================================================================
--- software/ui/src/games.cpp   2007-06-20 20:46:59 UTC (rev 3038)
+++ software/ui/src/games.cpp   2007-06-20 21:36:06 UTC (rev 3039)
@@ -150,8 +150,13 @@
        ui.TypeSelection->add(_("Any type"), 0, NULL, VoidString, 
FL_NORMAL_LABEL);
        ui.InterfaceSelection->clear();
        ui.InterfaceSelection->add(_("Any interface"), 0, NULL, VoidString, 
FL_NORMAL_LABEL);
+
+       static int widths[] = { 100, 300, 0 }; // widths for each column
        ui.ResultsBrowser->clear();
-       
+       ui.ResultsBrowser->column_widths(widths);
+       // tab delimited columns with colors
+       ui.ResultsBrowser->add("@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL 
PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]@[EMAIL PROTECTED]");
+
        // FIXME: there are better ways to remember the previous item
        
        const set<Tag> types = engine.types();
@@ -184,7 +189,7 @@
        {
                PackageRecord rec(engine.apt().rawRecord(i->name));
                char* userData = pkgString(rec.package());
-               string desc = rec.package() + " - " + rec.shortDescription() + 
" *";
+               string desc = rec.package() + "\t" + rec.shortDescription() + 
"\t*";
                int stars = (int)rintf(i->popcon * 4 / engine.popconLocalMax());
                for (int j = 0; j < stars; ++j)
                        desc += '*';

Modified: software/ui/src/pkgbrowser.cpp
===================================================================
--- software/ui/src/pkgbrowser.cpp      2007-06-20 20:46:59 UTC (rev 3038)
+++ software/ui/src/pkgbrowser.cpp      2007-06-20 21:36:06 UTC (rev 3039)
@@ -21,18 +21,21 @@
 #include <stdio.h>
 
 PackageBrowser::PackageBrowser(int x, int y, int w, int h, const char *l)
-       : Fl_Browser(x, y, w, h, l)
+       : VersatileBrowser(x, y, w, h, l)
 {
+       showcolsep(1);
+       //colsepcolor(FL_RED);
+       column_char('\t'); // tabs as column delimiters
 }
 
 void PackageBrowser::item_select(void *p, int s)
 {
-       Fl_Browser::item_select(p, s);
+       VersatileBrowser::item_select(p, s);
 
        if (s)
        {
-               int n = Fl_Browser::lineno(p);
-               void *data = Fl_Browser::data(n);
+               int n = VersatileBrowser::lineno(p);
+               void *data = VersatileBrowser::data(n);
                printf("  #%d : \"%s\"\n", n, (const char *)data);
                fflush(stdout);
        }
@@ -43,7 +46,7 @@
 //     printf("PackageBrowser::handle(int e = 0x%X)\n", e);
 //     fflush(stdout);
 
-       int ret = Fl_Browser::handle(e);
+       int ret = VersatileBrowser::handle(e);
 
        switch(e)
        {
@@ -90,50 +93,3 @@
        }
        return ret;
 }
-
-PackageViewer::PackageViewer(int X, int Y, int W, int H, const char *L)
-       : Fl_Scroll(X,Y,W,H,L), nchild(0)
-{
-       for ( int t=0; t<4; t++ ) { Add(); }
-}
-
-PackageViewer::Item::Item(int X, int Y, int W, int H, int FW, int DH, const 
char *L)
-       : Fl_Group(X, Y, W, H, L)
-{
-       // Green fixed width box
-       FixedBox = new Fl_Box(X, Y, FW, DH, "Fixed");
-       FixedBox->box(FL_UP_BOX);
-       FixedBox->color(FL_GREEN);
-       // Red stretchy box
-       StretchBox = new Fl_Box(X+FW, Y, W-FW, DH, "Stretch");
-       StretchBox->color(FL_BLUE);
-       StretchBox->box(FL_UP_BOX);
-       resizable(StretchBox);
-       end();
-}
-
-void PackageViewer::resize(int X, int Y, int W, int H)
-{
-       // Tell children to resize to our new width
-       for ( int t=0; t<children()-2; t++ )
-       { // -2: skip scrollbars
-               Fl_Widget *w = child(t);
-               w->resize(w->x(), w->y(), W-20, w->h()); // W-20: leave room 
for scrollbar
-       }
-       // Tell scroll children changed in size
-       init_sizes();
-       Fl_Scroll::resize(X,Y,W,H);
-}
-
-void PackageViewer::Add()
-{
-       // Append new scrollitem to bottom
-       // Note: An Fl_Pack would be a good way to do this, too
-       int ix = x() + 1;
-       int iy = y() - yposition() + (nchild*25)+1;
-       int iw = w() - 20; // -20: compensate for vscroll bar
-       int ih = 25;
-       add(new Item(ix, iy, iw, ih, 50, 25));
-       redraw();
-       nchild++;
-}

Modified: software/ui/src/pkgbrowser.h
===================================================================
--- software/ui/src/pkgbrowser.h        2007-06-20 20:46:59 UTC (rev 3038)
+++ software/ui/src/pkgbrowser.h        2007-06-20 21:36:06 UTC (rev 3039)
@@ -1,6 +1,9 @@
 /*
  * Copyright (C) 2007  Miriam Ruiz <[EMAIL PROTECTED]>
  *
+ * VersatileBrowser based in code by Greg Ercolano <[EMAIL PROTECTED]>
+ * URL: http://seriss.com/people/erco/fltk/
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -22,9 +25,175 @@
 #include <stdlib.h>
 
 #include <FL/Fl.H>
+#include <FL/Fl_Double_Window.H>
 #include <FL/Fl_Browser.H>
+#include <FL/fl_draw.H>
+//
+// Demonstrate how to derive a class extending Fl_Browser with interactively 
resizable columns
+// erco 1.10 12/09/2005
+//
+class VersatileBrowser : public Fl_Browser
+{
+    Fl_Color  _colsepcolor;    // color of column separator lines 
+    int       _showcolsep;     // flag to enable drawing column separators
+    Fl_Cursor _last_cursor;    // saved cursor state info
+    int       _dragging;       // 1=user dragging a column
+    int       _dragcol;                // col# user is currently dragging
+    int      *_widths;         // pointer to user's width[] array
+    int       _nowidths[1];    // default width array (non-const)
+    // CHANGE CURSOR
+    //     Does nothing if cursor already set to value specified.
+    //
+    void change_cursor(Fl_Cursor newcursor) {
+        if ( newcursor != _last_cursor ) {
+            fl_cursor(newcursor, FL_BLACK, FL_WHITE);
+            _last_cursor = newcursor;
+        }
+    }
+    // RETURN THE COLUMN MOUSE IS 'NEAR'
+    //     Returns -1 if none.
+    //
+    int which_col_near_mouse() {
+        int X,Y,W,H;
+        Fl_Browser::bbox(X,Y,W,H);             // area inside browser's box()
+        // EVENT NOT INSIDE BROWSER AREA? (eg. on a scrollbar)
+        if ( ! Fl::event_inside(X,Y,W,H) ) {
+            return(-1);
+        }
+        int mousex = Fl::event_x() + hposition();
+        int colx = this->x();
+        for ( int t=0; _widths[t]; t++ ) {
+            colx += _widths[t];
+            int diff = mousex - colx;
+            // MOUSE 'NEAR' A COLUMN?
+           //     Return column #
+           //
+            if ( diff >= -4 && diff <= 4 ) {
+                return(t);
+            }
+        }
+        return(-1);
+    }
 
-class PackageBrowser : public Fl_Browser
+protected:
+    // MANAGE EVENTS TO HANDLE COLUMN RESIZING
+    int handle(int e) {
+       // Not showing column separators? Use default Fl_Browser::handle() logic
+       if ( ! showcolsep() ) return(Fl_Browser::handle(e));
+        // Handle column resizing
+        int ret = 0;
+        switch ( e ) {
+            case FL_ENTER: {
+                ret = 1;
+                break;
+            }
+            case FL_MOVE: {
+                if ( which_col_near_mouse() >= 0 ) {
+                    change_cursor(FL_CURSOR_WE);
+                } else {
+                    change_cursor(FL_CURSOR_DEFAULT);
+                }
+                ret = 1;
+                break;
+            }
+            case FL_PUSH: {
+                int whichcol = which_col_near_mouse();
+                if ( whichcol >= 0 ) {
+                    // CLICKED ON RESIZER? START DRAGGING
+                    ret = 1;
+                    _dragging = 1;
+                    _dragcol = whichcol;
+                    change_cursor(FL_CURSOR_DEFAULT);
+                }
+                break;
+            }
+            case FL_DRAG: {
+                if ( _dragging ) {
+                    ret = 1;
+                   // Sum up column widths to determine position
+                    int mousex = Fl::event_x() + hposition();
+                    int newwidth = mousex - x();
+                    for ( int t=0; _widths[t] && t<_dragcol; t++ ) {
+                        newwidth -= _widths[t];
+                    }
+                    if ( newwidth > 0 ) {
+                       // Apply new width, redraw interface
+                        _widths[_dragcol] = newwidth;
+                        if ( _widths[_dragcol] < 2 ) {
+                            _widths[_dragcol] = 2;
+                        }
+                        redraw();
+                    }
+                }
+                break;
+            }
+            case FL_LEAVE:
+            case FL_RELEASE: {
+               _dragging = 0;                          // disable drag mode
+                change_cursor(FL_CURSOR_DEFAULT);      // ensure normal cursor
+                ret = 1;
+                break;
+            }
+        }
+        if ( _dragging ) return(1);                    // dragging? don't pass 
event to Fl_Browser
+       return(Fl_Browser::handle(e) ? 1 : ret);
+    }
+    void draw() {
+        // DRAW BROWSER
+        Fl_Browser::draw();
+        if ( _showcolsep ) {
+            // DRAW COLUMN SEPARATORS
+            int colx = this->x() - hposition();
+            int X,Y,W,H;
+            Fl_Browser::bbox(X,Y,W,H);
+            fl_color(_colsepcolor);
+            for ( int t=0; _widths[t]; t++ ) {
+                colx += _widths[t];
+                if ( colx > X && colx < (X+W) ) {
+                    fl_line(colx, Y, colx, Y+H-1);
+                }
+            }
+        }
+    }
+public:
+    // CTOR
+    VersatileBrowser(int X,int Y,int W,int H,const char*L=NULL) : 
Fl_Browser(X,Y,W,H,L) {
+        _colsepcolor = Fl_Color(FL_GRAY);
+        _last_cursor = FL_CURSOR_DEFAULT;
+        _showcolsep  = 0;
+        _dragging    = 0;
+        _nowidths[0] = 0;
+        _widths      = _nowidths;
+    }
+    // GET/SET COLUMN SEPARATOR LINE COLOR
+    Fl_Color colsepcolor() const {
+        return(_colsepcolor);
+    }
+    void colsepcolor(Fl_Color val) {
+        _colsepcolor = val;
+    }
+    // GET/SET DISPLAY OF COLUMN SEPARATOR LINES
+    //     1: show lines, 0: don't show lines
+    //
+    int showcolsep() const {
+        return(_showcolsep);
+    }
+    void showcolsep(int val) {
+        _showcolsep = val;
+    }
+    // GET/SET COLUMN WIDTHS ARRAY
+    //    Just like fltk method, but array is non-const.
+    //
+    int *column_widths() const {
+        return(_widths);
+    }
+    void column_widths(int *val) {
+        _widths = val;
+        Fl_Browser::column_widths(val);
+    }
+};
+
+class PackageBrowser : public VersatileBrowser
 {
 public:
        PackageBrowser(int x, int y, int w, int h, const char *l = NULL);
@@ -34,30 +203,4 @@
        virtual int handle(int e);
 };
 
-#include <FL/Fl.H>
-#include <FL/Fl_Double_Window.H>
-#include <FL/Fl_Group.H>
-#include <FL/Fl_Scroll.H>
-#include <FL/Fl_Button.H>
-#include <FL/Fl_Box.H>
-
-class PackageViewer : public Fl_Scroll {
-public:
-       class Item : public Fl_Group
-       {
-               private:
-                       Fl_Box* FixedBox;
-                       Fl_Box* StretchBox;
-               public:
-                       Item(int X, int Y, int W, int H, int FW, int DH, const 
char* L = NULL);
-       };
-
-       PackageViewer(int X, int Y, int W, int H, const char* L = NULL);
-       void resize(int X, int Y, int W, int H);
-       void Add();
-
-private:
-       int nchild;
-};
-
 #endif

Modified: software/ui/src/ui.fld
===================================================================
--- software/ui/src/ui.fld      2007-06-20 20:46:59 UTC (rev 3038)
+++ software/ui/src/ui.fld      2007-06-20 21:36:06 UTC (rev 3039)
@@ -16,7 +16,7 @@
   Function {CreateMainWindow()} {open
   } {
     Fl_Window {} {open
-      xywh {598 111 385 430} type Double resizable visible
+      xywh {633 58 385 430} type Double resizable visible
     } {
       Fl_Group {} {open
         xywh {5 14 375 139} box UP_FRAME


_______________________________________________
Pkg-games-commits mailing list
[email protected]
http://lists.alioth.debian.org/mailman/listinfo/pkg-games-commits

Reply via email to