File: picturebutton.c

package info (click to toggle)
libgpewidget 0.117-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,620 kB
  • ctags: 738
  • sloc: sh: 9,164; ansic: 6,417; makefile: 141
file content (132 lines) | stat: -rw-r--r-- 3,446 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (C) 2001, 2002, 2003 Philip Blundell <philb@gnu.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <gtk/gtk.h>

#include "pixmaps.h"
#include "picturebutton.h"

GtkWidget *
gpe_picture_button (GtkStyle *style, gchar *text, gchar *icon)
{
  return gpe_picture_button_aligned (style, text, icon, GPE_POS_CENTER);
}

GtkWidget *
gpe_picture_button_aligned (GtkStyle *style, gchar *text, gchar *icon, GpePositionType pos)
{
  GtkWidget *button = gtk_button_new ();
  GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
  GtkWidget *hbox2 = gtk_hbox_new (FALSE, 0);

  if (icon[0] == '!')
    {
      GtkWidget *pw = gtk_image_new_from_stock (icon + 1, GTK_ICON_SIZE_SMALL_TOOLBAR);
      gtk_box_pack_start (GTK_BOX (hbox), pw, FALSE, FALSE, 0);
      gtk_widget_show (pw);
    }
  else
    {
      GdkPixbuf *p = gpe_try_find_icon (icon, NULL);
      if (p)
	{
	  GtkWidget *pw = gtk_image_new_from_pixbuf (p);
	  gtk_box_pack_start (GTK_BOX (hbox), pw, FALSE, FALSE, 0);
	  gtk_widget_show (pw);
	}
    }

  if (text)
    {
      GtkWidget *label = gtk_label_new (text);
      gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
      gtk_widget_show (label);
    }

  gtk_widget_show (hbox2);
  gtk_widget_show (hbox);
  gtk_widget_show (button);

  gtk_box_pack_start (GTK_BOX (hbox2), hbox, TRUE, FALSE, 0);

  if (pos != GPE_POS_CENTER)
    {
      GtkWidget * alignment;
      switch(pos)
	{
	case GPE_POS_LEFT:
	  alignment = gtk_alignment_new (0.0, 0.5, 0, 0);
	  break;
	case GPE_POS_RIGHT:
	  alignment = gtk_alignment_new (1.0, 0.5, 0, 0);
	  break;
	default:
	  alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
	}

      gtk_container_add (GTK_CONTAINER (alignment), hbox2);
      gtk_container_add (GTK_CONTAINER (button), alignment);
    }
  else
    {
      gtk_container_add (GTK_CONTAINER (button), hbox2);  
    }
  return button;
}


GtkWidget *
gpe_button_new_from_stock (const gchar *stock_id, int type)
{
  GtkWidget *button;
  GtkStockItem item;

  if (type == GPE_BUTTON_TYPE_BOTH)
    return gtk_button_new_from_stock (stock_id);

  button = gtk_button_new ();
  
  if (gtk_stock_lookup (stock_id, &item) == TRUE)
    {
      GtkWidget *widget = NULL, *align;

      align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);

      switch (type)
	{
	case GPE_BUTTON_TYPE_ICON:
	  widget = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_SMALL_TOOLBAR);
	  gtk_container_add (GTK_CONTAINER (align), widget);
	  break;

	case GPE_BUTTON_TYPE_LABEL:
	  widget = gtk_label_new_with_mnemonic (item.label);
	  gtk_container_add (GTK_CONTAINER (align), widget);
	  break;

	default:
	  break;
	}

      gtk_container_add (GTK_CONTAINER (button), align);
      gtk_widget_show_all (align);
    }

  return button;
}