Changeset 42

Show
Ignore:
Timestamp:
05/13/07 20:27:02 (2 years ago)
Author:
lindquist
Message:

started a decent interface for ListBox?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/bughunt/minwin/listbox.d

    r41 r42  
    5555            return XY(width,height); 
    5656        } 
    57         // -1 for custom text 
     57 
    5858        int selection() { 
    59             return SendMessageA(peer,LB_GETCURSEL,0,0); 
     59            int sel = SendMessageA(peer,LB_GETCURSEL,0,0); 
     60            return sel == LB_ERR ? -1 : sel; 
    6061        } 
    6162        void selection(int n) { 
    62             SendMessageA(peer,LB_SETCURSEL,n,0); 
    63             version (LOG) log.writefln("number of item is %d", 
    64                         SendMessageA(peer,CB_GETCOUNT,0,0)); 
     63            if (n < 0) n = -1; 
     64            int r = SendMessageA(peer,LB_SETCURSEL,cast(WPARAM)n,0); 
     65            sysAssert(r != LB_ERR, "Unable to select ListBox item"); 
     66        } 
     67 
     68        int count() { 
     69            return SendMessageA(peer,LB_GETCOUNT,0,0); 
     70        } 
     71 
     72        char[] opIndex(int i) { 
     73            if (useWfuncs) { 
     74                int n = SendMessageW(peer,LB_GETTEXTLEN,cast(WPARAM)i,0); 
     75                if (n == LB_ERR) return null; 
     76                scope str = new wchar[n+1]; 
     77                n = SendMessageW(peer,LB_GETTEXT,cast(WPARAM)i,cast(LPARAM)str.ptr); 
     78                if (n == LB_ERR) return null; 
     79                return toUTF8(str[0..$-1]); 
     80            } else { 
     81                int n = SendMessageA(peer,LB_GETTEXTLEN,cast(WPARAM)i,0); 
     82                if (n == LB_ERR) return null; 
     83                scope str = new char[n+1]; 
     84                n = SendMessageA(peer,LB_GETTEXT,cast(WPARAM)i,cast(LPARAM)str.ptr); 
     85                if (n == LB_ERR) return null; 
     86                return fromMBSz(str.ptr); 
     87            } 
    6588        } 
    6689    } 
     
    7396    class ListBox : WindowChild { 
    7497        GtkTreeView* treeView; 
     98        GtkListStore* model; 
    7599        GtkScrolledWindow* scrolledWnd; 
    76100 
     
    84108                vscroll ? GtkPolicyType.GTK_POLICY_AUTOMATIC : GtkPolicyType.GTK_POLICY_NEVER); 
    85109 
    86             GtkListStore* model = gtk_list_store_new(1, 16<<2); // G_TYPE_STRING 
     110            model = gtk_list_store_new(1, 16<<2); // G_TYPE_STRING 
    87111            GtkTreeIter iter; 
    88112            foreach (s; texts) { 
     
    120144        } 
    121145 
    122         // -1 for custom text 
    123146        int selection() { 
    124             //            GtkComboBox* box = cast(GtkComboBox*)peer; 
    125             //            return gtk_combo_box_get_active(box); 
    126             return 0; 
    127         } 
    128         void selection(int n) { 
    129             //            GtkComboBox* box = cast(GtkComboBox*)peer; 
    130             //            gtk_combo_box_set_active(box,n); 
     147            GtkTreeSelection* sel = gtk_tree_view_get_selection(treeView); 
     148            GtkTreeIter iter; 
     149            if (!gtk_tree_selection_get_selected(sel,null,&iter)) 
     150                return -1; 
     151            GtkTreePath* path = gtk_tree_model_get_path(model, &iter); 
     152            scope(exit) gtk_tree_path_free(path); 
     153            sysAssert(gtk_tree_path_get_depth(path) == 1, "gtk tree path is not 1 deep"); 
     154            gint* indices = gtk_tree_path_get_indices(path); 
     155            sysAssert(indices !is null, "no tree path indices"); 
     156            return *indices; 
    131157        } 
    132158 
    133         char[] text() { 
    134             char* str; 
    135             GtkTreeModel* model = gtk_tree_view_get_model(treeView); 
    136             GtkTreeIter iter; 
    137             if (gtk_tree_model_get_iter_first(model,&iter)) { 
    138                 int n = selection(); 
    139                 GValue* val; 
    140                 while (n--) { 
    141                     gtk_tree_model_iter_next(model,&iter); 
    142                 } 
    143                 gtk_tree_model_get_value(model,&iter,0,val); 
    144                 str = *(cast(char**)val); 
    145                 g_value_unset(val); 
    146             } 
    147             if (str is null) 
    148                 return ""; 
    149             else 
    150                 return str[0..strlen(str)].dup; 
     159        void selection(int i) { 
     160            GtkTreePath* path = gtk_tree_path_new_from_indices(i,-1); 
     161            sysAssert(path !is null, "invalid selection"); 
     162            GtkTreeSelection* sel = gtk_tree_view_get_selection(treeView); 
     163            gtk_tree_selection_select_path(sel, path); 
     164            gtk_tree_path_free(path); 
    151165        } 
    152166 
    153         void text(char[] s) { 
    154             assert(false); 
     167        int count() { 
     168            return gtk_tree_model_iter_n_children(model,null); 
     169        } 
     170 
     171        char[] opIndex(int i) { 
     172            GtkTreePath* path = gtk_tree_path_new_from_indices(i,-1); 
     173            sysAssert(path !is null, "invalid index"); 
     174            scope(exit) gtk_tree_path_free(path); 
     175            GtkTreeIter iter; 
     176            if (!gtk_tree_model_get_iter(model, &iter, path)) 
     177                return null; 
     178            char* str; 
     179            gtk_tree_model_get(model, &iter, 0, &str, -1); 
     180            scope(exit) g_free(str); 
     181            return str[0..strlen(str)].dup; 
    155182        } 
    156183 
    157184        mixin WindowChildImpl!(); 
    158  
    159         /*PeerForAdd getPeerForAdd() { 
    160             return cast(GtkWidget*)treeView; 
    161         }*/ 
    162185 
    163186        Point preferredSize() { 
  • branches/bughunt/samples/widgets.d

    r38 r42  
    1010import minwin.all; 
    1111import std.utf; 
     12import std.stdio; 
    1213 
    1314extern (C) 
     
    4647 
    4748    auto p = new Canvas(win); 
    48     p.keyDelegate ~= delegate void(Component source, KeyEvent* event) { 
     49    p.keyDelegate ~= (Component source, KeyEvent* event) { 
    4950        if (event.id == KeyPressedEvent) { 
    5051            char[4] buf; 
     
    5253        } 
    5354    }; 
    54     p.paintDelegate ~= delegate void(Component source, GContext gc) { 
     55    p.mouseDelegate ~= (Component source, MouseEvent* event) { 
     56        int sel = list.selection; 
     57        char[] txt; 
     58        if (sel >= 0) 
     59            txt = list[sel]; 
     60        else 
     61            txt = "nil"; 
     62        writefln("Listbox selection is index ", sel," of ",list.count," items. text is:",txt); 
     63    }; 
     64    p.paintDelegate ~= (Component source, GContext gc) { 
    5565        assert(source !is null); 
    5666        scope Brush b = new Brush(RGB(250,20,20));