Dillo v3.1.1-91-g6d5b3ee3
Loading...
Searching...
No Matches
tipwin.cc
Go to the documentation of this file.
1/*
2 * File: tipwin.cc
3 *
4 * Copyright 2012 Jorge Arellano Cid <jcid@dillo.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * The tipwin idea was derived from the Fl_Slider example [1]
12 * by Greg Ercolano, which is in public domain.
13 *
14 * [1] http://seriss.com/people/erco/fltk/#SliderTooltip
15 */
16
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <FL/fl_draw.H>
22#include <FL/Fl.H>
23#include <FL/Fl_Group.H>
24#include <FL/Fl_Menu_Window.H>
25#include <FL/Fl_Tooltip.H>
26#include <FL/Fl_Button.H>
27
28#include "prefs.h"
29#include "tipwin.hh"
30#include "dlib/dlib.h"
31
32/*
33 * Forward declarations
34 */
35static void show_timeout(void*);
36static void recent_timeout(void*);
37
41TipWin::TipWin() : Fl_Menu_Window(1, 1) // will autosize
42{
43 bgcolor = fl_color_cube(FL_NUM_RED - 1, FL_NUM_GREEN - 1, FL_NUM_BLUE - 2);
44 recent = 0;
45 tip[0] = '\0';
46 cur_widget = NULL;
47 set_override(); // no border
48 end();
49}
50
52{
53 draw_box(FL_BORDER_BOX, 0, 0, w(), h(), bgcolor);
54 fl_color(FL_BLACK);
55 fl_font(labelfont(), labelsize());
56 fl_draw(tip, 3, 3, w() - 6, h() - 6,
57 //Fl_Align(FL_ALIGN_LEFT | FL_ALIGN_WRAP));
58 Fl_Align(FL_ALIGN_LEFT));
59}
60
61void TipWin::value(const char *s) {
62 // Recalc size of window
63 snprintf(tip, sizeof(tip) - 1, "%s", s);
64 fl_font(labelfont(), labelsize());
65 int W = w(), H = h();
66 W = 0;
67 fl_measure(tip, W, H, 0);
68 W += 8; H += 8;
69 size(W, H);
70 redraw();
71}
72
73void TipWin::do_show(void *wid) {
74 cur_widget = wid; // Keep track of requesting widget
76 Fl::add_timeout(recent ? 0.2f : 0.8f, show_timeout);
77 }
78}
79
81 Fl::remove_timeout(show_timeout);
82 if (shown()) {
83 hide();
84 recent = 1;
85 Fl::add_timeout(0.8f, recent_timeout);
86 }
87}
88
90 recent = val;
91}
92
93//--------------------------------------------------------------------------
94
96{
97 static TipWin *tw = NULL;
98
99 if (!tw) {
100 Fl_Group *save = Fl_Group::current(); // save current widget..
101 tw = new TipWin(); // ..because this trashes it
102 tw->hide(); // start hidden
103 Fl_Group::current(save); // ..then back to previous.
104 }
105 return tw;
106}
107
108static void show_timeout(void*) {
109 // if offscreen, move tip ABOVE mouse instead
110 int scr_x, scr_y, scr_w, scr_h;
111 Fl::screen_xywh(scr_x, scr_y, scr_w, scr_h);
112 int ty = Fl::event_y_root() + 20;
113 if (ty + my_tipwin()->h() > scr_h)
114 ty = Fl::event_y_root() - 20 - my_tipwin()->h();
115 if (ty < 0) ty = 0;
116
117 my_tipwin()->position(Fl::event_x_root(), ty);
118 my_tipwin()->show();
120}
121
122static void recent_timeout(void*) {
124}
125
126
127//---------------------------------------------------------------------------
128
132TipWinButton::TipWinButton(int x, int y, int w, int h, const char *l) :
133 Fl_Button(x, y, w, h, l)
134{
135 tipwin = my_tipwin();
136 mytooltip = dStrdup("empty");
137}
138
140{
141 tipwin->cancel(this); // cancel tooltip if shown
142 free(mytooltip);
143}
144
146{
147 switch (e) {
148 case FL_ENTER:
150 tipwin->do_show(this);
151 break;
152 case FL_PUSH: // push mouse
153 case FL_RELEASE: // release mouse
154 case FL_HIDE: // widget goes away
155 case FL_LEAVE: // leave focus
156 tipwin->do_hide();
157 break;
158 }
159 return (Fl_Button::handle(e));
160}
161
162void TipWinButton::set_tooltip(const char *s)
163{
164 free(mytooltip);
165 mytooltip = dStrdup(s);
166}
167
168
169//---------------------------------------------------------------------------
170
174CustButton::CustButton(int x, int y, int w, int h, const char *l) :
175 TipWinButton(x,y,w,h,l)
176{
177 norm_color = color();
179}
180
182{
183 if (active()) {
184 if (e == FL_ENTER) {
185 color(light_color);
186 redraw();
187 } else if (e == FL_LEAVE || e == FL_RELEASE || e == FL_HIDE) {
188 color(norm_color);
189 redraw();
190 }
191 } else if (e == FL_DEACTIVATE && color() != norm_color) {
192 color(norm_color);
193 redraw();
194 }
195 return TipWinButton::handle(e);
196}
197
198void CustButton::hl_color(Fl_Color col)
199{
200 light_color = col;
201}
202
203
204//---------------------------------------------------------------------------
205
209TipWinInput::TipWinInput (int x, int y, int w, int h, const char *l) :
210 Fl_Input(x,y,w,h,l)
211{
212 tipwin = my_tipwin();
213 mytooltip = dStrdup("empty");
214}
215
217{
218 tipwin->cancel(this); // cancel tooltip if shown
219 free(mytooltip);
220}
221
223{
224 switch (e) {
225 case FL_ENTER:
227 tipwin->do_show(this);
228 break;
229 case FL_PUSH: // push mouse
230 case FL_RELEASE: // release mouse
231 case FL_HIDE: // widget goes away
232 case FL_LEAVE: // leave focus
233 case FL_KEYBOARD: // key press
234 tipwin->do_hide();
235 break;
236 }
237 return (Fl_Input::handle(e));
238}
239
240void TipWinInput::set_tooltip(const char *s)
241{
242 free(mytooltip);
243 mytooltip = dStrdup(s);
244}
245
CustButton(int x, int y, int w, int h, const char *l=0)
A Light Button sharing a custom tooltip window.
Definition tipwin.cc:174
Fl_Color light_color
Definition tipwin.hh:50
virtual int handle(int e)
Definition tipwin.cc:181
Fl_Color norm_color
Definition tipwin.hh:50
void hl_color(Fl_Color col)
Definition tipwin.cc:198
A Button sharing a custom tooltip window.
Definition tipwin.hh:35
TipWinButton(int x, int y, int w, int h, const char *l=0)
A Button sharing a custom tooltip window.
Definition tipwin.cc:132
virtual int handle(int e)
Definition tipwin.cc:145
char * mytooltip
Definition tipwin.hh:36
void set_tooltip(const char *s)
Definition tipwin.cc:162
TipWin * tipwin
Definition tipwin.hh:37
TipWin * tipwin
Definition tipwin.hh:63
~TipWinInput(void)
Definition tipwin.cc:216
char * mytooltip
Definition tipwin.hh:62
void set_tooltip(const char *s)
Definition tipwin.cc:240
TipWinInput(int x, int y, int w, int h, const char *l=0)
An Input with custom tooltip window.
Definition tipwin.cc:209
virtual int handle(int e)
Definition tipwin.cc:222
Custom tooltip window.
Definition tipwin.hh:12
void do_show(void *wid)
Definition tipwin.cc:73
void do_hide()
Definition tipwin.cc:80
void value(const char *s)
Definition tipwin.cc:61
void recent_tooltip(int val)
Definition tipwin.cc:89
void draw()
Definition tipwin.cc:51
int recent
Definition tipwin.hh:14
void * cur_widget
Definition tipwin.hh:15
int bgcolor
Definition tipwin.hh:14
void cancel(void *wid)
Definition tipwin.hh:24
char tip[256]
Definition tipwin.hh:13
TipWin()
Custom tooltip window.
Definition tipwin.cc:41
char * dStrdup(const char *s)
Definition dlib.c:77
#define H(x, y, z)
DilloPrefs prefs
Global Data.
Definition prefs.c:33
#define PREFS_UI_BUTTON_HIGHLIGHT_COLOR
Definition prefs.h:28
bool_t show_ui_tooltip
Definition prefs.h:67
TipWin * my_tipwin(void)
Definition tipwin.cc:95
static void recent_timeout(void *)
Definition tipwin.cc:122
static void show_timeout(void *)
Definition tipwin.cc:108
TipWin * my_tipwin(void)
Definition tipwin.cc:95