Added the widget to show tooltips.

This commit is contained in:
David Capello 2008-02-10 18:49:12 +00:00
parent 8b365fa3cb
commit c6b2f7e601
2 changed files with 225 additions and 0 deletions

182
src/jinete/jtooltips.c Normal file
View File

@ -0,0 +1,182 @@
/* ASE - Allegro Sprite Editor
* Copyright (C) 2008 David A. Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#ifndef USE_PRECOMPILED_HEADER
#include <allegro.h>
#include "jinete/jinete.h"
#endif
typedef struct TipData
{
JWidget widget; /* widget that shows the tooltip */
JWidget window; /* window where is the tooltip */
char *text;
int timer_id;
} TipData;
static int tip_type(void);
static bool tip_hook(JWidget widget, JMessage msg);
static JWidget tip_window_new(const char *text);
static bool tip_window_hook(JWidget widget, JMessage msg);
void jwidget_add_tooltip_text(JWidget widget, const char *text)
{
TipData *tip = jnew(TipData, 1);
tip->widget = widget;
tip->window = NULL;
tip->text = jstrdup(text);
tip->timer_id = -1;
jwidget_add_hook(widget, tip_type(), tip_hook, tip);
}
static int tip_type(void)
{
static int type = 0;
if (!type)
type = ji_register_widget_type();
return type;
}
static bool tip_hook(JWidget widget, JMessage msg)
{
TipData *tip = jwidget_get_data(widget, tip_type());
switch (msg->type) {
case JM_DESTROY:
if (tip->timer_id >= 0)
jmanager_remove_timer(tip->timer_id);
jfree(tip->text);
jfree(tip);
break;
case JM_MOUSEENTER:
if (tip->timer_id < 0)
tip->timer_id = jmanager_add_timer(widget, 300);
jmanager_start_timer(tip->timer_id);
break;
case JM_MOUSELEAVE:
if (tip->window)
jwindow_close(tip->window, NULL);
jmanager_stop_timer(tip->timer_id);
break;
case JM_TIMER:
if (msg->timer.timer_id == tip->timer_id) {
JWidget window = tip_window_new(tip->text);
int x = tip->widget->rc->x1;
int y = tip->widget->rc->y2;
int w = jrect_w(window->rc);
int h = jrect_h(window->rc);
tip->window = window;
jwindow_remap(window);
jwindow_position(window,
MID(0, x, JI_SCREEN_W-w),
MID(0, y, JI_SCREEN_H-h));
jwindow_open(window);
jmanager_stop_timer(tip->timer_id);
}
break;
}
return FALSE;
}
static JWidget tip_window_new(const char *text)
{
JWidget window = jwindow_new(text);
JLink link, next;
jwindow_sizeable(window, FALSE);
jwindow_moveable(window, FALSE);
jwindow_wantfocus(window, FALSE);
/* jwidget_set_align(window, JI_CENTER | JI_MIDDLE); */
jwidget_set_align(window, JI_LEFT | JI_TOP);
/* remove decorative widgets */
JI_LIST_FOR_EACH_SAFE(window->children, link, next) {
jwidget_free(link->data);
}
jwidget_add_hook(window, JI_WIDGET, tip_window_hook, NULL);
jwidget_init_theme(window);
return window;
}
static bool tip_window_hook(JWidget widget, JMessage msg)
{
switch (msg->type) {
case JM_REQSIZE:
_ji_theme_textbox_draw(NULL, widget,
&msg->reqsize.w,
&msg->reqsize.h, 0, 0);
/* msg->reqsize.w += widget->border_width.l + widget->border_width.r; */
/* msg->reqsize.h += widget->border_width.t + widget->border_width.b; */
return TRUE;
case JM_SIGNAL:
if (msg->signal.num == JI_SIGNAL_INIT_THEME) {
widget->border_width.l = 3;
widget->border_width.t = 3;
widget->border_width.r = 3;
widget->border_width.b = 3;
return TRUE;
}
break;
case JM_MOUSELEAVE:
case JM_BUTTONPRESSED:
jwindow_close(widget, NULL);
break;
case JM_DRAW: {
JRect pos = jwidget_get_rect(widget);
jdraw_rect(pos, makecol(0, 0, 0));
jrect_shrink(pos, 1);
jdraw_rectfill(pos, makecol(255, 255, 140));
_ji_theme_textbox_draw(ji_screen, widget, NULL, NULL,
makecol(255, 255, 140),
makecol(0, 0, 0));
return TRUE;
}
}
return FALSE;
}

43
src/jinete/jtooltips.h Normal file
View File

@ -0,0 +1,43 @@
/* Jinete - a GUI library
* Copyright (C) 2003-2008 David A. Capello.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of the Jinete nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef JINETE_TOOLTIPS_H
#define JINETE_TOOLTIPS_H
#include "jinete/jbase.h"
JI_BEGIN_DECLS
void jwidget_add_tooltip_text(JWidget widget, const char *text);
JI_END_DECLS
#endif /* JINETE_VIEW_H */