If it is not possible to load the window state (for example, it is
the first time that the application runs), set a default value for
the sidebar width.

This fixes the following bug which causes the sidebar to extend to
the whole window (sidebar_width = 0), thus hiding the main panel:

https://bugzilla.gnome.org/show_bug.cgi?id=788621

Signed-off-by: Guido Trentalancia <guido@trentalancia.com>
---
 src/gdict-window.c |   37 ++++++++++++++++++++++++++-----------
 1 file changed, 26 insertions(+), 11 deletions(-)

--- a/src/gdict-window.c	2017-09-12 10:08:25.000000000 +0200
+++ b/src/gdict-window.c	2017-10-07 03:50:36.903804017 +0200
@@ -1294,15 +1294,21 @@ gdict_window_size_allocate (GtkWidget
 			    GtkAllocation *allocation)
 {
   GdictWindow *window = GDICT_WINDOW (widget);
+  gint current_width;
 
   if (GTK_WIDGET_CLASS (gdict_window_parent_class)->size_allocate != NULL)
     GTK_WIDGET_CLASS (gdict_window_parent_class)->size_allocate (widget, allocation);
 
   if (!window->is_maximized)
     {
-      gtk_window_get_size (GTK_WINDOW (widget),
+      current_width = window->current_width;
+
+      gtk_window_get_size (GTK_WINDOW (window),
                            &window->current_width,
                            &window->current_height);
+
+      if (current_width > window->current_width)
+        gtk_paned_set_position (GTK_PANED (window->main_pane), window->current_width - window->sidebar_width);
     }
 }
 
@@ -1312,14 +1318,18 @@ gdict_window_handle_notify_position_cb (
 					gpointer    user_data)
 {
   GdictWindow *window = GDICT_WINDOW (user_data);
-  gint window_width, pos;
-  GtkAllocation allocation;
+  gint pos;
 
-  pos = gtk_paned_get_position (GTK_PANED (widget));
-  gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
-  window_width = allocation.width;
+  pos = gtk_paned_get_position (GTK_PANED (window->main_pane));
+
+  if (!window->is_maximized)
+    {
+      gtk_window_get_size (GTK_WINDOW (window),
+                           &window->current_width,
+                           &window->current_height);
 
-  window->sidebar_width = window_width - pos;
+      window->sidebar_width = window->current_width - pos;
+    }
 }
 
 static void
@@ -1330,7 +1340,6 @@ gdict_window_constructed (GObject *gobje
   GtkWidget *button;
   PangoFontDescription *font_desc;
   gchar *font_name;
-  GtkAllocation allocation;
   GMenu *menu;
   
   window = GDICT_WINDOW (gobject);
@@ -1496,7 +1505,13 @@ gdict_window_constructed (GObject *gobje
     }
 
   pango_font_description_free (font_desc);
-  
+
+  // Sanitize the sidebar width (e.g. load saved window state failed)
+  if (window->sidebar_width <= 0)
+    {
+      window->sidebar_width = window->default_width / 3;
+    }
+
   gtk_window_set_title (GTK_WINDOW (window), _("Dictionary"));
   gtk_window_set_default_size (GTK_WINDOW (window),
                                window->default_width,
@@ -1504,8 +1519,6 @@ gdict_window_constructed (GObject *gobje
   if (window->is_maximized)
     gtk_window_maximize (GTK_WINDOW (window));
 
-  gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
-  gtk_paned_set_position (GTK_PANED (window->main_pane), allocation.width - window->sidebar_width);
   if (window->sidebar_page != NULL)
     gdict_sidebar_view_page (GDICT_SIDEBAR (window->sidebar), window->sidebar_page);
   else
@@ -1665,6 +1678,8 @@ gdict_window_init (GdictWindow *window)
 
   window->default_width = -1;
   window->default_height = -1;
+  window->current_width = -1;
+  window->current_height = -1;
   window->is_maximized = FALSE;
   window->sidebar_visible = FALSE;
   window->sidebar_page = NULL;
