diff --git a/src/gtk_ui.c b/src/gtk_ui.c index e5717c4..04cbf77 100644 --- a/src/gtk_ui.c +++ b/src/gtk_ui.c @@ -1,38 +1,64 @@ -#include "view.h" #define _GNU_SOURCE -#include "gtk_ui.h" +#include +#include +#include +#include +#undef MIN +#undef MAX -#include "misc/misc.h" -#define UNUSED(x) (void)(x) #include #include +#include "gtk_ui.h" +#include "view.h" + +#include "misc/misc.h" + #define STRINGIFY(x) # x const char* viewer_gui = #include "viewer.inc" ; -void ui_trigger_redraw(struct view_s* v) -{ - gtk_widget_queue_draw(v->ui->gtk_drawingarea); -} - -void ui_rgbbuffer_disconnect(struct view_s* v) -{ - if (NULL != v->ui->source) - cairo_surface_destroy(v->ui->source); -} - -void ui_rgbbuffer_connect(struct view_s* v, int rgbw, int rgbh, int rgbstr, unsigned char *buf) -{ - gtk_widget_set_size_request(v->ui->gtk_drawingarea, rgbw, rgbh); - v->ui->source = cairo_image_surface_create_for_data(buf, CAIRO_FORMAT_RGB24, rgbw, rgbh, rgbstr); -} -extern gboolean fit_callback(GtkWidget *widget, gpointer data) +struct view_gtk_ui_s { + + // UI + cairo_surface_t* source; + + // widgets + GtkComboBox* gtk_mode; + GtkComboBox* gtk_flip; + GtkComboBox* gtk_interp; + GtkWidget* gtk_drawingarea; + GtkWidget* gtk_viewport; + GtkAdjustment* gtk_winlow; + GtkAdjustment* gtk_winhigh; + GtkSpinButton* gtk_button_winlow; + GtkSpinButton* gtk_button_winhigh; + GtkToolItem* toolbar_scale1; + GtkToolItem* toolbar_scale2; + GtkToolItem* toolbar_button1; + GtkToolItem* toolbar_button2; + GtkAdjustment* gtk_zoom; + GtkAdjustment* gtk_aniso; + GtkEntry* gtk_entry; + GtkToggleToolButton* gtk_transpose; + GtkToggleToolButton* gtk_fit; + GtkToggleToolButton* gtk_sync; + GtkToggleToolButton* gtk_absolutewindowing; + + GtkAdjustment* gtk_posall[DIMS]; + GtkCheckButton* gtk_checkall[DIMS]; + + GtkWidget *dialog; // Save dialog + GtkFileChooser *chooser; // Save dialog + GtkWindow *window; +}; + + +extern gboolean fit_callback(GtkWidget* /*widget*/, gpointer data) { - UNUSED(widget); struct view_s* v = data; gboolean flag = gtk_toggle_tool_button_get_active(v->ui->gtk_fit); @@ -56,34 +82,11 @@ extern gboolean fit_callback(GtkWidget *widget, gpointer data) return FALSE; } - -extern gboolean configure_callback(GtkWidget *widget, GdkEvent* event, gpointer data) +extern gboolean configure_callback(GtkWidget *widget, GdkEvent* /*event*/, gpointer data) { - UNUSED(event); return fit_callback(widget, data); } -void ui_set_selected_dims(struct view_s* v, const bool* selected) -{ - // Avoid calling this function from itself. - // Toggling the GTK_TOOGLE_BUTTONs below would normally lead to another call of this function. - static bool in_callback = false; - if (in_callback) - return; - - in_callback = true; - - for (int i = 0; i < DIMS; i++) { - - if (selected[i]) - gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(v->ui->gtk_checkall[i]), TRUE); - else - gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(v->ui->gtk_checkall[i]), FALSE); - } - - in_callback = false; -} - extern gboolean geom_callback(GtkWidget* /*widget*/, gpointer data) { struct view_s* v = data; @@ -321,7 +324,46 @@ extern gboolean button_press_event(GtkWidget* /*widget*/, GdkEventButton *event, } -extern void ui_pull_geom(struct view_s* v) + +void ui_trigger_redraw(struct view_s* v) +{ + gtk_widget_queue_draw(v->ui->gtk_drawingarea); +} + +void ui_rgbbuffer_disconnect(struct view_s* v) +{ + if (NULL != v->ui->source) + cairo_surface_destroy(v->ui->source); +} + +void ui_rgbbuffer_connect(struct view_s* v, int rgbw, int rgbh, int rgbstr, unsigned char *buf) +{ + gtk_widget_set_size_request(v->ui->gtk_drawingarea, rgbw, rgbh); + v->ui->source = cairo_image_surface_create_for_data(buf, CAIRO_FORMAT_RGB24, rgbw, rgbh, rgbstr); +} + +void ui_set_selected_dims(struct view_s* v, const bool* selected) +{ + // Avoid calling this function from itself. + // Toggling the GTK_TOOGLE_BUTTONs below would normally lead to another call of this function. + static bool in_callback = false; + if (in_callback) + return; + + in_callback = true; + + for (int i = 0; i < DIMS; i++) { + + if (selected[i]) + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(v->ui->gtk_checkall[i]), TRUE); + else + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(v->ui->gtk_checkall[i]), FALSE); + } + + in_callback = false; +} + +void ui_pull_geom(struct view_s* v) { geom_callback(NULL, v); } @@ -348,13 +390,16 @@ void ui_set_mode(struct view_s* v) gtk_combo_box_set_active(v->ui->gtk_mode, v->settings.mode); } -extern void ui_set_msg(struct view_s* v, const char* msg) +void ui_set_msg(struct view_s* v, const char* msg) { gtk_entry_set_text(v->ui->gtk_entry, msg); } -extern void ui_window_new(struct view_s* v, int N, const long dims[N]) +void ui_window_new(struct view_s* v, int N, const long dims[N]) { + v->ui = xmalloc(sizeof(struct view_gtk_ui_s)); + v->ui->source = NULL; + GtkBuilder* builder = gtk_builder_new(); gtk_builder_add_from_string(builder, viewer_gui, -1, NULL); @@ -421,6 +466,18 @@ extern void ui_window_new(struct view_s* v, int N, const long dims[N]) gtk_widget_show(GTK_WIDGET(window)); } +void ui_init(int* argc_p, char** argv_p[]) +{ + gtk_disable_setlocale(); + gtk_init(argc_p, argv_p); + +} + +void ui_main() +{ + gtk_main(); +} + void ui_loop_quit() { gtk_main_quit(); @@ -448,7 +505,7 @@ void ui_set_windowing(struct view_s* v, double max, double inc, int digits) gtk_widget_set_visible(GTK_WIDGET(v->ui->toolbar_button2), v->settings.absolute_windowing ? TRUE : FALSE); } -extern void ui_set_position(struct view_s* v, unsigned int dim, unsigned int p) +void ui_set_position(struct view_s* v, unsigned int dim, unsigned int p) { v->settings.pos[dim] = p; diff --git a/src/gtk_ui.h b/src/gtk_ui.h index 95a5ec2..c19a1ad 100644 --- a/src/gtk_ui.h +++ b/src/gtk_ui.h @@ -1,72 +1,36 @@ -#include -#include -#include -#include - -#include "view.h" - -#undef MIN -#undef MAX - -#ifndef DIMS -#define DIMS 16 -#endif - -struct view_gtk_ui_s { - - // UI - cairo_surface_t* source; - - // widgets - GtkComboBox* gtk_mode; - GtkComboBox* gtk_flip; - GtkComboBox* gtk_interp; - GtkWidget* gtk_drawingarea; - GtkWidget* gtk_viewport; - GtkAdjustment* gtk_winlow; - GtkAdjustment* gtk_winhigh; - GtkSpinButton* gtk_button_winlow; - GtkSpinButton* gtk_button_winhigh; - GtkToolItem* toolbar_scale1; - GtkToolItem* toolbar_scale2; - GtkToolItem* toolbar_button1; - GtkToolItem* toolbar_button2; - GtkAdjustment* gtk_zoom; - GtkAdjustment* gtk_aniso; - GtkEntry* gtk_entry; - GtkToggleToolButton* gtk_transpose; - GtkToggleToolButton* gtk_fit; - GtkToggleToolButton* gtk_sync; - GtkToggleToolButton* gtk_absolutewindowing; - - GtkAdjustment* gtk_posall[DIMS]; - GtkCheckButton* gtk_checkall[DIMS]; - - GtkWidget *dialog; // Save dialog - GtkFileChooser *chooser; // Save dialog - GtkWindow *window; -}; +#include + +struct view_s; -extern void ui_trigger_redraw(struct view_s* v); extern void ui_rgbbuffer_disconnect(struct view_s* v); extern void ui_rgbbuffer_connect(struct view_s* v, int rgbw, int rgbh, int rgbstr, unsigned char* buf); extern void ui_set_selected_dims(struct view_s* v, const bool* selected); + +extern void ui_set_position(struct view_s* v, unsigned int dim, unsigned int p); + extern void ui_set_limits(struct view_s* v); extern void ui_set_values(struct view_s* v); -extern void ui_set_mode(struct view_s* v); +extern void ui_set_windowing(struct view_s* v, double max, double inc, int digits); -extern void ui_pull_geom(struct view_s* v); -extern void ui_pull_window(struct view_s* v); +extern void ui_set_mode(struct view_s* v); extern void ui_set_msg(struct view_s* v, const char* msg); + extern void ui_window_new(struct view_s* v, int N, const long dims[N]); +extern void ui_main(); +extern void ui_init(int* argc_p, char** argv_p[]); extern void ui_loop_quit(); + +extern void ui_trigger_redraw(struct view_s* v); + +extern void ui_pull_geom(struct view_s* v); +extern void ui_pull_window(struct view_s* v); + + extern bool gtk_ui_save_png(struct view_s* v, const char* filename); -extern void ui_set_windowing(struct view_s* v, double max, double inc, int digits); -extern void ui_set_position(struct view_s* v, unsigned int dim, unsigned int p); diff --git a/src/main.c b/src/main.c index 1e241ff..7bd408a 100644 --- a/src/main.c +++ b/src/main.c @@ -6,11 +6,6 @@ #include #include -#include - -#undef MAX -#undef MIN - #include "misc/misc.h" #include "misc/mmio.h" #include "misc/opts.h" @@ -22,6 +17,7 @@ extern void io_unregister(const char* name); #endif #include "view.h" +#include "gtk_ui.h" @@ -30,8 +26,7 @@ static const char help_str[] = "View images."; int main(int argc, char* argv[argc]) { - gtk_disable_setlocale(); - gtk_init(&argc, &argv); + ui_init(&argc, &argv); long count; const char** in_files; @@ -94,7 +89,7 @@ int main(int argc, char* argv[argc]) } } - gtk_main(); + ui_main(); return 0; } diff --git a/src/view.c b/src/view.c index c09cf9a..6f3592f 100644 --- a/src/view.c +++ b/src/view.c @@ -4,12 +4,11 @@ */ #define _GNU_SOURCE -#include #include #include #include -#include +#include #include "gtk_ui.h" @@ -87,7 +86,7 @@ static void add_text(cairo_surface_t* surface, int x, int y, int size, const cha } #endif -extern void update_geom(struct view_s* v) +void update_geom(struct view_s* v) { if (NULL == v->control->geom) return; @@ -122,15 +121,15 @@ void view_sync_windowing(struct view_s* v) } } -extern void view_refresh(struct view_s* v) +void view_refresh(struct view_s* v) { if (v->settings.absolute_windowing) { long idims[DIMS]; md_select_dims(DIMS, MD_BIT(v->settings.xdim) | MD_BIT(v->settings.ydim), idims, v->control->dims); - + complex float* tmp = md_alloc(DIMS, idims, sizeof(complex float)); - + long pos[DIMS]; md_copy_dims(DIMS, pos, v->settings.pos); pos[v->settings.xdim] = 0; @@ -176,7 +175,7 @@ extern void view_refresh(struct view_s* v) for (long j = 0; j < size; j++) if (max < cabsf(v->control->data[j])) max = cabsf(v->control->data[j]); - + max = MIN(1.e10, max); if (0. == max) @@ -189,7 +188,7 @@ extern void view_refresh(struct view_s* v) } -extern void view_add_geometry(struct view_s* v, unsigned long flags, const float (*geom)[3][3]) +void view_add_geometry(struct view_s* v, unsigned long flags, const float (*geom)[3][3]) { v->control->geom_flags = flags; v->control->geom = geom; @@ -295,7 +294,7 @@ bool view_save_png(struct view_s* v, const char *filename) } -extern bool view_save_pngmovie(struct view_s* v, const char *folder) +bool view_save_pngmovie(struct view_s* v, const char *folder) { int frame_dim = 10; @@ -484,7 +483,7 @@ struct view_s* create_view(const char* name, const long pos[DIMS], const long di struct view_s* v = xmalloc(sizeof(struct view_s)); v->control = xmalloc(sizeof(struct view_control_s)); - v->ui = xmalloc(sizeof(struct view_gtk_ui_s)); + v->ui = NULL; v->next = v->prev = v; v->sync = true; @@ -508,7 +507,6 @@ struct view_s* create_view(const char* name, const long pos[DIMS], const long di v->settings.xzoom = 2.; v->settings.yzoom = 2.; - v->ui->source = NULL; v->control->rgb = NULL; v->control->buf = NULL; @@ -563,7 +561,7 @@ void view_toggle_absolute_windowing(struct view_s* v) long size = md_calc_size(DIMS, v->control->dims); double max = 0.; - + for (long j = 0; j < size; j++) if (max < cabsf(v->control->data[j])) max = cabsf(v->control->data[j]); @@ -648,7 +646,7 @@ void view_windowing_release(struct view_s* v) static int nr_windows = 0; -extern void view_window_close(struct view_s* v) +void view_window_close(struct view_s* v) { delete_view(v); @@ -659,7 +657,7 @@ extern void view_window_close(struct view_s* v) -extern struct view_s* window_new(const char* name, const long pos[DIMS], const long dims[DIMS], const complex float* x, bool absolute_windowing) +struct view_s* window_new(const char* name, const long pos[DIMS], const long dims[DIMS], const complex float* x, bool absolute_windowing) { struct view_s* v = create_view(name, pos, dims, x); v->settings.absolute_windowing = absolute_windowing; diff --git a/src/view.h b/src/view.h index 1176714..a27541d 100644 --- a/src/view.h +++ b/src/view.h @@ -69,33 +69,42 @@ struct view_s { }; +// setup etc extern struct view_s* window_new(const char* name, const long pos[DIMS], const long dims[DIMS], const _Complex float* x, _Bool absolute_windowing); -extern void view_add_geometry(struct view_s* v, unsigned long flags, const float (*geom)[3][3]); extern void window_connect_sync(struct view_s* a, struct view_s* b); -extern struct view_s* view_clone(struct view_s* v, const long pos[DIMS]); -extern const long *view_get_dims(struct view_s* v); -extern void view_set_geom(struct view_s* v, struct view_ui_geom_params_s gp); -extern void view_refresh(struct view_s* v); -extern void view_redraw(struct view_s* v); +// usually callbacks: +extern struct view_s* view_clone(struct view_s* v, const long pos[DIMS]); -extern void view_touch_rgb_settings(struct view_s* v); +extern void view_add_geometry(struct view_s* v, unsigned long flags, const float (*geom)[3][3]); -extern void view_toggle_absolute_windowing(struct view_s* v); +extern void view_windowing_move(struct view_s* v, int x, int y, double inc_low, double inc_high); +extern void view_windowing_release(struct view_s* v); -extern char *construct_filename_view2(struct view_s* v); +extern void view_click(struct view_s* v, int x, int y, int button); extern bool view_save_png(struct view_s* v, const char *filename); extern bool view_save_pngmovie(struct view_s* v, const char *folder); -extern void view_windowing_move(struct view_s* v, int x, int y, double inc_low, double inc_high); -extern void view_windowing_release(struct view_s* v); - -extern void view_click(struct view_s* v, int x, int y, int button); +extern void view_toggle_absolute_windowing(struct view_s* v); extern void view_window_close(struct view_s* v); +// +extern void view_set_geom(struct view_s* v, struct view_ui_geom_params_s gp); + +extern void view_refresh(struct view_s* v); +extern void view_redraw(struct view_s* v); +extern void view_touch_rgb_settings(struct view_s* v); + + + +// helpers +extern const long *view_get_dims(struct view_s* v); + +extern char *construct_filename_view2(struct view_s* v); + #endif