[gedit-cossa] Add "All Samples" action.
- From: Ignacio Casal Quinteiro <icq src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit-cossa] Add "All Samples" action.
- Date: Tue, 14 Jun 2011 15:38:59 +0000 (UTC)
commit 944f4172ac91ba4391ab924aa82a22ac9df6cf0e
Author: Ignacio Casal Quinteiro <icq gnome org>
Date: Tue Jun 14 17:41:06 2011 +0200
Add "All Samples" action.
By default all samples are shown in the previewer window
src/cossa-previewer.c | 209 ++++++++++++++++++++++++++++++++++++++++++-------
src/cossa-previewer.h | 2 +
src/cossa-window.c | 28 ++++++-
3 files changed, 206 insertions(+), 33 deletions(-)
---
diff --git a/src/cossa-previewer.c b/src/cossa-previewer.c
index 8de6685..15dd012 100644
--- a/src/cossa-previewer.c
+++ b/src/cossa-previewer.c
@@ -28,6 +28,8 @@
#define BORDER_WIDTH 10
#define PREVIEW_PADDING 10
#define SHADOW_OFFSET 5
+#define SAMPLES_PER_ROW 3
+#define SAMPLE_OFFSET 10
typedef struct _CossaPreviewerPrivate CossaPreviewerPrivate;
typedef struct _SampleData SampleData;
@@ -50,6 +52,7 @@ struct _CossaPreviewerPrivate
CossaZoomLevel zoom_level;
GList *samples;
GList *cur_sample;
+ gboolean show_all;
GtkCssProvider *provider;
};
@@ -172,41 +175,24 @@ cossa_previewer_finalize (GObject *object)
G_OBJECT_CLASS (cossa_previewer_parent_class)->finalize (object);
}
-static gboolean
-cossa_previewer_draw (GtkWidget *widget,
- cairo_t *cr)
+static void
+draw_sample (GtkWidget *widget,
+ SampleData *sample,
+ cairo_t *cr)
{
- CossaPreviewer *previewer = (CossaPreviewer *) widget;
+ CossaPreviewer *previewer = COSSA_PREVIEWER (widget);
CossaPreviewerPrivate *priv = previewer->priv;
- GtkStyleContext *context;
- GtkAllocation allocation;
GtkAllocation child_alloc;
- SampleData *sample;
+ GtkStyleContext *context;
gint w, h;
context = gtk_widget_get_style_context (widget);
- if (!priv->cur_sample)
- return FALSE;
-
- gtk_widget_get_allocation (widget, &allocation);
-
- sample = priv->cur_sample->data;
gtk_widget_get_allocation (sample->widget, &child_alloc);
-
- cairo_save (cr);
-
w = child_alloc.width * priv->zoom_level;
h = child_alloc.height * priv->zoom_level;
- gtk_style_context_save (context);
- gtk_style_context_add_class (context, "previewer-background");
- gtk_render_background (context, cr, 0, 0, (gdouble) allocation.width, (gdouble) allocation.height);
- gtk_style_context_restore (context);
-
- cairo_translate (cr,
- allocation.width / 2 - w / 2,
- allocation.height / 2 - h / 2);
+ cairo_save (cr);
/* Render shadow first */
gtk_style_context_save (context);
@@ -230,6 +216,85 @@ cossa_previewer_draw (GtkWidget *widget,
cairo_fill (cr);
cairo_restore (cr);
+}
+
+static gboolean
+cossa_previewer_draw (GtkWidget *widget,
+ cairo_t *cr)
+{
+ CossaPreviewer *previewer = (CossaPreviewer *) widget;
+ CossaPreviewerPrivate *priv = previewer->priv;
+ GtkStyleContext *context;
+ GtkAllocation allocation;
+ GtkAllocation child_alloc;
+ SampleData *sample;
+ gint w, h;
+
+ context = gtk_widget_get_style_context (widget);
+
+ gtk_widget_get_allocation (widget, &allocation);
+
+ cairo_save (cr);
+
+ gtk_style_context_save (context);
+ gtk_style_context_add_class (context, "previewer-background");
+ gtk_render_background (context, cr, 0, 0, (gdouble) allocation.width, (gdouble) allocation.height);
+ gtk_style_context_restore (context);
+
+ if (priv->show_all)
+ {
+ GList *l;
+ gint y;
+
+ y = SAMPLE_OFFSET;
+ l = priv->samples;
+ while (l)
+ {
+ gint i;
+ gint x;
+
+ h = 0;
+ x = SAMPLE_OFFSET;
+
+ for (i = 0; i < SAMPLES_PER_ROW; i++)
+ {
+ sample = l->data;
+
+ gtk_widget_get_allocation (sample->widget, &child_alloc);
+ w = child_alloc.width * priv->zoom_level;
+ h = MAX (h, child_alloc.height * priv->zoom_level);
+
+ cairo_save (cr);
+ cairo_translate (cr, x, y);
+ draw_sample (widget, sample, cr);
+ cairo_restore (cr);
+
+ x += w + SAMPLE_OFFSET;
+ l = l->next;
+ if (!l)
+ goto end_draw;
+ }
+
+ y += h + SAMPLE_OFFSET;
+ l = l->next;
+ }
+ }
+ else if (priv->cur_sample)
+ {
+ sample = priv->cur_sample->data;
+
+ gtk_widget_get_allocation (sample->widget, &child_alloc);
+ w = child_alloc.width * priv->zoom_level;
+ h = child_alloc.height * priv->zoom_level;
+
+ cairo_translate (cr,
+ allocation.width / 2 - w / 2,
+ allocation.height / 2 - h / 2);
+ draw_sample (widget, sample, cr);
+ }
+
+end_draw:
+ cairo_restore (cr);
return FALSE;
}
@@ -248,7 +313,38 @@ cossa_previewer_get_preferred_width (GtkWidget *widget,
CossaPreviewer *previewer = (CossaPreviewer *) widget;
CossaPreviewerPrivate *priv = previewer->priv;
- if (priv->cur_sample)
+ if (priv->show_all)
+ {
+ GList *sample;
+ gint width = 0;
+ gint row_width;
+
+ sample = priv->samples;
+ while (sample)
+ {
+ gint i;
+
+ row_width = 0;
+
+ for (i = 0; i < SAMPLES_PER_ROW; i++)
+ {
+ SampleData *data = sample->data;
+
+ row_width += cairo_image_surface_get_width (data->surface) + SAMPLE_OFFSET;
+ sample = sample->next;
+ if (!sample)
+ goto end_width;
+ }
+
+ width = MAX (width, row_width);
+ sample = sample->next;
+ }
+
+end_width:
+ width = MAX (width, row_width);
+ *minimum_width = *natural_width = width;
+ }
+ else if (priv->cur_sample)
{
SampleData *data = priv->cur_sample->data;
@@ -265,7 +361,38 @@ cossa_previewer_get_preferred_height_for_width (GtkWidget *widget,
CossaPreviewer *previewer = (CossaPreviewer *) widget;
CossaPreviewerPrivate *priv = previewer->priv;
- if (priv->cur_sample)
+ if (priv->show_all)
+ {
+ GList *sample;
+ gint height = 0;
+ gint row_height;
+
+ sample = priv->samples;
+ while (sample)
+ {
+ gint i;
+
+ row_height = 0;
+
+ for (i = 0; i < SAMPLES_PER_ROW; i++)
+ {
+ SampleData *data = sample->data;
+
+ row_height = MAX (row_height, cairo_image_surface_get_height (data->surface));
+ sample = sample->next;
+ if (!sample)
+ goto end_height;
+ }
+
+ height += row_height;
+ sample = sample->next;
+ }
+
+end_height:
+ height += row_height;
+ *minimum_height = *natural_height = height;
+ }
+ else if (priv->cur_sample)
{
SampleData *data = priv->cur_sample->data;
@@ -361,7 +488,9 @@ cossa_previewer_add_sample (CossaPreviewer *previewer,
priv->samples = g_list_prepend (priv->samples, sample);
- if (!priv->cur_sample)
+ if (priv->show_all)
+ gtk_widget_queue_resize (GTK_WIDGET (previewer));
+ else if (!priv->cur_sample)
{
priv->cur_sample = priv->samples;
@@ -384,6 +513,7 @@ cossa_previewer_select_sample (CossaPreviewer *previewer,
widget = GTK_WIDGET (previewer);
priv = previewer->priv;
samples = priv->samples;
+ priv->show_all = FALSE;
while (samples)
{
@@ -408,6 +538,21 @@ cossa_previewer_select_sample (CossaPreviewer *previewer,
gtk_widget_queue_draw (GTK_WIDGET (previewer));
}
+void
+cossa_previewer_show_all_samples (CossaPreviewer *previewer)
+{
+ CossaPreviewerPrivate *priv;
+
+ g_return_if_fail (COSSA_IS_PREVIEWER (previewer));
+
+ priv = previewer->priv;
+
+ priv->show_all = TRUE;
+ priv->cur_sample = NULL;
+
+ gtk_widget_queue_resize (GTK_WIDGET (previewer));
+}
+
static void
cossa_previewer_invalidate_samples (CossaPreviewer *previewer,
guint invalidation)
@@ -424,9 +569,16 @@ cossa_previewer_invalidate_samples (CossaPreviewer *previewer,
SampleData *data = l->data;
data->invalidation |= invalidation;
+
+ if (priv->show_all)
+ update_sample_surface (data, priv->zoom_level);
}
- if (priv->cur_sample)
+ if (priv->show_all)
+ {
+ gtk_widget_queue_resize (widget);
+ }
+ else if (priv->cur_sample)
{
update_sample_surface (priv->cur_sample->data, priv->zoom_level);
gtk_widget_queue_resize (widget);
@@ -469,7 +621,6 @@ cossa_previewer_update_samples (CossaPreviewer *previewer)
cossa_previewer_invalidate_samples (previewer, INVALIDATE_STYLE);
}
-
GtkCssProvider *
cossa_previewer_get_style (CossaPreviewer *previewer)
{
diff --git a/src/cossa-previewer.h b/src/cossa-previewer.h
index 7c3edbf..016a4dc 100644
--- a/src/cossa-previewer.h
+++ b/src/cossa-previewer.h
@@ -63,6 +63,8 @@ void cossa_previewer_add_sample (CossaPreviewer *previewer,
void cossa_previewer_select_sample (CossaPreviewer *previewer,
GtkWidget *sample);
+void cossa_previewer_show_all_samples (CossaPreviewer *previewer);
+
void cossa_previewer_set_zoom_level (CossaPreviewer *previewer,
CossaZoomLevel zoom);
CossaZoomLevel cossa_previewer_get_zoom_level (CossaPreviewer *previewer);
diff --git a/src/cossa-window.c b/src/cossa-window.c
index 12fd15f..1eed79f 100644
--- a/src/cossa-window.c
+++ b/src/cossa-window.c
@@ -96,7 +96,10 @@ select_sample_cb (GtkAction *action,
widget = g_object_get_data (G_OBJECT (action), "cossa-sample-widget");
priv = COSSA_WINDOW (user_data)->priv;
- cossa_previewer_select_sample (COSSA_PREVIEWER (priv->previewer), widget);
+ if (widget != NULL)
+ cossa_previewer_select_sample (COSSA_PREVIEWER (priv->previewer), widget);
+ else
+ cossa_previewer_show_all_samples (COSSA_PREVIEWER (priv->previewer));
}
static void
@@ -110,12 +113,19 @@ add_sample_widget (CossaWindow *window,
gchar *action_name;
priv = window->priv;
- cossa_previewer_add_sample (COSSA_PREVIEWER (priv->previewer), widget);
+
+ if (widget != NULL)
+ {
+ cossa_previewer_add_sample (COSSA_PREVIEWER (priv->previewer), widget);
+ title = gtk_window_get_title (GTK_WINDOW (widget));
+ }
+ else
+ title = _("All Samples");
action_name = g_strdup_printf ("Sample%d", n_sample);
/* Add item to menu */
- title = gtk_window_get_title (GTK_WINDOW (widget));
+
action = GTK_ACTION (gtk_radio_action_new (action_name, title, NULL, NULL, n_sample));
g_object_set_data (G_OBJECT (action), "cossa-sample-widget", widget);
g_signal_connect (action, "activate",
@@ -172,11 +182,14 @@ compare_titles (gconstpointer a,
static void
load_samples (CossaWindow *window)
{
+ CossaWindowPrivate *priv;
GList *list = NULL, *l;
const gchar *name;
- gint i = 0;
+ gint i = 1;
GDir *dir;
+ GtkAction *active_action;
+ priv = window->priv;
dir = g_dir_open (SAMPLES_DIR, 0, NULL);
if (!dir)
@@ -206,9 +219,16 @@ load_samples (CossaWindow *window)
g_dir_close (dir);
+ /* Generic one for all samples */
+ add_sample_widget (window, NULL, 0);
+
for (l = list; l; l = l->next)
add_sample_widget (window, l->data, i++);
+ active_action = gtk_action_group_get_action (priv->action_group,
+ "Sample0");
+ gtk_action_activate (active_action);
+
g_list_free (list);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]