Back to index

im-sdk  12.3.91
chartable.c
Go to the documentation of this file.
00001 #include <gtk/gtk.h>
00002 #include <gdk/gdk.h>
00003 #include <gtk/gtkdrawingarea.h>
00004 #include "chartable.h"
00005 
00006 enum {
00007   PROP_0,
00008   PROP_RANGE_BEGIN,
00009   PROP_RANGE_END
00010 };
00011 
00012 
00013 static void chartable_class_init (CharTableClass *klass);
00014 static void chartable_init (CharTable *table);
00015 static void
00016 chartable_get_property (GObject      *object,
00017                      guint         property_id,
00018                      GValue       *value,
00019                      GParamSpec   *pspec);
00020 static void
00021 chartable_set_property (GObject      *object,
00022                      guint         property_id,
00023                      const GValue *value,
00024                      GParamSpec   *pspec);
00025 static gint
00026 event_handler_expose (GtkWidget *widget,
00027                     GdkEventExpose *event,
00028                     CharTable *chartable);
00029 static gint get_cell_vector (CharTable *table);
00030 static void draw_chartable (CharTable *table);
00031 static GtkWidget *make_scrollbar (CharTable *table);
00032 static void scroll_chartable (GtkAdjustment *adjustment, CharTable *table);
00033 
00034 
00035 
00036 
00037 GType
00038 chartable_get_type (void)
00039 {
00040   static GType chartable_type = 0;
00041 
00042   if (!chartable_type)
00043     {
00044       static const GTypeInfo chartable_info =
00045        {
00046          sizeof (CharTableClass),
00047          NULL,                /* base_init */
00048          NULL,                /* base_finalize */
00049          (GClassInitFunc) chartable_class_init,
00050          NULL,                /* class_finalize */
00051          NULL,                /* class_data */
00052          sizeof (CharTable),
00053          0,
00054          (GInstanceInitFunc) chartable_init,
00055        };
00056       chartable_type = g_type_register_static (GTK_TYPE_HBOX,
00057                                           "CharTable",
00058                                           &chartable_info, 0);
00059     }
00060   return chartable_type;
00061 }
00062 
00063 static void
00064 chartable_class_init (CharTableClass *klass)
00065 {
00066   GObjectClass *gobject_class;
00067   gobject_class = G_OBJECT_CLASS (klass);
00068 
00069   gobject_class->get_property = chartable_get_property;
00070   gobject_class->set_property = chartable_set_property;
00071   g_object_class_install_property (gobject_class,
00072                                PROP_RANGE_BEGIN,
00073                                g_param_spec_uint ("code_begin",
00074                                                 "code_begin",
00075                                                 "begin value of code range",
00076                                                 0, 0xffff, 0,
00077                                                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
00078   g_object_class_install_property (gobject_class,
00079                                PROP_RANGE_END,
00080                                g_param_spec_uint ("code_end",
00081                                                 "code_end",
00082                                                 "end value of code range",
00083                                                 0, 0xffff, 0,
00084                                                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));  
00085 
00086   
00087   return;
00088 }
00089 
00090 static void
00091 chartable_init (CharTable *table)
00092 {
00093   PangoContext *context;
00094   int n_cols, n_rows;
00095   n_cols = 16, n_rows = 16;
00096   table->drawingarea = gtk_drawing_area_new ();
00097   table->pixmap = NULL;
00098   table->ncols = n_cols;
00099   table->nrows = n_rows;
00100   gtk_widget_set_events (table->drawingarea,
00101                       GDK_EXPOSURE_MASK
00102                       );
00103 
00104   g_signal_connect (G_OBJECT (table->drawingarea),
00105                   "expose-event",
00106                   G_CALLBACK (event_handler_expose),
00107                   table);
00108 
00109   gtk_box_pack_start (GTK_BOX (table), table->drawingarea,
00110                     TRUE, TRUE, 0);
00111                     
00112 
00113 
00114   context = gtk_widget_get_pango_context (table->drawingarea);
00115   
00116   table->font_name = NULL;
00117   table->font_metrics =
00118     pango_context_get_metrics (context,
00119                             table->drawingarea->style->font_desc, NULL);
00120   table->pango_layout = pango_layout_new (context);
00121   pango_layout_set_font_description (table->pango_layout,
00122                                  table->drawingarea->style->font_desc);
00123 #if 0
00124   g_printf ("cell width should be %d\n", get_cell_vector (table));
00125 #endif
00126   gtk_widget_set_size_request (GTK_WIDGET (table->drawingarea),
00127                             get_cell_vector (table) * n_cols + (n_cols + 1),
00128                             get_cell_vector (table) * n_rows + (n_rows + 1));
00129   gtk_box_pack_start (GTK_BOX (table),
00130                     make_scrollbar (table),
00131                     FALSE, FALSE, 0);
00132   gtk_widget_show_all (GTK_WIDGET (table));
00133     
00134 }
00135 
00136 static GtkWidget *
00137 make_scrollbar (CharTable *table)
00138 {
00139   int n_rows;
00140   n_rows = table->nrows;
00141   
00142   table->adjustment =
00143     gtk_adjustment_new (0.0, 0.0, 100.0,
00144                      1.0 * (get_cell_vector (table) + 1),
00145                      1.0 * ((get_cell_vector (table) * n_rows + n_rows + 1)),
00146                      0.0);
00147   table->adjustment_changed_handler_id =
00148     g_signal_connect (G_OBJECT (table->adjustment), "value-changed",
00149                     G_CALLBACK (scroll_chartable), table);
00150   return gtk_vscrollbar_new (GTK_ADJUSTMENT (table->adjustment));
00151 }
00152 
00153 static void
00154 scroll_chartable (GtkAdjustment *adjustment, CharTable *table)
00155 {
00156   g_printf ("scrollbar is being changed...\n");
00157 }
00158 
00159 static void
00160 draw_borders (CharTable *table)
00161 {
00162   int n_cols, n_rows;
00163   int ic, ir;
00164   int x, y;
00165   n_cols = table->ncols;
00166   n_rows = table->nrows;
00167 
00168   gdk_draw_line (table->pixmap,
00169                table->drawingarea->style->dark_gc[GTK_STATE_NORMAL],
00170                0,0,0, table->drawingarea->allocation.height - 1);
00171 
00172   for (ic = 0, x = 0; ic < table->ncols; ic++){
00173     x += get_cell_vector (table) + 1 ;
00174     gdk_draw_line (table->pixmap,
00175                  table->drawingarea->style->dark_gc[GTK_STATE_NORMAL],
00176                  x, 0, x, table->drawingarea->allocation.height - 1);
00177   }
00178   
00179   gdk_draw_line (table->pixmap,
00180                table->drawingarea->style->dark_gc[GTK_STATE_NORMAL],
00181                0,0,0, table->drawingarea->allocation.width - 1);
00182   for (ir = 0, y = 0; ir < table->nrows; ir++){
00183     y += get_cell_vector (table) + 1;
00184     gdk_draw_line (table->pixmap,
00185                  table->drawingarea->style->dark_gc[GTK_STATE_NORMAL],
00186                  0, y, table->drawingarea->allocation.width - 1, y);
00187   }
00188 }
00189 
00190 static void
00191 chartable_get_property (GObject      *object,
00192                      guint         property_id,
00193                      GValue       *value,
00194                      GParamSpec   *pspec)
00195 {
00196   CharTable *table;
00197   table = CHARTABLE (object);
00198   switch (property_id)
00199     {
00200     case PROP_RANGE_BEGIN:
00201       g_value_set_uint (value, table->_begin);
00202       break;
00203     case PROP_RANGE_END:
00204       g_value_set_uint (value, table->_end);
00205       break;
00206     default:
00207       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
00208       break;      
00209     }
00210 }
00211 
00212 static void
00213 chartable_set_property (GObject      *object,
00214                      guint         property_id,
00215                      const GValue *value,
00216                      GParamSpec   *pspec)
00217 {
00218   CharTable *table;
00219   table = CHARTABLE (object);
00220   g_printf ("set_property seems to be being called\n");
00221   
00222   switch (property_id)
00223     {
00224     case PROP_RANGE_BEGIN:
00225       chartable_set_begin (table, g_value_get_uint (value));
00226       break;
00227     case PROP_RANGE_END:
00228       chartable_set_end (table, g_value_get_uint (value));
00229       break;
00230     }
00231 }
00232 
00233 static gint
00234 event_handler_expose (GtkWidget *widget,
00235                     GdkEventExpose *event,
00236                     CharTable *chartable)
00237 {
00238   if (chartable->pixmap == NULL){
00239     draw_chartable (chartable);
00240   }
00241   gdk_draw_drawable (chartable->drawingarea->window,
00242                    widget->style->fg_gc[GTK_STATE_NORMAL],
00243                    chartable->pixmap,
00244                    event->area.x, event->area.y,
00245                      event->area.x, event->area.y,
00246                      event->area.width, event->area.height);
00247   return FALSE;
00248 }
00249 
00250 static void
00251 draw_chartable (CharTable *chartable)
00252 {
00253   if (chartable->pixmap == NULL)
00254     chartable->pixmap =
00255       gdk_pixmap_new (chartable->drawingarea->window,
00256                     chartable->drawingarea->allocation.width,
00257                     chartable->drawingarea->allocation.height,
00258                     -1);
00259   gdk_draw_rectangle (chartable->pixmap,
00260                     chartable->drawingarea->style->base_gc[GTK_STATE_NORMAL],
00261                     TRUE, 0, 0,
00262                     chartable->drawingarea->allocation.width,
00263                     chartable->drawingarea->allocation.height);
00264   gdk_draw_line (chartable->pixmap,
00265                chartable->drawingarea->style->base_gc[GTK_STATE_NORMAL],
00266                0,0,
00267                chartable->drawingarea->allocation.width,
00268                chartable->drawingarea->allocation.height);
00269   draw_borders (chartable);
00270 
00271 }
00272 
00273 static gint
00274 get_font_height (PangoFontMetrics *font_metrics)
00275 {
00276   gint height;
00277   height = pango_font_metrics_get_ascent (font_metrics) +
00278     pango_font_metrics_get_descent (font_metrics);
00279   return PANGO_PIXELS (height);
00280 }
00281 
00282 static gint
00283 get_cell_vector (CharTable *table)
00284 {
00285   return get_font_height (table->font_metrics) + 8;
00286 }
00287 
00288 GtkWidget *
00289 chartable_new (void)
00290 {
00291   return g_object_new (CHARTABLE_TYPE, NULL);
00292 }
00293 
00294 GtkWidget *
00295 chartable_new_with_range (guint begin, guint end)
00296 {
00297   guint real_begin, real_end;
00298 
00299   real_begin = (begin & 0xff00);
00300   real_end = (end | 0x00ff);
00301   
00302   return g_object_new (CHARTABLE_TYPE,
00303                      "code_begin", real_begin,
00304                      "code_end", real_end,
00305                      NULL);
00306 }
00307 
00308 void
00309 chartable_set_begin (CharTable *table, guint begin)
00310 {
00311   table->_begin = (begin & 0xff00);
00312   g_object_notify (G_OBJECT (table), "code_begin");
00313 }
00314 
00315 void
00316 chartable_set_end (CharTable *table, guint end)
00317 {
00318   table->_end = (end | 0x00ff);
00319   g_object_notify (G_OBJECT (table), "code_end");
00320 }
00321 
00322 void
00323 chartable_set_range (CharTable *table, guint begin, guint end)
00324 {
00325   table->_begin = (begin & 0xff00);
00326   table->_end = (end | 0x00ff);
00327   
00328   g_object_notify (G_OBJECT (table), "code_begin");
00329   g_object_notify (G_OBJECT (table), "code_end");  
00330 }