]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/slider.cpp
No changes, just use wxGtkObject<> instead of g_object_unref().
[wxWidgets.git] / src / gtk / slider.cpp
index c2ba63e207eebe9e1ea0d3bf37fcf68e4912b1b4..83deaec5f310f56f656a2fc1a17619e8958d4141 100644 (file)
 
 #ifndef WX_PRECOMP
     #include "wx/utils.h"
+    #include "wx/math.h"
 #endif
 
-#include "wx/gtk/private.h"
+#include <gtk/gtk.h>
 
 //-----------------------------------------------------------------------------
 // data
@@ -45,7 +46,7 @@ ProcessScrollEvent(wxSlider *win, wxEventType evtType)
     {
         wxScrollEvent event( evtType, win->GetId(), value, orient );
         event.SetEventObject( win );
-        win->GetEventHandler()->ProcessEvent( event );
+        win->HandleWindowEvent( event );
     }
 
     // but, in any case, except if we're dragging the slider (and so the change
@@ -54,14 +55,14 @@ ProcessScrollEvent(wxSlider *win, wxEventType evtType)
     {
         wxScrollEvent event(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
         event.SetEventObject( win );
-        win->GetEventHandler()->ProcessEvent( event );
+        win->HandleWindowEvent( event );
     }
 
     // and also generate a command event for compatibility
     wxCommandEvent event( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
     event.SetEventObject( win );
     event.SetInt( value );
-    win->GetEventHandler()->ProcessEvent( event );
+    win->HandleWindowEvent( event );
 }
 
 static inline wxEventType GtkScrollTypeToWx(int scrollType)
@@ -99,7 +100,7 @@ static inline wxEventType GtkScrollTypeToWx(int scrollType)
         eventType = wxEVT_SCROLL_THUMBTRACK;
         break;
     default:
-        wxFAIL_MSG(_T("Unknown GtkScrollType"));
+        wxFAIL_MSG(wxT("Unknown GtkScrollType"));
         eventType = wxEVT_NULL;
         break;
     }
@@ -123,16 +124,15 @@ extern "C" {
 static void
 gtk_value_changed(GtkRange* range, wxSlider* win)
 {
-    if (g_isIdle) wxapp_install_idle_handler();
-
-    if (!win->m_hasVMT) return;
-    if (g_blockEventsOnDrag) return;
-
-    GtkAdjustment* adj = range->adjustment;
-    const int pos = int(round(adj->value));
+    GtkAdjustment* adj = gtk_range_get_adjustment (range);
+    const int pos = wxRound(adj->value);
     const double oldPos = win->m_pos;
     win->m_pos = adj->value;
-    if (win->m_blockScrollEvent)
+
+    if (!win->m_hasVMT || g_blockEventsOnDrag)
+        return;
+
+    if (win->GTKEventsDisabled())
     {
         win->m_scrollEventType = GTK_SCROLL_NONE;
         return;
@@ -178,9 +178,8 @@ gtk_value_changed(GtkRange* range, wxSlider* win)
     win->m_scrollEventType = GTK_SCROLL_NONE;
 
     // If integral position has changed
-    if (int(round(oldPos)) != pos)
+    if (wxRound(oldPos) != pos)
     {
-        wxCHECK_RET(eventType != wxEVT_NULL, _T("Unknown slider scroll event type"));
         ProcessScrollEvent(win, eventType);
         win->m_needThumbRelease = eventType == wxEVT_SCROLL_THUMBTRACK;
     }
@@ -214,25 +213,28 @@ gtk_button_press_event(GtkWidget*, GdkEventButton*, wxSlider* win)
 }
 }
 
-// Single shot idle callback, to generate thumb release event, and
-//  truncate position to integral value.  Idle callback is used
-//  because position cannot be changed from button release event.
+//-----------------------------------------------------------------------------
+// "event_after"
+//-----------------------------------------------------------------------------
+
 extern "C" {
-static gboolean
-idle_thumbrelease(void* data)
+static void
+gtk_event_after(GtkRange* range, GdkEvent* event, wxSlider* win)
 {
-    wxSlider* win = (wxSlider*)data;
-    win->m_isScrolling = false;
-    if (win->m_needThumbRelease)
+    if (event->type == GDK_BUTTON_RELEASE)
     {
-        win->m_needThumbRelease = false;
-        ProcessScrollEvent(win, wxEVT_SCROLL_THUMBRELEASE);
+        g_signal_handlers_block_by_func(range, (gpointer) gtk_event_after, win);
+
+        if (win->m_needThumbRelease)
+        {
+            win->m_needThumbRelease = false;
+            ProcessScrollEvent(win, wxEVT_SCROLL_THUMBRELEASE);
+        }
+        // Keep slider at an integral position
+        win->GTKDisableEvents();
+        gtk_range_set_value(GTK_RANGE (win->m_scale), win->GetValue());
+        win->GTKEnableEvents();
     }
-    // Keep slider at an integral position
-    win->BlockScrollEvent();
-    gtk_range_set_value((GtkRange*)win->m_widget, win->GetValue());
-    win->UnblockScrollEvent();
-    return false;
 }
 }
 
@@ -242,12 +244,13 @@ idle_thumbrelease(void* data)
 
 extern "C" {
 static gboolean
-gtk_button_release_event(GtkWidget*, GdkEventButton*, wxSlider* win)
+gtk_button_release_event(GtkRange* range, GdkEventButton*, wxSlider* win)
 {
     win->m_mouseButtonDown = false;
     if (win->m_isScrolling)
     {
-        g_idle_add(idle_thumbrelease, win);
+        win->m_isScrolling = false;
+        g_signal_handlers_unblock_by_func(range, (gpointer) gtk_event_after, win);
     }
     return false;
 }
@@ -261,7 +264,7 @@ extern "C" {
 static gchar* gtk_format_value(GtkScale*, double value, void*)
 {
     // Format value as nearest integer
-    return g_strdup_printf("%d", int(round(value)));
+    return g_strdup_printf("%d", wxRound(value));
 }
 }
 
@@ -274,17 +277,24 @@ IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
 wxSlider::wxSlider()
 {
     m_pos = 0;
-    m_scrollEventType = 0;
+    m_scrollEventType = GTK_SCROLL_NONE;
     m_needThumbRelease = false;
+    m_blockScrollEvent = false;
 }
 
-bool wxSlider::Create(wxWindow *parent, wxWindowID id,
-        int value, int minValue, int maxValue,
-        const wxPoint& pos, const wxSize& size,
-        long style, const wxValidator& validator, const wxString& name )
+bool wxSlider::Create(wxWindow *parent,
+                      wxWindowID id,
+                      int value,
+                      int minValue,
+                      int maxValue,
+                      const wxPoint& pos,
+                      const wxSize& size,
+                      long style,
+                      const wxValidator& validator,
+                      const wxString& name)
 {
-    m_acceptsFocus = true;
-    m_needParent = true;
+    m_pos = value;
+    m_scrollEventType = GTK_SCROLL_NONE;
 
     if (!PreCreation( parent, pos, size ) ||
         !CreateBase( parent, id, pos, size, style, validator, name ))
@@ -293,30 +303,105 @@ bool wxSlider::Create(wxWindow *parent, wxWindowID id,
         return false;
     }
 
-    m_pos = 0;
-    m_scrollEventType = 0;
-    m_needThumbRelease = false;
 
     if (style & wxSL_VERTICAL)
-        m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
+        m_scale = gtk_vscale_new( NULL );
+    else
+        m_scale = gtk_hscale_new( NULL );
+    g_object_ref(m_scale);
+
+    if (style & wxSL_MIN_MAX_LABELS)
+    {
+        gtk_widget_show( m_scale );
+
+        if (style & wxSL_VERTICAL)
+            m_widget = gtk_hbox_new(false, 0);
+        else
+            m_widget = gtk_vbox_new(false, 0);
+        g_object_ref(m_widget);
+        gtk_widget_show( m_widget );
+        gtk_container_add( GTK_CONTAINER(m_widget), m_scale );
+
+        GtkWidget *box;
+        if (style & wxSL_VERTICAL)
+            box = gtk_vbox_new(false,0);
+        else
+            box = gtk_hbox_new(false,0);
+        g_object_ref(box);
+        gtk_widget_show(box);
+        gtk_container_add( GTK_CONTAINER(m_widget), box );
+
+        m_minLabel = gtk_label_new(NULL);
+        g_object_ref(m_minLabel);
+        gtk_widget_show( m_minLabel );
+        gtk_container_add( GTK_CONTAINER(box), m_minLabel );
+        gtk_box_set_child_packing( GTK_BOX(box), m_minLabel, FALSE, FALSE, 0, GTK_PACK_START );
+
+        // expanding empty space between the min/max labels
+        GtkWidget *space = gtk_label_new(NULL);
+        g_object_ref(space);
+        gtk_widget_show( space );
+        gtk_container_add( GTK_CONTAINER(box), space );
+        gtk_box_set_child_packing( GTK_BOX(box), space, TRUE, FALSE, 0, GTK_PACK_START );
+
+        m_maxLabel = gtk_label_new(NULL);
+        g_object_ref(m_maxLabel);
+        gtk_widget_show( m_maxLabel );
+        gtk_container_add( GTK_CONTAINER(box), m_maxLabel );
+        gtk_box_set_child_packing( GTK_BOX(box), m_maxLabel, FALSE, FALSE, 0, GTK_PACK_END );
+    }
     else
-        m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
+    {
+        m_widget = m_scale;
+        m_maxLabel = NULL;
+        m_minLabel = NULL;
+    }
+
+    const bool showValueLabel = (style & wxSL_VALUE_LABEL) != 0;
+    gtk_scale_set_draw_value(GTK_SCALE (m_scale), showValueLabel );
+    if ( showValueLabel )
+    {
+        // position the label appropriately: notice that wxSL_DIRECTION flags
+        // specify the position of the ticks, not label, under MSW and so the
+        // label is on the opposite side
+        GtkPositionType posLabel;
+        if ( style & wxSL_VERTICAL )
+        {
+            if ( style & wxSL_LEFT )
+                posLabel = GTK_POS_RIGHT;
+            else // if ( style & wxSL_RIGHT ) -- this is also the default
+                posLabel = GTK_POS_LEFT;
+        }
+        else // horizontal slider
+        {
+            if ( style & wxSL_TOP )
+                posLabel = GTK_POS_BOTTOM;
+            else // if ( style & wxSL_BOTTOM) -- this is again the default
+                posLabel = GTK_POS_TOP;
+        }
+
+        gtk_scale_set_value_pos( GTK_SCALE(m_scale), posLabel );
+    }
 
-    gtk_scale_set_draw_value((GtkScale*)m_widget, (style & wxSL_LABELS) != 0);
     // Keep full precision in position value
-    gtk_scale_set_digits((GtkScale*)m_widget, -1);
+    gtk_scale_set_digits(GTK_SCALE (m_scale), -1);
 
     if (style & wxSL_INVERSE)
-        gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE );
+        gtk_range_set_inverted( GTK_RANGE(m_scale), TRUE );
 
-    g_signal_connect(m_widget, "button_press_event", G_CALLBACK(gtk_button_press_event), this);
-    g_signal_connect(m_widget, "button_release_event", G_CALLBACK(gtk_button_release_event), this);
-    g_signal_connect(m_widget, "move_slider", G_CALLBACK(gtk_move_slider), this);
-    g_signal_connect(m_widget, "format_value", G_CALLBACK(gtk_format_value), NULL);
-    g_signal_connect(m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
+    g_signal_connect(m_scale, "button_press_event", G_CALLBACK(gtk_button_press_event), this);
+    g_signal_connect(m_scale, "button_release_event", G_CALLBACK(gtk_button_release_event), this);
+    g_signal_connect(m_scale, "move_slider", G_CALLBACK(gtk_move_slider), this);
+    g_signal_connect(m_scale, "format_value", G_CALLBACK(gtk_format_value), NULL);
+    g_signal_connect(m_scale, "value_changed", G_CALLBACK(gtk_value_changed), this);
+    gulong handler_id = g_signal_connect(m_scale, "event_after", G_CALLBACK(gtk_event_after), this);
+    g_signal_handler_block(m_scale, handler_id);
 
     SetRange( minValue, maxValue );
-    SetValue( value );
+
+    // don't call the public SetValue() as it won't do anything unless the
+    // value really changed
+    GTKSetValue( value );
 
     m_parent->DoAddChild( this );
 
@@ -325,49 +410,87 @@ bool wxSlider::Create(wxWindow *parent, wxWindowID id,
     return true;
 }
 
+void wxSlider::GTKDisableEvents()
+{
+    m_blockScrollEvent = true;
+}
+
+void wxSlider::GTKEnableEvents()
+{
+    m_blockScrollEvent = false;
+}
+
+bool wxSlider::GTKEventsDisabled() const
+{
+   return m_blockScrollEvent;
+}
+
 int wxSlider::GetValue() const
 {
-    return int(round(m_pos));
+    return wxRound(m_pos);
 }
 
 void wxSlider::SetValue( int value )
 {
     if (GetValue() != value)
-    {
-        BlockScrollEvent();
-        gtk_range_set_value((GtkRange*)m_widget, value);
-        UnblockScrollEvent();
-    }
+        GTKSetValue(value);
+}
+
+void wxSlider::GTKSetValue(int value)
+{
+    GTKDisableEvents();
+    gtk_range_set_value(GTK_RANGE (m_scale), value);
+    GTKEnableEvents();
 }
 
 void wxSlider::SetRange( int minValue, int maxValue )
 {
-    BlockScrollEvent();
-    gtk_range_set_range((GtkRange*)m_widget, minValue, maxValue);
-    gtk_range_set_increments((GtkRange*)m_widget, 1, (maxValue - minValue + 9) / 10);
-    UnblockScrollEvent();
+    GTKDisableEvents();
+    if (minValue == maxValue)
+       maxValue++;
+    gtk_range_set_range(GTK_RANGE (m_scale), minValue, maxValue);
+    gtk_range_set_increments(GTK_RANGE (m_scale), 1, (maxValue - minValue + 9) / 10);
+    GTKEnableEvents();
+
+    if (HasFlag(wxSL_MIN_MAX_LABELS))
+    {
+        wxString str;
+
+        str.Printf( "%d", minValue );
+        if (HasFlag(wxSL_INVERSE))
+            gtk_label_set_text( GTK_LABEL(m_maxLabel), str.utf8_str() );
+        else
+            gtk_label_set_text( GTK_LABEL(m_minLabel), str.utf8_str() );
+
+        str.Printf( "%d", maxValue );
+        if (HasFlag(wxSL_INVERSE))
+            gtk_label_set_text( GTK_LABEL(m_minLabel), str.utf8_str() );
+        else
+            gtk_label_set_text( GTK_LABEL(m_maxLabel), str.utf8_str() );
+
+    }
 }
 
 int wxSlider::GetMin() const
 {
-    return int(((GtkRange*)m_widget)->adjustment->lower);
+    return int(gtk_range_get_adjustment (GTK_RANGE (m_scale))->lower);
 }
 
 int wxSlider::GetMax() const
 {
-    return int(((GtkRange*)m_widget)->adjustment->upper);
+    return int(gtk_range_get_adjustment (GTK_RANGE (m_scale))->upper);
 }
 
 void wxSlider::SetPageSize( int pageSize )
 {
-    BlockScrollEvent();
-    gtk_range_set_increments((GtkRange*)m_widget, 1, pageSize);
-    UnblockScrollEvent();
+    GTKDisableEvents();
+    gtk_range_set_increments(GTK_RANGE (m_scale), GetLineSize(), pageSize);
+    GTKEnableEvents();
 }
 
 int wxSlider::GetPageSize() const
 {
-    return int(((GtkRange*)m_widget)->adjustment->page_increment);
+    return int(gtk_range_get_adjustment (GTK_RANGE (m_scale))->page_increment);
 }
 
 // GTK does not support changing the size of the slider
@@ -380,19 +503,21 @@ int wxSlider::GetThumbLength() const
     return 0;
 }
 
-void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
+void wxSlider::SetLineSize( int lineSize )
 {
+    GTKDisableEvents();
+    gtk_range_set_increments(GTK_RANGE (m_scale), lineSize, GetPageSize());
+    GTKEnableEvents();
 }
 
 int wxSlider::GetLineSize() const
 {
-    return 0;
+    return int(gtk_range_get_adjustment (GTK_RANGE (m_scale))->step_increment);
 }
 
-bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
+GdkWindow *wxSlider::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
 {
-    GtkRange *range = GTK_RANGE(m_widget);
-    return (range->event_window == window);
+    return GTK_RANGE(m_scale)->event_window;
 }
 
 // static