| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* xap_TableWidget.cpp |
| 4 |
* Copyright 2002 Joaquin Cuenca Abela |
| 5 |
* Copyright 2016-2024 Hubert Figuière |
| 6 |
* |
| 7 |
* Authors: |
| 8 |
* Joaquin Cuenca Abela ([email protected]) |
| 9 |
* |
| 10 |
* This library is free software; you can redistribute it and/or |
| 11 |
* modify it under the terms of the GNU Library General Public |
| 12 |
* License, version 2, as published by the Free Software Foundation. |
| 13 |
* |
| 14 |
* This library is distributed in the hope that it will be useful, but |
| 15 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
* Library General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU Library General Public |
| 20 |
* License along with this library; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 22 |
* 02110-1301 USA. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
|
| 28 |
#include <algorithm> |
| 29 |
|
| 30 |
#include <gdk/gdkkeysyms.h> |
| 31 |
#include <gdk/gdk.h> |
| 32 |
#include <gtk/gtk.h> |
| 33 |
|
| 34 |
#include "xap_Gtk2Compat.h" |
| 35 |
#if GTK_CHECK_VERSION(3,0,0) |
| 36 |
#include "xap_GtkStyle.h" |
| 37 |
#endif |
| 38 |
#include "xap_UnixTableWidget.h" |
| 39 |
#include "ut_debugmsg.h" |
| 40 |
#include "ut_assert.h" |
| 41 |
|
| 42 |
/* NONE:UINT,UINT (/dev/stdin:1) */ |
| 43 |
static void |
| 44 |
g_cclosure_user_marshal_VOID__UINT_UINT (GClosure *closure, |
| 45 |
GValue * /*return_value*/, |
| 46 |
guint n_param_values, |
| 47 |
const GValue *param_values, |
| 48 |
gpointer /*invocation_hint*/, |
| 49 |
gpointer marshal_data) |
| 50 |
{ |
| 51 |
typedef void (*GMarshalFunc_VOID__UINT_UINT) (gpointer data1, |
| 52 |
guint arg_1, |
| 53 |
guint arg_2, |
| 54 |
gpointer data2); |
| 55 |
register GMarshalFunc_VOID__UINT_UINT callback; |
| 56 |
register GCClosure *cc = (GCClosure*) closure; |
| 57 |
register gpointer data1, data2; |
| 58 |
|
| 59 |
UT_return_if_fail (n_param_values == 3); |
| 60 |
UT_DEBUGMSG(("IN AbiTable g_cclose_.. \n")); |
| 61 |
if (G_CCLOSURE_SWAP_DATA (closure)) |
| 62 |
{ |
| 63 |
data1 = closure->data; |
| 64 |
data2 = g_value_get_pointer (param_values + 0); |
| 65 |
} |
| 66 |
else |
| 67 |
{ |
| 68 |
data1 = g_value_get_pointer (param_values + 0); |
| 69 |
data2 = closure->data; |
| 70 |
} |
| 71 |
callback = (GMarshalFunc_VOID__UINT_UINT) (marshal_data ? marshal_data : cc->callback); |
| 72 |
UT_DEBUGMSG(("Calling callback marshell data %p cc %p \n",callback,cc)); |
| 73 |
|
| 74 |
callback (data1, |
| 75 |
g_value_get_uint (param_values + 1), |
| 76 |
g_value_get_uint (param_values + 2), |
| 77 |
data2); |
| 78 |
} |
| 79 |
|
| 80 |
enum |
| 81 |
{ |
| 82 |
SELECTED, |
| 83 |
LAST_SIGNAL |
| 84 |
}; |
| 85 |
|
| 86 |
static gint abi_table_signals [LAST_SIGNAL] = { 0 }; |
| 87 |
|
| 88 |
static GtkWidgetClass *abi_table_parent_class; |
| 89 |
|
| 90 |
/* ------------------- now the guts of AbiTable ---------------------- */ |
| 91 |
|
| 92 |
static const guint cell_width = 24; |
| 93 |
static const guint cell_height = 24; |
| 94 |
static const guint cell_spacing = 4; |
| 95 |
static const guint init_rows = 0; |
| 96 |
static const guint init_cols = 0; |
| 97 |
|
| 98 |
static inline void |
| 99 |
cells_to_pixels(guint cols, guint rows, guint* w, guint* h) |
| 100 |
{ |
| 101 |
UT_return_if_fail(w); |
| 102 |
UT_return_if_fail(h); |
| 103 |
|
| 104 |
*w = cell_width * cols + cell_spacing * (cols + 1); |
| 105 |
*h = cell_height * rows + cell_spacing * (rows + 1); |
| 106 |
} |
| 107 |
|
| 108 |
static inline void |
| 109 |
pixels_to_cells(guint w, guint h, guint* cols, guint* rows) |
| 110 |
{ |
| 111 |
UT_return_if_fail(cols); |
| 112 |
UT_return_if_fail(rows); |
| 113 |
|
| 114 |
*cols = w / (cell_width + cell_spacing) + 1; |
| 115 |
*rows = h / (cell_height + cell_spacing) + 1; |
| 116 |
} |
| 117 |
|
| 118 |
static void |
| 119 |
abi_table_resize(AbiTable* table) |
| 120 |
{ |
| 121 |
guint width; |
| 122 |
guint height; |
| 123 |
char* text; |
| 124 |
GtkRequisition size; |
| 125 |
|
| 126 |
UT_return_if_fail(table); |
| 127 |
|
| 128 |
if (table->selected_rows == 0 && table->selected_cols == 0) |
| 129 |
text = g_strdup(table->szCancel); |
| 130 |
else |
| 131 |
{ |
| 132 |
text = g_strdup_printf("%d x %d %s", table->selected_rows, table->selected_cols, table->szTable); |
| 133 |
} |
| 134 |
cells_to_pixels(table->total_cols, table->total_rows, &width, &height); |
| 135 |
gtk_widget_get_preferred_size(GTK_WIDGET(table->window_label), &size, NULL); |
| 136 |
|
| 137 |
gtk_label_set_text(table->window_label, text); |
| 138 |
gtk_window_resize(table->window, width + 1, height + size.height); |
| 139 |
|
| 140 |
g_free(text); |
| 141 |
} |
| 142 |
|
| 143 |
extern "C" void |
| 144 |
abi_table_set_selected (AbiTable* abi_table, guint rows, guint cols) |
| 145 |
{ |
| 146 |
UT_return_if_fail (abi_table); |
| 147 |
|
| 148 |
abi_table->selected_rows = rows; |
| 149 |
abi_table->selected_cols = cols; |
| 150 |
|
| 151 |
abi_table_resize(abi_table); |
| 152 |
} |
| 153 |
|
| 154 |
extern "C" void |
| 155 |
abi_table_get_selected (const AbiTable* abi_table, guint* rows, guint* cols) |
| 156 |
{ |
| 157 |
UT_return_if_fail (abi_table); |
| 158 |
|
| 159 |
if (rows) |
| 160 |
*rows = abi_table->selected_rows; |
| 161 |
|
| 162 |
if (cols) |
| 163 |
*cols = abi_table->selected_cols; |
| 164 |
} |
| 165 |
|
| 166 |
extern "C" void |
| 167 |
abi_table_set_max_size (AbiTable* abi_table, guint rows, guint cols) |
| 168 |
{ |
| 169 |
UT_return_if_fail (abi_table); |
| 170 |
|
| 171 |
abi_table->total_rows = rows; |
| 172 |
abi_table->total_cols = cols; |
| 173 |
|
| 174 |
abi_table_resize(abi_table); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
extern "C" void |
| 179 |
abi_table_set_labels(AbiTable* abi_table, const gchar * szTable, const gchar * szCancel) |
| 180 |
{ |
| 181 |
if(abi_table->szTable) |
| 182 |
g_free(abi_table->szTable); |
| 183 |
abi_table->szTable = g_strdup(szTable); |
| 184 |
if(abi_table->szCancel) |
| 185 |
g_free(abi_table->szCancel); |
| 186 |
abi_table->szCancel = g_strdup(szCancel); |
| 187 |
} |
| 188 |
|
| 189 |
extern "C" void |
| 190 |
abi_table_get_max_size (const AbiTable* abi_table, guint* rows, guint* cols) |
| 191 |
{ |
| 192 |
UT_return_if_fail (abi_table); |
| 193 |
|
| 194 |
if (rows) |
| 195 |
*rows = abi_table->total_rows; |
| 196 |
|
| 197 |
if (cols) |
| 198 |
*cols = abi_table->total_cols; |
| 199 |
} |
| 200 |
|
| 201 |
static gboolean |
| 202 |
#if GTK_CHECK_VERSION(3,0,0) |
| 203 |
on_drawing_area_event (GtkWidget *area, cairo_t *cr, gpointer user_data) |
| 204 |
#else |
| 205 |
on_drawing_area_event (GtkWidget *area, GdkEventExpose *, gpointer user_data) |
| 206 |
#endif |
| 207 |
{ |
| 208 |
AbiTable* table = static_cast<AbiTable*>(user_data); |
| 209 |
|
| 210 |
#if GTK_CHECK_VERSION(3,0,0) |
| 211 |
if (!table || !table->style_context) { |
| 212 |
return TRUE; |
| 213 |
} |
| 214 |
#endif |
| 215 |
|
| 216 |
guint i; |
| 217 |
guint j; |
| 218 |
guint selected_rows = table->selected_rows; |
| 219 |
guint selected_cols = table->selected_cols; |
| 220 |
guint x; |
| 221 |
guint y; |
| 222 |
|
| 223 |
#if GTK_CHECK_VERSION(3,0,0) |
| 224 |
GtkStyleContext* ctxt = gtk_widget_get_style_context(GTK_WIDGET(area)); |
| 225 |
gtk_style_context_save(ctxt); |
| 226 |
gtk_style_context_set_state(ctxt, GTK_STATE_FLAG_FOCUSED); |
| 227 |
#else |
| 228 |
gdk_draw_rectangle (area->window, |
| 229 |
area->style->bg_gc[GTK_STATE_NORMAL], |
| 230 |
TRUE, |
| 231 |
0, 0, |
| 232 |
area->allocation.width, |
| 233 |
area->allocation.height); |
| 234 |
#endif |
| 235 |
|
| 236 |
for (i = 0; i < table->total_rows; ++i) { |
| 237 |
for (j = 0; j < table->total_cols; ++j) { |
| 238 |
cells_to_pixels(j, i, &x, &y); |
| 239 |
|
| 240 |
#if GTK_CHECK_VERSION(3,0,0) |
| 241 |
if (i < selected_rows && j < selected_cols) { |
| 242 |
gtk_style_context_set_state(table->style_context, GTK_STATE_FLAG_SELECTED); |
| 243 |
} else { |
| 244 |
gtk_style_context_set_state(table->style_context, GTK_STATE_FLAG_NORMAL); |
| 245 |
} |
| 246 |
gtk_render_background(table->style_context, cr, x + 1, y + 1, |
| 247 |
cell_width - 1, cell_height - 1); |
| 248 |
|
| 249 |
gtk_render_frame(ctxt, cr, x, y, cell_width, cell_height); |
| 250 |
#else |
| 251 |
gdk_draw_rectangle (area->window, |
| 252 |
area->style->dark_gc[GTK_STATE_NORMAL], |
| 253 |
FALSE, |
| 254 |
x - 1, y - 1, |
| 255 |
cell_width + 1, |
| 256 |
cell_height + 1); |
| 257 |
if (i < selected_rows && j < selected_cols) { |
| 258 |
gdk_draw_rectangle (area->window, |
| 259 |
table->selected_gc, |
| 260 |
TRUE, |
| 261 |
x, y, |
| 262 |
cell_width, |
| 263 |
cell_height); |
| 264 |
} |
| 265 |
else { |
| 266 |
gdk_draw_rectangle (area->window, |
| 267 |
area->style->white_gc, |
| 268 |
TRUE, |
| 269 |
x, y, |
| 270 |
cell_width, |
| 271 |
cell_height); |
| 272 |
} |
| 273 |
/* black border line */ |
| 274 |
gdk_draw_line (area->window, |
| 275 |
area->style->black_gc, |
| 276 |
area->allocation.width - 1, 0, area->allocation.width - 1, area->allocation.height - 1); |
| 277 |
gdk_draw_line (area->window, |
| 278 |
area->style->black_gc, |
| 279 |
area->allocation.width - 1, area->allocation.height - 1, 0, area->allocation.height - 1); |
| 280 |
|
| 281 |
/* dark border line */ |
| 282 |
gdk_draw_line (area->window, |
| 283 |
area->style->dark_gc[GTK_STATE_NORMAL], |
| 284 |
area->allocation.width - 2, 1, area->allocation.width - 2, |
| 285 |
area->allocation.height - 2); |
| 286 |
gdk_draw_line (area->window, |
| 287 |
area->style->dark_gc[GTK_STATE_NORMAL], |
| 288 |
area->allocation.width - 2, area->allocation.height - 2, 1, |
| 289 |
area->allocation.height - 2); |
| 290 |
|
| 291 |
/* ligth border line */ |
| 292 |
gdk_draw_line (area->window, |
| 293 |
area->style->light_gc[GTK_STATE_NORMAL], |
| 294 |
0, 0, area->allocation.width - 3, 0); |
| 295 |
gdk_draw_line (area->window, |
| 296 |
area->style->light_gc[GTK_STATE_NORMAL], |
| 297 |
0, 0, 0, area->allocation.height - 2); |
| 298 |
#endif |
| 299 |
} |
| 300 |
} |
| 301 |
#if GTK_CHECK_VERSION(3,0,0) |
| 302 |
gtk_style_context_restore(ctxt); |
| 303 |
#endif |
| 304 |
|
| 305 |
return TRUE; |
| 306 |
} |
| 307 |
|
| 308 |
static inline guint |
| 309 |
my_max(guint a, guint b) |
| 310 |
{ |
| 311 |
return a < b ? b : a; |
| 312 |
} |
| 313 |
|
| 314 |
static gboolean |
| 315 |
on_motion_notify_event (GtkWidget *window, GdkEventMotion *ev, gpointer user_data) |
| 316 |
{ |
| 317 |
AbiTable* table = static_cast<AbiTable*>(user_data); |
| 318 |
guint selected_cols; |
| 319 |
guint selected_rows; |
| 320 |
double x = ev->x; |
| 321 |
double y = ev->y; |
| 322 |
|
| 323 |
|
| 324 |
#if GTK_CHECK_VERSION(3, 0, 0) |
| 325 |
// In Gtk3, the coordinate of the motion event are relative to the top-level |
| 326 |
x -= table->pos_x; |
| 327 |
y -= table->pos_y; |
| 328 |
#endif |
| 329 |
|
| 330 |
if (x < 0 || y < 0) |
| 331 |
return TRUE; |
| 332 |
|
| 333 |
pixels_to_cells(static_cast<guint>(x), static_cast<guint>(y), &selected_cols, &selected_rows); |
| 334 |
|
| 335 |
if ((selected_cols != table->selected_cols) || (selected_rows != table->selected_rows)) |
| 336 |
{ |
| 337 |
/* grow or shrink the table widget as necessary */ |
| 338 |
table->selected_cols = selected_cols; |
| 339 |
table->selected_rows = selected_rows; |
| 340 |
|
| 341 |
if (table->selected_rows <= 0 || table->selected_cols <= 0) |
| 342 |
table->selected_rows = table->selected_cols = 0; |
| 343 |
|
| 344 |
table->total_rows = my_max(table->selected_rows + 1, 3); |
| 345 |
table->total_cols = my_max(table->selected_cols + 1, 3); |
| 346 |
|
| 347 |
abi_table_resize(table); |
| 348 |
gtk_widget_queue_draw (window); |
| 349 |
} |
| 350 |
|
| 351 |
return TRUE; |
| 352 |
} |
| 353 |
|
| 354 |
static void |
| 355 |
restart_widget (AbiTable *table) |
| 356 |
{ |
| 357 |
table->selected_cols = init_cols; |
| 358 |
table->selected_rows = init_rows; |
| 359 |
table->total_cols = my_max(init_cols + 1, 5); |
| 360 |
table->total_rows = my_max(init_rows + 1, 6); |
| 361 |
g_signal_emit_by_name(table, "released"); |
| 362 |
gtk_widget_hide(GTK_WIDGET(table->window)); |
| 363 |
} |
| 364 |
|
| 365 |
/* |
| 366 |
* Fires signal "selected", and reset and hide the widget |
| 367 |
*/ |
| 368 |
static void |
| 369 |
emit_selected (AbiTable *table) |
| 370 |
{ |
| 371 |
gtk_widget_hide(GTK_WIDGET(table->window)); |
| 372 |
|
| 373 |
while (gtk_events_pending()) |
| 374 |
gtk_main_iteration(); |
| 375 |
|
| 376 |
if (table->selected_rows > 0 && table->selected_cols > 0) |
| 377 |
g_signal_emit (G_OBJECT (table), |
| 378 |
abi_table_signals [SELECTED], 0, |
| 379 |
table->selected_rows, table->selected_cols); |
| 380 |
|
| 381 |
restart_widget(table); |
| 382 |
} |
| 383 |
|
| 384 |
static gboolean |
| 385 |
on_button_release_event (GtkWidget *, GdkEventButton *ev, gpointer user_data) |
| 386 |
{ |
| 387 |
AbiTable* table = static_cast<AbiTable*>(user_data); |
| 388 |
|
| 389 |
/* Quick test to know if we're possibly over the button */ |
| 390 |
if (ev->y < 0.0 && ev->x >= 0.0) |
| 391 |
{ |
| 392 |
GtkRequisition size; |
| 393 |
|
| 394 |
gtk_widget_get_preferred_size(GTK_WIDGET(table), &size, NULL); |
| 395 |
|
| 396 |
/* And now, precise and slightly slower test. |
| 397 |
I wonder if the double test really matters from a speed pov */ |
| 398 |
if (-ev->y < size.height && ev->x < size.width) |
| 399 |
return TRUE; |
| 400 |
} |
| 401 |
|
| 402 |
emit_selected(table); |
| 403 |
|
| 404 |
return TRUE; |
| 405 |
} |
| 406 |
|
| 407 |
static gboolean |
| 408 |
on_leave_event (GtkWidget *area, |
| 409 |
GdkEventCrossing *event, |
| 410 |
gpointer user_data) |
| 411 |
{ |
| 412 |
AbiTable* table = static_cast<AbiTable*>(user_data); |
| 413 |
|
| 414 |
if (gtk_widget_get_visible(GTK_WIDGET(table->window)) && (event->x < 0 || event->y < 0)) |
| 415 |
{ |
| 416 |
table->selected_rows = 0; |
| 417 |
table->selected_cols = 0; |
| 418 |
table->total_rows = my_max(table->selected_rows + 1, 3); |
| 419 |
table->total_cols = my_max(table->selected_cols + 1, 3); |
| 420 |
|
| 421 |
abi_table_resize(table); |
| 422 |
gtk_widget_queue_draw (area); |
| 423 |
} |
| 424 |
|
| 425 |
return TRUE; |
| 426 |
} |
| 427 |
|
| 428 |
static gboolean |
| 429 |
popup_grab_on_window (GdkWindow *window, |
| 430 |
guint32 activate_time) |
| 431 |
{ |
| 432 |
#if !GTK_CHECK_VERSION(3,20,0) |
| 433 |
GdkEventMask emask = static_cast<GdkEventMask>(GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | |
| 434 |
GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK | |
| 435 |
GDK_ENTER_NOTIFY_MASK) ; |
| 436 |
if ((XAP_gdk_pointer_grab (window, FALSE,emask, |
| 437 |
NULL, NULL, activate_time) == 0)) |
| 438 |
{ |
| 439 |
if (XAP_gdk_keyboard_grab (window, FALSE, |
| 440 |
activate_time) == 0) |
| 441 |
return TRUE; |
| 442 |
else |
| 443 |
{ |
| 444 |
XAP_gdk_pointer_ungrab (activate_time); |
| 445 |
return FALSE; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
return FALSE; |
| 450 |
#else |
| 451 |
GdkSeat *seat = gdk_display_get_default_seat(gdk_window_get_display(window)); |
| 452 |
return gdk_seat_grab(seat, window, GDK_SEAT_CAPABILITY_ALL, |
| 453 |
FALSE, nullptr, nullptr, nullptr, nullptr) == GDK_GRAB_SUCCESS; |
| 454 |
#endif |
| 455 |
} |
| 456 |
|
| 457 |
static void |
| 458 |
on_pressed(GtkButton* button, gpointer user_data) |
| 459 |
{ |
| 460 |
AbiTable* table = static_cast<AbiTable*>(user_data); |
| 461 |
int left, top; |
| 462 |
GtkAllocation alloc; |
| 463 |
|
| 464 |
/* Temporarily grab pointer and keyboard on a window we know exists; we |
| 465 |
* do this so that the grab (with owner events == TRUE) affects |
| 466 |
* events generated when the window is mapped, such as enter |
| 467 |
* notify events on subwidgets. If the grab fails, bail out. |
| 468 |
*/ |
| 469 |
if (!popup_grab_on_window (gtk_widget_get_window(GTK_WIDGET(button)), |
| 470 |
gtk_get_current_event_time ())) |
| 471 |
return; |
| 472 |
|
| 473 |
auto toplevel = gtk_widget_get_toplevel(GTK_WIDGET(table)); |
| 474 |
gtk_window_set_transient_for(table->window, GTK_WINDOW(toplevel)); |
| 475 |
gdk_window_get_origin (gtk_widget_get_window(GTK_WIDGET(table)), &left, &top); |
| 476 |
gtk_widget_get_allocation(GTK_WIDGET(table), &alloc); |
| 477 |
table->pos_x = left + alloc.x; |
| 478 |
table->pos_y = top + alloc.y + alloc.height; |
| 479 |
gtk_window_move(table->window, table->pos_x, table->pos_y); |
| 480 |
abi_table_resize(table); |
| 481 |
|
| 482 |
gtk_widget_show(GTK_WIDGET(table->window)); |
| 483 |
gtk_widget_grab_focus(GTK_WIDGET(table->window)); |
| 484 |
|
| 485 |
/* Now transfer our grabs to the popup window; this |
| 486 |
* should always succeed. |
| 487 |
*/ |
| 488 |
popup_grab_on_window (gtk_widget_get_window(GTK_WIDGET(table->area)), |
| 489 |
gtk_get_current_event_time ()); |
| 490 |
#if !GTK_CHECK_VERSION(3,0,0) |
| 491 |
GdkColor selected_color = (GTK_WIDGET (button))->style->base[GTK_STATE_SELECTED]; |
| 492 |
|
| 493 |
/* leak */ |
| 494 |
table->selected_gc = gdk_gc_new(GTK_WIDGET(button)->window); |
| 495 |
gdk_gc_set_rgb_fg_color(table->selected_gc, &selected_color); |
| 496 |
#endif |
| 497 |
} |
| 498 |
|
| 499 |
gboolean |
| 500 |
on_key_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data) |
| 501 |
{ |
| 502 |
AbiTable* table = static_cast<AbiTable*>(user_data); |
| 503 |
gboolean grew = FALSE; |
| 504 |
|
| 505 |
switch (event->keyval) |
| 506 |
{ |
| 507 |
case GDK_KEY_Up: |
| 508 |
case GDK_KEY_KP_Up: |
| 509 |
if (table->selected_rows > 0) |
| 510 |
--table->selected_rows; |
| 511 |
break; |
| 512 |
case GDK_KEY_Down: |
| 513 |
case GDK_KEY_KP_Down: |
| 514 |
grew = TRUE; |
| 515 |
++table->selected_rows; |
| 516 |
break; |
| 517 |
case GDK_KEY_Left: |
| 518 |
case GDK_KEY_KP_Left: |
| 519 |
if (table->selected_cols > 0) |
| 520 |
--table->selected_cols; |
| 521 |
break; |
| 522 |
case GDK_KEY_Right: |
| 523 |
case GDK_KEY_KP_Right: |
| 524 |
grew = TRUE; |
| 525 |
++table->selected_cols; |
| 526 |
break; |
| 527 |
case GDK_KEY_Escape: |
| 528 |
restart_widget(table); |
| 529 |
return TRUE; |
| 530 |
case GDK_KEY_KP_Space: |
| 531 |
case GDK_KEY_KP_Enter: |
| 532 |
case GDK_KEY_space: |
| 533 |
case GDK_KEY_3270_Enter: |
| 534 |
case GDK_KEY_ISO_Enter: |
| 535 |
case GDK_KEY_Return: |
| 536 |
emit_selected(table); |
| 537 |
return TRUE; |
| 538 |
} |
| 539 |
|
| 540 |
if(table->selected_rows == 0 || table->selected_cols == 0) |
| 541 |
table->selected_rows = table->selected_cols = (grew ? 1 : 0) ; |
| 542 |
|
| 543 |
table->total_rows = my_max(table->selected_rows + 1, 3); |
| 544 |
table->total_cols = my_max(table->selected_cols + 1, 3); |
| 545 |
|
| 546 |
abi_table_resize(table); |
| 547 |
gtk_widget_queue_draw (widget); |
| 548 |
|
| 549 |
return TRUE; |
| 550 |
} |
| 551 |
|
| 552 |
void abi_table_set_icon(AbiTable* abi_table,GtkWidget * gtkImageIcon) |
| 553 |
{ |
| 554 |
if(!GTK_IS_IMAGE(G_OBJECT(gtkImageIcon))) |
| 555 |
return; |
| 556 |
g_object_unref (G_OBJECT (abi_table->icon)); |
| 557 |
abi_table->icon = gtkImageIcon; |
| 558 |
} |
| 559 |
|
| 560 |
/* ------------------- and now the GObject part ---------------------- */ |
| 561 |
|
| 562 |
static void |
| 563 |
abi_table_dispose (GObject *instance) |
| 564 |
{ |
| 565 |
AbiTable* self = ABITABLE_WIDGET(instance); |
| 566 |
|
| 567 |
// For some reason I get an alert |
| 568 |
if(self->label) { |
| 569 |
g_object_unref(self->label); |
| 570 |
self->label = NULL; |
| 571 |
} |
| 572 |
|
| 573 |
if(self->szTable) { |
| 574 |
g_free(self->szTable); |
| 575 |
self->szTable = NULL; |
| 576 |
} |
| 577 |
if(self->szCancel) { |
| 578 |
g_free(self->szCancel); |
| 579 |
self->szCancel = NULL; |
| 580 |
} |
| 581 |
|
| 582 |
#if GTK_CHECK_VERSION(3,0,0) |
| 583 |
g_clear_object(&self->style_context); |
| 584 |
#endif |
| 585 |
|
| 586 |
G_OBJECT_CLASS (abi_table_parent_class)->dispose (instance); |
| 587 |
} |
| 588 |
|
| 589 |
static void |
| 590 |
abi_table_class_init (AbiTableClass *klass) |
| 591 |
{ |
| 592 |
GtkWidgetClass *object_class = reinterpret_cast<GtkWidgetClass*>(klass); |
| 593 |
|
| 594 |
G_OBJECT_CLASS(object_class)->dispose = abi_table_dispose; |
| 595 |
|
| 596 |
abi_table_parent_class = static_cast<GtkWidgetClass *>(g_type_class_peek (GTK_TYPE_BUTTON)); |
| 597 |
abi_table_signals [SELECTED] = |
| 598 |
g_signal_new ("selected", |
| 599 |
G_OBJECT_CLASS_TYPE (object_class), |
| 600 |
G_SIGNAL_RUN_LAST, |
| 601 |
G_STRUCT_OFFSET (AbiTableClass, selected), |
| 602 |
NULL, NULL, |
| 603 |
g_cclosure_user_marshal_VOID__UINT_UINT, |
| 604 |
G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT); |
| 605 |
} |
| 606 |
|
| 607 |
static void |
| 608 |
abi_table_init (AbiTable* table) |
| 609 |
{ |
| 610 |
char* text = g_strdup_printf("%d x %d ", init_rows, init_cols); |
| 611 |
|
| 612 |
#if GTK_CHECK_VERSION(3,0,0) |
| 613 |
table->style_context = XAP_GtkStyle_get_style(NULL, "GtkTreeView.view"); // "textview.view" |
| 614 |
#endif |
| 615 |
|
| 616 |
table->button_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 617 |
|
| 618 |
table->window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_POPUP)); |
| 619 |
table->window_vbox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 0)); |
| 620 |
|
| 621 |
table->area = GTK_DRAWING_AREA(gtk_drawing_area_new()); |
| 622 |
|
| 623 |
table->handlers = 0; |
| 624 |
table->window_label = GTK_LABEL(gtk_label_new(text)); |
| 625 |
g_free(text); |
| 626 |
table->szTable = NULL; |
| 627 |
table->szCancel = NULL; |
| 628 |
gtk_container_add(GTK_CONTAINER(table->window), GTK_WIDGET(table->window_vbox)); |
| 629 |
gtk_box_pack_end(GTK_BOX(table->window_vbox), GTK_WIDGET(table->window_label), FALSE, FALSE, 0); |
| 630 |
gtk_box_pack_end(GTK_BOX(table->window_vbox), GTK_WIDGET(table->area), TRUE, TRUE, 0); |
| 631 |
|
| 632 |
gtk_widget_show_all(GTK_WIDGET(table->window_vbox)); |
| 633 |
|
| 634 |
table->pos_x = 0; |
| 635 |
table->pos_y = 0; |
| 636 |
table->selected_rows = init_rows; |
| 637 |
table->selected_cols = init_cols; |
| 638 |
|
| 639 |
table->total_rows = my_max(init_rows + 1, 5); |
| 640 |
table->total_cols = my_max(init_cols + 1, 6); |
| 641 |
|
| 642 |
abi_table_resize(table); |
| 643 |
|
| 644 |
table->label = gtk_label_new_with_mnemonic(table->stock_item.label); |
| 645 |
table->icon = gtk_image_new_from_resource("/com/abisource/AbiWord/24x24/actions/tb_insert_table.png"); |
| 646 |
gtk_widget_show(table->icon); |
| 647 |
gtk_widget_show(table->label); |
| 648 |
g_object_ref_sink(table->label); |
| 649 |
// |
| 650 |
// We actually never want this label in toolbar |
| 651 |
// gtk_box_pack_end(GTK_BOX(table->button_box), table->label, FALSE, FALSE, 0); |
| 652 |
gtk_box_pack_end(GTK_BOX(table->button_box), table->icon, FALSE, FALSE, 0); |
| 653 |
UT_DEBUGMSG(("abi-table icon loaded %p !\n",table->icon)); |
| 654 |
|
| 655 |
gtk_container_add(GTK_CONTAINER(table), GTK_WIDGET(table->button_box)); |
| 656 |
|
| 657 |
g_signal_connect(G_OBJECT(table), "pressed", |
| 658 |
G_CALLBACK(on_pressed), static_cast<gpointer>(table)); |
| 659 |
#if GTK_CHECK_VERSION(3,0,0) |
| 660 |
g_signal_connect(G_OBJECT(table->area), "draw", |
| 661 |
G_CALLBACK(on_drawing_area_event), static_cast<gpointer>(table)); |
| 662 |
#else |
| 663 |
g_signal_connect(G_OBJECT(table->area), "expose_event", |
| 664 |
G_CALLBACK(on_drawing_area_event), static_cast<gpointer>(table)); |
| 665 |
#endif |
| 666 |
g_signal_connect(G_OBJECT(table->area), "motion_notify_event", |
| 667 |
G_CALLBACK(on_motion_notify_event), static_cast<gpointer>(table)); |
| 668 |
g_signal_connect(G_OBJECT(table->area), "button_release_event", |
| 669 |
G_CALLBACK(on_button_release_event), static_cast<gpointer>(table)); |
| 670 |
g_signal_connect(G_OBJECT(table->area), "button_press_event", |
| 671 |
G_CALLBACK(on_button_release_event), static_cast<gpointer>(table)); |
| 672 |
g_signal_connect(G_OBJECT(table->area), "leave_notify_event", |
| 673 |
G_CALLBACK(on_leave_event), static_cast<gpointer>(table)); |
| 674 |
g_signal_connect(G_OBJECT(table->window), "key_press_event", |
| 675 |
G_CALLBACK(on_key_event), static_cast<gpointer>(table)); |
| 676 |
|
| 677 |
gtk_widget_set_events (GTK_WIDGET(table->area), GDK_EXPOSURE_MASK |
| 678 |
| GDK_LEAVE_NOTIFY_MASK |
| 679 |
| GDK_BUTTON_PRESS_MASK |
| 680 |
| GDK_BUTTON_RELEASE_MASK |
| 681 |
| GDK_POINTER_MOTION_MASK |
| 682 |
| GDK_KEY_PRESS_MASK |
| 683 |
| GDK_KEY_RELEASE_MASK); |
| 684 |
|
| 685 |
gtk_button_set_relief (GTK_BUTTON (table), GTK_RELIEF_NORMAL); |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
GType |
| 690 |
abi_table_get_type (void) |
| 691 |
{ |
| 692 |
static GType type = 0; |
| 693 |
|
| 694 |
if (!type) |
| 695 |
{ |
| 696 |
static const GTypeInfo info = |
| 697 |
{ |
| 698 |
sizeof (AbiTableClass), |
| 699 |
NULL, /* base_init */ |
| 700 |
NULL, /* base_finalize */ |
| 701 |
reinterpret_cast<GClassInitFunc>(abi_table_class_init), |
| 702 |
NULL, /* class_finalize */ |
| 703 |
NULL, /* class_data */ |
| 704 |
sizeof (AbiTable), |
| 705 |
0, /* n_preallocs */ |
| 706 |
reinterpret_cast<GInstanceInitFunc>(abi_table_init), |
| 707 |
NULL |
| 708 |
}; |
| 709 |
|
| 710 |
type = g_type_register_static (GTK_TYPE_BUTTON, "AbiTable", &info, static_cast<GTypeFlags>(0)); |
| 711 |
} |
| 712 |
|
| 713 |
return type; |
| 714 |
} |
| 715 |
|
| 716 |
GtkWidget* |
| 717 |
abi_table_new (void) |
| 718 |
{ |
| 719 |
UT_DEBUGMSG(("COnstructing ABITABLE widget \n")); |
| 720 |
return GTK_WIDGET (g_object_new (abi_table_get_type (), NULL)); |
| 721 |
} |
| 722 |
|
| 723 |
/** |
| 724 |
* abi_table_get_label: |
| 725 |
* @abi_table: #AbiTable |
| 726 |
* |
| 727 |
* Returns: (transfer none): the label widget. |
| 728 |
*/ |
| 729 |
GtkWidget* abi_table_get_label(AbiTable* abi_table) |
| 730 |
{ |
| 731 |
return abi_table->label; |
| 732 |
} |