Skip to content

Commit 5d471a8

Browse files
committed
Let Gtk handle tooltips
Add a TOOLTIP_COLUMN to the model, store the text there (when appropriate), and let the tree display tooltips automatically.
1 parent c21b107 commit 5d471a8

File tree

3 files changed

+16
-46
lines changed

3 files changed

+16
-46
lines changed

interface.c

+7-41
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,6 @@ static GtkTreeViewColumn *treeColumn;
2424

2525
int timer;
2626

27-
static gboolean
28-
query_tooltip_cb(GtkWidget *widget, gint x, gint y,
29-
gboolean keyboard_tip, GtkTooltip *tooltip, void *unused)
30-
{
31-
GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeUSB));
32-
GtkTreePath *path = NULL;
33-
GtkTreeIter iter;
34-
gint cellx = 0, celly = 0;
35-
gchar *color;
36-
gboolean return_val = FALSE;
37-
38-
if (!model)
39-
return FALSE;
40-
41-
/* This -30 is a kludge. It seems that if you don't do this, gtk_tree_view_get_path_at_pos()
42-
* gives you the wrong path, the path for the cell that is 30 or so pixels down below the
43-
* mouse. I don't know why it does that. Either we're doing something wrong, or it's a gtk-3
44-
* bug.
45-
*/
46-
y = y - 30;
47-
48-
if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), x, y, &path, &treeColumn, &cellx, &celly))
49-
return FALSE;
50-
if (!path)
51-
return FALSE;
52-
/* Get the color, if it's red, set tooltip to indicate device has no driver. */
53-
gtk_tree_model_get_iter(model, &iter, path);
54-
gtk_tree_model_get(model, &iter, COLOR_COLUMN, &color, -1);
55-
if (color != NULL && strncmp(color, "red", 4) == 0) {
56-
gtk_tooltip_set_text(tooltip, "This device has no attached driver");
57-
return_val = TRUE;
58-
}
59-
gtk_tree_path_free(path);
60-
return return_val;
61-
}
62-
6327
GtkWidget*
6428
create_windowMain ()
6529
{
@@ -94,7 +58,8 @@ create_windowMain ()
9458
treeStore = gtk_tree_store_new (N_COLUMNS,
9559
G_TYPE_STRING, /* NAME_COLUMN */
9660
G_TYPE_INT, /* DEVICE_ADDR_COLUMN */
97-
G_TYPE_STRING /* COLOR_COLUMN */);
61+
G_TYPE_STRING, /* COLOR_COLUMN */
62+
G_TYPE_STRING /* TOOLTIP_COLUMN */);
9863
treeUSB = gtk_tree_view_new_with_model (GTK_TREE_MODEL (treeStore));
9964
treeRenderer = gtk_cell_renderer_text_new ();
10065
treeColumn = gtk_tree_view_column_new_with_attributes (
@@ -104,6 +69,10 @@ create_windowMain ()
10469
"foreground", COLOR_COLUMN,
10570
NULL);
10671
gtk_tree_view_append_column (GTK_TREE_VIEW (treeUSB), treeColumn);
72+
gtk_tree_view_set_tooltip_column(
73+
GTK_TREE_VIEW (treeUSB), TOOLTIP_COLUMN
74+
);
75+
10776
gtk_widget_set_name (treeUSB, "treeUSB");
10877
gtk_widget_show (treeUSB);
10978
gtk_paned_pack1 (GTK_PANED (hpaned1), treeUSB, FALSE, FALSE);
@@ -164,12 +133,9 @@ create_windowMain ()
164133
g_signal_connect (G_OBJECT (buttonClose), "clicked",
165134
G_CALLBACK (on_buttonClose_clicked),
166135
NULL);
167-
g_object_set(G_OBJECT (treeUSB), "has-tooltip", TRUE, NULL);
168-
g_signal_connect(G_OBJECT (treeUSB), "query-tooltip", G_CALLBACK (query_tooltip_cb), NULL);
169136

170137
/* create our timer */
171138
//timer = gtk_timeout_add (2000, on_timer_timeout, 0);
172-
139+
173140
return windowMain;
174141
}
175-

usbtree.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static void PopulateListBox (int deviceId)
150150
/* show this config */
151151
sprintf (string, "\n\nConfig Number: %i\n\tNumber of Interfaces: %i\n\t"
152152
"Attributes: %.2x\n\tMaxPower Needed: %s",
153-
config->configNumber, config->numInterfaces,
153+
config->configNumber, config->numInterfaces,
154154
config->attributes, config->maxPower);
155155
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string,strlen(string));
156156

@@ -169,7 +169,7 @@ static void PopulateListBox (int deviceId)
169169

170170
sprintf (string, "\n\t\tAlternate Number: %i\n\t\tClass: %s\n\t\t"
171171
"Sub Class: %.2x\n\t\tProtocol: %.2x\n\t\tNumber of Endpoints: %i",
172-
interface->alternateNumber, interface->class,
172+
interface->alternateNumber, interface->class,
173173
interface->subClass, interface->protocol, interface->numEndpoints);
174174
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string, strlen(string));
175175

@@ -181,7 +181,7 @@ static void PopulateListBox (int deviceId)
181181
sprintf (string, "\n\n\t\t\tEndpoint Address: %.2x\n\t\t\t"
182182
"Direction: %s\n\t\t\tAttribute: %i\n\t\t\t"
183183
"Type: %s\n\t\t\tMax Packet Size: %i\n\t\t\tInterval: %s",
184-
endpoint->address,
184+
endpoint->address,
185185
endpoint->in ? "in" : "out", endpoint->attribute,
186186
endpoint->type, endpoint->maxPacketSize, endpoint->interval);
187187
gtk_text_buffer_insert_at_cursor(textDescriptionBuffer, string,strlen(string));
@@ -224,6 +224,7 @@ static void DisplayDevice (struct Device *parent, struct Device *device)
224224
int interfaceNum;
225225
gboolean driverAttached = TRUE;
226226
gint deviceAddr;
227+
const gchar *tooltip = NULL;
227228
const gchar *color = NULL;
228229

229230
if (device == NULL)
@@ -251,13 +252,16 @@ static void DisplayDevice (struct Device *parent, struct Device *device)
251252
}
252253

253254
/* change the color of this leaf if there are no drivers attached to it */
254-
if (driverAttached == FALSE)
255+
if (driverAttached == FALSE) {
255256
color = "red";
257+
tooltip = "This device has no attached driver";
258+
}
256259

257260
gtk_tree_store_set (treeStore, &device->leaf,
258261
NAME_COLUMN, device->name,
259262
DEVICE_ADDR_COLUMN, deviceAddr,
260263
COLOR_COLUMN, color,
264+
TOOLTIP_COLUMN, tooltip,
261265
-1);
262266

263267
/* create all of the children's leafs */
@@ -306,4 +310,3 @@ void initialize_stuff(void)
306310
{
307311
return;
308312
}
309-

usbtree.h

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ enum {
1010
NAME_COLUMN,
1111
DEVICE_ADDR_COLUMN,
1212
COLOR_COLUMN,
13+
TOOLTIP_COLUMN,
1314
N_COLUMNS
1415
};
1516

0 commit comments

Comments
 (0)