1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/gtk/gauge.cpp
3 // Purpose:
4 // Author:      Robert Roebling
5 // Copyright:   (c) 1998 Robert Roebling
6 // Licence:     wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8 
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11 
12 #if wxUSE_GAUGE
13 
14 #include "wx/gauge.h"
15 
16 #include "wx/gtk/private/wrapgtk.h"
17 
18 //-----------------------------------------------------------------------------
19 // wxGauge
20 //-----------------------------------------------------------------------------
21 
Create(wxWindow * parent,wxWindowID id,int range,const wxPoint & pos,const wxSize & size,long style,const wxValidator & validator,const wxString & name)22 bool wxGauge::Create( wxWindow *parent,
23                       wxWindowID id,
24                       int range,
25                       const wxPoint& pos,
26                       const wxSize& size,
27                       long style,
28                       const wxValidator& validator,
29                       const wxString& name )
30 {
31     if (!PreCreation( parent, pos, size ) ||
32         !CreateBase( parent, id, pos, size, style, validator, name ))
33     {
34         wxFAIL_MSG( wxT("wxGauge creation failed") );
35         return false;
36     }
37 
38     m_rangeMax = range;
39 
40     m_widget = gtk_progress_bar_new();
41     g_object_ref(m_widget);
42     if ( style & wxGA_VERTICAL )
43     {
44 #ifdef __WXGTK3__
45         gtk_orientable_set_orientation(GTK_ORIENTABLE(m_widget), GTK_ORIENTATION_VERTICAL);
46         gtk_progress_bar_set_inverted(GTK_PROGRESS_BAR(m_widget), true);
47 #else
48         gtk_progress_bar_set_orientation( GTK_PROGRESS_BAR(m_widget),
49                                           GTK_PROGRESS_BOTTOM_TO_TOP );
50 #endif
51     }
52 
53     // when using the gauge in indeterminate mode, we need this:
54     gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR (m_widget), 0.05);
55 
56     m_parent->DoAddChild( this );
57 
58     PostCreation(size);
59     SetInitialSize(size);
60 
61     return true;
62 }
63 
DoSetGauge()64 void wxGauge::DoSetGauge()
65 {
66     wxASSERT_MSG( 0 <= m_gaugePos && m_gaugePos <= m_rangeMax,
67                   wxT("invalid gauge position in DoSetGauge()") );
68 
69     gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (m_widget),
70                                    m_rangeMax ? ((double)m_gaugePos)/m_rangeMax : 0.0);
71 }
72 
SetRange(int range)73 void wxGauge::SetRange( int range )
74 {
75     m_rangeMax = range;
76     if (m_gaugePos > m_rangeMax)
77         m_gaugePos = m_rangeMax;
78 
79     DoSetGauge();
80 }
81 
SetValue(int pos)82 void wxGauge::SetValue( int pos )
83 {
84     wxCHECK_RET( pos <= m_rangeMax, wxT("invalid value in wxGauge::SetValue()") );
85 
86     m_gaugePos = pos;
87 
88     DoSetGauge();
89 }
90 
GetRange() const91 int wxGauge::GetRange() const
92 {
93     return m_rangeMax;
94 }
95 
GetValue() const96 int wxGauge::GetValue() const
97 {
98     return m_gaugePos;
99 }
100 
Pulse()101 void wxGauge::Pulse()
102 {
103     gtk_progress_bar_pulse(GTK_PROGRESS_BAR (m_widget));
104 }
105 
GetDefaultAttributes() const106 wxVisualAttributes wxGauge::GetDefaultAttributes() const
107 {
108     // Visible gauge colours use a different colour state
109     return GetDefaultAttributesFromGTKWidget(m_widget,
110                                              UseGTKStyleBase(),
111                                              GTK_STATE_ACTIVE);
112 
113 }
114 
115 // static
116 wxVisualAttributes
GetClassDefaultAttributes(wxWindowVariant WXUNUSED (variant))117 wxGauge::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
118 {
119     return GetDefaultAttributesFromGTKWidget(gtk_progress_bar_new(),
120                                              false, GTK_STATE_ACTIVE);
121 }
122 
123 #endif // wxUSE_GAUGE
124