Code

screen_artist: use a list_window paint callback
[ncmpc.git] / src / screen_song.c
index 82e4cd4e1376456d50176a760528576db3b4325c..b56b5510d2331f2bee6774d0893258e82929ccb1 100644 (file)
@@ -76,19 +76,11 @@ screen_song_repaint(void)
 }
 
 static const char *
-screen_song_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
-                         G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
+screen_song_list_callback(unsigned idx, G_GNUC_UNUSED void *data)
 {
-       static char buffer[256];
-       char *value;
-
        assert(idx < current.lines->len);
 
-       value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
-       g_strlcpy(buffer, value, sizeof(buffer));
-       g_free(value);
-
-       return buffer;
+       return g_ptr_array_index(current.lines, idx);
 }
 
 
@@ -115,8 +107,7 @@ screen_song_exit(void)
 static void
 screen_song_resize(int cols, int rows)
 {
-       lw->cols = cols;
-       lw->rows = rows;
+       list_window_resize(lw, cols, rows);
 }
 
 static const char *
@@ -136,53 +127,55 @@ screen_song_paint(void)
 static void
 screen_song_append(const char *label, const char *value, unsigned label_col)
 {
-       int value_col, linebreaks, entry_size, label_size;
-       int i, k;
+       unsigned label_width = locale_width(label) + 2;
+       int value_col, label_size;
        gchar *entry, *entry_iter;
        const gchar *value_iter;
+       char *p, *q;
+       unsigned width;
 
        assert(label != NULL);
-       assert(g_utf8_validate(label, -1, NULL));
-
-       if (value != NULL) {
-               assert(g_utf8_validate(value, -1, NULL));
-               /* +2 for ': ' */
-               label_col += 2;
-               value_col = lw->cols - label_col;
-               /* calculate the number of required linebreaks */
-               linebreaks = (utf8_width(value) - 1) / value_col + 1;
-               value_iter = value;
-               label_size = strlen(label) + label_col - utf8_width(label);
-               entry_size = label_size + strlen(value) + 2;
-
-               for (i = 0; i < linebreaks; ++i)
-               {
-                       entry = g_malloc(entry_size);
-                       if (i == 0) {
-                               entry_iter = entry + g_sprintf(entry, "%s: ", label);
-                               /* fill the label column with whitespaces */
-                               for ( ; entry_iter < entry + label_size; ++entry_iter)
-                                       *entry_iter = ' ';
-                       }
-                       else {
-                               entry_iter = entry;
-                               /* fill the label column with whitespaces */
-                               for ( ; entry_iter < entry + label_col; ++entry_iter)
-                                       *entry_iter = ' ';
-                       }
-                       /* skip whitespaces */
-                       while (g_ascii_isspace(*value_iter)) ++value_iter;
-                       k = 0;
-                       while (value_iter && k < value_col)
-                       {
-                               g_utf8_strncpy(entry_iter, value_iter, 1);
-                               value_iter = g_utf8_find_next_char(value_iter, NULL);
-                               entry_iter = g_utf8_find_next_char(entry_iter, NULL);
-                               ++k;
-                       }
-                       *entry_iter = '\0';
-                       g_ptr_array_add(current.lines, entry);
+       assert(value != NULL);
+       assert(g_utf8_validate(value, -1, NULL));
+
+       /* +2 for ': ' */
+       label_col += 2;
+       value_col = lw->cols - label_col;
+       /* calculate the number of required linebreaks */
+       value_iter = value;
+       label_size = strlen(label) + label_col;
+
+       while (*value_iter != 0) {
+               entry = g_malloc(label_size);
+               if (value_iter == value) {
+                       entry_iter = entry + g_sprintf(entry, "%s: ", label);
+                       /* fill the label column with whitespaces */
+                       memset(entry_iter, ' ', label_col - label_width);
+                       entry_iter += label_col - label_width;
                }
+               else {
+                       /* fill the label column with whitespaces */
+                       memset(entry, ' ', label_col);
+                       entry_iter = entry + label_col;
+               }
+               /* skip whitespaces */
+               while (g_ascii_isspace(*value_iter)) ++value_iter;
+
+               p = g_strdup(value_iter);
+               width = utf8_cut_width(p, value_col);
+               if (width == 0)
+                       /* not enough room for anything - bail out */
+                       break;
+
+               *entry_iter = 0;
+
+               value_iter += strlen(p);
+               p = replace_utf8_to_locale(p);
+               q = g_strconcat(entry, p, NULL);
+               g_free(entry);
+               g_free(p);
+
+               g_ptr_array_add(current.lines, q);
        }
 }
 
@@ -255,10 +248,8 @@ screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c)
        screen_song_append_tag(labels[COMMENT], song, MPD_TAG_COMMENT,
                               max_label_width);
        screen_song_append(_("Path"), mpd_song_get_uri(song), max_label_width);
-       if (c->status != NULL && c->song != NULL &&
-           strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0 &&
-           (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
-            mpd_status_get_state(c->status) == MPD_STATE_PAUSE)) {
+       if (mpdclient_is_playing(c) && c->song != NULL &&
+           strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0) {
                char buf[16];
                g_snprintf(buf, sizeof(buf), _("%d kbps"),
                           mpd_status_get_kbit_rate(c->status));
@@ -333,6 +324,8 @@ screen_song_add_stats(struct mpd_connection *connection)
 static void
 screen_song_update(struct mpdclient *c)
 {
+       struct mpd_connection *connection;
+
        /* Clear all lines */
        for (guint i = 0; i < current.lines->len; ++i)
                g_free(g_ptr_array_index(current.lines, i));
@@ -349,17 +342,13 @@ screen_song_update(struct mpdclient *c)
                        (c->song == NULL ||
                         strcmp(mpd_song_get_uri(current.selected_song),
                                mpd_song_get_uri(c->song)) != 0 ||
-                        c->status == NULL ||
-                        (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
-                         mpd_status_get_state(c->status) != MPD_STATE_PAUSE))) {
+                        !mpdclient_is_playing(c))) {
                g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
                screen_song_add_song(current.selected_song, c);
                g_ptr_array_add(current.lines, g_strdup("\0"));
        }
 
-       if (c->song != NULL && c->status != NULL &&
-           (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
-            mpd_status_get_state(c->status) != MPD_STATE_PAUSE)) {
+       if (c->song != NULL && mpdclient_is_playing(c)) {
                if (current.played_song != NULL) {
                        mpd_song_free(current.played_song);
                }
@@ -370,8 +359,8 @@ screen_song_update(struct mpdclient *c)
        }
 
        /* Add some statistics about mpd */
-       if (mpdclient_is_connected(c) &&
-           !screen_song_add_stats(mpdclient_get_connection(c)))
+       connection = mpdclient_get_connection(c);
+       if (connection != NULL && !screen_song_add_stats(connection))
                mpdclient_handle_error(c);
 
        list_window_set_length(lw, current.lines->len);