| 1 |
/* |
| 2 |
Copyright (C) 2002 Kai Sterker <[email protected]> |
| 3 |
Part of the Adonthell Project <http://adonthell.nongnu.org> |
| 4 |
|
| 5 |
Dlgedit is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation; either version 2 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
Dlgedit is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with Dlgedit. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
/** |
| 20 |
* @file gui_tooltip.cc |
| 21 |
* |
| 22 |
* @author Kai Sterker |
| 23 |
* @brief A custom, tooltip-like widget |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "gui_dlgedit.h" |
| 27 |
#include "gui_tooltip.h" |
| 28 |
|
| 29 |
// constructor |
| 30 |
GuiTooltip::GuiTooltip (DlgNode *n) |
| 31 |
{ |
| 32 |
GtkWidget *tip; |
| 33 |
std::string text; |
| 34 |
|
| 35 |
// the actual tooltip |
| 36 |
tooltip = gtk_window_new (GTK_WINDOW_POPUP); |
| 37 |
gtk_window_set_keep_above (GTK_WINDOW (tooltip), FALSE); |
| 38 |
gtk_window_set_transient_for (GTK_WINDOW (tooltip), GTK_WINDOW (GuiDlgedit::window->getWindow ())); |
| 39 |
gtk_window_set_type_hint (GTK_WINDOW (tooltip), GDK_WINDOW_TYPE_HINT_NOTIFICATION); |
| 40 |
gtk_window_set_skip_taskbar_hint(GTK_WINDOW (tooltip), TRUE); |
| 41 |
gtk_window_set_opacity (GTK_WINDOW (tooltip), 0.9f); |
| 42 |
|
| 43 |
g_object_set_data (G_OBJECT (tooltip), "tip_window", tooltip); |
| 44 |
gtk_window_set_resizable (GTK_WINDOW (tooltip), FALSE); |
| 45 |
|
| 46 |
// get the text |
| 47 |
switch (n->type ()) |
| 48 |
{ |
| 49 |
case NPC: |
| 50 |
case PLAYER: |
| 51 |
case NARRATOR: |
| 52 |
{ |
| 53 |
text = ((DlgCircle *) n)->tooltip (); |
| 54 |
break; |
| 55 |
} |
| 56 |
case MODULE: |
| 57 |
{ |
| 58 |
text = ((DlgModule *) n)->entry ()->description (); |
| 59 |
break; |
| 60 |
} |
| 61 |
default: break; |
| 62 |
} |
| 63 |
|
| 64 |
// label with the text |
| 65 |
tip = gtk_label_new (text.c_str ()); |
| 66 |
g_object_ref (tip); |
| 67 |
g_object_set_data_full (G_OBJECT (tooltip), "tip", tip, (GDestroyNotify) g_object_unref); |
| 68 |
gtk_widget_show (tip); |
| 69 |
|
| 70 |
gtk_container_add (GTK_CONTAINER (tooltip), tip); |
| 71 |
gtk_label_set_justify (GTK_LABEL (tip), GTK_JUSTIFY_LEFT); |
| 72 |
gtk_label_set_line_wrap (GTK_LABEL (tip), TRUE); |
| 73 |
gtk_misc_set_padding (GTK_MISC (tip), 4, 1); |
| 74 |
|
| 75 |
// keep a pointer to the node |
| 76 |
node = n; |
| 77 |
} |
| 78 |
|
| 79 |
// destructor |
| 80 |
GuiTooltip::~GuiTooltip () |
| 81 |
{ |
| 82 |
gtk_widget_destroy (tooltip); |
| 83 |
} |
| 84 |
|
| 85 |
// draw the tooltip |
| 86 |
void GuiTooltip::draw (GtkWidget *graph, DlgPoint &offset) |
| 87 |
{ |
| 88 |
if (node->type () == LINK) return; |
| 89 |
|
| 90 |
// get position and extension of dlgedit window |
| 91 |
int x, y; |
| 92 |
|
| 93 |
GdkWindow *window = gtk_widget_get_parent_window (graph); |
| 94 |
gdk_window_get_origin (window, &x, &y); |
| 95 |
int width = gdk_window_get_width(window); |
| 96 |
|
| 97 |
gtk_widget_realize (tooltip); |
| 98 |
|
| 99 |
GtkAllocation allocation; |
| 100 |
gtk_widget_get_allocation (graph, &allocation); |
| 101 |
|
| 102 |
// calculate the position of the tooltip |
| 103 |
x += width - allocation.width; |
| 104 |
if (node->x () < allocation.width / 2) |
| 105 |
{ |
| 106 |
x += node->x () + node->width (); |
| 107 |
} |
| 108 |
else |
| 109 |
{ |
| 110 |
gtk_widget_get_allocation (tooltip, &allocation); |
| 111 |
x += node->x () - allocation.width; |
| 112 |
} |
| 113 |
y += node->y () + node->height (); |
| 114 |
|
| 115 |
// position and display the tooltip |
| 116 |
gtk_window_move (GTK_WINDOW(tooltip), x + offset.x (), y + offset.y ()); |
| 117 |
gtk_widget_show (tooltip); |
| 118 |
} |