From 3c1675731caaac73d6c362075ffb82bc45d2f060 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sun, 4 Sep 2022 10:09:42 +0200 Subject: [PATCH] handle btn1 in sidebar, switchable tracks --- application/Sidebar.c | 54 ++++++++++++++++++++++++++++++------------ application/Sidebar.h | 2 ++ application/SidebarP.h | 2 ++ application/player.c | 8 +++++++ application/playlist.c | 21 +++++++++------- application/playlist.h | 2 ++ application/window.c | 7 +++--- application/window.h | 2 ++ 8 files changed, 71 insertions(+), 27 deletions(-) diff --git a/application/Sidebar.c b/application/Sidebar.c index d6b3ff4..4faff71 100644 --- a/application/Sidebar.c +++ b/application/Sidebar.c @@ -43,9 +43,10 @@ static void sidebar_expose(Widget widget, XEvent *event, Region region); static Boolean sidebar_set_values(Widget old, Widget request, Widget neww, ArgList args, Cardinal *num_args); static void sidebar_insert_child(Widget child); Boolean sidebar_acceptfocus(Widget widget, Time *time); -static void FocusInAP(Widget w, XEvent *event, String *args, Cardinal *nArgs); -static void xdndEnterAP(Widget w, XEvent *event, String *args, Cardinal *nArgs); +static void FocusInAP(Widget w, XEvent *event, String *args, Cardinal *nArgs); +static void SelectElmAP(Widget w, XEvent *event, String *args, Cardinal *nArgs); +static void PopupAP(Widget w, XEvent *event, String *args, Cardinal *nArgs); static void sidebar_xdnd_callback(Widget w, XtPointer udata, XtPointer cdata); @@ -56,12 +57,13 @@ static XtResource resources[] = { static XtActionsRec actionslist[] = { {"focusIn", FocusInAP}, - {"xdnd_enter", xdndEnterAP}, - {"NULL", NULL} + {"selectElm", SelectElmAP}, + {"popup", PopupAP} }; -static char defaultTranslations[] = ": focusIn()\n" - "XdndEnter: xdnd_enter()\n"; +static char defaultTranslations[] = ": focusIn()\n" + ": selectElm()\n" + ": popup()"; static XtResource constraints[] = {}; @@ -156,6 +158,8 @@ static void sidebar_class_part_init (WidgetClass wc) { static void sidebar_init(Widget request, Widget neww, ArgList args, Cardinal *num_args) { Sidebar sb = (Sidebar)neww; + // initialize everything except XftDraw or other stuff that needs a Window + XftColor fg, bg; fg.color.red = 0; fg.color.green = 0; @@ -175,6 +179,11 @@ static void sidebar_init(Widget request, Widget neww, ArgList args, Cardinal *nu fprintf(stderr, "Cannot open font.\nAbort.\n"); exit(-1); } + + XftFont *xftFont = sb->sidebar.font->fonts->font; + int padding = 2; + int fontheight = xftFont->ascent; + sb->sidebar.elmHeight = fontheight + 2*padding; } @@ -216,17 +225,20 @@ static void sidebar_realize(Widget widget, XtValueMask *mask, XSetWindowAttribut } static void sidebar_expose(Widget widget, XEvent *event, Region region) { - printf("expose\n"); + //printf("expose\n"); Dimension w, h; XtMakeResizeRequest(widget, 200, 200, &w, &h); Sidebar s = (Sidebar)widget; + XftFont *xftFont = s->sidebar.font->fonts->font; + XftDrawRect(s->sidebar.d, &s->sidebar.bg, 0, 0, s->core.width, s->core.height); - int height = 20; + int fontheight = xftFont->ascent; + int height = s->sidebar.elmHeight; - printf("current track: %d\n", s->sidebar.window->playlist.current_track); + //printf("current track: %d\n", s->sidebar.window->playlist.current_track); int i = 0; UCX_FOREACH(elm, s->sidebar.window->playlist.tracks) { @@ -237,7 +249,7 @@ static void sidebar_expose(Widget widget, XEvent *event, Region region) { cg = &s->sidebar.bg; } - XftDrawString8(s->sidebar.d, cg, s->sidebar.font->fonts->font, 10, i*height + 15, (const FcChar8*)name, strlen(name)); + XftDrawString8(s->sidebar.d, cg, s->sidebar.font->fonts->font, 10, i*height + xftFont->ascent, (const FcChar8*)name, strlen(name)); i++; } @@ -259,15 +271,23 @@ Boolean sidebar_acceptfocus(Widget widget, Time *time) { static void FocusInAP(Widget w, XEvent *event, String *args, Cardinal *nArgs) { - printf("focusin\n"); - fflush(stdout); + } -static void xdndEnterAP(Widget w, XEvent *event, String *args, Cardinal *nArgs) { - printf("xdndEnterAP\n"); - fflush(stdout); +static void SelectElmAP(Widget w, XEvent *event, String *args, Cardinal *nArgs) { + //printf("btn1\n"); + XButtonEvent *e = &event->xbutton; + Sidebar s = (Sidebar)w; + + int selected = e->y / s->sidebar.elmHeight; + PlayListPlayTrack(s->sidebar.window, selected); + + SidebarRepaint(w); } +static void PopupAP(Widget w, XEvent *event, String *args, Cardinal *nArgs) { + //printf("btn3\n"); +} @@ -345,3 +365,7 @@ void SidebarSetWindow(Widget widget, MainWindow *win) { Sidebar sb = (Sidebar)widget; sb->sidebar.window = win; } + +void SidebarRepaint(Widget widget) { + XClearArea(XtDisplay(widget), XtWindow(widget), 0, 0, widget->core.width, widget->core.height, true); +} diff --git a/application/Sidebar.h b/application/Sidebar.h index 2109bc8..96a7586 100644 --- a/application/Sidebar.h +++ b/application/Sidebar.h @@ -40,6 +40,8 @@ Widget CreateSidebar( void SidebarSetWindow(Widget widget, MainWindow *win); +void SidebarRepaint(Widget widget); + #ifdef __cplusplus } diff --git a/application/SidebarP.h b/application/SidebarP.h index 6ff771e..c483a90 100644 --- a/application/SidebarP.h +++ b/application/SidebarP.h @@ -58,6 +58,8 @@ typedef struct SidebarPart { XftColor fg; XftColor bg; + int elmHeight; + MainWindow *window; } SidebarPart; diff --git a/application/player.c b/application/player.c index 42f09e7..be4d77f 100644 --- a/application/player.c +++ b/application/player.c @@ -242,6 +242,11 @@ static int connect_to_ipc(Player *player) { return 0; } +static Boolean update_player_window(XtPointer data) { + MainWindow *win = data; + WindowUpdate(win); +} + static void* start_player(void *data) { MainWindow *win = data; @@ -272,6 +277,9 @@ static void* start_player(void *data) { } win->player = player; + // update main window + AppExecProc(update_player_window, win); + // IO player_io(player); diff --git a/application/playlist.c b/application/playlist.c index 06172c3..80d335b 100644 --- a/application/playlist.c +++ b/application/playlist.c @@ -54,13 +54,18 @@ void PlayListPlayNext(MainWindow *win, bool force) { current = 0; } - win->playlist.current_track = current; - - UcxList *fileElm = ucx_list_get(tracks, current); - if(!fileElm) { - return; + PlayListPlayTrack(win, current); +} + +void PlayListPlayTrack(MainWindow *win, int i) { + UcxList *tracks = win->playlist.tracks; + if(i < ucx_list_size(tracks)) { + win->playlist.current_track = i; + UcxList *fileElm = ucx_list_get(tracks, i); + if(fileElm) { + win->file = fileElm->data; + PlayerOpenFile(win); + win->playlist.current_track = i; + } } - win->file = fileElm->data; - - PlayerOpenFile(win); } diff --git a/application/playlist.h b/application/playlist.h index abb4e62..5e1a0b8 100644 --- a/application/playlist.h +++ b/application/playlist.h @@ -35,6 +35,8 @@ void PlayListAddFile(MainWindow *win, const char *file); void PlayListPlayNext(MainWindow *win, bool force); +void PlayListPlayTrack(MainWindow *win, int i); + #ifdef __cplusplus } #endif diff --git a/application/window.c b/application/window.c index eb6335a..1b351a8 100644 --- a/application/window.c +++ b/application/window.c @@ -698,7 +698,6 @@ void WindowShowSidebar(MainWindow *win) { XtVaSetValues(win->player_widget, XmNrightAttachment, XmATTACH_WIDGET, XmNrightWidget, win->sidebar, NULL); } - - - - +void WindowUpdate(MainWindow *win) { + SidebarRepaint(win->sidebar); +} diff --git a/application/window.h b/application/window.h index 617ff71..c00f29e 100644 --- a/application/window.h +++ b/application/window.h @@ -109,6 +109,8 @@ void WindowHandlePlayerEvent(MainWindow *win, XEvent *event); void WindowHideSidebar(MainWindow *win); void WindowShowSidebar(MainWindow *win); +void WindowUpdate(MainWindow *win); + #ifdef __cplusplus } -- 2.43.5