| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "fixedtip.h" |
| 24 |
|
| 25 |
|
| 26 |
enum |
| 27 |
{ |
| 28 |
CLICKED, |
| 29 |
LAST_SIGNAL |
| 30 |
}; |
| 31 |
|
| 32 |
static guint fixedtip_signals[LAST_SIGNAL] = { 0 }; |
| 33 |
|
| 34 |
struct _NaFixedTipPrivate |
| 35 |
{ |
| 36 |
GtkWidget *parent; |
| 37 |
GtkWidget *label; |
| 38 |
GtkOrientation orientation; |
| 39 |
}; |
| 40 |
|
| 41 |
G_DEFINE_TYPE (NaFixedTip, na_fixed_tip, GTK_TYPE_WINDOW) |
| 42 |
|
| 43 |
static gboolean |
| 44 |
button_press_handler (GtkWidget *fixedtip, |
| 45 |
GdkEventButton *event, |
| 46 |
gpointer data) |
| 47 |
{ |
| 48 |
if (event->button == 1 && event->type == GDK_BUTTON_PRESS) |
| 49 |
g_signal_emit (fixedtip, fixedtip_signals[CLICKED], 0); |
| 50 |
|
| 51 |
return FALSE; |
| 52 |
} |
| 53 |
|
| 54 |
static gboolean |
| 55 |
na_fixed_tip_draw (GtkWidget *widget, cairo_t *cr) |
| 56 |
{ |
| 57 |
GtkStyleContext *context; |
| 58 |
GtkStateFlags state; |
| 59 |
int width, height; |
| 60 |
|
| 61 |
width = gtk_widget_get_allocated_width (widget); |
| 62 |
height = gtk_widget_get_allocated_height (widget); |
| 63 |
|
| 64 |
state = gtk_widget_get_state_flags (widget); |
| 65 |
context = gtk_widget_get_style_context (widget); |
| 66 |
gtk_style_context_save (context); |
| 67 |
gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP); |
| 68 |
gtk_style_context_set_state (context, state); |
| 69 |
|
| 70 |
cairo_save (cr); |
| 71 |
gtk_render_background (context, cr, |
| 72 |
0., 0., |
| 73 |
(gdouble)width, |
| 74 |
(gdouble)height); |
| 75 |
cairo_restore (cr); |
| 76 |
|
| 77 |
gtk_style_context_restore (context); |
| 78 |
|
| 79 |
return FALSE; |
| 80 |
} |
| 81 |
|
| 82 |
static void |
| 83 |
na_fixed_tip_class_init (NaFixedTipClass *class) |
| 84 |
{ |
| 85 |
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class); |
| 86 |
|
| 87 |
widget_class->draw = na_fixed_tip_draw; |
| 88 |
|
| 89 |
fixedtip_signals[CLICKED] = |
| 90 |
g_signal_new ("clicked", |
| 91 |
G_OBJECT_CLASS_TYPE (class), |
| 92 |
G_SIGNAL_RUN_LAST, |
| 93 |
G_STRUCT_OFFSET (NaFixedTipClass, clicked), |
| 94 |
NULL, NULL, |
| 95 |
g_cclosure_marshal_VOID__VOID, |
| 96 |
G_TYPE_NONE, 0); |
| 97 |
|
| 98 |
g_type_class_add_private (class, sizeof (NaFixedTipPrivate)); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
static void |
| 103 |
na_fixed_tip_init (NaFixedTip *fixedtip) |
| 104 |
{ |
| 105 |
GtkWidget *label; |
| 106 |
|
| 107 |
fixedtip->priv = G_TYPE_INSTANCE_GET_PRIVATE (fixedtip, NA_TYPE_FIXED_TIP, |
| 108 |
NaFixedTipPrivate); |
| 109 |
|
| 110 |
gtk_window_set_type_hint (GTK_WINDOW (fixedtip), |
| 111 |
GDK_WINDOW_TYPE_HINT_TOOLTIP); |
| 112 |
|
| 113 |
gtk_widget_set_app_paintable (GTK_WIDGET (fixedtip), TRUE); |
| 114 |
gtk_window_set_resizable (GTK_WINDOW (fixedtip), FALSE); |
| 115 |
gtk_widget_set_name (GTK_WIDGET (fixedtip), "gtk-tooltips"); |
| 116 |
gtk_container_set_border_width (GTK_CONTAINER (fixedtip), 4); |
| 117 |
|
| 118 |
label = gtk_label_new (NULL); |
| 119 |
gtk_label_set_line_wrap (GTK_LABEL (label), TRUE); |
| 120 |
gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5); |
| 121 |
gtk_widget_show (label); |
| 122 |
gtk_container_add (GTK_CONTAINER (fixedtip), label); |
| 123 |
fixedtip->priv->label = label; |
| 124 |
|
| 125 |
gtk_widget_add_events (GTK_WIDGET (fixedtip), GDK_BUTTON_PRESS_MASK); |
| 126 |
|
| 127 |
g_signal_connect (fixedtip, "button_press_event", |
| 128 |
G_CALLBACK (button_press_handler), NULL); |
| 129 |
|
| 130 |
fixedtip->priv->orientation = GTK_ORIENTATION_HORIZONTAL; |
| 131 |
} |
| 132 |
|
| 133 |
static void |
| 134 |
na_fixed_tip_position (NaFixedTip *fixedtip) |
| 135 |
{ |
| 136 |
GdkScreen *screen; |
| 137 |
GdkWindow *parent_window; |
| 138 |
GtkRequisition req; |
| 139 |
int root_x; |
| 140 |
int root_y; |
| 141 |
int parent_width; |
| 142 |
int parent_height; |
| 143 |
int screen_width; |
| 144 |
int screen_height; |
| 145 |
|
| 146 |
screen = gtk_widget_get_screen (fixedtip->priv->parent); |
| 147 |
parent_window = gtk_widget_get_window (fixedtip->priv->parent); |
| 148 |
|
| 149 |
gtk_window_set_screen (GTK_WINDOW (fixedtip), screen); |
| 150 |
|
| 151 |
gtk_widget_get_preferred_size (GTK_WIDGET (fixedtip), &req, NULL); |
| 152 |
|
| 153 |
gdk_window_get_origin (parent_window, &root_x, &root_y); |
| 154 |
parent_width = gdk_window_get_width (parent_window); |
| 155 |
parent_height = gdk_window_get_height (parent_window); |
| 156 |
|
| 157 |
screen_width = gdk_screen_get_width (screen); |
| 158 |
screen_height = gdk_screen_get_height (screen); |
| 159 |
|
| 160 |
|
| 161 |
#define PAD 5 |
| 162 |
|
| 163 |
if (fixedtip->priv->orientation == GTK_ORIENTATION_VERTICAL) |
| 164 |
{ |
| 165 |
if (root_x <= screen_width / 2) |
| 166 |
root_x += parent_width + PAD; |
| 167 |
else |
| 168 |
root_x -= req.width + PAD; |
| 169 |
} |
| 170 |
else |
| 171 |
{ |
| 172 |
if (root_y <= screen_height / 2) |
| 173 |
root_y += parent_height + PAD; |
| 174 |
else |
| 175 |
root_y -= req.height + PAD; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
if ((root_x + req.width) > screen_width) |
| 180 |
root_x = screen_width - req.width; |
| 181 |
|
| 182 |
if ((root_y + req.height) > screen_height) |
| 183 |
root_y = screen_height - req.height; |
| 184 |
|
| 185 |
gtk_window_move (GTK_WINDOW (fixedtip), root_x, root_y); |
| 186 |
} |
| 187 |
|
| 188 |
static void |
| 189 |
na_fixed_tip_parent_size_allocated (GtkWidget *parent, |
| 190 |
GtkAllocation *allocation, |
| 191 |
NaFixedTip *fixedtip) |
| 192 |
{ |
| 193 |
na_fixed_tip_position (fixedtip); |
| 194 |
} |
| 195 |
|
| 196 |
static void |
| 197 |
na_fixed_tip_parent_screen_changed (GtkWidget *parent, |
| 198 |
GdkScreen *new_screen, |
| 199 |
NaFixedTip *fixedtip) |
| 200 |
{ |
| 201 |
na_fixed_tip_position (fixedtip); |
| 202 |
} |
| 203 |
|
| 204 |
GtkWidget * |
| 205 |
na_fixed_tip_new (GtkWidget *parent, |
| 206 |
GtkOrientation orientation) |
| 207 |
{ |
| 208 |
NaFixedTip *fixedtip; |
| 209 |
|
| 210 |
g_return_val_if_fail (parent != NULL, NULL); |
| 211 |
|
| 212 |
fixedtip = g_object_new (NA_TYPE_FIXED_TIP, |
| 213 |
"type", GTK_WINDOW_POPUP, |
| 214 |
NULL); |
| 215 |
|
| 216 |
fixedtip->priv->parent = parent; |
| 217 |
|
| 218 |
#if 0 |
| 219 |
|
| 220 |
|
| 221 |
GtkWidget *toplevel; |
| 222 |
|
| 223 |
toplevel = gtk_widget_get_toplevel (parent); |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
#endif |
| 229 |
|
| 230 |
fixedtip->priv->orientation = orientation; |
| 231 |
|
| 232 |
|
| 233 |
g_signal_connect_object (parent, "size-allocate", |
| 234 |
G_CALLBACK (na_fixed_tip_parent_size_allocated), |
| 235 |
fixedtip, 0); |
| 236 |
g_signal_connect_object (parent, "screen-changed", |
| 237 |
G_CALLBACK (na_fixed_tip_parent_screen_changed), |
| 238 |
fixedtip, 0); |
| 239 |
|
| 240 |
na_fixed_tip_position (fixedtip); |
| 241 |
|
| 242 |
return GTK_WIDGET (fixedtip); |
| 243 |
} |
| 244 |
|
| 245 |
void |
| 246 |
na_fixed_tip_set_markup (GtkWidget *widget, |
| 247 |
const char *markup_text) |
| 248 |
{ |
| 249 |
NaFixedTip *fixedtip; |
| 250 |
|
| 251 |
g_return_if_fail (NA_IS_FIXED_TIP (widget)); |
| 252 |
|
| 253 |
fixedtip = NA_FIXED_TIP (widget); |
| 254 |
|
| 255 |
gtk_label_set_markup (GTK_LABEL (fixedtip->priv->label), |
| 256 |
markup_text); |
| 257 |
|
| 258 |
na_fixed_tip_position (fixedtip); |
| 259 |
} |
| 260 |
|
| 261 |
void |
| 262 |
na_fixed_tip_set_orientation (GtkWidget *widget, |
| 263 |
GtkOrientation orientation) |
| 264 |
{ |
| 265 |
NaFixedTip *fixedtip; |
| 266 |
|
| 267 |
g_return_if_fail (NA_IS_FIXED_TIP (widget)); |
| 268 |
|
| 269 |
fixedtip = NA_FIXED_TIP (widget); |
| 270 |
|
| 271 |
if (orientation == fixedtip->priv->orientation) |
| 272 |
return; |
| 273 |
|
| 274 |
fixedtip->priv->orientation = orientation; |
| 275 |
|
| 276 |
na_fixed_tip_position (fixedtip); |
| 277 |
} |