summaryrefslogtreecommitdiffstats
path: root/window.c
blob: 5dcbf205cd45dbe25c5ba1075c0bb8bdc761c313 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "window.h"
#include "ui.h"
#include "draw.h"
#include <gtk/gtk.h>


static GtkWidget *drawingArea = NULL;
static GtkAdjustment *hAdjustment = NULL, *vAdjustment = NULL;


static gboolean delete_event(GtkWidget *widget, GdkEvent  *event, gpointer data) {
  return FALSE;
}

static void destroy(GtkWidget *widget, gpointer data) {
  gtk_main_quit();
}

static void refreshScrolling() {
  if(getImageWidth() < drawingArea->allocation.width)
    setXTranslate((getImageWidth()-drawingArea->allocation.width)/2);
  else
    setXTranslate(gtk_adjustment_get_value(hAdjustment));
  
  if(getImageHeight() < drawingArea->allocation.height)
    setYTranslate((getImageHeight()-drawingArea->allocation.height)/2);
  else 
    setYTranslate(gtk_adjustment_get_value(vAdjustment));
  
  gdk_window_invalidate_rect(drawingArea->window, NULL, FALSE);
}

static void updateScrollbars() {
  const gdouble imageWidth = getImageWidth(), imageHeight = getImageHeight();
  const gdouble width = drawingArea->allocation.width, height = drawingArea->allocation.height;
  gdouble upper, pageSize, value;
  
  g_object_get(G_OBJECT(hAdjustment), "upper", &upper, "page_size", &pageSize, NULL);
  g_object_set(G_OBJECT(hAdjustment), "upper", imageWidth, "page_size", width, NULL);
  gtk_adjustment_changed(hAdjustment);
  
  if(pageSize > upper && width < imageWidth)
    value = (imageWidth-width)/2;
  else
    value = MAX(MIN(gtk_adjustment_get_value(hAdjustment) - (width-pageSize)/2 + (imageWidth-upper)/2, imageWidth-width), 0);
  gtk_adjustment_set_value(hAdjustment, value);
  
  g_object_get(G_OBJECT(vAdjustment), "upper", &upper, "page_size", &pageSize, NULL);
  g_object_set(G_OBJECT(vAdjustment), "upper", imageHeight, "page_size", height, NULL);
  
  if(pageSize > upper && height < imageHeight)
    value = (imageHeight-height)/2;
  else
    value = MAX(MIN(gtk_adjustment_get_value(vAdjustment) - (height-pageSize)/2 + (imageHeight-upper)/2, imageHeight-height), 0);
  gtk_adjustment_set_value(vAdjustment, value);
  
  refreshScrolling();
}

GtkWidget* createMainWindow() {
  GtkWidget *window, *vbox, *menubar, *toolbar, *table, *vScroll, *hScroll;
  GdkColor color = {0, 0, 0, 0};
  
  
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size(GTK_WINDOW(window), 640, 480);
  g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(delete_event), NULL);
  g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
  
  vbox = gtk_vbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(window), vbox);
  
  menubar = getMenu();
  gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
  gtk_widget_show(menubar);
  
  toolbar = getToolbar();
  gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 0);
  gtk_widget_show(toolbar);
  
  gtk_window_add_accel_group(GTK_WINDOW(window), getAccels());
  
  table = gtk_table_new(2, 2, FALSE);
  gtk_box_pack_end(GTK_BOX(vbox), table, TRUE, TRUE, 0);
  
  drawingArea = gtk_drawing_area_new();
  gtk_widget_modify_bg(drawingArea, GTK_WIDGET_STATE(drawingArea), &color);
  g_signal_connect(G_OBJECT(drawingArea), "configure-event", G_CALLBACK(updateScrollbars), NULL);
  g_signal_connect(G_OBJECT(drawingArea), "expose-event", G_CALLBACK(drawTopView), NULL);
  gtk_table_attach(GTK_TABLE(table), drawingArea, 0, 1, 0, 1, GTK_FILL|GTK_EXPAND|GTK_SHRINK, GTK_FILL|GTK_EXPAND|GTK_SHRINK, 0, 0);
  gtk_widget_show(drawingArea);
  
  hAdjustment = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, 0, 10, 100, 100));
  hScroll = gtk_hscrollbar_new(hAdjustment);
  g_signal_connect(G_OBJECT(hScroll), "value-changed", G_CALLBACK(refreshScrolling), NULL);
  gtk_table_attach(GTK_TABLE(table), hScroll, 0, 1, 1, 2, GTK_FILL|GTK_EXPAND|GTK_SHRINK, 0, 0, 0);
  gtk_widget_show(hScroll);
  
  vAdjustment = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, 0, 10, 100, 100));
  vScroll = gtk_vscrollbar_new(vAdjustment);
  g_signal_connect(G_OBJECT(vScroll), "value-changed", G_CALLBACK(refreshScrolling), NULL);
  gtk_table_attach(GTK_TABLE(table), vScroll, 1, 2, 0, 1, 0, GTK_FILL|GTK_EXPAND|GTK_SHRINK, 0, 0);
  gtk_widget_show(vScroll);
  
  gtk_widget_show(table);
  gtk_widget_show(vbox);
  
  return window;
}

void zoomIn() {
  setScale(getScale()*1.5);
  updateScrollbars();
}

void zoomOut() {
  setScale(getScale()/1.5);
  updateScrollbars();
}