ucx update
[uwplayer.git] / application / Sidebar.c
index 4faff71..0b96328 100644 (file)
@@ -31,6 +31,7 @@
 #include "xdnd.h"
 #include "utils.h"
 #include "playlist.h"
+#include <cx/string.h>
 
 
 static void sidebar_class_init(void);
@@ -192,8 +193,18 @@ static void sidebar_destroy(Widget widget) {
     
 }
 
+static int testresize = 1;
 static void sidebar_resize(Widget widget) {
-    
+    if(testresize) {
+        XtWidgetGeometry geom;
+        geom.request_mode = CWWidth | CWHeight;
+        geom.width = 200;
+        geom.height = 2000;
+        XtWidgetGeometry result;
+        //XtMakeGeometryRequest(widget, &geom, &result);
+        testresize = 0;
+    }
+    printf("resize\n");
 }
 
 static int xdnd_initialized = 0;
@@ -222,36 +233,56 @@ static void sidebar_realize(Widget widget, XtValueMask *mask, XSetWindowAttribut
             XtWindow(widget),
             visual,
             widget->core.colormap);
+    
+    Dimension w, h;
+    
+    Widget parent = XtParent(widget);
+    XtVaSetValues(parent, XmNbackground, WhitePixelOfScreen(XtScreen(parent)), NULL);
+    
+    XtMakeResizeRequest(widget, 200, 100, &w, &h);
+     
 }
 
 static void sidebar_expose(Widget widget, XEvent *event, Region        region) {
     //printf("expose\n");
     Dimension w, h;
-    XtMakeResizeRequest(widget, 200, 200, &w, &h);
+    //XtMakeResizeRequest(widget, 200, 2000, &w, &h);
     
-    Sidebar s = (Sidebar)widget;
-    XftFont *xftFont = s->sidebar.font->fonts->font;
+    w = widget->core.width;
+    h = widget->core.height;
     
+    Widget parent = XtParent(widget);
     
-    XftDrawRect(s->sidebar.d, &s->sidebar.bg, 0, 0, s->core.width, s->core.height);
+    Sidebar s = (Sidebar)widget;
+    XftFont *xftFont = s->sidebar.font->fonts->font;
+    CxList *tracks = s->sidebar.window->playlist.tracks;
+    size_t numTracks = tracks->size;
     
     int fontheight = xftFont->ascent;
     int height = s->sidebar.elmHeight;
     
+    
+    int list_height = numTracks * height;
+    if((list_height > s->core.height) || (w < parent->core.width)) {
+        XtMakeResizeRequest(widget, parent->core.width, list_height + 5, &w, &h);
+    }
+    
+    
+    XftDrawRect(s->sidebar.d, &s->sidebar.bg, 0, 0, w, h);
+    
+    
     //printf("current track: %d\n", s->sidebar.window->playlist.current_track);
     
-    int i = 0;
-    UCX_FOREACH(elm, s->sidebar.window->playlist.tracks) {
-        char *name = util_resource_name(elm->data);
+    CxIterator i = cxListIterator(s->sidebar.window->playlist.tracks);
+    cx_foreach(const char *, elm, i) {
+        const char *name = util_resource_name(elm);
         XftColor *cg = &s->sidebar.fg;
-        if(i == s->sidebar.window->playlist.current_track) {
-            XftDrawRect(s->sidebar.d, &s->sidebar.fg, 0, i*height, s->core.width, height);
+        if(i.index == s->sidebar.window->playlist.current_track) {
+            XftDrawRect(s->sidebar.d, &s->sidebar.fg, 0, i.index*height, s->core.width, height);
             cg = &s->sidebar.bg;
         }
         
-        XftDrawString8(s->sidebar.d, cg, s->sidebar.font->fonts->font, 10, i*height + xftFont->ascent, (const FcChar8*)name, strlen(name));
-        
-        i++;
+        XftDrawString8(s->sidebar.d, cg, s->sidebar.font->fonts->font, 10, i.index*height + xftFont->ascent, (const FcChar8*)name, strlen(name));
     }
 }
 
@@ -290,16 +321,9 @@ static void PopupAP(Widget w, XEvent *event, String *args, Cardinal *nArgs) {
 }
 
 
-
-static void sidebar_xdnd_callback(Widget w, XtPointer udata, XtPointer cdata) {
-    printf("xdnd\n");
-    fflush(stdout);
-    
-    Sidebar s = (Sidebar)cdata;
-    
-    char *urilist = udata;
-    
-    size_t len = strlen(urilist);
+static void open_uri(Sidebar s, cxstring uri) {
+    const char *urilist = uri.ptr;
+    size_t len = uri.length;
     
     size_t start = 0;
     if(len > 7 && !memcmp(urilist, "file://", 7)) {
@@ -349,6 +373,23 @@ static void sidebar_xdnd_callback(Widget w, XtPointer udata, XtPointer cdata) {
     free(path);
 }
 
+static void sidebar_xdnd_callback(Widget w, XtPointer udata, XtPointer cdata) {
+    printf("xdnd\n");
+    fflush(stdout);
+    
+    Sidebar s = (Sidebar)cdata;
+    
+    cxstring urllist = cx_str(udata);
+    
+    CxStrtokCtx tk = cx_strtok(urllist, cx_str("\r\n"), INT_MAX);
+    cxstring uri;
+    while(cx_strtok_next(&tk, &uri)) {
+        open_uri(s, uri);
+    }
+    
+    SidebarRepaint((Widget)s);
+}
+
 
 /* --------------------------- public --------------------------- */