gucharmap-chapters-view.c 8.64 KB
Newer Older
1
/*
Christian Persch's avatar
Christian Persch committed
2
 * Copyright © 2004 Noah Levitt
3 4 5
 *
 * 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
Christian Persch's avatar
Christian Persch committed
6
 * Free Software Foundation; either version 3 of the License, or (at your
7 8 9 10 11 12 13 14 15
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
17 18
 */

Christian Persch's avatar
Christian Persch committed
19
#include <config.h>
20 21
#include <string.h>
#include <gtk/gtk.h>
22 23

#include "gucharmap.h"
24
#include "gucharmap-private.h"
25

26 27
#include "unicode-blocks.h"

28 29 30 31 32
struct _GucharmapChaptersViewPrivate {
  GtkTreeViewColumn *column;
  GucharmapChaptersModel *model;
};

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
static void
select_iter (GucharmapChaptersView *view,
             GtkTreeIter *iter)
{
  GtkTreeView *tree_view = GTK_TREE_VIEW (view);
  GtkTreeSelection *selection;
  GtkTreePath *path;

  selection = gtk_tree_view_get_selection (tree_view);
  gtk_tree_selection_select_iter (selection, iter);

  path = gtk_tree_model_get_path (gtk_tree_view_get_model (tree_view), iter);
  gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
  gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.5, 0);
  gtk_tree_path_free (path);
}

static void
gucharmap_chapters_view_init (GucharmapChaptersView *view)
{
53
  GucharmapChaptersViewPrivate *priv;
54 55 56 57 58
  GtkTreeView *tree_view = GTK_TREE_VIEW (view);
  GtkCellRenderer *cell;
  GtkTreeViewColumn *column;
  GtkTreeSelection *selection;

59 60
  priv = view->priv = G_TYPE_INSTANCE_GET_PRIVATE (view, GUCHARMAP_TYPE_CHAPTERS_VIEW, GucharmapChaptersViewPrivate);

61
  cell = gtk_cell_renderer_text_new ();
62 63 64 65
  g_object_set (cell, 
                "ellipsize", PANGO_ELLIPSIZE_END,
                "ellipsize-set", TRUE,
                NULL);
66
  column = priv->column = gtk_tree_view_column_new ();
67
  gtk_tree_view_column_pack_start (column, cell, FALSE);
68
  gtk_tree_view_column_add_attribute (column, cell, "text", GUCHARMAP_CHAPTERS_MODEL_COLUMN_LABEL);
69 70 71 72
  gtk_tree_view_append_column (tree_view, column);

  selection = gtk_tree_view_get_selection (tree_view);
  gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
73 74

  gtk_tree_view_set_enable_search (tree_view, TRUE);
75 76 77 78 79
}

static void
gucharmap_chapters_view_class_init (GucharmapChaptersViewClass *klass)
{
80
  g_type_class_add_private (klass, sizeof (GucharmapChaptersViewPrivate));
81 82 83 84
}

G_DEFINE_TYPE (GucharmapChaptersView, gucharmap_chapters_view, GTK_TYPE_TREE_VIEW)

85 86 87 88 89 90 91
/**
 * gucharmap_chapters_view_new:
 *
 * Creates a new #GucharmapChaptersView object.
 *
 * Returns: (transfer full): a new #GucharmapChaptersView
 */
92 93 94 95 96 97
GtkWidget * 
gucharmap_chapters_view_new (void)
{
  return g_object_new (gucharmap_chapters_view_get_type (), NULL);
}

98 99 100 101 102 103 104 105
/**
 * gucharmap_chapters_view_get_model:
 * @view: a #GucharmapChaptersView
 *
 * Gets the #GucharmapChaptersModel of #view.
 *
 * Returns: (transfer none): the #GucharmapChaptersModel
 */
106 107 108
GucharmapChaptersModel *
gucharmap_chapters_view_get_model (GucharmapChaptersView *view)
{
109 110 111
  GucharmapChaptersViewPrivate *priv = view->priv;

  return priv->model;
112 113
}

114 115 116 117
void
gucharmap_chapters_view_set_model (GucharmapChaptersView *view,
                                   GucharmapChaptersModel *model)
{
118
  GucharmapChaptersViewPrivate *priv = view->priv;
119 120
  GtkTreeView *tree_view = GTK_TREE_VIEW (view);

121
  priv->model = model;
122 123 124 125
  gtk_tree_view_set_model (tree_view, GTK_TREE_MODEL (model));
  if (!model)
    return;

126
  gtk_tree_view_column_set_title (priv->column, gucharmap_chapters_model_get_title (model));
127 128 129

  /* Need to re-set this here since it's set to -1 when the tree view model changes! */
  gtk_tree_view_set_search_column (tree_view, GUCHARMAP_CHAPTERS_MODEL_COLUMN_LABEL);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
}

/**
 * gucharmap_view_view_next:
 * @view: a #GucharmapChapters
 *
 * Moves to the next chapter if applicable.
 **/
void
gucharmap_chapters_view_next (GucharmapChaptersView *view)
{
  GtkTreeView *tree_view = GTK_TREE_VIEW (view);
  GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
  GtkTreeModel *model;
  GtkTreePath *path;
  GtkTreeIter iter;

  if (!gtk_tree_selection_get_selected (selection, &model, &iter))
    return;
  if (!gtk_tree_model_iter_next (model, &iter))
    return;
   
  path = gtk_tree_model_get_path (model, &iter);
  gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
  gtk_tree_path_free (path);
}

/**
 * gucharmap_chapters_view_previous:
 * @view: a #GucharmapChapters
 *
 * Moves to the previous chapter if applicable.
 **/
void
gucharmap_chapters_view_previous (GucharmapChaptersView *view)
{
  GtkTreeView *tree_view = GTK_TREE_VIEW (view);
  GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
  GtkTreeModel *model;
  GtkTreePath *path;
  GtkTreeIter iter;

  if (!gtk_tree_selection_get_selected (selection, &model, &iter))
    return;

  path = gtk_tree_model_get_path (model, &iter);
  if (gtk_tree_path_prev (path))
    gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
  gtk_tree_path_free (path);
}


/**
 * gucharmap_chapter_view_get_selected:
 * @view: a #GucharmapChapters
 *
 * Returns a newly allocated string containing
 * the name of the currently selected chapter
 **/
gchar*
gucharmap_chapters_view_get_selected (GucharmapChaptersView *view)
{
  GtkTreeView *tree_view = GTK_TREE_VIEW (view);
  GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
  GtkTreeModel *model;
  GtkTreeIter iter;
  gchar *name = NULL;

  if (gtk_tree_selection_get_selected (selection, &model, &iter))
199
    gtk_tree_model_get(model, &iter, GUCHARMAP_CHAPTERS_MODEL_COLUMN_ID, &name, -1);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

  return name;
}

/**
 * gucharmap_chapter_view_set_selected:
 * @view: a #GucharmapChapters
 * @name: 
 *
 * Sets the selection to the row specified by @name
 * Return value: %TRUE on success, %FALSE on failure
 **/
gboolean
gucharmap_chapters_view_set_selected (GucharmapChaptersView *view,
                                      const char        *name)
{
216
  GucharmapChaptersViewPrivate *priv = view->priv;
217 218
  GtkTreeIter iter;

219
  if (!gucharmap_chapters_model_id_to_iter (priv->model, name, &iter))
220 221 222 223 224 225 226
    return FALSE;

  select_iter (view, &iter);
  return TRUE;
}

/**
Christian Persch's avatar
Christian Persch committed
227
 * gucharmap_chapters_view_select_character:
228 229 230 231 232 233 234 235 236
 * @view: a #GucharmapChaptersView
 * @wc: a character
 *
 * Return value: %TRUE on success, %FALSE on failure.
 **/
gboolean
gucharmap_chapters_view_select_character (GucharmapChaptersView *view,
                                          gunichar           wc)
{
237
  GucharmapChaptersViewPrivate *priv = view->priv;
238 239
  GtkTreeIter iter;

240
  g_return_val_if_fail (GUCHARMAP_IS_CHAPTERS_VIEW (view), FALSE);
241 242 243 244

  if (wc > UNICHAR_MAX)
    return FALSE;

245
  if (!gucharmap_chapters_model_character_to_iter (priv->model, wc, &iter))
246 247 248 249 250 251 252 253 254 255 256 257 258
    return FALSE;

  select_iter (view, &iter);
  return TRUE;
}

/**
 * gucharmap_chapters_view_get_codepoint_list:
 * @view: a #GucharmapChaptersView
 *
 * Creates a new #GucharmapCodepointList representing the characters in the
 * current chapter.
 *
259 260 261
 * Return value: (transfer full): the newly-created #GucharmapCodepointList,
 * or NULL if there is no chapter selected. The caller should release the
 * result with g_object_unref() when finished.
262 263 264 265
 **/
GucharmapCodepointList * 
gucharmap_chapters_view_get_codepoint_list (GucharmapChaptersView *view)
{
266
  GucharmapChaptersViewPrivate *priv = view->priv;
267 268 269
  GtkTreeSelection *selection;
  GtkTreeIter iter;
  
270
  g_return_val_if_fail (GUCHARMAP_IS_CHAPTERS_VIEW (view), NULL);
271 272 273 274 275

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
  if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
    return NULL;

276
  return gucharmap_chapters_model_get_codepoint_list (priv->model, &iter);
277 278 279
}

/**
280
 * gucharmap_chapters_view_get_book_codepoint_list:
281 282
 * @view: a #GucharmapChaptersView
 *
283 284 285
 * Return value: (transfer full): a reference to a #GucharmapCodepointList representing
 * all the characters in all the chapters. It should not be modified, but must
 * be g_object_unref()'d after use.
286
 **/
287
GucharmapCodepointList *
288 289
gucharmap_chapters_view_get_book_codepoint_list (GucharmapChaptersView *view)
{
290 291
  GucharmapChaptersViewPrivate *priv = view->priv;

292
  g_return_val_if_fail (GUCHARMAP_IS_CHAPTERS_VIEW (view), NULL);
293

294
  return gucharmap_chapters_model_get_book_codepoint_list (priv->model);
295
}
296 297 298 299 300 301 302

gboolean
gucharmap_chapters_view_select_locale (GucharmapChaptersView *view)
{
  return gucharmap_chapters_view_select_character (view,
                                                   gucharmap_unicode_get_locale_character ());
}