Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
Improve Attribute API
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Dec 1, 2019
1 parent 44eca9d commit 82d96e9
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 103 deletions.
4 changes: 1 addition & 3 deletions src/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ impl Analysis {
}

pub fn extra_attrs(&self) -> Vec<Attribute> {
unsafe {
FromGlibPtrContainer::from_glib_none(self.0.extra_attrs)
}
unsafe { FromGlibPtrContainer::from_glib_none(self.0.extra_attrs) }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/attr_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use AttrType;
use glib::translate::from_glib;
use glib::translate::{FromGlibPtrFull, FromGlibPtrNone, Stash, ToGlibPtr};
use pango_sys;
use AttrType;

#[doc(hidden)]
impl<'a> ToGlibPtr<'a, *mut pango_sys::PangoAttrClass> for &'a AttrClass {
Expand Down
76 changes: 0 additions & 76 deletions src/attr_color.rs

This file was deleted.

106 changes: 85 additions & 21 deletions src/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
// Copyright 2017, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use glib::translate::*;
use pango_sys;
use AttrClass;
use Attribute;
use FontDescription;
use Gravity;
use GravityHint;
use Language;
use Rectangle;
use Stretch;
use Style;
use Underline;
use Variant;
use Weight;

use glib::translate::from_glib_full;
use glib::translate::ToGlib;
use glib::translate::ToGlibPtr;

impl Attribute {
#[cfg(any(feature = "v1_38", feature = "dox"))]
pub fn new_background_alpha(alpha: u16) -> Option<Attribute> {
Expand All @@ -32,10 +36,54 @@ impl Attribute {
}
}

// TODO: available at 1.44
// pub fn new_allow_breaks(allow_breaks: bool) -> Option<Attribute> {
// unsafe {
// from_glib_full(pango_sys::pango_attr_allow_breaks_new(
// allow_breaks.to_glib(),
// ))
// }
// }

// TODO: available at 1.44
// pub fn new_insert_hyphens(insert_hyphens: bool) -> Option<Attribute> {
// unsafe {
// from_glib_full(pango_sys::pango_attr_insert_hyphens_new(
// insert_hyphens.to_glib(),
// ))
// }
// }

// TODO: available at 1.44, needs PangoShowFlags
// pub fn new_show(flags: PangoShowFlags) -> Option<Attribute> {
// unsafe {
// from_glib_full(pango_sys::pango_attr_show_new(
// flags.to_glib(),
// ))
// }
// }

pub fn new_language(language: &Language) -> Option<Attribute> {
unsafe {
from_glib_full(pango_sys::pango_attr_language_new(
language.to_glib_none().0,
))
}
}

pub fn new_family(family: &str) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_family_new(family.to_glib_none().0)) }
}

#[cfg(any(feature = "v1_38", feature = "dox"))]
pub fn new_font_features(features: &str) -> Option<Attribute> {
unsafe {
from_glib_full(pango_sys::pango_attr_font_features_new(
features.to_glib_none().0,
))
}
}

#[cfg(any(feature = "v1_38", feature = "dox"))]
pub fn new_foreground_alpha(alpha: u16) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_foreground_alpha_new(alpha)) }
Expand All @@ -61,18 +109,34 @@ impl Attribute {
unsafe { from_glib_full(pango_sys::pango_attr_rise_new(rise)) }
}

pub fn new_scale(scale_factor: f64) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_scale_new(scale_factor)) }
}

pub fn new_size(size: i32) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_size_new(size)) }
}

pub fn new_size_absolute(size: i32) -> Option<Attribute> {
pub fn new_absolute_size(size: i32) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_size_new_absolute(size)) }
}

pub fn new_font_desc(desc: &FontDescription) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_font_desc_new(desc.to_glib_none().0)) }
}

pub fn new_shape(
ink_rect: &Rectangle,
logical_rect: &Rectangle,
) -> Option<Attribute> {
unsafe {
from_glib_full(pango_sys::pango_attr_shape_new(
ink_rect.to_glib_none().0,
logical_rect.to_glib_none().0,
))
}
}

pub fn new_scale(scale_factor: f64) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_scale_new(scale_factor)) }
}

pub fn new_stretch(stretch: Stretch) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_stretch_new(stretch.to_glib())) }
}
Expand Down Expand Up @@ -105,9 +169,9 @@ impl Attribute {
unsafe { from_glib_full(pango_sys::pango_attr_underline_new(underline.to_glib())) }
}

pub fn new_variant(variant: Variant) -> Option<Attribute> {
/*pub fn attr_variant_new(variant: Variant) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_variant_new(variant.to_glib())) }
}
}*/

pub fn new_weight(weight: Weight) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_weight_new(weight.to_glib())) }
Expand All @@ -117,31 +181,31 @@ impl Attribute {
unsafe { from_glib_full((*self.to_glib_none().0).klass) }
}

pub fn get_start_index(&self) -> u32 {
pub fn get_end_index(&self) -> u32 {
unsafe {
let stash = self.to_glib_none();
(*stash.0).start_index
(*stash.0).end_index
}
}

pub fn get_end_index(&self) -> u32 {
pub fn set_end_index(&mut self, index: u32) {
unsafe {
let stash = self.to_glib_none();
(*stash.0).end_index
let stash = self.to_glib_none_mut();
(*stash.0).end_index = index;
}
}

pub fn set_start_index(&mut self, index: u32) {
pub fn get_start_index(&self) -> u32 {
unsafe {
let stash = self.to_glib_none_mut();
(*stash.0).start_index = index;
let stash = self.to_glib_none();
(*stash.0).start_index
}
}

pub fn set_end_index(&mut self, index: u32) {
pub fn set_start_index(&mut self, index: u32) {
unsafe {
let stash = self.to_glib_none_mut();
(*stash.0).end_index = index;
(*stash.0).start_index = index;
}
}
}
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ pub mod analysis;
pub use analysis::Analysis;
pub mod attr_class;
pub use attr_class::AttrClass;
pub mod attr_color;
pub use attr_color::AttrColor;
pub mod attr_iterator;
pub mod attr_list;
pub mod attribute;
Expand Down

0 comments on commit 82d96e9

Please sign in to comment.