Initial add new widgets system.

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
Slava Zanko 2009-09-25 02:53:34 +03:00
parent 382795b697
commit f4b5ebabaf
26 changed files with 817 additions and 2 deletions

View File

@ -573,6 +573,7 @@ src/search/Makefile
src/skin/Makefile
src/tty/Makefile
src/viewer/Makefile
src/widget/Makefile
edit/Makefile
syntax/Makefile

View File

@ -1,4 +1,4 @@
SUBDIRS = mcconfig search tty viewer filehighlight skin
SUBDIRS = mcconfig search tty viewer filehighlight skin widget
AM_CFLAGS = $(GLIB_CFLAGS) -I$(top_srcdir) $(PCRE_CFLAGS)
@ -46,9 +46,10 @@ TTYLIB = tty/libmctty.la
VIEWERLIB = viewer/libmcviewer.la
FILEHIGHLIGHTLIB=filehighlight/libmcfilehighlight.la
SKINLIB = skin/libmcskin.la
WIDGETLIB = widget/libmcwidget.la
mc_LDADD = $(EDITLIB) $(VFSLIB) $(FILEHIGHLIGHTLIB) $(SKINLIB) \
$(MCCONFIGLIB) $(SEARCHLIB) $(TTYLIB) $(VIEWERLIB) \
$(MCCONFIGLIB) $(SEARCHLIB) $(TTYLIB) $(VIEWERLIB) $(WIDGETLIB) \
$(INTLLIBS) $(MCLIBS) $(SLANGLIB) $(LIBICONV) $(GLIB_LIBS)
CHARSET_SRC = charsets.c charsets.h selcodepage.c selcodepage.h

22
src/widget/Makefile.am Normal file
View File

@ -0,0 +1,22 @@
noinst_LTLIBRARIES = libmcwidget.la
libmcwidget_la_SOURCES = \
container.c \
container.h \
events.c \
events.h \
internal.h \
main.c \
main.h \
object.c \
object.h \
widget.c \
widget.h \
widget-desktop.c \
widget-desktop.h
libmcwidget_la_CFLAGS=-I../ -I$(top_srcdir)/src \
$(GLIB_CFLAGS) $(PCRE_CFLAGS) \
-DDATADIR=\""$(pkgdatadir)/"\" -DLOCALEDIR=\""$(localedir)"\"

145
src/widget/container.c Normal file
View File

@ -0,0 +1,145 @@
/*
Widgets library
Containers of objects.
Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
Written by:
2009 Slava Zanko <slavazanko@google.com>
This file is part of the Midnight Commander.
The Midnight Commander 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.
The Midnight Commander 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., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#include "main.h"
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
/*** file scope functions ************************************************************************/
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
void
w_container_init (w_container_t * container)
{
w_object_init ((w_object_t *) container);
container->parent = NULL;
container->children = NULL;
container->prev = NULL;
container->next = NULL;
}
/* --------------------------------------------------------------------------------------------- */
void
w_container_deinit (w_container_t * container)
{
w_container_t *child;
w_object_deinit ((w_object_t *) container);
/* TODO: safe removal container from tree */
w_container_remove_from_siblings (container);
/* Is this need? */
for (child = container->children; child != NULL; child = child->next)
child->parent = container->parent;
}
/* --------------------------------------------------------------------------------------------- */
GPtrArray *
w_container_get_path (w_container_t * container)
{
GPtrArray *ret;
w_container_t *parent;
if (container->parent == NULL)
return NULL;
ret = g_ptr_array_new ();
for (parent = container->parent; parent != NULL; parent = parent->parent) {
g_ptr_array_add (ret, (gpointer) parent);
}
return ret;
}
/* --------------------------------------------------------------------------------------------- */
void
w_container_append_child (w_container_t * container, w_container_t * container_child)
{
w_container_t *child;
/* setting up of child */
container_child->parent = container;
w_container_remove_from_siblings (container_child);
if (container->children == NULL) {
/* pervonah here if no have any children */
container->children = container_child;
return;
}
/* search last node */
child = w_container_get_last_child (container);
/* append new node into end of list */
container_child->prev = child;
child->next = container_child;
}
/* --------------------------------------------------------------------------------------------- */
void
w_container_remove_from_siblings (w_container_t * container)
{
if (container->prev != NULL) {
container->prev->next = container->next;
container->prev = NULL;
}
if (container->next != NULL) {
container->next->prev = container->prev;
container->next = NULL;
}
}
/* --------------------------------------------------------------------------------------------- */
w_container_t *
w_container_get_last_child (w_container_t * container)
{
w_container_t *child;
if (container->children == NULL)
return NULL;
for (child = container->children; child->next != NULL; child = child->next);
return child;
}
/* --------------------------------------------------------------------------------------------- */

35
src/widget/container.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef W_WIDGET_CONTAINER_H
#define W_WIDGET_CONTAINER_H
#include "object.h"
/*** typedefs(not structures) and defined constants ********************/
/*** enums *************************************************************/
/*** structures declarations (and typedefs of structures)***************/
typedef struct w_container_struct {
w_object_t object;
struct w_container_struct *parent;
struct w_container_struct *children; /* pointer to first child */
struct w_container_struct *prev;
struct w_container_struct *next;
} w_container_t;
/*** global variables defined in .c file *******************************/
/*** declarations of public functions **********************************/
void w_container_init (w_container_t *);
void w_container_deinit (w_container_t *);
GPtrArray *w_container_get_path (w_container_t *);
void w_container_append_child (w_container_t *, w_container_t *);
void w_container_remove_from_siblings (w_container_t *);
w_container_t *w_container_get_last_child (w_container_t *);
#endif

147
src/widget/events.c Normal file
View File

@ -0,0 +1,147 @@
/*
Widgets library
Event functions for object
Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
Written by:
2009 Slava Zanko <slavazanko@google.com>
This file is part of the Midnight Commander.
The Midnight Commander 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.
The Midnight Commander 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., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#include "main.h"
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
/*** file scope functions ************************************************************************/
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
void
w_event_set (w_object_t * object, const char *event_name, w_event_cb_t cb)
{
if (object == NULL || event_name == NULL || cb == NULL)
return;
if (w_event_isset (object, event_name))
g_hash_table_replace (object->events, (gpointer) g_strdup (event_name), (gpointer) cb);
else
g_hash_table_insert (object->events, (gpointer) g_strdup (event_name), (gpointer) cb);
}
/* --------------------------------------------------------------------------------------------- */
void
w_event_remove (w_object_t * object, const char *event_name)
{
if (object == NULL || event_name == NULL)
return;
(void) g_hash_table_remove (object->events, (gpointer) event_name);
}
/* --------------------------------------------------------------------------------------------- */
gboolean
w_event_isset (w_object_t * object, const char *event_name)
{
if (object == NULL || event_name == NULL)
return FALSE;
return (g_hash_table_lookup (object->events, (gpointer) event_name) != NULL);
}
/* --------------------------------------------------------------------------------------------- */
gboolean
w_event_raise (w_object_t * object, const char *event_name, gpointer data)
{
w_event_cb_t cb;
if (object == NULL || event_name == NULL)
return FALSE;
cb = (w_event_cb_t) g_hash_table_lookup (object->events, (gpointer) event_name);
if (cb == NULL)
return FALSE;
return (*cb) (object, event_name, data);
}
/* --------------------------------------------------------------------------------------------- */
void
w_event_raise_to_parents (w_container_t * container, const char *event_name, gpointer data)
{
GPtrArray *containers_list;
w_container_t *one_of_parents;
guint index;
if (w_event_isset ((w_object_t *) container, event_name) &&
w_event_raise ((w_object_t *) container, event_name, data)
)
return;
/* getting path of current node from widgets tree */
containers_list = w_container_get_path (container);
if (containers_list == NULL)
return;
/* now get parent widgets in reverse order */
for (index = containers_list->len; index != 0; index--) {
one_of_parents = (w_container_t *) g_ptr_array_index (containers_list, index - 1);
if (w_event_isset ((w_object_t *) one_of_parents, event_name) &&
w_event_raise ((w_object_t *) one_of_parents, event_name, data))
break;
}
(void) g_ptr_array_free (containers_list, TRUE);
}
/* --------------------------------------------------------------------------------------------- */
gboolean
w_event_raise_to_children (w_container_t * container, const char *event_name, gpointer data)
{
w_container_t *child;
if (w_event_isset ((w_object_t *) container, event_name) &&
w_event_raise ((w_object_t *) container, event_name, data)
)
return TRUE;
if (container->children == NULL)
return FALSE;
for (child = container->children; child->next != NULL; child = child->next) {
if (!w_event_raise_to_children (child, event_name, data))
return TRUE;
}
return FALSE;
}
/* --------------------------------------------------------------------------------------------- */

28
src/widget/events.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef W_WIDGET_EVENTS_H
#define W_WIDGET_EVENTS_H
#include "object.h"
#include "container.h"
/*** typedefs(not structures) and defined constants ********************/
/*** enums *************************************************************/
/*** structures declarations (and typedefs of structures)***************/
/*** global variables defined in .c file *******************************/
/*** declarations of public functions **********************************/
void w_event_set (w_object_t *, const char *, w_event_cb_t);
void w_event_remove (w_object_t *, const char *);
gboolean w_event_isset (w_object_t *, const char *);
gboolean w_event_raise (w_object_t *, const char *, gpointer);
void w_event_raise_to_parents (w_container_t *, const char *, gpointer);
gboolean w_event_raise_to_children (w_container_t *, const char *, gpointer);
#endif

19
src/widget/internal.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef W_WIDGET_INTERNAL_H
#define W_WIDGET_INTERNAL_H
/*** typedefs(not structures) and defined constants ********************/
/*** enums *************************************************************/
typedef enum {
WTYPE_WINDOW
} w_type_t;
/*** structures declarations (and typedefs of structures)***************/
/*** global variables defined in .c file *******************************/
/*** declarations of public functions **********************************/
#endif

45
src/widget/main.c Normal file
View File

@ -0,0 +1,45 @@
/*
Widgets library
Interface functions.
Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
Written by:
2009 Slava Zanko <slavazanko@google.com>
This file is part of the Midnight Commander.
The Midnight Commander 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.
The Midnight Commander 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., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#include <config.h>
#include "../src/global.h"
#include "internal.h"
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
/*** file scope functions ************************************************************************/
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */

20
src/widget/main.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef W_WIDGET_MAIN_H
#define W_WIDGET_MAIN_H
#include "events.h"
#include "widget.h"
#include "widget-desktop.h"
/*** typedefs(not structures) and defined constants ********************/
/*** enums *************************************************************/
/*** structures declarations (and typedefs of structures)***************/
/*** global variables defined in .c file *******************************/
/*** declarations of public functions **********************************/
#endif

79
src/widget/object.c Normal file
View File

@ -0,0 +1,79 @@
/*
Widgets library
Low-level objects.
Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
Written by:
2009 Slava Zanko <slavazanko@google.com>
This file is part of the Midnight Commander.
The Midnight Commander 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.
The Midnight Commander 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., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#include "main.h"
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
static void
w_object_event_hash_destroy_key (gpointer data)
{
g_free (data);
}
/* --------------------------------------------------------------------------------------------- */
static void
w_object_event_hash_destroy_value (gpointer data)
{
}
/* --------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
void
w_object_init (w_object_t * object)
{
object->events = g_hash_table_new_full (g_str_hash, g_str_equal,
w_object_event_hash_destroy_key,
w_object_event_hash_destroy_value);
}
/* --------------------------------------------------------------------------------------------- */
void
w_object_deinit (w_object_t * object)
{
g_hash_table_destroy (object->events);
}
/* --------------------------------------------------------------------------------------------- */

29
src/widget/object.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef W_WIDGET_OBJECT_H
#define W_WIDGET_OBJECT_H
#include <config.h>
#include "../src/global.h"
/*** typedefs(not structures) and defined constants ********************/
/*** enums *************************************************************/
/*** structures declarations (and typedefs of structures)***************/
struct w_object_struct;
typedef gboolean (*w_event_cb_t) (struct w_object_struct *, const char *, gpointer);
typedef struct w_object_struct {
GHashTable *events;
} w_object_t;
/*** global variables defined in .c file *******************************/
/*** declarations of public functions **********************************/
void w_object_init (w_object_t *);
void w_object_deinit (w_object_t *);
#endif

View File

@ -0,0 +1,90 @@
/*
Widgets library
Desktop widget - this main widget on entire screen.
Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
Written by:
2009 Slava Zanko <slavazanko@google.com>
This file is part of the Midnight Commander.
The Midnight Commander 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.
The Midnight Commander 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., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#include "main.h"
#include "../src/tty/tty.h"
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
/*** file scope functions ************************************************************************/
static gboolean
w_desktop_cb_on_resize (w_object_t * object, const char *event_name, gpointer data)
{
/* Some actions here. May be, repaint background */
/* retranslate event to all childs */
(void) w_event_raise_to_children ((w_container_t *) object, event_name, data);
}
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
w_desktop_t *
w_desktop_new (const char *name)
{
/* required part */
w_desktop_t *desktop;
desktop = g_new0 (w_desktop_t, 1);
if (desktop == NULL)
return NULL;
w_widget_init ((w_widget_t *) desktop, name);
/* Some other init code here : */
desktop->widget.top = 0;
desktop->widget.left = 0;
desktop->widget.width = COLS;
desktop->widget.height = LINES;
w_event_set ((w_object_t *) desktop, "on_resize", w_desktop_cb_on_resize);
return desktop;
}
/* --------------------------------------------------------------------------------------------- */
void
w_desktop_free (w_desktop_t * desktop)
{
/* Some other deinit code here : */
/* required part */
w_widget_deinit ((w_widget_t *) desktop);
g_free (desktop);
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -0,0 +1,24 @@
#ifndef W_WIDGET_DESKTOP_H
#define W_WIDGET_DESKTOP_H
#include "widget.h"
/*** typedefs(not structures) and defined constants ********************/
/*** enums *************************************************************/
/*** structures declarations (and typedefs of structures)***************/
typedef struct w_desktop_struct {
w_widget_t widget;
} w_desktop_t;
/*** global variables defined in .c file *******************************/
/*** declarations of public functions **********************************/
w_desktop_t *w_desktop_new (const char *);
void w_desktop_free (w_desktop_t *);
#endif

View File

View File

0
src/widget/widget-menu.c Normal file
View File

0
src/widget/widget-menu.h Normal file
View File

View File

View File

View File

View File

View File

View File

93
src/widget/widget.c Normal file
View File

@ -0,0 +1,93 @@
/*
Widgets library
Common widgets.
Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
Written by:
2009 Slava Zanko <slavazanko@google.com>
This file is part of the Midnight Commander.
The Midnight Commander 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.
The Midnight Commander 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., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#include <string.h>
#include "main.h"
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
/*** file scope functions ************************************************************************/
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
void
w_widget_init (w_widget_t * widget, const char *name)
{
w_container_init ((w_container_t *) widget);
widget->top = 0;
widget->left = 0;
widget->width = 0;
widget->height = 0;
widget->name = (name != NULL) ? g_strdup (name) : NULL;
}
/* --------------------------------------------------------------------------------------------- */
void
w_widget_deinit (w_widget_t * widget)
{
g_free (widget->name);
w_container_deinit ((w_container_t *) widget);
}
/* --------------------------------------------------------------------------------------------- */
w_widget_t *
w_widget_find_by_name (w_widget_t * widget, const char *name)
{
w_container_t *container;
w_widget_t *ret;
if (widget == NULL || name == NULL)
return NULL;
if (strcmp (widget->name, name) == 0)
return widget;
container = (w_container_t *) widget;
if (container->children == NULL)
return NULL;
for (container = container->children; container != NULL; container = container->next) {
ret = w_widget_find_by_name ((w_widget_t *) container, name);
if (ret != NULL)
return ret;
}
return NULL;
}
/* --------------------------------------------------------------------------------------------- */

37
src/widget/widget.h Normal file
View File

@ -0,0 +1,37 @@
#ifndef W_WIDGET_H
#define W_WIDGET_H
#include "container.h"
/*** typedefs(not structures) and defined constants ********************/
/*** enums *************************************************************/
/*** structures declarations (and typedefs of structures)***************/
typedef struct w_widget_struct {
w_container_t tree;
char *name;
int left;
int top;
int width;
int height;
/* Todo: is this good place for these variables? */
int color_pair;
GHashTable *keybindings;
} w_widget_t;
/*** global variables defined in .c file *******************************/
/*** declarations of public functions **********************************/
void w_widget_init (w_widget_t *, const char *);
void w_widget_deinit (w_widget_t *);
w_widget_t *w_widget_find_by_name (w_widget_t *, const char *);
#endif